blob: 7fd88a09304b0d7968d1997b35eedf58a0e8b3b9 [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;
Dave Airlieb164d312016-04-27 11:10:09 +1000153 drm_connector_unreference(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 Lankhorst7f4eaa82016-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;
278 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200279 crtc_state->state = state;
280
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200281 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
282 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200283
284 return crtc_state;
285}
286EXPORT_SYMBOL(drm_atomic_get_crtc_state);
287
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900288static void set_out_fence_for_crtc(struct drm_atomic_state *state,
289 struct drm_crtc *crtc, s64 __user *fence_ptr)
290{
291 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
292}
293
294static s64 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
295 struct drm_crtc *crtc)
296{
297 s64 __user *fence_ptr;
298
299 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
300 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
301
302 return fence_ptr;
303}
304
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200305/**
Daniel Stone819364d2015-05-26 14:36:48 +0100306 * drm_atomic_set_mode_for_crtc - set mode for CRTC
307 * @state: the CRTC whose incoming state to update
308 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
309 *
310 * Set a mode (originating from the kernel) on the desired CRTC state. Does
311 * not change any other state properties, including enable, active, or
312 * mode_changed.
313 *
314 * RETURNS:
315 * Zero on success, error code on failure. Cannot return -EDEADLK.
316 */
317int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
318 struct drm_display_mode *mode)
319{
Daniel Stone99cf4a22015-05-25 19:11:51 +0100320 struct drm_mode_modeinfo umode;
321
Daniel Stone819364d2015-05-26 14:36:48 +0100322 /* Early return for no change. */
323 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
324 return 0;
325
Markus Elfring5f911902015-11-06 12:03:46 +0100326 drm_property_unreference_blob(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100327 state->mode_blob = NULL;
328
Daniel Stone819364d2015-05-26 14:36:48 +0100329 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100330 drm_mode_convert_to_umode(&umode, mode);
331 state->mode_blob =
332 drm_property_create_blob(state->crtc->dev,
333 sizeof(umode),
334 &umode);
335 if (IS_ERR(state->mode_blob))
336 return PTR_ERR(state->mode_blob);
337
Daniel Stone819364d2015-05-26 14:36:48 +0100338 drm_mode_copy(&state->mode, mode);
339 state->enable = true;
340 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
341 mode->name, state);
342 } else {
343 memset(&state->mode, 0, sizeof(state->mode));
344 state->enable = false;
345 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
346 state);
347 }
348
349 return 0;
350}
351EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
352
Daniel Stone819364d2015-05-26 14:36:48 +0100353/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100354 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
355 * @state: the CRTC whose incoming state to update
356 * @blob: pointer to blob property to use for mode
357 *
358 * Set a mode (originating from a blob property) on the desired CRTC state.
359 * This function will take a reference on the blob property for the CRTC state,
360 * and release the reference held on the state's existing mode property, if any
361 * was set.
362 *
363 * RETURNS:
364 * Zero on success, error code on failure. Cannot return -EDEADLK.
365 */
366int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
367 struct drm_property_blob *blob)
368{
369 if (blob == state->mode_blob)
370 return 0;
371
Markus Elfring5f911902015-11-06 12:03:46 +0100372 drm_property_unreference_blob(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100373 state->mode_blob = NULL;
374
Tomi Valkeinen67098872016-05-31 15:03:17 +0300375 memset(&state->mode, 0, sizeof(state->mode));
376
Daniel Stone955f3c32015-05-25 19:11:52 +0100377 if (blob) {
378 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
379 drm_mode_convert_umode(&state->mode,
380 (const struct drm_mode_modeinfo *)
381 blob->data))
382 return -EINVAL;
383
384 state->mode_blob = drm_property_reference_blob(blob);
385 state->enable = true;
386 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
387 state->mode.name, state);
388 } else {
Daniel Stone955f3c32015-05-25 19:11:52 +0100389 state->enable = false;
390 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
391 state);
392 }
393
394 return 0;
395}
396EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
397
398/**
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000399 * drm_atomic_replace_property_blob - replace a blob property
400 * @blob: a pointer to the member blob to be replaced
401 * @new_blob: the new blob to replace with
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000402 * @replaced: whether the blob has been replaced
403 *
404 * RETURNS:
405 * Zero on success, error code on failure
406 */
407static void
408drm_atomic_replace_property_blob(struct drm_property_blob **blob,
409 struct drm_property_blob *new_blob,
410 bool *replaced)
411{
412 struct drm_property_blob *old_blob = *blob;
413
414 if (old_blob == new_blob)
415 return;
416
Markus Elfringf35cbe62016-07-20 17:54:32 +0200417 drm_property_unreference_blob(old_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000418 if (new_blob)
419 drm_property_reference_blob(new_blob);
420 *blob = new_blob;
421 *replaced = true;
422
423 return;
424}
425
426static int
427drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
428 struct drm_property_blob **blob,
429 uint64_t blob_id,
430 ssize_t expected_size,
431 bool *replaced)
432{
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000433 struct drm_property_blob *new_blob = NULL;
434
435 if (blob_id != 0) {
Felix Monningercac5fced2016-10-25 22:28:08 +0100436 new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000437 if (new_blob == NULL)
438 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100439
440 if (expected_size > 0 && expected_size != new_blob->length) {
441 drm_property_unreference_blob(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000442 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100443 }
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000444 }
445
446 drm_atomic_replace_property_blob(blob, new_blob, replaced);
Felix Monningercac5fced2016-10-25 22:28:08 +0100447 drm_property_unreference_blob(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000448
449 return 0;
450}
451
452/**
Rob Clark40ecc692014-12-18 16:01:46 -0500453 * drm_atomic_crtc_set_property - set property on CRTC
454 * @crtc: the drm CRTC to set a property on
455 * @state: the state object to update with the new property value
456 * @property: the property to set
457 * @val: the new property value
458 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100459 * This function handles generic/core properties and calls out to driver's
460 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
461 * consistent behavior you must call this function rather than the driver hook
462 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500463 *
464 * RETURNS:
465 * Zero on success, error code on failure
466 */
467int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
468 struct drm_crtc_state *state, struct drm_property *property,
469 uint64_t val)
470{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100471 struct drm_device *dev = crtc->dev;
472 struct drm_mode_config *config = &dev->mode_config;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000473 bool replaced = false;
Daniel Stone955f3c32015-05-25 19:11:52 +0100474 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100475
Daniel Stone27798362015-03-19 04:33:26 +0000476 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100477 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100478 else if (property == config->prop_mode_id) {
479 struct drm_property_blob *mode =
480 drm_property_lookup_blob(dev, val);
481 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Markus Elfring5f911902015-11-06 12:03:46 +0100482 drm_property_unreference_blob(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100483 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000484 } else if (property == config->degamma_lut_property) {
485 ret = drm_atomic_replace_property_blob_from_id(crtc,
486 &state->degamma_lut,
487 val,
488 -1,
489 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200490 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000491 return ret;
492 } else if (property == config->ctm_property) {
493 ret = drm_atomic_replace_property_blob_from_id(crtc,
494 &state->ctm,
495 val,
496 sizeof(struct drm_color_ctm),
497 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200498 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000499 return ret;
500 } else if (property == config->gamma_lut_property) {
501 ret = drm_atomic_replace_property_blob_from_id(crtc,
502 &state->gamma_lut,
503 val,
504 -1,
505 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200506 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000507 return ret;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900508 } else if (property == config->prop_out_fence_ptr) {
509 s64 __user *fence_ptr = u64_to_user_ptr(val);
510
511 if (!fence_ptr)
512 return 0;
513
514 if (put_user(-1, fence_ptr))
515 return -EFAULT;
516
517 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000518 } else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500519 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000520 else
521 return -EINVAL;
522
523 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500524}
525EXPORT_SYMBOL(drm_atomic_crtc_set_property);
526
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100527/**
528 * drm_atomic_crtc_get_property - get property value from CRTC state
529 * @crtc: the drm CRTC to set a property on
530 * @state: the state object to get the property value from
531 * @property: the property to set
532 * @val: return location for the property value
533 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100534 * This function handles generic/core properties and calls out to driver's
535 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
536 * consistent behavior you must call this function rather than the driver hook
537 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100538 *
539 * RETURNS:
540 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500541 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700542static int
543drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500544 const struct drm_crtc_state *state,
545 struct drm_property *property, uint64_t *val)
546{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000547 struct drm_device *dev = crtc->dev;
548 struct drm_mode_config *config = &dev->mode_config;
549
550 if (property == config->prop_active)
551 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100552 else if (property == config->prop_mode_id)
553 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000554 else if (property == config->degamma_lut_property)
555 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
556 else if (property == config->ctm_property)
557 *val = (state->ctm) ? state->ctm->base.id : 0;
558 else if (property == config->gamma_lut_property)
559 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900560 else if (property == config->prop_out_fence_ptr)
561 *val = 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000562 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500563 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000564 else
565 return -EINVAL;
566
567 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500568}
Rob Clarkac9c9252014-12-18 16:01:47 -0500569
570/**
Rob Clark5e743732014-12-18 16:01:51 -0500571 * drm_atomic_crtc_check - check crtc state
572 * @crtc: crtc to check
573 * @state: crtc state to check
574 *
575 * Provides core sanity checks for crtc state.
576 *
577 * RETURNS:
578 * Zero on success, error code on failure
579 */
580static int drm_atomic_crtc_check(struct drm_crtc *crtc,
581 struct drm_crtc_state *state)
582{
583 /* NOTE: we explicitly don't enforce constraints such as primary
584 * layer covering entire screen, since that is something we want
585 * to allow (on hw that supports it). For hw that does not, it
586 * should be checked in driver's crtc->atomic_check() vfunc.
587 *
588 * TODO: Add generic modeset state checks once we support those.
589 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100590
591 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200592 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
593 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100594 return -EINVAL;
595 }
596
Daniel Stone99cf4a22015-05-25 19:11:51 +0100597 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
598 * as this is a kernel-internal detail that userspace should never
599 * be able to trigger. */
600 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
601 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200602 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
603 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100604 return -EINVAL;
605 }
606
607 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
608 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200609 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
610 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100611 return -EINVAL;
612 }
613
Daniel Vetter4cba6852015-12-08 09:49:20 +0100614 /*
615 * Reject event generation for when a CRTC is off and stays off.
616 * It wouldn't be hard to implement this, but userspace has a track
617 * record of happily burning through 100% cpu (or worse, crash) when the
618 * display pipe is suspended. To avoid all that fun just reject updates
619 * that ask for events since likely that indicates a bug in the
620 * compositor's drawing loop. This is consistent with the vblank IOCTL
621 * and legacy page_flip IOCTL which also reject service on a disabled
622 * pipe.
623 */
624 if (state->event && !state->active && !crtc->state->active) {
625 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
626 crtc->base.id);
627 return -EINVAL;
628 }
629
Rob Clark5e743732014-12-18 16:01:51 -0500630 return 0;
631}
632
Rob Clarkfceffb322016-11-05 11:08:09 -0400633static void drm_atomic_crtc_print_state(struct drm_printer *p,
634 const struct drm_crtc_state *state)
635{
636 struct drm_crtc *crtc = state->crtc;
637
638 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
639 drm_printf(p, "\tenable=%d\n", state->enable);
640 drm_printf(p, "\tactive=%d\n", state->active);
641 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
642 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
643 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
644 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
645 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
646 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
647 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
648 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
649 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
650
651 if (crtc->funcs->atomic_print_state)
652 crtc->funcs->atomic_print_state(p, state);
653}
654
Rob Clark5e743732014-12-18 16:01:51 -0500655/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200656 * drm_atomic_get_plane_state - get plane state
657 * @state: global atomic state object
658 * @plane: plane to get state object for
659 *
660 * This function returns the plane state for the given plane, allocating it if
661 * needed. It will also grab the relevant plane lock to make sure that the state
662 * is consistent.
663 *
664 * Returns:
665 *
666 * Either the allocated state or the error code encoded into the pointer. When
667 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
668 * entire atomic sequence must be restarted. All other errors are fatal.
669 */
670struct drm_plane_state *
671drm_atomic_get_plane_state(struct drm_atomic_state *state,
672 struct drm_plane *plane)
673{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200674 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200675 struct drm_plane_state *plane_state;
676
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200677 WARN_ON(!state->acquire_ctx);
678
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200679 plane_state = drm_atomic_get_existing_plane_state(state, plane);
680 if (plane_state)
681 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200682
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100683 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200684 if (ret)
685 return ERR_PTR(ret);
686
687 plane_state = plane->funcs->atomic_duplicate_state(plane);
688 if (!plane_state)
689 return ERR_PTR(-ENOMEM);
690
Daniel Vetterb8b53422016-06-02 00:06:33 +0200691 state->planes[index].state = plane_state;
692 state->planes[index].ptr = plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200693 plane_state->state = state;
694
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200695 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
696 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200697
698 if (plane_state->crtc) {
699 struct drm_crtc_state *crtc_state;
700
701 crtc_state = drm_atomic_get_crtc_state(state,
702 plane_state->crtc);
703 if (IS_ERR(crtc_state))
704 return ERR_CAST(crtc_state);
705 }
706
707 return plane_state;
708}
709EXPORT_SYMBOL(drm_atomic_get_plane_state);
710
711/**
Rob Clark40ecc692014-12-18 16:01:46 -0500712 * drm_atomic_plane_set_property - set property on plane
713 * @plane: the drm plane to set a property on
714 * @state: the state object to update with the new property value
715 * @property: the property to set
716 * @val: the new property value
717 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100718 * This function handles generic/core properties and calls out to driver's
719 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
720 * consistent behavior you must call this function rather than the driver hook
721 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500722 *
723 * RETURNS:
724 * Zero on success, error code on failure
725 */
726int drm_atomic_plane_set_property(struct drm_plane *plane,
727 struct drm_plane_state *state, struct drm_property *property,
728 uint64_t val)
729{
Rob Clark6b4959f2014-12-18 16:01:53 -0500730 struct drm_device *dev = plane->dev;
731 struct drm_mode_config *config = &dev->mode_config;
732
733 if (property == config->prop_fb_id) {
734 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
735 drm_atomic_set_fb_for_plane(state, fb);
736 if (fb)
737 drm_framebuffer_unreference(fb);
Gustavo Padovan96260142016-11-15 22:06:39 +0900738 } else if (property == config->prop_in_fence_fd) {
739 if (state->fence)
740 return -EINVAL;
741
742 if (U642I64(val) == -1)
743 return 0;
744
745 state->fence = sync_file_get_fence(val);
746 if (!state->fence)
747 return -EINVAL;
748
Rob Clark6b4959f2014-12-18 16:01:53 -0500749 } else if (property == config->prop_crtc_id) {
750 struct drm_crtc *crtc = drm_crtc_find(dev, val);
751 return drm_atomic_set_crtc_for_plane(state, crtc);
752 } else if (property == config->prop_crtc_x) {
753 state->crtc_x = U642I64(val);
754 } else if (property == config->prop_crtc_y) {
755 state->crtc_y = U642I64(val);
756 } else if (property == config->prop_crtc_w) {
757 state->crtc_w = val;
758 } else if (property == config->prop_crtc_h) {
759 state->crtc_h = val;
760 } else if (property == config->prop_src_x) {
761 state->src_x = val;
762 } else if (property == config->prop_src_y) {
763 state->src_y = val;
764 } else if (property == config->prop_src_w) {
765 state->src_w = val;
766 } else if (property == config->prop_src_h) {
767 state->src_h = val;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300768 } else if (property == plane->rotation_property) {
Ville Syrjälä6e0c7c32016-09-26 19:30:47 +0300769 if (!is_power_of_2(val & DRM_ROTATE_MASK))
770 return -EINVAL;
Matt Roper1da30622015-01-21 16:35:40 -0800771 state->rotation = val;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200772 } else if (property == plane->zpos_property) {
773 state->zpos = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500774 } else if (plane->funcs->atomic_set_property) {
775 return plane->funcs->atomic_set_property(plane, state,
776 property, val);
777 } else {
778 return -EINVAL;
779 }
780
781 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500782}
783EXPORT_SYMBOL(drm_atomic_plane_set_property);
784
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100785/**
786 * drm_atomic_plane_get_property - get property value from plane state
787 * @plane: the drm plane to set a property on
788 * @state: the state object to get the property value from
789 * @property: the property to set
790 * @val: return location for the property value
791 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100792 * This function handles generic/core properties and calls out to driver's
793 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
794 * consistent behavior you must call this function rather than the driver hook
795 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100796 *
797 * RETURNS:
798 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500799 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100800static int
801drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500802 const struct drm_plane_state *state,
803 struct drm_property *property, uint64_t *val)
804{
Rob Clark6b4959f2014-12-18 16:01:53 -0500805 struct drm_device *dev = plane->dev;
806 struct drm_mode_config *config = &dev->mode_config;
807
808 if (property == config->prop_fb_id) {
809 *val = (state->fb) ? state->fb->base.id : 0;
Gustavo Padovan96260142016-11-15 22:06:39 +0900810 } else if (property == config->prop_in_fence_fd) {
811 *val = -1;
Rob Clark6b4959f2014-12-18 16:01:53 -0500812 } else if (property == config->prop_crtc_id) {
813 *val = (state->crtc) ? state->crtc->base.id : 0;
814 } else if (property == config->prop_crtc_x) {
815 *val = I642U64(state->crtc_x);
816 } else if (property == config->prop_crtc_y) {
817 *val = I642U64(state->crtc_y);
818 } else if (property == config->prop_crtc_w) {
819 *val = state->crtc_w;
820 } else if (property == config->prop_crtc_h) {
821 *val = state->crtc_h;
822 } else if (property == config->prop_src_x) {
823 *val = state->src_x;
824 } else if (property == config->prop_src_y) {
825 *val = state->src_y;
826 } else if (property == config->prop_src_w) {
827 *val = state->src_w;
828 } else if (property == config->prop_src_h) {
829 *val = state->src_h;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300830 } else if (property == plane->rotation_property) {
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000831 *val = state->rotation;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200832 } else if (property == plane->zpos_property) {
833 *val = state->zpos;
Rob Clark6b4959f2014-12-18 16:01:53 -0500834 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500835 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500836 } else {
837 return -EINVAL;
838 }
839
840 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500841}
Rob Clarkac9c9252014-12-18 16:01:47 -0500842
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200843static bool
844plane_switching_crtc(struct drm_atomic_state *state,
845 struct drm_plane *plane,
846 struct drm_plane_state *plane_state)
847{
848 if (!plane->state->crtc || !plane_state->crtc)
849 return false;
850
851 if (plane->state->crtc == plane_state->crtc)
852 return false;
853
854 /* This could be refined, but currently there's no helper or driver code
855 * to implement direct switching of active planes nor userspace to take
856 * advantage of more direct plane switching without the intermediate
857 * full OFF state.
858 */
859 return true;
860}
861
Rob Clarkac9c9252014-12-18 16:01:47 -0500862/**
Rob Clark5e743732014-12-18 16:01:51 -0500863 * drm_atomic_plane_check - check plane state
864 * @plane: plane to check
865 * @state: plane state to check
866 *
867 * Provides core sanity checks for plane state.
868 *
869 * RETURNS:
870 * Zero on success, error code on failure
871 */
872static int drm_atomic_plane_check(struct drm_plane *plane,
873 struct drm_plane_state *state)
874{
875 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200876 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500877
878 /* either *both* CRTC and FB must be set, or neither */
879 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100880 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500881 return -EINVAL;
882 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100883 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500884 return -EINVAL;
885 }
886
887 /* if disabled, we don't care about the rest of the state: */
888 if (!state->crtc)
889 return 0;
890
891 /* Check whether this plane is usable on this CRTC */
892 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100893 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500894 return -EINVAL;
895 }
896
897 /* Check whether this plane supports the fb pixel format. */
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200898 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
Laurent Pinchartead86102015-03-05 02:25:43 +0200899 if (ret) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000900 struct drm_format_name_buf format_name;
901 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200902 drm_get_format_name(state->fb->format->format,
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000903 &format_name));
Laurent Pinchartead86102015-03-05 02:25:43 +0200904 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500905 }
906
907 /* Give drivers some help against integer overflows */
908 if (state->crtc_w > INT_MAX ||
909 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
910 state->crtc_h > INT_MAX ||
911 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100912 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
913 state->crtc_w, state->crtc_h,
914 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500915 return -ERANGE;
916 }
917
918 fb_width = state->fb->width << 16;
919 fb_height = state->fb->height << 16;
920
921 /* Make sure source coordinates are inside the fb. */
922 if (state->src_w > fb_width ||
923 state->src_x > fb_width - state->src_w ||
924 state->src_h > fb_height ||
925 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100926 DRM_DEBUG_ATOMIC("Invalid source coordinates "
927 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
928 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
929 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
930 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
931 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500932 return -ENOSPC;
933 }
934
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200935 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200936 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
937 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200938 return -EINVAL;
939 }
940
Rob Clark5e743732014-12-18 16:01:51 -0500941 return 0;
942}
943
Rob Clarkfceffb322016-11-05 11:08:09 -0400944static void drm_atomic_plane_print_state(struct drm_printer *p,
945 const struct drm_plane_state *state)
946{
947 struct drm_plane *plane = state->plane;
948 struct drm_rect src = drm_plane_state_src(state);
949 struct drm_rect dest = drm_plane_state_dest(state);
950
951 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
952 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
953 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
954 if (state->fb) {
955 struct drm_framebuffer *fb = state->fb;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200956 int i, n = fb->format->num_planes;
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000957 struct drm_format_name_buf format_name;
Rob Clarkfceffb322016-11-05 11:08:09 -0400958
959 drm_printf(p, "\t\tformat=%s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200960 drm_get_format_name(fb->format->format, &format_name));
Ville Syrjäläbae781b2016-11-16 13:33:16 +0200961 drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
Rob Clarkfceffb322016-11-05 11:08:09 -0400962 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
963 drm_printf(p, "\t\tlayers:\n");
964 for (i = 0; i < n; i++) {
965 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
966 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
Rob Clarkfceffb322016-11-05 11:08:09 -0400967 }
968 }
969 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
970 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
971 drm_printf(p, "\trotation=%x\n", state->rotation);
972
973 if (plane->funcs->atomic_print_state)
974 plane->funcs->atomic_print_state(p, state);
975}
976
Rob Clark5e743732014-12-18 16:01:51 -0500977/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200978 * drm_atomic_get_connector_state - get connector state
979 * @state: global atomic state object
980 * @connector: connector to get state object for
981 *
982 * This function returns the connector state for the given connector,
983 * allocating it if needed. It will also grab the relevant connector lock to
984 * make sure that the state is consistent.
985 *
986 * Returns:
987 *
988 * Either the allocated state or the error code encoded into the pointer. When
989 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
990 * entire atomic sequence must be restarted. All other errors are fatal.
991 */
992struct drm_connector_state *
993drm_atomic_get_connector_state(struct drm_atomic_state *state,
994 struct drm_connector *connector)
995{
996 int ret, index;
997 struct drm_mode_config *config = &connector->dev->mode_config;
998 struct drm_connector_state *connector_state;
999
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +02001000 WARN_ON(!state->acquire_ctx);
1001
Daniel Vetterc7eb76f2014-11-19 18:38:06 +01001002 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1003 if (ret)
1004 return ERR_PTR(ret);
1005
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001006 index = drm_connector_index(connector);
1007
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001008 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +02001009 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001010 int alloc = max(index + 1, config->num_connector);
1011
1012 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1013 if (!c)
1014 return ERR_PTR(-ENOMEM);
1015
1016 state->connectors = c;
1017 memset(&state->connectors[state->num_connector], 0,
1018 sizeof(*state->connectors) * (alloc - state->num_connector));
1019
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001020 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001021 }
1022
Daniel Vetter63e83c12016-06-02 00:06:32 +02001023 if (state->connectors[index].state)
1024 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001025
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001026 connector_state = connector->funcs->atomic_duplicate_state(connector);
1027 if (!connector_state)
1028 return ERR_PTR(-ENOMEM);
1029
Dave Airlieb164d312016-04-27 11:10:09 +10001030 drm_connector_reference(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +02001031 state->connectors[index].state = connector_state;
1032 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001033 connector_state->state = state;
1034
Daniel Vetter17a38d92015-02-22 12:24:16 +01001035 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
1036 connector->base.id, connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001037
1038 if (connector_state->crtc) {
1039 struct drm_crtc_state *crtc_state;
1040
1041 crtc_state = drm_atomic_get_crtc_state(state,
1042 connector_state->crtc);
1043 if (IS_ERR(crtc_state))
1044 return ERR_CAST(crtc_state);
1045 }
1046
1047 return connector_state;
1048}
1049EXPORT_SYMBOL(drm_atomic_get_connector_state);
1050
1051/**
Rob Clark40ecc692014-12-18 16:01:46 -05001052 * drm_atomic_connector_set_property - set property on connector.
1053 * @connector: the drm connector to set a property on
1054 * @state: the state object to update with the new property value
1055 * @property: the property to set
1056 * @val: the new property value
1057 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001058 * This function handles generic/core properties and calls out to driver's
1059 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1060 * consistent behavior you must call this function rather than the driver hook
1061 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -05001062 *
1063 * RETURNS:
1064 * Zero on success, error code on failure
1065 */
1066int drm_atomic_connector_set_property(struct drm_connector *connector,
1067 struct drm_connector_state *state, struct drm_property *property,
1068 uint64_t val)
1069{
1070 struct drm_device *dev = connector->dev;
1071 struct drm_mode_config *config = &dev->mode_config;
1072
Rob Clarkae16c592014-12-18 16:01:54 -05001073 if (property == config->prop_crtc_id) {
1074 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1075 return drm_atomic_set_crtc_for_connector(state, crtc);
1076 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -05001077 /* setting DPMS property requires special handling, which
1078 * is done in legacy setprop path for us. Disallow (for
1079 * now?) atomic writes to DPMS property:
1080 */
1081 return -EINVAL;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001082 } else if (property == config->tv_select_subconnector_property) {
1083 state->tv.subconnector = val;
1084 } else if (property == config->tv_left_margin_property) {
1085 state->tv.margins.left = val;
1086 } else if (property == config->tv_right_margin_property) {
1087 state->tv.margins.right = val;
1088 } else if (property == config->tv_top_margin_property) {
1089 state->tv.margins.top = val;
1090 } else if (property == config->tv_bottom_margin_property) {
1091 state->tv.margins.bottom = val;
1092 } else if (property == config->tv_mode_property) {
1093 state->tv.mode = val;
1094 } else if (property == config->tv_brightness_property) {
1095 state->tv.brightness = val;
1096 } else if (property == config->tv_contrast_property) {
1097 state->tv.contrast = val;
1098 } else if (property == config->tv_flicker_reduction_property) {
1099 state->tv.flicker_reduction = val;
1100 } else if (property == config->tv_overscan_property) {
1101 state->tv.overscan = val;
1102 } else if (property == config->tv_saturation_property) {
1103 state->tv.saturation = val;
1104 } else if (property == config->tv_hue_property) {
1105 state->tv.hue = val;
Rob Clark40ecc692014-12-18 16:01:46 -05001106 } else if (connector->funcs->atomic_set_property) {
1107 return connector->funcs->atomic_set_property(connector,
1108 state, property, val);
1109 } else {
1110 return -EINVAL;
1111 }
Boris Brezillon299a16b2016-12-02 14:48:09 +01001112
1113 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -05001114}
1115EXPORT_SYMBOL(drm_atomic_connector_set_property);
1116
Rob Clarkfceffb322016-11-05 11:08:09 -04001117static void drm_atomic_connector_print_state(struct drm_printer *p,
1118 const struct drm_connector_state *state)
1119{
1120 struct drm_connector *connector = state->connector;
1121
1122 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1123 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1124
1125 if (connector->funcs->atomic_print_state)
1126 connector->funcs->atomic_print_state(p, state);
1127}
1128
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001129/**
1130 * drm_atomic_connector_get_property - get property value from connector state
1131 * @connector: the drm connector to set a property on
1132 * @state: the state object to get the property value from
1133 * @property: the property to set
1134 * @val: return location for the property value
1135 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001136 * This function handles generic/core properties and calls out to driver's
1137 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1138 * consistent behavior you must call this function rather than the driver hook
1139 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001140 *
1141 * RETURNS:
1142 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -05001143 */
Daniel Vettera97df1c2014-12-18 22:49:02 +01001144static int
1145drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -05001146 const struct drm_connector_state *state,
1147 struct drm_property *property, uint64_t *val)
1148{
1149 struct drm_device *dev = connector->dev;
1150 struct drm_mode_config *config = &dev->mode_config;
1151
Rob Clarkae16c592014-12-18 16:01:54 -05001152 if (property == config->prop_crtc_id) {
1153 *val = (state->crtc) ? state->crtc->base.id : 0;
1154 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -05001155 *val = connector->dpms;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001156 } else if (property == config->tv_select_subconnector_property) {
1157 *val = state->tv.subconnector;
1158 } else if (property == config->tv_left_margin_property) {
1159 *val = state->tv.margins.left;
1160 } else if (property == config->tv_right_margin_property) {
1161 *val = state->tv.margins.right;
1162 } else if (property == config->tv_top_margin_property) {
1163 *val = state->tv.margins.top;
1164 } else if (property == config->tv_bottom_margin_property) {
1165 *val = state->tv.margins.bottom;
1166 } else if (property == config->tv_mode_property) {
1167 *val = state->tv.mode;
1168 } else if (property == config->tv_brightness_property) {
1169 *val = state->tv.brightness;
1170 } else if (property == config->tv_contrast_property) {
1171 *val = state->tv.contrast;
1172 } else if (property == config->tv_flicker_reduction_property) {
1173 *val = state->tv.flicker_reduction;
1174 } else if (property == config->tv_overscan_property) {
1175 *val = state->tv.overscan;
1176 } else if (property == config->tv_saturation_property) {
1177 *val = state->tv.saturation;
1178 } else if (property == config->tv_hue_property) {
1179 *val = state->tv.hue;
Rob Clarkac9c9252014-12-18 16:01:47 -05001180 } else if (connector->funcs->atomic_get_property) {
1181 return connector->funcs->atomic_get_property(connector,
1182 state, property, val);
1183 } else {
1184 return -EINVAL;
1185 }
1186
1187 return 0;
1188}
Rob Clarkac9c9252014-12-18 16:01:47 -05001189
Rob Clark88a48e22014-12-18 16:01:50 -05001190int drm_atomic_get_property(struct drm_mode_object *obj,
1191 struct drm_property *property, uint64_t *val)
1192{
1193 struct drm_device *dev = property->dev;
1194 int ret;
1195
1196 switch (obj->type) {
1197 case DRM_MODE_OBJECT_CONNECTOR: {
1198 struct drm_connector *connector = obj_to_connector(obj);
1199 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1200 ret = drm_atomic_connector_get_property(connector,
1201 connector->state, property, val);
1202 break;
1203 }
1204 case DRM_MODE_OBJECT_CRTC: {
1205 struct drm_crtc *crtc = obj_to_crtc(obj);
1206 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1207 ret = drm_atomic_crtc_get_property(crtc,
1208 crtc->state, property, val);
1209 break;
1210 }
1211 case DRM_MODE_OBJECT_PLANE: {
1212 struct drm_plane *plane = obj_to_plane(obj);
1213 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1214 ret = drm_atomic_plane_get_property(plane,
1215 plane->state, property, val);
1216 break;
1217 }
1218 default:
1219 ret = -EINVAL;
1220 break;
1221 }
1222
1223 return ret;
1224}
1225
1226/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001227 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001228 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001229 * @crtc: crtc to use for the plane
1230 *
1231 * Changing the assigned crtc for a plane requires us to grab the lock and state
1232 * for the new crtc, as needed. This function takes care of all these details
1233 * besides updating the pointer in the state object itself.
1234 *
1235 * Returns:
1236 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1237 * then the w/w mutex code has detected a deadlock and the entire atomic
1238 * sequence must be restarted. All other errors are fatal.
1239 */
1240int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001241drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1242 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001243{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001244 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001245 struct drm_crtc_state *crtc_state;
1246
Rob Clark6ddd3882014-11-21 15:28:31 -05001247 if (plane_state->crtc) {
1248 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1249 plane_state->crtc);
1250 if (WARN_ON(IS_ERR(crtc_state)))
1251 return PTR_ERR(crtc_state);
1252
1253 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1254 }
1255
1256 plane_state->crtc = crtc;
1257
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001258 if (crtc) {
1259 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1260 crtc);
1261 if (IS_ERR(crtc_state))
1262 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -05001263 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001264 }
1265
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001266 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001267 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1268 plane_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001269 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001270 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1271 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001272
1273 return 0;
1274}
1275EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1276
1277/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001278 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001279 * @plane_state: atomic state object for the plane
1280 * @fb: fb to use for the plane
1281 *
1282 * Changing the assigned framebuffer for a plane requires us to grab a reference
1283 * to the new fb and drop the reference to the old fb, if there is one. This
1284 * function takes care of all these details besides updating the pointer in the
1285 * state object itself.
1286 */
1287void
1288drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1289 struct drm_framebuffer *fb)
1290{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001291 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001292 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1293 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001294 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001295 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1296 plane_state);
Chris Wilson389f78b2016-11-25 15:32:30 +00001297
1298 drm_framebuffer_assign(&plane_state->fb, fb);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001299}
1300EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1301
1302/**
Gustavo Padovan13b55662016-11-07 19:03:30 +09001303 * drm_atomic_set_fence_for_plane - set fence for plane
1304 * @plane_state: atomic state object for the plane
1305 * @fence: dma_fence to use for the plane
1306 *
1307 * Helper to setup the plane_state fence in case it is not set yet.
1308 * By using this drivers doesn't need to worry if the user choose
1309 * implicit or explicit fencing.
1310 *
1311 * This function will not set the fence to the state if it was set
Daniel Vetterd5745282017-01-25 07:26:45 +01001312 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1313 * drop the reference to the fence as we are not storing it anywhere.
1314 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1315 * with the received implicit fence. In both cases this function consumes a
1316 * reference for @fence.
Gustavo Padovan13b55662016-11-07 19:03:30 +09001317 */
1318void
1319drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1320 struct dma_fence *fence)
1321{
1322 if (plane_state->fence) {
1323 dma_fence_put(fence);
1324 return;
1325 }
1326
1327 plane_state->fence = fence;
1328}
1329EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1330
1331/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001332 * drm_atomic_set_crtc_for_connector - set crtc for connector
1333 * @conn_state: atomic state object for the connector
1334 * @crtc: crtc to use for the connector
1335 *
1336 * Changing the assigned crtc for a connector requires us to grab the lock and
1337 * state for the new crtc, as needed. This function takes care of all these
1338 * details besides updating the pointer in the state object itself.
1339 *
1340 * Returns:
1341 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1342 * then the w/w mutex code has detected a deadlock and the entire atomic
1343 * sequence must be restarted. All other errors are fatal.
1344 */
1345int
1346drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1347 struct drm_crtc *crtc)
1348{
1349 struct drm_crtc_state *crtc_state;
1350
Chris Wilsone2d800a2016-05-06 12:47:45 +01001351 if (conn_state->crtc == crtc)
1352 return 0;
1353
1354 if (conn_state->crtc) {
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001355 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1356 conn_state->crtc);
1357
1358 crtc_state->connector_mask &=
1359 ~(1 << drm_connector_index(conn_state->connector));
Chris Wilsone2d800a2016-05-06 12:47:45 +01001360
1361 drm_connector_unreference(conn_state->connector);
1362 conn_state->crtc = NULL;
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001363 }
1364
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001365 if (crtc) {
1366 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1367 if (IS_ERR(crtc_state))
1368 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001369
1370 crtc_state->connector_mask |=
1371 1 << drm_connector_index(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001372
Chris Wilsone2d800a2016-05-06 12:47:45 +01001373 drm_connector_reference(conn_state->connector);
1374 conn_state->crtc = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001375
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001376 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1377 conn_state, crtc->base.id, crtc->name);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001378 } else {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001379 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1380 conn_state);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001381 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001382
1383 return 0;
1384}
1385EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1386
1387/**
1388 * drm_atomic_add_affected_connectors - add connectors for crtc
1389 * @state: atomic state
1390 * @crtc: DRM crtc
1391 *
1392 * This function walks the current configuration and adds all connectors
1393 * currently using @crtc to the atomic configuration @state. Note that this
1394 * function must acquire the connection mutex. This can potentially cause
1395 * unneeded seralization if the update is just for the planes on one crtc. Hence
1396 * drivers and helpers should only call this when really needed (e.g. when a
1397 * full modeset needs to happen due to some change).
1398 *
1399 * Returns:
1400 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1401 * then the w/w mutex code has detected a deadlock and the entire atomic
1402 * sequence must be restarted. All other errors are fatal.
1403 */
1404int
1405drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1406 struct drm_crtc *crtc)
1407{
1408 struct drm_mode_config *config = &state->dev->mode_config;
1409 struct drm_connector *connector;
1410 struct drm_connector_state *conn_state;
Daniel Vetter613051d2016-12-14 00:08:06 +01001411 struct drm_connector_list_iter conn_iter;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001412 int ret;
1413
1414 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1415 if (ret)
1416 return ret;
1417
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001418 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1419 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001420
1421 /*
1422 * Changed connectors are already in @state, so only need to look at the
1423 * current configuration.
1424 */
Daniel Vetter613051d2016-12-14 00:08:06 +01001425 drm_connector_list_iter_get(state->dev, &conn_iter);
1426 drm_for_each_connector_iter(connector, &conn_iter) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001427 if (connector->state->crtc != crtc)
1428 continue;
1429
1430 conn_state = drm_atomic_get_connector_state(state, connector);
Daniel Vetter613051d2016-12-14 00:08:06 +01001431 if (IS_ERR(conn_state)) {
1432 drm_connector_list_iter_put(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001433 return PTR_ERR(conn_state);
Daniel Vetter613051d2016-12-14 00:08:06 +01001434 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001435 }
Daniel Vetter613051d2016-12-14 00:08:06 +01001436 drm_connector_list_iter_put(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001437
1438 return 0;
1439}
1440EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1441
1442/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001443 * drm_atomic_add_affected_planes - add planes for crtc
1444 * @state: atomic state
1445 * @crtc: DRM crtc
1446 *
1447 * This function walks the current configuration and adds all planes
1448 * currently used by @crtc to the atomic configuration @state. This is useful
1449 * when an atomic commit also needs to check all currently enabled plane on
1450 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1451 * to avoid special code to force-enable all planes.
1452 *
1453 * Since acquiring a plane state will always also acquire the w/w mutex of the
1454 * current CRTC for that plane (if there is any) adding all the plane states for
1455 * a CRTC will not reduce parallism of atomic updates.
1456 *
1457 * Returns:
1458 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1459 * then the w/w mutex code has detected a deadlock and the entire atomic
1460 * sequence must be restarted. All other errors are fatal.
1461 */
1462int
1463drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1464 struct drm_crtc *crtc)
1465{
1466 struct drm_plane *plane;
1467
1468 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1469
1470 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1471 struct drm_plane_state *plane_state =
1472 drm_atomic_get_plane_state(state, plane);
1473
1474 if (IS_ERR(plane_state))
1475 return PTR_ERR(plane_state);
1476 }
1477 return 0;
1478}
1479EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1480
1481/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001482 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1483 * @state: atomic state
1484 *
1485 * This function should be used by legacy entry points which don't understand
1486 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +08001487 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001488 */
1489void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1490{
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001491 struct drm_device *dev = state->dev;
1492 unsigned crtc_mask = 0;
1493 struct drm_crtc *crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001494 int ret;
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001495 bool global = false;
1496
1497 drm_for_each_crtc(crtc, dev) {
1498 if (crtc->acquire_ctx != state->acquire_ctx)
1499 continue;
1500
1501 crtc_mask |= drm_crtc_mask(crtc);
1502 crtc->acquire_ctx = NULL;
1503 }
1504
1505 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1506 global = true;
1507
1508 dev->mode_config.acquire_ctx = NULL;
1509 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001510
1511retry:
1512 drm_modeset_backoff(state->acquire_ctx);
1513
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001514 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001515 if (ret)
1516 goto retry;
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001517
1518 drm_for_each_crtc(crtc, dev)
1519 if (drm_crtc_mask(crtc) & crtc_mask)
1520 crtc->acquire_ctx = state->acquire_ctx;
1521
1522 if (global)
1523 dev->mode_config.acquire_ctx = state->acquire_ctx;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001524}
1525EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1526
1527/**
1528 * drm_atomic_check_only - check whether a given config would work
1529 * @state: atomic configuration to check
1530 *
1531 * Note that this function can return -EDEADLK if the driver needed to acquire
1532 * more locks but encountered a deadlock. The caller must then do the usual w/w
1533 * backoff dance and restart. All other errors are fatal.
1534 *
1535 * Returns:
1536 * 0 on success, negative error code on failure.
1537 */
1538int drm_atomic_check_only(struct drm_atomic_state *state)
1539{
Rob Clark5e743732014-12-18 16:01:51 -05001540 struct drm_device *dev = state->dev;
1541 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001542 struct drm_plane *plane;
1543 struct drm_plane_state *plane_state;
1544 struct drm_crtc *crtc;
1545 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001546 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001547
Daniel Vetter17a38d92015-02-22 12:24:16 +01001548 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001549
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001550 for_each_plane_in_state(state, plane, plane_state, i) {
1551 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001552 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001553 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1554 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001555 return ret;
1556 }
1557 }
1558
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001559 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1560 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001561 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001562 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1563 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001564 return ret;
1565 }
1566 }
1567
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001568 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001569 ret = config->funcs->atomic_check(state->dev, state);
1570
Rob Clarkd34f20d2014-12-18 16:01:56 -05001571 if (!state->allow_modeset) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001572 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001573 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001574 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1575 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001576 return -EINVAL;
1577 }
1578 }
1579 }
1580
Rob Clark5e743732014-12-18 16:01:51 -05001581 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001582}
1583EXPORT_SYMBOL(drm_atomic_check_only);
1584
1585/**
1586 * drm_atomic_commit - commit configuration atomically
1587 * @state: atomic configuration to check
1588 *
1589 * Note that this function can return -EDEADLK if the driver needed to acquire
1590 * more locks but encountered a deadlock. The caller must then do the usual w/w
1591 * backoff dance and restart. All other errors are fatal.
1592 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001593 * This function will take its own reference on @state.
1594 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001595 *
1596 * Returns:
1597 * 0 on success, negative error code on failure.
1598 */
1599int drm_atomic_commit(struct drm_atomic_state *state)
1600{
1601 struct drm_mode_config *config = &state->dev->mode_config;
1602 int ret;
1603
1604 ret = drm_atomic_check_only(state);
1605 if (ret)
1606 return ret;
1607
Daniel Vetter17a38d92015-02-22 12:24:16 +01001608 DRM_DEBUG_ATOMIC("commiting %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001609
1610 return config->funcs->atomic_commit(state->dev, state, false);
1611}
1612EXPORT_SYMBOL(drm_atomic_commit);
1613
1614/**
Daniel Vetterd5745282017-01-25 07:26:45 +01001615 * drm_atomic_nonblocking_commit - atomic nonblocking commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001616 * @state: atomic configuration to check
1617 *
1618 * Note that this function can return -EDEADLK if the driver needed to acquire
1619 * more locks but encountered a deadlock. The caller must then do the usual w/w
1620 * backoff dance and restart. All other errors are fatal.
1621 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001622 * This function will take its own reference on @state.
1623 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001624 *
1625 * Returns:
1626 * 0 on success, negative error code on failure.
1627 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001628int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001629{
1630 struct drm_mode_config *config = &state->dev->mode_config;
1631 int ret;
1632
1633 ret = drm_atomic_check_only(state);
1634 if (ret)
1635 return ret;
1636
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001637 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001638
1639 return config->funcs->atomic_commit(state->dev, state, true);
1640}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001641EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001642
Rob Clarkfceffb322016-11-05 11:08:09 -04001643static void drm_atomic_print_state(const struct drm_atomic_state *state)
1644{
1645 struct drm_printer p = drm_info_printer(state->dev->dev);
1646 struct drm_plane *plane;
1647 struct drm_plane_state *plane_state;
1648 struct drm_crtc *crtc;
1649 struct drm_crtc_state *crtc_state;
1650 struct drm_connector *connector;
1651 struct drm_connector_state *connector_state;
1652 int i;
1653
1654 DRM_DEBUG_ATOMIC("checking %p\n", state);
1655
1656 for_each_plane_in_state(state, plane, plane_state, i)
1657 drm_atomic_plane_print_state(&p, plane_state);
1658
1659 for_each_crtc_in_state(state, crtc, crtc_state, i)
1660 drm_atomic_crtc_print_state(&p, crtc_state);
1661
1662 for_each_connector_in_state(state, connector, connector_state, i)
1663 drm_atomic_connector_print_state(&p, connector_state);
1664}
1665
Rob Clark6559c902016-11-05 11:08:10 -04001666/**
1667 * drm_state_dump - dump entire device atomic state
1668 * @dev: the drm device
1669 * @p: where to print the state to
1670 *
1671 * Just for debugging. Drivers might want an option to dump state
1672 * to dmesg in case of error irq's. (Hint, you probably want to
1673 * ratelimit this!)
1674 *
1675 * The caller must drm_modeset_lock_all(), or if this is called
1676 * from error irq handler, it should not be enabled by default.
1677 * (Ie. if you are debugging errors you might not care that this
1678 * is racey. But calling this without all modeset locks held is
1679 * not inherently safe.)
1680 */
1681void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1682{
1683 struct drm_mode_config *config = &dev->mode_config;
1684 struct drm_plane *plane;
1685 struct drm_crtc *crtc;
1686 struct drm_connector *connector;
Daniel Vetter613051d2016-12-14 00:08:06 +01001687 struct drm_connector_list_iter conn_iter;
Rob Clark6559c902016-11-05 11:08:10 -04001688
1689 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1690 return;
1691
1692 list_for_each_entry(plane, &config->plane_list, head)
1693 drm_atomic_plane_print_state(p, plane->state);
1694
1695 list_for_each_entry(crtc, &config->crtc_list, head)
1696 drm_atomic_crtc_print_state(p, crtc->state);
1697
Daniel Vetter613051d2016-12-14 00:08:06 +01001698 drm_connector_list_iter_get(dev, &conn_iter);
1699 drm_for_each_connector_iter(connector, &conn_iter)
Rob Clark6559c902016-11-05 11:08:10 -04001700 drm_atomic_connector_print_state(p, connector->state);
Daniel Vetter613051d2016-12-14 00:08:06 +01001701 drm_connector_list_iter_put(&conn_iter);
Rob Clark6559c902016-11-05 11:08:10 -04001702}
1703EXPORT_SYMBOL(drm_state_dump);
1704
1705#ifdef CONFIG_DEBUG_FS
1706static int drm_state_info(struct seq_file *m, void *data)
1707{
1708 struct drm_info_node *node = (struct drm_info_node *) m->private;
1709 struct drm_device *dev = node->minor->dev;
1710 struct drm_printer p = drm_seq_file_printer(m);
1711
1712 drm_modeset_lock_all(dev);
1713 drm_state_dump(dev, &p);
1714 drm_modeset_unlock_all(dev);
1715
1716 return 0;
1717}
1718
1719/* any use in debugfs files to dump individual planes/crtc/etc? */
1720static const struct drm_info_list drm_atomic_debugfs_list[] = {
1721 {"state", drm_state_info, 0},
1722};
1723
1724int drm_atomic_debugfs_init(struct drm_minor *minor)
1725{
1726 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1727 ARRAY_SIZE(drm_atomic_debugfs_list),
1728 minor->debugfs_root, minor);
1729}
Liviu Dudau8c0b55e2016-11-17 11:41:29 +00001730
1731int drm_atomic_debugfs_cleanup(struct drm_minor *minor)
1732{
1733 return drm_debugfs_remove_files(drm_atomic_debugfs_list,
1734 ARRAY_SIZE(drm_atomic_debugfs_list),
1735 minor);
1736}
Rob Clark6559c902016-11-05 11:08:10 -04001737#endif
1738
Rob Clarkd34f20d2014-12-18 16:01:56 -05001739/*
1740 * The big monstor ioctl
1741 */
1742
1743static struct drm_pending_vblank_event *create_vblank_event(
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09001744 struct drm_device *dev, uint64_t user_data)
Rob Clarkd34f20d2014-12-18 16:01:56 -05001745{
1746 struct drm_pending_vblank_event *e = NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001747
1748 e = kzalloc(sizeof *e, GFP_KERNEL);
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001749 if (!e)
1750 return NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001751
1752 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001753 e->event.base.length = sizeof(e->event);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001754 e->event.user_data = user_data;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001755
Rob Clarkd34f20d2014-12-18 16:01:56 -05001756 return e;
1757}
1758
Rob Clarkd34f20d2014-12-18 16:01:56 -05001759static int atomic_set_prop(struct drm_atomic_state *state,
1760 struct drm_mode_object *obj, struct drm_property *prop,
1761 uint64_t prop_value)
1762{
1763 struct drm_mode_object *ref;
1764 int ret;
1765
1766 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1767 return -EINVAL;
1768
1769 switch (obj->type) {
1770 case DRM_MODE_OBJECT_CONNECTOR: {
1771 struct drm_connector *connector = obj_to_connector(obj);
1772 struct drm_connector_state *connector_state;
1773
1774 connector_state = drm_atomic_get_connector_state(state, connector);
1775 if (IS_ERR(connector_state)) {
1776 ret = PTR_ERR(connector_state);
1777 break;
1778 }
1779
1780 ret = drm_atomic_connector_set_property(connector,
1781 connector_state, prop, prop_value);
1782 break;
1783 }
1784 case DRM_MODE_OBJECT_CRTC: {
1785 struct drm_crtc *crtc = obj_to_crtc(obj);
1786 struct drm_crtc_state *crtc_state;
1787
1788 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1789 if (IS_ERR(crtc_state)) {
1790 ret = PTR_ERR(crtc_state);
1791 break;
1792 }
1793
1794 ret = drm_atomic_crtc_set_property(crtc,
1795 crtc_state, prop, prop_value);
1796 break;
1797 }
1798 case DRM_MODE_OBJECT_PLANE: {
1799 struct drm_plane *plane = obj_to_plane(obj);
1800 struct drm_plane_state *plane_state;
1801
1802 plane_state = drm_atomic_get_plane_state(state, plane);
1803 if (IS_ERR(plane_state)) {
1804 ret = PTR_ERR(plane_state);
1805 break;
1806 }
1807
1808 ret = drm_atomic_plane_set_property(plane,
1809 plane_state, prop, prop_value);
1810 break;
1811 }
1812 default:
1813 ret = -EINVAL;
1814 break;
1815 }
1816
1817 drm_property_change_valid_put(prop, ref);
1818 return ret;
1819}
1820
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001821/**
Maarten Lankhorst9744bf42015-11-24 10:34:34 +01001822 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001823 *
1824 * @dev: drm device to check.
1825 * @plane_mask: plane mask for planes that were updated.
1826 * @ret: return value, can be -EDEADLK for a retry.
1827 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001828 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1829 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1830 * is a common operation for each atomic update, so this call is split off as a
1831 * helper.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001832 */
1833void drm_atomic_clean_old_fb(struct drm_device *dev,
1834 unsigned plane_mask,
1835 int ret)
1836{
1837 struct drm_plane *plane;
1838
1839 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1840 * locks (ie. while it is still safe to deref plane->state). We
1841 * need to do this here because the driver entry points cannot
1842 * distinguish between legacy and atomic ioctls.
1843 */
1844 drm_for_each_plane_mask(plane, dev, plane_mask) {
1845 if (ret == 0) {
1846 struct drm_framebuffer *new_fb = plane->state->fb;
1847 if (new_fb)
1848 drm_framebuffer_reference(new_fb);
1849 plane->fb = new_fb;
1850 plane->crtc = plane->state->crtc;
1851
1852 if (plane->old_fb)
1853 drm_framebuffer_unreference(plane->old_fb);
1854 }
1855 plane->old_fb = NULL;
1856 }
1857}
1858EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1859
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001860/**
1861 * DOC: explicit fencing properties
1862 *
1863 * Explicit fencing allows userspace to control the buffer synchronization
1864 * between devices. A Fence or a group of fences are transfered to/from
1865 * userspace using Sync File fds and there are two DRM properties for that.
1866 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1867 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1868 *
1869 * As a contrast, with implicit fencing the kernel keeps track of any
1870 * ongoing rendering, and automatically ensures that the atomic update waits
1871 * for any pending rendering to complete. For shared buffers represented with
Daniel Vetterd5745282017-01-25 07:26:45 +01001872 * a &struct dma_buf this is tracked in &struct reservation_object.
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001873 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1874 * whereas explicit fencing is what Android wants.
1875 *
1876 * "IN_FENCE_FD”:
1877 * Use this property to pass a fence that DRM should wait on before
1878 * proceeding with the Atomic Commit request and show the framebuffer for
1879 * the plane on the screen. The fence can be either a normal fence or a
1880 * merged one, the sync_file framework will handle both cases and use a
1881 * fence_array if a merged fence is received. Passing -1 here means no
1882 * fences to wait on.
1883 *
1884 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1885 * it will only check if the Sync File is a valid one.
1886 *
1887 * On the driver side the fence is stored on the @fence parameter of
Daniel Vetterea0dd852016-12-29 21:48:26 +01001888 * &struct drm_plane_state. Drivers which also support implicit fencing
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001889 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1890 * to make sure there's consistent behaviour between drivers in precedence
1891 * of implicit vs. explicit fencing.
1892 *
1893 * "OUT_FENCE_PTR”:
1894 * Use this property to pass a file descriptor pointer to DRM. Once the
1895 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1896 * the file descriptor number of a Sync File. This Sync File contains the
1897 * CRTC fence that will be signaled when all framebuffers present on the
1898 * Atomic Commit * request for that given CRTC are scanned out on the
1899 * screen.
1900 *
1901 * The Atomic Commit request fails if a invalid pointer is passed. If the
1902 * Atomic Commit request fails for any other reason the out fence fd
1903 * returned will be -1. On a Atomic Commit with the
1904 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1905 *
1906 * Note that out-fences don't have a special interface to drivers and are
Daniel Vetterea0dd852016-12-29 21:48:26 +01001907 * internally represented by a &struct drm_pending_vblank_event in struct
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001908 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1909 * helpers and for the DRM event handling for existing userspace.
1910 */
1911
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09001912struct drm_out_fence_state {
1913 s64 __user *out_fence_ptr;
1914 struct sync_file *sync_file;
1915 int fd;
1916};
1917
1918static int setup_out_fence(struct drm_out_fence_state *fence_state,
1919 struct dma_fence *fence)
1920{
1921 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1922 if (fence_state->fd < 0)
1923 return fence_state->fd;
1924
1925 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1926 return -EFAULT;
1927
1928 fence_state->sync_file = sync_file_create(fence);
1929 if (!fence_state->sync_file)
1930 return -ENOMEM;
1931
1932 return 0;
1933}
1934
1935static int prepare_crtc_signaling(struct drm_device *dev,
1936 struct drm_atomic_state *state,
1937 struct drm_mode_atomic *arg,
1938 struct drm_file *file_priv,
1939 struct drm_out_fence_state **fence_state,
1940 unsigned int *num_fences)
1941{
1942 struct drm_crtc *crtc;
1943 struct drm_crtc_state *crtc_state;
1944 int i, ret;
1945
1946 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1947 return 0;
1948
1949 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1950 u64 __user *fence_ptr;
1951
1952 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1953
1954 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1955 struct drm_pending_vblank_event *e;
1956
1957 e = create_vblank_event(dev, arg->user_data);
1958 if (!e)
1959 return -ENOMEM;
1960
1961 crtc_state->event = e;
1962 }
1963
1964 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1965 struct drm_pending_vblank_event *e = crtc_state->event;
1966
1967 if (!file_priv)
1968 continue;
1969
1970 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1971 &e->event.base);
1972 if (ret) {
1973 kfree(e);
1974 crtc_state->event = NULL;
1975 return ret;
1976 }
1977 }
1978
1979 if (fence_ptr) {
1980 struct dma_fence *fence;
1981 struct drm_out_fence_state *f;
1982
1983 f = krealloc(*fence_state, sizeof(**fence_state) *
1984 (*num_fences + 1), GFP_KERNEL);
1985 if (!f)
1986 return -ENOMEM;
1987
1988 memset(&f[*num_fences], 0, sizeof(*f));
1989
1990 f[*num_fences].out_fence_ptr = fence_ptr;
1991 *fence_state = f;
1992
Gustavo Padovan35f8cc32016-12-06 15:47:17 -02001993 fence = drm_crtc_create_fence(crtc);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09001994 if (!fence)
1995 return -ENOMEM;
1996
1997 ret = setup_out_fence(&f[(*num_fences)++], fence);
1998 if (ret) {
1999 dma_fence_put(fence);
2000 return ret;
2001 }
2002
2003 crtc_state->event->base.fence = fence;
2004 }
2005 }
2006
2007 return 0;
2008}
2009
2010static void complete_crtc_signaling(struct drm_device *dev,
2011 struct drm_atomic_state *state,
2012 struct drm_out_fence_state *fence_state,
2013 unsigned int num_fences,
2014 bool install_fds)
2015{
2016 struct drm_crtc *crtc;
2017 struct drm_crtc_state *crtc_state;
2018 int i;
2019
2020 if (install_fds) {
2021 for (i = 0; i < num_fences; i++)
2022 fd_install(fence_state[i].fd,
2023 fence_state[i].sync_file->file);
2024
2025 kfree(fence_state);
2026 return;
2027 }
2028
2029 for_each_crtc_in_state(state, crtc, crtc_state, i) {
2030 /*
2031 * TEST_ONLY and PAGE_FLIP_EVENT are mutually
2032 * exclusive, if they weren't, this code should be
2033 * called on success for TEST_ONLY too.
2034 */
2035 if (crtc_state->event)
2036 drm_event_cancel_free(dev, &crtc_state->event->base);
2037 }
2038
2039 if (!fence_state)
2040 return;
2041
2042 for (i = 0; i < num_fences; i++) {
2043 if (fence_state[i].sync_file)
2044 fput(fence_state[i].sync_file->file);
2045 if (fence_state[i].fd >= 0)
2046 put_unused_fd(fence_state[i].fd);
2047
2048 /* If this fails log error to the user */
2049 if (fence_state[i].out_fence_ptr &&
2050 put_user(-1, fence_state[i].out_fence_ptr))
2051 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2052 }
2053
2054 kfree(fence_state);
2055}
2056
Rob Clarkd34f20d2014-12-18 16:01:56 -05002057int drm_mode_atomic_ioctl(struct drm_device *dev,
2058 void *data, struct drm_file *file_priv)
2059{
2060 struct drm_mode_atomic *arg = data;
2061 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2062 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2063 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2064 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2065 unsigned int copied_objs, copied_props;
2066 struct drm_atomic_state *state;
2067 struct drm_modeset_acquire_ctx ctx;
2068 struct drm_plane *plane;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002069 struct drm_out_fence_state *fence_state = NULL;
Maarten Lankhorst45723722015-11-11 11:29:08 +01002070 unsigned plane_mask;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002071 int ret = 0;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002072 unsigned int i, j, num_fences = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002073
2074 /* disallow for drivers not supporting atomic: */
2075 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2076 return -EINVAL;
2077
2078 /* disallow for userspace that has not enabled atomic cap (even
2079 * though this may be a bit overkill, since legacy userspace
2080 * wouldn't know how to call this ioctl)
2081 */
2082 if (!file_priv->atomic)
2083 return -EINVAL;
2084
2085 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2086 return -EINVAL;
2087
2088 if (arg->reserved)
2089 return -EINVAL;
2090
2091 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2092 !dev->mode_config.async_page_flip)
2093 return -EINVAL;
2094
2095 /* can't test and expect an event at the same time. */
2096 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2097 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2098 return -EINVAL;
2099
2100 drm_modeset_acquire_init(&ctx, 0);
2101
2102 state = drm_atomic_state_alloc(dev);
2103 if (!state)
2104 return -ENOMEM;
2105
2106 state->acquire_ctx = &ctx;
2107 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2108
2109retry:
Maarten Lankhorst45723722015-11-11 11:29:08 +01002110 plane_mask = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002111 copied_objs = 0;
2112 copied_props = 0;
2113
2114 for (i = 0; i < arg->count_objs; i++) {
2115 uint32_t obj_id, count_props;
2116 struct drm_mode_object *obj;
2117
2118 if (get_user(obj_id, objs_ptr + copied_objs)) {
2119 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002120 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002121 }
2122
2123 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
Dave Airlieb164d312016-04-27 11:10:09 +10002124 if (!obj) {
2125 ret = -ENOENT;
2126 goto out;
2127 }
2128
2129 if (!obj->properties) {
2130 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002131 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002132 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002133 }
2134
Rob Clarkd34f20d2014-12-18 16:01:56 -05002135 if (get_user(count_props, count_props_ptr + copied_objs)) {
Dave Airlieb164d312016-04-27 11:10:09 +10002136 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002137 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002138 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002139 }
2140
2141 copied_objs++;
2142
2143 for (j = 0; j < count_props; j++) {
2144 uint32_t prop_id;
2145 uint64_t prop_value;
2146 struct drm_property *prop;
2147
2148 if (get_user(prop_id, props_ptr + copied_props)) {
Dave Airlieb164d312016-04-27 11:10:09 +10002149 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002150 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002151 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002152 }
2153
Maarten Lankhorstf92f0532016-09-08 12:30:01 +02002154 prop = drm_mode_obj_find_prop_id(obj, prop_id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002155 if (!prop) {
Dave Airlieb164d312016-04-27 11:10:09 +10002156 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002157 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002158 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002159 }
2160
Guenter Roeck42c58142015-01-12 21:12:17 -08002161 if (copy_from_user(&prop_value,
2162 prop_values_ptr + copied_props,
2163 sizeof(prop_value))) {
Dave Airlieb164d312016-04-27 11:10:09 +10002164 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002165 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002166 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002167 }
2168
2169 ret = atomic_set_prop(state, obj, prop, prop_value);
Dave Airlieb164d312016-04-27 11:10:09 +10002170 if (ret) {
2171 drm_mode_object_unreference(obj);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002172 goto out;
Dave Airlieb164d312016-04-27 11:10:09 +10002173 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05002174
2175 copied_props++;
2176 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002177
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002178 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2179 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002180 plane = obj_to_plane(obj);
2181 plane_mask |= (1 << drm_plane_index(plane));
2182 plane->old_fb = plane->fb;
2183 }
Dave Airlieb164d312016-04-27 11:10:09 +10002184 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002185 }
2186
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002187 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2188 &num_fences);
2189 if (ret)
2190 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002191
2192 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2193 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002194 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002195 ret = drm_atomic_nonblocking_commit(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002196 } else {
Rob Clarkfceffb322016-11-05 11:08:09 -04002197 if (unlikely(drm_debug & DRM_UT_STATE))
2198 drm_atomic_print_state(state);
2199
Rob Clarkd34f20d2014-12-18 16:01:56 -05002200 ret = drm_atomic_commit(state);
2201 }
2202
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002203out:
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01002204 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002205
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002206 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002207
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002208 if (ret == -EDEADLK) {
2209 drm_atomic_state_clear(state);
2210 drm_modeset_backoff(&ctx);
2211 goto retry;
2212 }
2213
Chris Wilson08536952016-10-14 13:18:18 +01002214 drm_atomic_state_put(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002215
2216 drm_modeset_drop_locks(&ctx);
2217 drm_modeset_acquire_fini(&ctx);
2218
2219 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002220}