2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_plane_helper.h>
33 static void kfree_state(struct drm_atomic_state *state)
35 kfree(state->connectors);
36 kfree(state->connector_states);
38 kfree(state->crtc_states);
40 kfree(state->plane_states);
45 * drm_atomic_state_alloc - allocate atomic state
48 * This allocates an empty atomic state to track updates.
50 struct drm_atomic_state *
51 drm_atomic_state_alloc(struct drm_device *dev)
53 struct drm_atomic_state *state;
55 state = kzalloc(sizeof(*state), GFP_KERNEL);
59 state->crtcs = kcalloc(dev->mode_config.num_crtc,
60 sizeof(*state->crtcs), GFP_KERNEL);
63 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
64 sizeof(*state->crtc_states), GFP_KERNEL);
65 if (!state->crtc_states)
67 state->planes = kcalloc(dev->mode_config.num_total_plane,
68 sizeof(*state->planes), GFP_KERNEL);
71 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
72 sizeof(*state->plane_states), GFP_KERNEL);
73 if (!state->plane_states)
75 state->connectors = kcalloc(dev->mode_config.num_connector,
76 sizeof(*state->connectors),
78 if (!state->connectors)
80 state->connector_states = kcalloc(dev->mode_config.num_connector,
81 sizeof(*state->connector_states),
83 if (!state->connector_states)
88 DRM_DEBUG_KMS("Allocate atomic state %p\n", state);
96 EXPORT_SYMBOL(drm_atomic_state_alloc);
99 * drm_atomic_state_clear - clear state object
100 * @state: atomic state
102 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
103 * all locks. So someone else could sneak in and change the current modeset
104 * configuration. Which means that all the state assembled in @state is no
105 * longer an atomic update to the current state, but to some arbitrary earlier
106 * state. Which could break assumptions the driver's ->atomic_check likely
109 * Hence we must clear all cached state and completely start over, using this
112 void drm_atomic_state_clear(struct drm_atomic_state *state)
114 struct drm_device *dev = state->dev;
117 DRM_DEBUG_KMS("Clearing atomic state %p\n", state);
119 for (i = 0; i < dev->mode_config.num_connector; i++) {
120 struct drm_connector *connector = state->connectors[i];
125 connector->funcs->atomic_destroy_state(connector,
126 state->connector_states[i]);
129 for (i = 0; i < dev->mode_config.num_crtc; i++) {
130 struct drm_crtc *crtc = state->crtcs[i];
135 crtc->funcs->atomic_destroy_state(crtc,
136 state->crtc_states[i]);
139 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
140 struct drm_plane *plane = state->planes[i];
145 plane->funcs->atomic_destroy_state(plane,
146 state->plane_states[i]);
149 EXPORT_SYMBOL(drm_atomic_state_clear);
152 * drm_atomic_state_free - free all memory for an atomic state
153 * @state: atomic state to deallocate
155 * This frees all memory associated with an atomic state, including all the
156 * per-object state for planes, crtcs and connectors.
158 void drm_atomic_state_free(struct drm_atomic_state *state)
160 drm_atomic_state_clear(state);
162 DRM_DEBUG_KMS("Freeing atomic state %p\n", state);
166 EXPORT_SYMBOL(drm_atomic_state_free);
169 * drm_atomic_get_crtc_state - get crtc state
170 * @state: global atomic state object
171 * @crtc: crtc to get state object for
173 * This function returns the crtc state for the given crtc, allocating it if
174 * needed. It will also grab the relevant crtc lock to make sure that the state
179 * Either the allocated state or the error code encoded into the pointer. When
180 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
181 * entire atomic sequence must be restarted. All other errors are fatal.
183 struct drm_crtc_state *
184 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
185 struct drm_crtc *crtc)
188 struct drm_crtc_state *crtc_state;
190 index = drm_crtc_index(crtc);
192 if (state->crtc_states[index])
193 return state->crtc_states[index];
195 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
199 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
201 return ERR_PTR(-ENOMEM);
203 state->crtc_states[index] = crtc_state;
204 state->crtcs[index] = crtc;
205 crtc_state->state = state;
207 DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n",
208 crtc->base.id, crtc_state, state);
212 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
215 * drm_atomic_get_plane_state - get plane state
216 * @state: global atomic state object
217 * @plane: plane to get state object for
219 * This function returns the plane state for the given plane, allocating it if
220 * needed. It will also grab the relevant plane lock to make sure that the state
225 * Either the allocated state or the error code encoded into the pointer. When
226 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
227 * entire atomic sequence must be restarted. All other errors are fatal.
229 struct drm_plane_state *
230 drm_atomic_get_plane_state(struct drm_atomic_state *state,
231 struct drm_plane *plane)
234 struct drm_plane_state *plane_state;
236 index = drm_plane_index(plane);
238 if (state->plane_states[index])
239 return state->plane_states[index];
242 * TODO: We currently don't have per-plane mutexes. So instead of trying
243 * crazy tricks with deferring plane->crtc and hoping for the best just
244 * grab all crtc locks. Once we have per-plane locks we must update this
245 * to only take the plane mutex.
247 ret = drm_modeset_lock_all_crtcs(state->dev, state->acquire_ctx);
251 plane_state = plane->funcs->atomic_duplicate_state(plane);
253 return ERR_PTR(-ENOMEM);
255 state->plane_states[index] = plane_state;
256 state->planes[index] = plane;
257 plane_state->state = state;
259 DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
260 plane->base.id, plane_state, state);
262 if (plane_state->crtc) {
263 struct drm_crtc_state *crtc_state;
265 crtc_state = drm_atomic_get_crtc_state(state,
267 if (IS_ERR(crtc_state))
268 return ERR_CAST(crtc_state);
273 EXPORT_SYMBOL(drm_atomic_get_plane_state);
276 * drm_atomic_get_connector_state - get connector state
277 * @state: global atomic state object
278 * @connector: connector to get state object for
280 * This function returns the connector state for the given connector,
281 * allocating it if needed. It will also grab the relevant connector lock to
282 * make sure that the state is consistent.
286 * Either the allocated state or the error code encoded into the pointer. When
287 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
288 * entire atomic sequence must be restarted. All other errors are fatal.
290 struct drm_connector_state *
291 drm_atomic_get_connector_state(struct drm_atomic_state *state,
292 struct drm_connector *connector)
295 struct drm_mode_config *config = &connector->dev->mode_config;
296 struct drm_connector_state *connector_state;
298 index = drm_connector_index(connector);
300 if (state->connector_states[index])
301 return state->connector_states[index];
303 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
307 connector_state = connector->funcs->atomic_duplicate_state(connector);
308 if (!connector_state)
309 return ERR_PTR(-ENOMEM);
311 state->connector_states[index] = connector_state;
312 state->connectors[index] = connector;
313 connector_state->state = state;
315 DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
316 connector->base.id, connector_state, state);
318 if (connector_state->crtc) {
319 struct drm_crtc_state *crtc_state;
321 crtc_state = drm_atomic_get_crtc_state(state,
322 connector_state->crtc);
323 if (IS_ERR(crtc_state))
324 return ERR_CAST(crtc_state);
327 return connector_state;
329 EXPORT_SYMBOL(drm_atomic_get_connector_state);
332 * drm_atomic_set_crtc_for_plane - set crtc for plane
333 * @plane_state: atomic state object for the plane
334 * @crtc: crtc to use for the plane
336 * Changing the assigned crtc for a plane requires us to grab the lock and state
337 * for the new crtc, as needed. This function takes care of all these details
338 * besides updating the pointer in the state object itself.
341 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
342 * then the w/w mutex code has detected a deadlock and the entire atomic
343 * sequence must be restarted. All other errors are fatal.
346 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
347 struct drm_crtc *crtc)
349 struct drm_crtc_state *crtc_state;
352 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
354 if (IS_ERR(crtc_state))
355 return PTR_ERR(crtc_state);
358 plane_state->crtc = crtc;
361 DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
362 plane_state, crtc->base.id);
364 DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
368 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
371 * drm_atomic_set_fb_for_plane - set crtc for plane
372 * @plane_state: atomic state object for the plane
373 * @fb: fb to use for the plane
375 * Changing the assigned framebuffer for a plane requires us to grab a reference
376 * to the new fb and drop the reference to the old fb, if there is one. This
377 * function takes care of all these details besides updating the pointer in the
378 * state object itself.
381 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
382 struct drm_framebuffer *fb)
385 drm_framebuffer_unreference(plane_state->fb);
387 drm_framebuffer_reference(fb);
388 plane_state->fb = fb;
391 DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
392 fb->base.id, plane_state);
394 DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
396 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
399 * drm_atomic_set_crtc_for_connector - set crtc for connector
400 * @conn_state: atomic state object for the connector
401 * @crtc: crtc to use for the connector
403 * Changing the assigned crtc for a connector requires us to grab the lock and
404 * state for the new crtc, as needed. This function takes care of all these
405 * details besides updating the pointer in the state object itself.
408 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
409 * then the w/w mutex code has detected a deadlock and the entire atomic
410 * sequence must be restarted. All other errors are fatal.
413 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
414 struct drm_crtc *crtc)
416 struct drm_crtc_state *crtc_state;
419 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
420 if (IS_ERR(crtc_state))
421 return PTR_ERR(crtc_state);
424 conn_state->crtc = crtc;
427 DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
428 conn_state, crtc->base.id);
430 DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
435 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
438 * drm_atomic_add_affected_connectors - add connectors for crtc
439 * @state: atomic state
442 * This function walks the current configuration and adds all connectors
443 * currently using @crtc to the atomic configuration @state. Note that this
444 * function must acquire the connection mutex. This can potentially cause
445 * unneeded seralization if the update is just for the planes on one crtc. Hence
446 * drivers and helpers should only call this when really needed (e.g. when a
447 * full modeset needs to happen due to some change).
450 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
451 * then the w/w mutex code has detected a deadlock and the entire atomic
452 * sequence must be restarted. All other errors are fatal.
455 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
456 struct drm_crtc *crtc)
458 struct drm_mode_config *config = &state->dev->mode_config;
459 struct drm_connector *connector;
460 struct drm_connector_state *conn_state;
463 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
467 DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
468 crtc->base.id, state);
471 * Changed connectors are already in @state, so only need to look at the
472 * current configuration.
474 list_for_each_entry(connector, &config->connector_list, head) {
475 if (connector->state->crtc != crtc)
478 conn_state = drm_atomic_get_connector_state(state, connector);
479 if (IS_ERR(conn_state))
480 return PTR_ERR(conn_state);
485 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
488 * drm_atomic_connectors_for_crtc - count number of connected outputs
489 * @state: atomic state
492 * This function counts all connectors which will be connected to @crtc
493 * according to @state. Useful to recompute the enable state for @crtc.
496 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
497 struct drm_crtc *crtc)
499 int nconnectors = state->dev->mode_config.num_connector;
500 int i, num_connected_connectors = 0;
502 for (i = 0; i < nconnectors; i++) {
503 struct drm_connector_state *conn_state;
505 conn_state = state->connector_states[i];
507 if (conn_state && conn_state->crtc == crtc)
508 num_connected_connectors++;
511 DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
512 state, num_connected_connectors, crtc->base.id);
514 return num_connected_connectors;
516 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
519 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
520 * @state: atomic state
522 * This function should be used by legacy entry points which don't understand
523 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
524 * the slowpath completed.
526 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
531 drm_modeset_backoff(state->acquire_ctx);
533 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
537 ret = drm_modeset_lock_all_crtcs(state->dev,
542 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
545 * drm_atomic_check_only - check whether a given config would work
546 * @state: atomic configuration to check
548 * Note that this function can return -EDEADLK if the driver needed to acquire
549 * more locks but encountered a deadlock. The caller must then do the usual w/w
550 * backoff dance and restart. All other errors are fatal.
553 * 0 on success, negative error code on failure.
555 int drm_atomic_check_only(struct drm_atomic_state *state)
557 struct drm_mode_config *config = &state->dev->mode_config;
559 DRM_DEBUG_KMS("checking %p\n", state);
561 if (config->funcs->atomic_check)
562 return config->funcs->atomic_check(state->dev, state);
566 EXPORT_SYMBOL(drm_atomic_check_only);
569 * drm_atomic_commit - commit configuration atomically
570 * @state: atomic configuration to check
572 * Note that this function can return -EDEADLK if the driver needed to acquire
573 * more locks but encountered a deadlock. The caller must then do the usual w/w
574 * backoff dance and restart. All other errors are fatal.
576 * Also note that on successful execution ownership of @state is transferred
577 * from the caller of this function to the function itself. The caller must not
578 * free or in any other way access @state. If the function fails then the caller
579 * must clean up @state itself.
582 * 0 on success, negative error code on failure.
584 int drm_atomic_commit(struct drm_atomic_state *state)
586 struct drm_mode_config *config = &state->dev->mode_config;
589 ret = drm_atomic_check_only(state);
593 DRM_DEBUG_KMS("commiting %p\n", state);
595 return config->funcs->atomic_commit(state->dev, state, false);
597 EXPORT_SYMBOL(drm_atomic_commit);
600 * drm_atomic_async_commit - atomic&async configuration commit
601 * @state: atomic configuration to check
603 * Note that this function can return -EDEADLK if the driver needed to acquire
604 * more locks but encountered a deadlock. The caller must then do the usual w/w
605 * backoff dance and restart. All other errors are fatal.
607 * Also note that on successful execution ownership of @state is transferred
608 * from the caller of this function to the function itself. The caller must not
609 * free or in any other way access @state. If the function fails then the caller
610 * must clean up @state itself.
613 * 0 on success, negative error code on failure.
615 int drm_atomic_async_commit(struct drm_atomic_state *state)
617 struct drm_mode_config *config = &state->dev->mode_config;
620 ret = drm_atomic_check_only(state);
624 DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
626 return config->funcs->atomic_commit(state->dev, state, true);
628 EXPORT_SYMBOL(drm_atomic_async_commit);