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 #include "drm_crtc_internal.h"
35 static void kfree_state(struct drm_atomic_state *state)
37 kfree(state->connectors);
38 kfree(state->connector_states);
40 kfree(state->crtc_states);
42 kfree(state->plane_states);
47 * drm_atomic_state_alloc - allocate atomic state
50 * This allocates an empty atomic state to track updates.
52 struct drm_atomic_state *
53 drm_atomic_state_alloc(struct drm_device *dev)
55 struct drm_atomic_state *state;
57 state = kzalloc(sizeof(*state), GFP_KERNEL);
61 /* TODO legacy paths should maybe do a better job about
62 * setting this appropriately?
64 state->allow_modeset = true;
66 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
68 state->crtcs = kcalloc(dev->mode_config.num_crtc,
69 sizeof(*state->crtcs), GFP_KERNEL);
72 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
73 sizeof(*state->crtc_states), GFP_KERNEL);
74 if (!state->crtc_states)
76 state->planes = kcalloc(dev->mode_config.num_total_plane,
77 sizeof(*state->planes), GFP_KERNEL);
80 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
81 sizeof(*state->plane_states), GFP_KERNEL);
82 if (!state->plane_states)
84 state->connectors = kcalloc(state->num_connector,
85 sizeof(*state->connectors),
87 if (!state->connectors)
89 state->connector_states = kcalloc(state->num_connector,
90 sizeof(*state->connector_states),
92 if (!state->connector_states)
97 DRM_DEBUG_ATOMIC("Allocate atomic state %p\n", state);
105 EXPORT_SYMBOL(drm_atomic_state_alloc);
108 * drm_atomic_state_clear - clear state object
109 * @state: atomic state
111 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
112 * all locks. So someone else could sneak in and change the current modeset
113 * configuration. Which means that all the state assembled in @state is no
114 * longer an atomic update to the current state, but to some arbitrary earlier
115 * state. Which could break assumptions the driver's ->atomic_check likely
118 * Hence we must clear all cached state and completely start over, using this
121 void drm_atomic_state_clear(struct drm_atomic_state *state)
123 struct drm_device *dev = state->dev;
124 struct drm_mode_config *config = &dev->mode_config;
127 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
129 for (i = 0; i < state->num_connector; i++) {
130 struct drm_connector *connector = state->connectors[i];
135 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
137 connector->funcs->atomic_destroy_state(connector,
138 state->connector_states[i]);
139 state->connectors[i] = NULL;
140 state->connector_states[i] = NULL;
143 for (i = 0; i < config->num_crtc; i++) {
144 struct drm_crtc *crtc = state->crtcs[i];
149 crtc->funcs->atomic_destroy_state(crtc,
150 state->crtc_states[i]);
151 state->crtcs[i] = NULL;
152 state->crtc_states[i] = NULL;
155 for (i = 0; i < config->num_total_plane; i++) {
156 struct drm_plane *plane = state->planes[i];
161 plane->funcs->atomic_destroy_state(plane,
162 state->plane_states[i]);
163 state->planes[i] = NULL;
164 state->plane_states[i] = NULL;
167 EXPORT_SYMBOL(drm_atomic_state_clear);
170 * drm_atomic_state_free - free all memory for an atomic state
171 * @state: atomic state to deallocate
173 * This frees all memory associated with an atomic state, including all the
174 * per-object state for planes, crtcs and connectors.
176 void drm_atomic_state_free(struct drm_atomic_state *state)
181 drm_atomic_state_clear(state);
183 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
187 EXPORT_SYMBOL(drm_atomic_state_free);
190 * drm_atomic_get_crtc_state - get crtc state
191 * @state: global atomic state object
192 * @crtc: crtc to get state object for
194 * This function returns the crtc state for the given crtc, allocating it if
195 * needed. It will also grab the relevant crtc lock to make sure that the state
200 * Either the allocated state or the error code encoded into the pointer. When
201 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
202 * entire atomic sequence must be restarted. All other errors are fatal.
204 struct drm_crtc_state *
205 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
206 struct drm_crtc *crtc)
209 struct drm_crtc_state *crtc_state;
211 index = drm_crtc_index(crtc);
213 if (state->crtc_states[index])
214 return state->crtc_states[index];
216 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
220 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
222 return ERR_PTR(-ENOMEM);
224 state->crtc_states[index] = crtc_state;
225 state->crtcs[index] = crtc;
226 crtc_state->state = state;
228 DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
229 crtc->base.id, crtc_state, state);
233 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
236 * drm_atomic_crtc_set_property - set property on CRTC
237 * @crtc: the drm CRTC to set a property on
238 * @state: the state object to update with the new property value
239 * @property: the property to set
240 * @val: the new property value
242 * Use this instead of calling crtc->atomic_set_property directly.
243 * This function handles generic/core properties and calls out to
244 * driver's ->atomic_set_property() for driver properties. To ensure
245 * consistent behavior you must call this function rather than the
246 * driver hook directly.
249 * Zero on success, error code on failure
251 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
252 struct drm_crtc_state *state, struct drm_property *property,
255 struct drm_device *dev = crtc->dev;
256 struct drm_mode_config *config = &dev->mode_config;
258 /* FIXME: Mode prop is missing, which also controls ->enable. */
259 if (property == config->prop_active)
261 else if (crtc->funcs->atomic_set_property)
262 return crtc->funcs->atomic_set_property(crtc, state, property, val);
268 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
271 * This function handles generic/core properties and calls out to
272 * driver's ->atomic_get_property() for driver properties. To ensure
273 * consistent behavior you must call this function rather than the
274 * driver hook directly.
276 static int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
277 const struct drm_crtc_state *state,
278 struct drm_property *property, uint64_t *val)
280 struct drm_device *dev = crtc->dev;
281 struct drm_mode_config *config = &dev->mode_config;
283 if (property == config->prop_active)
284 *val = state->active;
285 else if (crtc->funcs->atomic_get_property)
286 return crtc->funcs->atomic_get_property(crtc, state, property, val);
294 * drm_atomic_crtc_check - check crtc state
295 * @crtc: crtc to check
296 * @state: crtc state to check
298 * Provides core sanity checks for crtc state.
301 * Zero on success, error code on failure
303 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
304 struct drm_crtc_state *state)
306 /* NOTE: we explicitly don't enforce constraints such as primary
307 * layer covering entire screen, since that is something we want
308 * to allow (on hw that supports it). For hw that does not, it
309 * should be checked in driver's crtc->atomic_check() vfunc.
311 * TODO: Add generic modeset state checks once we support those.
314 if (state->active && !state->enable) {
315 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
324 * drm_atomic_get_plane_state - get plane state
325 * @state: global atomic state object
326 * @plane: plane to get state object for
328 * This function returns the plane state for the given plane, allocating it if
329 * needed. It will also grab the relevant plane lock to make sure that the state
334 * Either the allocated state or the error code encoded into the pointer. When
335 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
336 * entire atomic sequence must be restarted. All other errors are fatal.
338 struct drm_plane_state *
339 drm_atomic_get_plane_state(struct drm_atomic_state *state,
340 struct drm_plane *plane)
343 struct drm_plane_state *plane_state;
345 index = drm_plane_index(plane);
347 if (state->plane_states[index])
348 return state->plane_states[index];
350 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
354 plane_state = plane->funcs->atomic_duplicate_state(plane);
356 return ERR_PTR(-ENOMEM);
358 state->plane_states[index] = plane_state;
359 state->planes[index] = plane;
360 plane_state->state = state;
362 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
363 plane->base.id, plane_state, state);
365 if (plane_state->crtc) {
366 struct drm_crtc_state *crtc_state;
368 crtc_state = drm_atomic_get_crtc_state(state,
370 if (IS_ERR(crtc_state))
371 return ERR_CAST(crtc_state);
376 EXPORT_SYMBOL(drm_atomic_get_plane_state);
379 * drm_atomic_plane_set_property - set property on plane
380 * @plane: the drm plane to set a property on
381 * @state: the state object to update with the new property value
382 * @property: the property to set
383 * @val: the new property value
385 * Use this instead of calling plane->atomic_set_property directly.
386 * This function handles generic/core properties and calls out to
387 * driver's ->atomic_set_property() for driver properties. To ensure
388 * consistent behavior you must call this function rather than the
389 * driver hook directly.
392 * Zero on success, error code on failure
394 int drm_atomic_plane_set_property(struct drm_plane *plane,
395 struct drm_plane_state *state, struct drm_property *property,
398 struct drm_device *dev = plane->dev;
399 struct drm_mode_config *config = &dev->mode_config;
401 if (property == config->prop_fb_id) {
402 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
403 drm_atomic_set_fb_for_plane(state, fb);
405 drm_framebuffer_unreference(fb);
406 } else if (property == config->prop_crtc_id) {
407 struct drm_crtc *crtc = drm_crtc_find(dev, val);
408 return drm_atomic_set_crtc_for_plane(state, crtc);
409 } else if (property == config->prop_crtc_x) {
410 state->crtc_x = U642I64(val);
411 } else if (property == config->prop_crtc_y) {
412 state->crtc_y = U642I64(val);
413 } else if (property == config->prop_crtc_w) {
415 } else if (property == config->prop_crtc_h) {
417 } else if (property == config->prop_src_x) {
419 } else if (property == config->prop_src_y) {
421 } else if (property == config->prop_src_w) {
423 } else if (property == config->prop_src_h) {
425 } else if (property == config->rotation_property) {
426 state->rotation = val;
427 } else if (plane->funcs->atomic_set_property) {
428 return plane->funcs->atomic_set_property(plane, state,
436 EXPORT_SYMBOL(drm_atomic_plane_set_property);
439 * This function handles generic/core properties and calls out to
440 * driver's ->atomic_get_property() for driver properties. To ensure
441 * consistent behavior you must call this function rather than the
442 * driver hook directly.
445 drm_atomic_plane_get_property(struct drm_plane *plane,
446 const struct drm_plane_state *state,
447 struct drm_property *property, uint64_t *val)
449 struct drm_device *dev = plane->dev;
450 struct drm_mode_config *config = &dev->mode_config;
452 if (property == config->prop_fb_id) {
453 *val = (state->fb) ? state->fb->base.id : 0;
454 } else if (property == config->prop_crtc_id) {
455 *val = (state->crtc) ? state->crtc->base.id : 0;
456 } else if (property == config->prop_crtc_x) {
457 *val = I642U64(state->crtc_x);
458 } else if (property == config->prop_crtc_y) {
459 *val = I642U64(state->crtc_y);
460 } else if (property == config->prop_crtc_w) {
461 *val = state->crtc_w;
462 } else if (property == config->prop_crtc_h) {
463 *val = state->crtc_h;
464 } else if (property == config->prop_src_x) {
466 } else if (property == config->prop_src_y) {
468 } else if (property == config->prop_src_w) {
470 } else if (property == config->prop_src_h) {
472 } else if (property == config->rotation_property) {
473 *val = state->rotation;
474 } else if (plane->funcs->atomic_get_property) {
475 return plane->funcs->atomic_get_property(plane, state, property, val);
484 * drm_atomic_plane_check - check plane state
485 * @plane: plane to check
486 * @state: plane state to check
488 * Provides core sanity checks for plane state.
491 * Zero on success, error code on failure
493 static int drm_atomic_plane_check(struct drm_plane *plane,
494 struct drm_plane_state *state)
496 unsigned int fb_width, fb_height;
499 /* either *both* CRTC and FB must be set, or neither */
500 if (WARN_ON(state->crtc && !state->fb)) {
501 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
503 } else if (WARN_ON(state->fb && !state->crtc)) {
504 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
508 /* if disabled, we don't care about the rest of the state: */
512 /* Check whether this plane is usable on this CRTC */
513 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
514 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
518 /* Check whether this plane supports the fb pixel format. */
519 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
521 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
522 drm_get_format_name(state->fb->pixel_format));
526 /* Give drivers some help against integer overflows */
527 if (state->crtc_w > INT_MAX ||
528 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
529 state->crtc_h > INT_MAX ||
530 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
531 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
532 state->crtc_w, state->crtc_h,
533 state->crtc_x, state->crtc_y);
537 fb_width = state->fb->width << 16;
538 fb_height = state->fb->height << 16;
540 /* Make sure source coordinates are inside the fb. */
541 if (state->src_w > fb_width ||
542 state->src_x > fb_width - state->src_w ||
543 state->src_h > fb_height ||
544 state->src_y > fb_height - state->src_h) {
545 DRM_DEBUG_ATOMIC("Invalid source coordinates "
546 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
547 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
548 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
549 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
550 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
558 * drm_atomic_get_connector_state - get connector state
559 * @state: global atomic state object
560 * @connector: connector to get state object for
562 * This function returns the connector state for the given connector,
563 * allocating it if needed. It will also grab the relevant connector lock to
564 * make sure that the state is consistent.
568 * Either the allocated state or the error code encoded into the pointer. When
569 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
570 * entire atomic sequence must be restarted. All other errors are fatal.
572 struct drm_connector_state *
573 drm_atomic_get_connector_state(struct drm_atomic_state *state,
574 struct drm_connector *connector)
577 struct drm_mode_config *config = &connector->dev->mode_config;
578 struct drm_connector_state *connector_state;
580 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
584 index = drm_connector_index(connector);
587 * Construction of atomic state updates can race with a connector
588 * hot-add which might overflow. In this case flip the table and just
589 * restart the entire ioctl - no one is fast enough to livelock a cpu
590 * with physical hotplug events anyway.
592 * Note that we only grab the indexes once we have the right lock to
593 * prevent hotplug/unplugging of connectors. So removal is no problem,
594 * at most the array is a bit too large.
596 if (index >= state->num_connector) {
597 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
598 return ERR_PTR(-EAGAIN);
601 if (state->connector_states[index])
602 return state->connector_states[index];
604 connector_state = connector->funcs->atomic_duplicate_state(connector);
605 if (!connector_state)
606 return ERR_PTR(-ENOMEM);
608 state->connector_states[index] = connector_state;
609 state->connectors[index] = connector;
610 connector_state->state = state;
612 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
613 connector->base.id, connector_state, state);
615 if (connector_state->crtc) {
616 struct drm_crtc_state *crtc_state;
618 crtc_state = drm_atomic_get_crtc_state(state,
619 connector_state->crtc);
620 if (IS_ERR(crtc_state))
621 return ERR_CAST(crtc_state);
624 return connector_state;
626 EXPORT_SYMBOL(drm_atomic_get_connector_state);
629 * drm_atomic_connector_set_property - set property on connector.
630 * @connector: the drm connector to set a property on
631 * @state: the state object to update with the new property value
632 * @property: the property to set
633 * @val: the new property value
635 * Use this instead of calling connector->atomic_set_property directly.
636 * This function handles generic/core properties and calls out to
637 * driver's ->atomic_set_property() for driver properties. To ensure
638 * consistent behavior you must call this function rather than the
639 * driver hook directly.
642 * Zero on success, error code on failure
644 int drm_atomic_connector_set_property(struct drm_connector *connector,
645 struct drm_connector_state *state, struct drm_property *property,
648 struct drm_device *dev = connector->dev;
649 struct drm_mode_config *config = &dev->mode_config;
651 if (property == config->prop_crtc_id) {
652 struct drm_crtc *crtc = drm_crtc_find(dev, val);
653 return drm_atomic_set_crtc_for_connector(state, crtc);
654 } else if (property == config->dpms_property) {
655 /* setting DPMS property requires special handling, which
656 * is done in legacy setprop path for us. Disallow (for
657 * now?) atomic writes to DPMS property:
660 } else if (connector->funcs->atomic_set_property) {
661 return connector->funcs->atomic_set_property(connector,
662 state, property, val);
667 EXPORT_SYMBOL(drm_atomic_connector_set_property);
670 * This function handles generic/core properties and calls out to
671 * driver's ->atomic_get_property() for driver properties. To ensure
672 * consistent behavior you must call this function rather than the
673 * driver hook directly.
676 drm_atomic_connector_get_property(struct drm_connector *connector,
677 const struct drm_connector_state *state,
678 struct drm_property *property, uint64_t *val)
680 struct drm_device *dev = connector->dev;
681 struct drm_mode_config *config = &dev->mode_config;
683 if (property == config->prop_crtc_id) {
684 *val = (state->crtc) ? state->crtc->base.id : 0;
685 } else if (property == config->dpms_property) {
686 *val = connector->dpms;
687 } else if (connector->funcs->atomic_get_property) {
688 return connector->funcs->atomic_get_property(connector,
689 state, property, val);
697 int drm_atomic_get_property(struct drm_mode_object *obj,
698 struct drm_property *property, uint64_t *val)
700 struct drm_device *dev = property->dev;
704 case DRM_MODE_OBJECT_CONNECTOR: {
705 struct drm_connector *connector = obj_to_connector(obj);
706 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
707 ret = drm_atomic_connector_get_property(connector,
708 connector->state, property, val);
711 case DRM_MODE_OBJECT_CRTC: {
712 struct drm_crtc *crtc = obj_to_crtc(obj);
713 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
714 ret = drm_atomic_crtc_get_property(crtc,
715 crtc->state, property, val);
718 case DRM_MODE_OBJECT_PLANE: {
719 struct drm_plane *plane = obj_to_plane(obj);
720 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
721 ret = drm_atomic_plane_get_property(plane,
722 plane->state, property, val);
734 * drm_atomic_set_crtc_for_plane - set crtc for plane
735 * @plane_state: the plane whose incoming state to update
736 * @crtc: crtc to use for the plane
738 * Changing the assigned crtc for a plane requires us to grab the lock and state
739 * for the new crtc, as needed. This function takes care of all these details
740 * besides updating the pointer in the state object itself.
743 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
744 * then the w/w mutex code has detected a deadlock and the entire atomic
745 * sequence must be restarted. All other errors are fatal.
748 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
749 struct drm_crtc *crtc)
751 struct drm_plane *plane = plane_state->plane;
752 struct drm_crtc_state *crtc_state;
754 if (plane_state->crtc) {
755 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
757 if (WARN_ON(IS_ERR(crtc_state)))
758 return PTR_ERR(crtc_state);
760 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
763 plane_state->crtc = crtc;
766 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
768 if (IS_ERR(crtc_state))
769 return PTR_ERR(crtc_state);
770 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
774 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
775 plane_state, crtc->base.id);
777 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
782 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
785 * drm_atomic_set_fb_for_plane - set framebuffer for plane
786 * @plane_state: atomic state object for the plane
787 * @fb: fb to use for the plane
789 * Changing the assigned framebuffer for a plane requires us to grab a reference
790 * to the new fb and drop the reference to the old fb, if there is one. This
791 * function takes care of all these details besides updating the pointer in the
792 * state object itself.
795 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
796 struct drm_framebuffer *fb)
799 drm_framebuffer_unreference(plane_state->fb);
801 drm_framebuffer_reference(fb);
802 plane_state->fb = fb;
805 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
806 fb->base.id, plane_state);
808 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
811 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
814 * drm_atomic_set_crtc_for_connector - set crtc for connector
815 * @conn_state: atomic state object for the connector
816 * @crtc: crtc to use for the connector
818 * Changing the assigned crtc for a connector requires us to grab the lock and
819 * state for the new crtc, as needed. This function takes care of all these
820 * details besides updating the pointer in the state object itself.
823 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
824 * then the w/w mutex code has detected a deadlock and the entire atomic
825 * sequence must be restarted. All other errors are fatal.
828 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
829 struct drm_crtc *crtc)
831 struct drm_crtc_state *crtc_state;
834 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
835 if (IS_ERR(crtc_state))
836 return PTR_ERR(crtc_state);
839 conn_state->crtc = crtc;
842 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
843 conn_state, crtc->base.id);
845 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
850 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
853 * drm_atomic_add_affected_connectors - add connectors for crtc
854 * @state: atomic state
857 * This function walks the current configuration and adds all connectors
858 * currently using @crtc to the atomic configuration @state. Note that this
859 * function must acquire the connection mutex. This can potentially cause
860 * unneeded seralization if the update is just for the planes on one crtc. Hence
861 * drivers and helpers should only call this when really needed (e.g. when a
862 * full modeset needs to happen due to some change).
865 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
866 * then the w/w mutex code has detected a deadlock and the entire atomic
867 * sequence must be restarted. All other errors are fatal.
870 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
871 struct drm_crtc *crtc)
873 struct drm_mode_config *config = &state->dev->mode_config;
874 struct drm_connector *connector;
875 struct drm_connector_state *conn_state;
878 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
882 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
883 crtc->base.id, state);
886 * Changed connectors are already in @state, so only need to look at the
887 * current configuration.
889 list_for_each_entry(connector, &config->connector_list, head) {
890 if (connector->state->crtc != crtc)
893 conn_state = drm_atomic_get_connector_state(state, connector);
894 if (IS_ERR(conn_state))
895 return PTR_ERR(conn_state);
900 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
903 * drm_atomic_connectors_for_crtc - count number of connected outputs
904 * @state: atomic state
907 * This function counts all connectors which will be connected to @crtc
908 * according to @state. Useful to recompute the enable state for @crtc.
911 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
912 struct drm_crtc *crtc)
914 struct drm_connector *connector;
915 struct drm_connector_state *conn_state;
917 int i, num_connected_connectors = 0;
919 for_each_connector_in_state(state, connector, conn_state, i) {
920 if (conn_state->crtc == crtc)
921 num_connected_connectors++;
924 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
925 state, num_connected_connectors, crtc->base.id);
927 return num_connected_connectors;
929 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
932 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
933 * @state: atomic state
935 * This function should be used by legacy entry points which don't understand
936 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
937 * the slowpath completed.
939 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
944 drm_modeset_backoff(state->acquire_ctx);
946 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
950 ret = drm_modeset_lock_all_crtcs(state->dev,
955 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
958 * drm_atomic_check_only - check whether a given config would work
959 * @state: atomic configuration to check
961 * Note that this function can return -EDEADLK if the driver needed to acquire
962 * more locks but encountered a deadlock. The caller must then do the usual w/w
963 * backoff dance and restart. All other errors are fatal.
966 * 0 on success, negative error code on failure.
968 int drm_atomic_check_only(struct drm_atomic_state *state)
970 struct drm_device *dev = state->dev;
971 struct drm_mode_config *config = &dev->mode_config;
972 struct drm_plane *plane;
973 struct drm_plane_state *plane_state;
974 struct drm_crtc *crtc;
975 struct drm_crtc_state *crtc_state;
978 DRM_DEBUG_ATOMIC("checking %p\n", state);
980 for_each_plane_in_state(state, plane, plane_state, i) {
981 ret = drm_atomic_plane_check(plane, plane_state);
983 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
989 for_each_crtc_in_state(state, crtc, crtc_state, i) {
990 ret = drm_atomic_crtc_check(crtc, crtc_state);
992 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
998 if (config->funcs->atomic_check)
999 ret = config->funcs->atomic_check(state->dev, state);
1001 if (!state->allow_modeset) {
1002 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1003 if (crtc_state->mode_changed ||
1004 crtc_state->active_changed) {
1005 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1014 EXPORT_SYMBOL(drm_atomic_check_only);
1017 * drm_atomic_commit - commit configuration atomically
1018 * @state: atomic configuration to check
1020 * Note that this function can return -EDEADLK if the driver needed to acquire
1021 * more locks but encountered a deadlock. The caller must then do the usual w/w
1022 * backoff dance and restart. All other errors are fatal.
1024 * Also note that on successful execution ownership of @state is transferred
1025 * from the caller of this function to the function itself. The caller must not
1026 * free or in any other way access @state. If the function fails then the caller
1027 * must clean up @state itself.
1030 * 0 on success, negative error code on failure.
1032 int drm_atomic_commit(struct drm_atomic_state *state)
1034 struct drm_mode_config *config = &state->dev->mode_config;
1037 ret = drm_atomic_check_only(state);
1041 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1043 return config->funcs->atomic_commit(state->dev, state, false);
1045 EXPORT_SYMBOL(drm_atomic_commit);
1048 * drm_atomic_async_commit - atomic&async configuration commit
1049 * @state: atomic configuration to check
1051 * Note that this function can return -EDEADLK if the driver needed to acquire
1052 * more locks but encountered a deadlock. The caller must then do the usual w/w
1053 * backoff dance and restart. All other errors are fatal.
1055 * Also note that on successful execution ownership of @state is transferred
1056 * from the caller of this function to the function itself. The caller must not
1057 * free or in any other way access @state. If the function fails then the caller
1058 * must clean up @state itself.
1061 * 0 on success, negative error code on failure.
1063 int drm_atomic_async_commit(struct drm_atomic_state *state)
1065 struct drm_mode_config *config = &state->dev->mode_config;
1068 ret = drm_atomic_check_only(state);
1072 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1074 return config->funcs->atomic_commit(state->dev, state, true);
1076 EXPORT_SYMBOL(drm_atomic_async_commit);
1079 * The big monstor ioctl
1082 static struct drm_pending_vblank_event *create_vblank_event(
1083 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1085 struct drm_pending_vblank_event *e = NULL;
1087 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
1088 if (file_priv->event_space < sizeof e->event) {
1089 lockmgr(&dev->event_lock, LK_RELEASE);
1092 file_priv->event_space -= sizeof e->event;
1093 lockmgr(&dev->event_lock, LK_RELEASE);
1095 e = kzalloc(sizeof *e, GFP_KERNEL);
1097 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
1098 file_priv->event_space += sizeof e->event;
1099 lockmgr(&dev->event_lock, LK_RELEASE);
1103 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1104 e->event.base.length = sizeof e->event;
1105 e->event.user_data = user_data;
1106 e->base.event = &e->event.base;
1107 e->base.file_priv = file_priv;
1108 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1114 static void destroy_vblank_event(struct drm_device *dev,
1115 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1118 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
1119 file_priv->event_space += sizeof e->event;
1120 lockmgr(&dev->event_lock, LK_RELEASE);
1124 static int atomic_set_prop(struct drm_atomic_state *state,
1125 struct drm_mode_object *obj, struct drm_property *prop,
1126 uint64_t prop_value)
1128 struct drm_mode_object *ref;
1131 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1134 switch (obj->type) {
1135 case DRM_MODE_OBJECT_CONNECTOR: {
1136 struct drm_connector *connector = obj_to_connector(obj);
1137 struct drm_connector_state *connector_state;
1139 connector_state = drm_atomic_get_connector_state(state, connector);
1140 if (IS_ERR(connector_state)) {
1141 ret = PTR_ERR(connector_state);
1145 ret = drm_atomic_connector_set_property(connector,
1146 connector_state, prop, prop_value);
1149 case DRM_MODE_OBJECT_CRTC: {
1150 struct drm_crtc *crtc = obj_to_crtc(obj);
1151 struct drm_crtc_state *crtc_state;
1153 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1154 if (IS_ERR(crtc_state)) {
1155 ret = PTR_ERR(crtc_state);
1159 ret = drm_atomic_crtc_set_property(crtc,
1160 crtc_state, prop, prop_value);
1163 case DRM_MODE_OBJECT_PLANE: {
1164 struct drm_plane *plane = obj_to_plane(obj);
1165 struct drm_plane_state *plane_state;
1167 plane_state = drm_atomic_get_plane_state(state, plane);
1168 if (IS_ERR(plane_state)) {
1169 ret = PTR_ERR(plane_state);
1173 ret = drm_atomic_plane_set_property(plane,
1174 plane_state, prop, prop_value);
1182 drm_property_change_valid_put(prop, ref);
1186 int drm_mode_atomic_ioctl(struct drm_device *dev,
1187 void *data, struct drm_file *file_priv)
1189 struct drm_mode_atomic *arg = data;
1190 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1191 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1192 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1193 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1194 unsigned int copied_objs, copied_props;
1195 struct drm_atomic_state *state;
1196 struct drm_modeset_acquire_ctx ctx;
1197 struct drm_plane *plane;
1198 struct drm_crtc *crtc;
1199 struct drm_crtc_state *crtc_state;
1200 unsigned plane_mask = 0;
1204 /* disallow for drivers not supporting atomic: */
1205 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1208 /* disallow for userspace that has not enabled atomic cap (even
1209 * though this may be a bit overkill, since legacy userspace
1210 * wouldn't know how to call this ioctl)
1212 if (!file_priv->atomic)
1215 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1221 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1222 !dev->mode_config.async_page_flip)
1225 /* can't test and expect an event at the same time. */
1226 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1227 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1230 drm_modeset_acquire_init(&ctx, 0);
1232 state = drm_atomic_state_alloc(dev);
1236 state->acquire_ctx = &ctx;
1237 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1243 for (i = 0; i < arg->count_objs; i++) {
1244 uint32_t obj_id, count_props;
1245 struct drm_mode_object *obj;
1247 if (get_user(obj_id, objs_ptr + copied_objs)) {
1252 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1253 if (!obj || !obj->properties) {
1258 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1259 plane = obj_to_plane(obj);
1260 plane_mask |= (1 << drm_plane_index(plane));
1261 plane->old_fb = plane->fb;
1264 if (get_user(count_props, count_props_ptr + copied_objs)) {
1271 for (j = 0; j < count_props; j++) {
1273 uint64_t prop_value;
1274 struct drm_property *prop;
1276 if (get_user(prop_id, props_ptr + copied_props)) {
1281 prop = drm_property_find(dev, prop_id);
1287 if (copy_from_user(&prop_value,
1288 prop_values_ptr + copied_props,
1289 sizeof(prop_value))) {
1294 ret = atomic_set_prop(state, obj, prop, prop_value);
1302 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1303 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1304 struct drm_pending_vblank_event *e;
1306 e = create_vblank_event(dev, file_priv, arg->user_data);
1312 crtc_state->event = e;
1316 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1317 ret = drm_atomic_check_only(state);
1318 /* _check_only() does not free state, unlike _commit() */
1319 drm_atomic_state_free(state);
1320 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1321 ret = drm_atomic_async_commit(state);
1323 ret = drm_atomic_commit(state);
1326 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1327 * locks (ie. while it is still safe to deref plane->state). We
1328 * need to do this here because the driver entry points cannot
1329 * distinguish between legacy and atomic ioctls.
1331 drm_for_each_plane_mask(plane, dev, plane_mask) {
1333 struct drm_framebuffer *new_fb = plane->state->fb;
1335 drm_framebuffer_reference(new_fb);
1337 plane->crtc = plane->state->crtc;
1339 plane->old_fb = NULL;
1341 if (plane->old_fb) {
1342 drm_framebuffer_unreference(plane->old_fb);
1343 plane->old_fb = NULL;
1347 drm_modeset_drop_locks(&ctx);
1348 drm_modeset_acquire_fini(&ctx);
1353 if (ret == -EDEADLK)
1356 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1357 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1358 destroy_vblank_event(dev, file_priv, crtc_state->event);
1359 crtc_state->event = NULL;
1363 drm_atomic_state_free(state);
1365 drm_modeset_drop_locks(&ctx);
1366 drm_modeset_acquire_fini(&ctx);
1371 drm_atomic_state_clear(state);
1372 drm_modeset_backoff(&ctx);