blob: 281cf9cbb44c41981b7541408c97fda822fb0061 [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>
Rob Clarkfceffb322016-11-05 11:08:09 -040032#include <drm/drm_print.h>
Brian Starkey935774c2017-03-29 17:42:32 +010033#include <drm/drm_writeback.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"
Noralf Trønnesf02b6042017-11-07 20:13:41 +010037#include "drm_internal.h"
Thierry Redingbe35f942016-04-28 15:19:56 +020038
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010039void __drm_crtc_commit_free(struct kref *kref)
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020040{
41 struct drm_crtc_commit *commit =
42 container_of(kref, struct drm_crtc_commit, ref);
43
44 kfree(commit);
45}
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010046EXPORT_SYMBOL(__drm_crtc_commit_free);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020047
Maarten Lankhorst036ef572015-05-18 10:06:40 +020048/**
49 * drm_atomic_state_default_release -
50 * release memory initialized by drm_atomic_state_init
51 * @state: atomic state
52 *
53 * Free all the memory allocated by drm_atomic_state_init.
Daniel Vetterda6c0592017-12-14 21:30:53 +010054 * This should only be used by drivers which are still subclassing
55 * &drm_atomic_state and haven't switched to &drm_private_state yet.
Maarten Lankhorst036ef572015-05-18 10:06:40 +020056 */
57void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020058{
59 kfree(state->connectors);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020060 kfree(state->crtcs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020061 kfree(state->planes);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -070062 kfree(state->private_objs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020063}
Maarten Lankhorst036ef572015-05-18 10:06:40 +020064EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020065
66/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020067 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020068 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +020069 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020070 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +020071 * Default implementation for filling in a new atomic state.
Daniel Vetterda6c0592017-12-14 21:30:53 +010072 * This should only be used by drivers which are still subclassing
73 * &drm_atomic_state and haven't switched to &drm_private_state yet.
Daniel Vettercc4ceb42014-07-25 21:30:38 +020074 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +020075int
76drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020077{
Chris Wilson08536952016-10-14 13:18:18 +010078 kref_init(&state->ref);
79
Rob Clarkd34f20d2014-12-18 16:01:56 -050080 /* TODO legacy paths should maybe do a better job about
81 * setting this appropriately?
82 */
83 state->allow_modeset = true;
84
Daniel Vettercc4ceb42014-07-25 21:30:38 +020085 state->crtcs = kcalloc(dev->mode_config.num_crtc,
86 sizeof(*state->crtcs), GFP_KERNEL);
87 if (!state->crtcs)
88 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020089 state->planes = kcalloc(dev->mode_config.num_total_plane,
90 sizeof(*state->planes), GFP_KERNEL);
91 if (!state->planes)
92 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020093
94 state->dev = dev;
95
Maarten Lankhorst036ef572015-05-18 10:06:40 +020096 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020097
Maarten Lankhorst036ef572015-05-18 10:06:40 +020098 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020099fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200100 drm_atomic_state_default_release(state);
101 return -ENOMEM;
102}
103EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200104
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200105/**
106 * drm_atomic_state_alloc - allocate atomic state
107 * @dev: DRM device
108 *
109 * This allocates an empty atomic state to track updates.
110 */
111struct drm_atomic_state *
112drm_atomic_state_alloc(struct drm_device *dev)
113{
114 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200115
116 if (!config->funcs->atomic_state_alloc) {
Dawid Kurekac7c7482017-06-15 19:45:56 +0200117 struct drm_atomic_state *state;
118
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200119 state = kzalloc(sizeof(*state), GFP_KERNEL);
120 if (!state)
121 return NULL;
122 if (drm_atomic_state_init(dev, state) < 0) {
123 kfree(state);
124 return NULL;
125 }
126 return state;
127 }
128
129 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200130}
131EXPORT_SYMBOL(drm_atomic_state_alloc);
132
133/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200134 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200135 * @state: atomic state
136 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200137 * Default implementation for clearing atomic state.
Daniel Vetterda6c0592017-12-14 21:30:53 +0100138 * This should only be used by drivers which are still subclassing
139 * &drm_atomic_state and haven't switched to &drm_private_state yet.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200140 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200141void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200142{
143 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100144 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200145 int i;
146
Daniel Vetter17a38d92015-02-22 12:24:16 +0100147 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200148
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100149 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200150 struct drm_connector *connector = state->connectors[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200151
152 if (!connector)
153 continue;
154
Dave Airlied2307de2016-04-27 11:27:39 +1000155 connector->funcs->atomic_destroy_state(connector,
Daniel Vetter63e83c12016-06-02 00:06:32 +0200156 state->connectors[i].state);
157 state->connectors[i].ptr = NULL;
158 state->connectors[i].state = NULL;
Ville Syrjäläf0b408e2018-05-02 21:32:47 +0300159 state->connectors[i].old_state = NULL;
160 state->connectors[i].new_state = NULL;
Thierry Redingad093602017-02-28 15:46:39 +0100161 drm_connector_put(connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200162 }
163
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100164 for (i = 0; i < config->num_crtc; i++) {
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200165 struct drm_crtc *crtc = state->crtcs[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200166
167 if (!crtc)
168 continue;
169
170 crtc->funcs->atomic_destroy_state(crtc,
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200171 state->crtcs[i].state);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200172
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200173 state->crtcs[i].ptr = NULL;
174 state->crtcs[i].state = NULL;
Ville Syrjäläf0b408e2018-05-02 21:32:47 +0300175 state->crtcs[i].old_state = NULL;
176 state->crtcs[i].new_state = NULL;
Leo Li4364bcb2018-10-15 09:46:40 -0400177
178 if (state->crtcs[i].commit) {
179 drm_crtc_commit_put(state->crtcs[i].commit);
180 state->crtcs[i].commit = NULL;
181 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200182 }
183
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100184 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vetterb8b53422016-06-02 00:06:33 +0200185 struct drm_plane *plane = state->planes[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200186
187 if (!plane)
188 continue;
189
190 plane->funcs->atomic_destroy_state(plane,
Daniel Vetterb8b53422016-06-02 00:06:33 +0200191 state->planes[i].state);
192 state->planes[i].ptr = NULL;
193 state->planes[i].state = NULL;
Ville Syrjäläf0b408e2018-05-02 21:32:47 +0300194 state->planes[i].old_state = NULL;
195 state->planes[i].new_state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200196 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700197
198 for (i = 0; i < state->num_private_objs; i++) {
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300199 struct drm_private_obj *obj = state->private_objs[i].ptr;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700200
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300201 obj->funcs->atomic_destroy_state(obj,
202 state->private_objs[i].state);
203 state->private_objs[i].ptr = NULL;
204 state->private_objs[i].state = NULL;
Ville Syrjäläb5cb2e52018-05-02 21:32:47 +0300205 state->private_objs[i].old_state = NULL;
206 state->private_objs[i].new_state = NULL;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700207 }
208 state->num_private_objs = 0;
209
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +0200210 if (state->fake_commit) {
211 drm_crtc_commit_put(state->fake_commit);
212 state->fake_commit = NULL;
213 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200214}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200215EXPORT_SYMBOL(drm_atomic_state_default_clear);
216
217/**
218 * drm_atomic_state_clear - clear state object
219 * @state: atomic state
220 *
221 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
222 * all locks. So someone else could sneak in and change the current modeset
223 * configuration. Which means that all the state assembled in @state is no
224 * longer an atomic update to the current state, but to some arbitrary earlier
Daniel Vetterd5745282017-01-25 07:26:45 +0100225 * state. Which could break assumptions the driver's
226 * &drm_mode_config_funcs.atomic_check likely relies on.
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200227 *
228 * Hence we must clear all cached state and completely start over, using this
229 * function.
230 */
231void drm_atomic_state_clear(struct drm_atomic_state *state)
232{
233 struct drm_device *dev = state->dev;
234 struct drm_mode_config *config = &dev->mode_config;
235
236 if (config->funcs->atomic_state_clear)
237 config->funcs->atomic_state_clear(state);
238 else
239 drm_atomic_state_default_clear(state);
240}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200241EXPORT_SYMBOL(drm_atomic_state_clear);
242
243/**
Chris Wilson08536952016-10-14 13:18:18 +0100244 * __drm_atomic_state_free - free all memory for an atomic state
245 * @ref: This atomic state to deallocate
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200246 *
247 * This frees all memory associated with an atomic state, including all the
248 * per-object state for planes, crtcs and connectors.
249 */
Chris Wilson08536952016-10-14 13:18:18 +0100250void __drm_atomic_state_free(struct kref *ref)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200251{
Chris Wilson08536952016-10-14 13:18:18 +0100252 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
253 struct drm_mode_config *config = &state->dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200254
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200255 drm_atomic_state_clear(state);
256
Daniel Vetter17a38d92015-02-22 12:24:16 +0100257 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200258
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200259 if (config->funcs->atomic_state_free) {
260 config->funcs->atomic_state_free(state);
261 } else {
262 drm_atomic_state_default_release(state);
263 kfree(state);
264 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200265}
Chris Wilson08536952016-10-14 13:18:18 +0100266EXPORT_SYMBOL(__drm_atomic_state_free);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200267
268/**
269 * drm_atomic_get_crtc_state - get crtc state
270 * @state: global atomic state object
271 * @crtc: crtc to get state object for
272 *
273 * This function returns the crtc state for the given crtc, allocating it if
274 * needed. It will also grab the relevant crtc lock to make sure that the state
275 * is consistent.
276 *
277 * Returns:
278 *
279 * Either the allocated state or the error code encoded into the pointer. When
280 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
281 * entire atomic sequence must be restarted. All other errors are fatal.
282 */
283struct drm_crtc_state *
284drm_atomic_get_crtc_state(struct drm_atomic_state *state,
285 struct drm_crtc *crtc)
286{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200287 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200288 struct drm_crtc_state *crtc_state;
289
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200290 WARN_ON(!state->acquire_ctx);
291
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200292 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
293 if (crtc_state)
294 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200295
296 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
297 if (ret)
298 return ERR_PTR(ret);
299
300 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
301 if (!crtc_state)
302 return ERR_PTR(-ENOMEM);
303
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200304 state->crtcs[index].state = crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100305 state->crtcs[index].old_state = crtc->state;
306 state->crtcs[index].new_state = crtc_state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200307 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200308 crtc_state->state = state;
309
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200310 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
311 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200312
313 return crtc_state;
314}
315EXPORT_SYMBOL(drm_atomic_get_crtc_state);
316
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900317static void set_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200318 struct drm_crtc *crtc, s32 __user *fence_ptr)
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900319{
320 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
321}
322
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200323static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900324 struct drm_crtc *crtc)
325{
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200326 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900327
328 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
329 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
330
331 return fence_ptr;
332}
333
Brian Starkeyb13cc8d2017-03-29 17:42:33 +0100334static int set_out_fence_for_connector(struct drm_atomic_state *state,
335 struct drm_connector *connector,
336 s32 __user *fence_ptr)
337{
338 unsigned int index = drm_connector_index(connector);
339
340 if (!fence_ptr)
341 return 0;
342
343 if (put_user(-1, fence_ptr))
344 return -EFAULT;
345
346 state->connectors[index].out_fence_ptr = fence_ptr;
347
348 return 0;
349}
350
351static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
352 struct drm_connector *connector)
353{
354 unsigned int index = drm_connector_index(connector);
355 s32 __user *fence_ptr;
356
357 fence_ptr = state->connectors[index].out_fence_ptr;
358 state->connectors[index].out_fence_ptr = NULL;
359
360 return fence_ptr;
361}
362
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200363/**
Daniel Stone819364d2015-05-26 14:36:48 +0100364 * drm_atomic_set_mode_for_crtc - set mode for CRTC
365 * @state: the CRTC whose incoming state to update
366 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
367 *
Dhinakaran Pandiyancbef9092017-01-30 22:18:38 -0800368 * Set a mode (originating from the kernel) on the desired CRTC state and update
369 * the enable property.
Daniel Stone819364d2015-05-26 14:36:48 +0100370 *
371 * RETURNS:
372 * Zero on success, error code on failure. Cannot return -EDEADLK.
373 */
374int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
Ville Syrjälä91110a42017-05-18 22:38:36 +0300375 const struct drm_display_mode *mode)
Daniel Stone819364d2015-05-26 14:36:48 +0100376{
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300377 struct drm_crtc *crtc = state->crtc;
Daniel Stone99cf4a22015-05-25 19:11:51 +0100378 struct drm_mode_modeinfo umode;
379
Daniel Stone819364d2015-05-26 14:36:48 +0100380 /* Early return for no change. */
381 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
382 return 0;
383
Thierry Reding6472e502017-02-28 15:46:42 +0100384 drm_property_blob_put(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100385 state->mode_blob = NULL;
386
Daniel Stone819364d2015-05-26 14:36:48 +0100387 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100388 drm_mode_convert_to_umode(&umode, mode);
389 state->mode_blob =
390 drm_property_create_blob(state->crtc->dev,
391 sizeof(umode),
392 &umode);
393 if (IS_ERR(state->mode_blob))
394 return PTR_ERR(state->mode_blob);
395
Daniel Stone819364d2015-05-26 14:36:48 +0100396 drm_mode_copy(&state->mode, mode);
397 state->enable = true;
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300398 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
399 mode->name, crtc->base.id, crtc->name, state);
Daniel Stone819364d2015-05-26 14:36:48 +0100400 } else {
401 memset(&state->mode, 0, sizeof(state->mode));
402 state->enable = false;
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300403 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
404 crtc->base.id, crtc->name, state);
Daniel Stone819364d2015-05-26 14:36:48 +0100405 }
406
407 return 0;
408}
409EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
410
Daniel Stone819364d2015-05-26 14:36:48 +0100411/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100412 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
413 * @state: the CRTC whose incoming state to update
414 * @blob: pointer to blob property to use for mode
415 *
416 * Set a mode (originating from a blob property) on the desired CRTC state.
417 * This function will take a reference on the blob property for the CRTC state,
418 * and release the reference held on the state's existing mode property, if any
419 * was set.
420 *
421 * RETURNS:
422 * Zero on success, error code on failure. Cannot return -EDEADLK.
423 */
424int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
425 struct drm_property_blob *blob)
426{
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300427 struct drm_crtc *crtc = state->crtc;
428
Daniel Stone955f3c32015-05-25 19:11:52 +0100429 if (blob == state->mode_blob)
430 return 0;
431
Thierry Reding6472e502017-02-28 15:46:42 +0100432 drm_property_blob_put(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100433 state->mode_blob = NULL;
434
Tomi Valkeinen67098872016-05-31 15:03:17 +0300435 memset(&state->mode, 0, sizeof(state->mode));
436
Daniel Stone955f3c32015-05-25 19:11:52 +0100437 if (blob) {
Ville Syrjälä6ab0edf2018-06-11 22:34:02 +0300438 int ret;
439
440 if (blob->length != sizeof(struct drm_mode_modeinfo)) {
441 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
442 crtc->base.id, crtc->name,
443 blob->length);
Daniel Stone955f3c32015-05-25 19:11:52 +0100444 return -EINVAL;
Ville Syrjälä6ab0edf2018-06-11 22:34:02 +0300445 }
446
447 ret = drm_mode_convert_umode(crtc->dev,
448 &state->mode, blob->data);
449 if (ret) {
450 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
451 crtc->base.id, crtc->name,
452 ret, drm_get_mode_status_name(state->mode.status));
453 drm_mode_debug_printmodeline(&state->mode);
454 return -EINVAL;
455 }
Daniel Stone955f3c32015-05-25 19:11:52 +0100456
Thierry Reding6472e502017-02-28 15:46:42 +0100457 state->mode_blob = drm_property_blob_get(blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100458 state->enable = true;
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300459 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
460 state->mode.name, crtc->base.id, crtc->name,
461 state);
Daniel Stone955f3c32015-05-25 19:11:52 +0100462 } else {
Daniel Stone955f3c32015-05-25 19:11:52 +0100463 state->enable = false;
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300464 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
465 crtc->base.id, crtc->name, state);
Daniel Stone955f3c32015-05-25 19:11:52 +0100466 }
467
468 return 0;
469}
470EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
471
Ville Syrjäläd2a24ed2018-03-15 17:22:41 +0200472/**
473 * drm_atomic_replace_property_blob_from_id - lookup the new blob and replace the old one with it
474 * @dev: DRM device
475 * @blob: a pointer to the member blob to be replaced
476 * @blob_id: ID of the new blob
477 * @expected_size: total expected size of the blob data (in bytes)
478 * @expected_elem_size: expected element size of the blob data (in bytes)
479 * @replaced: did the blob get replaced?
480 *
481 * Replace @blob with another blob with the ID @blob_id. If @blob_id is zero
482 * @blob becomes NULL.
483 *
484 * If @expected_size is positive the new blob length is expected to be equal
485 * to @expected_size bytes. If @expected_elem_size is positive the new blob
486 * length is expected to be a multiple of @expected_elem_size bytes. Otherwise
487 * an error is returned.
488 *
489 * @replaced will indicate to the caller whether the blob was replaced or not.
490 * If the old and new blobs were in fact the same blob @replaced will be false
491 * otherwise it will be true.
492 *
493 * RETURNS:
494 * Zero on success, error code on failure.
495 */
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000496static int
Jyri Sarhadafee602017-04-21 12:51:13 +0300497drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000498 struct drm_property_blob **blob,
499 uint64_t blob_id,
500 ssize_t expected_size,
Ville Syrjäläd2a24ed2018-03-15 17:22:41 +0200501 ssize_t expected_elem_size,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000502 bool *replaced)
503{
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000504 struct drm_property_blob *new_blob = NULL;
505
506 if (blob_id != 0) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300507 new_blob = drm_property_lookup_blob(dev, blob_id);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000508 if (new_blob == NULL)
509 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100510
Ville Syrjäläd2a24ed2018-03-15 17:22:41 +0200511 if (expected_size > 0 &&
512 new_blob->length != expected_size) {
513 drm_property_blob_put(new_blob);
514 return -EINVAL;
515 }
516 if (expected_elem_size > 0 &&
517 new_blob->length % expected_elem_size != 0) {
Thierry Reding6472e502017-02-28 15:46:42 +0100518 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000519 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100520 }
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000521 }
522
Peter Rosin5f057ff2017-07-13 18:25:25 +0200523 *replaced |= drm_property_replace_blob(blob, new_blob);
Thierry Reding6472e502017-02-28 15:46:42 +0100524 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000525
526 return 0;
527}
528
529/**
Rob Clark40ecc692014-12-18 16:01:46 -0500530 * drm_atomic_crtc_set_property - set property on CRTC
531 * @crtc: the drm CRTC to set a property on
532 * @state: the state object to update with the new property value
533 * @property: the property to set
534 * @val: the new property value
535 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100536 * This function handles generic/core properties and calls out to driver's
537 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
538 * consistent behavior you must call this function rather than the driver hook
539 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500540 *
541 * RETURNS:
542 * Zero on success, error code on failure
543 */
544int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
545 struct drm_crtc_state *state, struct drm_property *property,
546 uint64_t val)
547{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100548 struct drm_device *dev = crtc->dev;
549 struct drm_mode_config *config = &dev->mode_config;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000550 bool replaced = false;
Daniel Stone955f3c32015-05-25 19:11:52 +0100551 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100552
Daniel Stone27798362015-03-19 04:33:26 +0000553 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100554 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100555 else if (property == config->prop_mode_id) {
556 struct drm_property_blob *mode =
557 drm_property_lookup_blob(dev, val);
558 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Thierry Reding6472e502017-02-28 15:46:42 +0100559 drm_property_blob_put(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100560 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000561 } else if (property == config->degamma_lut_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300562 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000563 &state->degamma_lut,
564 val,
Ville Syrjäläd2a24ed2018-03-15 17:22:41 +0200565 -1, sizeof(struct drm_color_lut),
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000566 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200567 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000568 return ret;
569 } else if (property == config->ctm_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300570 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000571 &state->ctm,
572 val,
Ville Syrjäläd2a24ed2018-03-15 17:22:41 +0200573 sizeof(struct drm_color_ctm), -1,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000574 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200575 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000576 return ret;
577 } else if (property == config->gamma_lut_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300578 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000579 &state->gamma_lut,
580 val,
Ville Syrjäläd2a24ed2018-03-15 17:22:41 +0200581 -1, sizeof(struct drm_color_lut),
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000582 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200583 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000584 return ret;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900585 } else if (property == config->prop_out_fence_ptr) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200586 s32 __user *fence_ptr = u64_to_user_ptr(val);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900587
588 if (!fence_ptr)
589 return 0;
590
591 if (put_user(-1, fence_ptr))
592 return -EFAULT;
593
594 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300595 } else if (crtc->funcs->atomic_set_property) {
Rob Clark40ecc692014-12-18 16:01:46 -0500596 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300597 } else {
598 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
599 crtc->base.id, crtc->name,
600 property->base.id, property->name);
Daniel Stone27798362015-03-19 04:33:26 +0000601 return -EINVAL;
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300602 }
Daniel Stone27798362015-03-19 04:33:26 +0000603
604 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500605}
606EXPORT_SYMBOL(drm_atomic_crtc_set_property);
607
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100608/**
609 * drm_atomic_crtc_get_property - get property value from CRTC state
610 * @crtc: the drm CRTC to set a property on
611 * @state: the state object to get the property value from
612 * @property: the property to set
613 * @val: return location for the property value
614 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100615 * This function handles generic/core properties and calls out to driver's
616 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
617 * consistent behavior you must call this function rather than the driver hook
618 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100619 *
620 * RETURNS:
621 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500622 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700623static int
624drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500625 const struct drm_crtc_state *state,
626 struct drm_property *property, uint64_t *val)
627{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000628 struct drm_device *dev = crtc->dev;
629 struct drm_mode_config *config = &dev->mode_config;
630
631 if (property == config->prop_active)
632 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100633 else if (property == config->prop_mode_id)
634 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000635 else if (property == config->degamma_lut_property)
636 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
637 else if (property == config->ctm_property)
638 *val = (state->ctm) ? state->ctm->base.id : 0;
639 else if (property == config->gamma_lut_property)
640 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900641 else if (property == config->prop_out_fence_ptr)
642 *val = 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000643 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500644 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000645 else
646 return -EINVAL;
647
648 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500649}
Rob Clarkac9c9252014-12-18 16:01:47 -0500650
651/**
Rob Clark5e743732014-12-18 16:01:51 -0500652 * drm_atomic_crtc_check - check crtc state
653 * @crtc: crtc to check
654 * @state: crtc state to check
655 *
656 * Provides core sanity checks for crtc state.
657 *
658 * RETURNS:
659 * Zero on success, error code on failure
660 */
661static int drm_atomic_crtc_check(struct drm_crtc *crtc,
662 struct drm_crtc_state *state)
663{
664 /* NOTE: we explicitly don't enforce constraints such as primary
665 * layer covering entire screen, since that is something we want
666 * to allow (on hw that supports it). For hw that does not, it
667 * should be checked in driver's crtc->atomic_check() vfunc.
668 *
669 * TODO: Add generic modeset state checks once we support those.
670 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100671
672 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200673 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
674 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100675 return -EINVAL;
676 }
677
Daniel Stone99cf4a22015-05-25 19:11:51 +0100678 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
679 * as this is a kernel-internal detail that userspace should never
680 * be able to trigger. */
681 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
682 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200683 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
684 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100685 return -EINVAL;
686 }
687
688 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
689 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200690 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
691 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100692 return -EINVAL;
693 }
694
Daniel Vetter4cba6852015-12-08 09:49:20 +0100695 /*
696 * Reject event generation for when a CRTC is off and stays off.
697 * It wouldn't be hard to implement this, but userspace has a track
698 * record of happily burning through 100% cpu (or worse, crash) when the
699 * display pipe is suspended. To avoid all that fun just reject updates
700 * that ask for events since likely that indicates a bug in the
701 * compositor's drawing loop. This is consistent with the vblank IOCTL
702 * and legacy page_flip IOCTL which also reject service on a disabled
703 * pipe.
704 */
705 if (state->event && !state->active && !crtc->state->active) {
Russell King6ac7c542017-02-13 12:27:03 +0000706 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
707 crtc->base.id, crtc->name);
Daniel Vetter4cba6852015-12-08 09:49:20 +0100708 return -EINVAL;
709 }
710
Rob Clark5e743732014-12-18 16:01:51 -0500711 return 0;
712}
713
Rob Clarkfceffb322016-11-05 11:08:09 -0400714static void drm_atomic_crtc_print_state(struct drm_printer *p,
715 const struct drm_crtc_state *state)
716{
717 struct drm_crtc *crtc = state->crtc;
718
719 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
720 drm_printf(p, "\tenable=%d\n", state->enable);
721 drm_printf(p, "\tactive=%d\n", state->active);
722 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
723 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
724 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
725 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
726 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
727 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
728 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
729 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
730 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
731
732 if (crtc->funcs->atomic_print_state)
733 crtc->funcs->atomic_print_state(p, state);
734}
735
Rob Clark5e743732014-12-18 16:01:51 -0500736/**
Brian Starkey935774c2017-03-29 17:42:32 +0100737 * drm_atomic_connector_check - check connector state
738 * @connector: connector to check
739 * @state: connector state to check
740 *
741 * Provides core sanity checks for connector state.
742 *
743 * RETURNS:
744 * Zero on success, error code on failure
745 */
746static int drm_atomic_connector_check(struct drm_connector *connector,
747 struct drm_connector_state *state)
748{
749 struct drm_crtc_state *crtc_state;
750 struct drm_writeback_job *writeback_job = state->writeback_job;
751
752 if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
753 return 0;
754
755 if (writeback_job->fb && !state->crtc) {
756 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
757 connector->base.id, connector->name);
758 return -EINVAL;
759 }
760
761 if (state->crtc)
762 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
763 state->crtc);
764
765 if (writeback_job->fb && !crtc_state->active) {
766 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
767 connector->base.id, connector->name,
768 state->crtc->base.id);
769 return -EINVAL;
770 }
771
Brian Starkeyb13cc8d2017-03-29 17:42:33 +0100772 if (writeback_job->out_fence && !writeback_job->fb) {
773 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
774 connector->base.id, connector->name);
775 return -EINVAL;
776 }
777
Brian Starkey935774c2017-03-29 17:42:32 +0100778 return 0;
779}
780
781/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200782 * drm_atomic_get_plane_state - get plane state
783 * @state: global atomic state object
784 * @plane: plane to get state object for
785 *
786 * This function returns the plane state for the given plane, allocating it if
787 * needed. It will also grab the relevant plane lock to make sure that the state
788 * is consistent.
789 *
790 * Returns:
791 *
792 * Either the allocated state or the error code encoded into the pointer. When
793 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
794 * entire atomic sequence must be restarted. All other errors are fatal.
795 */
796struct drm_plane_state *
797drm_atomic_get_plane_state(struct drm_atomic_state *state,
798 struct drm_plane *plane)
799{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200800 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200801 struct drm_plane_state *plane_state;
802
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200803 WARN_ON(!state->acquire_ctx);
804
Ville Syrjäläe00fb852018-05-25 21:50:45 +0300805 /* the legacy pointers should never be set */
806 WARN_ON(plane->fb);
807 WARN_ON(plane->old_fb);
808 WARN_ON(plane->crtc);
809
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200810 plane_state = drm_atomic_get_existing_plane_state(state, plane);
811 if (plane_state)
812 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200813
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100814 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200815 if (ret)
816 return ERR_PTR(ret);
817
818 plane_state = plane->funcs->atomic_duplicate_state(plane);
819 if (!plane_state)
820 return ERR_PTR(-ENOMEM);
821
Daniel Vetterb8b53422016-06-02 00:06:33 +0200822 state->planes[index].state = plane_state;
823 state->planes[index].ptr = plane;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100824 state->planes[index].old_state = plane->state;
825 state->planes[index].new_state = plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200826 plane_state->state = state;
827
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200828 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
829 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200830
831 if (plane_state->crtc) {
832 struct drm_crtc_state *crtc_state;
833
834 crtc_state = drm_atomic_get_crtc_state(state,
835 plane_state->crtc);
836 if (IS_ERR(crtc_state))
837 return ERR_CAST(crtc_state);
838 }
839
840 return plane_state;
841}
842EXPORT_SYMBOL(drm_atomic_get_plane_state);
843
844/**
Rob Clark40ecc692014-12-18 16:01:46 -0500845 * drm_atomic_plane_set_property - set property on plane
846 * @plane: the drm plane to set a property on
847 * @state: the state object to update with the new property value
848 * @property: the property to set
849 * @val: the new property value
850 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100851 * This function handles generic/core properties and calls out to driver's
852 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
853 * consistent behavior you must call this function rather than the driver hook
854 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500855 *
856 * RETURNS:
857 * Zero on success, error code on failure
858 */
Daniel Vettere90271b2017-07-25 10:01:19 +0200859static int drm_atomic_plane_set_property(struct drm_plane *plane,
Rob Clark40ecc692014-12-18 16:01:46 -0500860 struct drm_plane_state *state, struct drm_property *property,
861 uint64_t val)
862{
Rob Clark6b4959f2014-12-18 16:01:53 -0500863 struct drm_device *dev = plane->dev;
864 struct drm_mode_config *config = &dev->mode_config;
865
866 if (property == config->prop_fb_id) {
Keith Packard418da172017-03-14 23:25:07 -0700867 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500868 drm_atomic_set_fb_for_plane(state, fb);
869 if (fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +0100870 drm_framebuffer_put(fb);
Gustavo Padovan96260142016-11-15 22:06:39 +0900871 } else if (property == config->prop_in_fence_fd) {
872 if (state->fence)
873 return -EINVAL;
874
875 if (U642I64(val) == -1)
876 return 0;
877
878 state->fence = sync_file_get_fence(val);
879 if (!state->fence)
880 return -EINVAL;
881
Rob Clark6b4959f2014-12-18 16:01:53 -0500882 } else if (property == config->prop_crtc_id) {
Keith Packard418da172017-03-14 23:25:07 -0700883 struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500884 return drm_atomic_set_crtc_for_plane(state, crtc);
885 } else if (property == config->prop_crtc_x) {
886 state->crtc_x = U642I64(val);
887 } else if (property == config->prop_crtc_y) {
888 state->crtc_y = U642I64(val);
889 } else if (property == config->prop_crtc_w) {
890 state->crtc_w = val;
891 } else if (property == config->prop_crtc_h) {
892 state->crtc_h = val;
893 } else if (property == config->prop_src_x) {
894 state->src_x = val;
895 } else if (property == config->prop_src_y) {
896 state->src_y = val;
897 } else if (property == config->prop_src_w) {
898 state->src_w = val;
899 } else if (property == config->prop_src_h) {
900 state->src_h = val;
Maxime Ripardae0e2822018-04-11 09:39:25 +0200901 } else if (property == plane->alpha_property) {
902 state->alpha = val;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300903 } else if (property == plane->rotation_property) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300904 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
905 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
906 plane->base.id, plane->name, val);
Ville Syrjälä6e0c7c32016-09-26 19:30:47 +0300907 return -EINVAL;
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300908 }
Matt Roper1da30622015-01-21 16:35:40 -0800909 state->rotation = val;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200910 } else if (property == plane->zpos_property) {
911 state->zpos = val;
Jyri Sarha80f690e2018-02-19 22:28:23 +0200912 } else if (property == plane->color_encoding_property) {
913 state->color_encoding = val;
914 } else if (property == plane->color_range_property) {
915 state->color_range = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500916 } else if (plane->funcs->atomic_set_property) {
917 return plane->funcs->atomic_set_property(plane, state,
918 property, val);
919 } else {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +0300920 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
921 plane->base.id, plane->name,
922 property->base.id, property->name);
Rob Clark6b4959f2014-12-18 16:01:53 -0500923 return -EINVAL;
924 }
925
926 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500927}
Rob Clark40ecc692014-12-18 16:01:46 -0500928
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100929/**
930 * drm_atomic_plane_get_property - get property value from plane state
931 * @plane: the drm plane to set a property on
932 * @state: the state object to get the property value from
933 * @property: the property to set
934 * @val: return location for the property value
935 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100936 * This function handles generic/core properties and calls out to driver's
937 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
938 * consistent behavior you must call this function rather than the driver hook
939 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100940 *
941 * RETURNS:
942 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500943 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100944static int
945drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500946 const struct drm_plane_state *state,
947 struct drm_property *property, uint64_t *val)
948{
Rob Clark6b4959f2014-12-18 16:01:53 -0500949 struct drm_device *dev = plane->dev;
950 struct drm_mode_config *config = &dev->mode_config;
951
952 if (property == config->prop_fb_id) {
953 *val = (state->fb) ? state->fb->base.id : 0;
Gustavo Padovan96260142016-11-15 22:06:39 +0900954 } else if (property == config->prop_in_fence_fd) {
955 *val = -1;
Rob Clark6b4959f2014-12-18 16:01:53 -0500956 } else if (property == config->prop_crtc_id) {
957 *val = (state->crtc) ? state->crtc->base.id : 0;
958 } else if (property == config->prop_crtc_x) {
959 *val = I642U64(state->crtc_x);
960 } else if (property == config->prop_crtc_y) {
961 *val = I642U64(state->crtc_y);
962 } else if (property == config->prop_crtc_w) {
963 *val = state->crtc_w;
964 } else if (property == config->prop_crtc_h) {
965 *val = state->crtc_h;
966 } else if (property == config->prop_src_x) {
967 *val = state->src_x;
968 } else if (property == config->prop_src_y) {
969 *val = state->src_y;
970 } else if (property == config->prop_src_w) {
971 *val = state->src_w;
972 } else if (property == config->prop_src_h) {
973 *val = state->src_h;
Maxime Ripardae0e2822018-04-11 09:39:25 +0200974 } else if (property == plane->alpha_property) {
975 *val = state->alpha;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300976 } else if (property == plane->rotation_property) {
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000977 *val = state->rotation;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200978 } else if (property == plane->zpos_property) {
979 *val = state->zpos;
Jyri Sarha80f690e2018-02-19 22:28:23 +0200980 } else if (property == plane->color_encoding_property) {
981 *val = state->color_encoding;
982 } else if (property == plane->color_range_property) {
983 *val = state->color_range;
Rob Clark6b4959f2014-12-18 16:01:53 -0500984 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500985 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500986 } else {
987 return -EINVAL;
988 }
989
990 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500991}
Rob Clarkac9c9252014-12-18 16:01:47 -0500992
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200993static bool
994plane_switching_crtc(struct drm_atomic_state *state,
995 struct drm_plane *plane,
996 struct drm_plane_state *plane_state)
997{
998 if (!plane->state->crtc || !plane_state->crtc)
999 return false;
1000
1001 if (plane->state->crtc == plane_state->crtc)
1002 return false;
1003
1004 /* This could be refined, but currently there's no helper or driver code
1005 * to implement direct switching of active planes nor userspace to take
1006 * advantage of more direct plane switching without the intermediate
1007 * full OFF state.
1008 */
1009 return true;
1010}
1011
Rob Clarkac9c9252014-12-18 16:01:47 -05001012/**
Rob Clark5e743732014-12-18 16:01:51 -05001013 * drm_atomic_plane_check - check plane state
1014 * @plane: plane to check
1015 * @state: plane state to check
1016 *
1017 * Provides core sanity checks for plane state.
1018 *
1019 * RETURNS:
1020 * Zero on success, error code on failure
1021 */
1022static int drm_atomic_plane_check(struct drm_plane *plane,
1023 struct drm_plane_state *state)
1024{
1025 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +02001026 int ret;
Rob Clark5e743732014-12-18 16:01:51 -05001027
1028 /* either *both* CRTC and FB must be set, or neither */
Maarten Lankhorstfa5aaee2018-01-30 11:27:04 +01001029 if (state->crtc && !state->fb) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001030 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
1031 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001032 return -EINVAL;
Maarten Lankhorstfa5aaee2018-01-30 11:27:04 +01001033 } else if (state->fb && !state->crtc) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001034 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
1035 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001036 return -EINVAL;
1037 }
1038
1039 /* if disabled, we don't care about the rest of the state: */
1040 if (!state->crtc)
1041 return 0;
1042
1043 /* Check whether this plane is usable on this CRTC */
1044 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001045 DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
1046 state->crtc->base.id, state->crtc->name,
1047 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001048 return -EINVAL;
1049 }
1050
1051 /* Check whether this plane supports the fb pixel format. */
Ville Syrjälä23163a72017-12-22 21:22:30 +02001052 ret = drm_plane_check_pixel_format(plane, state->fb->format->format,
1053 state->fb->modifier);
Laurent Pinchartead86102015-03-05 02:25:43 +02001054 if (ret) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +00001055 struct drm_format_name_buf format_name;
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001056 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n",
1057 plane->base.id, plane->name,
Ville Syrjälä23163a72017-12-22 21:22:30 +02001058 drm_get_format_name(state->fb->format->format,
1059 &format_name),
1060 state->fb->modifier);
Laurent Pinchartead86102015-03-05 02:25:43 +02001061 return ret;
Rob Clark5e743732014-12-18 16:01:51 -05001062 }
1063
1064 /* Give drivers some help against integer overflows */
1065 if (state->crtc_w > INT_MAX ||
1066 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
1067 state->crtc_h > INT_MAX ||
1068 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001069 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
1070 plane->base.id, plane->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +01001071 state->crtc_w, state->crtc_h,
1072 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -05001073 return -ERANGE;
1074 }
1075
1076 fb_width = state->fb->width << 16;
1077 fb_height = state->fb->height << 16;
1078
1079 /* Make sure source coordinates are inside the fb. */
1080 if (state->src_w > fb_width ||
1081 state->src_x > fb_width - state->src_w ||
1082 state->src_h > fb_height ||
1083 state->src_y > fb_height - state->src_h) {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001084 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
Ville Syrjälä0338f0d2017-11-01 20:35:33 +02001085 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001086 plane->base.id, plane->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +01001087 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
1088 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
1089 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
Ville Syrjälä0338f0d2017-11-01 20:35:33 +02001090 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10,
1091 state->fb->width, state->fb->height);
Rob Clark5e743732014-12-18 16:01:51 -05001092 return -ENOSPC;
1093 }
1094
Daniel Vetterf8aeb412015-08-26 21:49:42 +02001095 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001096 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
1097 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +02001098 return -EINVAL;
1099 }
1100
Rob Clark5e743732014-12-18 16:01:51 -05001101 return 0;
1102}
1103
Rob Clarkfceffb322016-11-05 11:08:09 -04001104static void drm_atomic_plane_print_state(struct drm_printer *p,
1105 const struct drm_plane_state *state)
1106{
1107 struct drm_plane *plane = state->plane;
1108 struct drm_rect src = drm_plane_state_src(state);
1109 struct drm_rect dest = drm_plane_state_dest(state);
1110
1111 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
1112 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1113 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
Noralf Trønnesf02b6042017-11-07 20:13:41 +01001114 if (state->fb)
1115 drm_framebuffer_print_info(p, 2, state->fb);
Rob Clarkfceffb322016-11-05 11:08:09 -04001116 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
1117 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
1118 drm_printf(p, "\trotation=%x\n", state->rotation);
Benjamin Gaignardf8878bb2018-06-05 15:54:01 +02001119 drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
Ville Syrjälä56dbbaf2018-02-19 22:28:46 +02001120 drm_printf(p, "\tcolor-encoding=%s\n",
1121 drm_get_color_encoding_name(state->color_encoding));
1122 drm_printf(p, "\tcolor-range=%s\n",
1123 drm_get_color_range_name(state->color_range));
Rob Clarkfceffb322016-11-05 11:08:09 -04001124
1125 if (plane->funcs->atomic_print_state)
1126 plane->funcs->atomic_print_state(p, state);
1127}
1128
Rob Clark5e743732014-12-18 16:01:51 -05001129/**
Daniel Vetterda6c0592017-12-14 21:30:53 +01001130 * DOC: handling driver private state
1131 *
1132 * Very often the DRM objects exposed to userspace in the atomic modeset api
1133 * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
1134 * underlying hardware. Especially for any kind of shared resources (e.g. shared
1135 * clocks, scaler units, bandwidth and fifo limits shared among a group of
1136 * planes or CRTCs, and so on) it makes sense to model these as independent
1137 * objects. Drivers then need to do similar state tracking and commit ordering for
1138 * such private (since not exposed to userpace) objects as the atomic core and
1139 * helpers already provide for connectors, planes and CRTCs.
1140 *
1141 * To make this easier on drivers the atomic core provides some support to track
1142 * driver private state objects using struct &drm_private_obj, with the
1143 * associated state struct &drm_private_state.
1144 *
1145 * Similar to userspace-exposed objects, private state structures can be
1146 * acquired by calling drm_atomic_get_private_obj_state(). Since this function
1147 * does not take care of locking, drivers should wrap it for each type of
1148 * private state object they have with the required call to drm_modeset_lock()
1149 * for the corresponding &drm_modeset_lock.
1150 *
1151 * All private state structures contained in a &drm_atomic_state update can be
1152 * iterated using for_each_oldnew_private_obj_in_state(),
1153 * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
1154 * Drivers are recommended to wrap these for each type of driver private state
1155 * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
1156 * least if they want to iterate over all objects of a given type.
1157 *
1158 * An earlier way to handle driver private state was by subclassing struct
1159 * &drm_atomic_state. But since that encourages non-standard ways to implement
1160 * the check/commit split atomic requires (by using e.g. "check and rollback or
1161 * commit instead" of "duplicate state, check, then either commit or release
1162 * duplicated state) it is deprecated in favour of using &drm_private_state.
1163 */
1164
1165/**
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001166 * drm_atomic_private_obj_init - initialize private object
1167 * @obj: private object
1168 * @state: initial private object state
1169 * @funcs: pointer to the struct of function pointers that identify the object
1170 * type
1171 *
1172 * Initialize the private object, which can be embedded into any
1173 * driver private object that needs its own atomic state.
1174 */
1175void
1176drm_atomic_private_obj_init(struct drm_private_obj *obj,
1177 struct drm_private_state *state,
1178 const struct drm_private_state_funcs *funcs)
1179{
1180 memset(obj, 0, sizeof(*obj));
1181
1182 obj->state = state;
1183 obj->funcs = funcs;
1184}
1185EXPORT_SYMBOL(drm_atomic_private_obj_init);
1186
1187/**
1188 * drm_atomic_private_obj_fini - finalize private object
1189 * @obj: private object
1190 *
1191 * Finalize the private object.
1192 */
1193void
1194drm_atomic_private_obj_fini(struct drm_private_obj *obj)
1195{
1196 obj->funcs->atomic_destroy_state(obj, obj->state);
1197}
1198EXPORT_SYMBOL(drm_atomic_private_obj_fini);
1199
1200/**
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001201 * drm_atomic_get_private_obj_state - get private object state
1202 * @state: global atomic state
1203 * @obj: private object to get the state for
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001204 *
1205 * This function returns the private object state for the given private object,
1206 * allocating the state if needed. It does not grab any locks as the caller is
1207 * expected to care of any required locking.
1208 *
1209 * RETURNS:
1210 *
1211 * Either the allocated state or the error code encoded into a pointer.
1212 */
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001213struct drm_private_state *
1214drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
1215 struct drm_private_obj *obj)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001216{
1217 int index, num_objs, i;
1218 size_t size;
1219 struct __drm_private_objs_state *arr;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001220 struct drm_private_state *obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001221
1222 for (i = 0; i < state->num_private_objs; i++)
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001223 if (obj == state->private_objs[i].ptr)
1224 return state->private_objs[i].state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001225
1226 num_objs = state->num_private_objs + 1;
1227 size = sizeof(*state->private_objs) * num_objs;
1228 arr = krealloc(state->private_objs, size, GFP_KERNEL);
1229 if (!arr)
1230 return ERR_PTR(-ENOMEM);
1231
1232 state->private_objs = arr;
1233 index = state->num_private_objs;
1234 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1235
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001236 obj_state = obj->funcs->atomic_duplicate_state(obj);
1237 if (!obj_state)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001238 return ERR_PTR(-ENOMEM);
1239
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001240 state->private_objs[index].state = obj_state;
1241 state->private_objs[index].old_state = obj->state;
1242 state->private_objs[index].new_state = obj_state;
1243 state->private_objs[index].ptr = obj;
Alexandru Gheorghee89ea352018-05-30 18:30:52 +01001244 obj_state->state = state;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001245
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001246 state->num_private_objs = num_objs;
1247
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001248 DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
1249 obj, obj_state, state);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001250
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001251 return obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001252}
1253EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1254
1255/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001256 * drm_atomic_get_connector_state - get connector state
1257 * @state: global atomic state object
1258 * @connector: connector to get state object for
1259 *
1260 * This function returns the connector state for the given connector,
1261 * allocating it if needed. It will also grab the relevant connector lock to
1262 * make sure that the state is consistent.
1263 *
1264 * Returns:
1265 *
1266 * Either the allocated state or the error code encoded into the pointer. When
1267 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1268 * entire atomic sequence must be restarted. All other errors are fatal.
1269 */
1270struct drm_connector_state *
1271drm_atomic_get_connector_state(struct drm_atomic_state *state,
1272 struct drm_connector *connector)
1273{
1274 int ret, index;
1275 struct drm_mode_config *config = &connector->dev->mode_config;
1276 struct drm_connector_state *connector_state;
1277
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +02001278 WARN_ON(!state->acquire_ctx);
1279
Daniel Vetterc7eb76f2014-11-19 18:38:06 +01001280 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1281 if (ret)
1282 return ERR_PTR(ret);
1283
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001284 index = drm_connector_index(connector);
1285
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001286 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +02001287 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001288 int alloc = max(index + 1, config->num_connector);
1289
1290 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1291 if (!c)
1292 return ERR_PTR(-ENOMEM);
1293
1294 state->connectors = c;
1295 memset(&state->connectors[state->num_connector], 0,
1296 sizeof(*state->connectors) * (alloc - state->num_connector));
1297
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001298 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001299 }
1300
Daniel Vetter63e83c12016-06-02 00:06:32 +02001301 if (state->connectors[index].state)
1302 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001303
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001304 connector_state = connector->funcs->atomic_duplicate_state(connector);
1305 if (!connector_state)
1306 return ERR_PTR(-ENOMEM);
1307
Thierry Redingad093602017-02-28 15:46:39 +01001308 drm_connector_get(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +02001309 state->connectors[index].state = connector_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01001310 state->connectors[index].old_state = connector->state;
1311 state->connectors[index].new_state = connector_state;
Daniel Vetter63e83c12016-06-02 00:06:32 +02001312 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001313 connector_state->state = state;
1314
Russell King6ac7c542017-02-13 12:27:03 +00001315 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1316 connector->base.id, connector->name,
1317 connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001318
1319 if (connector_state->crtc) {
1320 struct drm_crtc_state *crtc_state;
1321
1322 crtc_state = drm_atomic_get_crtc_state(state,
1323 connector_state->crtc);
1324 if (IS_ERR(crtc_state))
1325 return ERR_CAST(crtc_state);
1326 }
1327
1328 return connector_state;
1329}
1330EXPORT_SYMBOL(drm_atomic_get_connector_state);
1331
1332/**
Rob Clark40ecc692014-12-18 16:01:46 -05001333 * drm_atomic_connector_set_property - set property on connector.
1334 * @connector: the drm connector to set a property on
1335 * @state: the state object to update with the new property value
1336 * @property: the property to set
1337 * @val: the new property value
1338 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001339 * This function handles generic/core properties and calls out to driver's
1340 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1341 * consistent behavior you must call this function rather than the driver hook
1342 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -05001343 *
1344 * RETURNS:
1345 * Zero on success, error code on failure
1346 */
Daniel Vetter482b0e32017-07-25 10:01:20 +02001347static int drm_atomic_connector_set_property(struct drm_connector *connector,
Rob Clark40ecc692014-12-18 16:01:46 -05001348 struct drm_connector_state *state, struct drm_property *property,
1349 uint64_t val)
1350{
1351 struct drm_device *dev = connector->dev;
1352 struct drm_mode_config *config = &dev->mode_config;
1353
Rob Clarkae16c592014-12-18 16:01:54 -05001354 if (property == config->prop_crtc_id) {
Keith Packard418da172017-03-14 23:25:07 -07001355 struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
Rob Clarkae16c592014-12-18 16:01:54 -05001356 return drm_atomic_set_crtc_for_connector(state, crtc);
1357 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -05001358 /* setting DPMS property requires special handling, which
1359 * is done in legacy setprop path for us. Disallow (for
1360 * now?) atomic writes to DPMS property:
1361 */
1362 return -EINVAL;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001363 } else if (property == config->tv_select_subconnector_property) {
1364 state->tv.subconnector = val;
1365 } else if (property == config->tv_left_margin_property) {
1366 state->tv.margins.left = val;
1367 } else if (property == config->tv_right_margin_property) {
1368 state->tv.margins.right = val;
1369 } else if (property == config->tv_top_margin_property) {
1370 state->tv.margins.top = val;
1371 } else if (property == config->tv_bottom_margin_property) {
1372 state->tv.margins.bottom = val;
1373 } else if (property == config->tv_mode_property) {
1374 state->tv.mode = val;
1375 } else if (property == config->tv_brightness_property) {
1376 state->tv.brightness = val;
1377 } else if (property == config->tv_contrast_property) {
1378 state->tv.contrast = val;
1379 } else if (property == config->tv_flicker_reduction_property) {
1380 state->tv.flicker_reduction = val;
1381 } else if (property == config->tv_overscan_property) {
1382 state->tv.overscan = val;
1383 } else if (property == config->tv_saturation_property) {
1384 state->tv.saturation = val;
1385 } else if (property == config->tv_hue_property) {
1386 state->tv.hue = val;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001387 } else if (property == config->link_status_property) {
1388 /* Never downgrade from GOOD to BAD on userspace's request here,
1389 * only hw issues can do that.
1390 *
1391 * For an atomic property the userspace doesn't need to be able
1392 * to understand all the properties, but needs to be able to
1393 * restore the state it wants on VT switch. So if the userspace
1394 * tries to change the link_status from GOOD to BAD, driver
1395 * silently rejects it and returns a 0. This prevents userspace
1396 * from accidently breaking the display when it restores the
1397 * state.
1398 */
1399 if (state->link_status != DRM_LINK_STATUS_GOOD)
1400 state->link_status = val;
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02001401 } else if (property == config->aspect_ratio_property) {
1402 state->picture_aspect_ratio = val;
Stanislav Lisovskiy50525c32018-05-15 16:59:27 +03001403 } else if (property == config->content_type_property) {
1404 state->content_type = val;
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001405 } else if (property == connector->scaling_mode_property) {
1406 state->scaling_mode = val;
Sean Paul24557862018-01-08 14:55:37 -05001407 } else if (property == connector->content_protection_property) {
1408 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
1409 DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
1410 return -EINVAL;
1411 }
1412 state->content_protection = val;
Brian Starkey935774c2017-03-29 17:42:32 +01001413 } else if (property == config->writeback_fb_id_property) {
1414 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
1415 int ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
1416 if (fb)
1417 drm_framebuffer_put(fb);
1418 return ret;
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01001419 } else if (property == config->writeback_out_fence_ptr_property) {
1420 s32 __user *fence_ptr = u64_to_user_ptr(val);
1421
1422 return set_out_fence_for_connector(state->state, connector,
1423 fence_ptr);
Rob Clark40ecc692014-12-18 16:01:46 -05001424 } else if (connector->funcs->atomic_set_property) {
1425 return connector->funcs->atomic_set_property(connector,
1426 state, property, val);
1427 } else {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001428 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
1429 connector->base.id, connector->name,
1430 property->base.id, property->name);
Rob Clark40ecc692014-12-18 16:01:46 -05001431 return -EINVAL;
1432 }
Boris Brezillon299a16b2016-12-02 14:48:09 +01001433
1434 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -05001435}
Rob Clark40ecc692014-12-18 16:01:46 -05001436
Rob Clarkfceffb322016-11-05 11:08:09 -04001437static void drm_atomic_connector_print_state(struct drm_printer *p,
1438 const struct drm_connector_state *state)
1439{
1440 struct drm_connector *connector = state->connector;
1441
1442 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1443 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1444
Brian Starkey8cbc5ca2017-11-02 16:49:51 +00001445 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1446 if (state->writeback_job && state->writeback_job->fb)
1447 drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
1448
Rob Clarkfceffb322016-11-05 11:08:09 -04001449 if (connector->funcs->atomic_print_state)
1450 connector->funcs->atomic_print_state(p, state);
1451}
1452
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001453/**
1454 * drm_atomic_connector_get_property - get property value from connector state
1455 * @connector: the drm connector to set a property on
1456 * @state: the state object to get the property value from
1457 * @property: the property to set
1458 * @val: return location for the property value
1459 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001460 * This function handles generic/core properties and calls out to driver's
1461 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1462 * consistent behavior you must call this function rather than the driver hook
1463 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001464 *
1465 * RETURNS:
1466 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -05001467 */
Daniel Vettera97df1c2014-12-18 22:49:02 +01001468static int
1469drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -05001470 const struct drm_connector_state *state,
1471 struct drm_property *property, uint64_t *val)
1472{
1473 struct drm_device *dev = connector->dev;
1474 struct drm_mode_config *config = &dev->mode_config;
1475
Rob Clarkae16c592014-12-18 16:01:54 -05001476 if (property == config->prop_crtc_id) {
1477 *val = (state->crtc) ? state->crtc->base.id : 0;
1478 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -05001479 *val = connector->dpms;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001480 } else if (property == config->tv_select_subconnector_property) {
1481 *val = state->tv.subconnector;
1482 } else if (property == config->tv_left_margin_property) {
1483 *val = state->tv.margins.left;
1484 } else if (property == config->tv_right_margin_property) {
1485 *val = state->tv.margins.right;
1486 } else if (property == config->tv_top_margin_property) {
1487 *val = state->tv.margins.top;
1488 } else if (property == config->tv_bottom_margin_property) {
1489 *val = state->tv.margins.bottom;
1490 } else if (property == config->tv_mode_property) {
1491 *val = state->tv.mode;
1492 } else if (property == config->tv_brightness_property) {
1493 *val = state->tv.brightness;
1494 } else if (property == config->tv_contrast_property) {
1495 *val = state->tv.contrast;
1496 } else if (property == config->tv_flicker_reduction_property) {
1497 *val = state->tv.flicker_reduction;
1498 } else if (property == config->tv_overscan_property) {
1499 *val = state->tv.overscan;
1500 } else if (property == config->tv_saturation_property) {
1501 *val = state->tv.saturation;
1502 } else if (property == config->tv_hue_property) {
1503 *val = state->tv.hue;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001504 } else if (property == config->link_status_property) {
1505 *val = state->link_status;
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02001506 } else if (property == config->aspect_ratio_property) {
1507 *val = state->picture_aspect_ratio;
Stanislav Lisovskiy50525c32018-05-15 16:59:27 +03001508 } else if (property == config->content_type_property) {
1509 *val = state->content_type;
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001510 } else if (property == connector->scaling_mode_property) {
1511 *val = state->scaling_mode;
Sean Paul24557862018-01-08 14:55:37 -05001512 } else if (property == connector->content_protection_property) {
1513 *val = state->content_protection;
Brian Starkey935774c2017-03-29 17:42:32 +01001514 } else if (property == config->writeback_fb_id_property) {
1515 /* Writeback framebuffer is one-shot, write and forget */
1516 *val = 0;
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01001517 } else if (property == config->writeback_out_fence_ptr_property) {
1518 *val = 0;
Rob Clarkac9c9252014-12-18 16:01:47 -05001519 } else if (connector->funcs->atomic_get_property) {
1520 return connector->funcs->atomic_get_property(connector,
1521 state, property, val);
1522 } else {
1523 return -EINVAL;
1524 }
1525
1526 return 0;
1527}
Rob Clarkac9c9252014-12-18 16:01:47 -05001528
Rob Clark88a48e22014-12-18 16:01:50 -05001529int drm_atomic_get_property(struct drm_mode_object *obj,
1530 struct drm_property *property, uint64_t *val)
1531{
1532 struct drm_device *dev = property->dev;
1533 int ret;
1534
1535 switch (obj->type) {
1536 case DRM_MODE_OBJECT_CONNECTOR: {
1537 struct drm_connector *connector = obj_to_connector(obj);
1538 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1539 ret = drm_atomic_connector_get_property(connector,
1540 connector->state, property, val);
1541 break;
1542 }
1543 case DRM_MODE_OBJECT_CRTC: {
1544 struct drm_crtc *crtc = obj_to_crtc(obj);
1545 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1546 ret = drm_atomic_crtc_get_property(crtc,
1547 crtc->state, property, val);
1548 break;
1549 }
1550 case DRM_MODE_OBJECT_PLANE: {
1551 struct drm_plane *plane = obj_to_plane(obj);
1552 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1553 ret = drm_atomic_plane_get_property(plane,
1554 plane->state, property, val);
1555 break;
1556 }
1557 default:
1558 ret = -EINVAL;
1559 break;
1560 }
1561
1562 return ret;
1563}
1564
1565/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001566 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001567 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001568 * @crtc: crtc to use for the plane
1569 *
1570 * Changing the assigned crtc for a plane requires us to grab the lock and state
1571 * for the new crtc, as needed. This function takes care of all these details
1572 * besides updating the pointer in the state object itself.
1573 *
1574 * Returns:
1575 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1576 * then the w/w mutex code has detected a deadlock and the entire atomic
1577 * sequence must be restarted. All other errors are fatal.
1578 */
1579int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001580drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1581 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001582{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001583 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001584 struct drm_crtc_state *crtc_state;
Satendra Singh Thakurfc2a69f2018-05-03 11:19:32 +05301585 /* Nothing to do for same crtc*/
1586 if (plane_state->crtc == crtc)
1587 return 0;
Rob Clark6ddd3882014-11-21 15:28:31 -05001588 if (plane_state->crtc) {
1589 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1590 plane_state->crtc);
1591 if (WARN_ON(IS_ERR(crtc_state)))
1592 return PTR_ERR(crtc_state);
1593
Ville Syrjälä62f77ad2018-06-26 22:47:07 +03001594 crtc_state->plane_mask &= ~drm_plane_mask(plane);
Rob Clark6ddd3882014-11-21 15:28:31 -05001595 }
1596
1597 plane_state->crtc = crtc;
1598
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001599 if (crtc) {
1600 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1601 crtc);
1602 if (IS_ERR(crtc_state))
1603 return PTR_ERR(crtc_state);
Ville Syrjälä62f77ad2018-06-26 22:47:07 +03001604 crtc_state->plane_mask |= drm_plane_mask(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001605 }
1606
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001607 if (crtc)
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001608 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
1609 plane->base.id, plane->name, plane_state,
1610 crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001611 else
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001612 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
1613 plane->base.id, plane->name, plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001614
1615 return 0;
1616}
1617EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1618
1619/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001620 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001621 * @plane_state: atomic state object for the plane
1622 * @fb: fb to use for the plane
1623 *
1624 * Changing the assigned framebuffer for a plane requires us to grab a reference
1625 * to the new fb and drop the reference to the old fb, if there is one. This
1626 * function takes care of all these details besides updating the pointer in the
1627 * state object itself.
1628 */
1629void
1630drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1631 struct drm_framebuffer *fb)
1632{
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001633 struct drm_plane *plane = plane_state->plane;
1634
Daniel Vetter321ebf02014-11-04 22:57:27 +01001635 if (fb)
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001636 DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
1637 fb->base.id, plane->base.id, plane->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +01001638 plane_state);
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001639 else
1640 DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
1641 plane->base.id, plane->name, plane_state);
Chris Wilson389f78b2016-11-25 15:32:30 +00001642
1643 drm_framebuffer_assign(&plane_state->fb, fb);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001644}
1645EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1646
1647/**
Gustavo Padovan13b55662016-11-07 19:03:30 +09001648 * drm_atomic_set_fence_for_plane - set fence for plane
1649 * @plane_state: atomic state object for the plane
1650 * @fence: dma_fence to use for the plane
1651 *
1652 * Helper to setup the plane_state fence in case it is not set yet.
1653 * By using this drivers doesn't need to worry if the user choose
1654 * implicit or explicit fencing.
1655 *
1656 * This function will not set the fence to the state if it was set
Daniel Vetterd5745282017-01-25 07:26:45 +01001657 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1658 * drop the reference to the fence as we are not storing it anywhere.
1659 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1660 * with the received implicit fence. In both cases this function consumes a
1661 * reference for @fence.
Daniel Vetter30d23f22018-04-05 17:44:46 +02001662 *
1663 * This way explicit fencing can be used to overrule implicit fencing, which is
1664 * important to make explicit fencing use-cases work: One example is using one
1665 * buffer for 2 screens with different refresh rates. Implicit fencing will
1666 * clamp rendering to the refresh rate of the slower screen, whereas explicit
1667 * fence allows 2 independent render and display loops on a single buffer. If a
1668 * driver allows obeys both implicit and explicit fences for plane updates, then
1669 * it will break all the benefits of explicit fencing.
Gustavo Padovan13b55662016-11-07 19:03:30 +09001670 */
1671void
1672drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1673 struct dma_fence *fence)
1674{
1675 if (plane_state->fence) {
1676 dma_fence_put(fence);
1677 return;
1678 }
1679
1680 plane_state->fence = fence;
1681}
1682EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1683
1684/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001685 * drm_atomic_set_crtc_for_connector - set crtc for connector
1686 * @conn_state: atomic state object for the connector
1687 * @crtc: crtc to use for the connector
1688 *
1689 * Changing the assigned crtc for a connector requires us to grab the lock and
1690 * state for the new crtc, as needed. This function takes care of all these
1691 * details besides updating the pointer in the state object itself.
1692 *
1693 * Returns:
1694 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1695 * then the w/w mutex code has detected a deadlock and the entire atomic
1696 * sequence must be restarted. All other errors are fatal.
1697 */
1698int
1699drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1700 struct drm_crtc *crtc)
1701{
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001702 struct drm_connector *connector = conn_state->connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001703 struct drm_crtc_state *crtc_state;
1704
Chris Wilsone2d800a2016-05-06 12:47:45 +01001705 if (conn_state->crtc == crtc)
1706 return 0;
1707
1708 if (conn_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001709 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1710 conn_state->crtc);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001711
1712 crtc_state->connector_mask &=
Ville Syrjälä73705732018-06-26 22:47:10 +03001713 ~drm_connector_mask(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001714
Thierry Redingad093602017-02-28 15:46:39 +01001715 drm_connector_put(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001716 conn_state->crtc = NULL;
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001717 }
1718
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001719 if (crtc) {
1720 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1721 if (IS_ERR(crtc_state))
1722 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001723
1724 crtc_state->connector_mask |=
Ville Syrjälä73705732018-06-26 22:47:10 +03001725 drm_connector_mask(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001726
Thierry Redingad093602017-02-28 15:46:39 +01001727 drm_connector_get(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001728 conn_state->crtc = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001729
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001730 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
1731 connector->base.id, connector->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001732 conn_state, crtc->base.id, crtc->name);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001733 } else {
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001734 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
1735 connector->base.id, connector->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +01001736 conn_state);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001737 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001738
1739 return 0;
1740}
1741EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1742
Brian Starkey935774c2017-03-29 17:42:32 +01001743/*
1744 * drm_atomic_get_writeback_job - return or allocate a writeback job
1745 * @conn_state: Connector state to get the job for
1746 *
1747 * Writeback jobs have a different lifetime to the atomic state they are
1748 * associated with. This convenience function takes care of allocating a job
1749 * if there isn't yet one associated with the connector state, otherwise
1750 * it just returns the existing job.
1751 *
1752 * Returns: The writeback job for the given connector state
1753 */
1754static struct drm_writeback_job *
1755drm_atomic_get_writeback_job(struct drm_connector_state *conn_state)
1756{
1757 WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1758
1759 if (!conn_state->writeback_job)
1760 conn_state->writeback_job =
1761 kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL);
1762
1763 return conn_state->writeback_job;
1764}
1765
1766/**
1767 * drm_atomic_set_writeback_fb_for_connector - set writeback framebuffer
1768 * @conn_state: atomic state object for the connector
1769 * @fb: fb to use for the connector
1770 *
1771 * This is used to set the framebuffer for a writeback connector, which outputs
1772 * to a buffer instead of an actual physical connector.
1773 * Changing the assigned framebuffer requires us to grab a reference to the new
1774 * fb and drop the reference to the old fb, if there is one. This function
1775 * takes care of all these details besides updating the pointer in the
1776 * state object itself.
1777 *
1778 * Note: The only way conn_state can already have an fb set is if the commit
1779 * sets the property more than once.
1780 *
1781 * See also: drm_writeback_connector_init()
1782 *
1783 * Returns: 0 on success
1784 */
1785int drm_atomic_set_writeback_fb_for_connector(
1786 struct drm_connector_state *conn_state,
1787 struct drm_framebuffer *fb)
1788{
1789 struct drm_writeback_job *job =
1790 drm_atomic_get_writeback_job(conn_state);
1791 if (!job)
1792 return -ENOMEM;
1793
1794 drm_framebuffer_assign(&job->fb, fb);
1795
1796 if (fb)
1797 DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
1798 fb->base.id, conn_state);
1799 else
1800 DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
1801 conn_state);
1802
1803 return 0;
1804}
1805EXPORT_SYMBOL(drm_atomic_set_writeback_fb_for_connector);
1806
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001807/**
1808 * drm_atomic_add_affected_connectors - add connectors for crtc
1809 * @state: atomic state
1810 * @crtc: DRM crtc
1811 *
1812 * This function walks the current configuration and adds all connectors
1813 * currently using @crtc to the atomic configuration @state. Note that this
1814 * function must acquire the connection mutex. This can potentially cause
1815 * unneeded seralization if the update is just for the planes on one crtc. Hence
1816 * drivers and helpers should only call this when really needed (e.g. when a
1817 * full modeset needs to happen due to some change).
1818 *
1819 * Returns:
1820 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1821 * then the w/w mutex code has detected a deadlock and the entire atomic
1822 * sequence must be restarted. All other errors are fatal.
1823 */
1824int
1825drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1826 struct drm_crtc *crtc)
1827{
1828 struct drm_mode_config *config = &state->dev->mode_config;
1829 struct drm_connector *connector;
1830 struct drm_connector_state *conn_state;
Daniel Vetter613051d2016-12-14 00:08:06 +01001831 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001832 struct drm_crtc_state *crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001833 int ret;
1834
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001835 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1836 if (IS_ERR(crtc_state))
1837 return PTR_ERR(crtc_state);
1838
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001839 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1840 if (ret)
1841 return ret;
1842
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001843 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1844 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001845
1846 /*
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001847 * Changed connectors are already in @state, so only need to look
1848 * at the connector_mask in crtc_state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001849 */
Thierry Redingb982dab2017-02-28 15:46:43 +01001850 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +01001851 drm_for_each_connector_iter(connector, &conn_iter) {
Ville Syrjälä73705732018-06-26 22:47:10 +03001852 if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001853 continue;
1854
1855 conn_state = drm_atomic_get_connector_state(state, connector);
Daniel Vetter613051d2016-12-14 00:08:06 +01001856 if (IS_ERR(conn_state)) {
Thierry Redingb982dab2017-02-28 15:46:43 +01001857 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001858 return PTR_ERR(conn_state);
Daniel Vetter613051d2016-12-14 00:08:06 +01001859 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001860 }
Thierry Redingb982dab2017-02-28 15:46:43 +01001861 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001862
1863 return 0;
1864}
1865EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1866
1867/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001868 * drm_atomic_add_affected_planes - add planes for crtc
1869 * @state: atomic state
1870 * @crtc: DRM crtc
1871 *
1872 * This function walks the current configuration and adds all planes
1873 * currently used by @crtc to the atomic configuration @state. This is useful
1874 * when an atomic commit also needs to check all currently enabled plane on
1875 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1876 * to avoid special code to force-enable all planes.
1877 *
1878 * Since acquiring a plane state will always also acquire the w/w mutex of the
1879 * current CRTC for that plane (if there is any) adding all the plane states for
1880 * a CRTC will not reduce parallism of atomic updates.
1881 *
1882 * Returns:
1883 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1884 * then the w/w mutex code has detected a deadlock and the entire atomic
1885 * sequence must be restarted. All other errors are fatal.
1886 */
1887int
1888drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1889 struct drm_crtc *crtc)
1890{
1891 struct drm_plane *plane;
1892
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001893 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001894
Ville Syrjäläb6f690a2018-06-11 22:34:01 +03001895 DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
1896 crtc->base.id, crtc->name, state);
1897
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001898 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1899 struct drm_plane_state *plane_state =
1900 drm_atomic_get_plane_state(state, plane);
1901
1902 if (IS_ERR(plane_state))
1903 return PTR_ERR(plane_state);
1904 }
1905 return 0;
1906}
1907EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1908
1909/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001910 * drm_atomic_check_only - check whether a given config would work
1911 * @state: atomic configuration to check
1912 *
1913 * Note that this function can return -EDEADLK if the driver needed to acquire
1914 * more locks but encountered a deadlock. The caller must then do the usual w/w
1915 * backoff dance and restart. All other errors are fatal.
1916 *
1917 * Returns:
1918 * 0 on success, negative error code on failure.
1919 */
1920int drm_atomic_check_only(struct drm_atomic_state *state)
1921{
Rob Clark5e743732014-12-18 16:01:51 -05001922 struct drm_device *dev = state->dev;
1923 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001924 struct drm_plane *plane;
1925 struct drm_plane_state *plane_state;
1926 struct drm_crtc *crtc;
1927 struct drm_crtc_state *crtc_state;
Brian Starkey935774c2017-03-29 17:42:32 +01001928 struct drm_connector *conn;
1929 struct drm_connector_state *conn_state;
Rob Clark5e743732014-12-18 16:01:51 -05001930 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001931
Daniel Vetter17a38d92015-02-22 12:24:16 +01001932 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001933
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001934 for_each_new_plane_in_state(state, plane, plane_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001935 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001936 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001937 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1938 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001939 return ret;
1940 }
1941 }
1942
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001943 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001944 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001945 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001946 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1947 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001948 return ret;
1949 }
1950 }
1951
Brian Starkey935774c2017-03-29 17:42:32 +01001952 for_each_new_connector_in_state(state, conn, conn_state, i) {
1953 ret = drm_atomic_connector_check(conn, conn_state);
1954 if (ret) {
1955 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
1956 conn->base.id, conn->name);
1957 return ret;
1958 }
1959 }
1960
Lyude Paul14d4e522018-04-11 19:42:40 -04001961 if (config->funcs->atomic_check) {
Rob Clark5e743732014-12-18 16:01:51 -05001962 ret = config->funcs->atomic_check(state->dev, state);
1963
Lyude Paul14d4e522018-04-11 19:42:40 -04001964 if (ret) {
1965 DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
1966 state, ret);
1967 return ret;
1968 }
1969 }
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001970
Rob Clarkd34f20d2014-12-18 16:01:56 -05001971 if (!state->allow_modeset) {
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001972 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001973 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001974 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1975 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001976 return -EINVAL;
1977 }
1978 }
1979 }
1980
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001981 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001982}
1983EXPORT_SYMBOL(drm_atomic_check_only);
1984
1985/**
1986 * drm_atomic_commit - commit configuration atomically
1987 * @state: atomic configuration to check
1988 *
1989 * Note that this function can return -EDEADLK if the driver needed to acquire
1990 * more locks but encountered a deadlock. The caller must then do the usual w/w
1991 * backoff dance and restart. All other errors are fatal.
1992 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001993 * This function will take its own reference on @state.
1994 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001995 *
1996 * Returns:
1997 * 0 on success, negative error code on failure.
1998 */
1999int drm_atomic_commit(struct drm_atomic_state *state)
2000{
2001 struct drm_mode_config *config = &state->dev->mode_config;
2002 int ret;
2003
2004 ret = drm_atomic_check_only(state);
2005 if (ret)
2006 return ret;
2007
Colin Ian Kinga0752d42017-04-12 17:27:22 +01002008 DRM_DEBUG_ATOMIC("committing %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02002009
2010 return config->funcs->atomic_commit(state->dev, state, false);
2011}
2012EXPORT_SYMBOL(drm_atomic_commit);
2013
2014/**
Daniel Vetterd5745282017-01-25 07:26:45 +01002015 * drm_atomic_nonblocking_commit - atomic nonblocking commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02002016 * @state: atomic configuration to check
2017 *
2018 * Note that this function can return -EDEADLK if the driver needed to acquire
2019 * more locks but encountered a deadlock. The caller must then do the usual w/w
2020 * backoff dance and restart. All other errors are fatal.
2021 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01002022 * This function will take its own reference on @state.
2023 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02002024 *
2025 * Returns:
2026 * 0 on success, negative error code on failure.
2027 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002028int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02002029{
2030 struct drm_mode_config *config = &state->dev->mode_config;
2031 int ret;
2032
2033 ret = drm_atomic_check_only(state);
2034 if (ret)
2035 return ret;
2036
Colin Ian Kinga0752d42017-04-12 17:27:22 +01002037 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02002038
2039 return config->funcs->atomic_commit(state->dev, state, true);
2040}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002041EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002042
Rob Clarkfceffb322016-11-05 11:08:09 -04002043static void drm_atomic_print_state(const struct drm_atomic_state *state)
2044{
2045 struct drm_printer p = drm_info_printer(state->dev->dev);
2046 struct drm_plane *plane;
2047 struct drm_plane_state *plane_state;
2048 struct drm_crtc *crtc;
2049 struct drm_crtc_state *crtc_state;
2050 struct drm_connector *connector;
2051 struct drm_connector_state *connector_state;
2052 int i;
2053
2054 DRM_DEBUG_ATOMIC("checking %p\n", state);
2055
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002056 for_each_new_plane_in_state(state, plane, plane_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04002057 drm_atomic_plane_print_state(&p, plane_state);
2058
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002059 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04002060 drm_atomic_crtc_print_state(&p, crtc_state);
2061
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002062 for_each_new_connector_in_state(state, connector, connector_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04002063 drm_atomic_connector_print_state(&p, connector_state);
2064}
2065
Daniel Vetterc2d85562017-04-03 10:32:54 +02002066static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
2067 bool take_locks)
2068{
2069 struct drm_mode_config *config = &dev->mode_config;
2070 struct drm_plane *plane;
2071 struct drm_crtc *crtc;
2072 struct drm_connector *connector;
2073 struct drm_connector_list_iter conn_iter;
2074
Lyude Paul3c499ea2018-09-17 13:37:33 -04002075 if (!drm_drv_uses_atomic_modeset(dev))
Daniel Vetterc2d85562017-04-03 10:32:54 +02002076 return;
2077
2078 list_for_each_entry(plane, &config->plane_list, head) {
2079 if (take_locks)
2080 drm_modeset_lock(&plane->mutex, NULL);
2081 drm_atomic_plane_print_state(p, plane->state);
2082 if (take_locks)
2083 drm_modeset_unlock(&plane->mutex);
2084 }
2085
2086 list_for_each_entry(crtc, &config->crtc_list, head) {
2087 if (take_locks)
2088 drm_modeset_lock(&crtc->mutex, NULL);
2089 drm_atomic_crtc_print_state(p, crtc->state);
2090 if (take_locks)
2091 drm_modeset_unlock(&crtc->mutex);
2092 }
2093
2094 drm_connector_list_iter_begin(dev, &conn_iter);
2095 if (take_locks)
2096 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2097 drm_for_each_connector_iter(connector, &conn_iter)
2098 drm_atomic_connector_print_state(p, connector->state);
2099 if (take_locks)
2100 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2101 drm_connector_list_iter_end(&conn_iter);
2102}
2103
Rob Clark6559c902016-11-05 11:08:10 -04002104/**
2105 * drm_state_dump - dump entire device atomic state
2106 * @dev: the drm device
2107 * @p: where to print the state to
2108 *
2109 * Just for debugging. Drivers might want an option to dump state
2110 * to dmesg in case of error irq's. (Hint, you probably want to
2111 * ratelimit this!)
2112 *
2113 * The caller must drm_modeset_lock_all(), or if this is called
2114 * from error irq handler, it should not be enabled by default.
2115 * (Ie. if you are debugging errors you might not care that this
2116 * is racey. But calling this without all modeset locks held is
2117 * not inherently safe.)
2118 */
2119void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
2120{
Daniel Vetterc2d85562017-04-03 10:32:54 +02002121 __drm_state_dump(dev, p, false);
Rob Clark6559c902016-11-05 11:08:10 -04002122}
2123EXPORT_SYMBOL(drm_state_dump);
2124
2125#ifdef CONFIG_DEBUG_FS
2126static int drm_state_info(struct seq_file *m, void *data)
2127{
2128 struct drm_info_node *node = (struct drm_info_node *) m->private;
2129 struct drm_device *dev = node->minor->dev;
2130 struct drm_printer p = drm_seq_file_printer(m);
2131
Daniel Vetterc2d85562017-04-03 10:32:54 +02002132 __drm_state_dump(dev, &p, true);
Rob Clark6559c902016-11-05 11:08:10 -04002133
2134 return 0;
2135}
2136
2137/* any use in debugfs files to dump individual planes/crtc/etc? */
2138static const struct drm_info_list drm_atomic_debugfs_list[] = {
2139 {"state", drm_state_info, 0},
2140};
2141
2142int drm_atomic_debugfs_init(struct drm_minor *minor)
2143{
2144 return drm_debugfs_create_files(drm_atomic_debugfs_list,
2145 ARRAY_SIZE(drm_atomic_debugfs_list),
2146 minor->debugfs_root, minor);
2147}
2148#endif
2149
Rob Clarkd34f20d2014-12-18 16:01:56 -05002150/*
Liviu Dudau21be9152017-11-01 14:04:36 +00002151 * The big monster ioctl
Rob Clarkd34f20d2014-12-18 16:01:56 -05002152 */
2153
2154static struct drm_pending_vblank_event *create_vblank_event(
Keith Packardbd386e52017-07-05 14:34:23 -07002155 struct drm_crtc *crtc, uint64_t user_data)
Rob Clarkd34f20d2014-12-18 16:01:56 -05002156{
2157 struct drm_pending_vblank_event *e = NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002158
2159 e = kzalloc(sizeof *e, GFP_KERNEL);
Daniel Vetter2dd500f2016-01-11 22:40:56 +01002160 if (!e)
2161 return NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002162
2163 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01002164 e->event.base.length = sizeof(e->event);
Keith Packardbd386e52017-07-05 14:34:23 -07002165 e->event.vbl.crtc_id = crtc->base.id;
2166 e->event.vbl.user_data = user_data;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002167
Rob Clarkd34f20d2014-12-18 16:01:56 -05002168 return e;
2169}
2170
Daniel Vetter144a7992017-07-25 14:02:04 +02002171int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
2172 struct drm_connector *connector,
2173 int mode)
2174{
2175 struct drm_connector *tmp_connector;
2176 struct drm_connector_state *new_conn_state;
2177 struct drm_crtc *crtc;
2178 struct drm_crtc_state *crtc_state;
2179 int i, ret, old_mode = connector->dpms;
2180 bool active = false;
2181
2182 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
2183 state->acquire_ctx);
2184 if (ret)
2185 return ret;
2186
2187 if (mode != DRM_MODE_DPMS_ON)
2188 mode = DRM_MODE_DPMS_OFF;
2189 connector->dpms = mode;
2190
2191 crtc = connector->state->crtc;
2192 if (!crtc)
2193 goto out;
2194 ret = drm_atomic_add_affected_connectors(state, crtc);
2195 if (ret)
2196 goto out;
2197
2198 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2199 if (IS_ERR(crtc_state)) {
2200 ret = PTR_ERR(crtc_state);
2201 goto out;
2202 }
2203
2204 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
2205 if (new_conn_state->crtc != crtc)
2206 continue;
2207 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2208 active = true;
2209 break;
2210 }
2211 }
2212
2213 crtc_state->active = active;
2214 ret = drm_atomic_commit(state);
2215out:
2216 if (ret != 0)
2217 connector->dpms = old_mode;
2218 return ret;
2219}
2220
2221int drm_atomic_set_property(struct drm_atomic_state *state,
2222 struct drm_mode_object *obj,
2223 struct drm_property *prop,
2224 uint64_t prop_value)
Rob Clarkd34f20d2014-12-18 16:01:56 -05002225{
2226 struct drm_mode_object *ref;
2227 int ret;
2228
2229 if (!drm_property_change_valid_get(prop, prop_value, &ref))
2230 return -EINVAL;
2231
2232 switch (obj->type) {
2233 case DRM_MODE_OBJECT_CONNECTOR: {
2234 struct drm_connector *connector = obj_to_connector(obj);
2235 struct drm_connector_state *connector_state;
2236
2237 connector_state = drm_atomic_get_connector_state(state, connector);
2238 if (IS_ERR(connector_state)) {
2239 ret = PTR_ERR(connector_state);
2240 break;
2241 }
2242
2243 ret = drm_atomic_connector_set_property(connector,
2244 connector_state, prop, prop_value);
2245 break;
2246 }
2247 case DRM_MODE_OBJECT_CRTC: {
2248 struct drm_crtc *crtc = obj_to_crtc(obj);
2249 struct drm_crtc_state *crtc_state;
2250
2251 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2252 if (IS_ERR(crtc_state)) {
2253 ret = PTR_ERR(crtc_state);
2254 break;
2255 }
2256
2257 ret = drm_atomic_crtc_set_property(crtc,
2258 crtc_state, prop, prop_value);
2259 break;
2260 }
2261 case DRM_MODE_OBJECT_PLANE: {
2262 struct drm_plane *plane = obj_to_plane(obj);
2263 struct drm_plane_state *plane_state;
2264
2265 plane_state = drm_atomic_get_plane_state(state, plane);
2266 if (IS_ERR(plane_state)) {
2267 ret = PTR_ERR(plane_state);
2268 break;
2269 }
2270
2271 ret = drm_atomic_plane_set_property(plane,
2272 plane_state, prop, prop_value);
2273 break;
2274 }
2275 default:
2276 ret = -EINVAL;
2277 break;
2278 }
2279
2280 drm_property_change_valid_put(prop, ref);
2281 return ret;
2282}
2283
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01002284/**
Gustavo Padovan9a83a712016-11-22 09:11:28 +09002285 * DOC: explicit fencing properties
2286 *
2287 * Explicit fencing allows userspace to control the buffer synchronization
2288 * between devices. A Fence or a group of fences are transfered to/from
2289 * userspace using Sync File fds and there are two DRM properties for that.
2290 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
2291 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
2292 *
2293 * As a contrast, with implicit fencing the kernel keeps track of any
2294 * ongoing rendering, and automatically ensures that the atomic update waits
2295 * for any pending rendering to complete. For shared buffers represented with
Daniel Vetterd5745282017-01-25 07:26:45 +01002296 * a &struct dma_buf this is tracked in &struct reservation_object.
Gustavo Padovan9a83a712016-11-22 09:11:28 +09002297 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
2298 * whereas explicit fencing is what Android wants.
2299 *
2300 * "IN_FENCE_FD”:
2301 * Use this property to pass a fence that DRM should wait on before
2302 * proceeding with the Atomic Commit request and show the framebuffer for
2303 * the plane on the screen. The fence can be either a normal fence or a
2304 * merged one, the sync_file framework will handle both cases and use a
2305 * fence_array if a merged fence is received. Passing -1 here means no
2306 * fences to wait on.
2307 *
2308 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
2309 * it will only check if the Sync File is a valid one.
2310 *
2311 * On the driver side the fence is stored on the @fence parameter of
Daniel Vetterea0dd852016-12-29 21:48:26 +01002312 * &struct drm_plane_state. Drivers which also support implicit fencing
Gustavo Padovan9a83a712016-11-22 09:11:28 +09002313 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
2314 * to make sure there's consistent behaviour between drivers in precedence
2315 * of implicit vs. explicit fencing.
2316 *
2317 * "OUT_FENCE_PTR”:
2318 * Use this property to pass a file descriptor pointer to DRM. Once the
2319 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
2320 * the file descriptor number of a Sync File. This Sync File contains the
2321 * CRTC fence that will be signaled when all framebuffers present on the
2322 * Atomic Commit * request for that given CRTC are scanned out on the
2323 * screen.
2324 *
2325 * The Atomic Commit request fails if a invalid pointer is passed. If the
2326 * Atomic Commit request fails for any other reason the out fence fd
2327 * returned will be -1. On a Atomic Commit with the
2328 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2329 *
2330 * Note that out-fences don't have a special interface to drivers and are
Daniel Vetterea0dd852016-12-29 21:48:26 +01002331 * internally represented by a &struct drm_pending_vblank_event in struct
Gustavo Padovan9a83a712016-11-22 09:11:28 +09002332 * &drm_crtc_state, which is also used by the nonblocking atomic commit
2333 * helpers and for the DRM event handling for existing userspace.
2334 */
2335
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002336struct drm_out_fence_state {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02002337 s32 __user *out_fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002338 struct sync_file *sync_file;
2339 int fd;
2340};
2341
2342static int setup_out_fence(struct drm_out_fence_state *fence_state,
2343 struct dma_fence *fence)
2344{
2345 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
2346 if (fence_state->fd < 0)
2347 return fence_state->fd;
2348
2349 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
2350 return -EFAULT;
2351
2352 fence_state->sync_file = sync_file_create(fence);
2353 if (!fence_state->sync_file)
2354 return -ENOMEM;
2355
2356 return 0;
2357}
2358
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01002359static int prepare_signaling(struct drm_device *dev,
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002360 struct drm_atomic_state *state,
2361 struct drm_mode_atomic *arg,
2362 struct drm_file *file_priv,
2363 struct drm_out_fence_state **fence_state,
2364 unsigned int *num_fences)
2365{
2366 struct drm_crtc *crtc;
2367 struct drm_crtc_state *crtc_state;
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01002368 struct drm_connector *conn;
2369 struct drm_connector_state *conn_state;
Andrey Grodzovsky330c4222017-06-20 13:57:06 -04002370 int i, c = 0, ret;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002371
2372 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
2373 return 0;
2374
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002375 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02002376 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002377
2378 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
2379
2380 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
2381 struct drm_pending_vblank_event *e;
2382
Keith Packardbd386e52017-07-05 14:34:23 -07002383 e = create_vblank_event(crtc, arg->user_data);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002384 if (!e)
2385 return -ENOMEM;
2386
2387 crtc_state->event = e;
2388 }
2389
2390 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2391 struct drm_pending_vblank_event *e = crtc_state->event;
2392
2393 if (!file_priv)
2394 continue;
2395
2396 ret = drm_event_reserve_init(dev, file_priv, &e->base,
2397 &e->event.base);
2398 if (ret) {
2399 kfree(e);
2400 crtc_state->event = NULL;
2401 return ret;
2402 }
2403 }
2404
2405 if (fence_ptr) {
2406 struct dma_fence *fence;
2407 struct drm_out_fence_state *f;
2408
2409 f = krealloc(*fence_state, sizeof(**fence_state) *
2410 (*num_fences + 1), GFP_KERNEL);
2411 if (!f)
2412 return -ENOMEM;
2413
2414 memset(&f[*num_fences], 0, sizeof(*f));
2415
2416 f[*num_fences].out_fence_ptr = fence_ptr;
2417 *fence_state = f;
2418
Gustavo Padovan35f8cc32016-12-06 15:47:17 -02002419 fence = drm_crtc_create_fence(crtc);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002420 if (!fence)
2421 return -ENOMEM;
2422
2423 ret = setup_out_fence(&f[(*num_fences)++], fence);
2424 if (ret) {
2425 dma_fence_put(fence);
2426 return ret;
2427 }
2428
2429 crtc_state->event->base.fence = fence;
2430 }
Andrey Grodzovsky330c4222017-06-20 13:57:06 -04002431
2432 c++;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002433 }
2434
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01002435 for_each_new_connector_in_state(state, conn, conn_state, i) {
Boris Brezillonb82c1f82018-07-03 09:50:15 +02002436 struct drm_writeback_connector *wb_conn;
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01002437 struct drm_writeback_job *job;
2438 struct drm_out_fence_state *f;
2439 struct dma_fence *fence;
2440 s32 __user *fence_ptr;
2441
2442 fence_ptr = get_out_fence_for_connector(state, conn);
2443 if (!fence_ptr)
2444 continue;
2445
2446 job = drm_atomic_get_writeback_job(conn_state);
2447 if (!job)
2448 return -ENOMEM;
2449
2450 f = krealloc(*fence_state, sizeof(**fence_state) *
2451 (*num_fences + 1), GFP_KERNEL);
2452 if (!f)
2453 return -ENOMEM;
2454
2455 memset(&f[*num_fences], 0, sizeof(*f));
2456
2457 f[*num_fences].out_fence_ptr = fence_ptr;
2458 *fence_state = f;
2459
Boris Brezillonb82c1f82018-07-03 09:50:15 +02002460 wb_conn = drm_connector_to_writeback(conn);
2461 fence = drm_writeback_get_out_fence(wb_conn);
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01002462 if (!fence)
2463 return -ENOMEM;
2464
2465 ret = setup_out_fence(&f[(*num_fences)++], fence);
2466 if (ret) {
2467 dma_fence_put(fence);
2468 return ret;
2469 }
2470
2471 job->out_fence = fence;
2472 }
2473
Andrey Grodzovsky330c4222017-06-20 13:57:06 -04002474 /*
2475 * Having this flag means user mode pends on event which will never
2476 * reach due to lack of at least one CRTC for signaling
2477 */
2478 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2479 return -EINVAL;
2480
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002481 return 0;
2482}
2483
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01002484static void complete_signaling(struct drm_device *dev,
2485 struct drm_atomic_state *state,
2486 struct drm_out_fence_state *fence_state,
2487 unsigned int num_fences,
2488 bool install_fds)
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002489{
2490 struct drm_crtc *crtc;
2491 struct drm_crtc_state *crtc_state;
2492 int i;
2493
2494 if (install_fds) {
2495 for (i = 0; i < num_fences; i++)
2496 fd_install(fence_state[i].fd,
2497 fence_state[i].sync_file->file);
2498
2499 kfree(fence_state);
2500 return;
2501 }
2502
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002503 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002504 struct drm_pending_vblank_event *event = crtc_state->event;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002505 /*
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002506 * Free the allocated event. drm_atomic_helper_setup_commit
2507 * can allocate an event too, so only free it if it's ours
2508 * to prevent a double free in drm_atomic_state_clear.
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002509 */
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002510 if (event && (event->base.fence || event->base.file_priv)) {
2511 drm_event_cancel_free(dev, &event->base);
2512 crtc_state->event = NULL;
2513 }
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002514 }
2515
2516 if (!fence_state)
2517 return;
2518
2519 for (i = 0; i < num_fences; i++) {
2520 if (fence_state[i].sync_file)
2521 fput(fence_state[i].sync_file->file);
2522 if (fence_state[i].fd >= 0)
2523 put_unused_fd(fence_state[i].fd);
2524
2525 /* If this fails log error to the user */
2526 if (fence_state[i].out_fence_ptr &&
2527 put_user(-1, fence_state[i].out_fence_ptr))
2528 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2529 }
2530
2531 kfree(fence_state);
2532}
2533
Rob Clarkd34f20d2014-12-18 16:01:56 -05002534int drm_mode_atomic_ioctl(struct drm_device *dev,
2535 void *data, struct drm_file *file_priv)
2536{
2537 struct drm_mode_atomic *arg = data;
2538 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2539 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2540 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2541 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2542 unsigned int copied_objs, copied_props;
2543 struct drm_atomic_state *state;
2544 struct drm_modeset_acquire_ctx ctx;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002545 struct drm_out_fence_state *fence_state;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002546 int ret = 0;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002547 unsigned int i, j, num_fences;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002548
2549 /* disallow for drivers not supporting atomic: */
2550 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2551 return -EINVAL;
2552
2553 /* disallow for userspace that has not enabled atomic cap (even
2554 * though this may be a bit overkill, since legacy userspace
2555 * wouldn't know how to call this ioctl)
2556 */
2557 if (!file_priv->atomic)
2558 return -EINVAL;
2559
2560 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2561 return -EINVAL;
2562
2563 if (arg->reserved)
2564 return -EINVAL;
2565
2566 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2567 !dev->mode_config.async_page_flip)
2568 return -EINVAL;
2569
2570 /* can't test and expect an event at the same time. */
2571 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2572 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2573 return -EINVAL;
2574
Maarten Lankhorstdad56ce2017-09-12 15:37:45 +02002575 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002576
2577 state = drm_atomic_state_alloc(dev);
2578 if (!state)
2579 return -ENOMEM;
2580
2581 state->acquire_ctx = &ctx;
2582 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2583
2584retry:
2585 copied_objs = 0;
2586 copied_props = 0;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002587 fence_state = NULL;
2588 num_fences = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002589
2590 for (i = 0; i < arg->count_objs; i++) {
2591 uint32_t obj_id, count_props;
2592 struct drm_mode_object *obj;
2593
2594 if (get_user(obj_id, objs_ptr + copied_objs)) {
2595 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002596 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002597 }
2598
Keith Packard418da172017-03-14 23:25:07 -07002599 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
Dave Airlieb164d312016-04-27 11:10:09 +10002600 if (!obj) {
2601 ret = -ENOENT;
2602 goto out;
2603 }
2604
2605 if (!obj->properties) {
Thierry Reding020a2182017-02-28 15:46:38 +01002606 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002607 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002608 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002609 }
2610
Rob Clarkd34f20d2014-12-18 16:01:56 -05002611 if (get_user(count_props, count_props_ptr + copied_objs)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002612 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002613 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002614 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002615 }
2616
2617 copied_objs++;
2618
2619 for (j = 0; j < count_props; j++) {
2620 uint32_t prop_id;
2621 uint64_t prop_value;
2622 struct drm_property *prop;
2623
2624 if (get_user(prop_id, props_ptr + copied_props)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002625 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002626 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002627 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002628 }
2629
Maarten Lankhorstf92f0532016-09-08 12:30:01 +02002630 prop = drm_mode_obj_find_prop_id(obj, prop_id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002631 if (!prop) {
Thierry Reding020a2182017-02-28 15:46:38 +01002632 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002633 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002634 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002635 }
2636
Guenter Roeck42c58142015-01-12 21:12:17 -08002637 if (copy_from_user(&prop_value,
2638 prop_values_ptr + copied_props,
2639 sizeof(prop_value))) {
Thierry Reding020a2182017-02-28 15:46:38 +01002640 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002641 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002642 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002643 }
2644
Daniel Vetter144a7992017-07-25 14:02:04 +02002645 ret = drm_atomic_set_property(state, obj, prop,
2646 prop_value);
Dave Airlieb164d312016-04-27 11:10:09 +10002647 if (ret) {
Thierry Reding020a2182017-02-28 15:46:38 +01002648 drm_mode_object_put(obj);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002649 goto out;
Dave Airlieb164d312016-04-27 11:10:09 +10002650 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05002651
2652 copied_props++;
2653 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002654
Thierry Reding020a2182017-02-28 15:46:38 +01002655 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002656 }
2657
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01002658 ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
2659 &num_fences);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002660 if (ret)
2661 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002662
2663 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2664 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002665 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002666 ret = drm_atomic_nonblocking_commit(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002667 } else {
Rob Clarkfceffb322016-11-05 11:08:09 -04002668 if (unlikely(drm_debug & DRM_UT_STATE))
2669 drm_atomic_print_state(state);
2670
Rob Clarkd34f20d2014-12-18 16:01:56 -05002671 ret = drm_atomic_commit(state);
2672 }
2673
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002674out:
Brian Starkeyb13cc8d2017-03-29 17:42:33 +01002675 complete_signaling(dev, state, fence_state, num_fences, !ret);
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002676
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002677 if (ret == -EDEADLK) {
2678 drm_atomic_state_clear(state);
Maarten Lankhorstdad56ce2017-09-12 15:37:45 +02002679 ret = drm_modeset_backoff(&ctx);
2680 if (!ret)
2681 goto retry;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002682 }
2683
Chris Wilson08536952016-10-14 13:18:18 +01002684 drm_atomic_state_put(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002685
2686 drm_modeset_drop_locks(&ctx);
2687 drm_modeset_acquire_fini(&ctx);
2688
2689 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002690}