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 /* TODO legacy paths should maybe do a better job about
60 * setting this appropriately?
62 state->allow_modeset = true;
64 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
66 state->crtcs = kcalloc(dev->mode_config.num_crtc,
67 sizeof(*state->crtcs), GFP_KERNEL);
70 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
71 sizeof(*state->crtc_states), GFP_KERNEL);
72 if (!state->crtc_states)
74 state->planes = kcalloc(dev->mode_config.num_total_plane,
75 sizeof(*state->planes), GFP_KERNEL);
78 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
79 sizeof(*state->plane_states), GFP_KERNEL);
80 if (!state->plane_states)
82 state->connectors = kcalloc(state->num_connector,
83 sizeof(*state->connectors),
85 if (!state->connectors)
87 state->connector_states = kcalloc(state->num_connector,
88 sizeof(*state->connector_states),
90 if (!state->connector_states)
95 DRM_DEBUG_ATOMIC("Allocate atomic state %p\n", state);
103 EXPORT_SYMBOL(drm_atomic_state_alloc);
106 * drm_atomic_state_clear - clear state object
107 * @state: atomic state
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
116 * Hence we must clear all cached state and completely start over, using this
119 void drm_atomic_state_clear(struct drm_atomic_state *state)
121 struct drm_device *dev = state->dev;
122 struct drm_mode_config *config = &dev->mode_config;
125 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
127 for (i = 0; i < state->num_connector; i++) {
128 struct drm_connector *connector = state->connectors[i];
133 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
135 connector->funcs->atomic_destroy_state(connector,
136 state->connector_states[i]);
137 state->connector_states[i] = NULL;
140 for (i = 0; i < config->num_crtc; i++) {
141 struct drm_crtc *crtc = state->crtcs[i];
146 crtc->funcs->atomic_destroy_state(crtc,
147 state->crtc_states[i]);
148 state->crtc_states[i] = NULL;
151 for (i = 0; i < config->num_total_plane; i++) {
152 struct drm_plane *plane = state->planes[i];
157 plane->funcs->atomic_destroy_state(plane,
158 state->plane_states[i]);
159 state->plane_states[i] = NULL;
162 EXPORT_SYMBOL(drm_atomic_state_clear);
165 * drm_atomic_state_free - free all memory for an atomic state
166 * @state: atomic state to deallocate
168 * This frees all memory associated with an atomic state, including all the
169 * per-object state for planes, crtcs and connectors.
171 void drm_atomic_state_free(struct drm_atomic_state *state)
173 drm_atomic_state_clear(state);
175 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
179 EXPORT_SYMBOL(drm_atomic_state_free);
182 * drm_atomic_get_crtc_state - get crtc state
183 * @state: global atomic state object
184 * @crtc: crtc to get state object for
186 * This function returns the crtc state for the given crtc, allocating it if
187 * needed. It will also grab the relevant crtc lock to make sure that the state
192 * Either the allocated state or the error code encoded into the pointer. When
193 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
194 * entire atomic sequence must be restarted. All other errors are fatal.
196 struct drm_crtc_state *
197 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
198 struct drm_crtc *crtc)
201 struct drm_crtc_state *crtc_state;
203 index = drm_crtc_index(crtc);
205 if (state->crtc_states[index])
206 return state->crtc_states[index];
208 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
212 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
214 return ERR_PTR(-ENOMEM);
216 state->crtc_states[index] = crtc_state;
217 state->crtcs[index] = crtc;
218 crtc_state->state = state;
220 DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
221 crtc->base.id, crtc_state, state);
225 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
228 * drm_atomic_crtc_set_property - set property on CRTC
229 * @crtc: the drm CRTC to set a property on
230 * @state: the state object to update with the new property value
231 * @property: the property to set
232 * @val: the new property value
234 * Use this instead of calling crtc->atomic_set_property directly.
235 * This function handles generic/core properties and calls out to
236 * driver's ->atomic_set_property() for driver properties. To ensure
237 * consistent behavior you must call this function rather than the
238 * driver hook directly.
241 * Zero on success, error code on failure
243 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
244 struct drm_crtc_state *state, struct drm_property *property,
247 struct drm_device *dev = crtc->dev;
248 struct drm_mode_config *config = &dev->mode_config;
250 /* FIXME: Mode prop is missing, which also controls ->enable. */
251 if (property == config->prop_active) {
253 } else if (crtc->funcs->atomic_set_property)
254 return crtc->funcs->atomic_set_property(crtc, state, property, val);
257 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
260 * This function handles generic/core properties and calls out to
261 * driver's ->atomic_get_property() for driver properties. To ensure
262 * consistent behavior you must call this function rather than the
263 * driver hook directly.
265 int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
266 const struct drm_crtc_state *state,
267 struct drm_property *property, uint64_t *val)
269 if (crtc->funcs->atomic_get_property)
270 return crtc->funcs->atomic_get_property(crtc, state, property, val);
275 * drm_atomic_crtc_check - check crtc state
276 * @crtc: crtc to check
277 * @state: crtc state to check
279 * Provides core sanity checks for crtc state.
282 * Zero on success, error code on failure
284 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
285 struct drm_crtc_state *state)
287 /* NOTE: we explicitly don't enforce constraints such as primary
288 * layer covering entire screen, since that is something we want
289 * to allow (on hw that supports it). For hw that does not, it
290 * should be checked in driver's crtc->atomic_check() vfunc.
292 * TODO: Add generic modeset state checks once we support those.
295 if (state->active && !state->enable) {
296 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
305 * drm_atomic_get_plane_state - get plane state
306 * @state: global atomic state object
307 * @plane: plane to get state object for
309 * This function returns the plane state for the given plane, allocating it if
310 * needed. It will also grab the relevant plane lock to make sure that the state
315 * Either the allocated state or the error code encoded into the pointer. When
316 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
317 * entire atomic sequence must be restarted. All other errors are fatal.
319 struct drm_plane_state *
320 drm_atomic_get_plane_state(struct drm_atomic_state *state,
321 struct drm_plane *plane)
324 struct drm_plane_state *plane_state;
326 index = drm_plane_index(plane);
328 if (state->plane_states[index])
329 return state->plane_states[index];
331 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
335 plane_state = plane->funcs->atomic_duplicate_state(plane);
337 return ERR_PTR(-ENOMEM);
339 state->plane_states[index] = plane_state;
340 state->planes[index] = plane;
341 plane_state->state = state;
343 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
344 plane->base.id, plane_state, state);
346 if (plane_state->crtc) {
347 struct drm_crtc_state *crtc_state;
349 crtc_state = drm_atomic_get_crtc_state(state,
351 if (IS_ERR(crtc_state))
352 return ERR_CAST(crtc_state);
357 EXPORT_SYMBOL(drm_atomic_get_plane_state);
360 * drm_atomic_plane_set_property - set property on plane
361 * @plane: the drm plane to set a property on
362 * @state: the state object to update with the new property value
363 * @property: the property to set
364 * @val: the new property value
366 * Use this instead of calling plane->atomic_set_property directly.
367 * This function handles generic/core properties and calls out to
368 * driver's ->atomic_set_property() for driver properties. To ensure
369 * consistent behavior you must call this function rather than the
370 * driver hook directly.
373 * Zero on success, error code on failure
375 int drm_atomic_plane_set_property(struct drm_plane *plane,
376 struct drm_plane_state *state, struct drm_property *property,
379 struct drm_device *dev = plane->dev;
380 struct drm_mode_config *config = &dev->mode_config;
382 if (property == config->prop_fb_id) {
383 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
384 drm_atomic_set_fb_for_plane(state, fb);
386 drm_framebuffer_unreference(fb);
387 } else if (property == config->prop_crtc_id) {
388 struct drm_crtc *crtc = drm_crtc_find(dev, val);
389 return drm_atomic_set_crtc_for_plane(state, crtc);
390 } else if (property == config->prop_crtc_x) {
391 state->crtc_x = U642I64(val);
392 } else if (property == config->prop_crtc_y) {
393 state->crtc_y = U642I64(val);
394 } else if (property == config->prop_crtc_w) {
396 } else if (property == config->prop_crtc_h) {
398 } else if (property == config->prop_src_x) {
400 } else if (property == config->prop_src_y) {
402 } else if (property == config->prop_src_w) {
404 } else if (property == config->prop_src_h) {
406 } else if (property == config->rotation_property) {
407 state->rotation = val;
408 } else if (plane->funcs->atomic_set_property) {
409 return plane->funcs->atomic_set_property(plane, state,
417 EXPORT_SYMBOL(drm_atomic_plane_set_property);
420 * This function handles generic/core properties and calls out to
421 * driver's ->atomic_get_property() for driver properties. To ensure
422 * consistent behavior you must call this function rather than the
423 * driver hook directly.
426 drm_atomic_plane_get_property(struct drm_plane *plane,
427 const struct drm_plane_state *state,
428 struct drm_property *property, uint64_t *val)
430 struct drm_device *dev = plane->dev;
431 struct drm_mode_config *config = &dev->mode_config;
433 if (property == config->prop_fb_id) {
434 *val = (state->fb) ? state->fb->base.id : 0;
435 } else if (property == config->prop_crtc_id) {
436 *val = (state->crtc) ? state->crtc->base.id : 0;
437 } else if (property == config->prop_crtc_x) {
438 *val = I642U64(state->crtc_x);
439 } else if (property == config->prop_crtc_y) {
440 *val = I642U64(state->crtc_y);
441 } else if (property == config->prop_crtc_w) {
442 *val = state->crtc_w;
443 } else if (property == config->prop_crtc_h) {
444 *val = state->crtc_h;
445 } else if (property == config->prop_src_x) {
447 } else if (property == config->prop_src_y) {
449 } else if (property == config->prop_src_w) {
451 } else if (property == config->prop_src_h) {
453 } else if (property == config->rotation_property) {
454 *val = state->rotation;
455 } else if (plane->funcs->atomic_get_property) {
456 return plane->funcs->atomic_get_property(plane, state, property, val);
465 * drm_atomic_plane_check - check plane state
466 * @plane: plane to check
467 * @state: plane state to check
469 * Provides core sanity checks for plane state.
472 * Zero on success, error code on failure
474 static int drm_atomic_plane_check(struct drm_plane *plane,
475 struct drm_plane_state *state)
477 unsigned int fb_width, fb_height;
480 /* either *both* CRTC and FB must be set, or neither */
481 if (WARN_ON(state->crtc && !state->fb)) {
482 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
484 } else if (WARN_ON(state->fb && !state->crtc)) {
485 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
489 /* if disabled, we don't care about the rest of the state: */
493 /* Check whether this plane is usable on this CRTC */
494 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
495 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
499 /* Check whether this plane supports the fb pixel format. */
500 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
502 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
503 drm_get_format_name(state->fb->pixel_format));
507 /* Give drivers some help against integer overflows */
508 if (state->crtc_w > INT_MAX ||
509 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
510 state->crtc_h > INT_MAX ||
511 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
512 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
513 state->crtc_w, state->crtc_h,
514 state->crtc_x, state->crtc_y);
518 fb_width = state->fb->width << 16;
519 fb_height = state->fb->height << 16;
521 /* Make sure source coordinates are inside the fb. */
522 if (state->src_w > fb_width ||
523 state->src_x > fb_width - state->src_w ||
524 state->src_h > fb_height ||
525 state->src_y > fb_height - state->src_h) {
526 DRM_DEBUG_ATOMIC("Invalid source coordinates "
527 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
528 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
529 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
530 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
531 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
539 * drm_atomic_get_connector_state - get connector state
540 * @state: global atomic state object
541 * @connector: connector to get state object for
543 * This function returns the connector state for the given connector,
544 * allocating it if needed. It will also grab the relevant connector lock to
545 * make sure that the state is consistent.
549 * Either the allocated state or the error code encoded into the pointer. When
550 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
551 * entire atomic sequence must be restarted. All other errors are fatal.
553 struct drm_connector_state *
554 drm_atomic_get_connector_state(struct drm_atomic_state *state,
555 struct drm_connector *connector)
558 struct drm_mode_config *config = &connector->dev->mode_config;
559 struct drm_connector_state *connector_state;
561 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
565 index = drm_connector_index(connector);
568 * Construction of atomic state updates can race with a connector
569 * hot-add which might overflow. In this case flip the table and just
570 * restart the entire ioctl - no one is fast enough to livelock a cpu
571 * with physical hotplug events anyway.
573 * Note that we only grab the indexes once we have the right lock to
574 * prevent hotplug/unplugging of connectors. So removal is no problem,
575 * at most the array is a bit too large.
577 if (index >= state->num_connector) {
578 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
579 return ERR_PTR(-EAGAIN);
582 if (state->connector_states[index])
583 return state->connector_states[index];
585 connector_state = connector->funcs->atomic_duplicate_state(connector);
586 if (!connector_state)
587 return ERR_PTR(-ENOMEM);
589 state->connector_states[index] = connector_state;
590 state->connectors[index] = connector;
591 connector_state->state = state;
593 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
594 connector->base.id, connector_state, state);
596 if (connector_state->crtc) {
597 struct drm_crtc_state *crtc_state;
599 crtc_state = drm_atomic_get_crtc_state(state,
600 connector_state->crtc);
601 if (IS_ERR(crtc_state))
602 return ERR_CAST(crtc_state);
605 return connector_state;
607 EXPORT_SYMBOL(drm_atomic_get_connector_state);
610 * drm_atomic_connector_set_property - set property on connector.
611 * @connector: the drm connector to set a property on
612 * @state: the state object to update with the new property value
613 * @property: the property to set
614 * @val: the new property value
616 * Use this instead of calling connector->atomic_set_property directly.
617 * This function handles generic/core properties and calls out to
618 * driver's ->atomic_set_property() for driver properties. To ensure
619 * consistent behavior you must call this function rather than the
620 * driver hook directly.
623 * Zero on success, error code on failure
625 int drm_atomic_connector_set_property(struct drm_connector *connector,
626 struct drm_connector_state *state, struct drm_property *property,
629 struct drm_device *dev = connector->dev;
630 struct drm_mode_config *config = &dev->mode_config;
632 if (property == config->prop_crtc_id) {
633 struct drm_crtc *crtc = drm_crtc_find(dev, val);
634 return drm_atomic_set_crtc_for_connector(state, crtc);
635 } else if (property == config->dpms_property) {
636 /* setting DPMS property requires special handling, which
637 * is done in legacy setprop path for us. Disallow (for
638 * now?) atomic writes to DPMS property:
641 } else if (connector->funcs->atomic_set_property) {
642 return connector->funcs->atomic_set_property(connector,
643 state, property, val);
648 EXPORT_SYMBOL(drm_atomic_connector_set_property);
651 * This function handles generic/core properties and calls out to
652 * driver's ->atomic_get_property() for driver properties. To ensure
653 * consistent behavior you must call this function rather than the
654 * driver hook directly.
657 drm_atomic_connector_get_property(struct drm_connector *connector,
658 const struct drm_connector_state *state,
659 struct drm_property *property, uint64_t *val)
661 struct drm_device *dev = connector->dev;
662 struct drm_mode_config *config = &dev->mode_config;
664 if (property == config->prop_crtc_id) {
665 *val = (state->crtc) ? state->crtc->base.id : 0;
666 } else if (property == config->dpms_property) {
667 *val = connector->dpms;
668 } else if (connector->funcs->atomic_get_property) {
669 return connector->funcs->atomic_get_property(connector,
670 state, property, val);
678 int drm_atomic_get_property(struct drm_mode_object *obj,
679 struct drm_property *property, uint64_t *val)
681 struct drm_device *dev = property->dev;
685 case DRM_MODE_OBJECT_CONNECTOR: {
686 struct drm_connector *connector = obj_to_connector(obj);
687 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
688 ret = drm_atomic_connector_get_property(connector,
689 connector->state, property, val);
692 case DRM_MODE_OBJECT_CRTC: {
693 struct drm_crtc *crtc = obj_to_crtc(obj);
694 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
695 ret = drm_atomic_crtc_get_property(crtc,
696 crtc->state, property, val);
699 case DRM_MODE_OBJECT_PLANE: {
700 struct drm_plane *plane = obj_to_plane(obj);
701 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
702 ret = drm_atomic_plane_get_property(plane,
703 plane->state, property, val);
715 * drm_atomic_set_crtc_for_plane - set crtc for plane
716 * @plane_state: the plane whose incoming state to update
717 * @crtc: crtc to use for the plane
719 * Changing the assigned crtc for a plane requires us to grab the lock and state
720 * for the new crtc, as needed. This function takes care of all these details
721 * besides updating the pointer in the state object itself.
724 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
725 * then the w/w mutex code has detected a deadlock and the entire atomic
726 * sequence must be restarted. All other errors are fatal.
729 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
730 struct drm_crtc *crtc)
732 struct drm_plane *plane = plane_state->plane;
733 struct drm_crtc_state *crtc_state;
735 if (plane_state->crtc) {
736 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
738 if (WARN_ON(IS_ERR(crtc_state)))
739 return PTR_ERR(crtc_state);
741 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
744 plane_state->crtc = crtc;
747 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
749 if (IS_ERR(crtc_state))
750 return PTR_ERR(crtc_state);
751 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
755 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
756 plane_state, crtc->base.id);
758 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
763 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
766 * drm_atomic_set_fb_for_plane - set crtc for plane
767 * @plane_state: atomic state object for the plane
768 * @fb: fb to use for the plane
770 * Changing the assigned framebuffer for a plane requires us to grab a reference
771 * to the new fb and drop the reference to the old fb, if there is one. This
772 * function takes care of all these details besides updating the pointer in the
773 * state object itself.
776 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
777 struct drm_framebuffer *fb)
780 drm_framebuffer_unreference(plane_state->fb);
782 drm_framebuffer_reference(fb);
783 plane_state->fb = fb;
786 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
787 fb->base.id, plane_state);
789 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
792 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
795 * drm_atomic_set_crtc_for_connector - set crtc for connector
796 * @conn_state: atomic state object for the connector
797 * @crtc: crtc to use for the connector
799 * Changing the assigned crtc for a connector requires us to grab the lock and
800 * state for the new crtc, as needed. This function takes care of all these
801 * details besides updating the pointer in the state object itself.
804 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
805 * then the w/w mutex code has detected a deadlock and the entire atomic
806 * sequence must be restarted. All other errors are fatal.
809 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
810 struct drm_crtc *crtc)
812 struct drm_crtc_state *crtc_state;
815 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
816 if (IS_ERR(crtc_state))
817 return PTR_ERR(crtc_state);
820 conn_state->crtc = crtc;
823 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
824 conn_state, crtc->base.id);
826 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
831 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
834 * drm_atomic_add_affected_connectors - add connectors for crtc
835 * @state: atomic state
838 * This function walks the current configuration and adds all connectors
839 * currently using @crtc to the atomic configuration @state. Note that this
840 * function must acquire the connection mutex. This can potentially cause
841 * unneeded seralization if the update is just for the planes on one crtc. Hence
842 * drivers and helpers should only call this when really needed (e.g. when a
843 * full modeset needs to happen due to some change).
846 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
847 * then the w/w mutex code has detected a deadlock and the entire atomic
848 * sequence must be restarted. All other errors are fatal.
851 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
852 struct drm_crtc *crtc)
854 struct drm_mode_config *config = &state->dev->mode_config;
855 struct drm_connector *connector;
856 struct drm_connector_state *conn_state;
859 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
863 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
864 crtc->base.id, state);
867 * Changed connectors are already in @state, so only need to look at the
868 * current configuration.
870 list_for_each_entry(connector, &config->connector_list, head) {
871 if (connector->state->crtc != crtc)
874 conn_state = drm_atomic_get_connector_state(state, connector);
875 if (IS_ERR(conn_state))
876 return PTR_ERR(conn_state);
881 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
884 * drm_atomic_connectors_for_crtc - count number of connected outputs
885 * @state: atomic state
888 * This function counts all connectors which will be connected to @crtc
889 * according to @state. Useful to recompute the enable state for @crtc.
892 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
893 struct drm_crtc *crtc)
895 int i, num_connected_connectors = 0;
897 for (i = 0; i < state->num_connector; i++) {
898 struct drm_connector_state *conn_state;
900 conn_state = state->connector_states[i];
902 if (conn_state && conn_state->crtc == crtc)
903 num_connected_connectors++;
906 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
907 state, num_connected_connectors, crtc->base.id);
909 return num_connected_connectors;
911 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
914 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
915 * @state: atomic state
917 * This function should be used by legacy entry points which don't understand
918 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
919 * the slowpath completed.
921 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
926 drm_modeset_backoff(state->acquire_ctx);
928 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
932 ret = drm_modeset_lock_all_crtcs(state->dev,
937 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
940 * drm_atomic_check_only - check whether a given config would work
941 * @state: atomic configuration to check
943 * Note that this function can return -EDEADLK if the driver needed to acquire
944 * more locks but encountered a deadlock. The caller must then do the usual w/w
945 * backoff dance and restart. All other errors are fatal.
948 * 0 on success, negative error code on failure.
950 int drm_atomic_check_only(struct drm_atomic_state *state)
952 struct drm_device *dev = state->dev;
953 struct drm_mode_config *config = &dev->mode_config;
954 int nplanes = config->num_total_plane;
955 int ncrtcs = config->num_crtc;
958 DRM_DEBUG_ATOMIC("checking %p\n", state);
960 for (i = 0; i < nplanes; i++) {
961 struct drm_plane *plane = state->planes[i];
966 ret = drm_atomic_plane_check(plane, state->plane_states[i]);
968 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
974 for (i = 0; i < ncrtcs; i++) {
975 struct drm_crtc *crtc = state->crtcs[i];
980 ret = drm_atomic_crtc_check(crtc, state->crtc_states[i]);
982 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
988 if (config->funcs->atomic_check)
989 ret = config->funcs->atomic_check(state->dev, state);
991 if (!state->allow_modeset) {
992 for (i = 0; i < ncrtcs; i++) {
993 struct drm_crtc *crtc = state->crtcs[i];
994 struct drm_crtc_state *crtc_state = state->crtc_states[i];
999 if (crtc_state->mode_changed ||
1000 crtc_state->active_changed) {
1001 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1010 EXPORT_SYMBOL(drm_atomic_check_only);
1013 * drm_atomic_commit - commit configuration atomically
1014 * @state: atomic configuration to check
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.
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.
1026 * 0 on success, negative error code on failure.
1028 int drm_atomic_commit(struct drm_atomic_state *state)
1030 struct drm_mode_config *config = &state->dev->mode_config;
1033 ret = drm_atomic_check_only(state);
1037 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1039 return config->funcs->atomic_commit(state->dev, state, false);
1041 EXPORT_SYMBOL(drm_atomic_commit);
1044 * drm_atomic_async_commit - atomic&async configuration commit
1045 * @state: atomic configuration to check
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.
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.
1057 * 0 on success, negative error code on failure.
1059 int drm_atomic_async_commit(struct drm_atomic_state *state)
1061 struct drm_mode_config *config = &state->dev->mode_config;
1064 ret = drm_atomic_check_only(state);
1068 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1070 return config->funcs->atomic_commit(state->dev, state, true);
1072 EXPORT_SYMBOL(drm_atomic_async_commit);
1075 * The big monstor ioctl
1078 static struct drm_pending_vblank_event *create_vblank_event(
1079 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1081 struct drm_pending_vblank_event *e = NULL;
1082 unsigned long flags;
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);
1089 file_priv->event_space -= sizeof e->event;
1090 spin_unlock_irqrestore(&dev->event_lock, flags);
1092 e = kzalloc(sizeof *e, GFP_KERNEL);
1094 spin_lock_irqsave(&dev->event_lock, flags);
1095 file_priv->event_space += sizeof e->event;
1096 spin_unlock_irqrestore(&dev->event_lock, flags);
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;
1111 static void destroy_vblank_event(struct drm_device *dev,
1112 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1114 unsigned long flags;
1116 spin_lock_irqsave(&dev->event_lock, flags);
1117 file_priv->event_space += sizeof e->event;
1118 spin_unlock_irqrestore(&dev->event_lock, flags);
1122 static int atomic_set_prop(struct drm_atomic_state *state,
1123 struct drm_mode_object *obj, struct drm_property *prop,
1124 uint64_t prop_value)
1126 struct drm_mode_object *ref;
1129 if (!drm_property_change_valid_get(prop, prop_value, &ref))
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;
1137 connector_state = drm_atomic_get_connector_state(state, connector);
1138 if (IS_ERR(connector_state)) {
1139 ret = PTR_ERR(connector_state);
1143 ret = drm_atomic_connector_set_property(connector,
1144 connector_state, prop, prop_value);
1147 case DRM_MODE_OBJECT_CRTC: {
1148 struct drm_crtc *crtc = obj_to_crtc(obj);
1149 struct drm_crtc_state *crtc_state;
1151 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1152 if (IS_ERR(crtc_state)) {
1153 ret = PTR_ERR(crtc_state);
1157 ret = drm_atomic_crtc_set_property(crtc,
1158 crtc_state, prop, prop_value);
1161 case DRM_MODE_OBJECT_PLANE: {
1162 struct drm_plane *plane = obj_to_plane(obj);
1163 struct drm_plane_state *plane_state;
1165 plane_state = drm_atomic_get_plane_state(state, plane);
1166 if (IS_ERR(plane_state)) {
1167 ret = PTR_ERR(plane_state);
1171 ret = drm_atomic_plane_set_property(plane,
1172 plane_state, prop, prop_value);
1180 drm_property_change_valid_put(prop, ref);
1184 int drm_mode_atomic_ioctl(struct drm_device *dev,
1185 void *data, struct drm_file *file_priv)
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;
1196 unsigned plane_mask = 0;
1200 /* disallow for drivers not supporting atomic: */
1201 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1204 /* disallow for userspace that has not enabled atomic cap (even
1205 * though this may be a bit overkill, since legacy userspace
1206 * wouldn't know how to call this ioctl)
1208 if (!file_priv->atomic)
1211 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1217 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1218 !dev->mode_config.async_page_flip)
1221 /* can't test and expect an event at the same time. */
1222 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1223 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1226 drm_modeset_acquire_init(&ctx, 0);
1228 state = drm_atomic_state_alloc(dev);
1232 state->acquire_ctx = &ctx;
1233 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1239 for (i = 0; i < arg->count_objs; i++) {
1240 uint32_t obj_id, count_props;
1241 struct drm_mode_object *obj;
1243 if (get_user(obj_id, objs_ptr + copied_objs)) {
1248 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1249 if (!obj || !obj->properties) {
1254 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1255 plane = obj_to_plane(obj);
1256 plane_mask |= (1 << drm_plane_index(plane));
1257 plane->old_fb = plane->fb;
1260 if (get_user(count_props, count_props_ptr + copied_objs)) {
1267 for (j = 0; j < count_props; j++) {
1269 uint64_t prop_value;
1270 struct drm_property *prop;
1272 if (get_user(prop_id, props_ptr + copied_props)) {
1277 prop = drm_property_find(dev, prop_id);
1283 if (copy_from_user(&prop_value,
1284 prop_values_ptr + copied_props,
1285 sizeof(prop_value))) {
1290 ret = atomic_set_prop(state, obj, prop, prop_value);
1298 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1299 int ncrtcs = dev->mode_config.num_crtc;
1301 for (i = 0; i < ncrtcs; i++) {
1302 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1303 struct drm_pending_vblank_event *e;
1308 e = create_vblank_event(dev, file_priv, arg->user_data);
1314 crtc_state->event = e;
1318 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1319 ret = drm_atomic_check_only(state);
1320 /* _check_only() does not free state, unlike _commit() */
1321 drm_atomic_state_free(state);
1322 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1323 ret = drm_atomic_async_commit(state);
1325 ret = drm_atomic_commit(state);
1328 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1329 * locks (ie. while it is still safe to deref plane->state). We
1330 * need to do this here because the driver entry points cannot
1331 * distinguish between legacy and atomic ioctls.
1333 drm_for_each_plane_mask(plane, dev, plane_mask) {
1335 struct drm_framebuffer *new_fb = plane->state->fb;
1337 drm_framebuffer_reference(new_fb);
1339 plane->crtc = plane->state->crtc;
1341 plane->old_fb = NULL;
1343 if (plane->old_fb) {
1344 drm_framebuffer_unreference(plane->old_fb);
1345 plane->old_fb = NULL;
1349 drm_modeset_drop_locks(&ctx);
1350 drm_modeset_acquire_fini(&ctx);
1355 if (ret == -EDEADLK)
1358 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1359 int ncrtcs = dev->mode_config.num_crtc;
1361 for (i = 0; i < ncrtcs; i++) {
1362 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1367 destroy_vblank_event(dev, file_priv, crtc_state->event);
1368 crtc_state->event = NULL;
1372 drm_atomic_state_free(state);
1374 drm_modeset_drop_locks(&ctx);
1375 drm_modeset_acquire_fini(&ctx);
1380 drm_atomic_state_clear(state);
1381 drm_modeset_backoff(&ctx);