blob: fd96c3dad2fbdff318760a93b78699abfb0b4dc2 [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"
36
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010037void __drm_crtc_commit_free(struct kref *kref)
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020038{
39 struct drm_crtc_commit *commit =
40 container_of(kref, struct drm_crtc_commit, ref);
41
42 kfree(commit);
43}
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010044EXPORT_SYMBOL(__drm_crtc_commit_free);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020045
Maarten Lankhorst036ef572015-05-18 10:06:40 +020046/**
47 * drm_atomic_state_default_release -
48 * release memory initialized by drm_atomic_state_init
49 * @state: atomic state
50 *
51 * Free all the memory allocated by drm_atomic_state_init.
52 * This is useful for drivers that subclass the atomic state.
53 */
54void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020055{
56 kfree(state->connectors);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020057 kfree(state->crtcs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020058 kfree(state->planes);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -070059 kfree(state->private_objs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020060}
Maarten Lankhorst036ef572015-05-18 10:06:40 +020061EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020062
63/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020064 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020065 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +020066 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020067 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +020068 * Default implementation for filling in a new atomic state.
69 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +020070 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +020071int
72drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020073{
Chris Wilson08536952016-10-14 13:18:18 +010074 kref_init(&state->ref);
75
Rob Clarkd34f20d2014-12-18 16:01:56 -050076 /* TODO legacy paths should maybe do a better job about
77 * setting this appropriately?
78 */
79 state->allow_modeset = true;
80
Daniel Vettercc4ceb42014-07-25 21:30:38 +020081 state->crtcs = kcalloc(dev->mode_config.num_crtc,
82 sizeof(*state->crtcs), GFP_KERNEL);
83 if (!state->crtcs)
84 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020085 state->planes = kcalloc(dev->mode_config.num_total_plane,
86 sizeof(*state->planes), GFP_KERNEL);
87 if (!state->planes)
88 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020089
90 state->dev = dev;
91
Maarten Lankhorst036ef572015-05-18 10:06:40 +020092 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020093
Maarten Lankhorst036ef572015-05-18 10:06:40 +020094 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020095fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +020096 drm_atomic_state_default_release(state);
97 return -ENOMEM;
98}
99EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200100
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200101/**
102 * drm_atomic_state_alloc - allocate atomic state
103 * @dev: DRM device
104 *
105 * This allocates an empty atomic state to track updates.
106 */
107struct drm_atomic_state *
108drm_atomic_state_alloc(struct drm_device *dev)
109{
110 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200111
112 if (!config->funcs->atomic_state_alloc) {
Dawid Kurekac7c7482017-06-15 19:45:56 +0200113 struct drm_atomic_state *state;
114
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200115 state = kzalloc(sizeof(*state), GFP_KERNEL);
116 if (!state)
117 return NULL;
118 if (drm_atomic_state_init(dev, state) < 0) {
119 kfree(state);
120 return NULL;
121 }
122 return state;
123 }
124
125 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200126}
127EXPORT_SYMBOL(drm_atomic_state_alloc);
128
129/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200130 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200131 * @state: atomic state
132 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200133 * Default implementation for clearing atomic state.
134 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200135 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200136void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200137{
138 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100139 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200140 int i;
141
Daniel Vetter17a38d92015-02-22 12:24:16 +0100142 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200143
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100144 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200145 struct drm_connector *connector = state->connectors[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200146
147 if (!connector)
148 continue;
149
Dave Airlied2307de2016-04-27 11:27:39 +1000150 connector->funcs->atomic_destroy_state(connector,
Daniel Vetter63e83c12016-06-02 00:06:32 +0200151 state->connectors[i].state);
152 state->connectors[i].ptr = NULL;
153 state->connectors[i].state = NULL;
Thierry Redingad093602017-02-28 15:46:39 +0100154 drm_connector_put(connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200155 }
156
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100157 for (i = 0; i < config->num_crtc; i++) {
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200158 struct drm_crtc *crtc = state->crtcs[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200159
160 if (!crtc)
161 continue;
162
163 crtc->funcs->atomic_destroy_state(crtc,
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200164 state->crtcs[i].state);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200165
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200166 state->crtcs[i].ptr = NULL;
167 state->crtcs[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200168 }
169
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100170 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vetterb8b53422016-06-02 00:06:33 +0200171 struct drm_plane *plane = state->planes[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200172
173 if (!plane)
174 continue;
175
176 plane->funcs->atomic_destroy_state(plane,
Daniel Vetterb8b53422016-06-02 00:06:33 +0200177 state->planes[i].state);
178 state->planes[i].ptr = NULL;
179 state->planes[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200180 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700181
182 for (i = 0; i < state->num_private_objs; i++) {
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300183 struct drm_private_obj *obj = state->private_objs[i].ptr;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700184
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300185 obj->funcs->atomic_destroy_state(obj,
186 state->private_objs[i].state);
187 state->private_objs[i].ptr = NULL;
188 state->private_objs[i].state = NULL;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700189 }
190 state->num_private_objs = 0;
191
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +0200192 if (state->fake_commit) {
193 drm_crtc_commit_put(state->fake_commit);
194 state->fake_commit = NULL;
195 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200196}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200197EXPORT_SYMBOL(drm_atomic_state_default_clear);
198
199/**
200 * drm_atomic_state_clear - clear state object
201 * @state: atomic state
202 *
203 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
204 * all locks. So someone else could sneak in and change the current modeset
205 * configuration. Which means that all the state assembled in @state is no
206 * longer an atomic update to the current state, but to some arbitrary earlier
Daniel Vetterd5745282017-01-25 07:26:45 +0100207 * state. Which could break assumptions the driver's
208 * &drm_mode_config_funcs.atomic_check likely relies on.
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200209 *
210 * Hence we must clear all cached state and completely start over, using this
211 * function.
212 */
213void drm_atomic_state_clear(struct drm_atomic_state *state)
214{
215 struct drm_device *dev = state->dev;
216 struct drm_mode_config *config = &dev->mode_config;
217
218 if (config->funcs->atomic_state_clear)
219 config->funcs->atomic_state_clear(state);
220 else
221 drm_atomic_state_default_clear(state);
222}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200223EXPORT_SYMBOL(drm_atomic_state_clear);
224
225/**
Chris Wilson08536952016-10-14 13:18:18 +0100226 * __drm_atomic_state_free - free all memory for an atomic state
227 * @ref: This atomic state to deallocate
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200228 *
229 * This frees all memory associated with an atomic state, including all the
230 * per-object state for planes, crtcs and connectors.
231 */
Chris Wilson08536952016-10-14 13:18:18 +0100232void __drm_atomic_state_free(struct kref *ref)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200233{
Chris Wilson08536952016-10-14 13:18:18 +0100234 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
235 struct drm_mode_config *config = &state->dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200236
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200237 drm_atomic_state_clear(state);
238
Daniel Vetter17a38d92015-02-22 12:24:16 +0100239 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200240
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200241 if (config->funcs->atomic_state_free) {
242 config->funcs->atomic_state_free(state);
243 } else {
244 drm_atomic_state_default_release(state);
245 kfree(state);
246 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200247}
Chris Wilson08536952016-10-14 13:18:18 +0100248EXPORT_SYMBOL(__drm_atomic_state_free);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200249
250/**
251 * drm_atomic_get_crtc_state - get crtc state
252 * @state: global atomic state object
253 * @crtc: crtc to get state object for
254 *
255 * This function returns the crtc state for the given crtc, allocating it if
256 * needed. It will also grab the relevant crtc lock to make sure that the state
257 * is consistent.
258 *
259 * Returns:
260 *
261 * Either the allocated state or the error code encoded into the pointer. When
262 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
263 * entire atomic sequence must be restarted. All other errors are fatal.
264 */
265struct drm_crtc_state *
266drm_atomic_get_crtc_state(struct drm_atomic_state *state,
267 struct drm_crtc *crtc)
268{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200269 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200270 struct drm_crtc_state *crtc_state;
271
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200272 WARN_ON(!state->acquire_ctx);
273
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200274 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
275 if (crtc_state)
276 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200277
278 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
279 if (ret)
280 return ERR_PTR(ret);
281
282 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
283 if (!crtc_state)
284 return ERR_PTR(-ENOMEM);
285
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200286 state->crtcs[index].state = crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100287 state->crtcs[index].old_state = crtc->state;
288 state->crtcs[index].new_state = crtc_state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200289 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200290 crtc_state->state = state;
291
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200292 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
293 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200294
295 return crtc_state;
296}
297EXPORT_SYMBOL(drm_atomic_get_crtc_state);
298
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900299static void set_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200300 struct drm_crtc *crtc, s32 __user *fence_ptr)
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900301{
302 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
303}
304
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200305static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900306 struct drm_crtc *crtc)
307{
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200308 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900309
310 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
311 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
312
313 return fence_ptr;
314}
315
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200316/**
Daniel Stone819364d2015-05-26 14:36:48 +0100317 * drm_atomic_set_mode_for_crtc - set mode for CRTC
318 * @state: the CRTC whose incoming state to update
319 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
320 *
Dhinakaran Pandiyancbef9092017-01-30 22:18:38 -0800321 * Set a mode (originating from the kernel) on the desired CRTC state and update
322 * the enable property.
Daniel Stone819364d2015-05-26 14:36:48 +0100323 *
324 * RETURNS:
325 * Zero on success, error code on failure. Cannot return -EDEADLK.
326 */
327int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
Ville Syrjälä91110a42017-05-18 22:38:36 +0300328 const struct drm_display_mode *mode)
Daniel Stone819364d2015-05-26 14:36:48 +0100329{
Daniel Stone99cf4a22015-05-25 19:11:51 +0100330 struct drm_mode_modeinfo umode;
331
Daniel Stone819364d2015-05-26 14:36:48 +0100332 /* Early return for no change. */
333 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
334 return 0;
335
Thierry Reding6472e502017-02-28 15:46:42 +0100336 drm_property_blob_put(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100337 state->mode_blob = NULL;
338
Daniel Stone819364d2015-05-26 14:36:48 +0100339 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100340 drm_mode_convert_to_umode(&umode, mode);
341 state->mode_blob =
342 drm_property_create_blob(state->crtc->dev,
343 sizeof(umode),
344 &umode);
345 if (IS_ERR(state->mode_blob))
346 return PTR_ERR(state->mode_blob);
347
Daniel Stone819364d2015-05-26 14:36:48 +0100348 drm_mode_copy(&state->mode, mode);
349 state->enable = true;
350 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
351 mode->name, state);
352 } else {
353 memset(&state->mode, 0, sizeof(state->mode));
354 state->enable = false;
355 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
356 state);
357 }
358
359 return 0;
360}
361EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
362
Daniel Stone819364d2015-05-26 14:36:48 +0100363/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100364 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
365 * @state: the CRTC whose incoming state to update
366 * @blob: pointer to blob property to use for mode
367 *
368 * Set a mode (originating from a blob property) on the desired CRTC state.
369 * This function will take a reference on the blob property for the CRTC state,
370 * and release the reference held on the state's existing mode property, if any
371 * was set.
372 *
373 * RETURNS:
374 * Zero on success, error code on failure. Cannot return -EDEADLK.
375 */
376int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
377 struct drm_property_blob *blob)
378{
379 if (blob == state->mode_blob)
380 return 0;
381
Thierry Reding6472e502017-02-28 15:46:42 +0100382 drm_property_blob_put(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100383 state->mode_blob = NULL;
384
Tomi Valkeinen67098872016-05-31 15:03:17 +0300385 memset(&state->mode, 0, sizeof(state->mode));
386
Daniel Stone955f3c32015-05-25 19:11:52 +0100387 if (blob) {
388 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
389 drm_mode_convert_umode(&state->mode,
390 (const struct drm_mode_modeinfo *)
391 blob->data))
392 return -EINVAL;
393
Thierry Reding6472e502017-02-28 15:46:42 +0100394 state->mode_blob = drm_property_blob_get(blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100395 state->enable = true;
396 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
397 state->mode.name, state);
398 } else {
Daniel Stone955f3c32015-05-25 19:11:52 +0100399 state->enable = false;
400 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
401 state);
402 }
403
404 return 0;
405}
406EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
407
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000408static int
Jyri Sarhadafee602017-04-21 12:51:13 +0300409drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000410 struct drm_property_blob **blob,
411 uint64_t blob_id,
412 ssize_t expected_size,
413 bool *replaced)
414{
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000415 struct drm_property_blob *new_blob = NULL;
416
417 if (blob_id != 0) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300418 new_blob = drm_property_lookup_blob(dev, blob_id);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000419 if (new_blob == NULL)
420 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100421
422 if (expected_size > 0 && expected_size != new_blob->length) {
Thierry Reding6472e502017-02-28 15:46:42 +0100423 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000424 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100425 }
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000426 }
427
Peter Rosin5f057ff2017-07-13 18:25:25 +0200428 *replaced |= drm_property_replace_blob(blob, new_blob);
Thierry Reding6472e502017-02-28 15:46:42 +0100429 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000430
431 return 0;
432}
433
434/**
Rob Clark40ecc692014-12-18 16:01:46 -0500435 * drm_atomic_crtc_set_property - set property on CRTC
436 * @crtc: the drm CRTC to set a property on
437 * @state: the state object to update with the new property value
438 * @property: the property to set
439 * @val: the new property value
440 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100441 * This function handles generic/core properties and calls out to driver's
442 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
443 * consistent behavior you must call this function rather than the driver hook
444 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500445 *
446 * RETURNS:
447 * Zero on success, error code on failure
448 */
449int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
450 struct drm_crtc_state *state, struct drm_property *property,
451 uint64_t val)
452{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100453 struct drm_device *dev = crtc->dev;
454 struct drm_mode_config *config = &dev->mode_config;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000455 bool replaced = false;
Daniel Stone955f3c32015-05-25 19:11:52 +0100456 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100457
Daniel Stone27798362015-03-19 04:33:26 +0000458 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100459 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100460 else if (property == config->prop_mode_id) {
461 struct drm_property_blob *mode =
462 drm_property_lookup_blob(dev, val);
463 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Thierry Reding6472e502017-02-28 15:46:42 +0100464 drm_property_blob_put(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100465 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000466 } else if (property == config->degamma_lut_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300467 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000468 &state->degamma_lut,
469 val,
470 -1,
471 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200472 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000473 return ret;
474 } else if (property == config->ctm_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300475 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000476 &state->ctm,
477 val,
478 sizeof(struct drm_color_ctm),
479 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200480 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000481 return ret;
482 } else if (property == config->gamma_lut_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300483 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000484 &state->gamma_lut,
485 val,
486 -1,
487 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200488 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000489 return ret;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900490 } else if (property == config->prop_out_fence_ptr) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200491 s32 __user *fence_ptr = u64_to_user_ptr(val);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900492
493 if (!fence_ptr)
494 return 0;
495
496 if (put_user(-1, fence_ptr))
497 return -EFAULT;
498
499 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000500 } else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500501 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000502 else
503 return -EINVAL;
504
505 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500506}
507EXPORT_SYMBOL(drm_atomic_crtc_set_property);
508
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100509/**
510 * drm_atomic_crtc_get_property - get property value from CRTC state
511 * @crtc: the drm CRTC to set a property on
512 * @state: the state object to get the property value from
513 * @property: the property to set
514 * @val: return location for the property value
515 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100516 * This function handles generic/core properties and calls out to driver's
517 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
518 * consistent behavior you must call this function rather than the driver hook
519 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100520 *
521 * RETURNS:
522 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500523 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700524static int
525drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500526 const struct drm_crtc_state *state,
527 struct drm_property *property, uint64_t *val)
528{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000529 struct drm_device *dev = crtc->dev;
530 struct drm_mode_config *config = &dev->mode_config;
531
532 if (property == config->prop_active)
533 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100534 else if (property == config->prop_mode_id)
535 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000536 else if (property == config->degamma_lut_property)
537 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
538 else if (property == config->ctm_property)
539 *val = (state->ctm) ? state->ctm->base.id : 0;
540 else if (property == config->gamma_lut_property)
541 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900542 else if (property == config->prop_out_fence_ptr)
543 *val = 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000544 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500545 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000546 else
547 return -EINVAL;
548
549 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500550}
Rob Clarkac9c9252014-12-18 16:01:47 -0500551
552/**
Rob Clark5e743732014-12-18 16:01:51 -0500553 * drm_atomic_crtc_check - check crtc state
554 * @crtc: crtc to check
555 * @state: crtc state to check
556 *
557 * Provides core sanity checks for crtc state.
558 *
559 * RETURNS:
560 * Zero on success, error code on failure
561 */
562static int drm_atomic_crtc_check(struct drm_crtc *crtc,
563 struct drm_crtc_state *state)
564{
565 /* NOTE: we explicitly don't enforce constraints such as primary
566 * layer covering entire screen, since that is something we want
567 * to allow (on hw that supports it). For hw that does not, it
568 * should be checked in driver's crtc->atomic_check() vfunc.
569 *
570 * TODO: Add generic modeset state checks once we support those.
571 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100572
573 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200574 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
575 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100576 return -EINVAL;
577 }
578
Daniel Stone99cf4a22015-05-25 19:11:51 +0100579 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
580 * as this is a kernel-internal detail that userspace should never
581 * be able to trigger. */
582 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
583 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200584 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
585 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100586 return -EINVAL;
587 }
588
589 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
590 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200591 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
592 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100593 return -EINVAL;
594 }
595
Daniel Vetter4cba6852015-12-08 09:49:20 +0100596 /*
597 * Reject event generation for when a CRTC is off and stays off.
598 * It wouldn't be hard to implement this, but userspace has a track
599 * record of happily burning through 100% cpu (or worse, crash) when the
600 * display pipe is suspended. To avoid all that fun just reject updates
601 * that ask for events since likely that indicates a bug in the
602 * compositor's drawing loop. This is consistent with the vblank IOCTL
603 * and legacy page_flip IOCTL which also reject service on a disabled
604 * pipe.
605 */
606 if (state->event && !state->active && !crtc->state->active) {
Russell King6ac7c542017-02-13 12:27:03 +0000607 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
608 crtc->base.id, crtc->name);
Daniel Vetter4cba6852015-12-08 09:49:20 +0100609 return -EINVAL;
610 }
611
Rob Clark5e743732014-12-18 16:01:51 -0500612 return 0;
613}
614
Rob Clarkfceffb322016-11-05 11:08:09 -0400615static void drm_atomic_crtc_print_state(struct drm_printer *p,
616 const struct drm_crtc_state *state)
617{
618 struct drm_crtc *crtc = state->crtc;
619
620 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
621 drm_printf(p, "\tenable=%d\n", state->enable);
622 drm_printf(p, "\tactive=%d\n", state->active);
623 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
624 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
625 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
626 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
627 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
628 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
629 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
630 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
631 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
632
633 if (crtc->funcs->atomic_print_state)
634 crtc->funcs->atomic_print_state(p, state);
635}
636
Rob Clark5e743732014-12-18 16:01:51 -0500637/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200638 * drm_atomic_get_plane_state - get plane state
639 * @state: global atomic state object
640 * @plane: plane to get state object for
641 *
642 * This function returns the plane state for the given plane, allocating it if
643 * needed. It will also grab the relevant plane lock to make sure that the state
644 * is consistent.
645 *
646 * Returns:
647 *
648 * Either the allocated state or the error code encoded into the pointer. When
649 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
650 * entire atomic sequence must be restarted. All other errors are fatal.
651 */
652struct drm_plane_state *
653drm_atomic_get_plane_state(struct drm_atomic_state *state,
654 struct drm_plane *plane)
655{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200656 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200657 struct drm_plane_state *plane_state;
658
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200659 WARN_ON(!state->acquire_ctx);
660
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200661 plane_state = drm_atomic_get_existing_plane_state(state, plane);
662 if (plane_state)
663 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200664
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100665 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200666 if (ret)
667 return ERR_PTR(ret);
668
669 plane_state = plane->funcs->atomic_duplicate_state(plane);
670 if (!plane_state)
671 return ERR_PTR(-ENOMEM);
672
Daniel Vetterb8b53422016-06-02 00:06:33 +0200673 state->planes[index].state = plane_state;
674 state->planes[index].ptr = plane;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100675 state->planes[index].old_state = plane->state;
676 state->planes[index].new_state = plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200677 plane_state->state = state;
678
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200679 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
680 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200681
682 if (plane_state->crtc) {
683 struct drm_crtc_state *crtc_state;
684
685 crtc_state = drm_atomic_get_crtc_state(state,
686 plane_state->crtc);
687 if (IS_ERR(crtc_state))
688 return ERR_CAST(crtc_state);
689 }
690
691 return plane_state;
692}
693EXPORT_SYMBOL(drm_atomic_get_plane_state);
694
695/**
Rob Clark40ecc692014-12-18 16:01:46 -0500696 * drm_atomic_plane_set_property - set property on plane
697 * @plane: the drm plane to set a property on
698 * @state: the state object to update with the new property value
699 * @property: the property to set
700 * @val: the new property value
701 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100702 * This function handles generic/core properties and calls out to driver's
703 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
704 * consistent behavior you must call this function rather than the driver hook
705 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500706 *
707 * RETURNS:
708 * Zero on success, error code on failure
709 */
Daniel Vettere90271b2017-07-25 10:01:19 +0200710static int drm_atomic_plane_set_property(struct drm_plane *plane,
Rob Clark40ecc692014-12-18 16:01:46 -0500711 struct drm_plane_state *state, struct drm_property *property,
712 uint64_t val)
713{
Rob Clark6b4959f2014-12-18 16:01:53 -0500714 struct drm_device *dev = plane->dev;
715 struct drm_mode_config *config = &dev->mode_config;
716
717 if (property == config->prop_fb_id) {
718 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
719 drm_atomic_set_fb_for_plane(state, fb);
720 if (fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +0100721 drm_framebuffer_put(fb);
Gustavo Padovan96260142016-11-15 22:06:39 +0900722 } else if (property == config->prop_in_fence_fd) {
723 if (state->fence)
724 return -EINVAL;
725
726 if (U642I64(val) == -1)
727 return 0;
728
729 state->fence = sync_file_get_fence(val);
730 if (!state->fence)
731 return -EINVAL;
732
Rob Clark6b4959f2014-12-18 16:01:53 -0500733 } else if (property == config->prop_crtc_id) {
734 struct drm_crtc *crtc = drm_crtc_find(dev, val);
735 return drm_atomic_set_crtc_for_plane(state, crtc);
736 } else if (property == config->prop_crtc_x) {
737 state->crtc_x = U642I64(val);
738 } else if (property == config->prop_crtc_y) {
739 state->crtc_y = U642I64(val);
740 } else if (property == config->prop_crtc_w) {
741 state->crtc_w = val;
742 } else if (property == config->prop_crtc_h) {
743 state->crtc_h = val;
744 } else if (property == config->prop_src_x) {
745 state->src_x = val;
746 } else if (property == config->prop_src_y) {
747 state->src_y = val;
748 } else if (property == config->prop_src_w) {
749 state->src_w = val;
750 } else if (property == config->prop_src_h) {
751 state->src_h = val;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300752 } else if (property == plane->rotation_property) {
Robert Fossc2c446a2017-05-19 16:50:17 -0400753 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK))
Ville Syrjälä6e0c7c32016-09-26 19:30:47 +0300754 return -EINVAL;
Matt Roper1da30622015-01-21 16:35:40 -0800755 state->rotation = val;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200756 } else if (property == plane->zpos_property) {
757 state->zpos = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500758 } else if (plane->funcs->atomic_set_property) {
759 return plane->funcs->atomic_set_property(plane, state,
760 property, val);
761 } else {
762 return -EINVAL;
763 }
764
765 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500766}
Rob Clark40ecc692014-12-18 16:01:46 -0500767
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100768/**
769 * drm_atomic_plane_get_property - get property value from plane state
770 * @plane: the drm plane to set a property on
771 * @state: the state object to get the property value from
772 * @property: the property to set
773 * @val: return location for the property value
774 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100775 * This function handles generic/core properties and calls out to driver's
776 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
777 * consistent behavior you must call this function rather than the driver hook
778 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100779 *
780 * RETURNS:
781 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500782 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100783static int
784drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500785 const struct drm_plane_state *state,
786 struct drm_property *property, uint64_t *val)
787{
Rob Clark6b4959f2014-12-18 16:01:53 -0500788 struct drm_device *dev = plane->dev;
789 struct drm_mode_config *config = &dev->mode_config;
790
791 if (property == config->prop_fb_id) {
792 *val = (state->fb) ? state->fb->base.id : 0;
Gustavo Padovan96260142016-11-15 22:06:39 +0900793 } else if (property == config->prop_in_fence_fd) {
794 *val = -1;
Rob Clark6b4959f2014-12-18 16:01:53 -0500795 } else if (property == config->prop_crtc_id) {
796 *val = (state->crtc) ? state->crtc->base.id : 0;
797 } else if (property == config->prop_crtc_x) {
798 *val = I642U64(state->crtc_x);
799 } else if (property == config->prop_crtc_y) {
800 *val = I642U64(state->crtc_y);
801 } else if (property == config->prop_crtc_w) {
802 *val = state->crtc_w;
803 } else if (property == config->prop_crtc_h) {
804 *val = state->crtc_h;
805 } else if (property == config->prop_src_x) {
806 *val = state->src_x;
807 } else if (property == config->prop_src_y) {
808 *val = state->src_y;
809 } else if (property == config->prop_src_w) {
810 *val = state->src_w;
811 } else if (property == config->prop_src_h) {
812 *val = state->src_h;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300813 } else if (property == plane->rotation_property) {
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000814 *val = state->rotation;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200815 } else if (property == plane->zpos_property) {
816 *val = state->zpos;
Rob Clark6b4959f2014-12-18 16:01:53 -0500817 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500818 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500819 } else {
820 return -EINVAL;
821 }
822
823 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500824}
Rob Clarkac9c9252014-12-18 16:01:47 -0500825
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200826static bool
827plane_switching_crtc(struct drm_atomic_state *state,
828 struct drm_plane *plane,
829 struct drm_plane_state *plane_state)
830{
831 if (!plane->state->crtc || !plane_state->crtc)
832 return false;
833
834 if (plane->state->crtc == plane_state->crtc)
835 return false;
836
837 /* This could be refined, but currently there's no helper or driver code
838 * to implement direct switching of active planes nor userspace to take
839 * advantage of more direct plane switching without the intermediate
840 * full OFF state.
841 */
842 return true;
843}
844
Rob Clarkac9c9252014-12-18 16:01:47 -0500845/**
Rob Clark5e743732014-12-18 16:01:51 -0500846 * drm_atomic_plane_check - check plane state
847 * @plane: plane to check
848 * @state: plane state to check
849 *
850 * Provides core sanity checks for plane state.
851 *
852 * RETURNS:
853 * Zero on success, error code on failure
854 */
855static int drm_atomic_plane_check(struct drm_plane *plane,
856 struct drm_plane_state *state)
857{
858 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200859 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500860
861 /* either *both* CRTC and FB must be set, or neither */
862 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100863 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500864 return -EINVAL;
865 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100866 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500867 return -EINVAL;
868 }
869
870 /* if disabled, we don't care about the rest of the state: */
871 if (!state->crtc)
872 return 0;
873
874 /* Check whether this plane is usable on this CRTC */
875 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100876 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500877 return -EINVAL;
878 }
879
880 /* Check whether this plane supports the fb pixel format. */
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200881 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
Laurent Pinchartead86102015-03-05 02:25:43 +0200882 if (ret) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000883 struct drm_format_name_buf format_name;
884 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200885 drm_get_format_name(state->fb->format->format,
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000886 &format_name));
Laurent Pinchartead86102015-03-05 02:25:43 +0200887 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500888 }
889
890 /* Give drivers some help against integer overflows */
891 if (state->crtc_w > INT_MAX ||
892 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
893 state->crtc_h > INT_MAX ||
894 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100895 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
896 state->crtc_w, state->crtc_h,
897 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500898 return -ERANGE;
899 }
900
901 fb_width = state->fb->width << 16;
902 fb_height = state->fb->height << 16;
903
904 /* Make sure source coordinates are inside the fb. */
905 if (state->src_w > fb_width ||
906 state->src_x > fb_width - state->src_w ||
907 state->src_h > fb_height ||
908 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100909 DRM_DEBUG_ATOMIC("Invalid source coordinates "
Ville Syrjälä0338f0d2017-11-01 20:35:33 +0200910 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100911 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
912 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
913 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
Ville Syrjälä0338f0d2017-11-01 20:35:33 +0200914 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10,
915 state->fb->width, state->fb->height);
Rob Clark5e743732014-12-18 16:01:51 -0500916 return -ENOSPC;
917 }
918
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200919 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200920 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
921 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200922 return -EINVAL;
923 }
924
Rob Clark5e743732014-12-18 16:01:51 -0500925 return 0;
926}
927
Rob Clarkfceffb322016-11-05 11:08:09 -0400928static void drm_atomic_plane_print_state(struct drm_printer *p,
929 const struct drm_plane_state *state)
930{
931 struct drm_plane *plane = state->plane;
932 struct drm_rect src = drm_plane_state_src(state);
933 struct drm_rect dest = drm_plane_state_dest(state);
934
935 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
936 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
937 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
938 if (state->fb) {
939 struct drm_framebuffer *fb = state->fb;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200940 int i, n = fb->format->num_planes;
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000941 struct drm_format_name_buf format_name;
Rob Clarkfceffb322016-11-05 11:08:09 -0400942
943 drm_printf(p, "\t\tformat=%s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200944 drm_get_format_name(fb->format->format, &format_name));
Ville Syrjäläbae781b2016-11-16 13:33:16 +0200945 drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
Rob Clarkfceffb322016-11-05 11:08:09 -0400946 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
947 drm_printf(p, "\t\tlayers:\n");
948 for (i = 0; i < n; i++) {
949 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
950 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
Rob Clarkfceffb322016-11-05 11:08:09 -0400951 }
952 }
953 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
954 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
955 drm_printf(p, "\trotation=%x\n", state->rotation);
956
957 if (plane->funcs->atomic_print_state)
958 plane->funcs->atomic_print_state(p, state);
959}
960
Rob Clark5e743732014-12-18 16:01:51 -0500961/**
Ville Syrjäläa4370c72017-07-12 18:51:02 +0300962 * drm_atomic_private_obj_init - initialize private object
963 * @obj: private object
964 * @state: initial private object state
965 * @funcs: pointer to the struct of function pointers that identify the object
966 * type
967 *
968 * Initialize the private object, which can be embedded into any
969 * driver private object that needs its own atomic state.
970 */
971void
972drm_atomic_private_obj_init(struct drm_private_obj *obj,
973 struct drm_private_state *state,
974 const struct drm_private_state_funcs *funcs)
975{
976 memset(obj, 0, sizeof(*obj));
977
978 obj->state = state;
979 obj->funcs = funcs;
980}
981EXPORT_SYMBOL(drm_atomic_private_obj_init);
982
983/**
984 * drm_atomic_private_obj_fini - finalize private object
985 * @obj: private object
986 *
987 * Finalize the private object.
988 */
989void
990drm_atomic_private_obj_fini(struct drm_private_obj *obj)
991{
992 obj->funcs->atomic_destroy_state(obj, obj->state);
993}
994EXPORT_SYMBOL(drm_atomic_private_obj_fini);
995
996/**
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700997 * drm_atomic_get_private_obj_state - get private object state
998 * @state: global atomic state
999 * @obj: private object to get the state for
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001000 *
1001 * This function returns the private object state for the given private object,
1002 * allocating the state if needed. It does not grab any locks as the caller is
1003 * expected to care of any required locking.
1004 *
1005 * RETURNS:
1006 *
1007 * Either the allocated state or the error code encoded into a pointer.
1008 */
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001009struct drm_private_state *
1010drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
1011 struct drm_private_obj *obj)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001012{
1013 int index, num_objs, i;
1014 size_t size;
1015 struct __drm_private_objs_state *arr;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001016 struct drm_private_state *obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001017
1018 for (i = 0; i < state->num_private_objs; i++)
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001019 if (obj == state->private_objs[i].ptr)
1020 return state->private_objs[i].state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001021
1022 num_objs = state->num_private_objs + 1;
1023 size = sizeof(*state->private_objs) * num_objs;
1024 arr = krealloc(state->private_objs, size, GFP_KERNEL);
1025 if (!arr)
1026 return ERR_PTR(-ENOMEM);
1027
1028 state->private_objs = arr;
1029 index = state->num_private_objs;
1030 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1031
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001032 obj_state = obj->funcs->atomic_duplicate_state(obj);
1033 if (!obj_state)
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001034 return ERR_PTR(-ENOMEM);
1035
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001036 state->private_objs[index].state = obj_state;
1037 state->private_objs[index].old_state = obj->state;
1038 state->private_objs[index].new_state = obj_state;
1039 state->private_objs[index].ptr = obj;
1040
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001041 state->num_private_objs = num_objs;
1042
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001043 DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
1044 obj, obj_state, state);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001045
Ville Syrjäläa4370c72017-07-12 18:51:02 +03001046 return obj_state;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07001047}
1048EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1049
1050/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001051 * drm_atomic_get_connector_state - get connector state
1052 * @state: global atomic state object
1053 * @connector: connector to get state object for
1054 *
1055 * This function returns the connector state for the given connector,
1056 * allocating it if needed. It will also grab the relevant connector lock to
1057 * make sure that the state is consistent.
1058 *
1059 * Returns:
1060 *
1061 * Either the allocated state or the error code encoded into the pointer. When
1062 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1063 * entire atomic sequence must be restarted. All other errors are fatal.
1064 */
1065struct drm_connector_state *
1066drm_atomic_get_connector_state(struct drm_atomic_state *state,
1067 struct drm_connector *connector)
1068{
1069 int ret, index;
1070 struct drm_mode_config *config = &connector->dev->mode_config;
1071 struct drm_connector_state *connector_state;
1072
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +02001073 WARN_ON(!state->acquire_ctx);
1074
Daniel Vetterc7eb76f2014-11-19 18:38:06 +01001075 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1076 if (ret)
1077 return ERR_PTR(ret);
1078
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001079 index = drm_connector_index(connector);
1080
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001081 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +02001082 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001083 int alloc = max(index + 1, config->num_connector);
1084
1085 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1086 if (!c)
1087 return ERR_PTR(-ENOMEM);
1088
1089 state->connectors = c;
1090 memset(&state->connectors[state->num_connector], 0,
1091 sizeof(*state->connectors) * (alloc - state->num_connector));
1092
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001093 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001094 }
1095
Daniel Vetter63e83c12016-06-02 00:06:32 +02001096 if (state->connectors[index].state)
1097 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001098
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001099 connector_state = connector->funcs->atomic_duplicate_state(connector);
1100 if (!connector_state)
1101 return ERR_PTR(-ENOMEM);
1102
Thierry Redingad093602017-02-28 15:46:39 +01001103 drm_connector_get(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +02001104 state->connectors[index].state = connector_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01001105 state->connectors[index].old_state = connector->state;
1106 state->connectors[index].new_state = connector_state;
Daniel Vetter63e83c12016-06-02 00:06:32 +02001107 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001108 connector_state->state = state;
1109
Russell King6ac7c542017-02-13 12:27:03 +00001110 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1111 connector->base.id, connector->name,
1112 connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001113
1114 if (connector_state->crtc) {
1115 struct drm_crtc_state *crtc_state;
1116
1117 crtc_state = drm_atomic_get_crtc_state(state,
1118 connector_state->crtc);
1119 if (IS_ERR(crtc_state))
1120 return ERR_CAST(crtc_state);
1121 }
1122
1123 return connector_state;
1124}
1125EXPORT_SYMBOL(drm_atomic_get_connector_state);
1126
1127/**
Rob Clark40ecc692014-12-18 16:01:46 -05001128 * drm_atomic_connector_set_property - set property on connector.
1129 * @connector: the drm connector to set a property on
1130 * @state: the state object to update with the new property value
1131 * @property: the property to set
1132 * @val: the new property value
1133 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001134 * This function handles generic/core properties and calls out to driver's
1135 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1136 * consistent behavior you must call this function rather than the driver hook
1137 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -05001138 *
1139 * RETURNS:
1140 * Zero on success, error code on failure
1141 */
Daniel Vetter482b0e32017-07-25 10:01:20 +02001142static int drm_atomic_connector_set_property(struct drm_connector *connector,
Rob Clark40ecc692014-12-18 16:01:46 -05001143 struct drm_connector_state *state, struct drm_property *property,
1144 uint64_t val)
1145{
1146 struct drm_device *dev = connector->dev;
1147 struct drm_mode_config *config = &dev->mode_config;
1148
Rob Clarkae16c592014-12-18 16:01:54 -05001149 if (property == config->prop_crtc_id) {
1150 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1151 return drm_atomic_set_crtc_for_connector(state, crtc);
1152 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -05001153 /* setting DPMS property requires special handling, which
1154 * is done in legacy setprop path for us. Disallow (for
1155 * now?) atomic writes to DPMS property:
1156 */
1157 return -EINVAL;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001158 } else if (property == config->tv_select_subconnector_property) {
1159 state->tv.subconnector = val;
1160 } else if (property == config->tv_left_margin_property) {
1161 state->tv.margins.left = val;
1162 } else if (property == config->tv_right_margin_property) {
1163 state->tv.margins.right = val;
1164 } else if (property == config->tv_top_margin_property) {
1165 state->tv.margins.top = val;
1166 } else if (property == config->tv_bottom_margin_property) {
1167 state->tv.margins.bottom = val;
1168 } else if (property == config->tv_mode_property) {
1169 state->tv.mode = val;
1170 } else if (property == config->tv_brightness_property) {
1171 state->tv.brightness = val;
1172 } else if (property == config->tv_contrast_property) {
1173 state->tv.contrast = val;
1174 } else if (property == config->tv_flicker_reduction_property) {
1175 state->tv.flicker_reduction = val;
1176 } else if (property == config->tv_overscan_property) {
1177 state->tv.overscan = val;
1178 } else if (property == config->tv_saturation_property) {
1179 state->tv.saturation = val;
1180 } else if (property == config->tv_hue_property) {
1181 state->tv.hue = val;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001182 } else if (property == config->link_status_property) {
1183 /* Never downgrade from GOOD to BAD on userspace's request here,
1184 * only hw issues can do that.
1185 *
1186 * For an atomic property the userspace doesn't need to be able
1187 * to understand all the properties, but needs to be able to
1188 * restore the state it wants on VT switch. So if the userspace
1189 * tries to change the link_status from GOOD to BAD, driver
1190 * silently rejects it and returns a 0. This prevents userspace
1191 * from accidently breaking the display when it restores the
1192 * state.
1193 */
1194 if (state->link_status != DRM_LINK_STATUS_GOOD)
1195 state->link_status = val;
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02001196 } else if (property == config->aspect_ratio_property) {
1197 state->picture_aspect_ratio = val;
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001198 } else if (property == connector->scaling_mode_property) {
1199 state->scaling_mode = val;
Rob Clark40ecc692014-12-18 16:01:46 -05001200 } else if (connector->funcs->atomic_set_property) {
1201 return connector->funcs->atomic_set_property(connector,
1202 state, property, val);
1203 } else {
1204 return -EINVAL;
1205 }
Boris Brezillon299a16b2016-12-02 14:48:09 +01001206
1207 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -05001208}
Rob Clark40ecc692014-12-18 16:01:46 -05001209
Rob Clarkfceffb322016-11-05 11:08:09 -04001210static void drm_atomic_connector_print_state(struct drm_printer *p,
1211 const struct drm_connector_state *state)
1212{
1213 struct drm_connector *connector = state->connector;
1214
1215 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1216 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1217
1218 if (connector->funcs->atomic_print_state)
1219 connector->funcs->atomic_print_state(p, state);
1220}
1221
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001222/**
1223 * drm_atomic_connector_get_property - get property value from connector state
1224 * @connector: the drm connector to set a property on
1225 * @state: the state object to get the property value from
1226 * @property: the property to set
1227 * @val: return location for the property value
1228 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001229 * This function handles generic/core properties and calls out to driver's
1230 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1231 * consistent behavior you must call this function rather than the driver hook
1232 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001233 *
1234 * RETURNS:
1235 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -05001236 */
Daniel Vettera97df1c2014-12-18 22:49:02 +01001237static int
1238drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -05001239 const struct drm_connector_state *state,
1240 struct drm_property *property, uint64_t *val)
1241{
1242 struct drm_device *dev = connector->dev;
1243 struct drm_mode_config *config = &dev->mode_config;
1244
Rob Clarkae16c592014-12-18 16:01:54 -05001245 if (property == config->prop_crtc_id) {
1246 *val = (state->crtc) ? state->crtc->base.id : 0;
1247 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -05001248 *val = connector->dpms;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001249 } else if (property == config->tv_select_subconnector_property) {
1250 *val = state->tv.subconnector;
1251 } else if (property == config->tv_left_margin_property) {
1252 *val = state->tv.margins.left;
1253 } else if (property == config->tv_right_margin_property) {
1254 *val = state->tv.margins.right;
1255 } else if (property == config->tv_top_margin_property) {
1256 *val = state->tv.margins.top;
1257 } else if (property == config->tv_bottom_margin_property) {
1258 *val = state->tv.margins.bottom;
1259 } else if (property == config->tv_mode_property) {
1260 *val = state->tv.mode;
1261 } else if (property == config->tv_brightness_property) {
1262 *val = state->tv.brightness;
1263 } else if (property == config->tv_contrast_property) {
1264 *val = state->tv.contrast;
1265 } else if (property == config->tv_flicker_reduction_property) {
1266 *val = state->tv.flicker_reduction;
1267 } else if (property == config->tv_overscan_property) {
1268 *val = state->tv.overscan;
1269 } else if (property == config->tv_saturation_property) {
1270 *val = state->tv.saturation;
1271 } else if (property == config->tv_hue_property) {
1272 *val = state->tv.hue;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001273 } else if (property == config->link_status_property) {
1274 *val = state->link_status;
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02001275 } else if (property == config->aspect_ratio_property) {
1276 *val = state->picture_aspect_ratio;
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001277 } else if (property == connector->scaling_mode_property) {
1278 *val = state->scaling_mode;
Rob Clarkac9c9252014-12-18 16:01:47 -05001279 } else if (connector->funcs->atomic_get_property) {
1280 return connector->funcs->atomic_get_property(connector,
1281 state, property, val);
1282 } else {
1283 return -EINVAL;
1284 }
1285
1286 return 0;
1287}
Rob Clarkac9c9252014-12-18 16:01:47 -05001288
Rob Clark88a48e22014-12-18 16:01:50 -05001289int drm_atomic_get_property(struct drm_mode_object *obj,
1290 struct drm_property *property, uint64_t *val)
1291{
1292 struct drm_device *dev = property->dev;
1293 int ret;
1294
1295 switch (obj->type) {
1296 case DRM_MODE_OBJECT_CONNECTOR: {
1297 struct drm_connector *connector = obj_to_connector(obj);
1298 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1299 ret = drm_atomic_connector_get_property(connector,
1300 connector->state, property, val);
1301 break;
1302 }
1303 case DRM_MODE_OBJECT_CRTC: {
1304 struct drm_crtc *crtc = obj_to_crtc(obj);
1305 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1306 ret = drm_atomic_crtc_get_property(crtc,
1307 crtc->state, property, val);
1308 break;
1309 }
1310 case DRM_MODE_OBJECT_PLANE: {
1311 struct drm_plane *plane = obj_to_plane(obj);
1312 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1313 ret = drm_atomic_plane_get_property(plane,
1314 plane->state, property, val);
1315 break;
1316 }
1317 default:
1318 ret = -EINVAL;
1319 break;
1320 }
1321
1322 return ret;
1323}
1324
1325/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001326 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001327 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001328 * @crtc: crtc to use for the plane
1329 *
1330 * Changing the assigned crtc for a plane requires us to grab the lock and state
1331 * for the new crtc, as needed. This function takes care of all these details
1332 * besides updating the pointer in the state object itself.
1333 *
1334 * Returns:
1335 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1336 * then the w/w mutex code has detected a deadlock and the entire atomic
1337 * sequence must be restarted. All other errors are fatal.
1338 */
1339int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001340drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1341 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001342{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001343 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001344 struct drm_crtc_state *crtc_state;
1345
Rob Clark6ddd3882014-11-21 15:28:31 -05001346 if (plane_state->crtc) {
1347 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1348 plane_state->crtc);
1349 if (WARN_ON(IS_ERR(crtc_state)))
1350 return PTR_ERR(crtc_state);
1351
1352 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1353 }
1354
1355 plane_state->crtc = crtc;
1356
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001357 if (crtc) {
1358 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1359 crtc);
1360 if (IS_ERR(crtc_state))
1361 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -05001362 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001363 }
1364
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001365 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001366 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1367 plane_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001368 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001369 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1370 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001371
1372 return 0;
1373}
1374EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1375
1376/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001377 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001378 * @plane_state: atomic state object for the plane
1379 * @fb: fb to use for the plane
1380 *
1381 * Changing the assigned framebuffer for a plane requires us to grab a reference
1382 * to the new fb and drop the reference to the old fb, if there is one. This
1383 * function takes care of all these details besides updating the pointer in the
1384 * state object itself.
1385 */
1386void
1387drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1388 struct drm_framebuffer *fb)
1389{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001390 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001391 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1392 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001393 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001394 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1395 plane_state);
Chris Wilson389f78b2016-11-25 15:32:30 +00001396
1397 drm_framebuffer_assign(&plane_state->fb, fb);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001398}
1399EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1400
1401/**
Gustavo Padovan13b55662016-11-07 19:03:30 +09001402 * drm_atomic_set_fence_for_plane - set fence for plane
1403 * @plane_state: atomic state object for the plane
1404 * @fence: dma_fence to use for the plane
1405 *
1406 * Helper to setup the plane_state fence in case it is not set yet.
1407 * By using this drivers doesn't need to worry if the user choose
1408 * implicit or explicit fencing.
1409 *
1410 * This function will not set the fence to the state if it was set
Daniel Vetterd5745282017-01-25 07:26:45 +01001411 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1412 * drop the reference to the fence as we are not storing it anywhere.
1413 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1414 * with the received implicit fence. In both cases this function consumes a
1415 * reference for @fence.
Gustavo Padovan13b55662016-11-07 19:03:30 +09001416 */
1417void
1418drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1419 struct dma_fence *fence)
1420{
1421 if (plane_state->fence) {
1422 dma_fence_put(fence);
1423 return;
1424 }
1425
1426 plane_state->fence = fence;
1427}
1428EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1429
1430/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001431 * drm_atomic_set_crtc_for_connector - set crtc for connector
1432 * @conn_state: atomic state object for the connector
1433 * @crtc: crtc to use for the connector
1434 *
1435 * Changing the assigned crtc for a connector requires us to grab the lock and
1436 * state for the new crtc, as needed. This function takes care of all these
1437 * details besides updating the pointer in the state object itself.
1438 *
1439 * Returns:
1440 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1441 * then the w/w mutex code has detected a deadlock and the entire atomic
1442 * sequence must be restarted. All other errors are fatal.
1443 */
1444int
1445drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1446 struct drm_crtc *crtc)
1447{
1448 struct drm_crtc_state *crtc_state;
1449
Chris Wilsone2d800a2016-05-06 12:47:45 +01001450 if (conn_state->crtc == crtc)
1451 return 0;
1452
1453 if (conn_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001454 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1455 conn_state->crtc);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001456
1457 crtc_state->connector_mask &=
1458 ~(1 << drm_connector_index(conn_state->connector));
Chris Wilsone2d800a2016-05-06 12:47:45 +01001459
Thierry Redingad093602017-02-28 15:46:39 +01001460 drm_connector_put(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001461 conn_state->crtc = NULL;
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001462 }
1463
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001464 if (crtc) {
1465 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1466 if (IS_ERR(crtc_state))
1467 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001468
1469 crtc_state->connector_mask |=
1470 1 << drm_connector_index(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001471
Thierry Redingad093602017-02-28 15:46:39 +01001472 drm_connector_get(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001473 conn_state->crtc = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001474
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001475 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1476 conn_state, crtc->base.id, crtc->name);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001477 } else {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001478 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1479 conn_state);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001480 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001481
1482 return 0;
1483}
1484EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1485
1486/**
1487 * drm_atomic_add_affected_connectors - add connectors for crtc
1488 * @state: atomic state
1489 * @crtc: DRM crtc
1490 *
1491 * This function walks the current configuration and adds all connectors
1492 * currently using @crtc to the atomic configuration @state. Note that this
1493 * function must acquire the connection mutex. This can potentially cause
1494 * unneeded seralization if the update is just for the planes on one crtc. Hence
1495 * drivers and helpers should only call this when really needed (e.g. when a
1496 * full modeset needs to happen due to some change).
1497 *
1498 * Returns:
1499 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1500 * then the w/w mutex code has detected a deadlock and the entire atomic
1501 * sequence must be restarted. All other errors are fatal.
1502 */
1503int
1504drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1505 struct drm_crtc *crtc)
1506{
1507 struct drm_mode_config *config = &state->dev->mode_config;
1508 struct drm_connector *connector;
1509 struct drm_connector_state *conn_state;
Daniel Vetter613051d2016-12-14 00:08:06 +01001510 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001511 struct drm_crtc_state *crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001512 int ret;
1513
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001514 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1515 if (IS_ERR(crtc_state))
1516 return PTR_ERR(crtc_state);
1517
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001518 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1519 if (ret)
1520 return ret;
1521
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001522 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1523 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001524
1525 /*
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001526 * Changed connectors are already in @state, so only need to look
1527 * at the connector_mask in crtc_state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001528 */
Thierry Redingb982dab2017-02-28 15:46:43 +01001529 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +01001530 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001531 if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001532 continue;
1533
1534 conn_state = drm_atomic_get_connector_state(state, connector);
Daniel Vetter613051d2016-12-14 00:08:06 +01001535 if (IS_ERR(conn_state)) {
Thierry Redingb982dab2017-02-28 15:46:43 +01001536 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001537 return PTR_ERR(conn_state);
Daniel Vetter613051d2016-12-14 00:08:06 +01001538 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001539 }
Thierry Redingb982dab2017-02-28 15:46:43 +01001540 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001541
1542 return 0;
1543}
1544EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1545
1546/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001547 * drm_atomic_add_affected_planes - add planes for crtc
1548 * @state: atomic state
1549 * @crtc: DRM crtc
1550 *
1551 * This function walks the current configuration and adds all planes
1552 * currently used by @crtc to the atomic configuration @state. This is useful
1553 * when an atomic commit also needs to check all currently enabled plane on
1554 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1555 * to avoid special code to force-enable all planes.
1556 *
1557 * Since acquiring a plane state will always also acquire the w/w mutex of the
1558 * current CRTC for that plane (if there is any) adding all the plane states for
1559 * a CRTC will not reduce parallism of atomic updates.
1560 *
1561 * Returns:
1562 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1563 * then the w/w mutex code has detected a deadlock and the entire atomic
1564 * sequence must be restarted. All other errors are fatal.
1565 */
1566int
1567drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1568 struct drm_crtc *crtc)
1569{
1570 struct drm_plane *plane;
1571
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001572 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001573
1574 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1575 struct drm_plane_state *plane_state =
1576 drm_atomic_get_plane_state(state, plane);
1577
1578 if (IS_ERR(plane_state))
1579 return PTR_ERR(plane_state);
1580 }
1581 return 0;
1582}
1583EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1584
1585/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001586 * drm_atomic_check_only - check whether a given config would work
1587 * @state: atomic configuration to check
1588 *
1589 * Note that this function can return -EDEADLK if the driver needed to acquire
1590 * more locks but encountered a deadlock. The caller must then do the usual w/w
1591 * backoff dance and restart. All other errors are fatal.
1592 *
1593 * Returns:
1594 * 0 on success, negative error code on failure.
1595 */
1596int drm_atomic_check_only(struct drm_atomic_state *state)
1597{
Rob Clark5e743732014-12-18 16:01:51 -05001598 struct drm_device *dev = state->dev;
1599 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001600 struct drm_plane *plane;
1601 struct drm_plane_state *plane_state;
1602 struct drm_crtc *crtc;
1603 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001604 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001605
Daniel Vetter17a38d92015-02-22 12:24:16 +01001606 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001607
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001608 for_each_new_plane_in_state(state, plane, plane_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001609 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001610 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001611 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1612 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001613 return ret;
1614 }
1615 }
1616
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001617 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001618 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001619 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001620 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1621 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001622 return ret;
1623 }
1624 }
1625
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001626 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001627 ret = config->funcs->atomic_check(state->dev, state);
1628
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001629 if (ret)
1630 return ret;
1631
Rob Clarkd34f20d2014-12-18 16:01:56 -05001632 if (!state->allow_modeset) {
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001633 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001634 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001635 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1636 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001637 return -EINVAL;
1638 }
1639 }
1640 }
1641
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001642 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001643}
1644EXPORT_SYMBOL(drm_atomic_check_only);
1645
1646/**
1647 * drm_atomic_commit - commit configuration atomically
1648 * @state: atomic configuration to check
1649 *
1650 * Note that this function can return -EDEADLK if the driver needed to acquire
1651 * more locks but encountered a deadlock. The caller must then do the usual w/w
1652 * backoff dance and restart. All other errors are fatal.
1653 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001654 * This function will take its own reference on @state.
1655 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001656 *
1657 * Returns:
1658 * 0 on success, negative error code on failure.
1659 */
1660int drm_atomic_commit(struct drm_atomic_state *state)
1661{
1662 struct drm_mode_config *config = &state->dev->mode_config;
1663 int ret;
1664
1665 ret = drm_atomic_check_only(state);
1666 if (ret)
1667 return ret;
1668
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001669 DRM_DEBUG_ATOMIC("committing %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001670
1671 return config->funcs->atomic_commit(state->dev, state, false);
1672}
1673EXPORT_SYMBOL(drm_atomic_commit);
1674
1675/**
Daniel Vetterd5745282017-01-25 07:26:45 +01001676 * drm_atomic_nonblocking_commit - atomic nonblocking commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001677 * @state: atomic configuration to check
1678 *
1679 * Note that this function can return -EDEADLK if the driver needed to acquire
1680 * more locks but encountered a deadlock. The caller must then do the usual w/w
1681 * backoff dance and restart. All other errors are fatal.
1682 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001683 * This function will take its own reference on @state.
1684 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001685 *
1686 * Returns:
1687 * 0 on success, negative error code on failure.
1688 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001689int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001690{
1691 struct drm_mode_config *config = &state->dev->mode_config;
1692 int ret;
1693
1694 ret = drm_atomic_check_only(state);
1695 if (ret)
1696 return ret;
1697
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001698 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001699
1700 return config->funcs->atomic_commit(state->dev, state, true);
1701}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001702EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001703
Rob Clarkfceffb322016-11-05 11:08:09 -04001704static void drm_atomic_print_state(const struct drm_atomic_state *state)
1705{
1706 struct drm_printer p = drm_info_printer(state->dev->dev);
1707 struct drm_plane *plane;
1708 struct drm_plane_state *plane_state;
1709 struct drm_crtc *crtc;
1710 struct drm_crtc_state *crtc_state;
1711 struct drm_connector *connector;
1712 struct drm_connector_state *connector_state;
1713 int i;
1714
1715 DRM_DEBUG_ATOMIC("checking %p\n", state);
1716
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001717 for_each_new_plane_in_state(state, plane, plane_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001718 drm_atomic_plane_print_state(&p, plane_state);
1719
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001720 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001721 drm_atomic_crtc_print_state(&p, crtc_state);
1722
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001723 for_each_new_connector_in_state(state, connector, connector_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001724 drm_atomic_connector_print_state(&p, connector_state);
1725}
1726
Daniel Vetterc2d85562017-04-03 10:32:54 +02001727static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1728 bool take_locks)
1729{
1730 struct drm_mode_config *config = &dev->mode_config;
1731 struct drm_plane *plane;
1732 struct drm_crtc *crtc;
1733 struct drm_connector *connector;
1734 struct drm_connector_list_iter conn_iter;
1735
1736 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1737 return;
1738
1739 list_for_each_entry(plane, &config->plane_list, head) {
1740 if (take_locks)
1741 drm_modeset_lock(&plane->mutex, NULL);
1742 drm_atomic_plane_print_state(p, plane->state);
1743 if (take_locks)
1744 drm_modeset_unlock(&plane->mutex);
1745 }
1746
1747 list_for_each_entry(crtc, &config->crtc_list, head) {
1748 if (take_locks)
1749 drm_modeset_lock(&crtc->mutex, NULL);
1750 drm_atomic_crtc_print_state(p, crtc->state);
1751 if (take_locks)
1752 drm_modeset_unlock(&crtc->mutex);
1753 }
1754
1755 drm_connector_list_iter_begin(dev, &conn_iter);
1756 if (take_locks)
1757 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1758 drm_for_each_connector_iter(connector, &conn_iter)
1759 drm_atomic_connector_print_state(p, connector->state);
1760 if (take_locks)
1761 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1762 drm_connector_list_iter_end(&conn_iter);
1763}
1764
Rob Clark6559c902016-11-05 11:08:10 -04001765/**
1766 * drm_state_dump - dump entire device atomic state
1767 * @dev: the drm device
1768 * @p: where to print the state to
1769 *
1770 * Just for debugging. Drivers might want an option to dump state
1771 * to dmesg in case of error irq's. (Hint, you probably want to
1772 * ratelimit this!)
1773 *
1774 * The caller must drm_modeset_lock_all(), or if this is called
1775 * from error irq handler, it should not be enabled by default.
1776 * (Ie. if you are debugging errors you might not care that this
1777 * is racey. But calling this without all modeset locks held is
1778 * not inherently safe.)
1779 */
1780void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1781{
Daniel Vetterc2d85562017-04-03 10:32:54 +02001782 __drm_state_dump(dev, p, false);
Rob Clark6559c902016-11-05 11:08:10 -04001783}
1784EXPORT_SYMBOL(drm_state_dump);
1785
1786#ifdef CONFIG_DEBUG_FS
1787static int drm_state_info(struct seq_file *m, void *data)
1788{
1789 struct drm_info_node *node = (struct drm_info_node *) m->private;
1790 struct drm_device *dev = node->minor->dev;
1791 struct drm_printer p = drm_seq_file_printer(m);
1792
Daniel Vetterc2d85562017-04-03 10:32:54 +02001793 __drm_state_dump(dev, &p, true);
Rob Clark6559c902016-11-05 11:08:10 -04001794
1795 return 0;
1796}
1797
1798/* any use in debugfs files to dump individual planes/crtc/etc? */
1799static const struct drm_info_list drm_atomic_debugfs_list[] = {
1800 {"state", drm_state_info, 0},
1801};
1802
1803int drm_atomic_debugfs_init(struct drm_minor *minor)
1804{
1805 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1806 ARRAY_SIZE(drm_atomic_debugfs_list),
1807 minor->debugfs_root, minor);
1808}
1809#endif
1810
Rob Clarkd34f20d2014-12-18 16:01:56 -05001811/*
Liviu Dudau21be9152017-11-01 14:04:36 +00001812 * The big monster ioctl
Rob Clarkd34f20d2014-12-18 16:01:56 -05001813 */
1814
1815static struct drm_pending_vblank_event *create_vblank_event(
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09001816 struct drm_device *dev, uint64_t user_data)
Rob Clarkd34f20d2014-12-18 16:01:56 -05001817{
1818 struct drm_pending_vblank_event *e = NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001819
1820 e = kzalloc(sizeof *e, GFP_KERNEL);
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001821 if (!e)
1822 return NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001823
1824 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001825 e->event.base.length = sizeof(e->event);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001826 e->event.user_data = user_data;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001827
Rob Clarkd34f20d2014-12-18 16:01:56 -05001828 return e;
1829}
1830
Daniel Vetter144a7992017-07-25 14:02:04 +02001831int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
1832 struct drm_connector *connector,
1833 int mode)
1834{
1835 struct drm_connector *tmp_connector;
1836 struct drm_connector_state *new_conn_state;
1837 struct drm_crtc *crtc;
1838 struct drm_crtc_state *crtc_state;
1839 int i, ret, old_mode = connector->dpms;
1840 bool active = false;
1841
1842 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1843 state->acquire_ctx);
1844 if (ret)
1845 return ret;
1846
1847 if (mode != DRM_MODE_DPMS_ON)
1848 mode = DRM_MODE_DPMS_OFF;
1849 connector->dpms = mode;
1850
1851 crtc = connector->state->crtc;
1852 if (!crtc)
1853 goto out;
1854 ret = drm_atomic_add_affected_connectors(state, crtc);
1855 if (ret)
1856 goto out;
1857
1858 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1859 if (IS_ERR(crtc_state)) {
1860 ret = PTR_ERR(crtc_state);
1861 goto out;
1862 }
1863
1864 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
1865 if (new_conn_state->crtc != crtc)
1866 continue;
1867 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
1868 active = true;
1869 break;
1870 }
1871 }
1872
1873 crtc_state->active = active;
1874 ret = drm_atomic_commit(state);
1875out:
1876 if (ret != 0)
1877 connector->dpms = old_mode;
1878 return ret;
1879}
1880
1881int drm_atomic_set_property(struct drm_atomic_state *state,
1882 struct drm_mode_object *obj,
1883 struct drm_property *prop,
1884 uint64_t prop_value)
Rob Clarkd34f20d2014-12-18 16:01:56 -05001885{
1886 struct drm_mode_object *ref;
1887 int ret;
1888
1889 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1890 return -EINVAL;
1891
1892 switch (obj->type) {
1893 case DRM_MODE_OBJECT_CONNECTOR: {
1894 struct drm_connector *connector = obj_to_connector(obj);
1895 struct drm_connector_state *connector_state;
1896
1897 connector_state = drm_atomic_get_connector_state(state, connector);
1898 if (IS_ERR(connector_state)) {
1899 ret = PTR_ERR(connector_state);
1900 break;
1901 }
1902
1903 ret = drm_atomic_connector_set_property(connector,
1904 connector_state, prop, prop_value);
1905 break;
1906 }
1907 case DRM_MODE_OBJECT_CRTC: {
1908 struct drm_crtc *crtc = obj_to_crtc(obj);
1909 struct drm_crtc_state *crtc_state;
1910
1911 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1912 if (IS_ERR(crtc_state)) {
1913 ret = PTR_ERR(crtc_state);
1914 break;
1915 }
1916
1917 ret = drm_atomic_crtc_set_property(crtc,
1918 crtc_state, prop, prop_value);
1919 break;
1920 }
1921 case DRM_MODE_OBJECT_PLANE: {
1922 struct drm_plane *plane = obj_to_plane(obj);
1923 struct drm_plane_state *plane_state;
1924
1925 plane_state = drm_atomic_get_plane_state(state, plane);
1926 if (IS_ERR(plane_state)) {
1927 ret = PTR_ERR(plane_state);
1928 break;
1929 }
1930
1931 ret = drm_atomic_plane_set_property(plane,
1932 plane_state, prop, prop_value);
1933 break;
1934 }
1935 default:
1936 ret = -EINVAL;
1937 break;
1938 }
1939
1940 drm_property_change_valid_put(prop, ref);
1941 return ret;
1942}
1943
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001944/**
Maarten Lankhorst9744bf42015-11-24 10:34:34 +01001945 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001946 *
1947 * @dev: drm device to check.
1948 * @plane_mask: plane mask for planes that were updated.
1949 * @ret: return value, can be -EDEADLK for a retry.
1950 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001951 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1952 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1953 * is a common operation for each atomic update, so this call is split off as a
1954 * helper.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001955 */
1956void drm_atomic_clean_old_fb(struct drm_device *dev,
1957 unsigned plane_mask,
1958 int ret)
1959{
1960 struct drm_plane *plane;
1961
1962 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1963 * locks (ie. while it is still safe to deref plane->state). We
1964 * need to do this here because the driver entry points cannot
1965 * distinguish between legacy and atomic ioctls.
1966 */
1967 drm_for_each_plane_mask(plane, dev, plane_mask) {
1968 if (ret == 0) {
1969 struct drm_framebuffer *new_fb = plane->state->fb;
1970 if (new_fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01001971 drm_framebuffer_get(new_fb);
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001972 plane->fb = new_fb;
1973 plane->crtc = plane->state->crtc;
1974
1975 if (plane->old_fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01001976 drm_framebuffer_put(plane->old_fb);
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001977 }
1978 plane->old_fb = NULL;
1979 }
1980}
1981EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1982
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001983/**
1984 * DOC: explicit fencing properties
1985 *
1986 * Explicit fencing allows userspace to control the buffer synchronization
1987 * between devices. A Fence or a group of fences are transfered to/from
1988 * userspace using Sync File fds and there are two DRM properties for that.
1989 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1990 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1991 *
1992 * As a contrast, with implicit fencing the kernel keeps track of any
1993 * ongoing rendering, and automatically ensures that the atomic update waits
1994 * for any pending rendering to complete. For shared buffers represented with
Daniel Vetterd5745282017-01-25 07:26:45 +01001995 * a &struct dma_buf this is tracked in &struct reservation_object.
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001996 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1997 * whereas explicit fencing is what Android wants.
1998 *
1999 * "IN_FENCE_FD”:
2000 * Use this property to pass a fence that DRM should wait on before
2001 * proceeding with the Atomic Commit request and show the framebuffer for
2002 * the plane on the screen. The fence can be either a normal fence or a
2003 * merged one, the sync_file framework will handle both cases and use a
2004 * fence_array if a merged fence is received. Passing -1 here means no
2005 * fences to wait on.
2006 *
2007 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
2008 * it will only check if the Sync File is a valid one.
2009 *
2010 * On the driver side the fence is stored on the @fence parameter of
Daniel Vetterea0dd852016-12-29 21:48:26 +01002011 * &struct drm_plane_state. Drivers which also support implicit fencing
Gustavo Padovan9a83a712016-11-22 09:11:28 +09002012 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
2013 * to make sure there's consistent behaviour between drivers in precedence
2014 * of implicit vs. explicit fencing.
2015 *
2016 * "OUT_FENCE_PTR”:
2017 * Use this property to pass a file descriptor pointer to DRM. Once the
2018 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
2019 * the file descriptor number of a Sync File. This Sync File contains the
2020 * CRTC fence that will be signaled when all framebuffers present on the
2021 * Atomic Commit * request for that given CRTC are scanned out on the
2022 * screen.
2023 *
2024 * The Atomic Commit request fails if a invalid pointer is passed. If the
2025 * Atomic Commit request fails for any other reason the out fence fd
2026 * returned will be -1. On a Atomic Commit with the
2027 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2028 *
2029 * Note that out-fences don't have a special interface to drivers and are
Daniel Vetterea0dd852016-12-29 21:48:26 +01002030 * internally represented by a &struct drm_pending_vblank_event in struct
Gustavo Padovan9a83a712016-11-22 09:11:28 +09002031 * &drm_crtc_state, which is also used by the nonblocking atomic commit
2032 * helpers and for the DRM event handling for existing userspace.
2033 */
2034
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002035struct drm_out_fence_state {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02002036 s32 __user *out_fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002037 struct sync_file *sync_file;
2038 int fd;
2039};
2040
2041static int setup_out_fence(struct drm_out_fence_state *fence_state,
2042 struct dma_fence *fence)
2043{
2044 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
2045 if (fence_state->fd < 0)
2046 return fence_state->fd;
2047
2048 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
2049 return -EFAULT;
2050
2051 fence_state->sync_file = sync_file_create(fence);
2052 if (!fence_state->sync_file)
2053 return -ENOMEM;
2054
2055 return 0;
2056}
2057
2058static int prepare_crtc_signaling(struct drm_device *dev,
2059 struct drm_atomic_state *state,
2060 struct drm_mode_atomic *arg,
2061 struct drm_file *file_priv,
2062 struct drm_out_fence_state **fence_state,
2063 unsigned int *num_fences)
2064{
2065 struct drm_crtc *crtc;
2066 struct drm_crtc_state *crtc_state;
Andrey Grodzovsky330c4222017-06-20 13:57:06 -04002067 int i, c = 0, ret;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002068
2069 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
2070 return 0;
2071
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002072 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02002073 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002074
2075 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
2076
2077 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
2078 struct drm_pending_vblank_event *e;
2079
2080 e = create_vblank_event(dev, arg->user_data);
2081 if (!e)
2082 return -ENOMEM;
2083
2084 crtc_state->event = e;
2085 }
2086
2087 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2088 struct drm_pending_vblank_event *e = crtc_state->event;
2089
2090 if (!file_priv)
2091 continue;
2092
2093 ret = drm_event_reserve_init(dev, file_priv, &e->base,
2094 &e->event.base);
2095 if (ret) {
2096 kfree(e);
2097 crtc_state->event = NULL;
2098 return ret;
2099 }
2100 }
2101
2102 if (fence_ptr) {
2103 struct dma_fence *fence;
2104 struct drm_out_fence_state *f;
2105
2106 f = krealloc(*fence_state, sizeof(**fence_state) *
2107 (*num_fences + 1), GFP_KERNEL);
2108 if (!f)
2109 return -ENOMEM;
2110
2111 memset(&f[*num_fences], 0, sizeof(*f));
2112
2113 f[*num_fences].out_fence_ptr = fence_ptr;
2114 *fence_state = f;
2115
Gustavo Padovan35f8cc32016-12-06 15:47:17 -02002116 fence = drm_crtc_create_fence(crtc);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002117 if (!fence)
2118 return -ENOMEM;
2119
2120 ret = setup_out_fence(&f[(*num_fences)++], fence);
2121 if (ret) {
2122 dma_fence_put(fence);
2123 return ret;
2124 }
2125
2126 crtc_state->event->base.fence = fence;
2127 }
Andrey Grodzovsky330c4222017-06-20 13:57:06 -04002128
2129 c++;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002130 }
2131
Andrey Grodzovsky330c4222017-06-20 13:57:06 -04002132 /*
2133 * Having this flag means user mode pends on event which will never
2134 * reach due to lack of at least one CRTC for signaling
2135 */
2136 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2137 return -EINVAL;
2138
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002139 return 0;
2140}
2141
2142static void complete_crtc_signaling(struct drm_device *dev,
2143 struct drm_atomic_state *state,
2144 struct drm_out_fence_state *fence_state,
2145 unsigned int num_fences,
2146 bool install_fds)
2147{
2148 struct drm_crtc *crtc;
2149 struct drm_crtc_state *crtc_state;
2150 int i;
2151
2152 if (install_fds) {
2153 for (i = 0; i < num_fences; i++)
2154 fd_install(fence_state[i].fd,
2155 fence_state[i].sync_file->file);
2156
2157 kfree(fence_state);
2158 return;
2159 }
2160
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002161 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002162 struct drm_pending_vblank_event *event = crtc_state->event;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002163 /*
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002164 * Free the allocated event. drm_atomic_helper_setup_commit
2165 * can allocate an event too, so only free it if it's ours
2166 * to prevent a double free in drm_atomic_state_clear.
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002167 */
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002168 if (event && (event->base.fence || event->base.file_priv)) {
2169 drm_event_cancel_free(dev, &event->base);
2170 crtc_state->event = NULL;
2171 }
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002172 }
2173
2174 if (!fence_state)
2175 return;
2176
2177 for (i = 0; i < num_fences; i++) {
2178 if (fence_state[i].sync_file)
2179 fput(fence_state[i].sync_file->file);
2180 if (fence_state[i].fd >= 0)
2181 put_unused_fd(fence_state[i].fd);
2182
2183 /* If this fails log error to the user */
2184 if (fence_state[i].out_fence_ptr &&
2185 put_user(-1, fence_state[i].out_fence_ptr))
2186 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2187 }
2188
2189 kfree(fence_state);
2190}
2191
Rob Clarkd34f20d2014-12-18 16:01:56 -05002192int drm_mode_atomic_ioctl(struct drm_device *dev,
2193 void *data, struct drm_file *file_priv)
2194{
2195 struct drm_mode_atomic *arg = data;
2196 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2197 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2198 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2199 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2200 unsigned int copied_objs, copied_props;
2201 struct drm_atomic_state *state;
2202 struct drm_modeset_acquire_ctx ctx;
2203 struct drm_plane *plane;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002204 struct drm_out_fence_state *fence_state;
Maarten Lankhorst45723722015-11-11 11:29:08 +01002205 unsigned plane_mask;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002206 int ret = 0;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002207 unsigned int i, j, num_fences;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002208
2209 /* disallow for drivers not supporting atomic: */
2210 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2211 return -EINVAL;
2212
2213 /* disallow for userspace that has not enabled atomic cap (even
2214 * though this may be a bit overkill, since legacy userspace
2215 * wouldn't know how to call this ioctl)
2216 */
2217 if (!file_priv->atomic)
2218 return -EINVAL;
2219
2220 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2221 return -EINVAL;
2222
2223 if (arg->reserved)
2224 return -EINVAL;
2225
2226 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2227 !dev->mode_config.async_page_flip)
2228 return -EINVAL;
2229
2230 /* can't test and expect an event at the same time. */
2231 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2232 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2233 return -EINVAL;
2234
Maarten Lankhorstdad56ce2017-09-12 15:37:45 +02002235 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002236
2237 state = drm_atomic_state_alloc(dev);
2238 if (!state)
2239 return -ENOMEM;
2240
2241 state->acquire_ctx = &ctx;
2242 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2243
2244retry:
Maarten Lankhorst45723722015-11-11 11:29:08 +01002245 plane_mask = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002246 copied_objs = 0;
2247 copied_props = 0;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002248 fence_state = NULL;
2249 num_fences = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002250
2251 for (i = 0; i < arg->count_objs; i++) {
2252 uint32_t obj_id, count_props;
2253 struct drm_mode_object *obj;
2254
2255 if (get_user(obj_id, objs_ptr + copied_objs)) {
2256 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002257 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002258 }
2259
2260 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
Dave Airlieb164d312016-04-27 11:10:09 +10002261 if (!obj) {
2262 ret = -ENOENT;
2263 goto out;
2264 }
2265
2266 if (!obj->properties) {
Thierry Reding020a2182017-02-28 15:46:38 +01002267 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002268 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002269 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002270 }
2271
Rob Clarkd34f20d2014-12-18 16:01:56 -05002272 if (get_user(count_props, count_props_ptr + copied_objs)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002273 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002274 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002275 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002276 }
2277
2278 copied_objs++;
2279
2280 for (j = 0; j < count_props; j++) {
2281 uint32_t prop_id;
2282 uint64_t prop_value;
2283 struct drm_property *prop;
2284
2285 if (get_user(prop_id, props_ptr + copied_props)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002286 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002287 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002288 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002289 }
2290
Maarten Lankhorstf92f0532016-09-08 12:30:01 +02002291 prop = drm_mode_obj_find_prop_id(obj, prop_id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002292 if (!prop) {
Thierry Reding020a2182017-02-28 15:46:38 +01002293 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002294 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002295 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002296 }
2297
Guenter Roeck42c58142015-01-12 21:12:17 -08002298 if (copy_from_user(&prop_value,
2299 prop_values_ptr + copied_props,
2300 sizeof(prop_value))) {
Thierry Reding020a2182017-02-28 15:46:38 +01002301 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002302 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002303 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002304 }
2305
Daniel Vetter144a7992017-07-25 14:02:04 +02002306 ret = drm_atomic_set_property(state, obj, prop,
2307 prop_value);
Dave Airlieb164d312016-04-27 11:10:09 +10002308 if (ret) {
Thierry Reding020a2182017-02-28 15:46:38 +01002309 drm_mode_object_put(obj);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002310 goto out;
Dave Airlieb164d312016-04-27 11:10:09 +10002311 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05002312
2313 copied_props++;
2314 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002315
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002316 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2317 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002318 plane = obj_to_plane(obj);
2319 plane_mask |= (1 << drm_plane_index(plane));
2320 plane->old_fb = plane->fb;
2321 }
Thierry Reding020a2182017-02-28 15:46:38 +01002322 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002323 }
2324
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002325 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2326 &num_fences);
2327 if (ret)
2328 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002329
2330 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2331 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002332 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002333 ret = drm_atomic_nonblocking_commit(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002334 } else {
Rob Clarkfceffb322016-11-05 11:08:09 -04002335 if (unlikely(drm_debug & DRM_UT_STATE))
2336 drm_atomic_print_state(state);
2337
Rob Clarkd34f20d2014-12-18 16:01:56 -05002338 ret = drm_atomic_commit(state);
2339 }
2340
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002341out:
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01002342 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002343
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002344 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002345
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002346 if (ret == -EDEADLK) {
2347 drm_atomic_state_clear(state);
Maarten Lankhorstdad56ce2017-09-12 15:37:45 +02002348 ret = drm_modeset_backoff(&ctx);
2349 if (!ret)
2350 goto retry;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002351 }
2352
Chris Wilson08536952016-10-14 13:18:18 +01002353 drm_atomic_state_put(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002354
2355 drm_modeset_drop_locks(&ctx);
2356 drm_modeset_acquire_fini(&ctx);
2357
2358 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002359}