drm/atomic: atomic plane properties
[linux-2.6/btrfs-unstable.git] / drivers / gpu / drm / drm_atomic.c
blob131d47f6f7a2431cea2834d61eb5fc438d875317
1 /*
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.
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
29 #include <drm/drmP.h>
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);
37 kfree(state->crtcs);
38 kfree(state->crtc_states);
39 kfree(state->planes);
40 kfree(state->plane_states);
41 kfree(state);
44 /**
45 * drm_atomic_state_alloc - allocate atomic state
46 * @dev: DRM device
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);
56 if (!state)
57 return NULL;
59 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
61 state->crtcs = kcalloc(dev->mode_config.num_crtc,
62 sizeof(*state->crtcs), GFP_KERNEL);
63 if (!state->crtcs)
64 goto fail;
65 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
66 sizeof(*state->crtc_states), GFP_KERNEL);
67 if (!state->crtc_states)
68 goto fail;
69 state->planes = kcalloc(dev->mode_config.num_total_plane,
70 sizeof(*state->planes), GFP_KERNEL);
71 if (!state->planes)
72 goto fail;
73 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
74 sizeof(*state->plane_states), GFP_KERNEL);
75 if (!state->plane_states)
76 goto fail;
77 state->connectors = kcalloc(state->num_connector,
78 sizeof(*state->connectors),
79 GFP_KERNEL);
80 if (!state->connectors)
81 goto fail;
82 state->connector_states = kcalloc(state->num_connector,
83 sizeof(*state->connector_states),
84 GFP_KERNEL);
85 if (!state->connector_states)
86 goto fail;
88 state->dev = dev;
90 DRM_DEBUG_KMS("Allocate atomic state %p\n", state);
92 return state;
93 fail:
94 kfree_state(state);
96 return NULL;
98 EXPORT_SYMBOL(drm_atomic_state_alloc);
101 * drm_atomic_state_clear - clear state object
102 * @state: atomic state
104 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
105 * all locks. So someone else could sneak in and change the current modeset
106 * configuration. Which means that all the state assembled in @state is no
107 * longer an atomic update to the current state, but to some arbitrary earlier
108 * state. Which could break assumptions the driver's ->atomic_check likely
109 * relies on.
111 * Hence we must clear all cached state and completely start over, using this
112 * function.
114 void drm_atomic_state_clear(struct drm_atomic_state *state)
116 struct drm_device *dev = state->dev;
117 struct drm_mode_config *config = &dev->mode_config;
118 int i;
120 DRM_DEBUG_KMS("Clearing atomic state %p\n", state);
122 for (i = 0; i < state->num_connector; i++) {
123 struct drm_connector *connector = state->connectors[i];
125 if (!connector)
126 continue;
128 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
130 connector->funcs->atomic_destroy_state(connector,
131 state->connector_states[i]);
134 for (i = 0; i < config->num_crtc; i++) {
135 struct drm_crtc *crtc = state->crtcs[i];
137 if (!crtc)
138 continue;
140 crtc->funcs->atomic_destroy_state(crtc,
141 state->crtc_states[i]);
144 for (i = 0; i < config->num_total_plane; i++) {
145 struct drm_plane *plane = state->planes[i];
147 if (!plane)
148 continue;
150 plane->funcs->atomic_destroy_state(plane,
151 state->plane_states[i]);
154 EXPORT_SYMBOL(drm_atomic_state_clear);
157 * drm_atomic_state_free - free all memory for an atomic state
158 * @state: atomic state to deallocate
160 * This frees all memory associated with an atomic state, including all the
161 * per-object state for planes, crtcs and connectors.
163 void drm_atomic_state_free(struct drm_atomic_state *state)
165 drm_atomic_state_clear(state);
167 DRM_DEBUG_KMS("Freeing atomic state %p\n", state);
169 kfree_state(state);
171 EXPORT_SYMBOL(drm_atomic_state_free);
174 * drm_atomic_get_crtc_state - get crtc state
175 * @state: global atomic state object
176 * @crtc: crtc to get state object for
178 * This function returns the crtc state for the given crtc, allocating it if
179 * needed. It will also grab the relevant crtc lock to make sure that the state
180 * is consistent.
182 * Returns:
184 * Either the allocated state or the error code encoded into the pointer. When
185 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
186 * entire atomic sequence must be restarted. All other errors are fatal.
188 struct drm_crtc_state *
189 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
190 struct drm_crtc *crtc)
192 int ret, index;
193 struct drm_crtc_state *crtc_state;
195 index = drm_crtc_index(crtc);
197 if (state->crtc_states[index])
198 return state->crtc_states[index];
200 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
201 if (ret)
202 return ERR_PTR(ret);
204 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
205 if (!crtc_state)
206 return ERR_PTR(-ENOMEM);
208 state->crtc_states[index] = crtc_state;
209 state->crtcs[index] = crtc;
210 crtc_state->state = state;
212 DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n",
213 crtc->base.id, crtc_state, state);
215 return crtc_state;
217 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
220 * drm_atomic_crtc_set_property - set property on CRTC
221 * @crtc: the drm CRTC to set a property on
222 * @state: the state object to update with the new property value
223 * @property: the property to set
224 * @val: the new property value
226 * Use this instead of calling crtc->atomic_set_property directly.
227 * This function handles generic/core properties and calls out to
228 * driver's ->atomic_set_property() for driver properties. To ensure
229 * consistent behavior you must call this function rather than the
230 * driver hook directly.
232 * RETURNS:
233 * Zero on success, error code on failure
235 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
236 struct drm_crtc_state *state, struct drm_property *property,
237 uint64_t val)
239 if (crtc->funcs->atomic_set_property)
240 return crtc->funcs->atomic_set_property(crtc, state, property, val);
241 return -EINVAL;
243 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
246 * drm_atomic_crtc_get_property - get property on CRTC
247 * @crtc: the drm CRTC to get a property on
248 * @state: the state object with the property value to read
249 * @property: the property to get
250 * @val: the property value (returned by reference)
252 * Use this instead of calling crtc->atomic_get_property directly.
253 * This function handles generic/core properties and calls out to
254 * driver's ->atomic_get_property() for driver properties. To ensure
255 * consistent behavior you must call this function rather than the
256 * driver hook directly.
258 * RETURNS:
259 * Zero on success, error code on failure
261 int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
262 const struct drm_crtc_state *state,
263 struct drm_property *property, uint64_t *val)
265 if (crtc->funcs->atomic_get_property)
266 return crtc->funcs->atomic_get_property(crtc, state, property, val);
267 return -EINVAL;
269 EXPORT_SYMBOL(drm_atomic_crtc_get_property);
272 * drm_atomic_crtc_check - check crtc state
273 * @crtc: crtc to check
274 * @state: crtc state to check
276 * Provides core sanity checks for crtc state.
278 * RETURNS:
279 * Zero on success, error code on failure
281 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
282 struct drm_crtc_state *state)
284 /* NOTE: we explicitly don't enforce constraints such as primary
285 * layer covering entire screen, since that is something we want
286 * to allow (on hw that supports it). For hw that does not, it
287 * should be checked in driver's crtc->atomic_check() vfunc.
289 * TODO: Add generic modeset state checks once we support those.
291 return 0;
295 * drm_atomic_get_plane_state - get plane state
296 * @state: global atomic state object
297 * @plane: plane to get state object for
299 * This function returns the plane state for the given plane, allocating it if
300 * needed. It will also grab the relevant plane lock to make sure that the state
301 * is consistent.
303 * Returns:
305 * Either the allocated state or the error code encoded into the pointer. When
306 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
307 * entire atomic sequence must be restarted. All other errors are fatal.
309 struct drm_plane_state *
310 drm_atomic_get_plane_state(struct drm_atomic_state *state,
311 struct drm_plane *plane)
313 int ret, index;
314 struct drm_plane_state *plane_state;
316 index = drm_plane_index(plane);
318 if (state->plane_states[index])
319 return state->plane_states[index];
321 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
322 if (ret)
323 return ERR_PTR(ret);
325 plane_state = plane->funcs->atomic_duplicate_state(plane);
326 if (!plane_state)
327 return ERR_PTR(-ENOMEM);
329 state->plane_states[index] = plane_state;
330 state->planes[index] = plane;
331 plane_state->state = state;
333 DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
334 plane->base.id, plane_state, state);
336 if (plane_state->crtc) {
337 struct drm_crtc_state *crtc_state;
339 crtc_state = drm_atomic_get_crtc_state(state,
340 plane_state->crtc);
341 if (IS_ERR(crtc_state))
342 return ERR_CAST(crtc_state);
345 return plane_state;
347 EXPORT_SYMBOL(drm_atomic_get_plane_state);
350 * drm_atomic_plane_set_property - set property on plane
351 * @plane: the drm plane to set a property on
352 * @state: the state object to update with the new property value
353 * @property: the property to set
354 * @val: the new property value
356 * Use this instead of calling plane->atomic_set_property directly.
357 * This function handles generic/core properties and calls out to
358 * driver's ->atomic_set_property() for driver properties. To ensure
359 * consistent behavior you must call this function rather than the
360 * driver hook directly.
362 * RETURNS:
363 * Zero on success, error code on failure
365 int drm_atomic_plane_set_property(struct drm_plane *plane,
366 struct drm_plane_state *state, struct drm_property *property,
367 uint64_t val)
369 struct drm_device *dev = plane->dev;
370 struct drm_mode_config *config = &dev->mode_config;
372 if (property == config->prop_fb_id) {
373 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
374 drm_atomic_set_fb_for_plane(state, fb);
375 if (fb)
376 drm_framebuffer_unreference(fb);
377 } else if (property == config->prop_crtc_id) {
378 struct drm_crtc *crtc = drm_crtc_find(dev, val);
379 return drm_atomic_set_crtc_for_plane(state, crtc);
380 } else if (property == config->prop_crtc_x) {
381 state->crtc_x = U642I64(val);
382 } else if (property == config->prop_crtc_y) {
383 state->crtc_y = U642I64(val);
384 } else if (property == config->prop_crtc_w) {
385 state->crtc_w = val;
386 } else if (property == config->prop_crtc_h) {
387 state->crtc_h = val;
388 } else if (property == config->prop_src_x) {
389 state->src_x = val;
390 } else if (property == config->prop_src_y) {
391 state->src_y = val;
392 } else if (property == config->prop_src_w) {
393 state->src_w = val;
394 } else if (property == config->prop_src_h) {
395 state->src_h = val;
396 } else if (plane->funcs->atomic_set_property) {
397 return plane->funcs->atomic_set_property(plane, state,
398 property, val);
399 } else {
400 return -EINVAL;
403 return 0;
405 EXPORT_SYMBOL(drm_atomic_plane_set_property);
408 * drm_atomic_plane_get_property - get property on plane
409 * @plane: the drm plane to get a property on
410 * @state: the state object with the property value to read
411 * @property: the property to get
412 * @val: the property value (returned by reference)
414 * Use this instead of calling plane->atomic_get_property directly.
415 * This function handles generic/core properties and calls out to
416 * driver's ->atomic_get_property() for driver properties. To ensure
417 * consistent behavior you must call this function rather than the
418 * driver hook directly.
420 * RETURNS:
421 * Zero on success, error code on failure
423 int drm_atomic_plane_get_property(struct drm_plane *plane,
424 const struct drm_plane_state *state,
425 struct drm_property *property, uint64_t *val)
427 struct drm_device *dev = plane->dev;
428 struct drm_mode_config *config = &dev->mode_config;
430 if (property == config->prop_fb_id) {
431 *val = (state->fb) ? state->fb->base.id : 0;
432 } else if (property == config->prop_crtc_id) {
433 *val = (state->crtc) ? state->crtc->base.id : 0;
434 } else if (property == config->prop_crtc_x) {
435 *val = I642U64(state->crtc_x);
436 } else if (property == config->prop_crtc_y) {
437 *val = I642U64(state->crtc_y);
438 } else if (property == config->prop_crtc_w) {
439 *val = state->crtc_w;
440 } else if (property == config->prop_crtc_h) {
441 *val = state->crtc_h;
442 } else if (property == config->prop_src_x) {
443 *val = state->src_x;
444 } else if (property == config->prop_src_y) {
445 *val = state->src_y;
446 } else if (property == config->prop_src_w) {
447 *val = state->src_w;
448 } else if (property == config->prop_src_h) {
449 *val = state->src_h;
450 } else if (plane->funcs->atomic_get_property) {
451 return plane->funcs->atomic_get_property(plane, state, property, val);
452 } else {
453 return -EINVAL;
456 return 0;
458 EXPORT_SYMBOL(drm_atomic_plane_get_property);
461 * drm_atomic_plane_check - check plane state
462 * @plane: plane to check
463 * @state: plane state to check
465 * Provides core sanity checks for plane state.
467 * RETURNS:
468 * Zero on success, error code on failure
470 static int drm_atomic_plane_check(struct drm_plane *plane,
471 struct drm_plane_state *state)
473 unsigned int fb_width, fb_height;
474 unsigned int i;
476 /* either *both* CRTC and FB must be set, or neither */
477 if (WARN_ON(state->crtc && !state->fb)) {
478 DRM_DEBUG_KMS("CRTC set but no FB\n");
479 return -EINVAL;
480 } else if (WARN_ON(state->fb && !state->crtc)) {
481 DRM_DEBUG_KMS("FB set but no CRTC\n");
482 return -EINVAL;
485 /* if disabled, we don't care about the rest of the state: */
486 if (!state->crtc)
487 return 0;
489 /* Check whether this plane is usable on this CRTC */
490 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
491 DRM_DEBUG_KMS("Invalid crtc for plane\n");
492 return -EINVAL;
495 /* Check whether this plane supports the fb pixel format. */
496 for (i = 0; i < plane->format_count; i++)
497 if (state->fb->pixel_format == plane->format_types[i])
498 break;
499 if (i == plane->format_count) {
500 DRM_DEBUG_KMS("Invalid pixel format %s\n",
501 drm_get_format_name(state->fb->pixel_format));
502 return -EINVAL;
505 /* Give drivers some help against integer overflows */
506 if (state->crtc_w > INT_MAX ||
507 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
508 state->crtc_h > INT_MAX ||
509 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
510 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
511 state->crtc_w, state->crtc_h,
512 state->crtc_x, state->crtc_y);
513 return -ERANGE;
516 fb_width = state->fb->width << 16;
517 fb_height = state->fb->height << 16;
519 /* Make sure source coordinates are inside the fb. */
520 if (state->src_w > fb_width ||
521 state->src_x > fb_width - state->src_w ||
522 state->src_h > fb_height ||
523 state->src_y > fb_height - state->src_h) {
524 DRM_DEBUG_KMS("Invalid source coordinates "
525 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
526 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
527 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
528 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
529 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
530 return -ENOSPC;
533 return 0;
537 * drm_atomic_get_connector_state - get connector state
538 * @state: global atomic state object
539 * @connector: connector to get state object for
541 * This function returns the connector state for the given connector,
542 * allocating it if needed. It will also grab the relevant connector lock to
543 * make sure that the state is consistent.
545 * Returns:
547 * Either the allocated state or the error code encoded into the pointer. When
548 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
549 * entire atomic sequence must be restarted. All other errors are fatal.
551 struct drm_connector_state *
552 drm_atomic_get_connector_state(struct drm_atomic_state *state,
553 struct drm_connector *connector)
555 int ret, index;
556 struct drm_mode_config *config = &connector->dev->mode_config;
557 struct drm_connector_state *connector_state;
559 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
560 if (ret)
561 return ERR_PTR(ret);
563 index = drm_connector_index(connector);
566 * Construction of atomic state updates can race with a connector
567 * hot-add which might overflow. In this case flip the table and just
568 * restart the entire ioctl - no one is fast enough to livelock a cpu
569 * with physical hotplug events anyway.
571 * Note that we only grab the indexes once we have the right lock to
572 * prevent hotplug/unplugging of connectors. So removal is no problem,
573 * at most the array is a bit too large.
575 if (index >= state->num_connector) {
576 DRM_DEBUG_KMS("Hot-added connector would overflow state array, restarting\n");
577 return ERR_PTR(-EAGAIN);
580 if (state->connector_states[index])
581 return state->connector_states[index];
583 connector_state = connector->funcs->atomic_duplicate_state(connector);
584 if (!connector_state)
585 return ERR_PTR(-ENOMEM);
587 state->connector_states[index] = connector_state;
588 state->connectors[index] = connector;
589 connector_state->state = state;
591 DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
592 connector->base.id, connector_state, state);
594 if (connector_state->crtc) {
595 struct drm_crtc_state *crtc_state;
597 crtc_state = drm_atomic_get_crtc_state(state,
598 connector_state->crtc);
599 if (IS_ERR(crtc_state))
600 return ERR_CAST(crtc_state);
603 return connector_state;
605 EXPORT_SYMBOL(drm_atomic_get_connector_state);
608 * drm_atomic_connector_set_property - set property on connector.
609 * @connector: the drm connector to set a property on
610 * @state: the state object to update with the new property value
611 * @property: the property to set
612 * @val: the new property value
614 * Use this instead of calling connector->atomic_set_property directly.
615 * This function handles generic/core properties and calls out to
616 * driver's ->atomic_set_property() for driver properties. To ensure
617 * consistent behavior you must call this function rather than the
618 * driver hook directly.
620 * RETURNS:
621 * Zero on success, error code on failure
623 int drm_atomic_connector_set_property(struct drm_connector *connector,
624 struct drm_connector_state *state, struct drm_property *property,
625 uint64_t val)
627 struct drm_device *dev = connector->dev;
628 struct drm_mode_config *config = &dev->mode_config;
630 if (property == config->dpms_property) {
631 /* setting DPMS property requires special handling, which
632 * is done in legacy setprop path for us. Disallow (for
633 * now?) atomic writes to DPMS property:
635 return -EINVAL;
636 } else if (connector->funcs->atomic_set_property) {
637 return connector->funcs->atomic_set_property(connector,
638 state, property, val);
639 } else {
640 return -EINVAL;
643 EXPORT_SYMBOL(drm_atomic_connector_set_property);
646 * drm_atomic_connector_get_property - get property on connector
647 * @connector: the drm connector to get a property on
648 * @state: the state object with the property value to read
649 * @property: the property to get
650 * @val: the property value (returned by reference)
652 * Use this instead of calling connector->atomic_get_property directly.
653 * This function handles generic/core properties and calls out to
654 * driver's ->atomic_get_property() for driver properties. To ensure
655 * consistent behavior you must call this function rather than the
656 * driver hook directly.
658 * RETURNS:
659 * Zero on success, error code on failure
661 int drm_atomic_connector_get_property(struct drm_connector *connector,
662 const struct drm_connector_state *state,
663 struct drm_property *property, uint64_t *val)
665 struct drm_device *dev = connector->dev;
666 struct drm_mode_config *config = &dev->mode_config;
668 if (property == config->dpms_property) {
669 *val = connector->dpms;
670 } else if (connector->funcs->atomic_get_property) {
671 return connector->funcs->atomic_get_property(connector,
672 state, property, val);
673 } else {
674 return -EINVAL;
677 return 0;
679 EXPORT_SYMBOL(drm_atomic_connector_get_property);
682 * drm_atomic_get_property - helper to read atomic property
683 * @obj: drm mode object whose property to read
684 * @property: the property to read
685 * @val: the read value, returned by reference
687 * RETURNS:
688 * Zero on success, error code on failure
690 int drm_atomic_get_property(struct drm_mode_object *obj,
691 struct drm_property *property, uint64_t *val)
693 struct drm_device *dev = property->dev;
694 int ret;
696 switch (obj->type) {
697 case DRM_MODE_OBJECT_CONNECTOR: {
698 struct drm_connector *connector = obj_to_connector(obj);
699 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
700 ret = drm_atomic_connector_get_property(connector,
701 connector->state, property, val);
702 break;
704 case DRM_MODE_OBJECT_CRTC: {
705 struct drm_crtc *crtc = obj_to_crtc(obj);
706 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
707 ret = drm_atomic_crtc_get_property(crtc,
708 crtc->state, property, val);
709 break;
711 case DRM_MODE_OBJECT_PLANE: {
712 struct drm_plane *plane = obj_to_plane(obj);
713 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
714 ret = drm_atomic_plane_get_property(plane,
715 plane->state, property, val);
716 break;
718 default:
719 ret = -EINVAL;
720 break;
723 return ret;
727 * drm_atomic_set_crtc_for_plane - set crtc for plane
728 * @plane_state: the plane whose incoming state to update
729 * @crtc: crtc to use for the plane
731 * Changing the assigned crtc for a plane requires us to grab the lock and state
732 * for the new crtc, as needed. This function takes care of all these details
733 * besides updating the pointer in the state object itself.
735 * Returns:
736 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
737 * then the w/w mutex code has detected a deadlock and the entire atomic
738 * sequence must be restarted. All other errors are fatal.
741 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
742 struct drm_crtc *crtc)
744 struct drm_plane *plane = plane_state->plane;
745 struct drm_crtc_state *crtc_state;
747 if (plane_state->crtc) {
748 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
749 plane_state->crtc);
750 if (WARN_ON(IS_ERR(crtc_state)))
751 return PTR_ERR(crtc_state);
753 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
756 plane_state->crtc = crtc;
758 if (crtc) {
759 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
760 crtc);
761 if (IS_ERR(crtc_state))
762 return PTR_ERR(crtc_state);
763 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
766 if (crtc)
767 DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
768 plane_state, crtc->base.id);
769 else
770 DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
772 return 0;
774 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
777 * drm_atomic_set_fb_for_plane - set crtc for plane
778 * @plane_state: atomic state object for the plane
779 * @fb: fb to use for the plane
781 * Changing the assigned framebuffer for a plane requires us to grab a reference
782 * to the new fb and drop the reference to the old fb, if there is one. This
783 * function takes care of all these details besides updating the pointer in the
784 * state object itself.
786 void
787 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
788 struct drm_framebuffer *fb)
790 if (plane_state->fb)
791 drm_framebuffer_unreference(plane_state->fb);
792 if (fb)
793 drm_framebuffer_reference(fb);
794 plane_state->fb = fb;
796 if (fb)
797 DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
798 fb->base.id, plane_state);
799 else
800 DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
802 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
805 * drm_atomic_set_crtc_for_connector - set crtc for connector
806 * @conn_state: atomic state object for the connector
807 * @crtc: crtc to use for the connector
809 * Changing the assigned crtc for a connector requires us to grab the lock and
810 * state for the new crtc, as needed. This function takes care of all these
811 * details besides updating the pointer in the state object itself.
813 * Returns:
814 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
815 * then the w/w mutex code has detected a deadlock and the entire atomic
816 * sequence must be restarted. All other errors are fatal.
819 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
820 struct drm_crtc *crtc)
822 struct drm_crtc_state *crtc_state;
824 if (crtc) {
825 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
826 if (IS_ERR(crtc_state))
827 return PTR_ERR(crtc_state);
830 conn_state->crtc = crtc;
832 if (crtc)
833 DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
834 conn_state, crtc->base.id);
835 else
836 DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
837 conn_state);
839 return 0;
841 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
844 * drm_atomic_add_affected_connectors - add connectors for crtc
845 * @state: atomic state
846 * @crtc: DRM crtc
848 * This function walks the current configuration and adds all connectors
849 * currently using @crtc to the atomic configuration @state. Note that this
850 * function must acquire the connection mutex. This can potentially cause
851 * unneeded seralization if the update is just for the planes on one crtc. Hence
852 * drivers and helpers should only call this when really needed (e.g. when a
853 * full modeset needs to happen due to some change).
855 * Returns:
856 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
857 * then the w/w mutex code has detected a deadlock and the entire atomic
858 * sequence must be restarted. All other errors are fatal.
861 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
862 struct drm_crtc *crtc)
864 struct drm_mode_config *config = &state->dev->mode_config;
865 struct drm_connector *connector;
866 struct drm_connector_state *conn_state;
867 int ret;
869 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
870 if (ret)
871 return ret;
873 DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
874 crtc->base.id, state);
877 * Changed connectors are already in @state, so only need to look at the
878 * current configuration.
880 list_for_each_entry(connector, &config->connector_list, head) {
881 if (connector->state->crtc != crtc)
882 continue;
884 conn_state = drm_atomic_get_connector_state(state, connector);
885 if (IS_ERR(conn_state))
886 return PTR_ERR(conn_state);
889 return 0;
891 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
894 * drm_atomic_connectors_for_crtc - count number of connected outputs
895 * @state: atomic state
896 * @crtc: DRM crtc
898 * This function counts all connectors which will be connected to @crtc
899 * according to @state. Useful to recompute the enable state for @crtc.
902 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
903 struct drm_crtc *crtc)
905 int i, num_connected_connectors = 0;
907 for (i = 0; i < state->num_connector; i++) {
908 struct drm_connector_state *conn_state;
910 conn_state = state->connector_states[i];
912 if (conn_state && conn_state->crtc == crtc)
913 num_connected_connectors++;
916 DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
917 state, num_connected_connectors, crtc->base.id);
919 return num_connected_connectors;
921 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
924 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
925 * @state: atomic state
927 * This function should be used by legacy entry points which don't understand
928 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
929 * the slowpath completed.
931 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
933 int ret;
935 retry:
936 drm_modeset_backoff(state->acquire_ctx);
938 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
939 state->acquire_ctx);
940 if (ret)
941 goto retry;
942 ret = drm_modeset_lock_all_crtcs(state->dev,
943 state->acquire_ctx);
944 if (ret)
945 goto retry;
947 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
950 * drm_atomic_check_only - check whether a given config would work
951 * @state: atomic configuration to check
953 * Note that this function can return -EDEADLK if the driver needed to acquire
954 * more locks but encountered a deadlock. The caller must then do the usual w/w
955 * backoff dance and restart. All other errors are fatal.
957 * Returns:
958 * 0 on success, negative error code on failure.
960 int drm_atomic_check_only(struct drm_atomic_state *state)
962 struct drm_device *dev = state->dev;
963 struct drm_mode_config *config = &dev->mode_config;
964 int nplanes = config->num_total_plane;
965 int ncrtcs = config->num_crtc;
966 int i, ret = 0;
968 DRM_DEBUG_KMS("checking %p\n", state);
970 for (i = 0; i < nplanes; i++) {
971 struct drm_plane *plane = state->planes[i];
973 if (!plane)
974 continue;
976 ret = drm_atomic_plane_check(plane, state->plane_states[i]);
977 if (ret) {
978 DRM_DEBUG_KMS("[PLANE:%d] atomic core check failed\n",
979 plane->base.id);
980 return ret;
984 for (i = 0; i < ncrtcs; i++) {
985 struct drm_crtc *crtc = state->crtcs[i];
987 if (!crtc)
988 continue;
990 ret = drm_atomic_crtc_check(crtc, state->crtc_states[i]);
991 if (ret) {
992 DRM_DEBUG_KMS("[CRTC:%d] atomic core check failed\n",
993 crtc->base.id);
994 return ret;
998 if (config->funcs->atomic_check)
999 ret = config->funcs->atomic_check(state->dev, state);
1001 return ret;
1003 EXPORT_SYMBOL(drm_atomic_check_only);
1006 * drm_atomic_commit - commit configuration atomically
1007 * @state: atomic configuration to check
1009 * Note that this function can return -EDEADLK if the driver needed to acquire
1010 * more locks but encountered a deadlock. The caller must then do the usual w/w
1011 * backoff dance and restart. All other errors are fatal.
1013 * Also note that on successful execution ownership of @state is transferred
1014 * from the caller of this function to the function itself. The caller must not
1015 * free or in any other way access @state. If the function fails then the caller
1016 * must clean up @state itself.
1018 * Returns:
1019 * 0 on success, negative error code on failure.
1021 int drm_atomic_commit(struct drm_atomic_state *state)
1023 struct drm_mode_config *config = &state->dev->mode_config;
1024 int ret;
1026 ret = drm_atomic_check_only(state);
1027 if (ret)
1028 return ret;
1030 DRM_DEBUG_KMS("commiting %p\n", state);
1032 return config->funcs->atomic_commit(state->dev, state, false);
1034 EXPORT_SYMBOL(drm_atomic_commit);
1037 * drm_atomic_async_commit - atomic&async configuration commit
1038 * @state: atomic configuration to check
1040 * Note that this function can return -EDEADLK if the driver needed to acquire
1041 * more locks but encountered a deadlock. The caller must then do the usual w/w
1042 * backoff dance and restart. All other errors are fatal.
1044 * Also note that on successful execution ownership of @state is transferred
1045 * from the caller of this function to the function itself. The caller must not
1046 * free or in any other way access @state. If the function fails then the caller
1047 * must clean up @state itself.
1049 * Returns:
1050 * 0 on success, negative error code on failure.
1052 int drm_atomic_async_commit(struct drm_atomic_state *state)
1054 struct drm_mode_config *config = &state->dev->mode_config;
1055 int ret;
1057 ret = drm_atomic_check_only(state);
1058 if (ret)
1059 return ret;
1061 DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
1063 return config->funcs->atomic_commit(state->dev, state, true);
1065 EXPORT_SYMBOL(drm_atomic_async_commit);