blob: c6277a4a1f2f235e43fcfd569944d46dc7133119 [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>
31#include <drm/drm_plane_helper.h>
32
33static void kfree_state(struct drm_atomic_state *state)
34{
35 kfree(state->connectors);
36 kfree(state->connector_states);
37 kfree(state->crtcs);
38 kfree(state->crtc_states);
39 kfree(state->planes);
40 kfree(state->plane_states);
41 kfree(state);
42}
43
44/**
45 * drm_atomic_state_alloc - allocate atomic state
46 * @dev: DRM device
47 *
48 * This allocates an empty atomic state to track updates.
49 */
50struct drm_atomic_state *
51drm_atomic_state_alloc(struct drm_device *dev)
52{
53 struct drm_atomic_state *state;
54
55 state = kzalloc(sizeof(*state), GFP_KERNEL);
56 if (!state)
57 return NULL;
58
Rob Clarkd34f20d2014-12-18 16:01:56 -050059 /* TODO legacy paths should maybe do a better job about
60 * setting this appropriately?
61 */
62 state->allow_modeset = true;
63
Daniel Vetterf52b69f12014-11-19 18:38:08 +010064 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
65
Daniel Vettercc4ceb42014-07-25 21:30:38 +020066 state->crtcs = kcalloc(dev->mode_config.num_crtc,
67 sizeof(*state->crtcs), GFP_KERNEL);
68 if (!state->crtcs)
69 goto fail;
70 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
71 sizeof(*state->crtc_states), GFP_KERNEL);
72 if (!state->crtc_states)
73 goto fail;
74 state->planes = kcalloc(dev->mode_config.num_total_plane,
75 sizeof(*state->planes), GFP_KERNEL);
76 if (!state->planes)
77 goto fail;
78 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
79 sizeof(*state->plane_states), GFP_KERNEL);
80 if (!state->plane_states)
81 goto fail;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010082 state->connectors = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020083 sizeof(*state->connectors),
84 GFP_KERNEL);
85 if (!state->connectors)
86 goto fail;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010087 state->connector_states = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020088 sizeof(*state->connector_states),
89 GFP_KERNEL);
90 if (!state->connector_states)
91 goto fail;
92
93 state->dev = dev;
94
Daniel Vetter17a38d92015-02-22 12:24:16 +010095 DRM_DEBUG_ATOMIC("Allocate atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020096
97 return state;
98fail:
99 kfree_state(state);
100
101 return NULL;
102}
103EXPORT_SYMBOL(drm_atomic_state_alloc);
104
105/**
106 * drm_atomic_state_clear - clear state object
107 * @state: atomic state
108 *
109 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
110 * all locks. So someone else could sneak in and change the current modeset
111 * configuration. Which means that all the state assembled in @state is no
112 * longer an atomic update to the current state, but to some arbitrary earlier
113 * state. Which could break assumptions the driver's ->atomic_check likely
114 * relies on.
115 *
116 * Hence we must clear all cached state and completely start over, using this
117 * function.
118 */
119void drm_atomic_state_clear(struct drm_atomic_state *state)
120{
121 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100122 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200123 int i;
124
Daniel Vetter17a38d92015-02-22 12:24:16 +0100125 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200126
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100127 for (i = 0; i < state->num_connector; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200128 struct drm_connector *connector = state->connectors[i];
129
130 if (!connector)
131 continue;
132
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100133 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
134
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200135 connector->funcs->atomic_destroy_state(connector,
136 state->connector_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300137 state->connectors[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200138 state->connector_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200139 }
140
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100141 for (i = 0; i < config->num_crtc; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200142 struct drm_crtc *crtc = state->crtcs[i];
143
144 if (!crtc)
145 continue;
146
147 crtc->funcs->atomic_destroy_state(crtc,
148 state->crtc_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300149 state->crtcs[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200150 state->crtc_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200151 }
152
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100153 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200154 struct drm_plane *plane = state->planes[i];
155
156 if (!plane)
157 continue;
158
159 plane->funcs->atomic_destroy_state(plane,
160 state->plane_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300161 state->planes[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200162 state->plane_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200163 }
164}
165EXPORT_SYMBOL(drm_atomic_state_clear);
166
167/**
168 * drm_atomic_state_free - free all memory for an atomic state
169 * @state: atomic state to deallocate
170 *
171 * This frees all memory associated with an atomic state, including all the
172 * per-object state for planes, crtcs and connectors.
173 */
174void drm_atomic_state_free(struct drm_atomic_state *state)
175{
Ander Conselvan de Oliveiraa0211bb2015-03-30 14:05:43 +0300176 if (!state)
177 return;
178
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200179 drm_atomic_state_clear(state);
180
Daniel Vetter17a38d92015-02-22 12:24:16 +0100181 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200182
183 kfree_state(state);
184}
185EXPORT_SYMBOL(drm_atomic_state_free);
186
187/**
188 * drm_atomic_get_crtc_state - get crtc state
189 * @state: global atomic state object
190 * @crtc: crtc to get state object for
191 *
192 * This function returns the crtc state for the given crtc, allocating it if
193 * needed. It will also grab the relevant crtc lock to make sure that the state
194 * is consistent.
195 *
196 * Returns:
197 *
198 * Either the allocated state or the error code encoded into the pointer. When
199 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
200 * entire atomic sequence must be restarted. All other errors are fatal.
201 */
202struct drm_crtc_state *
203drm_atomic_get_crtc_state(struct drm_atomic_state *state,
204 struct drm_crtc *crtc)
205{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200206 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200207 struct drm_crtc_state *crtc_state;
208
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200209 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
210 if (crtc_state)
211 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200212
213 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
214 if (ret)
215 return ERR_PTR(ret);
216
217 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
218 if (!crtc_state)
219 return ERR_PTR(-ENOMEM);
220
221 state->crtc_states[index] = crtc_state;
222 state->crtcs[index] = crtc;
223 crtc_state->state = state;
224
Daniel Vetter17a38d92015-02-22 12:24:16 +0100225 DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
226 crtc->base.id, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200227
228 return crtc_state;
229}
230EXPORT_SYMBOL(drm_atomic_get_crtc_state);
231
232/**
Rob Clark40ecc692014-12-18 16:01:46 -0500233 * drm_atomic_crtc_set_property - set property on CRTC
234 * @crtc: the drm CRTC to set a property on
235 * @state: the state object to update with the new property value
236 * @property: the property to set
237 * @val: the new property value
238 *
239 * Use this instead of calling crtc->atomic_set_property directly.
240 * This function handles generic/core properties and calls out to
241 * driver's ->atomic_set_property() for driver properties. To ensure
242 * consistent behavior you must call this function rather than the
243 * driver hook directly.
244 *
245 * RETURNS:
246 * Zero on success, error code on failure
247 */
248int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
249 struct drm_crtc_state *state, struct drm_property *property,
250 uint64_t val)
251{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100252 struct drm_device *dev = crtc->dev;
253 struct drm_mode_config *config = &dev->mode_config;
254
255 /* FIXME: Mode prop is missing, which also controls ->enable. */
Daniel Stone27798362015-03-19 04:33:26 +0000256 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100257 state->active = val;
Daniel Stone27798362015-03-19 04:33:26 +0000258 else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500259 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000260 else
261 return -EINVAL;
262
263 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500264}
265EXPORT_SYMBOL(drm_atomic_crtc_set_property);
266
Daniel Vettera97df1c2014-12-18 22:49:02 +0100267/*
Rob Clarkac9c9252014-12-18 16:01:47 -0500268 * This function handles generic/core properties and calls out to
269 * driver's ->atomic_get_property() for driver properties. To ensure
270 * consistent behavior you must call this function rather than the
271 * driver hook directly.
Rob Clarkac9c9252014-12-18 16:01:47 -0500272 */
273int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
274 const struct drm_crtc_state *state,
275 struct drm_property *property, uint64_t *val)
276{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000277 struct drm_device *dev = crtc->dev;
278 struct drm_mode_config *config = &dev->mode_config;
279
280 if (property == config->prop_active)
281 *val = state->active;
282 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500283 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000284 else
285 return -EINVAL;
286
287 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500288}
Rob Clarkac9c9252014-12-18 16:01:47 -0500289
290/**
Rob Clark5e743732014-12-18 16:01:51 -0500291 * drm_atomic_crtc_check - check crtc state
292 * @crtc: crtc to check
293 * @state: crtc state to check
294 *
295 * Provides core sanity checks for crtc state.
296 *
297 * RETURNS:
298 * Zero on success, error code on failure
299 */
300static int drm_atomic_crtc_check(struct drm_crtc *crtc,
301 struct drm_crtc_state *state)
302{
303 /* NOTE: we explicitly don't enforce constraints such as primary
304 * layer covering entire screen, since that is something we want
305 * to allow (on hw that supports it). For hw that does not, it
306 * should be checked in driver's crtc->atomic_check() vfunc.
307 *
308 * TODO: Add generic modeset state checks once we support those.
309 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100310
311 if (state->active && !state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100312 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
313 crtc->base.id);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100314 return -EINVAL;
315 }
316
Rob Clark5e743732014-12-18 16:01:51 -0500317 return 0;
318}
319
320/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200321 * drm_atomic_get_plane_state - get plane state
322 * @state: global atomic state object
323 * @plane: plane to get state object for
324 *
325 * This function returns the plane state for the given plane, allocating it if
326 * needed. It will also grab the relevant plane lock to make sure that the state
327 * is consistent.
328 *
329 * Returns:
330 *
331 * Either the allocated state or the error code encoded into the pointer. When
332 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
333 * entire atomic sequence must be restarted. All other errors are fatal.
334 */
335struct drm_plane_state *
336drm_atomic_get_plane_state(struct drm_atomic_state *state,
337 struct drm_plane *plane)
338{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200339 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200340 struct drm_plane_state *plane_state;
341
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200342 plane_state = drm_atomic_get_existing_plane_state(state, plane);
343 if (plane_state)
344 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200345
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100346 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200347 if (ret)
348 return ERR_PTR(ret);
349
350 plane_state = plane->funcs->atomic_duplicate_state(plane);
351 if (!plane_state)
352 return ERR_PTR(-ENOMEM);
353
354 state->plane_states[index] = plane_state;
355 state->planes[index] = plane;
356 plane_state->state = state;
357
Daniel Vetter17a38d92015-02-22 12:24:16 +0100358 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
359 plane->base.id, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200360
361 if (plane_state->crtc) {
362 struct drm_crtc_state *crtc_state;
363
364 crtc_state = drm_atomic_get_crtc_state(state,
365 plane_state->crtc);
366 if (IS_ERR(crtc_state))
367 return ERR_CAST(crtc_state);
368 }
369
370 return plane_state;
371}
372EXPORT_SYMBOL(drm_atomic_get_plane_state);
373
374/**
Rob Clark40ecc692014-12-18 16:01:46 -0500375 * drm_atomic_plane_set_property - set property on plane
376 * @plane: the drm plane to set a property on
377 * @state: the state object to update with the new property value
378 * @property: the property to set
379 * @val: the new property value
380 *
381 * Use this instead of calling plane->atomic_set_property directly.
382 * This function handles generic/core properties and calls out to
383 * driver's ->atomic_set_property() for driver properties. To ensure
384 * consistent behavior you must call this function rather than the
385 * driver hook directly.
386 *
387 * RETURNS:
388 * Zero on success, error code on failure
389 */
390int drm_atomic_plane_set_property(struct drm_plane *plane,
391 struct drm_plane_state *state, struct drm_property *property,
392 uint64_t val)
393{
Rob Clark6b4959f2014-12-18 16:01:53 -0500394 struct drm_device *dev = plane->dev;
395 struct drm_mode_config *config = &dev->mode_config;
396
397 if (property == config->prop_fb_id) {
398 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
399 drm_atomic_set_fb_for_plane(state, fb);
400 if (fb)
401 drm_framebuffer_unreference(fb);
402 } else if (property == config->prop_crtc_id) {
403 struct drm_crtc *crtc = drm_crtc_find(dev, val);
404 return drm_atomic_set_crtc_for_plane(state, crtc);
405 } else if (property == config->prop_crtc_x) {
406 state->crtc_x = U642I64(val);
407 } else if (property == config->prop_crtc_y) {
408 state->crtc_y = U642I64(val);
409 } else if (property == config->prop_crtc_w) {
410 state->crtc_w = val;
411 } else if (property == config->prop_crtc_h) {
412 state->crtc_h = val;
413 } else if (property == config->prop_src_x) {
414 state->src_x = val;
415 } else if (property == config->prop_src_y) {
416 state->src_y = val;
417 } else if (property == config->prop_src_w) {
418 state->src_w = val;
419 } else if (property == config->prop_src_h) {
420 state->src_h = val;
Matt Roper1da30622015-01-21 16:35:40 -0800421 } else if (property == config->rotation_property) {
422 state->rotation = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500423 } else if (plane->funcs->atomic_set_property) {
424 return plane->funcs->atomic_set_property(plane, state,
425 property, val);
426 } else {
427 return -EINVAL;
428 }
429
430 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500431}
432EXPORT_SYMBOL(drm_atomic_plane_set_property);
433
Daniel Vettera97df1c2014-12-18 22:49:02 +0100434/*
Rob Clarkac9c9252014-12-18 16:01:47 -0500435 * This function handles generic/core properties and calls out to
436 * driver's ->atomic_get_property() for driver properties. To ensure
437 * consistent behavior you must call this function rather than the
438 * driver hook directly.
Rob Clarkac9c9252014-12-18 16:01:47 -0500439 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100440static int
441drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500442 const struct drm_plane_state *state,
443 struct drm_property *property, uint64_t *val)
444{
Rob Clark6b4959f2014-12-18 16:01:53 -0500445 struct drm_device *dev = plane->dev;
446 struct drm_mode_config *config = &dev->mode_config;
447
448 if (property == config->prop_fb_id) {
449 *val = (state->fb) ? state->fb->base.id : 0;
450 } else if (property == config->prop_crtc_id) {
451 *val = (state->crtc) ? state->crtc->base.id : 0;
452 } else if (property == config->prop_crtc_x) {
453 *val = I642U64(state->crtc_x);
454 } else if (property == config->prop_crtc_y) {
455 *val = I642U64(state->crtc_y);
456 } else if (property == config->prop_crtc_w) {
457 *val = state->crtc_w;
458 } else if (property == config->prop_crtc_h) {
459 *val = state->crtc_h;
460 } else if (property == config->prop_src_x) {
461 *val = state->src_x;
462 } else if (property == config->prop_src_y) {
463 *val = state->src_y;
464 } else if (property == config->prop_src_w) {
465 *val = state->src_w;
466 } else if (property == config->prop_src_h) {
467 *val = state->src_h;
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000468 } else if (property == config->rotation_property) {
469 *val = state->rotation;
Rob Clark6b4959f2014-12-18 16:01:53 -0500470 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500471 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500472 } else {
473 return -EINVAL;
474 }
475
476 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500477}
Rob Clarkac9c9252014-12-18 16:01:47 -0500478
479/**
Rob Clark5e743732014-12-18 16:01:51 -0500480 * drm_atomic_plane_check - check plane state
481 * @plane: plane to check
482 * @state: plane state to check
483 *
484 * Provides core sanity checks for plane state.
485 *
486 * RETURNS:
487 * Zero on success, error code on failure
488 */
489static int drm_atomic_plane_check(struct drm_plane *plane,
490 struct drm_plane_state *state)
491{
492 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200493 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500494
495 /* either *both* CRTC and FB must be set, or neither */
496 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100497 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500498 return -EINVAL;
499 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100500 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500501 return -EINVAL;
502 }
503
504 /* if disabled, we don't care about the rest of the state: */
505 if (!state->crtc)
506 return 0;
507
508 /* Check whether this plane is usable on this CRTC */
509 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100510 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500511 return -EINVAL;
512 }
513
514 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +0200515 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
516 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100517 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
518 drm_get_format_name(state->fb->pixel_format));
Laurent Pinchartead86102015-03-05 02:25:43 +0200519 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500520 }
521
522 /* Give drivers some help against integer overflows */
523 if (state->crtc_w > INT_MAX ||
524 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
525 state->crtc_h > INT_MAX ||
526 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100527 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
528 state->crtc_w, state->crtc_h,
529 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500530 return -ERANGE;
531 }
532
533 fb_width = state->fb->width << 16;
534 fb_height = state->fb->height << 16;
535
536 /* Make sure source coordinates are inside the fb. */
537 if (state->src_w > fb_width ||
538 state->src_x > fb_width - state->src_w ||
539 state->src_h > fb_height ||
540 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100541 DRM_DEBUG_ATOMIC("Invalid source coordinates "
542 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
543 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
544 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
545 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
546 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500547 return -ENOSPC;
548 }
549
550 return 0;
551}
552
553/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200554 * drm_atomic_get_connector_state - get connector state
555 * @state: global atomic state object
556 * @connector: connector to get state object for
557 *
558 * This function returns the connector state for the given connector,
559 * allocating it if needed. It will also grab the relevant connector lock to
560 * make sure that the state is consistent.
561 *
562 * Returns:
563 *
564 * Either the allocated state or the error code encoded into the pointer. When
565 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
566 * entire atomic sequence must be restarted. All other errors are fatal.
567 */
568struct drm_connector_state *
569drm_atomic_get_connector_state(struct drm_atomic_state *state,
570 struct drm_connector *connector)
571{
572 int ret, index;
573 struct drm_mode_config *config = &connector->dev->mode_config;
574 struct drm_connector_state *connector_state;
575
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100576 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
577 if (ret)
578 return ERR_PTR(ret);
579
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200580 index = drm_connector_index(connector);
581
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100582 /*
583 * Construction of atomic state updates can race with a connector
584 * hot-add which might overflow. In this case flip the table and just
585 * restart the entire ioctl - no one is fast enough to livelock a cpu
586 * with physical hotplug events anyway.
587 *
588 * Note that we only grab the indexes once we have the right lock to
589 * prevent hotplug/unplugging of connectors. So removal is no problem,
590 * at most the array is a bit too large.
591 */
592 if (index >= state->num_connector) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100593 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
Daniel Vetterfc2d2bc2014-11-20 09:53:35 +0100594 return ERR_PTR(-EAGAIN);
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100595 }
596
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200597 if (state->connector_states[index])
598 return state->connector_states[index];
599
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200600 connector_state = connector->funcs->atomic_duplicate_state(connector);
601 if (!connector_state)
602 return ERR_PTR(-ENOMEM);
603
604 state->connector_states[index] = connector_state;
605 state->connectors[index] = connector;
606 connector_state->state = state;
607
Daniel Vetter17a38d92015-02-22 12:24:16 +0100608 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
609 connector->base.id, connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200610
611 if (connector_state->crtc) {
612 struct drm_crtc_state *crtc_state;
613
614 crtc_state = drm_atomic_get_crtc_state(state,
615 connector_state->crtc);
616 if (IS_ERR(crtc_state))
617 return ERR_CAST(crtc_state);
618 }
619
620 return connector_state;
621}
622EXPORT_SYMBOL(drm_atomic_get_connector_state);
623
624/**
Rob Clark40ecc692014-12-18 16:01:46 -0500625 * drm_atomic_connector_set_property - set property on connector.
626 * @connector: the drm connector to set a property on
627 * @state: the state object to update with the new property value
628 * @property: the property to set
629 * @val: the new property value
630 *
631 * Use this instead of calling connector->atomic_set_property directly.
632 * This function handles generic/core properties and calls out to
633 * driver's ->atomic_set_property() for driver properties. To ensure
634 * consistent behavior you must call this function rather than the
635 * driver hook directly.
636 *
637 * RETURNS:
638 * Zero on success, error code on failure
639 */
640int drm_atomic_connector_set_property(struct drm_connector *connector,
641 struct drm_connector_state *state, struct drm_property *property,
642 uint64_t val)
643{
644 struct drm_device *dev = connector->dev;
645 struct drm_mode_config *config = &dev->mode_config;
646
Rob Clarkae16c592014-12-18 16:01:54 -0500647 if (property == config->prop_crtc_id) {
648 struct drm_crtc *crtc = drm_crtc_find(dev, val);
649 return drm_atomic_set_crtc_for_connector(state, crtc);
650 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -0500651 /* setting DPMS property requires special handling, which
652 * is done in legacy setprop path for us. Disallow (for
653 * now?) atomic writes to DPMS property:
654 */
655 return -EINVAL;
656 } else if (connector->funcs->atomic_set_property) {
657 return connector->funcs->atomic_set_property(connector,
658 state, property, val);
659 } else {
660 return -EINVAL;
661 }
662}
663EXPORT_SYMBOL(drm_atomic_connector_set_property);
664
Daniel Vettera97df1c2014-12-18 22:49:02 +0100665/*
Rob Clarkac9c9252014-12-18 16:01:47 -0500666 * This function handles generic/core properties and calls out to
667 * driver's ->atomic_get_property() for driver properties. To ensure
668 * consistent behavior you must call this function rather than the
669 * driver hook directly.
Rob Clarkac9c9252014-12-18 16:01:47 -0500670 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100671static int
672drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -0500673 const struct drm_connector_state *state,
674 struct drm_property *property, uint64_t *val)
675{
676 struct drm_device *dev = connector->dev;
677 struct drm_mode_config *config = &dev->mode_config;
678
Rob Clarkae16c592014-12-18 16:01:54 -0500679 if (property == config->prop_crtc_id) {
680 *val = (state->crtc) ? state->crtc->base.id : 0;
681 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500682 *val = connector->dpms;
683 } else if (connector->funcs->atomic_get_property) {
684 return connector->funcs->atomic_get_property(connector,
685 state, property, val);
686 } else {
687 return -EINVAL;
688 }
689
690 return 0;
691}
Rob Clarkac9c9252014-12-18 16:01:47 -0500692
Rob Clark88a48e22014-12-18 16:01:50 -0500693int drm_atomic_get_property(struct drm_mode_object *obj,
694 struct drm_property *property, uint64_t *val)
695{
696 struct drm_device *dev = property->dev;
697 int ret;
698
699 switch (obj->type) {
700 case DRM_MODE_OBJECT_CONNECTOR: {
701 struct drm_connector *connector = obj_to_connector(obj);
702 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
703 ret = drm_atomic_connector_get_property(connector,
704 connector->state, property, val);
705 break;
706 }
707 case DRM_MODE_OBJECT_CRTC: {
708 struct drm_crtc *crtc = obj_to_crtc(obj);
709 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
710 ret = drm_atomic_crtc_get_property(crtc,
711 crtc->state, property, val);
712 break;
713 }
714 case DRM_MODE_OBJECT_PLANE: {
715 struct drm_plane *plane = obj_to_plane(obj);
716 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
717 ret = drm_atomic_plane_get_property(plane,
718 plane->state, property, val);
719 break;
720 }
721 default:
722 ret = -EINVAL;
723 break;
724 }
725
726 return ret;
727}
728
729/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200730 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100731 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200732 * @crtc: crtc to use for the plane
733 *
734 * Changing the assigned crtc for a plane requires us to grab the lock and state
735 * for the new crtc, as needed. This function takes care of all these details
736 * besides updating the pointer in the state object itself.
737 *
738 * Returns:
739 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
740 * then the w/w mutex code has detected a deadlock and the entire atomic
741 * sequence must be restarted. All other errors are fatal.
742 */
743int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100744drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
745 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200746{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100747 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200748 struct drm_crtc_state *crtc_state;
749
Rob Clark6ddd3882014-11-21 15:28:31 -0500750 if (plane_state->crtc) {
751 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
752 plane_state->crtc);
753 if (WARN_ON(IS_ERR(crtc_state)))
754 return PTR_ERR(crtc_state);
755
756 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
757 }
758
759 plane_state->crtc = crtc;
760
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200761 if (crtc) {
762 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
763 crtc);
764 if (IS_ERR(crtc_state))
765 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -0500766 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200767 }
768
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200769 if (crtc)
Daniel Vetter17a38d92015-02-22 12:24:16 +0100770 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
771 plane_state, crtc->base.id);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200772 else
Daniel Vetter17a38d92015-02-22 12:24:16 +0100773 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
774 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200775
776 return 0;
777}
778EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
779
780/**
John Hunter16d78bc2e2015-04-07 19:38:50 +0800781 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +0100782 * @plane_state: atomic state object for the plane
783 * @fb: fb to use for the plane
784 *
785 * Changing the assigned framebuffer for a plane requires us to grab a reference
786 * to the new fb and drop the reference to the old fb, if there is one. This
787 * function takes care of all these details besides updating the pointer in the
788 * state object itself.
789 */
790void
791drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
792 struct drm_framebuffer *fb)
793{
794 if (plane_state->fb)
795 drm_framebuffer_unreference(plane_state->fb);
796 if (fb)
797 drm_framebuffer_reference(fb);
798 plane_state->fb = fb;
799
800 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +0100801 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
802 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +0100803 else
Daniel Vetter17a38d92015-02-22 12:24:16 +0100804 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
805 plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +0100806}
807EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
808
809/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200810 * drm_atomic_set_crtc_for_connector - set crtc for connector
811 * @conn_state: atomic state object for the connector
812 * @crtc: crtc to use for the connector
813 *
814 * Changing the assigned crtc for a connector requires us to grab the lock and
815 * state for the new crtc, as needed. This function takes care of all these
816 * details besides updating the pointer in the state object itself.
817 *
818 * Returns:
819 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
820 * then the w/w mutex code has detected a deadlock and the entire atomic
821 * sequence must be restarted. All other errors are fatal.
822 */
823int
824drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
825 struct drm_crtc *crtc)
826{
827 struct drm_crtc_state *crtc_state;
828
829 if (crtc) {
830 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
831 if (IS_ERR(crtc_state))
832 return PTR_ERR(crtc_state);
833 }
834
835 conn_state->crtc = crtc;
836
837 if (crtc)
Daniel Vetter17a38d92015-02-22 12:24:16 +0100838 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
839 conn_state, crtc->base.id);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200840 else
Daniel Vetter17a38d92015-02-22 12:24:16 +0100841 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
842 conn_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200843
844 return 0;
845}
846EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
847
848/**
849 * drm_atomic_add_affected_connectors - add connectors for crtc
850 * @state: atomic state
851 * @crtc: DRM crtc
852 *
853 * This function walks the current configuration and adds all connectors
854 * currently using @crtc to the atomic configuration @state. Note that this
855 * function must acquire the connection mutex. This can potentially cause
856 * unneeded seralization if the update is just for the planes on one crtc. Hence
857 * drivers and helpers should only call this when really needed (e.g. when a
858 * full modeset needs to happen due to some change).
859 *
860 * Returns:
861 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
862 * then the w/w mutex code has detected a deadlock and the entire atomic
863 * sequence must be restarted. All other errors are fatal.
864 */
865int
866drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
867 struct drm_crtc *crtc)
868{
869 struct drm_mode_config *config = &state->dev->mode_config;
870 struct drm_connector *connector;
871 struct drm_connector_state *conn_state;
872 int ret;
873
874 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
875 if (ret)
876 return ret;
877
Daniel Vetter17a38d92015-02-22 12:24:16 +0100878 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
879 crtc->base.id, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200880
881 /*
882 * Changed connectors are already in @state, so only need to look at the
883 * current configuration.
884 */
885 list_for_each_entry(connector, &config->connector_list, head) {
886 if (connector->state->crtc != crtc)
887 continue;
888
889 conn_state = drm_atomic_get_connector_state(state, connector);
890 if (IS_ERR(conn_state))
891 return PTR_ERR(conn_state);
892 }
893
894 return 0;
895}
896EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
897
898/**
899 * drm_atomic_connectors_for_crtc - count number of connected outputs
900 * @state: atomic state
901 * @crtc: DRM crtc
902 *
903 * This function counts all connectors which will be connected to @crtc
904 * according to @state. Useful to recompute the enable state for @crtc.
905 */
906int
907drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
908 struct drm_crtc *crtc)
909{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300910 struct drm_connector *connector;
911 struct drm_connector_state *conn_state;
912
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200913 int i, num_connected_connectors = 0;
914
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300915 for_each_connector_in_state(state, connector, conn_state, i) {
916 if (conn_state->crtc == crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200917 num_connected_connectors++;
918 }
919
Daniel Vetter17a38d92015-02-22 12:24:16 +0100920 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
921 state, num_connected_connectors, crtc->base.id);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200922
923 return num_connected_connectors;
924}
925EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
926
927/**
928 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
929 * @state: atomic state
930 *
931 * This function should be used by legacy entry points which don't understand
932 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +0800933 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200934 */
935void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
936{
937 int ret;
938
939retry:
940 drm_modeset_backoff(state->acquire_ctx);
941
942 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
943 state->acquire_ctx);
944 if (ret)
945 goto retry;
946 ret = drm_modeset_lock_all_crtcs(state->dev,
947 state->acquire_ctx);
948 if (ret)
949 goto retry;
950}
951EXPORT_SYMBOL(drm_atomic_legacy_backoff);
952
953/**
954 * drm_atomic_check_only - check whether a given config would work
955 * @state: atomic configuration to check
956 *
957 * Note that this function can return -EDEADLK if the driver needed to acquire
958 * more locks but encountered a deadlock. The caller must then do the usual w/w
959 * backoff dance and restart. All other errors are fatal.
960 *
961 * Returns:
962 * 0 on success, negative error code on failure.
963 */
964int drm_atomic_check_only(struct drm_atomic_state *state)
965{
Rob Clark5e743732014-12-18 16:01:51 -0500966 struct drm_device *dev = state->dev;
967 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300968 struct drm_plane *plane;
969 struct drm_plane_state *plane_state;
970 struct drm_crtc *crtc;
971 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -0500972 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200973
Daniel Vetter17a38d92015-02-22 12:24:16 +0100974 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200975
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300976 for_each_plane_in_state(state, plane, plane_state, i) {
977 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -0500978 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100979 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
980 plane->base.id);
Rob Clark5e743732014-12-18 16:01:51 -0500981 return ret;
982 }
983 }
984
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300985 for_each_crtc_in_state(state, crtc, crtc_state, i) {
986 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -0500987 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100988 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
989 crtc->base.id);
Rob Clark5e743732014-12-18 16:01:51 -0500990 return ret;
991 }
992 }
993
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200994 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -0500995 ret = config->funcs->atomic_check(state->dev, state);
996
Rob Clarkd34f20d2014-12-18 16:01:56 -0500997 if (!state->allow_modeset) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300998 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100999 if (crtc_state->mode_changed ||
1000 crtc_state->active_changed) {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001001 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1002 crtc->base.id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001003 return -EINVAL;
1004 }
1005 }
1006 }
1007
Rob Clark5e743732014-12-18 16:01:51 -05001008 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001009}
1010EXPORT_SYMBOL(drm_atomic_check_only);
1011
1012/**
1013 * drm_atomic_commit - commit configuration atomically
1014 * @state: atomic configuration to check
1015 *
1016 * Note that this function can return -EDEADLK if the driver needed to acquire
1017 * more locks but encountered a deadlock. The caller must then do the usual w/w
1018 * backoff dance and restart. All other errors are fatal.
1019 *
1020 * Also note that on successful execution ownership of @state is transferred
1021 * from the caller of this function to the function itself. The caller must not
1022 * free or in any other way access @state. If the function fails then the caller
1023 * must clean up @state itself.
1024 *
1025 * Returns:
1026 * 0 on success, negative error code on failure.
1027 */
1028int drm_atomic_commit(struct drm_atomic_state *state)
1029{
1030 struct drm_mode_config *config = &state->dev->mode_config;
1031 int ret;
1032
1033 ret = drm_atomic_check_only(state);
1034 if (ret)
1035 return ret;
1036
Daniel Vetter17a38d92015-02-22 12:24:16 +01001037 DRM_DEBUG_ATOMIC("commiting %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001038
1039 return config->funcs->atomic_commit(state->dev, state, false);
1040}
1041EXPORT_SYMBOL(drm_atomic_commit);
1042
1043/**
1044 * drm_atomic_async_commit - atomic&async configuration commit
1045 * @state: atomic configuration to check
1046 *
1047 * Note that this function can return -EDEADLK if the driver needed to acquire
1048 * more locks but encountered a deadlock. The caller must then do the usual w/w
1049 * backoff dance and restart. All other errors are fatal.
1050 *
1051 * Also note that on successful execution ownership of @state is transferred
1052 * from the caller of this function to the function itself. The caller must not
1053 * free or in any other way access @state. If the function fails then the caller
1054 * must clean up @state itself.
1055 *
1056 * Returns:
1057 * 0 on success, negative error code on failure.
1058 */
1059int drm_atomic_async_commit(struct drm_atomic_state *state)
1060{
1061 struct drm_mode_config *config = &state->dev->mode_config;
1062 int ret;
1063
1064 ret = drm_atomic_check_only(state);
1065 if (ret)
1066 return ret;
1067
Daniel Vetter17a38d92015-02-22 12:24:16 +01001068 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001069
1070 return config->funcs->atomic_commit(state->dev, state, true);
1071}
1072EXPORT_SYMBOL(drm_atomic_async_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001073
1074/*
1075 * The big monstor ioctl
1076 */
1077
1078static struct drm_pending_vblank_event *create_vblank_event(
1079 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1080{
1081 struct drm_pending_vblank_event *e = NULL;
1082 unsigned long flags;
1083
1084 spin_lock_irqsave(&dev->event_lock, flags);
1085 if (file_priv->event_space < sizeof e->event) {
1086 spin_unlock_irqrestore(&dev->event_lock, flags);
1087 goto out;
1088 }
1089 file_priv->event_space -= sizeof e->event;
1090 spin_unlock_irqrestore(&dev->event_lock, flags);
1091
1092 e = kzalloc(sizeof *e, GFP_KERNEL);
1093 if (e == NULL) {
1094 spin_lock_irqsave(&dev->event_lock, flags);
1095 file_priv->event_space += sizeof e->event;
1096 spin_unlock_irqrestore(&dev->event_lock, flags);
1097 goto out;
1098 }
1099
1100 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1101 e->event.base.length = sizeof e->event;
1102 e->event.user_data = user_data;
1103 e->base.event = &e->event.base;
1104 e->base.file_priv = file_priv;
1105 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1106
1107out:
1108 return e;
1109}
1110
1111static void destroy_vblank_event(struct drm_device *dev,
1112 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1113{
1114 unsigned long flags;
1115
1116 spin_lock_irqsave(&dev->event_lock, flags);
1117 file_priv->event_space += sizeof e->event;
1118 spin_unlock_irqrestore(&dev->event_lock, flags);
1119 kfree(e);
1120}
1121
1122static int atomic_set_prop(struct drm_atomic_state *state,
1123 struct drm_mode_object *obj, struct drm_property *prop,
1124 uint64_t prop_value)
1125{
1126 struct drm_mode_object *ref;
1127 int ret;
1128
1129 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1130 return -EINVAL;
1131
1132 switch (obj->type) {
1133 case DRM_MODE_OBJECT_CONNECTOR: {
1134 struct drm_connector *connector = obj_to_connector(obj);
1135 struct drm_connector_state *connector_state;
1136
1137 connector_state = drm_atomic_get_connector_state(state, connector);
1138 if (IS_ERR(connector_state)) {
1139 ret = PTR_ERR(connector_state);
1140 break;
1141 }
1142
1143 ret = drm_atomic_connector_set_property(connector,
1144 connector_state, prop, prop_value);
1145 break;
1146 }
1147 case DRM_MODE_OBJECT_CRTC: {
1148 struct drm_crtc *crtc = obj_to_crtc(obj);
1149 struct drm_crtc_state *crtc_state;
1150
1151 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1152 if (IS_ERR(crtc_state)) {
1153 ret = PTR_ERR(crtc_state);
1154 break;
1155 }
1156
1157 ret = drm_atomic_crtc_set_property(crtc,
1158 crtc_state, prop, prop_value);
1159 break;
1160 }
1161 case DRM_MODE_OBJECT_PLANE: {
1162 struct drm_plane *plane = obj_to_plane(obj);
1163 struct drm_plane_state *plane_state;
1164
1165 plane_state = drm_atomic_get_plane_state(state, plane);
1166 if (IS_ERR(plane_state)) {
1167 ret = PTR_ERR(plane_state);
1168 break;
1169 }
1170
1171 ret = drm_atomic_plane_set_property(plane,
1172 plane_state, prop, prop_value);
1173 break;
1174 }
1175 default:
1176 ret = -EINVAL;
1177 break;
1178 }
1179
1180 drm_property_change_valid_put(prop, ref);
1181 return ret;
1182}
1183
1184int drm_mode_atomic_ioctl(struct drm_device *dev,
1185 void *data, struct drm_file *file_priv)
1186{
1187 struct drm_mode_atomic *arg = data;
1188 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1189 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1190 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1191 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1192 unsigned int copied_objs, copied_props;
1193 struct drm_atomic_state *state;
1194 struct drm_modeset_acquire_ctx ctx;
1195 struct drm_plane *plane;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001196 struct drm_crtc *crtc;
1197 struct drm_crtc_state *crtc_state;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001198 unsigned plane_mask = 0;
1199 int ret = 0;
1200 unsigned int i, j;
1201
1202 /* disallow for drivers not supporting atomic: */
1203 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1204 return -EINVAL;
1205
1206 /* disallow for userspace that has not enabled atomic cap (even
1207 * though this may be a bit overkill, since legacy userspace
1208 * wouldn't know how to call this ioctl)
1209 */
1210 if (!file_priv->atomic)
1211 return -EINVAL;
1212
1213 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1214 return -EINVAL;
1215
1216 if (arg->reserved)
1217 return -EINVAL;
1218
1219 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1220 !dev->mode_config.async_page_flip)
1221 return -EINVAL;
1222
1223 /* can't test and expect an event at the same time. */
1224 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1225 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1226 return -EINVAL;
1227
1228 drm_modeset_acquire_init(&ctx, 0);
1229
1230 state = drm_atomic_state_alloc(dev);
1231 if (!state)
1232 return -ENOMEM;
1233
1234 state->acquire_ctx = &ctx;
1235 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1236
1237retry:
1238 copied_objs = 0;
1239 copied_props = 0;
1240
1241 for (i = 0; i < arg->count_objs; i++) {
1242 uint32_t obj_id, count_props;
1243 struct drm_mode_object *obj;
1244
1245 if (get_user(obj_id, objs_ptr + copied_objs)) {
1246 ret = -EFAULT;
1247 goto fail;
1248 }
1249
1250 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1251 if (!obj || !obj->properties) {
1252 ret = -ENOENT;
1253 goto fail;
1254 }
1255
1256 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1257 plane = obj_to_plane(obj);
1258 plane_mask |= (1 << drm_plane_index(plane));
1259 plane->old_fb = plane->fb;
1260 }
1261
1262 if (get_user(count_props, count_props_ptr + copied_objs)) {
1263 ret = -EFAULT;
1264 goto fail;
1265 }
1266
1267 copied_objs++;
1268
1269 for (j = 0; j < count_props; j++) {
1270 uint32_t prop_id;
1271 uint64_t prop_value;
1272 struct drm_property *prop;
1273
1274 if (get_user(prop_id, props_ptr + copied_props)) {
1275 ret = -EFAULT;
1276 goto fail;
1277 }
1278
1279 prop = drm_property_find(dev, prop_id);
1280 if (!prop) {
1281 ret = -ENOENT;
1282 goto fail;
1283 }
1284
Guenter Roeck42c58142015-01-12 21:12:17 -08001285 if (copy_from_user(&prop_value,
1286 prop_values_ptr + copied_props,
1287 sizeof(prop_value))) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001288 ret = -EFAULT;
1289 goto fail;
1290 }
1291
1292 ret = atomic_set_prop(state, obj, prop, prop_value);
1293 if (ret)
1294 goto fail;
1295
1296 copied_props++;
1297 }
1298 }
1299
1300 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001301 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001302 struct drm_pending_vblank_event *e;
1303
Rob Clarkd34f20d2014-12-18 16:01:56 -05001304 e = create_vblank_event(dev, file_priv, arg->user_data);
1305 if (!e) {
1306 ret = -ENOMEM;
1307 goto fail;
1308 }
1309
1310 crtc_state->event = e;
1311 }
1312 }
1313
1314 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1315 ret = drm_atomic_check_only(state);
1316 /* _check_only() does not free state, unlike _commit() */
1317 drm_atomic_state_free(state);
1318 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1319 ret = drm_atomic_async_commit(state);
1320 } else {
1321 ret = drm_atomic_commit(state);
1322 }
1323
1324 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1325 * locks (ie. while it is still safe to deref plane->state). We
1326 * need to do this here because the driver entry points cannot
1327 * distinguish between legacy and atomic ioctls.
1328 */
1329 drm_for_each_plane_mask(plane, dev, plane_mask) {
1330 if (ret == 0) {
1331 struct drm_framebuffer *new_fb = plane->state->fb;
1332 if (new_fb)
1333 drm_framebuffer_reference(new_fb);
1334 plane->fb = new_fb;
1335 plane->crtc = plane->state->crtc;
1336 } else {
1337 plane->old_fb = NULL;
1338 }
1339 if (plane->old_fb) {
1340 drm_framebuffer_unreference(plane->old_fb);
1341 plane->old_fb = NULL;
1342 }
1343 }
1344
1345 drm_modeset_drop_locks(&ctx);
1346 drm_modeset_acquire_fini(&ctx);
1347
1348 return ret;
1349
1350fail:
1351 if (ret == -EDEADLK)
1352 goto backoff;
1353
1354 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001355 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001356 destroy_vblank_event(dev, file_priv, crtc_state->event);
1357 crtc_state->event = NULL;
1358 }
1359 }
1360
1361 drm_atomic_state_free(state);
1362
1363 drm_modeset_drop_locks(&ctx);
1364 drm_modeset_acquire_fini(&ctx);
1365
1366 return ret;
1367
1368backoff:
1369 drm_atomic_state_clear(state);
1370 drm_modeset_backoff(&ctx);
1371
1372 goto retry;
1373}