blob: 37445d50816a5c2654b5b145c3c397044a429db1 [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>
Gustavo Padovan96260142016-11-15 22:06:39 +090033#include <linux/sync_file.h>
Daniel Vettercc4ceb42014-07-25 21:30:38 +020034
Thierry Redingbe35f942016-04-28 15:19:56 +020035#include "drm_crtc_internal.h"
Noralf Trønnesf02b6042017-11-07 20:13:41 +010036#include "drm_internal.h"
Thierry Redingbe35f942016-04-28 15:19:56 +020037
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010038void __drm_crtc_commit_free(struct kref *kref)
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020039{
40 struct drm_crtc_commit *commit =
41 container_of(kref, struct drm_crtc_commit, ref);
42
43 kfree(commit);
44}
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010045EXPORT_SYMBOL(__drm_crtc_commit_free);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020046
Maarten Lankhorst036ef572015-05-18 10:06:40 +020047/**
48 * drm_atomic_state_default_release -
49 * release memory initialized by drm_atomic_state_init
50 * @state: atomic state
51 *
52 * Free all the memory allocated by drm_atomic_state_init.
53 * This is useful for drivers that subclass the atomic state.
54 */
55void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020056{
57 kfree(state->connectors);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020058 kfree(state->crtcs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020059 kfree(state->planes);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -070060 kfree(state->private_objs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020061}
Maarten Lankhorst036ef572015-05-18 10:06:40 +020062EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020063
64/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020065 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020066 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +020067 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020068 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +020069 * Default implementation for filling in a new atomic state.
70 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +020071 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +020072int
73drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020074{
Chris Wilson08536952016-10-14 13:18:18 +010075 kref_init(&state->ref);
76
Rob Clarkd34f20d2014-12-18 16:01:56 -050077 /* TODO legacy paths should maybe do a better job about
78 * setting this appropriately?
79 */
80 state->allow_modeset = true;
81
Daniel Vettercc4ceb42014-07-25 21:30:38 +020082 state->crtcs = kcalloc(dev->mode_config.num_crtc,
83 sizeof(*state->crtcs), GFP_KERNEL);
84 if (!state->crtcs)
85 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020086 state->planes = kcalloc(dev->mode_config.num_total_plane,
87 sizeof(*state->planes), GFP_KERNEL);
88 if (!state->planes)
89 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020090
91 state->dev = dev;
92
Maarten Lankhorst036ef572015-05-18 10:06:40 +020093 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020094
Maarten Lankhorst036ef572015-05-18 10:06:40 +020095 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020096fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +020097 drm_atomic_state_default_release(state);
98 return -ENOMEM;
99}
100EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200101
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200102/**
103 * drm_atomic_state_alloc - allocate atomic state
104 * @dev: DRM device
105 *
106 * This allocates an empty atomic state to track updates.
107 */
108struct drm_atomic_state *
109drm_atomic_state_alloc(struct drm_device *dev)
110{
111 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200112
113 if (!config->funcs->atomic_state_alloc) {
Dawid Kurekac7c7482017-06-15 19:45:56 +0200114 struct drm_atomic_state *state;
115
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200116 state = kzalloc(sizeof(*state), GFP_KERNEL);
117 if (!state)
118 return NULL;
119 if (drm_atomic_state_init(dev, state) < 0) {
120 kfree(state);
121 return NULL;
122 }
123 return state;
124 }
125
126 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200127}
128EXPORT_SYMBOL(drm_atomic_state_alloc);
129
130/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200131 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200132 * @state: atomic state
133 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200134 * Default implementation for clearing atomic state.
135 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200136 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200137void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200138{
139 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100140 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200141 int i;
142
Daniel Vetter17a38d92015-02-22 12:24:16 +0100143 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200144
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100145 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200146 struct drm_connector *connector = state->connectors[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200147
148 if (!connector)
149 continue;
150
Dave Airlied2307de2016-04-27 11:27:39 +1000151 connector->funcs->atomic_destroy_state(connector,
Daniel Vetter63e83c12016-06-02 00:06:32 +0200152 state->connectors[i].state);
153 state->connectors[i].ptr = NULL;
154 state->connectors[i].state = NULL;
Thierry Redingad093602017-02-28 15:46:39 +0100155 drm_connector_put(connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200156 }
157
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100158 for (i = 0; i < config->num_crtc; i++) {
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200159 struct drm_crtc *crtc = state->crtcs[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200160
161 if (!crtc)
162 continue;
163
164 crtc->funcs->atomic_destroy_state(crtc,
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200165 state->crtcs[i].state);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200166
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200167 state->crtcs[i].ptr = NULL;
168 state->crtcs[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200169 }
170
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100171 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vetterb8b53422016-06-02 00:06:33 +0200172 struct drm_plane *plane = state->planes[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200173
174 if (!plane)
175 continue;
176
177 plane->funcs->atomic_destroy_state(plane,
Daniel Vetterb8b53422016-06-02 00:06:33 +0200178 state->planes[i].state);
179 state->planes[i].ptr = NULL;
180 state->planes[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200181 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700182
183 for (i = 0; i < state->num_private_objs; i++) {
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300184 struct drm_private_obj *obj = state->private_objs[i].ptr;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700185
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300186 obj->funcs->atomic_destroy_state(obj,
187 state->private_objs[i].state);
188 state->private_objs[i].ptr = NULL;
189 state->private_objs[i].state = NULL;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700190 }
191 state->num_private_objs = 0;
192
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +0200193 if (state->fake_commit) {
194 drm_crtc_commit_put(state->fake_commit);
195 state->fake_commit = NULL;
196 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200197}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200198EXPORT_SYMBOL(drm_atomic_state_default_clear);
199
200/**
201 * drm_atomic_state_clear - clear state object
202 * @state: atomic state
203 *
204 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
205 * all locks. So someone else could sneak in and change the current modeset
206 * configuration. Which means that all the state assembled in @state is no
207 * longer an atomic update to the current state, but to some arbitrary earlier
Daniel Vetterd5745282017-01-25 07:26:45 +0100208 * state. Which could break assumptions the driver's
209 * &drm_mode_config_funcs.atomic_check likely relies on.
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200210 *
211 * Hence we must clear all cached state and completely start over, using this
212 * function.
213 */
214void drm_atomic_state_clear(struct drm_atomic_state *state)
215{
216 struct drm_device *dev = state->dev;
217 struct drm_mode_config *config = &dev->mode_config;
218
219 if (config->funcs->atomic_state_clear)
220 config->funcs->atomic_state_clear(state);
221 else
222 drm_atomic_state_default_clear(state);
223}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200224EXPORT_SYMBOL(drm_atomic_state_clear);
225
226/**
Chris Wilson08536952016-10-14 13:18:18 +0100227 * __drm_atomic_state_free - free all memory for an atomic state
228 * @ref: This atomic state to deallocate
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200229 *
230 * This frees all memory associated with an atomic state, including all the
231 * per-object state for planes, crtcs and connectors.
232 */
Chris Wilson08536952016-10-14 13:18:18 +0100233void __drm_atomic_state_free(struct kref *ref)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200234{
Chris Wilson08536952016-10-14 13:18:18 +0100235 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
236 struct drm_mode_config *config = &state->dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200237
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200238 drm_atomic_state_clear(state);
239
Daniel Vetter17a38d92015-02-22 12:24:16 +0100240 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200241
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200242 if (config->funcs->atomic_state_free) {
243 config->funcs->atomic_state_free(state);
244 } else {
245 drm_atomic_state_default_release(state);
246 kfree(state);
247 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200248}
Chris Wilson08536952016-10-14 13:18:18 +0100249EXPORT_SYMBOL(__drm_atomic_state_free);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200250
251/**
252 * drm_atomic_get_crtc_state - get crtc state
253 * @state: global atomic state object
254 * @crtc: crtc to get state object for
255 *
256 * This function returns the crtc state for the given crtc, allocating it if
257 * needed. It will also grab the relevant crtc lock to make sure that the state
258 * is consistent.
259 *
260 * Returns:
261 *
262 * Either the allocated state or the error code encoded into the pointer. When
263 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
264 * entire atomic sequence must be restarted. All other errors are fatal.
265 */
266struct drm_crtc_state *
267drm_atomic_get_crtc_state(struct drm_atomic_state *state,
268 struct drm_crtc *crtc)
269{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200270 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200271 struct drm_crtc_state *crtc_state;
272
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200273 WARN_ON(!state->acquire_ctx);
274
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200275 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
276 if (crtc_state)
277 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200278
279 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
280 if (ret)
281 return ERR_PTR(ret);
282
283 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
284 if (!crtc_state)
285 return ERR_PTR(-ENOMEM);
286
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200287 state->crtcs[index].state = crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100288 state->crtcs[index].old_state = crtc->state;
289 state->crtcs[index].new_state = crtc_state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200290 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200291 crtc_state->state = state;
292
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200293 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
294 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200295
296 return crtc_state;
297}
298EXPORT_SYMBOL(drm_atomic_get_crtc_state);
299
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900300static void set_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200301 struct drm_crtc *crtc, s32 __user *fence_ptr)
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900302{
303 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
304}
305
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200306static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900307 struct drm_crtc *crtc)
308{
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200309 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900310
311 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
312 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
313
314 return fence_ptr;
315}
316
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200317/**
Daniel Stone819364d2015-05-26 14:36:48 +0100318 * drm_atomic_set_mode_for_crtc - set mode for CRTC
319 * @state: the CRTC whose incoming state to update
320 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
321 *
Dhinakaran Pandiyancbef9092017-01-30 22:18:38 -0800322 * Set a mode (originating from the kernel) on the desired CRTC state and update
323 * the enable property.
Daniel Stone819364d2015-05-26 14:36:48 +0100324 *
325 * RETURNS:
326 * Zero on success, error code on failure. Cannot return -EDEADLK.
327 */
328int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
Ville Syrjälä91110a42017-05-18 22:38:36 +0300329 const struct drm_display_mode *mode)
Daniel Stone819364d2015-05-26 14:36:48 +0100330{
Daniel Stone99cf4a22015-05-25 19:11:51 +0100331 struct drm_mode_modeinfo umode;
332
Daniel Stone819364d2015-05-26 14:36:48 +0100333 /* Early return for no change. */
334 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
335 return 0;
336
Thierry Reding6472e502017-02-28 15:46:42 +0100337 drm_property_blob_put(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100338 state->mode_blob = NULL;
339
Daniel Stone819364d2015-05-26 14:36:48 +0100340 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100341 drm_mode_convert_to_umode(&umode, mode);
342 state->mode_blob =
343 drm_property_create_blob(state->crtc->dev,
344 sizeof(umode),
345 &umode);
346 if (IS_ERR(state->mode_blob))
347 return PTR_ERR(state->mode_blob);
348
Daniel Stone819364d2015-05-26 14:36:48 +0100349 drm_mode_copy(&state->mode, mode);
350 state->enable = true;
351 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
352 mode->name, state);
353 } else {
354 memset(&state->mode, 0, sizeof(state->mode));
355 state->enable = false;
356 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
357 state);
358 }
359
360 return 0;
361}
362EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
363
Daniel Stone819364d2015-05-26 14:36:48 +0100364/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100365 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
366 * @state: the CRTC whose incoming state to update
367 * @blob: pointer to blob property to use for mode
368 *
369 * Set a mode (originating from a blob property) on the desired CRTC state.
370 * This function will take a reference on the blob property for the CRTC state,
371 * and release the reference held on the state's existing mode property, if any
372 * was set.
373 *
374 * RETURNS:
375 * Zero on success, error code on failure. Cannot return -EDEADLK.
376 */
377int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
378 struct drm_property_blob *blob)
379{
380 if (blob == state->mode_blob)
381 return 0;
382
Thierry Reding6472e502017-02-28 15:46:42 +0100383 drm_property_blob_put(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100384 state->mode_blob = NULL;
385
Tomi Valkeinen67098872016-05-31 15:03:17 +0300386 memset(&state->mode, 0, sizeof(state->mode));
387
Daniel Stone955f3c32015-05-25 19:11:52 +0100388 if (blob) {
389 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
390 drm_mode_convert_umode(&state->mode,
391 (const struct drm_mode_modeinfo *)
392 blob->data))
393 return -EINVAL;
394
Thierry Reding6472e502017-02-28 15:46:42 +0100395 state->mode_blob = drm_property_blob_get(blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100396 state->enable = true;
397 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
398 state->mode.name, state);
399 } else {
Daniel Stone955f3c32015-05-25 19:11:52 +0100400 state->enable = false;
401 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
402 state);
403 }
404
405 return 0;
406}
407EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
408
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000409static int
Jyri Sarhadafee602017-04-21 12:51:13 +0300410drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000411 struct drm_property_blob **blob,
412 uint64_t blob_id,
413 ssize_t expected_size,
414 bool *replaced)
415{
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000416 struct drm_property_blob *new_blob = NULL;
417
418 if (blob_id != 0) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300419 new_blob = drm_property_lookup_blob(dev, blob_id);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000420 if (new_blob == NULL)
421 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100422
423 if (expected_size > 0 && expected_size != new_blob->length) {
Thierry Reding6472e502017-02-28 15:46:42 +0100424 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000425 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100426 }
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000427 }
428
Peter Rosin5f057ff2017-07-13 18:25:25 +0200429 *replaced |= drm_property_replace_blob(blob, new_blob);
Thierry Reding6472e502017-02-28 15:46:42 +0100430 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000431
432 return 0;
433}
434
435/**
Rob Clark40ecc692014-12-18 16:01:46 -0500436 * drm_atomic_crtc_set_property - set property on CRTC
437 * @crtc: the drm CRTC to set a property on
438 * @state: the state object to update with the new property value
439 * @property: the property to set
440 * @val: the new property value
441 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100442 * This function handles generic/core properties and calls out to driver's
443 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
444 * consistent behavior you must call this function rather than the driver hook
445 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500446 *
447 * RETURNS:
448 * Zero on success, error code on failure
449 */
450int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
451 struct drm_crtc_state *state, struct drm_property *property,
452 uint64_t val)
453{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100454 struct drm_device *dev = crtc->dev;
455 struct drm_mode_config *config = &dev->mode_config;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000456 bool replaced = false;
Daniel Stone955f3c32015-05-25 19:11:52 +0100457 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100458
Daniel Stone27798362015-03-19 04:33:26 +0000459 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100460 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100461 else if (property == config->prop_mode_id) {
462 struct drm_property_blob *mode =
463 drm_property_lookup_blob(dev, val);
464 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Thierry Reding6472e502017-02-28 15:46:42 +0100465 drm_property_blob_put(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100466 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000467 } else if (property == config->degamma_lut_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300468 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000469 &state->degamma_lut,
470 val,
471 -1,
472 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200473 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000474 return ret;
475 } else if (property == config->ctm_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300476 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000477 &state->ctm,
478 val,
479 sizeof(struct drm_color_ctm),
480 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200481 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000482 return ret;
483 } else if (property == config->gamma_lut_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300484 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000485 &state->gamma_lut,
486 val,
487 -1,
488 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200489 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000490 return ret;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900491 } else if (property == config->prop_out_fence_ptr) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200492 s32 __user *fence_ptr = u64_to_user_ptr(val);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900493
494 if (!fence_ptr)
495 return 0;
496
497 if (put_user(-1, fence_ptr))
498 return -EFAULT;
499
500 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000501 } else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500502 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000503 else
504 return -EINVAL;
505
506 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500507}
508EXPORT_SYMBOL(drm_atomic_crtc_set_property);
509
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100510/**
511 * drm_atomic_crtc_get_property - get property value from CRTC state
512 * @crtc: the drm CRTC to set a property on
513 * @state: the state object to get the property value from
514 * @property: the property to set
515 * @val: return location for the property value
516 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100517 * This function handles generic/core properties and calls out to driver's
518 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
519 * consistent behavior you must call this function rather than the driver hook
520 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100521 *
522 * RETURNS:
523 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500524 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700525static int
526drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500527 const struct drm_crtc_state *state,
528 struct drm_property *property, uint64_t *val)
529{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000530 struct drm_device *dev = crtc->dev;
531 struct drm_mode_config *config = &dev->mode_config;
532
533 if (property == config->prop_active)
534 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100535 else if (property == config->prop_mode_id)
536 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000537 else if (property == config->degamma_lut_property)
538 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
539 else if (property == config->ctm_property)
540 *val = (state->ctm) ? state->ctm->base.id : 0;
541 else if (property == config->gamma_lut_property)
542 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900543 else if (property == config->prop_out_fence_ptr)
544 *val = 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000545 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500546 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000547 else
548 return -EINVAL;
549
550 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500551}
Rob Clarkac9c9252014-12-18 16:01:47 -0500552
553/**
Rob Clark5e743732014-12-18 16:01:51 -0500554 * drm_atomic_crtc_check - check crtc state
555 * @crtc: crtc to check
556 * @state: crtc state to check
557 *
558 * Provides core sanity checks for crtc state.
559 *
560 * RETURNS:
561 * Zero on success, error code on failure
562 */
563static int drm_atomic_crtc_check(struct drm_crtc *crtc,
564 struct drm_crtc_state *state)
565{
566 /* NOTE: we explicitly don't enforce constraints such as primary
567 * layer covering entire screen, since that is something we want
568 * to allow (on hw that supports it). For hw that does not, it
569 * should be checked in driver's crtc->atomic_check() vfunc.
570 *
571 * TODO: Add generic modeset state checks once we support those.
572 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100573
574 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200575 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
576 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100577 return -EINVAL;
578 }
579
Daniel Stone99cf4a22015-05-25 19:11:51 +0100580 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
581 * as this is a kernel-internal detail that userspace should never
582 * be able to trigger. */
583 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
584 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200585 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
586 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100587 return -EINVAL;
588 }
589
590 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
591 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200592 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
593 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100594 return -EINVAL;
595 }
596
Daniel Vetter4cba6852015-12-08 09:49:20 +0100597 /*
598 * Reject event generation for when a CRTC is off and stays off.
599 * It wouldn't be hard to implement this, but userspace has a track
600 * record of happily burning through 100% cpu (or worse, crash) when the
601 * display pipe is suspended. To avoid all that fun just reject updates
602 * that ask for events since likely that indicates a bug in the
603 * compositor's drawing loop. This is consistent with the vblank IOCTL
604 * and legacy page_flip IOCTL which also reject service on a disabled
605 * pipe.
606 */
607 if (state->event && !state->active && !crtc->state->active) {
Russell King6ac7c542017-02-13 12:27:03 +0000608 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
609 crtc->base.id, crtc->name);
Daniel Vetter4cba6852015-12-08 09:49:20 +0100610 return -EINVAL;
611 }
612
Rob Clark5e743732014-12-18 16:01:51 -0500613 return 0;
614}
615
Rob Clarkfceffb322016-11-05 11:08:09 -0400616static void drm_atomic_crtc_print_state(struct drm_printer *p,
617 const struct drm_crtc_state *state)
618{
619 struct drm_crtc *crtc = state->crtc;
620
621 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
622 drm_printf(p, "\tenable=%d\n", state->enable);
623 drm_printf(p, "\tactive=%d\n", state->active);
624 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
625 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
626 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
627 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
628 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
629 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
630 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
631 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
632 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
633
634 if (crtc->funcs->atomic_print_state)
635 crtc->funcs->atomic_print_state(p, state);
636}
637
Rob Clark5e743732014-12-18 16:01:51 -0500638/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200639 * drm_atomic_get_plane_state - get plane state
640 * @state: global atomic state object
641 * @plane: plane to get state object for
642 *
643 * This function returns the plane state for the given plane, allocating it if
644 * needed. It will also grab the relevant plane lock to make sure that the state
645 * is consistent.
646 *
647 * Returns:
648 *
649 * Either the allocated state or the error code encoded into the pointer. When
650 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
651 * entire atomic sequence must be restarted. All other errors are fatal.
652 */
653struct drm_plane_state *
654drm_atomic_get_plane_state(struct drm_atomic_state *state,
655 struct drm_plane *plane)
656{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200657 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200658 struct drm_plane_state *plane_state;
659
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200660 WARN_ON(!state->acquire_ctx);
661
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200662 plane_state = drm_atomic_get_existing_plane_state(state, plane);
663 if (plane_state)
664 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200665
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100666 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200667 if (ret)
668 return ERR_PTR(ret);
669
670 plane_state = plane->funcs->atomic_duplicate_state(plane);
671 if (!plane_state)
672 return ERR_PTR(-ENOMEM);
673
Daniel Vetterb8b53422016-06-02 00:06:33 +0200674 state->planes[index].state = plane_state;
675 state->planes[index].ptr = plane;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100676 state->planes[index].old_state = plane->state;
677 state->planes[index].new_state = plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200678 plane_state->state = state;
679
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200680 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
681 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200682
683 if (plane_state->crtc) {
684 struct drm_crtc_state *crtc_state;
685
686 crtc_state = drm_atomic_get_crtc_state(state,
687 plane_state->crtc);
688 if (IS_ERR(crtc_state))
689 return ERR_CAST(crtc_state);
690 }
691
692 return plane_state;
693}
694EXPORT_SYMBOL(drm_atomic_get_plane_state);
695
696/**
Rob Clark40ecc692014-12-18 16:01:46 -0500697 * drm_atomic_plane_set_property - set property on plane
698 * @plane: the drm plane to set a property on
699 * @state: the state object to update with the new property value
700 * @property: the property to set
701 * @val: the new property value
702 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100703 * This function handles generic/core properties and calls out to driver's
704 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
705 * consistent behavior you must call this function rather than the driver hook
706 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500707 *
708 * RETURNS:
709 * Zero on success, error code on failure
710 */
Daniel Vettere90271b2017-07-25 10:01:19 +0200711static int drm_atomic_plane_set_property(struct drm_plane *plane,
Rob Clark40ecc692014-12-18 16:01:46 -0500712 struct drm_plane_state *state, struct drm_property *property,
713 uint64_t val)
714{
Rob Clark6b4959f2014-12-18 16:01:53 -0500715 struct drm_device *dev = plane->dev;
716 struct drm_mode_config *config = &dev->mode_config;
717
718 if (property == config->prop_fb_id) {
Keith Packard418da172017-03-14 23:25:07 -0700719 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500720 drm_atomic_set_fb_for_plane(state, fb);
721 if (fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +0100722 drm_framebuffer_put(fb);
Gustavo Padovan96260142016-11-15 22:06:39 +0900723 } else if (property == config->prop_in_fence_fd) {
724 if (state->fence)
725 return -EINVAL;
726
727 if (U642I64(val) == -1)
728 return 0;
729
730 state->fence = sync_file_get_fence(val);
731 if (!state->fence)
732 return -EINVAL;
733
Rob Clark6b4959f2014-12-18 16:01:53 -0500734 } else if (property == config->prop_crtc_id) {
Keith Packard418da172017-03-14 23:25:07 -0700735 struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500736 return drm_atomic_set_crtc_for_plane(state, crtc);
737 } else if (property == config->prop_crtc_x) {
738 state->crtc_x = U642I64(val);
739 } else if (property == config->prop_crtc_y) {
740 state->crtc_y = U642I64(val);
741 } else if (property == config->prop_crtc_w) {
742 state->crtc_w = val;
743 } else if (property == config->prop_crtc_h) {
744 state->crtc_h = val;
745 } else if (property == config->prop_src_x) {
746 state->src_x = val;
747 } else if (property == config->prop_src_y) {
748 state->src_y = val;
749 } else if (property == config->prop_src_w) {
750 state->src_w = val;
751 } else if (property == config->prop_src_h) {
752 state->src_h = val;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300753 } else if (property == plane->rotation_property) {
Robert Fossc2c446a2017-05-19 16:50:17 -0400754 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK))
Ville Syrjälä6e0c7c32016-09-26 19:30:47 +0300755 return -EINVAL;
Matt Roper1da30622015-01-21 16:35:40 -0800756 state->rotation = val;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200757 } else if (property == plane->zpos_property) {
758 state->zpos = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500759 } else if (plane->funcs->atomic_set_property) {
760 return plane->funcs->atomic_set_property(plane, state,
761 property, val);
762 } else {
763 return -EINVAL;
764 }
765
766 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500767}
Rob Clark40ecc692014-12-18 16:01:46 -0500768
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100769/**
770 * drm_atomic_plane_get_property - get property value from plane state
771 * @plane: the drm plane to set a property on
772 * @state: the state object to get the property value from
773 * @property: the property to set
774 * @val: return location for the property value
775 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100776 * This function handles generic/core properties and calls out to driver's
777 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
778 * consistent behavior you must call this function rather than the driver hook
779 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100780 *
781 * RETURNS:
782 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500783 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100784static int
785drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500786 const struct drm_plane_state *state,
787 struct drm_property *property, uint64_t *val)
788{
Rob Clark6b4959f2014-12-18 16:01:53 -0500789 struct drm_device *dev = plane->dev;
790 struct drm_mode_config *config = &dev->mode_config;
791
792 if (property == config->prop_fb_id) {
793 *val = (state->fb) ? state->fb->base.id : 0;
Gustavo Padovan96260142016-11-15 22:06:39 +0900794 } else if (property == config->prop_in_fence_fd) {
795 *val = -1;
Rob Clark6b4959f2014-12-18 16:01:53 -0500796 } else if (property == config->prop_crtc_id) {
797 *val = (state->crtc) ? state->crtc->base.id : 0;
798 } else if (property == config->prop_crtc_x) {
799 *val = I642U64(state->crtc_x);
800 } else if (property == config->prop_crtc_y) {
801 *val = I642U64(state->crtc_y);
802 } else if (property == config->prop_crtc_w) {
803 *val = state->crtc_w;
804 } else if (property == config->prop_crtc_h) {
805 *val = state->crtc_h;
806 } else if (property == config->prop_src_x) {
807 *val = state->src_x;
808 } else if (property == config->prop_src_y) {
809 *val = state->src_y;
810 } else if (property == config->prop_src_w) {
811 *val = state->src_w;
812 } else if (property == config->prop_src_h) {
813 *val = state->src_h;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300814 } else if (property == plane->rotation_property) {
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000815 *val = state->rotation;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200816 } else if (property == plane->zpos_property) {
817 *val = state->zpos;
Rob Clark6b4959f2014-12-18 16:01:53 -0500818 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500819 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500820 } else {
821 return -EINVAL;
822 }
823
824 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500825}
Rob Clarkac9c9252014-12-18 16:01:47 -0500826
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200827static bool
828plane_switching_crtc(struct drm_atomic_state *state,
829 struct drm_plane *plane,
830 struct drm_plane_state *plane_state)
831{
832 if (!plane->state->crtc || !plane_state->crtc)
833 return false;
834
835 if (plane->state->crtc == plane_state->crtc)
836 return false;
837
838 /* This could be refined, but currently there's no helper or driver code
839 * to implement direct switching of active planes nor userspace to take
840 * advantage of more direct plane switching without the intermediate
841 * full OFF state.
842 */
843 return true;
844}
845
Rob Clarkac9c9252014-12-18 16:01:47 -0500846/**
Rob Clark5e743732014-12-18 16:01:51 -0500847 * drm_atomic_plane_check - check plane state
848 * @plane: plane to check
849 * @state: plane state to check
850 *
851 * Provides core sanity checks for plane state.
852 *
853 * RETURNS:
854 * Zero on success, error code on failure
855 */
856static int drm_atomic_plane_check(struct drm_plane *plane,
857 struct drm_plane_state *state)
858{
859 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200860 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500861
862 /* either *both* CRTC and FB must be set, or neither */
863 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100864 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500865 return -EINVAL;
866 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100867 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500868 return -EINVAL;
869 }
870
871 /* if disabled, we don't care about the rest of the state: */
872 if (!state->crtc)
873 return 0;
874
875 /* Check whether this plane is usable on this CRTC */
876 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100877 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500878 return -EINVAL;
879 }
880
881 /* Check whether this plane supports the fb pixel format. */
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200882 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
Laurent Pinchartead86102015-03-05 02:25:43 +0200883 if (ret) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000884 struct drm_format_name_buf format_name;
885 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200886 drm_get_format_name(state->fb->format->format,
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000887 &format_name));
Laurent Pinchartead86102015-03-05 02:25:43 +0200888 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500889 }
890
891 /* Give drivers some help against integer overflows */
892 if (state->crtc_w > INT_MAX ||
893 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
894 state->crtc_h > INT_MAX ||
895 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100896 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
897 state->crtc_w, state->crtc_h,
898 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500899 return -ERANGE;
900 }
901
902 fb_width = state->fb->width << 16;
903 fb_height = state->fb->height << 16;
904
905 /* Make sure source coordinates are inside the fb. */
906 if (state->src_w > fb_width ||
907 state->src_x > fb_width - state->src_w ||
908 state->src_h > fb_height ||
909 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100910 DRM_DEBUG_ATOMIC("Invalid source coordinates "
Ville Syrjälä0338f0d2017-11-01 20:35:33 +0200911 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100912 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
913 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
914 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
Ville Syrjälä0338f0d2017-11-01 20:35:33 +0200915 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10,
916 state->fb->width, state->fb->height);
Rob Clark5e743732014-12-18 16:01:51 -0500917 return -ENOSPC;
918 }
919
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200920 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200921 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
922 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200923 return -EINVAL;
924 }
925
Rob Clark5e743732014-12-18 16:01:51 -0500926 return 0;
927}
928
Rob Clarkfceffb322016-11-05 11:08:09 -0400929static void drm_atomic_plane_print_state(struct drm_printer *p,
930 const struct drm_plane_state *state)
931{
932 struct drm_plane *plane = state->plane;
933 struct drm_rect src = drm_plane_state_src(state);
934 struct drm_rect dest = drm_plane_state_dest(state);
935
936 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
937 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
938 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
Noralf Trønnesf02b6042017-11-07 20:13:41 +0100939 if (state->fb)
940 drm_framebuffer_print_info(p, 2, state->fb);
Rob Clarkfceffb322016-11-05 11:08:09 -0400941 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
942 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
943 drm_printf(p, "\trotation=%x\n", state->rotation);
944
945 if (plane->funcs->atomic_print_state)
946 plane->funcs->atomic_print_state(p, state);
947}
948
Rob Clark5e743732014-12-18 16:01:51 -0500949/**
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300950 * drm_atomic_private_obj_init - initialize private object
951 * @obj: private object
952 * @state: initial private object state
953 * @funcs: pointer to the struct of function pointers that identify the object
954 * type
955 *
956 * Initialize the private object, which can be embedded into any
957 * driver private object that needs its own atomic state.
958 */
959void
960drm_atomic_private_obj_init(struct drm_private_obj *obj,
961 struct drm_private_state *state,
962 const struct drm_private_state_funcs *funcs)
963{
964 memset(obj, 0, sizeof(*obj));
965
966 obj->state = state;
967 obj->funcs = funcs;
968}
969EXPORT_SYMBOL(drm_atomic_private_obj_init);
970
971/**
972 * drm_atomic_private_obj_fini - finalize private object
973 * @obj: private object
974 *
975 * Finalize the private object.
976 */
977void
978drm_atomic_private_obj_fini(struct drm_private_obj *obj)
979{
980 obj->funcs->atomic_destroy_state(obj, obj->state);
981}
982EXPORT_SYMBOL(drm_atomic_private_obj_fini);
983
984/**
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700985 * drm_atomic_get_private_obj_state - get private object state
986 * @state: global atomic state
987 * @obj: private object to get the state for
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700988 *
989 * This function returns the private object state for the given private object,
990 * allocating the state if needed. It does not grab any locks as the caller is
991 * expected to care of any required locking.
992 *
993 * RETURNS:
994 *
995 * Either the allocated state or the error code encoded into a pointer.
996 */
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300997struct drm_private_state *
998drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
999 struct drm_private_obj *obj)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001000{
1001 int index, num_objs, i;
1002 size_t size;
1003 struct __drm_private_objs_state *arr;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001004 struct drm_private_state *obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001005
1006 for (i = 0; i < state->num_private_objs; i++)
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001007 if (obj == state->private_objs[i].ptr)
1008 return state->private_objs[i].state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001009
1010 num_objs = state->num_private_objs + 1;
1011 size = sizeof(*state->private_objs) * num_objs;
1012 arr = krealloc(state->private_objs, size, GFP_KERNEL);
1013 if (!arr)
1014 return ERR_PTR(-ENOMEM);
1015
1016 state->private_objs = arr;
1017 index = state->num_private_objs;
1018 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1019
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001020 obj_state = obj->funcs->atomic_duplicate_state(obj);
1021 if (!obj_state)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001022 return ERR_PTR(-ENOMEM);
1023
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001024 state->private_objs[index].state = obj_state;
1025 state->private_objs[index].old_state = obj->state;
1026 state->private_objs[index].new_state = obj_state;
1027 state->private_objs[index].ptr = obj;
1028
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001029 state->num_private_objs = num_objs;
1030
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001031 DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
1032 obj, obj_state, state);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001033
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001034 return obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001035}
1036EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1037
1038/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001039 * drm_atomic_get_connector_state - get connector state
1040 * @state: global atomic state object
1041 * @connector: connector to get state object for
1042 *
1043 * This function returns the connector state for the given connector,
1044 * allocating it if needed. It will also grab the relevant connector lock to
1045 * make sure that the state is consistent.
1046 *
1047 * Returns:
1048 *
1049 * Either the allocated state or the error code encoded into the pointer. When
1050 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1051 * entire atomic sequence must be restarted. All other errors are fatal.
1052 */
1053struct drm_connector_state *
1054drm_atomic_get_connector_state(struct drm_atomic_state *state,
1055 struct drm_connector *connector)
1056{
1057 int ret, index;
1058 struct drm_mode_config *config = &connector->dev->mode_config;
1059 struct drm_connector_state *connector_state;
1060
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +02001061 WARN_ON(!state->acquire_ctx);
1062
Daniel Vetterc7eb76f2014-11-19 18:38:06 +01001063 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1064 if (ret)
1065 return ERR_PTR(ret);
1066
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001067 index = drm_connector_index(connector);
1068
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001069 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +02001070 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001071 int alloc = max(index + 1, config->num_connector);
1072
1073 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1074 if (!c)
1075 return ERR_PTR(-ENOMEM);
1076
1077 state->connectors = c;
1078 memset(&state->connectors[state->num_connector], 0,
1079 sizeof(*state->connectors) * (alloc - state->num_connector));
1080
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001081 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001082 }
1083
Daniel Vetter63e83c12016-06-02 00:06:32 +02001084 if (state->connectors[index].state)
1085 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001086
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001087 connector_state = connector->funcs->atomic_duplicate_state(connector);
1088 if (!connector_state)
1089 return ERR_PTR(-ENOMEM);
1090
Thierry Redingad093602017-02-28 15:46:39 +01001091 drm_connector_get(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +02001092 state->connectors[index].state = connector_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01001093 state->connectors[index].old_state = connector->state;
1094 state->connectors[index].new_state = connector_state;
Daniel Vetter63e83c12016-06-02 00:06:32 +02001095 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001096 connector_state->state = state;
1097
Russell King6ac7c542017-02-13 12:27:03 +00001098 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1099 connector->base.id, connector->name,
1100 connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001101
1102 if (connector_state->crtc) {
1103 struct drm_crtc_state *crtc_state;
1104
1105 crtc_state = drm_atomic_get_crtc_state(state,
1106 connector_state->crtc);
1107 if (IS_ERR(crtc_state))
1108 return ERR_CAST(crtc_state);
1109 }
1110
1111 return connector_state;
1112}
1113EXPORT_SYMBOL(drm_atomic_get_connector_state);
1114
1115/**
Rob Clark40ecc692014-12-18 16:01:46 -05001116 * drm_atomic_connector_set_property - set property on connector.
1117 * @connector: the drm connector to set a property on
1118 * @state: the state object to update with the new property value
1119 * @property: the property to set
1120 * @val: the new property value
1121 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001122 * This function handles generic/core properties and calls out to driver's
1123 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1124 * consistent behavior you must call this function rather than the driver hook
1125 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -05001126 *
1127 * RETURNS:
1128 * Zero on success, error code on failure
1129 */
Daniel Vetter482b0e32017-07-25 10:01:20 +02001130static int drm_atomic_connector_set_property(struct drm_connector *connector,
Rob Clark40ecc692014-12-18 16:01:46 -05001131 struct drm_connector_state *state, struct drm_property *property,
1132 uint64_t val)
1133{
1134 struct drm_device *dev = connector->dev;
1135 struct drm_mode_config *config = &dev->mode_config;
1136
Rob Clarkae16c592014-12-18 16:01:54 -05001137 if (property == config->prop_crtc_id) {
Keith Packard418da172017-03-14 23:25:07 -07001138 struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
Rob Clarkae16c592014-12-18 16:01:54 -05001139 return drm_atomic_set_crtc_for_connector(state, crtc);
1140 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -05001141 /* setting DPMS property requires special handling, which
1142 * is done in legacy setprop path for us. Disallow (for
1143 * now?) atomic writes to DPMS property:
1144 */
1145 return -EINVAL;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001146 } else if (property == config->tv_select_subconnector_property) {
1147 state->tv.subconnector = val;
1148 } else if (property == config->tv_left_margin_property) {
1149 state->tv.margins.left = val;
1150 } else if (property == config->tv_right_margin_property) {
1151 state->tv.margins.right = val;
1152 } else if (property == config->tv_top_margin_property) {
1153 state->tv.margins.top = val;
1154 } else if (property == config->tv_bottom_margin_property) {
1155 state->tv.margins.bottom = val;
1156 } else if (property == config->tv_mode_property) {
1157 state->tv.mode = val;
1158 } else if (property == config->tv_brightness_property) {
1159 state->tv.brightness = val;
1160 } else if (property == config->tv_contrast_property) {
1161 state->tv.contrast = val;
1162 } else if (property == config->tv_flicker_reduction_property) {
1163 state->tv.flicker_reduction = val;
1164 } else if (property == config->tv_overscan_property) {
1165 state->tv.overscan = val;
1166 } else if (property == config->tv_saturation_property) {
1167 state->tv.saturation = val;
1168 } else if (property == config->tv_hue_property) {
1169 state->tv.hue = val;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001170 } else if (property == config->link_status_property) {
1171 /* Never downgrade from GOOD to BAD on userspace's request here,
1172 * only hw issues can do that.
1173 *
1174 * For an atomic property the userspace doesn't need to be able
1175 * to understand all the properties, but needs to be able to
1176 * restore the state it wants on VT switch. So if the userspace
1177 * tries to change the link_status from GOOD to BAD, driver
1178 * silently rejects it and returns a 0. This prevents userspace
1179 * from accidently breaking the display when it restores the
1180 * state.
1181 */
1182 if (state->link_status != DRM_LINK_STATUS_GOOD)
1183 state->link_status = val;
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02001184 } else if (property == config->aspect_ratio_property) {
1185 state->picture_aspect_ratio = val;
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001186 } else if (property == connector->scaling_mode_property) {
1187 state->scaling_mode = val;
Rob Clark40ecc692014-12-18 16:01:46 -05001188 } else if (connector->funcs->atomic_set_property) {
1189 return connector->funcs->atomic_set_property(connector,
1190 state, property, val);
1191 } else {
1192 return -EINVAL;
1193 }
Boris Brezillon299a16b2016-12-02 14:48:09 +01001194
1195 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -05001196}
Rob Clark40ecc692014-12-18 16:01:46 -05001197
Rob Clarkfceffb322016-11-05 11:08:09 -04001198static void drm_atomic_connector_print_state(struct drm_printer *p,
1199 const struct drm_connector_state *state)
1200{
1201 struct drm_connector *connector = state->connector;
1202
1203 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1204 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1205
1206 if (connector->funcs->atomic_print_state)
1207 connector->funcs->atomic_print_state(p, state);
1208}
1209
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001210/**
1211 * drm_atomic_connector_get_property - get property value from connector state
1212 * @connector: the drm connector to set a property on
1213 * @state: the state object to get the property value from
1214 * @property: the property to set
1215 * @val: return location for the property value
1216 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001217 * This function handles generic/core properties and calls out to driver's
1218 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1219 * consistent behavior you must call this function rather than the driver hook
1220 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001221 *
1222 * RETURNS:
1223 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -05001224 */
Daniel Vettera97df1c2014-12-18 22:49:02 +01001225static int
1226drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -05001227 const struct drm_connector_state *state,
1228 struct drm_property *property, uint64_t *val)
1229{
1230 struct drm_device *dev = connector->dev;
1231 struct drm_mode_config *config = &dev->mode_config;
1232
Rob Clarkae16c592014-12-18 16:01:54 -05001233 if (property == config->prop_crtc_id) {
1234 *val = (state->crtc) ? state->crtc->base.id : 0;
1235 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -05001236 *val = connector->dpms;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001237 } else if (property == config->tv_select_subconnector_property) {
1238 *val = state->tv.subconnector;
1239 } else if (property == config->tv_left_margin_property) {
1240 *val = state->tv.margins.left;
1241 } else if (property == config->tv_right_margin_property) {
1242 *val = state->tv.margins.right;
1243 } else if (property == config->tv_top_margin_property) {
1244 *val = state->tv.margins.top;
1245 } else if (property == config->tv_bottom_margin_property) {
1246 *val = state->tv.margins.bottom;
1247 } else if (property == config->tv_mode_property) {
1248 *val = state->tv.mode;
1249 } else if (property == config->tv_brightness_property) {
1250 *val = state->tv.brightness;
1251 } else if (property == config->tv_contrast_property) {
1252 *val = state->tv.contrast;
1253 } else if (property == config->tv_flicker_reduction_property) {
1254 *val = state->tv.flicker_reduction;
1255 } else if (property == config->tv_overscan_property) {
1256 *val = state->tv.overscan;
1257 } else if (property == config->tv_saturation_property) {
1258 *val = state->tv.saturation;
1259 } else if (property == config->tv_hue_property) {
1260 *val = state->tv.hue;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001261 } else if (property == config->link_status_property) {
1262 *val = state->link_status;
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02001263 } else if (property == config->aspect_ratio_property) {
1264 *val = state->picture_aspect_ratio;
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001265 } else if (property == connector->scaling_mode_property) {
1266 *val = state->scaling_mode;
Rob Clarkac9c9252014-12-18 16:01:47 -05001267 } else if (connector->funcs->atomic_get_property) {
1268 return connector->funcs->atomic_get_property(connector,
1269 state, property, val);
1270 } else {
1271 return -EINVAL;
1272 }
1273
1274 return 0;
1275}
Rob Clarkac9c9252014-12-18 16:01:47 -05001276
Rob Clark88a48e22014-12-18 16:01:50 -05001277int drm_atomic_get_property(struct drm_mode_object *obj,
1278 struct drm_property *property, uint64_t *val)
1279{
1280 struct drm_device *dev = property->dev;
1281 int ret;
1282
1283 switch (obj->type) {
1284 case DRM_MODE_OBJECT_CONNECTOR: {
1285 struct drm_connector *connector = obj_to_connector(obj);
1286 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1287 ret = drm_atomic_connector_get_property(connector,
1288 connector->state, property, val);
1289 break;
1290 }
1291 case DRM_MODE_OBJECT_CRTC: {
1292 struct drm_crtc *crtc = obj_to_crtc(obj);
1293 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1294 ret = drm_atomic_crtc_get_property(crtc,
1295 crtc->state, property, val);
1296 break;
1297 }
1298 case DRM_MODE_OBJECT_PLANE: {
1299 struct drm_plane *plane = obj_to_plane(obj);
1300 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1301 ret = drm_atomic_plane_get_property(plane,
1302 plane->state, property, val);
1303 break;
1304 }
1305 default:
1306 ret = -EINVAL;
1307 break;
1308 }
1309
1310 return ret;
1311}
1312
1313/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001314 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001315 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001316 * @crtc: crtc to use for the plane
1317 *
1318 * Changing the assigned crtc for a plane requires us to grab the lock and state
1319 * for the new crtc, as needed. This function takes care of all these details
1320 * besides updating the pointer in the state object itself.
1321 *
1322 * Returns:
1323 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1324 * then the w/w mutex code has detected a deadlock and the entire atomic
1325 * sequence must be restarted. All other errors are fatal.
1326 */
1327int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001328drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1329 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001330{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001331 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001332 struct drm_crtc_state *crtc_state;
1333
Rob Clark6ddd3882014-11-21 15:28:31 -05001334 if (plane_state->crtc) {
1335 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1336 plane_state->crtc);
1337 if (WARN_ON(IS_ERR(crtc_state)))
1338 return PTR_ERR(crtc_state);
1339
1340 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1341 }
1342
1343 plane_state->crtc = crtc;
1344
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001345 if (crtc) {
1346 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1347 crtc);
1348 if (IS_ERR(crtc_state))
1349 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -05001350 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001351 }
1352
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001353 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001354 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1355 plane_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001356 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001357 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1358 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001359
1360 return 0;
1361}
1362EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1363
1364/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001365 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001366 * @plane_state: atomic state object for the plane
1367 * @fb: fb to use for the plane
1368 *
1369 * Changing the assigned framebuffer for a plane requires us to grab a reference
1370 * to the new fb and drop the reference to the old fb, if there is one. This
1371 * function takes care of all these details besides updating the pointer in the
1372 * state object itself.
1373 */
1374void
1375drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1376 struct drm_framebuffer *fb)
1377{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001378 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001379 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1380 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001381 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001382 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1383 plane_state);
Chris Wilson389f78b2016-11-25 15:32:30 +00001384
1385 drm_framebuffer_assign(&plane_state->fb, fb);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001386}
1387EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1388
1389/**
Gustavo Padovan13b55662016-11-07 19:03:30 +09001390 * drm_atomic_set_fence_for_plane - set fence for plane
1391 * @plane_state: atomic state object for the plane
1392 * @fence: dma_fence to use for the plane
1393 *
1394 * Helper to setup the plane_state fence in case it is not set yet.
1395 * By using this drivers doesn't need to worry if the user choose
1396 * implicit or explicit fencing.
1397 *
1398 * This function will not set the fence to the state if it was set
Daniel Vetterd5745282017-01-25 07:26:45 +01001399 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1400 * drop the reference to the fence as we are not storing it anywhere.
1401 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1402 * with the received implicit fence. In both cases this function consumes a
1403 * reference for @fence.
Gustavo Padovan13b55662016-11-07 19:03:30 +09001404 */
1405void
1406drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1407 struct dma_fence *fence)
1408{
1409 if (plane_state->fence) {
1410 dma_fence_put(fence);
1411 return;
1412 }
1413
1414 plane_state->fence = fence;
1415}
1416EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1417
1418/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001419 * drm_atomic_set_crtc_for_connector - set crtc for connector
1420 * @conn_state: atomic state object for the connector
1421 * @crtc: crtc to use for the connector
1422 *
1423 * Changing the assigned crtc for a connector requires us to grab the lock and
1424 * state for the new crtc, as needed. This function takes care of all these
1425 * details besides updating the pointer in the state object itself.
1426 *
1427 * Returns:
1428 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1429 * then the w/w mutex code has detected a deadlock and the entire atomic
1430 * sequence must be restarted. All other errors are fatal.
1431 */
1432int
1433drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1434 struct drm_crtc *crtc)
1435{
1436 struct drm_crtc_state *crtc_state;
1437
Chris Wilsone2d800a2016-05-06 12:47:45 +01001438 if (conn_state->crtc == crtc)
1439 return 0;
1440
1441 if (conn_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001442 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1443 conn_state->crtc);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001444
1445 crtc_state->connector_mask &=
1446 ~(1 << drm_connector_index(conn_state->connector));
Chris Wilsone2d800a2016-05-06 12:47:45 +01001447
Thierry Redingad093602017-02-28 15:46:39 +01001448 drm_connector_put(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001449 conn_state->crtc = NULL;
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001450 }
1451
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001452 if (crtc) {
1453 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1454 if (IS_ERR(crtc_state))
1455 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001456
1457 crtc_state->connector_mask |=
1458 1 << drm_connector_index(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001459
Thierry Redingad093602017-02-28 15:46:39 +01001460 drm_connector_get(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001461 conn_state->crtc = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001462
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001463 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1464 conn_state, crtc->base.id, crtc->name);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001465 } else {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001466 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1467 conn_state);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001468 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001469
1470 return 0;
1471}
1472EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1473
1474/**
1475 * drm_atomic_add_affected_connectors - add connectors for crtc
1476 * @state: atomic state
1477 * @crtc: DRM crtc
1478 *
1479 * This function walks the current configuration and adds all connectors
1480 * currently using @crtc to the atomic configuration @state. Note that this
1481 * function must acquire the connection mutex. This can potentially cause
1482 * unneeded seralization if the update is just for the planes on one crtc. Hence
1483 * drivers and helpers should only call this when really needed (e.g. when a
1484 * full modeset needs to happen due to some change).
1485 *
1486 * Returns:
1487 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1488 * then the w/w mutex code has detected a deadlock and the entire atomic
1489 * sequence must be restarted. All other errors are fatal.
1490 */
1491int
1492drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1493 struct drm_crtc *crtc)
1494{
1495 struct drm_mode_config *config = &state->dev->mode_config;
1496 struct drm_connector *connector;
1497 struct drm_connector_state *conn_state;
Daniel Vetter613051d2016-12-14 00:08:06 +01001498 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001499 struct drm_crtc_state *crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001500 int ret;
1501
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001502 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1503 if (IS_ERR(crtc_state))
1504 return PTR_ERR(crtc_state);
1505
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001506 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1507 if (ret)
1508 return ret;
1509
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001510 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1511 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001512
1513 /*
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001514 * Changed connectors are already in @state, so only need to look
1515 * at the connector_mask in crtc_state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001516 */
Thierry Redingb982dab2017-02-28 15:46:43 +01001517 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +01001518 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001519 if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001520 continue;
1521
1522 conn_state = drm_atomic_get_connector_state(state, connector);
Daniel Vetter613051d2016-12-14 00:08:06 +01001523 if (IS_ERR(conn_state)) {
Thierry Redingb982dab2017-02-28 15:46:43 +01001524 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001525 return PTR_ERR(conn_state);
Daniel Vetter613051d2016-12-14 00:08:06 +01001526 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001527 }
Thierry Redingb982dab2017-02-28 15:46:43 +01001528 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001529
1530 return 0;
1531}
1532EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1533
1534/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001535 * drm_atomic_add_affected_planes - add planes for crtc
1536 * @state: atomic state
1537 * @crtc: DRM crtc
1538 *
1539 * This function walks the current configuration and adds all planes
1540 * currently used by @crtc to the atomic configuration @state. This is useful
1541 * when an atomic commit also needs to check all currently enabled plane on
1542 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1543 * to avoid special code to force-enable all planes.
1544 *
1545 * Since acquiring a plane state will always also acquire the w/w mutex of the
1546 * current CRTC for that plane (if there is any) adding all the plane states for
1547 * a CRTC will not reduce parallism of atomic updates.
1548 *
1549 * Returns:
1550 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1551 * then the w/w mutex code has detected a deadlock and the entire atomic
1552 * sequence must be restarted. All other errors are fatal.
1553 */
1554int
1555drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1556 struct drm_crtc *crtc)
1557{
1558 struct drm_plane *plane;
1559
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001560 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001561
1562 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1563 struct drm_plane_state *plane_state =
1564 drm_atomic_get_plane_state(state, plane);
1565
1566 if (IS_ERR(plane_state))
1567 return PTR_ERR(plane_state);
1568 }
1569 return 0;
1570}
1571EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1572
1573/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001574 * drm_atomic_check_only - check whether a given config would work
1575 * @state: atomic configuration to check
1576 *
1577 * Note that this function can return -EDEADLK if the driver needed to acquire
1578 * more locks but encountered a deadlock. The caller must then do the usual w/w
1579 * backoff dance and restart. All other errors are fatal.
1580 *
1581 * Returns:
1582 * 0 on success, negative error code on failure.
1583 */
1584int drm_atomic_check_only(struct drm_atomic_state *state)
1585{
Rob Clark5e743732014-12-18 16:01:51 -05001586 struct drm_device *dev = state->dev;
1587 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001588 struct drm_plane *plane;
1589 struct drm_plane_state *plane_state;
1590 struct drm_crtc *crtc;
1591 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001592 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001593
Daniel Vetter17a38d92015-02-22 12:24:16 +01001594 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001595
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001596 for_each_new_plane_in_state(state, plane, plane_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001597 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001598 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001599 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1600 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001601 return ret;
1602 }
1603 }
1604
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001605 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001606 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001607 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001608 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1609 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001610 return ret;
1611 }
1612 }
1613
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001614 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001615 ret = config->funcs->atomic_check(state->dev, state);
1616
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001617 if (ret)
1618 return ret;
1619
Rob Clarkd34f20d2014-12-18 16:01:56 -05001620 if (!state->allow_modeset) {
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001621 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001622 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001623 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1624 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001625 return -EINVAL;
1626 }
1627 }
1628 }
1629
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001630 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001631}
1632EXPORT_SYMBOL(drm_atomic_check_only);
1633
1634/**
1635 * drm_atomic_commit - commit configuration atomically
1636 * @state: atomic configuration to check
1637 *
1638 * Note that this function can return -EDEADLK if the driver needed to acquire
1639 * more locks but encountered a deadlock. The caller must then do the usual w/w
1640 * backoff dance and restart. All other errors are fatal.
1641 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001642 * This function will take its own reference on @state.
1643 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001644 *
1645 * Returns:
1646 * 0 on success, negative error code on failure.
1647 */
1648int drm_atomic_commit(struct drm_atomic_state *state)
1649{
1650 struct drm_mode_config *config = &state->dev->mode_config;
1651 int ret;
1652
1653 ret = drm_atomic_check_only(state);
1654 if (ret)
1655 return ret;
1656
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001657 DRM_DEBUG_ATOMIC("committing %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001658
1659 return config->funcs->atomic_commit(state->dev, state, false);
1660}
1661EXPORT_SYMBOL(drm_atomic_commit);
1662
1663/**
Daniel Vetterd5745282017-01-25 07:26:45 +01001664 * drm_atomic_nonblocking_commit - atomic nonblocking commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001665 * @state: atomic configuration to check
1666 *
1667 * Note that this function can return -EDEADLK if the driver needed to acquire
1668 * more locks but encountered a deadlock. The caller must then do the usual w/w
1669 * backoff dance and restart. All other errors are fatal.
1670 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001671 * This function will take its own reference on @state.
1672 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001673 *
1674 * Returns:
1675 * 0 on success, negative error code on failure.
1676 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001677int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001678{
1679 struct drm_mode_config *config = &state->dev->mode_config;
1680 int ret;
1681
1682 ret = drm_atomic_check_only(state);
1683 if (ret)
1684 return ret;
1685
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001686 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001687
1688 return config->funcs->atomic_commit(state->dev, state, true);
1689}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001690EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001691
Rob Clarkfceffb322016-11-05 11:08:09 -04001692static void drm_atomic_print_state(const struct drm_atomic_state *state)
1693{
1694 struct drm_printer p = drm_info_printer(state->dev->dev);
1695 struct drm_plane *plane;
1696 struct drm_plane_state *plane_state;
1697 struct drm_crtc *crtc;
1698 struct drm_crtc_state *crtc_state;
1699 struct drm_connector *connector;
1700 struct drm_connector_state *connector_state;
1701 int i;
1702
1703 DRM_DEBUG_ATOMIC("checking %p\n", state);
1704
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001705 for_each_new_plane_in_state(state, plane, plane_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001706 drm_atomic_plane_print_state(&p, plane_state);
1707
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001708 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001709 drm_atomic_crtc_print_state(&p, crtc_state);
1710
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001711 for_each_new_connector_in_state(state, connector, connector_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001712 drm_atomic_connector_print_state(&p, connector_state);
1713}
1714
Daniel Vetterc2d85562017-04-03 10:32:54 +02001715static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1716 bool take_locks)
1717{
1718 struct drm_mode_config *config = &dev->mode_config;
1719 struct drm_plane *plane;
1720 struct drm_crtc *crtc;
1721 struct drm_connector *connector;
1722 struct drm_connector_list_iter conn_iter;
1723
1724 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1725 return;
1726
1727 list_for_each_entry(plane, &config->plane_list, head) {
1728 if (take_locks)
1729 drm_modeset_lock(&plane->mutex, NULL);
1730 drm_atomic_plane_print_state(p, plane->state);
1731 if (take_locks)
1732 drm_modeset_unlock(&plane->mutex);
1733 }
1734
1735 list_for_each_entry(crtc, &config->crtc_list, head) {
1736 if (take_locks)
1737 drm_modeset_lock(&crtc->mutex, NULL);
1738 drm_atomic_crtc_print_state(p, crtc->state);
1739 if (take_locks)
1740 drm_modeset_unlock(&crtc->mutex);
1741 }
1742
1743 drm_connector_list_iter_begin(dev, &conn_iter);
1744 if (take_locks)
1745 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1746 drm_for_each_connector_iter(connector, &conn_iter)
1747 drm_atomic_connector_print_state(p, connector->state);
1748 if (take_locks)
1749 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1750 drm_connector_list_iter_end(&conn_iter);
1751}
1752
Rob Clark6559c902016-11-05 11:08:10 -04001753/**
1754 * drm_state_dump - dump entire device atomic state
1755 * @dev: the drm device
1756 * @p: where to print the state to
1757 *
1758 * Just for debugging. Drivers might want an option to dump state
1759 * to dmesg in case of error irq's. (Hint, you probably want to
1760 * ratelimit this!)
1761 *
1762 * The caller must drm_modeset_lock_all(), or if this is called
1763 * from error irq handler, it should not be enabled by default.
1764 * (Ie. if you are debugging errors you might not care that this
1765 * is racey. But calling this without all modeset locks held is
1766 * not inherently safe.)
1767 */
1768void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1769{
Daniel Vetterc2d85562017-04-03 10:32:54 +02001770 __drm_state_dump(dev, p, false);
Rob Clark6559c902016-11-05 11:08:10 -04001771}
1772EXPORT_SYMBOL(drm_state_dump);
1773
1774#ifdef CONFIG_DEBUG_FS
1775static int drm_state_info(struct seq_file *m, void *data)
1776{
1777 struct drm_info_node *node = (struct drm_info_node *) m->private;
1778 struct drm_device *dev = node->minor->dev;
1779 struct drm_printer p = drm_seq_file_printer(m);
1780
Daniel Vetterc2d85562017-04-03 10:32:54 +02001781 __drm_state_dump(dev, &p, true);
Rob Clark6559c902016-11-05 11:08:10 -04001782
1783 return 0;
1784}
1785
1786/* any use in debugfs files to dump individual planes/crtc/etc? */
1787static const struct drm_info_list drm_atomic_debugfs_list[] = {
1788 {"state", drm_state_info, 0},
1789};
1790
1791int drm_atomic_debugfs_init(struct drm_minor *minor)
1792{
1793 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1794 ARRAY_SIZE(drm_atomic_debugfs_list),
1795 minor->debugfs_root, minor);
1796}
1797#endif
1798
Rob Clarkd34f20d2014-12-18 16:01:56 -05001799/*
Liviu Dudau21be9152017-11-01 14:04:36 +00001800 * The big monster ioctl
Rob Clarkd34f20d2014-12-18 16:01:56 -05001801 */
1802
1803static struct drm_pending_vblank_event *create_vblank_event(
Keith Packardbd386e52017-07-05 14:34:23 -07001804 struct drm_crtc *crtc, uint64_t user_data)
Rob Clarkd34f20d2014-12-18 16:01:56 -05001805{
1806 struct drm_pending_vblank_event *e = NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001807
1808 e = kzalloc(sizeof *e, GFP_KERNEL);
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001809 if (!e)
1810 return NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001811
1812 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001813 e->event.base.length = sizeof(e->event);
Keith Packardbd386e52017-07-05 14:34:23 -07001814 e->event.vbl.crtc_id = crtc->base.id;
1815 e->event.vbl.user_data = user_data;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001816
Rob Clarkd34f20d2014-12-18 16:01:56 -05001817 return e;
1818}
1819
Daniel Vetter144a7992017-07-25 14:02:04 +02001820int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
1821 struct drm_connector *connector,
1822 int mode)
1823{
1824 struct drm_connector *tmp_connector;
1825 struct drm_connector_state *new_conn_state;
1826 struct drm_crtc *crtc;
1827 struct drm_crtc_state *crtc_state;
1828 int i, ret, old_mode = connector->dpms;
1829 bool active = false;
1830
1831 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1832 state->acquire_ctx);
1833 if (ret)
1834 return ret;
1835
1836 if (mode != DRM_MODE_DPMS_ON)
1837 mode = DRM_MODE_DPMS_OFF;
1838 connector->dpms = mode;
1839
1840 crtc = connector->state->crtc;
1841 if (!crtc)
1842 goto out;
1843 ret = drm_atomic_add_affected_connectors(state, crtc);
1844 if (ret)
1845 goto out;
1846
1847 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1848 if (IS_ERR(crtc_state)) {
1849 ret = PTR_ERR(crtc_state);
1850 goto out;
1851 }
1852
1853 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
1854 if (new_conn_state->crtc != crtc)
1855 continue;
1856 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
1857 active = true;
1858 break;
1859 }
1860 }
1861
1862 crtc_state->active = active;
1863 ret = drm_atomic_commit(state);
1864out:
1865 if (ret != 0)
1866 connector->dpms = old_mode;
1867 return ret;
1868}
1869
1870int drm_atomic_set_property(struct drm_atomic_state *state,
1871 struct drm_mode_object *obj,
1872 struct drm_property *prop,
1873 uint64_t prop_value)
Rob Clarkd34f20d2014-12-18 16:01:56 -05001874{
1875 struct drm_mode_object *ref;
1876 int ret;
1877
1878 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1879 return -EINVAL;
1880
1881 switch (obj->type) {
1882 case DRM_MODE_OBJECT_CONNECTOR: {
1883 struct drm_connector *connector = obj_to_connector(obj);
1884 struct drm_connector_state *connector_state;
1885
1886 connector_state = drm_atomic_get_connector_state(state, connector);
1887 if (IS_ERR(connector_state)) {
1888 ret = PTR_ERR(connector_state);
1889 break;
1890 }
1891
1892 ret = drm_atomic_connector_set_property(connector,
1893 connector_state, prop, prop_value);
1894 break;
1895 }
1896 case DRM_MODE_OBJECT_CRTC: {
1897 struct drm_crtc *crtc = obj_to_crtc(obj);
1898 struct drm_crtc_state *crtc_state;
1899
1900 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1901 if (IS_ERR(crtc_state)) {
1902 ret = PTR_ERR(crtc_state);
1903 break;
1904 }
1905
1906 ret = drm_atomic_crtc_set_property(crtc,
1907 crtc_state, prop, prop_value);
1908 break;
1909 }
1910 case DRM_MODE_OBJECT_PLANE: {
1911 struct drm_plane *plane = obj_to_plane(obj);
1912 struct drm_plane_state *plane_state;
1913
1914 plane_state = drm_atomic_get_plane_state(state, plane);
1915 if (IS_ERR(plane_state)) {
1916 ret = PTR_ERR(plane_state);
1917 break;
1918 }
1919
1920 ret = drm_atomic_plane_set_property(plane,
1921 plane_state, prop, prop_value);
1922 break;
1923 }
1924 default:
1925 ret = -EINVAL;
1926 break;
1927 }
1928
1929 drm_property_change_valid_put(prop, ref);
1930 return ret;
1931}
1932
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001933/**
Maarten Lankhorst9744bf42015-11-24 10:34:34 +01001934 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001935 *
1936 * @dev: drm device to check.
1937 * @plane_mask: plane mask for planes that were updated.
1938 * @ret: return value, can be -EDEADLK for a retry.
1939 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001940 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1941 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1942 * is a common operation for each atomic update, so this call is split off as a
1943 * helper.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001944 */
1945void drm_atomic_clean_old_fb(struct drm_device *dev,
1946 unsigned plane_mask,
1947 int ret)
1948{
1949 struct drm_plane *plane;
1950
1951 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1952 * locks (ie. while it is still safe to deref plane->state). We
1953 * need to do this here because the driver entry points cannot
1954 * distinguish between legacy and atomic ioctls.
1955 */
1956 drm_for_each_plane_mask(plane, dev, plane_mask) {
1957 if (ret == 0) {
1958 struct drm_framebuffer *new_fb = plane->state->fb;
1959 if (new_fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01001960 drm_framebuffer_get(new_fb);
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001961 plane->fb = new_fb;
1962 plane->crtc = plane->state->crtc;
1963
1964 if (plane->old_fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01001965 drm_framebuffer_put(plane->old_fb);
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001966 }
1967 plane->old_fb = NULL;
1968 }
1969}
1970EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1971
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001972/**
1973 * DOC: explicit fencing properties
1974 *
1975 * Explicit fencing allows userspace to control the buffer synchronization
1976 * between devices. A Fence or a group of fences are transfered to/from
1977 * userspace using Sync File fds and there are two DRM properties for that.
1978 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1979 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1980 *
1981 * As a contrast, with implicit fencing the kernel keeps track of any
1982 * ongoing rendering, and automatically ensures that the atomic update waits
1983 * for any pending rendering to complete. For shared buffers represented with
Daniel Vetterd5745282017-01-25 07:26:45 +01001984 * a &struct dma_buf this is tracked in &struct reservation_object.
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001985 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1986 * whereas explicit fencing is what Android wants.
1987 *
1988 * "IN_FENCE_FD”:
1989 * Use this property to pass a fence that DRM should wait on before
1990 * proceeding with the Atomic Commit request and show the framebuffer for
1991 * the plane on the screen. The fence can be either a normal fence or a
1992 * merged one, the sync_file framework will handle both cases and use a
1993 * fence_array if a merged fence is received. Passing -1 here means no
1994 * fences to wait on.
1995 *
1996 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1997 * it will only check if the Sync File is a valid one.
1998 *
1999 * On the driver side the fence is stored on the @fence parameter of
Daniel Vetterea0dd852016-12-29 21:48:26 +01002000 * &struct drm_plane_state. Drivers which also support implicit fencing
Gustavo Padovan9a83a712016-11-22 09:11:28 +09002001 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
2002 * to make sure there's consistent behaviour between drivers in precedence
2003 * of implicit vs. explicit fencing.
2004 *
2005 * "OUT_FENCE_PTR”:
2006 * Use this property to pass a file descriptor pointer to DRM. Once the
2007 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
2008 * the file descriptor number of a Sync File. This Sync File contains the
2009 * CRTC fence that will be signaled when all framebuffers present on the
2010 * Atomic Commit * request for that given CRTC are scanned out on the
2011 * screen.
2012 *
2013 * The Atomic Commit request fails if a invalid pointer is passed. If the
2014 * Atomic Commit request fails for any other reason the out fence fd
2015 * returned will be -1. On a Atomic Commit with the
2016 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2017 *
2018 * Note that out-fences don't have a special interface to drivers and are
Daniel Vetterea0dd852016-12-29 21:48:26 +01002019 * internally represented by a &struct drm_pending_vblank_event in struct
Gustavo Padovan9a83a712016-11-22 09:11:28 +09002020 * &drm_crtc_state, which is also used by the nonblocking atomic commit
2021 * helpers and for the DRM event handling for existing userspace.
2022 */
2023
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002024struct drm_out_fence_state {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02002025 s32 __user *out_fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002026 struct sync_file *sync_file;
2027 int fd;
2028};
2029
2030static int setup_out_fence(struct drm_out_fence_state *fence_state,
2031 struct dma_fence *fence)
2032{
2033 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
2034 if (fence_state->fd < 0)
2035 return fence_state->fd;
2036
2037 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
2038 return -EFAULT;
2039
2040 fence_state->sync_file = sync_file_create(fence);
2041 if (!fence_state->sync_file)
2042 return -ENOMEM;
2043
2044 return 0;
2045}
2046
2047static int prepare_crtc_signaling(struct drm_device *dev,
2048 struct drm_atomic_state *state,
2049 struct drm_mode_atomic *arg,
2050 struct drm_file *file_priv,
2051 struct drm_out_fence_state **fence_state,
2052 unsigned int *num_fences)
2053{
2054 struct drm_crtc *crtc;
2055 struct drm_crtc_state *crtc_state;
Andrey Grodzovsky330c4222017-06-20 13:57:06 -04002056 int i, c = 0, ret;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002057
2058 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
2059 return 0;
2060
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002061 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02002062 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002063
2064 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
2065
2066 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
2067 struct drm_pending_vblank_event *e;
2068
Keith Packardbd386e52017-07-05 14:34:23 -07002069 e = create_vblank_event(crtc, arg->user_data);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002070 if (!e)
2071 return -ENOMEM;
2072
2073 crtc_state->event = e;
2074 }
2075
2076 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2077 struct drm_pending_vblank_event *e = crtc_state->event;
2078
2079 if (!file_priv)
2080 continue;
2081
2082 ret = drm_event_reserve_init(dev, file_priv, &e->base,
2083 &e->event.base);
2084 if (ret) {
2085 kfree(e);
2086 crtc_state->event = NULL;
2087 return ret;
2088 }
2089 }
2090
2091 if (fence_ptr) {
2092 struct dma_fence *fence;
2093 struct drm_out_fence_state *f;
2094
2095 f = krealloc(*fence_state, sizeof(**fence_state) *
2096 (*num_fences + 1), GFP_KERNEL);
2097 if (!f)
2098 return -ENOMEM;
2099
2100 memset(&f[*num_fences], 0, sizeof(*f));
2101
2102 f[*num_fences].out_fence_ptr = fence_ptr;
2103 *fence_state = f;
2104
Gustavo Padovan35f8cc32016-12-06 15:47:17 -02002105 fence = drm_crtc_create_fence(crtc);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002106 if (!fence)
2107 return -ENOMEM;
2108
2109 ret = setup_out_fence(&f[(*num_fences)++], fence);
2110 if (ret) {
2111 dma_fence_put(fence);
2112 return ret;
2113 }
2114
2115 crtc_state->event->base.fence = fence;
2116 }
Andrey Grodzovsky330c4222017-06-20 13:57:06 -04002117
2118 c++;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002119 }
2120
Andrey Grodzovsky330c4222017-06-20 13:57:06 -04002121 /*
2122 * Having this flag means user mode pends on event which will never
2123 * reach due to lack of at least one CRTC for signaling
2124 */
2125 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2126 return -EINVAL;
2127
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002128 return 0;
2129}
2130
2131static void complete_crtc_signaling(struct drm_device *dev,
2132 struct drm_atomic_state *state,
2133 struct drm_out_fence_state *fence_state,
2134 unsigned int num_fences,
2135 bool install_fds)
2136{
2137 struct drm_crtc *crtc;
2138 struct drm_crtc_state *crtc_state;
2139 int i;
2140
2141 if (install_fds) {
2142 for (i = 0; i < num_fences; i++)
2143 fd_install(fence_state[i].fd,
2144 fence_state[i].sync_file->file);
2145
2146 kfree(fence_state);
2147 return;
2148 }
2149
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002150 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002151 struct drm_pending_vblank_event *event = crtc_state->event;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002152 /*
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002153 * Free the allocated event. drm_atomic_helper_setup_commit
2154 * can allocate an event too, so only free it if it's ours
2155 * to prevent a double free in drm_atomic_state_clear.
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002156 */
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002157 if (event && (event->base.fence || event->base.file_priv)) {
2158 drm_event_cancel_free(dev, &event->base);
2159 crtc_state->event = NULL;
2160 }
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002161 }
2162
2163 if (!fence_state)
2164 return;
2165
2166 for (i = 0; i < num_fences; i++) {
2167 if (fence_state[i].sync_file)
2168 fput(fence_state[i].sync_file->file);
2169 if (fence_state[i].fd >= 0)
2170 put_unused_fd(fence_state[i].fd);
2171
2172 /* If this fails log error to the user */
2173 if (fence_state[i].out_fence_ptr &&
2174 put_user(-1, fence_state[i].out_fence_ptr))
2175 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2176 }
2177
2178 kfree(fence_state);
2179}
2180
Rob Clarkd34f20d2014-12-18 16:01:56 -05002181int drm_mode_atomic_ioctl(struct drm_device *dev,
2182 void *data, struct drm_file *file_priv)
2183{
2184 struct drm_mode_atomic *arg = data;
2185 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2186 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2187 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2188 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2189 unsigned int copied_objs, copied_props;
2190 struct drm_atomic_state *state;
2191 struct drm_modeset_acquire_ctx ctx;
2192 struct drm_plane *plane;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002193 struct drm_out_fence_state *fence_state;
Maarten Lankhorst45723722015-11-11 11:29:08 +01002194 unsigned plane_mask;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002195 int ret = 0;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002196 unsigned int i, j, num_fences;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002197
2198 /* disallow for drivers not supporting atomic: */
2199 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2200 return -EINVAL;
2201
2202 /* disallow for userspace that has not enabled atomic cap (even
2203 * though this may be a bit overkill, since legacy userspace
2204 * wouldn't know how to call this ioctl)
2205 */
2206 if (!file_priv->atomic)
2207 return -EINVAL;
2208
2209 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2210 return -EINVAL;
2211
2212 if (arg->reserved)
2213 return -EINVAL;
2214
2215 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2216 !dev->mode_config.async_page_flip)
2217 return -EINVAL;
2218
2219 /* can't test and expect an event at the same time. */
2220 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2221 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2222 return -EINVAL;
2223
Maarten Lankhorstdad56ce2017-09-12 15:37:45 +02002224 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002225
2226 state = drm_atomic_state_alloc(dev);
2227 if (!state)
2228 return -ENOMEM;
2229
2230 state->acquire_ctx = &ctx;
2231 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2232
2233retry:
Maarten Lankhorst45723722015-11-11 11:29:08 +01002234 plane_mask = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002235 copied_objs = 0;
2236 copied_props = 0;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002237 fence_state = NULL;
2238 num_fences = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002239
2240 for (i = 0; i < arg->count_objs; i++) {
2241 uint32_t obj_id, count_props;
2242 struct drm_mode_object *obj;
2243
2244 if (get_user(obj_id, objs_ptr + copied_objs)) {
2245 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002246 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002247 }
2248
Keith Packard418da172017-03-14 23:25:07 -07002249 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
Dave Airlieb164d312016-04-27 11:10:09 +10002250 if (!obj) {
2251 ret = -ENOENT;
2252 goto out;
2253 }
2254
2255 if (!obj->properties) {
Thierry Reding020a2182017-02-28 15:46:38 +01002256 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002257 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002258 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002259 }
2260
Rob Clarkd34f20d2014-12-18 16:01:56 -05002261 if (get_user(count_props, count_props_ptr + copied_objs)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002262 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002263 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002264 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002265 }
2266
2267 copied_objs++;
2268
2269 for (j = 0; j < count_props; j++) {
2270 uint32_t prop_id;
2271 uint64_t prop_value;
2272 struct drm_property *prop;
2273
2274 if (get_user(prop_id, props_ptr + copied_props)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002275 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002276 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002277 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002278 }
2279
Maarten Lankhorstf92f0532016-09-08 12:30:01 +02002280 prop = drm_mode_obj_find_prop_id(obj, prop_id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002281 if (!prop) {
Thierry Reding020a2182017-02-28 15:46:38 +01002282 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002283 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002284 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002285 }
2286
Guenter Roeck42c58142015-01-12 21:12:17 -08002287 if (copy_from_user(&prop_value,
2288 prop_values_ptr + copied_props,
2289 sizeof(prop_value))) {
Thierry Reding020a2182017-02-28 15:46:38 +01002290 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002291 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002292 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002293 }
2294
Daniel Vetter144a7992017-07-25 14:02:04 +02002295 ret = drm_atomic_set_property(state, obj, prop,
2296 prop_value);
Dave Airlieb164d312016-04-27 11:10:09 +10002297 if (ret) {
Thierry Reding020a2182017-02-28 15:46:38 +01002298 drm_mode_object_put(obj);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002299 goto out;
Dave Airlieb164d312016-04-27 11:10:09 +10002300 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05002301
2302 copied_props++;
2303 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002304
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002305 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2306 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002307 plane = obj_to_plane(obj);
2308 plane_mask |= (1 << drm_plane_index(plane));
2309 plane->old_fb = plane->fb;
2310 }
Thierry Reding020a2182017-02-28 15:46:38 +01002311 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002312 }
2313
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002314 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2315 &num_fences);
2316 if (ret)
2317 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002318
2319 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2320 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002321 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002322 ret = drm_atomic_nonblocking_commit(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002323 } else {
Rob Clarkfceffb322016-11-05 11:08:09 -04002324 if (unlikely(drm_debug & DRM_UT_STATE))
2325 drm_atomic_print_state(state);
2326
Rob Clarkd34f20d2014-12-18 16:01:56 -05002327 ret = drm_atomic_commit(state);
2328 }
2329
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002330out:
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01002331 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002332
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002333 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002334
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002335 if (ret == -EDEADLK) {
2336 drm_atomic_state_clear(state);
Maarten Lankhorstdad56ce2017-09-12 15:37:45 +02002337 ret = drm_modeset_backoff(&ctx);
2338 if (!ret)
2339 goto retry;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002340 }
2341
Chris Wilson08536952016-10-14 13:18:18 +01002342 drm_atomic_state_put(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002343
2344 drm_modeset_drop_locks(&ctx);
2345 drm_modeset_acquire_fini(&ctx);
2346
2347 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002348}