blob: c7e7a955bb1e92552febfabed73484bb524a3ec2 [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>
33
Maarten Lankhorst036ef572015-05-18 10:06:40 +020034/**
35 * drm_atomic_state_default_release -
36 * release memory initialized by drm_atomic_state_init
37 * @state: atomic state
38 *
39 * Free all the memory allocated by drm_atomic_state_init.
40 * This is useful for drivers that subclass the atomic state.
41 */
42void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020043{
44 kfree(state->connectors);
45 kfree(state->connector_states);
46 kfree(state->crtcs);
47 kfree(state->crtc_states);
48 kfree(state->planes);
49 kfree(state->plane_states);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020050}
Maarten Lankhorst036ef572015-05-18 10:06:40 +020051EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020052
53/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020054 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020055 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +020056 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020057 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +020058 * Default implementation for filling in a new atomic state.
59 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +020060 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +020061int
62drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020063{
Rob Clarkd34f20d2014-12-18 16:01:56 -050064 /* TODO legacy paths should maybe do a better job about
65 * setting this appropriately?
66 */
67 state->allow_modeset = true;
68
Daniel Vetterf52b69f12014-11-19 18:38:08 +010069 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
70
Daniel Vettercc4ceb42014-07-25 21:30:38 +020071 state->crtcs = kcalloc(dev->mode_config.num_crtc,
72 sizeof(*state->crtcs), GFP_KERNEL);
73 if (!state->crtcs)
74 goto fail;
75 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
76 sizeof(*state->crtc_states), GFP_KERNEL);
77 if (!state->crtc_states)
78 goto fail;
79 state->planes = kcalloc(dev->mode_config.num_total_plane,
80 sizeof(*state->planes), GFP_KERNEL);
81 if (!state->planes)
82 goto fail;
83 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
84 sizeof(*state->plane_states), GFP_KERNEL);
85 if (!state->plane_states)
86 goto fail;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010087 state->connectors = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020088 sizeof(*state->connectors),
89 GFP_KERNEL);
90 if (!state->connectors)
91 goto fail;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010092 state->connector_states = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020093 sizeof(*state->connector_states),
94 GFP_KERNEL);
95 if (!state->connector_states)
96 goto fail;
97
98 state->dev = dev;
99
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200100 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200101
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200102 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200103fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200104 drm_atomic_state_default_release(state);
105 return -ENOMEM;
106}
107EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200108
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200109/**
110 * drm_atomic_state_alloc - allocate atomic state
111 * @dev: DRM device
112 *
113 * This allocates an empty atomic state to track updates.
114 */
115struct drm_atomic_state *
116drm_atomic_state_alloc(struct drm_device *dev)
117{
118 struct drm_mode_config *config = &dev->mode_config;
119 struct drm_atomic_state *state;
120
121 if (!config->funcs->atomic_state_alloc) {
122 state = kzalloc(sizeof(*state), GFP_KERNEL);
123 if (!state)
124 return NULL;
125 if (drm_atomic_state_init(dev, state) < 0) {
126 kfree(state);
127 return NULL;
128 }
129 return state;
130 }
131
132 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200133}
134EXPORT_SYMBOL(drm_atomic_state_alloc);
135
136/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200137 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200138 * @state: atomic state
139 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200140 * Default implementation for clearing atomic state.
141 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200142 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200143void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200144{
145 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100146 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200147 int i;
148
Daniel Vetter17a38d92015-02-22 12:24:16 +0100149 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200150
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100151 for (i = 0; i < state->num_connector; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200152 struct drm_connector *connector = state->connectors[i];
153
154 if (!connector)
155 continue;
156
Daniel Vetter460e8e22015-07-29 12:51:41 +0200157 /*
158 * FIXME: Async commits can race with connector unplugging and
159 * there's currently nothing that prevents cleanup up state for
160 * deleted connectors. As long as the callback doesn't look at
161 * the connector we'll be fine though, so make sure that's the
162 * case by setting all connector pointers to NULL.
163 */
164 state->connector_states[i]->connector = NULL;
165 connector->funcs->atomic_destroy_state(NULL,
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200166 state->connector_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300167 state->connectors[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200168 state->connector_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200169 }
170
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100171 for (i = 0; i < config->num_crtc; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200172 struct drm_crtc *crtc = state->crtcs[i];
173
174 if (!crtc)
175 continue;
176
177 crtc->funcs->atomic_destroy_state(crtc,
178 state->crtc_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300179 state->crtcs[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200180 state->crtc_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200181 }
182
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100183 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200184 struct drm_plane *plane = state->planes[i];
185
186 if (!plane)
187 continue;
188
189 plane->funcs->atomic_destroy_state(plane,
190 state->plane_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300191 state->planes[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200192 state->plane_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200193 }
194}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200195EXPORT_SYMBOL(drm_atomic_state_default_clear);
196
197/**
198 * drm_atomic_state_clear - clear state object
199 * @state: atomic state
200 *
201 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
202 * all locks. So someone else could sneak in and change the current modeset
203 * configuration. Which means that all the state assembled in @state is no
204 * longer an atomic update to the current state, but to some arbitrary earlier
205 * state. Which could break assumptions the driver's ->atomic_check likely
206 * relies on.
207 *
208 * Hence we must clear all cached state and completely start over, using this
209 * function.
210 */
211void drm_atomic_state_clear(struct drm_atomic_state *state)
212{
213 struct drm_device *dev = state->dev;
214 struct drm_mode_config *config = &dev->mode_config;
215
216 if (config->funcs->atomic_state_clear)
217 config->funcs->atomic_state_clear(state);
218 else
219 drm_atomic_state_default_clear(state);
220}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200221EXPORT_SYMBOL(drm_atomic_state_clear);
222
223/**
224 * drm_atomic_state_free - free all memory for an atomic state
225 * @state: atomic state to deallocate
226 *
227 * This frees all memory associated with an atomic state, including all the
228 * per-object state for planes, crtcs and connectors.
229 */
230void drm_atomic_state_free(struct drm_atomic_state *state)
231{
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200232 struct drm_device *dev;
233 struct drm_mode_config *config;
234
Ander Conselvan de Oliveiraa0211bb2015-03-30 14:05:43 +0300235 if (!state)
236 return;
237
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200238 dev = state->dev;
239 config = &dev->mode_config;
240
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200241 drm_atomic_state_clear(state);
242
Daniel Vetter17a38d92015-02-22 12:24:16 +0100243 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200244
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200245 if (config->funcs->atomic_state_free) {
246 config->funcs->atomic_state_free(state);
247 } else {
248 drm_atomic_state_default_release(state);
249 kfree(state);
250 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200251}
252EXPORT_SYMBOL(drm_atomic_state_free);
253
254/**
255 * drm_atomic_get_crtc_state - get crtc state
256 * @state: global atomic state object
257 * @crtc: crtc to get state object for
258 *
259 * This function returns the crtc state for the given crtc, allocating it if
260 * needed. It will also grab the relevant crtc lock to make sure that the state
261 * is consistent.
262 *
263 * Returns:
264 *
265 * Either the allocated state or the error code encoded into the pointer. When
266 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
267 * entire atomic sequence must be restarted. All other errors are fatal.
268 */
269struct drm_crtc_state *
270drm_atomic_get_crtc_state(struct drm_atomic_state *state,
271 struct drm_crtc *crtc)
272{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200273 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200274 struct drm_crtc_state *crtc_state;
275
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200276 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
277 if (crtc_state)
278 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200279
280 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
281 if (ret)
282 return ERR_PTR(ret);
283
284 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
285 if (!crtc_state)
286 return ERR_PTR(-ENOMEM);
287
288 state->crtc_states[index] = crtc_state;
289 state->crtcs[index] = crtc;
290 crtc_state->state = state;
291
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200292 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
293 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200294
295 return crtc_state;
296}
297EXPORT_SYMBOL(drm_atomic_get_crtc_state);
298
299/**
Daniel Stone819364d2015-05-26 14:36:48 +0100300 * drm_atomic_set_mode_for_crtc - set mode for CRTC
301 * @state: the CRTC whose incoming state to update
302 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
303 *
304 * Set a mode (originating from the kernel) on the desired CRTC state. Does
305 * not change any other state properties, including enable, active, or
306 * mode_changed.
307 *
308 * RETURNS:
309 * Zero on success, error code on failure. Cannot return -EDEADLK.
310 */
311int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
312 struct drm_display_mode *mode)
313{
Daniel Stone99cf4a22015-05-25 19:11:51 +0100314 struct drm_mode_modeinfo umode;
315
Daniel Stone819364d2015-05-26 14:36:48 +0100316 /* Early return for no change. */
317 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
318 return 0;
319
Markus Elfring5f911902015-11-06 12:03:46 +0100320 drm_property_unreference_blob(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100321 state->mode_blob = NULL;
322
Daniel Stone819364d2015-05-26 14:36:48 +0100323 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100324 drm_mode_convert_to_umode(&umode, mode);
325 state->mode_blob =
326 drm_property_create_blob(state->crtc->dev,
327 sizeof(umode),
328 &umode);
329 if (IS_ERR(state->mode_blob))
330 return PTR_ERR(state->mode_blob);
331
Daniel Stone819364d2015-05-26 14:36:48 +0100332 drm_mode_copy(&state->mode, mode);
333 state->enable = true;
334 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
335 mode->name, state);
336 } else {
337 memset(&state->mode, 0, sizeof(state->mode));
338 state->enable = false;
339 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
340 state);
341 }
342
343 return 0;
344}
345EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
346
Daniel Stone819364d2015-05-26 14:36:48 +0100347/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100348 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
349 * @state: the CRTC whose incoming state to update
350 * @blob: pointer to blob property to use for mode
351 *
352 * Set a mode (originating from a blob property) on the desired CRTC state.
353 * This function will take a reference on the blob property for the CRTC state,
354 * and release the reference held on the state's existing mode property, if any
355 * was set.
356 *
357 * RETURNS:
358 * Zero on success, error code on failure. Cannot return -EDEADLK.
359 */
360int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
361 struct drm_property_blob *blob)
362{
363 if (blob == state->mode_blob)
364 return 0;
365
Markus Elfring5f911902015-11-06 12:03:46 +0100366 drm_property_unreference_blob(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100367 state->mode_blob = NULL;
368
369 if (blob) {
370 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
371 drm_mode_convert_umode(&state->mode,
372 (const struct drm_mode_modeinfo *)
373 blob->data))
374 return -EINVAL;
375
376 state->mode_blob = drm_property_reference_blob(blob);
377 state->enable = true;
378 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
379 state->mode.name, state);
380 } else {
381 memset(&state->mode, 0, sizeof(state->mode));
382 state->enable = false;
383 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
384 state);
385 }
386
387 return 0;
388}
389EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
390
391/**
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000392 * drm_atomic_replace_property_blob - replace a blob property
393 * @blob: a pointer to the member blob to be replaced
394 * @new_blob: the new blob to replace with
395 * @expected_size: the expected size of the new blob
396 * @replaced: whether the blob has been replaced
397 *
398 * RETURNS:
399 * Zero on success, error code on failure
400 */
401static void
402drm_atomic_replace_property_blob(struct drm_property_blob **blob,
403 struct drm_property_blob *new_blob,
404 bool *replaced)
405{
406 struct drm_property_blob *old_blob = *blob;
407
408 if (old_blob == new_blob)
409 return;
410
411 if (old_blob)
412 drm_property_unreference_blob(old_blob);
413 if (new_blob)
414 drm_property_reference_blob(new_blob);
415 *blob = new_blob;
416 *replaced = true;
417
418 return;
419}
420
421static int
422drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
423 struct drm_property_blob **blob,
424 uint64_t blob_id,
425 ssize_t expected_size,
426 bool *replaced)
427{
428 struct drm_device *dev = crtc->dev;
429 struct drm_property_blob *new_blob = NULL;
430
431 if (blob_id != 0) {
432 new_blob = drm_property_lookup_blob(dev, blob_id);
433 if (new_blob == NULL)
434 return -EINVAL;
435 if (expected_size > 0 && expected_size != new_blob->length)
436 return -EINVAL;
437 }
438
439 drm_atomic_replace_property_blob(blob, new_blob, replaced);
440
441 return 0;
442}
443
444/**
Rob Clark40ecc692014-12-18 16:01:46 -0500445 * drm_atomic_crtc_set_property - set property on CRTC
446 * @crtc: the drm CRTC to set a property on
447 * @state: the state object to update with the new property value
448 * @property: the property to set
449 * @val: the new property value
450 *
451 * Use this instead of calling crtc->atomic_set_property directly.
452 * This function handles generic/core properties and calls out to
453 * driver's ->atomic_set_property() for driver properties. To ensure
454 * consistent behavior you must call this function rather than the
455 * driver hook directly.
456 *
457 * RETURNS:
458 * Zero on success, error code on failure
459 */
460int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
461 struct drm_crtc_state *state, struct drm_property *property,
462 uint64_t val)
463{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100464 struct drm_device *dev = crtc->dev;
465 struct drm_mode_config *config = &dev->mode_config;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000466 bool replaced = false;
Daniel Stone955f3c32015-05-25 19:11:52 +0100467 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100468
Daniel Stone27798362015-03-19 04:33:26 +0000469 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100470 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100471 else if (property == config->prop_mode_id) {
472 struct drm_property_blob *mode =
473 drm_property_lookup_blob(dev, val);
474 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Markus Elfring5f911902015-11-06 12:03:46 +0100475 drm_property_unreference_blob(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100476 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000477 } else if (property == config->degamma_lut_property) {
478 ret = drm_atomic_replace_property_blob_from_id(crtc,
479 &state->degamma_lut,
480 val,
481 -1,
482 &replaced);
483 state->color_mgmt_changed = replaced;
484 return ret;
485 } else if (property == config->ctm_property) {
486 ret = drm_atomic_replace_property_blob_from_id(crtc,
487 &state->ctm,
488 val,
489 sizeof(struct drm_color_ctm),
490 &replaced);
491 state->color_mgmt_changed = replaced;
492 return ret;
493 } else if (property == config->gamma_lut_property) {
494 ret = drm_atomic_replace_property_blob_from_id(crtc,
495 &state->gamma_lut,
496 val,
497 -1,
498 &replaced);
499 state->color_mgmt_changed = replaced;
500 return ret;
501 } else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500502 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000503 else
504 return -EINVAL;
505
506 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500507}
508EXPORT_SYMBOL(drm_atomic_crtc_set_property);
509
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100510/**
511 * drm_atomic_crtc_get_property - get property value from CRTC state
512 * @crtc: the drm CRTC to set a property on
513 * @state: the state object to get the property value from
514 * @property: the property to set
515 * @val: return location for the property value
516 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500517 * This function handles generic/core properties and calls out to
518 * driver's ->atomic_get_property() for driver properties. To ensure
519 * consistent behavior you must call this function rather than the
520 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100521 *
522 * RETURNS:
523 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500524 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700525static int
526drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500527 const struct drm_crtc_state *state,
528 struct drm_property *property, uint64_t *val)
529{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000530 struct drm_device *dev = crtc->dev;
531 struct drm_mode_config *config = &dev->mode_config;
532
533 if (property == config->prop_active)
534 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100535 else if (property == config->prop_mode_id)
536 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000537 else if (property == config->degamma_lut_property)
538 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
539 else if (property == config->ctm_property)
540 *val = (state->ctm) ? state->ctm->base.id : 0;
541 else if (property == config->gamma_lut_property)
542 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000543 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500544 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000545 else
546 return -EINVAL;
547
548 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500549}
Rob Clarkac9c9252014-12-18 16:01:47 -0500550
551/**
Rob Clark5e743732014-12-18 16:01:51 -0500552 * drm_atomic_crtc_check - check crtc state
553 * @crtc: crtc to check
554 * @state: crtc state to check
555 *
556 * Provides core sanity checks for crtc state.
557 *
558 * RETURNS:
559 * Zero on success, error code on failure
560 */
561static int drm_atomic_crtc_check(struct drm_crtc *crtc,
562 struct drm_crtc_state *state)
563{
564 /* NOTE: we explicitly don't enforce constraints such as primary
565 * layer covering entire screen, since that is something we want
566 * to allow (on hw that supports it). For hw that does not, it
567 * should be checked in driver's crtc->atomic_check() vfunc.
568 *
569 * TODO: Add generic modeset state checks once we support those.
570 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100571
572 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200573 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
574 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100575 return -EINVAL;
576 }
577
Daniel Stone99cf4a22015-05-25 19:11:51 +0100578 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
579 * as this is a kernel-internal detail that userspace should never
580 * be able to trigger. */
581 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
582 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200583 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
584 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100585 return -EINVAL;
586 }
587
588 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
589 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200590 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
591 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100592 return -EINVAL;
593 }
594
Daniel Vetter4cba6852015-12-08 09:49:20 +0100595 /*
596 * Reject event generation for when a CRTC is off and stays off.
597 * It wouldn't be hard to implement this, but userspace has a track
598 * record of happily burning through 100% cpu (or worse, crash) when the
599 * display pipe is suspended. To avoid all that fun just reject updates
600 * that ask for events since likely that indicates a bug in the
601 * compositor's drawing loop. This is consistent with the vblank IOCTL
602 * and legacy page_flip IOCTL which also reject service on a disabled
603 * pipe.
604 */
605 if (state->event && !state->active && !crtc->state->active) {
606 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
607 crtc->base.id);
608 return -EINVAL;
609 }
610
Rob Clark5e743732014-12-18 16:01:51 -0500611 return 0;
612}
613
614/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200615 * drm_atomic_get_plane_state - get plane state
616 * @state: global atomic state object
617 * @plane: plane to get state object for
618 *
619 * This function returns the plane state for the given plane, allocating it if
620 * needed. It will also grab the relevant plane lock to make sure that the state
621 * is consistent.
622 *
623 * Returns:
624 *
625 * Either the allocated state or the error code encoded into the pointer. When
626 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
627 * entire atomic sequence must be restarted. All other errors are fatal.
628 */
629struct drm_plane_state *
630drm_atomic_get_plane_state(struct drm_atomic_state *state,
631 struct drm_plane *plane)
632{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200633 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200634 struct drm_plane_state *plane_state;
635
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200636 plane_state = drm_atomic_get_existing_plane_state(state, plane);
637 if (plane_state)
638 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200639
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100640 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200641 if (ret)
642 return ERR_PTR(ret);
643
644 plane_state = plane->funcs->atomic_duplicate_state(plane);
645 if (!plane_state)
646 return ERR_PTR(-ENOMEM);
647
648 state->plane_states[index] = plane_state;
649 state->planes[index] = plane;
650 plane_state->state = state;
651
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200652 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
653 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200654
655 if (plane_state->crtc) {
656 struct drm_crtc_state *crtc_state;
657
658 crtc_state = drm_atomic_get_crtc_state(state,
659 plane_state->crtc);
660 if (IS_ERR(crtc_state))
661 return ERR_CAST(crtc_state);
662 }
663
664 return plane_state;
665}
666EXPORT_SYMBOL(drm_atomic_get_plane_state);
667
668/**
Rob Clark40ecc692014-12-18 16:01:46 -0500669 * drm_atomic_plane_set_property - set property on plane
670 * @plane: the drm plane to set a property on
671 * @state: the state object to update with the new property value
672 * @property: the property to set
673 * @val: the new property value
674 *
675 * Use this instead of calling plane->atomic_set_property directly.
676 * This function handles generic/core properties and calls out to
677 * driver's ->atomic_set_property() for driver properties. To ensure
678 * consistent behavior you must call this function rather than the
679 * driver hook directly.
680 *
681 * RETURNS:
682 * Zero on success, error code on failure
683 */
684int drm_atomic_plane_set_property(struct drm_plane *plane,
685 struct drm_plane_state *state, struct drm_property *property,
686 uint64_t val)
687{
Rob Clark6b4959f2014-12-18 16:01:53 -0500688 struct drm_device *dev = plane->dev;
689 struct drm_mode_config *config = &dev->mode_config;
690
691 if (property == config->prop_fb_id) {
692 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
693 drm_atomic_set_fb_for_plane(state, fb);
694 if (fb)
695 drm_framebuffer_unreference(fb);
696 } else if (property == config->prop_crtc_id) {
697 struct drm_crtc *crtc = drm_crtc_find(dev, val);
698 return drm_atomic_set_crtc_for_plane(state, crtc);
699 } else if (property == config->prop_crtc_x) {
700 state->crtc_x = U642I64(val);
701 } else if (property == config->prop_crtc_y) {
702 state->crtc_y = U642I64(val);
703 } else if (property == config->prop_crtc_w) {
704 state->crtc_w = val;
705 } else if (property == config->prop_crtc_h) {
706 state->crtc_h = val;
707 } else if (property == config->prop_src_x) {
708 state->src_x = val;
709 } else if (property == config->prop_src_y) {
710 state->src_y = val;
711 } else if (property == config->prop_src_w) {
712 state->src_w = val;
713 } else if (property == config->prop_src_h) {
714 state->src_h = val;
Matt Roper1da30622015-01-21 16:35:40 -0800715 } else if (property == config->rotation_property) {
716 state->rotation = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500717 } else if (plane->funcs->atomic_set_property) {
718 return plane->funcs->atomic_set_property(plane, state,
719 property, val);
720 } else {
721 return -EINVAL;
722 }
723
724 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500725}
726EXPORT_SYMBOL(drm_atomic_plane_set_property);
727
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100728/**
729 * drm_atomic_plane_get_property - get property value from plane state
730 * @plane: the drm plane to set a property on
731 * @state: the state object to get the property value from
732 * @property: the property to set
733 * @val: return location for the property value
734 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500735 * This function handles generic/core properties and calls out to
736 * driver's ->atomic_get_property() for driver properties. To ensure
737 * consistent behavior you must call this function rather than the
738 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100739 *
740 * RETURNS:
741 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500742 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100743static int
744drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500745 const struct drm_plane_state *state,
746 struct drm_property *property, uint64_t *val)
747{
Rob Clark6b4959f2014-12-18 16:01:53 -0500748 struct drm_device *dev = plane->dev;
749 struct drm_mode_config *config = &dev->mode_config;
750
751 if (property == config->prop_fb_id) {
752 *val = (state->fb) ? state->fb->base.id : 0;
753 } else if (property == config->prop_crtc_id) {
754 *val = (state->crtc) ? state->crtc->base.id : 0;
755 } else if (property == config->prop_crtc_x) {
756 *val = I642U64(state->crtc_x);
757 } else if (property == config->prop_crtc_y) {
758 *val = I642U64(state->crtc_y);
759 } else if (property == config->prop_crtc_w) {
760 *val = state->crtc_w;
761 } else if (property == config->prop_crtc_h) {
762 *val = state->crtc_h;
763 } else if (property == config->prop_src_x) {
764 *val = state->src_x;
765 } else if (property == config->prop_src_y) {
766 *val = state->src_y;
767 } else if (property == config->prop_src_w) {
768 *val = state->src_w;
769 } else if (property == config->prop_src_h) {
770 *val = state->src_h;
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000771 } else if (property == config->rotation_property) {
772 *val = state->rotation;
Rob Clark6b4959f2014-12-18 16:01:53 -0500773 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500774 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500775 } else {
776 return -EINVAL;
777 }
778
779 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500780}
Rob Clarkac9c9252014-12-18 16:01:47 -0500781
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200782static bool
783plane_switching_crtc(struct drm_atomic_state *state,
784 struct drm_plane *plane,
785 struct drm_plane_state *plane_state)
786{
787 if (!plane->state->crtc || !plane_state->crtc)
788 return false;
789
790 if (plane->state->crtc == plane_state->crtc)
791 return false;
792
793 /* This could be refined, but currently there's no helper or driver code
794 * to implement direct switching of active planes nor userspace to take
795 * advantage of more direct plane switching without the intermediate
796 * full OFF state.
797 */
798 return true;
799}
800
Rob Clarkac9c9252014-12-18 16:01:47 -0500801/**
Rob Clark5e743732014-12-18 16:01:51 -0500802 * drm_atomic_plane_check - check plane state
803 * @plane: plane to check
804 * @state: plane state to check
805 *
806 * Provides core sanity checks for plane state.
807 *
808 * RETURNS:
809 * Zero on success, error code on failure
810 */
811static int drm_atomic_plane_check(struct drm_plane *plane,
812 struct drm_plane_state *state)
813{
814 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200815 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500816
817 /* either *both* CRTC and FB must be set, or neither */
818 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100819 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500820 return -EINVAL;
821 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100822 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500823 return -EINVAL;
824 }
825
826 /* if disabled, we don't care about the rest of the state: */
827 if (!state->crtc)
828 return 0;
829
830 /* Check whether this plane is usable on this CRTC */
831 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100832 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500833 return -EINVAL;
834 }
835
836 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +0200837 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
838 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100839 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
840 drm_get_format_name(state->fb->pixel_format));
Laurent Pinchartead86102015-03-05 02:25:43 +0200841 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500842 }
843
844 /* Give drivers some help against integer overflows */
845 if (state->crtc_w > INT_MAX ||
846 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
847 state->crtc_h > INT_MAX ||
848 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100849 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
850 state->crtc_w, state->crtc_h,
851 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500852 return -ERANGE;
853 }
854
855 fb_width = state->fb->width << 16;
856 fb_height = state->fb->height << 16;
857
858 /* Make sure source coordinates are inside the fb. */
859 if (state->src_w > fb_width ||
860 state->src_x > fb_width - state->src_w ||
861 state->src_h > fb_height ||
862 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100863 DRM_DEBUG_ATOMIC("Invalid source coordinates "
864 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
865 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
866 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
867 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
868 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500869 return -ENOSPC;
870 }
871
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200872 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200873 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
874 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200875 return -EINVAL;
876 }
877
Rob Clark5e743732014-12-18 16:01:51 -0500878 return 0;
879}
880
881/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200882 * drm_atomic_get_connector_state - get connector state
883 * @state: global atomic state object
884 * @connector: connector to get state object for
885 *
886 * This function returns the connector state for the given connector,
887 * allocating it if needed. It will also grab the relevant connector lock to
888 * make sure that the state is consistent.
889 *
890 * Returns:
891 *
892 * Either the allocated state or the error code encoded into the pointer. When
893 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
894 * entire atomic sequence must be restarted. All other errors are fatal.
895 */
896struct drm_connector_state *
897drm_atomic_get_connector_state(struct drm_atomic_state *state,
898 struct drm_connector *connector)
899{
900 int ret, index;
901 struct drm_mode_config *config = &connector->dev->mode_config;
902 struct drm_connector_state *connector_state;
903
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100904 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
905 if (ret)
906 return ERR_PTR(ret);
907
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200908 index = drm_connector_index(connector);
909
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100910 /*
911 * Construction of atomic state updates can race with a connector
912 * hot-add which might overflow. In this case flip the table and just
913 * restart the entire ioctl - no one is fast enough to livelock a cpu
914 * with physical hotplug events anyway.
915 *
916 * Note that we only grab the indexes once we have the right lock to
917 * prevent hotplug/unplugging of connectors. So removal is no problem,
918 * at most the array is a bit too large.
919 */
920 if (index >= state->num_connector) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100921 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
Daniel Vetterfc2d2bc2014-11-20 09:53:35 +0100922 return ERR_PTR(-EAGAIN);
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100923 }
924
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200925 if (state->connector_states[index])
926 return state->connector_states[index];
927
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200928 connector_state = connector->funcs->atomic_duplicate_state(connector);
929 if (!connector_state)
930 return ERR_PTR(-ENOMEM);
931
932 state->connector_states[index] = connector_state;
933 state->connectors[index] = connector;
934 connector_state->state = state;
935
Daniel Vetter17a38d92015-02-22 12:24:16 +0100936 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
937 connector->base.id, connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200938
939 if (connector_state->crtc) {
940 struct drm_crtc_state *crtc_state;
941
942 crtc_state = drm_atomic_get_crtc_state(state,
943 connector_state->crtc);
944 if (IS_ERR(crtc_state))
945 return ERR_CAST(crtc_state);
946 }
947
948 return connector_state;
949}
950EXPORT_SYMBOL(drm_atomic_get_connector_state);
951
952/**
Rob Clark40ecc692014-12-18 16:01:46 -0500953 * drm_atomic_connector_set_property - set property on connector.
954 * @connector: the drm connector to set a property on
955 * @state: the state object to update with the new property value
956 * @property: the property to set
957 * @val: the new property value
958 *
959 * Use this instead of calling connector->atomic_set_property directly.
960 * This function handles generic/core properties and calls out to
961 * driver's ->atomic_set_property() for driver properties. To ensure
962 * consistent behavior you must call this function rather than the
963 * driver hook directly.
964 *
965 * RETURNS:
966 * Zero on success, error code on failure
967 */
968int drm_atomic_connector_set_property(struct drm_connector *connector,
969 struct drm_connector_state *state, struct drm_property *property,
970 uint64_t val)
971{
972 struct drm_device *dev = connector->dev;
973 struct drm_mode_config *config = &dev->mode_config;
974
Rob Clarkae16c592014-12-18 16:01:54 -0500975 if (property == config->prop_crtc_id) {
976 struct drm_crtc *crtc = drm_crtc_find(dev, val);
977 return drm_atomic_set_crtc_for_connector(state, crtc);
978 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -0500979 /* setting DPMS property requires special handling, which
980 * is done in legacy setprop path for us. Disallow (for
981 * now?) atomic writes to DPMS property:
982 */
983 return -EINVAL;
984 } else if (connector->funcs->atomic_set_property) {
985 return connector->funcs->atomic_set_property(connector,
986 state, property, val);
987 } else {
988 return -EINVAL;
989 }
990}
991EXPORT_SYMBOL(drm_atomic_connector_set_property);
992
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100993/**
994 * drm_atomic_connector_get_property - get property value from connector state
995 * @connector: the drm connector to set a property on
996 * @state: the state object to get the property value from
997 * @property: the property to set
998 * @val: return location for the property value
999 *
Rob Clarkac9c9252014-12-18 16:01:47 -05001000 * This function handles generic/core properties and calls out to
1001 * driver's ->atomic_get_property() for driver properties. To ensure
1002 * consistent behavior you must call this function rather than the
1003 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001004 *
1005 * RETURNS:
1006 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -05001007 */
Daniel Vettera97df1c2014-12-18 22:49:02 +01001008static int
1009drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -05001010 const struct drm_connector_state *state,
1011 struct drm_property *property, uint64_t *val)
1012{
1013 struct drm_device *dev = connector->dev;
1014 struct drm_mode_config *config = &dev->mode_config;
1015
Rob Clarkae16c592014-12-18 16:01:54 -05001016 if (property == config->prop_crtc_id) {
1017 *val = (state->crtc) ? state->crtc->base.id : 0;
1018 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -05001019 *val = connector->dpms;
1020 } else if (connector->funcs->atomic_get_property) {
1021 return connector->funcs->atomic_get_property(connector,
1022 state, property, val);
1023 } else {
1024 return -EINVAL;
1025 }
1026
1027 return 0;
1028}
Rob Clarkac9c9252014-12-18 16:01:47 -05001029
Rob Clark88a48e22014-12-18 16:01:50 -05001030int drm_atomic_get_property(struct drm_mode_object *obj,
1031 struct drm_property *property, uint64_t *val)
1032{
1033 struct drm_device *dev = property->dev;
1034 int ret;
1035
1036 switch (obj->type) {
1037 case DRM_MODE_OBJECT_CONNECTOR: {
1038 struct drm_connector *connector = obj_to_connector(obj);
1039 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1040 ret = drm_atomic_connector_get_property(connector,
1041 connector->state, property, val);
1042 break;
1043 }
1044 case DRM_MODE_OBJECT_CRTC: {
1045 struct drm_crtc *crtc = obj_to_crtc(obj);
1046 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1047 ret = drm_atomic_crtc_get_property(crtc,
1048 crtc->state, property, val);
1049 break;
1050 }
1051 case DRM_MODE_OBJECT_PLANE: {
1052 struct drm_plane *plane = obj_to_plane(obj);
1053 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1054 ret = drm_atomic_plane_get_property(plane,
1055 plane->state, property, val);
1056 break;
1057 }
1058 default:
1059 ret = -EINVAL;
1060 break;
1061 }
1062
1063 return ret;
1064}
1065
1066/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001067 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001068 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001069 * @crtc: crtc to use for the plane
1070 *
1071 * Changing the assigned crtc for a plane requires us to grab the lock and state
1072 * for the new crtc, as needed. This function takes care of all these details
1073 * besides updating the pointer in the state object itself.
1074 *
1075 * Returns:
1076 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1077 * then the w/w mutex code has detected a deadlock and the entire atomic
1078 * sequence must be restarted. All other errors are fatal.
1079 */
1080int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001081drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1082 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001083{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001084 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001085 struct drm_crtc_state *crtc_state;
1086
Rob Clark6ddd3882014-11-21 15:28:31 -05001087 if (plane_state->crtc) {
1088 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1089 plane_state->crtc);
1090 if (WARN_ON(IS_ERR(crtc_state)))
1091 return PTR_ERR(crtc_state);
1092
1093 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1094 }
1095
1096 plane_state->crtc = crtc;
1097
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001098 if (crtc) {
1099 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1100 crtc);
1101 if (IS_ERR(crtc_state))
1102 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -05001103 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001104 }
1105
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001106 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001107 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1108 plane_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001109 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001110 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1111 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001112
1113 return 0;
1114}
1115EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1116
1117/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001118 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001119 * @plane_state: atomic state object for the plane
1120 * @fb: fb to use for the plane
1121 *
1122 * Changing the assigned framebuffer for a plane requires us to grab a reference
1123 * to the new fb and drop the reference to the old fb, if there is one. This
1124 * function takes care of all these details besides updating the pointer in the
1125 * state object itself.
1126 */
1127void
1128drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1129 struct drm_framebuffer *fb)
1130{
1131 if (plane_state->fb)
1132 drm_framebuffer_unreference(plane_state->fb);
1133 if (fb)
1134 drm_framebuffer_reference(fb);
1135 plane_state->fb = fb;
1136
1137 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001138 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1139 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001140 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001141 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1142 plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001143}
1144EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1145
1146/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001147 * drm_atomic_set_crtc_for_connector - set crtc for connector
1148 * @conn_state: atomic state object for the connector
1149 * @crtc: crtc to use for the connector
1150 *
1151 * Changing the assigned crtc for a connector requires us to grab the lock and
1152 * state for the new crtc, as needed. This function takes care of all these
1153 * details besides updating the pointer in the state object itself.
1154 *
1155 * Returns:
1156 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1157 * then the w/w mutex code has detected a deadlock and the entire atomic
1158 * sequence must be restarted. All other errors are fatal.
1159 */
1160int
1161drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1162 struct drm_crtc *crtc)
1163{
1164 struct drm_crtc_state *crtc_state;
1165
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001166 if (conn_state->crtc && conn_state->crtc != crtc) {
1167 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1168 conn_state->crtc);
1169
1170 crtc_state->connector_mask &=
1171 ~(1 << drm_connector_index(conn_state->connector));
1172 }
1173
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001174 if (crtc) {
1175 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1176 if (IS_ERR(crtc_state))
1177 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001178
1179 crtc_state->connector_mask |=
1180 1 << drm_connector_index(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001181 }
1182
1183 conn_state->crtc = crtc;
1184
1185 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001186 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1187 conn_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001188 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001189 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1190 conn_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001191
1192 return 0;
1193}
1194EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1195
1196/**
1197 * drm_atomic_add_affected_connectors - add connectors for crtc
1198 * @state: atomic state
1199 * @crtc: DRM crtc
1200 *
1201 * This function walks the current configuration and adds all connectors
1202 * currently using @crtc to the atomic configuration @state. Note that this
1203 * function must acquire the connection mutex. This can potentially cause
1204 * unneeded seralization if the update is just for the planes on one crtc. Hence
1205 * drivers and helpers should only call this when really needed (e.g. when a
1206 * full modeset needs to happen due to some change).
1207 *
1208 * Returns:
1209 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1210 * then the w/w mutex code has detected a deadlock and the entire atomic
1211 * sequence must be restarted. All other errors are fatal.
1212 */
1213int
1214drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1215 struct drm_crtc *crtc)
1216{
1217 struct drm_mode_config *config = &state->dev->mode_config;
1218 struct drm_connector *connector;
1219 struct drm_connector_state *conn_state;
1220 int ret;
1221
1222 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1223 if (ret)
1224 return ret;
1225
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001226 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1227 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001228
1229 /*
1230 * Changed connectors are already in @state, so only need to look at the
1231 * current configuration.
1232 */
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001233 drm_for_each_connector(connector, state->dev) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001234 if (connector->state->crtc != crtc)
1235 continue;
1236
1237 conn_state = drm_atomic_get_connector_state(state, connector);
1238 if (IS_ERR(conn_state))
1239 return PTR_ERR(conn_state);
1240 }
1241
1242 return 0;
1243}
1244EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1245
1246/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001247 * drm_atomic_add_affected_planes - add planes for crtc
1248 * @state: atomic state
1249 * @crtc: DRM crtc
1250 *
1251 * This function walks the current configuration and adds all planes
1252 * currently used by @crtc to the atomic configuration @state. This is useful
1253 * when an atomic commit also needs to check all currently enabled plane on
1254 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1255 * to avoid special code to force-enable all planes.
1256 *
1257 * Since acquiring a plane state will always also acquire the w/w mutex of the
1258 * current CRTC for that plane (if there is any) adding all the plane states for
1259 * a CRTC will not reduce parallism of atomic updates.
1260 *
1261 * Returns:
1262 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1263 * then the w/w mutex code has detected a deadlock and the entire atomic
1264 * sequence must be restarted. All other errors are fatal.
1265 */
1266int
1267drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1268 struct drm_crtc *crtc)
1269{
1270 struct drm_plane *plane;
1271
1272 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1273
1274 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1275 struct drm_plane_state *plane_state =
1276 drm_atomic_get_plane_state(state, plane);
1277
1278 if (IS_ERR(plane_state))
1279 return PTR_ERR(plane_state);
1280 }
1281 return 0;
1282}
1283EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1284
1285/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001286 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1287 * @state: atomic state
1288 *
1289 * This function should be used by legacy entry points which don't understand
1290 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +08001291 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001292 */
1293void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1294{
1295 int ret;
1296
1297retry:
1298 drm_modeset_backoff(state->acquire_ctx);
1299
Thierry Reding06eaae42015-12-02 17:50:03 +01001300 ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001301 if (ret)
1302 goto retry;
1303}
1304EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1305
1306/**
1307 * drm_atomic_check_only - check whether a given config would work
1308 * @state: atomic configuration to check
1309 *
1310 * Note that this function can return -EDEADLK if the driver needed to acquire
1311 * more locks but encountered a deadlock. The caller must then do the usual w/w
1312 * backoff dance and restart. All other errors are fatal.
1313 *
1314 * Returns:
1315 * 0 on success, negative error code on failure.
1316 */
1317int drm_atomic_check_only(struct drm_atomic_state *state)
1318{
Rob Clark5e743732014-12-18 16:01:51 -05001319 struct drm_device *dev = state->dev;
1320 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001321 struct drm_plane *plane;
1322 struct drm_plane_state *plane_state;
1323 struct drm_crtc *crtc;
1324 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001325 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001326
Daniel Vetter17a38d92015-02-22 12:24:16 +01001327 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001328
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001329 for_each_plane_in_state(state, plane, plane_state, i) {
1330 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001331 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001332 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1333 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001334 return ret;
1335 }
1336 }
1337
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001338 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1339 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001340 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001341 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1342 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001343 return ret;
1344 }
1345 }
1346
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001347 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001348 ret = config->funcs->atomic_check(state->dev, state);
1349
Rob Clarkd34f20d2014-12-18 16:01:56 -05001350 if (!state->allow_modeset) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001351 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001352 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001353 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1354 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001355 return -EINVAL;
1356 }
1357 }
1358 }
1359
Rob Clark5e743732014-12-18 16:01:51 -05001360 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001361}
1362EXPORT_SYMBOL(drm_atomic_check_only);
1363
1364/**
1365 * drm_atomic_commit - commit configuration atomically
1366 * @state: atomic configuration to check
1367 *
1368 * Note that this function can return -EDEADLK if the driver needed to acquire
1369 * more locks but encountered a deadlock. The caller must then do the usual w/w
1370 * backoff dance and restart. All other errors are fatal.
1371 *
1372 * Also note that on successful execution ownership of @state is transferred
1373 * from the caller of this function to the function itself. The caller must not
1374 * free or in any other way access @state. If the function fails then the caller
1375 * must clean up @state itself.
1376 *
1377 * Returns:
1378 * 0 on success, negative error code on failure.
1379 */
1380int drm_atomic_commit(struct drm_atomic_state *state)
1381{
1382 struct drm_mode_config *config = &state->dev->mode_config;
1383 int ret;
1384
1385 ret = drm_atomic_check_only(state);
1386 if (ret)
1387 return ret;
1388
Daniel Vetter17a38d92015-02-22 12:24:16 +01001389 DRM_DEBUG_ATOMIC("commiting %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001390
1391 return config->funcs->atomic_commit(state->dev, state, false);
1392}
1393EXPORT_SYMBOL(drm_atomic_commit);
1394
1395/**
1396 * drm_atomic_async_commit - atomic&async configuration commit
1397 * @state: atomic configuration to check
1398 *
1399 * Note that this function can return -EDEADLK if the driver needed to acquire
1400 * more locks but encountered a deadlock. The caller must then do the usual w/w
1401 * backoff dance and restart. All other errors are fatal.
1402 *
1403 * Also note that on successful execution ownership of @state is transferred
1404 * from the caller of this function to the function itself. The caller must not
1405 * free or in any other way access @state. If the function fails then the caller
1406 * must clean up @state itself.
1407 *
1408 * Returns:
1409 * 0 on success, negative error code on failure.
1410 */
1411int drm_atomic_async_commit(struct drm_atomic_state *state)
1412{
1413 struct drm_mode_config *config = &state->dev->mode_config;
1414 int ret;
1415
1416 ret = drm_atomic_check_only(state);
1417 if (ret)
1418 return ret;
1419
Daniel Vetter17a38d92015-02-22 12:24:16 +01001420 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001421
1422 return config->funcs->atomic_commit(state->dev, state, true);
1423}
1424EXPORT_SYMBOL(drm_atomic_async_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001425
1426/*
1427 * The big monstor ioctl
1428 */
1429
1430static struct drm_pending_vblank_event *create_vblank_event(
1431 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1432{
1433 struct drm_pending_vblank_event *e = NULL;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001434 int ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001435
1436 e = kzalloc(sizeof *e, GFP_KERNEL);
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001437 if (!e)
1438 return NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001439
1440 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001441 e->event.base.length = sizeof(e->event);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001442 e->event.user_data = user_data;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001443
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001444 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
1445 if (ret) {
1446 kfree(e);
1447 return NULL;
1448 }
1449
Rob Clarkd34f20d2014-12-18 16:01:56 -05001450 return e;
1451}
1452
Rob Clarkd34f20d2014-12-18 16:01:56 -05001453static int atomic_set_prop(struct drm_atomic_state *state,
1454 struct drm_mode_object *obj, struct drm_property *prop,
1455 uint64_t prop_value)
1456{
1457 struct drm_mode_object *ref;
1458 int ret;
1459
1460 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1461 return -EINVAL;
1462
1463 switch (obj->type) {
1464 case DRM_MODE_OBJECT_CONNECTOR: {
1465 struct drm_connector *connector = obj_to_connector(obj);
1466 struct drm_connector_state *connector_state;
1467
1468 connector_state = drm_atomic_get_connector_state(state, connector);
1469 if (IS_ERR(connector_state)) {
1470 ret = PTR_ERR(connector_state);
1471 break;
1472 }
1473
1474 ret = drm_atomic_connector_set_property(connector,
1475 connector_state, prop, prop_value);
1476 break;
1477 }
1478 case DRM_MODE_OBJECT_CRTC: {
1479 struct drm_crtc *crtc = obj_to_crtc(obj);
1480 struct drm_crtc_state *crtc_state;
1481
1482 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1483 if (IS_ERR(crtc_state)) {
1484 ret = PTR_ERR(crtc_state);
1485 break;
1486 }
1487
1488 ret = drm_atomic_crtc_set_property(crtc,
1489 crtc_state, prop, prop_value);
1490 break;
1491 }
1492 case DRM_MODE_OBJECT_PLANE: {
1493 struct drm_plane *plane = obj_to_plane(obj);
1494 struct drm_plane_state *plane_state;
1495
1496 plane_state = drm_atomic_get_plane_state(state, plane);
1497 if (IS_ERR(plane_state)) {
1498 ret = PTR_ERR(plane_state);
1499 break;
1500 }
1501
1502 ret = drm_atomic_plane_set_property(plane,
1503 plane_state, prop, prop_value);
1504 break;
1505 }
1506 default:
1507 ret = -EINVAL;
1508 break;
1509 }
1510
1511 drm_property_change_valid_put(prop, ref);
1512 return ret;
1513}
1514
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001515/**
Maarten Lankhorst9744bf42015-11-24 10:34:34 +01001516 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001517 *
1518 * @dev: drm device to check.
1519 * @plane_mask: plane mask for planes that were updated.
1520 * @ret: return value, can be -EDEADLK for a retry.
1521 *
1522 * Before doing an update plane->old_fb is set to plane->fb,
1523 * but before dropping the locks old_fb needs to be set to NULL
1524 * and plane->fb updated. This is a common operation for each
1525 * atomic update, so this call is split off as a helper.
1526 */
1527void drm_atomic_clean_old_fb(struct drm_device *dev,
1528 unsigned plane_mask,
1529 int ret)
1530{
1531 struct drm_plane *plane;
1532
1533 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1534 * locks (ie. while it is still safe to deref plane->state). We
1535 * need to do this here because the driver entry points cannot
1536 * distinguish between legacy and atomic ioctls.
1537 */
1538 drm_for_each_plane_mask(plane, dev, plane_mask) {
1539 if (ret == 0) {
1540 struct drm_framebuffer *new_fb = plane->state->fb;
1541 if (new_fb)
1542 drm_framebuffer_reference(new_fb);
1543 plane->fb = new_fb;
1544 plane->crtc = plane->state->crtc;
1545
1546 if (plane->old_fb)
1547 drm_framebuffer_unreference(plane->old_fb);
1548 }
1549 plane->old_fb = NULL;
1550 }
1551}
1552EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1553
Rob Clarkd34f20d2014-12-18 16:01:56 -05001554int drm_mode_atomic_ioctl(struct drm_device *dev,
1555 void *data, struct drm_file *file_priv)
1556{
1557 struct drm_mode_atomic *arg = data;
1558 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1559 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1560 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1561 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1562 unsigned int copied_objs, copied_props;
1563 struct drm_atomic_state *state;
1564 struct drm_modeset_acquire_ctx ctx;
1565 struct drm_plane *plane;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001566 struct drm_crtc *crtc;
1567 struct drm_crtc_state *crtc_state;
Maarten Lankhorst45723722015-11-11 11:29:08 +01001568 unsigned plane_mask;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001569 int ret = 0;
1570 unsigned int i, j;
1571
1572 /* disallow for drivers not supporting atomic: */
1573 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1574 return -EINVAL;
1575
1576 /* disallow for userspace that has not enabled atomic cap (even
1577 * though this may be a bit overkill, since legacy userspace
1578 * wouldn't know how to call this ioctl)
1579 */
1580 if (!file_priv->atomic)
1581 return -EINVAL;
1582
1583 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1584 return -EINVAL;
1585
1586 if (arg->reserved)
1587 return -EINVAL;
1588
1589 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1590 !dev->mode_config.async_page_flip)
1591 return -EINVAL;
1592
1593 /* can't test and expect an event at the same time. */
1594 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1595 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1596 return -EINVAL;
1597
1598 drm_modeset_acquire_init(&ctx, 0);
1599
1600 state = drm_atomic_state_alloc(dev);
1601 if (!state)
1602 return -ENOMEM;
1603
1604 state->acquire_ctx = &ctx;
1605 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1606
1607retry:
Maarten Lankhorst45723722015-11-11 11:29:08 +01001608 plane_mask = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001609 copied_objs = 0;
1610 copied_props = 0;
1611
1612 for (i = 0; i < arg->count_objs; i++) {
1613 uint32_t obj_id, count_props;
1614 struct drm_mode_object *obj;
1615
1616 if (get_user(obj_id, objs_ptr + copied_objs)) {
1617 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001618 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001619 }
1620
1621 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1622 if (!obj || !obj->properties) {
1623 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001624 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001625 }
1626
Rob Clarkd34f20d2014-12-18 16:01:56 -05001627 if (get_user(count_props, count_props_ptr + copied_objs)) {
1628 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001629 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001630 }
1631
1632 copied_objs++;
1633
1634 for (j = 0; j < count_props; j++) {
1635 uint32_t prop_id;
1636 uint64_t prop_value;
1637 struct drm_property *prop;
1638
1639 if (get_user(prop_id, props_ptr + copied_props)) {
1640 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001641 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001642 }
1643
1644 prop = drm_property_find(dev, prop_id);
1645 if (!prop) {
1646 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001647 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001648 }
1649
Guenter Roeck42c58142015-01-12 21:12:17 -08001650 if (copy_from_user(&prop_value,
1651 prop_values_ptr + copied_props,
1652 sizeof(prop_value))) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001653 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001654 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001655 }
1656
1657 ret = atomic_set_prop(state, obj, prop, prop_value);
1658 if (ret)
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001659 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001660
1661 copied_props++;
1662 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02001663
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001664 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1665 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02001666 plane = obj_to_plane(obj);
1667 plane_mask |= (1 << drm_plane_index(plane));
1668 plane->old_fb = plane->fb;
1669 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05001670 }
1671
1672 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001673 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001674 struct drm_pending_vblank_event *e;
1675
Rob Clarkd34f20d2014-12-18 16:01:56 -05001676 e = create_vblank_event(dev, file_priv, arg->user_data);
1677 if (!e) {
1678 ret = -ENOMEM;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001679 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001680 }
1681
1682 crtc_state->event = e;
1683 }
1684 }
1685
1686 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001687 /*
1688 * Unlike commit, check_only does not clean up state.
1689 * Below we call drm_atomic_state_free for it.
1690 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05001691 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001692 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1693 ret = drm_atomic_async_commit(state);
1694 } else {
1695 ret = drm_atomic_commit(state);
1696 }
1697
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001698out:
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001699 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001700
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001701 if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1702 /*
1703 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1704 * if they weren't, this code should be called on success
1705 * for TEST_ONLY too.
1706 */
1707
1708 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1709 if (!crtc_state->event)
1710 continue;
1711
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001712 drm_event_cancel_free(dev, &crtc_state->event->base);
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001713 }
1714 }
1715
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001716 if (ret == -EDEADLK) {
1717 drm_atomic_state_clear(state);
1718 drm_modeset_backoff(&ctx);
1719 goto retry;
1720 }
1721
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001722 if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001723 drm_atomic_state_free(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001724
1725 drm_modeset_drop_locks(&ctx);
1726 drm_modeset_acquire_fini(&ctx);
1727
1728 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001729}