blob: f32506a7c1d61e91494e47f6a731fddaf1cc991c [file] [log] [blame]
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28
29#include <drm/drmP.h>
30#include <drm/drm_atomic.h>
Lionel Landwerlin5488dc12016-02-26 17:05:00 +000031#include <drm/drm_mode.h>
Daniel Vettercc4ceb42014-07-25 21:30:38 +020032#include <drm/drm_plane_helper.h>
Rob Clarkfceffb322016-11-05 11:08:09 -040033#include <drm/drm_print.h>
Gustavo Padovan96260142016-11-15 22:06:39 +090034#include <linux/sync_file.h>
Daniel Vettercc4ceb42014-07-25 21:30:38 +020035
Thierry Redingbe35f942016-04-28 15:19:56 +020036#include "drm_crtc_internal.h"
37
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010038void __drm_crtc_commit_free(struct kref *kref)
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020039{
40 struct drm_crtc_commit *commit =
41 container_of(kref, struct drm_crtc_commit, ref);
42
43 kfree(commit);
44}
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010045EXPORT_SYMBOL(__drm_crtc_commit_free);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020046
Maarten Lankhorst036ef572015-05-18 10:06:40 +020047/**
48 * drm_atomic_state_default_release -
49 * release memory initialized by drm_atomic_state_init
50 * @state: atomic state
51 *
52 * Free all the memory allocated by drm_atomic_state_init.
53 * This is useful for drivers that subclass the atomic state.
54 */
55void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020056{
57 kfree(state->connectors);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020058 kfree(state->crtcs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020059 kfree(state->planes);
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;
111 struct drm_atomic_state *state;
112
113 if (!config->funcs->atomic_state_alloc) {
114 state = kzalloc(sizeof(*state), GFP_KERNEL);
115 if (!state)
116 return NULL;
117 if (drm_atomic_state_init(dev, state) < 0) {
118 kfree(state);
119 return NULL;
120 }
121 return state;
122 }
123
124 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200125}
126EXPORT_SYMBOL(drm_atomic_state_alloc);
127
128/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200129 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200130 * @state: atomic state
131 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200132 * Default implementation for clearing atomic state.
133 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200134 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200135void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200136{
137 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100138 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200139 int i;
140
Daniel Vetter17a38d92015-02-22 12:24:16 +0100141 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200142
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100143 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200144 struct drm_connector *connector = state->connectors[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200145
146 if (!connector)
147 continue;
148
Dave Airlied2307de2016-04-27 11:27:39 +1000149 connector->funcs->atomic_destroy_state(connector,
Daniel Vetter63e83c12016-06-02 00:06:32 +0200150 state->connectors[i].state);
151 state->connectors[i].ptr = NULL;
152 state->connectors[i].state = NULL;
Thierry Redingad093602017-02-28 15:46:39 +0100153 drm_connector_put(connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200154 }
155
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100156 for (i = 0; i < config->num_crtc; i++) {
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200157 struct drm_crtc *crtc = state->crtcs[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200158
159 if (!crtc)
160 continue;
161
162 crtc->funcs->atomic_destroy_state(crtc,
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200163 state->crtcs[i].state);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200164
165 if (state->crtcs[i].commit) {
166 kfree(state->crtcs[i].commit->event);
167 state->crtcs[i].commit->event = NULL;
168 drm_crtc_commit_put(state->crtcs[i].commit);
169 }
170
171 state->crtcs[i].commit = NULL;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200172 state->crtcs[i].ptr = NULL;
173 state->crtcs[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200174 }
175
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100176 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vetterb8b53422016-06-02 00:06:33 +0200177 struct drm_plane *plane = state->planes[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200178
179 if (!plane)
180 continue;
181
182 plane->funcs->atomic_destroy_state(plane,
Daniel Vetterb8b53422016-06-02 00:06:33 +0200183 state->planes[i].state);
184 state->planes[i].ptr = NULL;
185 state->planes[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200186 }
187}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200188EXPORT_SYMBOL(drm_atomic_state_default_clear);
189
190/**
191 * drm_atomic_state_clear - clear state object
192 * @state: atomic state
193 *
194 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
195 * all locks. So someone else could sneak in and change the current modeset
196 * configuration. Which means that all the state assembled in @state is no
197 * longer an atomic update to the current state, but to some arbitrary earlier
Daniel Vetterd5745282017-01-25 07:26:45 +0100198 * state. Which could break assumptions the driver's
199 * &drm_mode_config_funcs.atomic_check likely relies on.
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200200 *
201 * Hence we must clear all cached state and completely start over, using this
202 * function.
203 */
204void drm_atomic_state_clear(struct drm_atomic_state *state)
205{
206 struct drm_device *dev = state->dev;
207 struct drm_mode_config *config = &dev->mode_config;
208
209 if (config->funcs->atomic_state_clear)
210 config->funcs->atomic_state_clear(state);
211 else
212 drm_atomic_state_default_clear(state);
213}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200214EXPORT_SYMBOL(drm_atomic_state_clear);
215
216/**
Chris Wilson08536952016-10-14 13:18:18 +0100217 * __drm_atomic_state_free - free all memory for an atomic state
218 * @ref: This atomic state to deallocate
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200219 *
220 * This frees all memory associated with an atomic state, including all the
221 * per-object state for planes, crtcs and connectors.
222 */
Chris Wilson08536952016-10-14 13:18:18 +0100223void __drm_atomic_state_free(struct kref *ref)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200224{
Chris Wilson08536952016-10-14 13:18:18 +0100225 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
226 struct drm_mode_config *config = &state->dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200227
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200228 drm_atomic_state_clear(state);
229
Daniel Vetter17a38d92015-02-22 12:24:16 +0100230 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200231
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200232 if (config->funcs->atomic_state_free) {
233 config->funcs->atomic_state_free(state);
234 } else {
235 drm_atomic_state_default_release(state);
236 kfree(state);
237 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200238}
Chris Wilson08536952016-10-14 13:18:18 +0100239EXPORT_SYMBOL(__drm_atomic_state_free);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200240
241/**
242 * drm_atomic_get_crtc_state - get crtc state
243 * @state: global atomic state object
244 * @crtc: crtc to get state object for
245 *
246 * This function returns the crtc state for the given crtc, allocating it if
247 * needed. It will also grab the relevant crtc lock to make sure that the state
248 * is consistent.
249 *
250 * Returns:
251 *
252 * Either the allocated state or the error code encoded into the pointer. When
253 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
254 * entire atomic sequence must be restarted. All other errors are fatal.
255 */
256struct drm_crtc_state *
257drm_atomic_get_crtc_state(struct drm_atomic_state *state,
258 struct drm_crtc *crtc)
259{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200260 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200261 struct drm_crtc_state *crtc_state;
262
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200263 WARN_ON(!state->acquire_ctx);
264
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200265 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
266 if (crtc_state)
267 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200268
269 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
270 if (ret)
271 return ERR_PTR(ret);
272
273 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
274 if (!crtc_state)
275 return ERR_PTR(-ENOMEM);
276
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200277 state->crtcs[index].state = crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100278 state->crtcs[index].old_state = crtc->state;
279 state->crtcs[index].new_state = crtc_state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200280 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200281 crtc_state->state = state;
282
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200283 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
284 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200285
286 return crtc_state;
287}
288EXPORT_SYMBOL(drm_atomic_get_crtc_state);
289
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900290static void set_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200291 struct drm_crtc *crtc, s32 __user *fence_ptr)
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900292{
293 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
294}
295
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200296static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900297 struct drm_crtc *crtc)
298{
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200299 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900300
301 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
302 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
303
304 return fence_ptr;
305}
306
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200307/**
Daniel Stone819364d2015-05-26 14:36:48 +0100308 * drm_atomic_set_mode_for_crtc - set mode for CRTC
309 * @state: the CRTC whose incoming state to update
310 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
311 *
Dhinakaran Pandiyancbef9092017-01-30 22:18:38 -0800312 * Set a mode (originating from the kernel) on the desired CRTC state and update
313 * the enable property.
Daniel Stone819364d2015-05-26 14:36:48 +0100314 *
315 * RETURNS:
316 * Zero on success, error code on failure. Cannot return -EDEADLK.
317 */
318int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
319 struct drm_display_mode *mode)
320{
Daniel Stone99cf4a22015-05-25 19:11:51 +0100321 struct drm_mode_modeinfo umode;
322
Daniel Stone819364d2015-05-26 14:36:48 +0100323 /* Early return for no change. */
324 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
325 return 0;
326
Thierry Reding6472e502017-02-28 15:46:42 +0100327 drm_property_blob_put(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100328 state->mode_blob = NULL;
329
Daniel Stone819364d2015-05-26 14:36:48 +0100330 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100331 drm_mode_convert_to_umode(&umode, mode);
332 state->mode_blob =
333 drm_property_create_blob(state->crtc->dev,
334 sizeof(umode),
335 &umode);
336 if (IS_ERR(state->mode_blob))
337 return PTR_ERR(state->mode_blob);
338
Daniel Stone819364d2015-05-26 14:36:48 +0100339 drm_mode_copy(&state->mode, mode);
340 state->enable = true;
341 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
342 mode->name, state);
343 } else {
344 memset(&state->mode, 0, sizeof(state->mode));
345 state->enable = false;
346 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
347 state);
348 }
349
350 return 0;
351}
352EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
353
Daniel Stone819364d2015-05-26 14:36:48 +0100354/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100355 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
356 * @state: the CRTC whose incoming state to update
357 * @blob: pointer to blob property to use for mode
358 *
359 * Set a mode (originating from a blob property) on the desired CRTC state.
360 * This function will take a reference on the blob property for the CRTC state,
361 * and release the reference held on the state's existing mode property, if any
362 * was set.
363 *
364 * RETURNS:
365 * Zero on success, error code on failure. Cannot return -EDEADLK.
366 */
367int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
368 struct drm_property_blob *blob)
369{
370 if (blob == state->mode_blob)
371 return 0;
372
Thierry Reding6472e502017-02-28 15:46:42 +0100373 drm_property_blob_put(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100374 state->mode_blob = NULL;
375
Tomi Valkeinen67098872016-05-31 15:03:17 +0300376 memset(&state->mode, 0, sizeof(state->mode));
377
Daniel Stone955f3c32015-05-25 19:11:52 +0100378 if (blob) {
379 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
380 drm_mode_convert_umode(&state->mode,
381 (const struct drm_mode_modeinfo *)
382 blob->data))
383 return -EINVAL;
384
Thierry Reding6472e502017-02-28 15:46:42 +0100385 state->mode_blob = drm_property_blob_get(blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100386 state->enable = true;
387 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
388 state->mode.name, state);
389 } else {
Daniel Stone955f3c32015-05-25 19:11:52 +0100390 state->enable = false;
391 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
392 state);
393 }
394
395 return 0;
396}
397EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
398
399/**
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000400 * drm_atomic_replace_property_blob - replace a blob property
401 * @blob: a pointer to the member blob to be replaced
402 * @new_blob: the new blob to replace with
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000403 * @replaced: whether the blob has been replaced
404 *
405 * RETURNS:
406 * Zero on success, error code on failure
407 */
408static void
409drm_atomic_replace_property_blob(struct drm_property_blob **blob,
410 struct drm_property_blob *new_blob,
411 bool *replaced)
412{
413 struct drm_property_blob *old_blob = *blob;
414
415 if (old_blob == new_blob)
416 return;
417
Thierry Reding6472e502017-02-28 15:46:42 +0100418 drm_property_blob_put(old_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000419 if (new_blob)
Thierry Reding6472e502017-02-28 15:46:42 +0100420 drm_property_blob_get(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000421 *blob = new_blob;
422 *replaced = true;
423
424 return;
425}
426
427static int
428drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
429 struct drm_property_blob **blob,
430 uint64_t blob_id,
431 ssize_t expected_size,
432 bool *replaced)
433{
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000434 struct drm_property_blob *new_blob = NULL;
435
436 if (blob_id != 0) {
Felix Monningercac5fced2016-10-25 22:28:08 +0100437 new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000438 if (new_blob == NULL)
439 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100440
441 if (expected_size > 0 && expected_size != new_blob->length) {
Thierry Reding6472e502017-02-28 15:46:42 +0100442 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000443 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100444 }
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000445 }
446
447 drm_atomic_replace_property_blob(blob, new_blob, replaced);
Thierry Reding6472e502017-02-28 15:46:42 +0100448 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000449
450 return 0;
451}
452
453/**
Rob Clark40ecc692014-12-18 16:01:46 -0500454 * drm_atomic_crtc_set_property - set property on CRTC
455 * @crtc: the drm CRTC to set a property on
456 * @state: the state object to update with the new property value
457 * @property: the property to set
458 * @val: the new property value
459 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100460 * This function handles generic/core properties and calls out to driver's
461 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
462 * consistent behavior you must call this function rather than the driver hook
463 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500464 *
465 * RETURNS:
466 * Zero on success, error code on failure
467 */
468int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
469 struct drm_crtc_state *state, struct drm_property *property,
470 uint64_t val)
471{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100472 struct drm_device *dev = crtc->dev;
473 struct drm_mode_config *config = &dev->mode_config;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000474 bool replaced = false;
Daniel Stone955f3c32015-05-25 19:11:52 +0100475 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100476
Daniel Stone27798362015-03-19 04:33:26 +0000477 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100478 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100479 else if (property == config->prop_mode_id) {
480 struct drm_property_blob *mode =
481 drm_property_lookup_blob(dev, val);
482 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Thierry Reding6472e502017-02-28 15:46:42 +0100483 drm_property_blob_put(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100484 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000485 } else if (property == config->degamma_lut_property) {
486 ret = drm_atomic_replace_property_blob_from_id(crtc,
487 &state->degamma_lut,
488 val,
489 -1,
490 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200491 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000492 return ret;
493 } else if (property == config->ctm_property) {
494 ret = drm_atomic_replace_property_blob_from_id(crtc,
495 &state->ctm,
496 val,
497 sizeof(struct drm_color_ctm),
498 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200499 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000500 return ret;
501 } else if (property == config->gamma_lut_property) {
502 ret = drm_atomic_replace_property_blob_from_id(crtc,
503 &state->gamma_lut,
504 val,
505 -1,
506 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200507 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000508 return ret;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900509 } else if (property == config->prop_out_fence_ptr) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200510 s32 __user *fence_ptr = u64_to_user_ptr(val);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900511
512 if (!fence_ptr)
513 return 0;
514
515 if (put_user(-1, fence_ptr))
516 return -EFAULT;
517
518 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000519 } else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500520 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000521 else
522 return -EINVAL;
523
524 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500525}
526EXPORT_SYMBOL(drm_atomic_crtc_set_property);
527
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100528/**
529 * drm_atomic_crtc_get_property - get property value from CRTC state
530 * @crtc: the drm CRTC to set a property on
531 * @state: the state object to get the property value from
532 * @property: the property to set
533 * @val: return location for the property value
534 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100535 * This function handles generic/core properties and calls out to driver's
536 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
537 * consistent behavior you must call this function rather than the driver hook
538 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100539 *
540 * RETURNS:
541 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500542 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700543static int
544drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500545 const struct drm_crtc_state *state,
546 struct drm_property *property, uint64_t *val)
547{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000548 struct drm_device *dev = crtc->dev;
549 struct drm_mode_config *config = &dev->mode_config;
550
551 if (property == config->prop_active)
552 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100553 else if (property == config->prop_mode_id)
554 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000555 else if (property == config->degamma_lut_property)
556 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
557 else if (property == config->ctm_property)
558 *val = (state->ctm) ? state->ctm->base.id : 0;
559 else if (property == config->gamma_lut_property)
560 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900561 else if (property == config->prop_out_fence_ptr)
562 *val = 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000563 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500564 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000565 else
566 return -EINVAL;
567
568 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500569}
Rob Clarkac9c9252014-12-18 16:01:47 -0500570
571/**
Rob Clark5e743732014-12-18 16:01:51 -0500572 * drm_atomic_crtc_check - check crtc state
573 * @crtc: crtc to check
574 * @state: crtc state to check
575 *
576 * Provides core sanity checks for crtc state.
577 *
578 * RETURNS:
579 * Zero on success, error code on failure
580 */
581static int drm_atomic_crtc_check(struct drm_crtc *crtc,
582 struct drm_crtc_state *state)
583{
584 /* NOTE: we explicitly don't enforce constraints such as primary
585 * layer covering entire screen, since that is something we want
586 * to allow (on hw that supports it). For hw that does not, it
587 * should be checked in driver's crtc->atomic_check() vfunc.
588 *
589 * TODO: Add generic modeset state checks once we support those.
590 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100591
592 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200593 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
594 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100595 return -EINVAL;
596 }
597
Daniel Stone99cf4a22015-05-25 19:11:51 +0100598 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
599 * as this is a kernel-internal detail that userspace should never
600 * be able to trigger. */
601 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
602 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200603 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
604 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100605 return -EINVAL;
606 }
607
608 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
609 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200610 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
611 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100612 return -EINVAL;
613 }
614
Daniel Vetter4cba6852015-12-08 09:49:20 +0100615 /*
616 * Reject event generation for when a CRTC is off and stays off.
617 * It wouldn't be hard to implement this, but userspace has a track
618 * record of happily burning through 100% cpu (or worse, crash) when the
619 * display pipe is suspended. To avoid all that fun just reject updates
620 * that ask for events since likely that indicates a bug in the
621 * compositor's drawing loop. This is consistent with the vblank IOCTL
622 * and legacy page_flip IOCTL which also reject service on a disabled
623 * pipe.
624 */
625 if (state->event && !state->active && !crtc->state->active) {
Russell King6ac7c542017-02-13 12:27:03 +0000626 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
627 crtc->base.id, crtc->name);
Daniel Vetter4cba6852015-12-08 09:49:20 +0100628 return -EINVAL;
629 }
630
Rob Clark5e743732014-12-18 16:01:51 -0500631 return 0;
632}
633
Rob Clarkfceffb322016-11-05 11:08:09 -0400634static void drm_atomic_crtc_print_state(struct drm_printer *p,
635 const struct drm_crtc_state *state)
636{
637 struct drm_crtc *crtc = state->crtc;
638
639 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
640 drm_printf(p, "\tenable=%d\n", state->enable);
641 drm_printf(p, "\tactive=%d\n", state->active);
642 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
643 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
644 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
645 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
646 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
647 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
648 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
649 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
650 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
651
652 if (crtc->funcs->atomic_print_state)
653 crtc->funcs->atomic_print_state(p, state);
654}
655
Rob Clark5e743732014-12-18 16:01:51 -0500656/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200657 * drm_atomic_get_plane_state - get plane state
658 * @state: global atomic state object
659 * @plane: plane to get state object for
660 *
661 * This function returns the plane state for the given plane, allocating it if
662 * needed. It will also grab the relevant plane lock to make sure that the state
663 * is consistent.
664 *
665 * Returns:
666 *
667 * Either the allocated state or the error code encoded into the pointer. When
668 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
669 * entire atomic sequence must be restarted. All other errors are fatal.
670 */
671struct drm_plane_state *
672drm_atomic_get_plane_state(struct drm_atomic_state *state,
673 struct drm_plane *plane)
674{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200675 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200676 struct drm_plane_state *plane_state;
677
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200678 WARN_ON(!state->acquire_ctx);
679
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200680 plane_state = drm_atomic_get_existing_plane_state(state, plane);
681 if (plane_state)
682 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200683
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100684 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200685 if (ret)
686 return ERR_PTR(ret);
687
688 plane_state = plane->funcs->atomic_duplicate_state(plane);
689 if (!plane_state)
690 return ERR_PTR(-ENOMEM);
691
Daniel Vetterb8b53422016-06-02 00:06:33 +0200692 state->planes[index].state = plane_state;
693 state->planes[index].ptr = plane;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100694 state->planes[index].old_state = plane->state;
695 state->planes[index].new_state = plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200696 plane_state->state = state;
697
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200698 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
699 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200700
701 if (plane_state->crtc) {
702 struct drm_crtc_state *crtc_state;
703
704 crtc_state = drm_atomic_get_crtc_state(state,
705 plane_state->crtc);
706 if (IS_ERR(crtc_state))
707 return ERR_CAST(crtc_state);
708 }
709
710 return plane_state;
711}
712EXPORT_SYMBOL(drm_atomic_get_plane_state);
713
714/**
Rob Clark40ecc692014-12-18 16:01:46 -0500715 * drm_atomic_plane_set_property - set property on plane
716 * @plane: the drm plane to set a property on
717 * @state: the state object to update with the new property value
718 * @property: the property to set
719 * @val: the new property value
720 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100721 * This function handles generic/core properties and calls out to driver's
722 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
723 * consistent behavior you must call this function rather than the driver hook
724 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500725 *
726 * RETURNS:
727 * Zero on success, error code on failure
728 */
729int drm_atomic_plane_set_property(struct drm_plane *plane,
730 struct drm_plane_state *state, struct drm_property *property,
731 uint64_t val)
732{
Rob Clark6b4959f2014-12-18 16:01:53 -0500733 struct drm_device *dev = plane->dev;
734 struct drm_mode_config *config = &dev->mode_config;
735
736 if (property == config->prop_fb_id) {
737 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
738 drm_atomic_set_fb_for_plane(state, fb);
739 if (fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +0100740 drm_framebuffer_put(fb);
Gustavo Padovan96260142016-11-15 22:06:39 +0900741 } else if (property == config->prop_in_fence_fd) {
742 if (state->fence)
743 return -EINVAL;
744
745 if (U642I64(val) == -1)
746 return 0;
747
748 state->fence = sync_file_get_fence(val);
749 if (!state->fence)
750 return -EINVAL;
751
Rob Clark6b4959f2014-12-18 16:01:53 -0500752 } else if (property == config->prop_crtc_id) {
753 struct drm_crtc *crtc = drm_crtc_find(dev, val);
754 return drm_atomic_set_crtc_for_plane(state, crtc);
755 } else if (property == config->prop_crtc_x) {
756 state->crtc_x = U642I64(val);
757 } else if (property == config->prop_crtc_y) {
758 state->crtc_y = U642I64(val);
759 } else if (property == config->prop_crtc_w) {
760 state->crtc_w = val;
761 } else if (property == config->prop_crtc_h) {
762 state->crtc_h = val;
763 } else if (property == config->prop_src_x) {
764 state->src_x = val;
765 } else if (property == config->prop_src_y) {
766 state->src_y = val;
767 } else if (property == config->prop_src_w) {
768 state->src_w = val;
769 } else if (property == config->prop_src_h) {
770 state->src_h = val;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300771 } else if (property == plane->rotation_property) {
Ville Syrjälä6e0c7c32016-09-26 19:30:47 +0300772 if (!is_power_of_2(val & DRM_ROTATE_MASK))
773 return -EINVAL;
Matt Roper1da30622015-01-21 16:35:40 -0800774 state->rotation = val;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200775 } else if (property == plane->zpos_property) {
776 state->zpos = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500777 } else if (plane->funcs->atomic_set_property) {
778 return plane->funcs->atomic_set_property(plane, state,
779 property, val);
780 } else {
781 return -EINVAL;
782 }
783
784 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500785}
786EXPORT_SYMBOL(drm_atomic_plane_set_property);
787
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100788/**
789 * drm_atomic_plane_get_property - get property value from plane state
790 * @plane: the drm plane to set a property on
791 * @state: the state object to get the property value from
792 * @property: the property to set
793 * @val: return location for the property value
794 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100795 * This function handles generic/core properties and calls out to driver's
796 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
797 * consistent behavior you must call this function rather than the driver hook
798 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100799 *
800 * RETURNS:
801 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500802 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100803static int
804drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500805 const struct drm_plane_state *state,
806 struct drm_property *property, uint64_t *val)
807{
Rob Clark6b4959f2014-12-18 16:01:53 -0500808 struct drm_device *dev = plane->dev;
809 struct drm_mode_config *config = &dev->mode_config;
810
811 if (property == config->prop_fb_id) {
812 *val = (state->fb) ? state->fb->base.id : 0;
Gustavo Padovan96260142016-11-15 22:06:39 +0900813 } else if (property == config->prop_in_fence_fd) {
814 *val = -1;
Rob Clark6b4959f2014-12-18 16:01:53 -0500815 } else if (property == config->prop_crtc_id) {
816 *val = (state->crtc) ? state->crtc->base.id : 0;
817 } else if (property == config->prop_crtc_x) {
818 *val = I642U64(state->crtc_x);
819 } else if (property == config->prop_crtc_y) {
820 *val = I642U64(state->crtc_y);
821 } else if (property == config->prop_crtc_w) {
822 *val = state->crtc_w;
823 } else if (property == config->prop_crtc_h) {
824 *val = state->crtc_h;
825 } else if (property == config->prop_src_x) {
826 *val = state->src_x;
827 } else if (property == config->prop_src_y) {
828 *val = state->src_y;
829 } else if (property == config->prop_src_w) {
830 *val = state->src_w;
831 } else if (property == config->prop_src_h) {
832 *val = state->src_h;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300833 } else if (property == plane->rotation_property) {
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000834 *val = state->rotation;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200835 } else if (property == plane->zpos_property) {
836 *val = state->zpos;
Rob Clark6b4959f2014-12-18 16:01:53 -0500837 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500838 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500839 } else {
840 return -EINVAL;
841 }
842
843 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500844}
Rob Clarkac9c9252014-12-18 16:01:47 -0500845
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200846static bool
847plane_switching_crtc(struct drm_atomic_state *state,
848 struct drm_plane *plane,
849 struct drm_plane_state *plane_state)
850{
851 if (!plane->state->crtc || !plane_state->crtc)
852 return false;
853
854 if (plane->state->crtc == plane_state->crtc)
855 return false;
856
857 /* This could be refined, but currently there's no helper or driver code
858 * to implement direct switching of active planes nor userspace to take
859 * advantage of more direct plane switching without the intermediate
860 * full OFF state.
861 */
862 return true;
863}
864
Rob Clarkac9c9252014-12-18 16:01:47 -0500865/**
Rob Clark5e743732014-12-18 16:01:51 -0500866 * drm_atomic_plane_check - check plane state
867 * @plane: plane to check
868 * @state: plane state to check
869 *
870 * Provides core sanity checks for plane state.
871 *
872 * RETURNS:
873 * Zero on success, error code on failure
874 */
875static int drm_atomic_plane_check(struct drm_plane *plane,
876 struct drm_plane_state *state)
877{
878 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200879 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500880
881 /* either *both* CRTC and FB must be set, or neither */
882 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100883 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500884 return -EINVAL;
885 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100886 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500887 return -EINVAL;
888 }
889
890 /* if disabled, we don't care about the rest of the state: */
891 if (!state->crtc)
892 return 0;
893
894 /* Check whether this plane is usable on this CRTC */
895 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100896 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500897 return -EINVAL;
898 }
899
900 /* Check whether this plane supports the fb pixel format. */
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200901 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
Laurent Pinchartead86102015-03-05 02:25:43 +0200902 if (ret) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000903 struct drm_format_name_buf format_name;
904 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200905 drm_get_format_name(state->fb->format->format,
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000906 &format_name));
Laurent Pinchartead86102015-03-05 02:25:43 +0200907 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500908 }
909
910 /* Give drivers some help against integer overflows */
911 if (state->crtc_w > INT_MAX ||
912 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
913 state->crtc_h > INT_MAX ||
914 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100915 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
916 state->crtc_w, state->crtc_h,
917 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500918 return -ERANGE;
919 }
920
921 fb_width = state->fb->width << 16;
922 fb_height = state->fb->height << 16;
923
924 /* Make sure source coordinates are inside the fb. */
925 if (state->src_w > fb_width ||
926 state->src_x > fb_width - state->src_w ||
927 state->src_h > fb_height ||
928 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100929 DRM_DEBUG_ATOMIC("Invalid source coordinates "
930 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
931 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
932 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
933 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
934 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500935 return -ENOSPC;
936 }
937
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200938 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200939 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
940 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200941 return -EINVAL;
942 }
943
Rob Clark5e743732014-12-18 16:01:51 -0500944 return 0;
945}
946
Rob Clarkfceffb322016-11-05 11:08:09 -0400947static void drm_atomic_plane_print_state(struct drm_printer *p,
948 const struct drm_plane_state *state)
949{
950 struct drm_plane *plane = state->plane;
951 struct drm_rect src = drm_plane_state_src(state);
952 struct drm_rect dest = drm_plane_state_dest(state);
953
954 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
955 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
956 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
957 if (state->fb) {
958 struct drm_framebuffer *fb = state->fb;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200959 int i, n = fb->format->num_planes;
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000960 struct drm_format_name_buf format_name;
Rob Clarkfceffb322016-11-05 11:08:09 -0400961
962 drm_printf(p, "\t\tformat=%s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200963 drm_get_format_name(fb->format->format, &format_name));
Ville Syrjäläbae781b2016-11-16 13:33:16 +0200964 drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
Rob Clarkfceffb322016-11-05 11:08:09 -0400965 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
966 drm_printf(p, "\t\tlayers:\n");
967 for (i = 0; i < n; i++) {
968 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
969 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
Rob Clarkfceffb322016-11-05 11:08:09 -0400970 }
971 }
972 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
973 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
974 drm_printf(p, "\trotation=%x\n", state->rotation);
975
976 if (plane->funcs->atomic_print_state)
977 plane->funcs->atomic_print_state(p, state);
978}
979
Rob Clark5e743732014-12-18 16:01:51 -0500980/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200981 * drm_atomic_get_connector_state - get connector state
982 * @state: global atomic state object
983 * @connector: connector to get state object for
984 *
985 * This function returns the connector state for the given connector,
986 * allocating it if needed. It will also grab the relevant connector lock to
987 * make sure that the state is consistent.
988 *
989 * Returns:
990 *
991 * Either the allocated state or the error code encoded into the pointer. When
992 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
993 * entire atomic sequence must be restarted. All other errors are fatal.
994 */
995struct drm_connector_state *
996drm_atomic_get_connector_state(struct drm_atomic_state *state,
997 struct drm_connector *connector)
998{
999 int ret, index;
1000 struct drm_mode_config *config = &connector->dev->mode_config;
1001 struct drm_connector_state *connector_state;
1002
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +02001003 WARN_ON(!state->acquire_ctx);
1004
Daniel Vetterc7eb76f2014-11-19 18:38:06 +01001005 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1006 if (ret)
1007 return ERR_PTR(ret);
1008
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001009 index = drm_connector_index(connector);
1010
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001011 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +02001012 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001013 int alloc = max(index + 1, config->num_connector);
1014
1015 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1016 if (!c)
1017 return ERR_PTR(-ENOMEM);
1018
1019 state->connectors = c;
1020 memset(&state->connectors[state->num_connector], 0,
1021 sizeof(*state->connectors) * (alloc - state->num_connector));
1022
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001023 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001024 }
1025
Daniel Vetter63e83c12016-06-02 00:06:32 +02001026 if (state->connectors[index].state)
1027 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001028
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001029 connector_state = connector->funcs->atomic_duplicate_state(connector);
1030 if (!connector_state)
1031 return ERR_PTR(-ENOMEM);
1032
Thierry Redingad093602017-02-28 15:46:39 +01001033 drm_connector_get(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +02001034 state->connectors[index].state = connector_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01001035 state->connectors[index].old_state = connector->state;
1036 state->connectors[index].new_state = connector_state;
Daniel Vetter63e83c12016-06-02 00:06:32 +02001037 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001038 connector_state->state = state;
1039
Russell King6ac7c542017-02-13 12:27:03 +00001040 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1041 connector->base.id, connector->name,
1042 connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001043
1044 if (connector_state->crtc) {
1045 struct drm_crtc_state *crtc_state;
1046
1047 crtc_state = drm_atomic_get_crtc_state(state,
1048 connector_state->crtc);
1049 if (IS_ERR(crtc_state))
1050 return ERR_CAST(crtc_state);
1051 }
1052
1053 return connector_state;
1054}
1055EXPORT_SYMBOL(drm_atomic_get_connector_state);
1056
1057/**
Rob Clark40ecc692014-12-18 16:01:46 -05001058 * drm_atomic_connector_set_property - set property on connector.
1059 * @connector: the drm connector to set a property on
1060 * @state: the state object to update with the new property value
1061 * @property: the property to set
1062 * @val: the new property value
1063 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001064 * This function handles generic/core properties and calls out to driver's
1065 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1066 * consistent behavior you must call this function rather than the driver hook
1067 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -05001068 *
1069 * RETURNS:
1070 * Zero on success, error code on failure
1071 */
1072int drm_atomic_connector_set_property(struct drm_connector *connector,
1073 struct drm_connector_state *state, struct drm_property *property,
1074 uint64_t val)
1075{
1076 struct drm_device *dev = connector->dev;
1077 struct drm_mode_config *config = &dev->mode_config;
1078
Rob Clarkae16c592014-12-18 16:01:54 -05001079 if (property == config->prop_crtc_id) {
1080 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1081 return drm_atomic_set_crtc_for_connector(state, crtc);
1082 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -05001083 /* setting DPMS property requires special handling, which
1084 * is done in legacy setprop path for us. Disallow (for
1085 * now?) atomic writes to DPMS property:
1086 */
1087 return -EINVAL;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001088 } else if (property == config->tv_select_subconnector_property) {
1089 state->tv.subconnector = val;
1090 } else if (property == config->tv_left_margin_property) {
1091 state->tv.margins.left = val;
1092 } else if (property == config->tv_right_margin_property) {
1093 state->tv.margins.right = val;
1094 } else if (property == config->tv_top_margin_property) {
1095 state->tv.margins.top = val;
1096 } else if (property == config->tv_bottom_margin_property) {
1097 state->tv.margins.bottom = val;
1098 } else if (property == config->tv_mode_property) {
1099 state->tv.mode = val;
1100 } else if (property == config->tv_brightness_property) {
1101 state->tv.brightness = val;
1102 } else if (property == config->tv_contrast_property) {
1103 state->tv.contrast = val;
1104 } else if (property == config->tv_flicker_reduction_property) {
1105 state->tv.flicker_reduction = val;
1106 } else if (property == config->tv_overscan_property) {
1107 state->tv.overscan = val;
1108 } else if (property == config->tv_saturation_property) {
1109 state->tv.saturation = val;
1110 } else if (property == config->tv_hue_property) {
1111 state->tv.hue = val;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001112 } else if (property == config->link_status_property) {
1113 /* Never downgrade from GOOD to BAD on userspace's request here,
1114 * only hw issues can do that.
1115 *
1116 * For an atomic property the userspace doesn't need to be able
1117 * to understand all the properties, but needs to be able to
1118 * restore the state it wants on VT switch. So if the userspace
1119 * tries to change the link_status from GOOD to BAD, driver
1120 * silently rejects it and returns a 0. This prevents userspace
1121 * from accidently breaking the display when it restores the
1122 * state.
1123 */
1124 if (state->link_status != DRM_LINK_STATUS_GOOD)
1125 state->link_status = val;
Rob Clark40ecc692014-12-18 16:01:46 -05001126 } else if (connector->funcs->atomic_set_property) {
1127 return connector->funcs->atomic_set_property(connector,
1128 state, property, val);
1129 } else {
1130 return -EINVAL;
1131 }
Boris Brezillon299a16b2016-12-02 14:48:09 +01001132
1133 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -05001134}
1135EXPORT_SYMBOL(drm_atomic_connector_set_property);
1136
Rob Clarkfceffb322016-11-05 11:08:09 -04001137static void drm_atomic_connector_print_state(struct drm_printer *p,
1138 const struct drm_connector_state *state)
1139{
1140 struct drm_connector *connector = state->connector;
1141
1142 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1143 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1144
1145 if (connector->funcs->atomic_print_state)
1146 connector->funcs->atomic_print_state(p, state);
1147}
1148
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001149/**
1150 * drm_atomic_connector_get_property - get property value from connector state
1151 * @connector: the drm connector to set a property on
1152 * @state: the state object to get the property value from
1153 * @property: the property to set
1154 * @val: return location for the property value
1155 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001156 * This function handles generic/core properties and calls out to driver's
1157 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1158 * consistent behavior you must call this function rather than the driver hook
1159 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001160 *
1161 * RETURNS:
1162 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -05001163 */
Daniel Vettera97df1c2014-12-18 22:49:02 +01001164static int
1165drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -05001166 const struct drm_connector_state *state,
1167 struct drm_property *property, uint64_t *val)
1168{
1169 struct drm_device *dev = connector->dev;
1170 struct drm_mode_config *config = &dev->mode_config;
1171
Rob Clarkae16c592014-12-18 16:01:54 -05001172 if (property == config->prop_crtc_id) {
1173 *val = (state->crtc) ? state->crtc->base.id : 0;
1174 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -05001175 *val = connector->dpms;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001176 } else if (property == config->tv_select_subconnector_property) {
1177 *val = state->tv.subconnector;
1178 } else if (property == config->tv_left_margin_property) {
1179 *val = state->tv.margins.left;
1180 } else if (property == config->tv_right_margin_property) {
1181 *val = state->tv.margins.right;
1182 } else if (property == config->tv_top_margin_property) {
1183 *val = state->tv.margins.top;
1184 } else if (property == config->tv_bottom_margin_property) {
1185 *val = state->tv.margins.bottom;
1186 } else if (property == config->tv_mode_property) {
1187 *val = state->tv.mode;
1188 } else if (property == config->tv_brightness_property) {
1189 *val = state->tv.brightness;
1190 } else if (property == config->tv_contrast_property) {
1191 *val = state->tv.contrast;
1192 } else if (property == config->tv_flicker_reduction_property) {
1193 *val = state->tv.flicker_reduction;
1194 } else if (property == config->tv_overscan_property) {
1195 *val = state->tv.overscan;
1196 } else if (property == config->tv_saturation_property) {
1197 *val = state->tv.saturation;
1198 } else if (property == config->tv_hue_property) {
1199 *val = state->tv.hue;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001200 } else if (property == config->link_status_property) {
1201 *val = state->link_status;
Rob Clarkac9c9252014-12-18 16:01:47 -05001202 } else if (connector->funcs->atomic_get_property) {
1203 return connector->funcs->atomic_get_property(connector,
1204 state, property, val);
1205 } else {
1206 return -EINVAL;
1207 }
1208
1209 return 0;
1210}
Rob Clarkac9c9252014-12-18 16:01:47 -05001211
Rob Clark88a48e22014-12-18 16:01:50 -05001212int drm_atomic_get_property(struct drm_mode_object *obj,
1213 struct drm_property *property, uint64_t *val)
1214{
1215 struct drm_device *dev = property->dev;
1216 int ret;
1217
1218 switch (obj->type) {
1219 case DRM_MODE_OBJECT_CONNECTOR: {
1220 struct drm_connector *connector = obj_to_connector(obj);
1221 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1222 ret = drm_atomic_connector_get_property(connector,
1223 connector->state, property, val);
1224 break;
1225 }
1226 case DRM_MODE_OBJECT_CRTC: {
1227 struct drm_crtc *crtc = obj_to_crtc(obj);
1228 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1229 ret = drm_atomic_crtc_get_property(crtc,
1230 crtc->state, property, val);
1231 break;
1232 }
1233 case DRM_MODE_OBJECT_PLANE: {
1234 struct drm_plane *plane = obj_to_plane(obj);
1235 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1236 ret = drm_atomic_plane_get_property(plane,
1237 plane->state, property, val);
1238 break;
1239 }
1240 default:
1241 ret = -EINVAL;
1242 break;
1243 }
1244
1245 return ret;
1246}
1247
1248/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001249 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001250 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001251 * @crtc: crtc to use for the plane
1252 *
1253 * Changing the assigned crtc for a plane requires us to grab the lock and state
1254 * for the new crtc, as needed. This function takes care of all these details
1255 * besides updating the pointer in the state object itself.
1256 *
1257 * Returns:
1258 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1259 * then the w/w mutex code has detected a deadlock and the entire atomic
1260 * sequence must be restarted. All other errors are fatal.
1261 */
1262int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001263drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1264 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001265{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001266 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001267 struct drm_crtc_state *crtc_state;
1268
Rob Clark6ddd3882014-11-21 15:28:31 -05001269 if (plane_state->crtc) {
1270 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1271 plane_state->crtc);
1272 if (WARN_ON(IS_ERR(crtc_state)))
1273 return PTR_ERR(crtc_state);
1274
1275 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1276 }
1277
1278 plane_state->crtc = crtc;
1279
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001280 if (crtc) {
1281 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1282 crtc);
1283 if (IS_ERR(crtc_state))
1284 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -05001285 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001286 }
1287
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001288 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001289 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1290 plane_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001291 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001292 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1293 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001294
1295 return 0;
1296}
1297EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1298
1299/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001300 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001301 * @plane_state: atomic state object for the plane
1302 * @fb: fb to use for the plane
1303 *
1304 * Changing the assigned framebuffer for a plane requires us to grab a reference
1305 * to the new fb and drop the reference to the old fb, if there is one. This
1306 * function takes care of all these details besides updating the pointer in the
1307 * state object itself.
1308 */
1309void
1310drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1311 struct drm_framebuffer *fb)
1312{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001313 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001314 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1315 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001316 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001317 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1318 plane_state);
Chris Wilson389f78b2016-11-25 15:32:30 +00001319
1320 drm_framebuffer_assign(&plane_state->fb, fb);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001321}
1322EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1323
1324/**
Gustavo Padovan13b55662016-11-07 19:03:30 +09001325 * drm_atomic_set_fence_for_plane - set fence for plane
1326 * @plane_state: atomic state object for the plane
1327 * @fence: dma_fence to use for the plane
1328 *
1329 * Helper to setup the plane_state fence in case it is not set yet.
1330 * By using this drivers doesn't need to worry if the user choose
1331 * implicit or explicit fencing.
1332 *
1333 * This function will not set the fence to the state if it was set
Daniel Vetterd5745282017-01-25 07:26:45 +01001334 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1335 * drop the reference to the fence as we are not storing it anywhere.
1336 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1337 * with the received implicit fence. In both cases this function consumes a
1338 * reference for @fence.
Gustavo Padovan13b55662016-11-07 19:03:30 +09001339 */
1340void
1341drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1342 struct dma_fence *fence)
1343{
1344 if (plane_state->fence) {
1345 dma_fence_put(fence);
1346 return;
1347 }
1348
1349 plane_state->fence = fence;
1350}
1351EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1352
1353/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001354 * drm_atomic_set_crtc_for_connector - set crtc for connector
1355 * @conn_state: atomic state object for the connector
1356 * @crtc: crtc to use for the connector
1357 *
1358 * Changing the assigned crtc for a connector requires us to grab the lock and
1359 * state for the new crtc, as needed. This function takes care of all these
1360 * details besides updating the pointer in the state object itself.
1361 *
1362 * Returns:
1363 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1364 * then the w/w mutex code has detected a deadlock and the entire atomic
1365 * sequence must be restarted. All other errors are fatal.
1366 */
1367int
1368drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1369 struct drm_crtc *crtc)
1370{
1371 struct drm_crtc_state *crtc_state;
1372
Chris Wilsone2d800a2016-05-06 12:47:45 +01001373 if (conn_state->crtc == crtc)
1374 return 0;
1375
1376 if (conn_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001377 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1378 conn_state->crtc);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001379
1380 crtc_state->connector_mask &=
1381 ~(1 << drm_connector_index(conn_state->connector));
Chris Wilsone2d800a2016-05-06 12:47:45 +01001382
Thierry Redingad093602017-02-28 15:46:39 +01001383 drm_connector_put(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001384 conn_state->crtc = NULL;
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001385 }
1386
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001387 if (crtc) {
1388 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1389 if (IS_ERR(crtc_state))
1390 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001391
1392 crtc_state->connector_mask |=
1393 1 << drm_connector_index(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001394
Thierry Redingad093602017-02-28 15:46:39 +01001395 drm_connector_get(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001396 conn_state->crtc = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001397
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001398 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1399 conn_state, crtc->base.id, crtc->name);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001400 } else {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001401 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1402 conn_state);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001403 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001404
1405 return 0;
1406}
1407EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1408
1409/**
1410 * drm_atomic_add_affected_connectors - add connectors for crtc
1411 * @state: atomic state
1412 * @crtc: DRM crtc
1413 *
1414 * This function walks the current configuration and adds all connectors
1415 * currently using @crtc to the atomic configuration @state. Note that this
1416 * function must acquire the connection mutex. This can potentially cause
1417 * unneeded seralization if the update is just for the planes on one crtc. Hence
1418 * drivers and helpers should only call this when really needed (e.g. when a
1419 * full modeset needs to happen due to some change).
1420 *
1421 * Returns:
1422 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1423 * then the w/w mutex code has detected a deadlock and the entire atomic
1424 * sequence must be restarted. All other errors are fatal.
1425 */
1426int
1427drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1428 struct drm_crtc *crtc)
1429{
1430 struct drm_mode_config *config = &state->dev->mode_config;
1431 struct drm_connector *connector;
1432 struct drm_connector_state *conn_state;
Daniel Vetter613051d2016-12-14 00:08:06 +01001433 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001434 struct drm_crtc_state *crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001435 int ret;
1436
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001437 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1438 if (IS_ERR(crtc_state))
1439 return PTR_ERR(crtc_state);
1440
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001441 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1442 if (ret)
1443 return ret;
1444
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001445 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1446 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001447
1448 /*
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001449 * Changed connectors are already in @state, so only need to look
1450 * at the connector_mask in crtc_state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001451 */
Thierry Redingb982dab2017-02-28 15:46:43 +01001452 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +01001453 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001454 if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001455 continue;
1456
1457 conn_state = drm_atomic_get_connector_state(state, connector);
Daniel Vetter613051d2016-12-14 00:08:06 +01001458 if (IS_ERR(conn_state)) {
Thierry Redingb982dab2017-02-28 15:46:43 +01001459 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001460 return PTR_ERR(conn_state);
Daniel Vetter613051d2016-12-14 00:08:06 +01001461 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001462 }
Thierry Redingb982dab2017-02-28 15:46:43 +01001463 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001464
1465 return 0;
1466}
1467EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1468
1469/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001470 * drm_atomic_add_affected_planes - add planes for crtc
1471 * @state: atomic state
1472 * @crtc: DRM crtc
1473 *
1474 * This function walks the current configuration and adds all planes
1475 * currently used by @crtc to the atomic configuration @state. This is useful
1476 * when an atomic commit also needs to check all currently enabled plane on
1477 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1478 * to avoid special code to force-enable all planes.
1479 *
1480 * Since acquiring a plane state will always also acquire the w/w mutex of the
1481 * current CRTC for that plane (if there is any) adding all the plane states for
1482 * a CRTC will not reduce parallism of atomic updates.
1483 *
1484 * Returns:
1485 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1486 * then the w/w mutex code has detected a deadlock and the entire atomic
1487 * sequence must be restarted. All other errors are fatal.
1488 */
1489int
1490drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1491 struct drm_crtc *crtc)
1492{
1493 struct drm_plane *plane;
1494
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001495 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001496
1497 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1498 struct drm_plane_state *plane_state =
1499 drm_atomic_get_plane_state(state, plane);
1500
1501 if (IS_ERR(plane_state))
1502 return PTR_ERR(plane_state);
1503 }
1504 return 0;
1505}
1506EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1507
1508/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001509 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1510 * @state: atomic state
1511 *
1512 * This function should be used by legacy entry points which don't understand
1513 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +08001514 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001515 */
1516void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1517{
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001518 struct drm_device *dev = state->dev;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001519 int ret;
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001520 bool global = false;
1521
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001522 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1523 global = true;
1524
1525 dev->mode_config.acquire_ctx = NULL;
1526 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001527
1528retry:
1529 drm_modeset_backoff(state->acquire_ctx);
1530
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001531 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001532 if (ret)
1533 goto retry;
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001534
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001535 if (global)
1536 dev->mode_config.acquire_ctx = state->acquire_ctx;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001537}
1538EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1539
1540/**
1541 * drm_atomic_check_only - check whether a given config would work
1542 * @state: atomic configuration to check
1543 *
1544 * Note that this function can return -EDEADLK if the driver needed to acquire
1545 * more locks but encountered a deadlock. The caller must then do the usual w/w
1546 * backoff dance and restart. All other errors are fatal.
1547 *
1548 * Returns:
1549 * 0 on success, negative error code on failure.
1550 */
1551int drm_atomic_check_only(struct drm_atomic_state *state)
1552{
Rob Clark5e743732014-12-18 16:01:51 -05001553 struct drm_device *dev = state->dev;
1554 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001555 struct drm_plane *plane;
1556 struct drm_plane_state *plane_state;
1557 struct drm_crtc *crtc;
1558 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001559 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001560
Daniel Vetter17a38d92015-02-22 12:24:16 +01001561 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001562
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001563 for_each_new_plane_in_state(state, plane, plane_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001564 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001565 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001566 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1567 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001568 return ret;
1569 }
1570 }
1571
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001572 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001573 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001574 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001575 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1576 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001577 return ret;
1578 }
1579 }
1580
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001581 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001582 ret = config->funcs->atomic_check(state->dev, state);
1583
Rob Clarkd34f20d2014-12-18 16:01:56 -05001584 if (!state->allow_modeset) {
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001585 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001586 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001587 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1588 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001589 return -EINVAL;
1590 }
1591 }
1592 }
1593
Rob Clark5e743732014-12-18 16:01:51 -05001594 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001595}
1596EXPORT_SYMBOL(drm_atomic_check_only);
1597
1598/**
1599 * drm_atomic_commit - commit configuration atomically
1600 * @state: atomic configuration to check
1601 *
1602 * Note that this function can return -EDEADLK if the driver needed to acquire
1603 * more locks but encountered a deadlock. The caller must then do the usual w/w
1604 * backoff dance and restart. All other errors are fatal.
1605 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001606 * This function will take its own reference on @state.
1607 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001608 *
1609 * Returns:
1610 * 0 on success, negative error code on failure.
1611 */
1612int drm_atomic_commit(struct drm_atomic_state *state)
1613{
1614 struct drm_mode_config *config = &state->dev->mode_config;
1615 int ret;
1616
1617 ret = drm_atomic_check_only(state);
1618 if (ret)
1619 return ret;
1620
Daniel Vetter17a38d92015-02-22 12:24:16 +01001621 DRM_DEBUG_ATOMIC("commiting %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001622
1623 return config->funcs->atomic_commit(state->dev, state, false);
1624}
1625EXPORT_SYMBOL(drm_atomic_commit);
1626
1627/**
Daniel Vetterd5745282017-01-25 07:26:45 +01001628 * drm_atomic_nonblocking_commit - atomic nonblocking commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001629 * @state: atomic configuration to check
1630 *
1631 * Note that this function can return -EDEADLK if the driver needed to acquire
1632 * more locks but encountered a deadlock. The caller must then do the usual w/w
1633 * backoff dance and restart. All other errors are fatal.
1634 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001635 * This function will take its own reference on @state.
1636 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001637 *
1638 * Returns:
1639 * 0 on success, negative error code on failure.
1640 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001641int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001642{
1643 struct drm_mode_config *config = &state->dev->mode_config;
1644 int ret;
1645
1646 ret = drm_atomic_check_only(state);
1647 if (ret)
1648 return ret;
1649
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001650 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001651
1652 return config->funcs->atomic_commit(state->dev, state, true);
1653}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001654EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001655
Rob Clarkfceffb322016-11-05 11:08:09 -04001656static void drm_atomic_print_state(const struct drm_atomic_state *state)
1657{
1658 struct drm_printer p = drm_info_printer(state->dev->dev);
1659 struct drm_plane *plane;
1660 struct drm_plane_state *plane_state;
1661 struct drm_crtc *crtc;
1662 struct drm_crtc_state *crtc_state;
1663 struct drm_connector *connector;
1664 struct drm_connector_state *connector_state;
1665 int i;
1666
1667 DRM_DEBUG_ATOMIC("checking %p\n", state);
1668
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001669 for_each_new_plane_in_state(state, plane, plane_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001670 drm_atomic_plane_print_state(&p, plane_state);
1671
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001672 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001673 drm_atomic_crtc_print_state(&p, crtc_state);
1674
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001675 for_each_new_connector_in_state(state, connector, connector_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001676 drm_atomic_connector_print_state(&p, connector_state);
1677}
1678
Daniel Vetterc2d85562017-04-03 10:32:54 +02001679static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1680 bool take_locks)
1681{
1682 struct drm_mode_config *config = &dev->mode_config;
1683 struct drm_plane *plane;
1684 struct drm_crtc *crtc;
1685 struct drm_connector *connector;
1686 struct drm_connector_list_iter conn_iter;
1687
1688 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1689 return;
1690
1691 list_for_each_entry(plane, &config->plane_list, head) {
1692 if (take_locks)
1693 drm_modeset_lock(&plane->mutex, NULL);
1694 drm_atomic_plane_print_state(p, plane->state);
1695 if (take_locks)
1696 drm_modeset_unlock(&plane->mutex);
1697 }
1698
1699 list_for_each_entry(crtc, &config->crtc_list, head) {
1700 if (take_locks)
1701 drm_modeset_lock(&crtc->mutex, NULL);
1702 drm_atomic_crtc_print_state(p, crtc->state);
1703 if (take_locks)
1704 drm_modeset_unlock(&crtc->mutex);
1705 }
1706
1707 drm_connector_list_iter_begin(dev, &conn_iter);
1708 if (take_locks)
1709 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1710 drm_for_each_connector_iter(connector, &conn_iter)
1711 drm_atomic_connector_print_state(p, connector->state);
1712 if (take_locks)
1713 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1714 drm_connector_list_iter_end(&conn_iter);
1715}
1716
Rob Clark6559c902016-11-05 11:08:10 -04001717/**
1718 * drm_state_dump - dump entire device atomic state
1719 * @dev: the drm device
1720 * @p: where to print the state to
1721 *
1722 * Just for debugging. Drivers might want an option to dump state
1723 * to dmesg in case of error irq's. (Hint, you probably want to
1724 * ratelimit this!)
1725 *
1726 * The caller must drm_modeset_lock_all(), or if this is called
1727 * from error irq handler, it should not be enabled by default.
1728 * (Ie. if you are debugging errors you might not care that this
1729 * is racey. But calling this without all modeset locks held is
1730 * not inherently safe.)
1731 */
1732void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1733{
Daniel Vetterc2d85562017-04-03 10:32:54 +02001734 __drm_state_dump(dev, p, false);
Rob Clark6559c902016-11-05 11:08:10 -04001735}
1736EXPORT_SYMBOL(drm_state_dump);
1737
1738#ifdef CONFIG_DEBUG_FS
1739static int drm_state_info(struct seq_file *m, void *data)
1740{
1741 struct drm_info_node *node = (struct drm_info_node *) m->private;
1742 struct drm_device *dev = node->minor->dev;
1743 struct drm_printer p = drm_seq_file_printer(m);
1744
Daniel Vetterc2d85562017-04-03 10:32:54 +02001745 __drm_state_dump(dev, &p, true);
Rob Clark6559c902016-11-05 11:08:10 -04001746
1747 return 0;
1748}
1749
1750/* any use in debugfs files to dump individual planes/crtc/etc? */
1751static const struct drm_info_list drm_atomic_debugfs_list[] = {
1752 {"state", drm_state_info, 0},
1753};
1754
1755int drm_atomic_debugfs_init(struct drm_minor *minor)
1756{
1757 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1758 ARRAY_SIZE(drm_atomic_debugfs_list),
1759 minor->debugfs_root, minor);
1760}
1761#endif
1762
Rob Clarkd34f20d2014-12-18 16:01:56 -05001763/*
1764 * The big monstor ioctl
1765 */
1766
1767static struct drm_pending_vblank_event *create_vblank_event(
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09001768 struct drm_device *dev, uint64_t user_data)
Rob Clarkd34f20d2014-12-18 16:01:56 -05001769{
1770 struct drm_pending_vblank_event *e = NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001771
1772 e = kzalloc(sizeof *e, GFP_KERNEL);
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001773 if (!e)
1774 return NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001775
1776 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001777 e->event.base.length = sizeof(e->event);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001778 e->event.user_data = user_data;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001779
Rob Clarkd34f20d2014-12-18 16:01:56 -05001780 return e;
1781}
1782
Rob Clarkd34f20d2014-12-18 16:01:56 -05001783static int atomic_set_prop(struct drm_atomic_state *state,
1784 struct drm_mode_object *obj, struct drm_property *prop,
1785 uint64_t prop_value)
1786{
1787 struct drm_mode_object *ref;
1788 int ret;
1789
1790 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1791 return -EINVAL;
1792
1793 switch (obj->type) {
1794 case DRM_MODE_OBJECT_CONNECTOR: {
1795 struct drm_connector *connector = obj_to_connector(obj);
1796 struct drm_connector_state *connector_state;
1797
1798 connector_state = drm_atomic_get_connector_state(state, connector);
1799 if (IS_ERR(connector_state)) {
1800 ret = PTR_ERR(connector_state);
1801 break;
1802 }
1803
1804 ret = drm_atomic_connector_set_property(connector,
1805 connector_state, prop, prop_value);
1806 break;
1807 }
1808 case DRM_MODE_OBJECT_CRTC: {
1809 struct drm_crtc *crtc = obj_to_crtc(obj);
1810 struct drm_crtc_state *crtc_state;
1811
1812 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1813 if (IS_ERR(crtc_state)) {
1814 ret = PTR_ERR(crtc_state);
1815 break;
1816 }
1817
1818 ret = drm_atomic_crtc_set_property(crtc,
1819 crtc_state, prop, prop_value);
1820 break;
1821 }
1822 case DRM_MODE_OBJECT_PLANE: {
1823 struct drm_plane *plane = obj_to_plane(obj);
1824 struct drm_plane_state *plane_state;
1825
1826 plane_state = drm_atomic_get_plane_state(state, plane);
1827 if (IS_ERR(plane_state)) {
1828 ret = PTR_ERR(plane_state);
1829 break;
1830 }
1831
1832 ret = drm_atomic_plane_set_property(plane,
1833 plane_state, prop, prop_value);
1834 break;
1835 }
1836 default:
1837 ret = -EINVAL;
1838 break;
1839 }
1840
1841 drm_property_change_valid_put(prop, ref);
1842 return ret;
1843}
1844
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001845/**
Maarten Lankhorst9744bf42015-11-24 10:34:34 +01001846 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001847 *
1848 * @dev: drm device to check.
1849 * @plane_mask: plane mask for planes that were updated.
1850 * @ret: return value, can be -EDEADLK for a retry.
1851 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001852 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1853 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1854 * is a common operation for each atomic update, so this call is split off as a
1855 * helper.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001856 */
1857void drm_atomic_clean_old_fb(struct drm_device *dev,
1858 unsigned plane_mask,
1859 int ret)
1860{
1861 struct drm_plane *plane;
1862
1863 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1864 * locks (ie. while it is still safe to deref plane->state). We
1865 * need to do this here because the driver entry points cannot
1866 * distinguish between legacy and atomic ioctls.
1867 */
1868 drm_for_each_plane_mask(plane, dev, plane_mask) {
1869 if (ret == 0) {
1870 struct drm_framebuffer *new_fb = plane->state->fb;
1871 if (new_fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01001872 drm_framebuffer_get(new_fb);
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001873 plane->fb = new_fb;
1874 plane->crtc = plane->state->crtc;
1875
1876 if (plane->old_fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01001877 drm_framebuffer_put(plane->old_fb);
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001878 }
1879 plane->old_fb = NULL;
1880 }
1881}
1882EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1883
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001884/**
1885 * DOC: explicit fencing properties
1886 *
1887 * Explicit fencing allows userspace to control the buffer synchronization
1888 * between devices. A Fence or a group of fences are transfered to/from
1889 * userspace using Sync File fds and there are two DRM properties for that.
1890 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1891 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1892 *
1893 * As a contrast, with implicit fencing the kernel keeps track of any
1894 * ongoing rendering, and automatically ensures that the atomic update waits
1895 * for any pending rendering to complete. For shared buffers represented with
Daniel Vetterd5745282017-01-25 07:26:45 +01001896 * a &struct dma_buf this is tracked in &struct reservation_object.
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001897 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1898 * whereas explicit fencing is what Android wants.
1899 *
1900 * "IN_FENCE_FD”:
1901 * Use this property to pass a fence that DRM should wait on before
1902 * proceeding with the Atomic Commit request and show the framebuffer for
1903 * the plane on the screen. The fence can be either a normal fence or a
1904 * merged one, the sync_file framework will handle both cases and use a
1905 * fence_array if a merged fence is received. Passing -1 here means no
1906 * fences to wait on.
1907 *
1908 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1909 * it will only check if the Sync File is a valid one.
1910 *
1911 * On the driver side the fence is stored on the @fence parameter of
Daniel Vetterea0dd852016-12-29 21:48:26 +01001912 * &struct drm_plane_state. Drivers which also support implicit fencing
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001913 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1914 * to make sure there's consistent behaviour between drivers in precedence
1915 * of implicit vs. explicit fencing.
1916 *
1917 * "OUT_FENCE_PTR”:
1918 * Use this property to pass a file descriptor pointer to DRM. Once the
1919 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1920 * the file descriptor number of a Sync File. This Sync File contains the
1921 * CRTC fence that will be signaled when all framebuffers present on the
1922 * Atomic Commit * request for that given CRTC are scanned out on the
1923 * screen.
1924 *
1925 * The Atomic Commit request fails if a invalid pointer is passed. If the
1926 * Atomic Commit request fails for any other reason the out fence fd
1927 * returned will be -1. On a Atomic Commit with the
1928 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1929 *
1930 * Note that out-fences don't have a special interface to drivers and are
Daniel Vetterea0dd852016-12-29 21:48:26 +01001931 * internally represented by a &struct drm_pending_vblank_event in struct
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001932 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1933 * helpers and for the DRM event handling for existing userspace.
1934 */
1935
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09001936struct drm_out_fence_state {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02001937 s32 __user *out_fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09001938 struct sync_file *sync_file;
1939 int fd;
1940};
1941
1942static int setup_out_fence(struct drm_out_fence_state *fence_state,
1943 struct dma_fence *fence)
1944{
1945 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1946 if (fence_state->fd < 0)
1947 return fence_state->fd;
1948
1949 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1950 return -EFAULT;
1951
1952 fence_state->sync_file = sync_file_create(fence);
1953 if (!fence_state->sync_file)
1954 return -ENOMEM;
1955
1956 return 0;
1957}
1958
1959static int prepare_crtc_signaling(struct drm_device *dev,
1960 struct drm_atomic_state *state,
1961 struct drm_mode_atomic *arg,
1962 struct drm_file *file_priv,
1963 struct drm_out_fence_state **fence_state,
1964 unsigned int *num_fences)
1965{
1966 struct drm_crtc *crtc;
1967 struct drm_crtc_state *crtc_state;
1968 int i, ret;
1969
1970 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1971 return 0;
1972
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001973 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02001974 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09001975
1976 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1977
1978 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1979 struct drm_pending_vblank_event *e;
1980
1981 e = create_vblank_event(dev, arg->user_data);
1982 if (!e)
1983 return -ENOMEM;
1984
1985 crtc_state->event = e;
1986 }
1987
1988 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1989 struct drm_pending_vblank_event *e = crtc_state->event;
1990
1991 if (!file_priv)
1992 continue;
1993
1994 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1995 &e->event.base);
1996 if (ret) {
1997 kfree(e);
1998 crtc_state->event = NULL;
1999 return ret;
2000 }
2001 }
2002
2003 if (fence_ptr) {
2004 struct dma_fence *fence;
2005 struct drm_out_fence_state *f;
2006
2007 f = krealloc(*fence_state, sizeof(**fence_state) *
2008 (*num_fences + 1), GFP_KERNEL);
2009 if (!f)
2010 return -ENOMEM;
2011
2012 memset(&f[*num_fences], 0, sizeof(*f));
2013
2014 f[*num_fences].out_fence_ptr = fence_ptr;
2015 *fence_state = f;
2016
Gustavo Padovan35f8cc32016-12-06 15:47:17 -02002017 fence = drm_crtc_create_fence(crtc);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002018 if (!fence)
2019 return -ENOMEM;
2020
2021 ret = setup_out_fence(&f[(*num_fences)++], fence);
2022 if (ret) {
2023 dma_fence_put(fence);
2024 return ret;
2025 }
2026
2027 crtc_state->event->base.fence = fence;
2028 }
2029 }
2030
2031 return 0;
2032}
2033
2034static void complete_crtc_signaling(struct drm_device *dev,
2035 struct drm_atomic_state *state,
2036 struct drm_out_fence_state *fence_state,
2037 unsigned int num_fences,
2038 bool install_fds)
2039{
2040 struct drm_crtc *crtc;
2041 struct drm_crtc_state *crtc_state;
2042 int i;
2043
2044 if (install_fds) {
2045 for (i = 0; i < num_fences; i++)
2046 fd_install(fence_state[i].fd,
2047 fence_state[i].sync_file->file);
2048
2049 kfree(fence_state);
2050 return;
2051 }
2052
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002053 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002054 struct drm_pending_vblank_event *event = crtc_state->event;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002055 /*
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002056 * Free the allocated event. drm_atomic_helper_setup_commit
2057 * can allocate an event too, so only free it if it's ours
2058 * to prevent a double free in drm_atomic_state_clear.
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002059 */
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002060 if (event && (event->base.fence || event->base.file_priv)) {
2061 drm_event_cancel_free(dev, &event->base);
2062 crtc_state->event = NULL;
2063 }
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002064 }
2065
2066 if (!fence_state)
2067 return;
2068
2069 for (i = 0; i < num_fences; i++) {
2070 if (fence_state[i].sync_file)
2071 fput(fence_state[i].sync_file->file);
2072 if (fence_state[i].fd >= 0)
2073 put_unused_fd(fence_state[i].fd);
2074
2075 /* If this fails log error to the user */
2076 if (fence_state[i].out_fence_ptr &&
2077 put_user(-1, fence_state[i].out_fence_ptr))
2078 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2079 }
2080
2081 kfree(fence_state);
2082}
2083
Rob Clarkd34f20d2014-12-18 16:01:56 -05002084int drm_mode_atomic_ioctl(struct drm_device *dev,
2085 void *data, struct drm_file *file_priv)
2086{
2087 struct drm_mode_atomic *arg = data;
2088 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2089 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2090 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2091 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2092 unsigned int copied_objs, copied_props;
2093 struct drm_atomic_state *state;
2094 struct drm_modeset_acquire_ctx ctx;
2095 struct drm_plane *plane;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002096 struct drm_out_fence_state *fence_state = NULL;
Maarten Lankhorst45723722015-11-11 11:29:08 +01002097 unsigned plane_mask;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002098 int ret = 0;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002099 unsigned int i, j, num_fences = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002100
2101 /* disallow for drivers not supporting atomic: */
2102 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2103 return -EINVAL;
2104
2105 /* disallow for userspace that has not enabled atomic cap (even
2106 * though this may be a bit overkill, since legacy userspace
2107 * wouldn't know how to call this ioctl)
2108 */
2109 if (!file_priv->atomic)
2110 return -EINVAL;
2111
2112 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2113 return -EINVAL;
2114
2115 if (arg->reserved)
2116 return -EINVAL;
2117
2118 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2119 !dev->mode_config.async_page_flip)
2120 return -EINVAL;
2121
2122 /* can't test and expect an event at the same time. */
2123 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2124 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2125 return -EINVAL;
2126
2127 drm_modeset_acquire_init(&ctx, 0);
2128
2129 state = drm_atomic_state_alloc(dev);
2130 if (!state)
2131 return -ENOMEM;
2132
2133 state->acquire_ctx = &ctx;
2134 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2135
2136retry:
Maarten Lankhorst45723722015-11-11 11:29:08 +01002137 plane_mask = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002138 copied_objs = 0;
2139 copied_props = 0;
2140
2141 for (i = 0; i < arg->count_objs; i++) {
2142 uint32_t obj_id, count_props;
2143 struct drm_mode_object *obj;
2144
2145 if (get_user(obj_id, objs_ptr + copied_objs)) {
2146 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002147 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002148 }
2149
2150 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
Dave Airlieb164d312016-04-27 11:10:09 +10002151 if (!obj) {
2152 ret = -ENOENT;
2153 goto out;
2154 }
2155
2156 if (!obj->properties) {
Thierry Reding020a2182017-02-28 15:46:38 +01002157 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002158 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002159 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002160 }
2161
Rob Clarkd34f20d2014-12-18 16:01:56 -05002162 if (get_user(count_props, count_props_ptr + copied_objs)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002163 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002164 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002165 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002166 }
2167
2168 copied_objs++;
2169
2170 for (j = 0; j < count_props; j++) {
2171 uint32_t prop_id;
2172 uint64_t prop_value;
2173 struct drm_property *prop;
2174
2175 if (get_user(prop_id, props_ptr + copied_props)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002176 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002177 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002178 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002179 }
2180
Maarten Lankhorstf92f0532016-09-08 12:30:01 +02002181 prop = drm_mode_obj_find_prop_id(obj, prop_id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002182 if (!prop) {
Thierry Reding020a2182017-02-28 15:46:38 +01002183 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002184 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002185 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002186 }
2187
Guenter Roeck42c58142015-01-12 21:12:17 -08002188 if (copy_from_user(&prop_value,
2189 prop_values_ptr + copied_props,
2190 sizeof(prop_value))) {
Thierry Reding020a2182017-02-28 15:46:38 +01002191 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002192 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002193 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002194 }
2195
2196 ret = atomic_set_prop(state, obj, prop, prop_value);
Dave Airlieb164d312016-04-27 11:10:09 +10002197 if (ret) {
Thierry Reding020a2182017-02-28 15:46:38 +01002198 drm_mode_object_put(obj);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002199 goto out;
Dave Airlieb164d312016-04-27 11:10:09 +10002200 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05002201
2202 copied_props++;
2203 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002204
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002205 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2206 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002207 plane = obj_to_plane(obj);
2208 plane_mask |= (1 << drm_plane_index(plane));
2209 plane->old_fb = plane->fb;
2210 }
Thierry Reding020a2182017-02-28 15:46:38 +01002211 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002212 }
2213
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002214 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2215 &num_fences);
2216 if (ret)
2217 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002218
2219 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2220 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002221 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002222 ret = drm_atomic_nonblocking_commit(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002223 } else {
Rob Clarkfceffb322016-11-05 11:08:09 -04002224 if (unlikely(drm_debug & DRM_UT_STATE))
2225 drm_atomic_print_state(state);
2226
Rob Clarkd34f20d2014-12-18 16:01:56 -05002227 ret = drm_atomic_commit(state);
2228 }
2229
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002230out:
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01002231 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002232
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002233 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002234
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002235 if (ret == -EDEADLK) {
2236 drm_atomic_state_clear(state);
2237 drm_modeset_backoff(&ctx);
2238 goto retry;
2239 }
2240
Chris Wilson08536952016-10-14 13:18:18 +01002241 drm_atomic_state_put(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002242
2243 drm_modeset_drop_locks(&ctx);
2244 drm_modeset_acquire_fini(&ctx);
2245
2246 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002247}