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);
260 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
263 * This function handles generic/core properties and calls out to
264 * driver's ->atomic_get_property() for driver properties. To ensure
265 * consistent behavior you must call this function rather than the
266 * driver hook directly.
268 int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
269 const struct drm_crtc_state *state,
270 struct drm_property *property, uint64_t *val)
272 struct drm_device *dev = crtc->dev;
273 struct drm_mode_config *config = &dev->mode_config;
275 if (property == config->prop_active)
276 *val = state->active;
277 else if (crtc->funcs->atomic_get_property)
278 return crtc->funcs->atomic_get_property(crtc, state, property, val);
286 * drm_atomic_crtc_check - check crtc state
287 * @crtc: crtc to check
288 * @state: crtc state to check
290 * Provides core sanity checks for crtc state.
293 * Zero on success, error code on failure
295 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
296 struct drm_crtc_state *state)
298 /* NOTE: we explicitly don't enforce constraints such as primary
299 * layer covering entire screen, since that is something we want
300 * to allow (on hw that supports it). For hw that does not, it
301 * should be checked in driver's crtc->atomic_check() vfunc.
303 * TODO: Add generic modeset state checks once we support those.
306 if (state->active && !state->enable) {
307 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
316 * drm_atomic_get_plane_state - get plane state
317 * @state: global atomic state object
318 * @plane: plane to get state object for
320 * This function returns the plane state for the given plane, allocating it if
321 * needed. It will also grab the relevant plane lock to make sure that the state
326 * Either the allocated state or the error code encoded into the pointer. When
327 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
328 * entire atomic sequence must be restarted. All other errors are fatal.
330 struct drm_plane_state *
331 drm_atomic_get_plane_state(struct drm_atomic_state *state,
332 struct drm_plane *plane)
335 struct drm_plane_state *plane_state;
337 index = drm_plane_index(plane);
339 if (state->plane_states[index])
340 return state->plane_states[index];
342 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
346 plane_state = plane->funcs->atomic_duplicate_state(plane);
348 return ERR_PTR(-ENOMEM);
350 state->plane_states[index] = plane_state;
351 state->planes[index] = plane;
352 plane_state->state = state;
354 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
355 plane->base.id, plane_state, state);
357 if (plane_state->crtc) {
358 struct drm_crtc_state *crtc_state;
360 crtc_state = drm_atomic_get_crtc_state(state,
362 if (IS_ERR(crtc_state))
363 return ERR_CAST(crtc_state);
368 EXPORT_SYMBOL(drm_atomic_get_plane_state);
371 * drm_atomic_plane_set_property - set property on plane
372 * @plane: the drm plane to set a property on
373 * @state: the state object to update with the new property value
374 * @property: the property to set
375 * @val: the new property value
377 * Use this instead of calling plane->atomic_set_property directly.
378 * This function handles generic/core properties and calls out to
379 * driver's ->atomic_set_property() for driver properties. To ensure
380 * consistent behavior you must call this function rather than the
381 * driver hook directly.
384 * Zero on success, error code on failure
386 int drm_atomic_plane_set_property(struct drm_plane *plane,
387 struct drm_plane_state *state, struct drm_property *property,
390 struct drm_device *dev = plane->dev;
391 struct drm_mode_config *config = &dev->mode_config;
393 if (property == config->prop_fb_id) {
394 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
395 drm_atomic_set_fb_for_plane(state, fb);
397 drm_framebuffer_unreference(fb);
398 } else if (property == config->prop_crtc_id) {
399 struct drm_crtc *crtc = drm_crtc_find(dev, val);
400 return drm_atomic_set_crtc_for_plane(state, crtc);
401 } else if (property == config->prop_crtc_x) {
402 state->crtc_x = U642I64(val);
403 } else if (property == config->prop_crtc_y) {
404 state->crtc_y = U642I64(val);
405 } else if (property == config->prop_crtc_w) {
407 } else if (property == config->prop_crtc_h) {
409 } else if (property == config->prop_src_x) {
411 } else if (property == config->prop_src_y) {
413 } else if (property == config->prop_src_w) {
415 } else if (property == config->prop_src_h) {
417 } else if (property == config->rotation_property) {
418 state->rotation = val;
419 } else if (plane->funcs->atomic_set_property) {
420 return plane->funcs->atomic_set_property(plane, state,
428 EXPORT_SYMBOL(drm_atomic_plane_set_property);
431 * This function handles generic/core properties and calls out to
432 * driver's ->atomic_get_property() for driver properties. To ensure
433 * consistent behavior you must call this function rather than the
434 * driver hook directly.
437 drm_atomic_plane_get_property(struct drm_plane *plane,
438 const struct drm_plane_state *state,
439 struct drm_property *property, uint64_t *val)
441 struct drm_device *dev = plane->dev;
442 struct drm_mode_config *config = &dev->mode_config;
444 if (property == config->prop_fb_id) {
445 *val = (state->fb) ? state->fb->base.id : 0;
446 } else if (property == config->prop_crtc_id) {
447 *val = (state->crtc) ? state->crtc->base.id : 0;
448 } else if (property == config->prop_crtc_x) {
449 *val = I642U64(state->crtc_x);
450 } else if (property == config->prop_crtc_y) {
451 *val = I642U64(state->crtc_y);
452 } else if (property == config->prop_crtc_w) {
453 *val = state->crtc_w;
454 } else if (property == config->prop_crtc_h) {
455 *val = state->crtc_h;
456 } else if (property == config->prop_src_x) {
458 } else if (property == config->prop_src_y) {
460 } else if (property == config->prop_src_w) {
462 } else if (property == config->prop_src_h) {
464 } else if (property == config->rotation_property) {
465 *val = state->rotation;
466 } else if (plane->funcs->atomic_get_property) {
467 return plane->funcs->atomic_get_property(plane, state, property, val);
476 * drm_atomic_plane_check - check plane state
477 * @plane: plane to check
478 * @state: plane state to check
480 * Provides core sanity checks for plane state.
483 * Zero on success, error code on failure
485 static int drm_atomic_plane_check(struct drm_plane *plane,
486 struct drm_plane_state *state)
488 unsigned int fb_width, fb_height;
491 /* either *both* CRTC and FB must be set, or neither */
492 if (WARN_ON(state->crtc && !state->fb)) {
493 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
495 } else if (WARN_ON(state->fb && !state->crtc)) {
496 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
500 /* if disabled, we don't care about the rest of the state: */
504 /* Check whether this plane is usable on this CRTC */
505 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
506 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
510 /* Check whether this plane supports the fb pixel format. */
511 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
513 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
514 drm_get_format_name(state->fb->pixel_format));
518 /* Give drivers some help against integer overflows */
519 if (state->crtc_w > INT_MAX ||
520 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
521 state->crtc_h > INT_MAX ||
522 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
523 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
524 state->crtc_w, state->crtc_h,
525 state->crtc_x, state->crtc_y);
529 fb_width = state->fb->width << 16;
530 fb_height = state->fb->height << 16;
532 /* Make sure source coordinates are inside the fb. */
533 if (state->src_w > fb_width ||
534 state->src_x > fb_width - state->src_w ||
535 state->src_h > fb_height ||
536 state->src_y > fb_height - state->src_h) {
537 DRM_DEBUG_ATOMIC("Invalid source coordinates "
538 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
539 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
540 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
541 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
542 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
550 * drm_atomic_get_connector_state - get connector state
551 * @state: global atomic state object
552 * @connector: connector to get state object for
554 * This function returns the connector state for the given connector,
555 * allocating it if needed. It will also grab the relevant connector lock to
556 * make sure that the state is consistent.
560 * Either the allocated state or the error code encoded into the pointer. When
561 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
562 * entire atomic sequence must be restarted. All other errors are fatal.
564 struct drm_connector_state *
565 drm_atomic_get_connector_state(struct drm_atomic_state *state,
566 struct drm_connector *connector)
569 struct drm_mode_config *config = &connector->dev->mode_config;
570 struct drm_connector_state *connector_state;
572 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
576 index = drm_connector_index(connector);
579 * Construction of atomic state updates can race with a connector
580 * hot-add which might overflow. In this case flip the table and just
581 * restart the entire ioctl - no one is fast enough to livelock a cpu
582 * with physical hotplug events anyway.
584 * Note that we only grab the indexes once we have the right lock to
585 * prevent hotplug/unplugging of connectors. So removal is no problem,
586 * at most the array is a bit too large.
588 if (index >= state->num_connector) {
589 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
590 return ERR_PTR(-EAGAIN);
593 if (state->connector_states[index])
594 return state->connector_states[index];
596 connector_state = connector->funcs->atomic_duplicate_state(connector);
597 if (!connector_state)
598 return ERR_PTR(-ENOMEM);
600 state->connector_states[index] = connector_state;
601 state->connectors[index] = connector;
602 connector_state->state = state;
604 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
605 connector->base.id, connector_state, state);
607 if (connector_state->crtc) {
608 struct drm_crtc_state *crtc_state;
610 crtc_state = drm_atomic_get_crtc_state(state,
611 connector_state->crtc);
612 if (IS_ERR(crtc_state))
613 return ERR_CAST(crtc_state);
616 return connector_state;
618 EXPORT_SYMBOL(drm_atomic_get_connector_state);
621 * drm_atomic_connector_set_property - set property on connector.
622 * @connector: the drm connector to set a property on
623 * @state: the state object to update with the new property value
624 * @property: the property to set
625 * @val: the new property value
627 * Use this instead of calling connector->atomic_set_property directly.
628 * This function handles generic/core properties and calls out to
629 * driver's ->atomic_set_property() for driver properties. To ensure
630 * consistent behavior you must call this function rather than the
631 * driver hook directly.
634 * Zero on success, error code on failure
636 int drm_atomic_connector_set_property(struct drm_connector *connector,
637 struct drm_connector_state *state, struct drm_property *property,
640 struct drm_device *dev = connector->dev;
641 struct drm_mode_config *config = &dev->mode_config;
643 if (property == config->prop_crtc_id) {
644 struct drm_crtc *crtc = drm_crtc_find(dev, val);
645 return drm_atomic_set_crtc_for_connector(state, crtc);
646 } else if (property == config->dpms_property) {
647 /* setting DPMS property requires special handling, which
648 * is done in legacy setprop path for us. Disallow (for
649 * now?) atomic writes to DPMS property:
652 } else if (connector->funcs->atomic_set_property) {
653 return connector->funcs->atomic_set_property(connector,
654 state, property, val);
659 EXPORT_SYMBOL(drm_atomic_connector_set_property);
662 * This function handles generic/core properties and calls out to
663 * driver's ->atomic_get_property() for driver properties. To ensure
664 * consistent behavior you must call this function rather than the
665 * driver hook directly.
668 drm_atomic_connector_get_property(struct drm_connector *connector,
669 const struct drm_connector_state *state,
670 struct drm_property *property, uint64_t *val)
672 struct drm_device *dev = connector->dev;
673 struct drm_mode_config *config = &dev->mode_config;
675 if (property == config->prop_crtc_id) {
676 *val = (state->crtc) ? state->crtc->base.id : 0;
677 } else if (property == config->dpms_property) {
678 *val = connector->dpms;
679 } else if (connector->funcs->atomic_get_property) {
680 return connector->funcs->atomic_get_property(connector,
681 state, property, val);
689 int drm_atomic_get_property(struct drm_mode_object *obj,
690 struct drm_property *property, uint64_t *val)
692 struct drm_device *dev = property->dev;
696 case DRM_MODE_OBJECT_CONNECTOR: {
697 struct drm_connector *connector = obj_to_connector(obj);
698 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
699 ret = drm_atomic_connector_get_property(connector,
700 connector->state, property, val);
703 case DRM_MODE_OBJECT_CRTC: {
704 struct drm_crtc *crtc = obj_to_crtc(obj);
705 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
706 ret = drm_atomic_crtc_get_property(crtc,
707 crtc->state, property, val);
710 case DRM_MODE_OBJECT_PLANE: {
711 struct drm_plane *plane = obj_to_plane(obj);
712 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
713 ret = drm_atomic_plane_get_property(plane,
714 plane->state, property, val);
726 * drm_atomic_set_crtc_for_plane - set crtc for plane
727 * @plane_state: the plane whose incoming state to update
728 * @crtc: crtc to use for the plane
730 * Changing the assigned crtc for a plane requires us to grab the lock and state
731 * for the new crtc, as needed. This function takes care of all these details
732 * besides updating the pointer in the state object itself.
735 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
736 * then the w/w mutex code has detected a deadlock and the entire atomic
737 * sequence must be restarted. All other errors are fatal.
740 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
741 struct drm_crtc *crtc)
743 struct drm_plane *plane = plane_state->plane;
744 struct drm_crtc_state *crtc_state;
746 if (plane_state->crtc) {
747 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
749 if (WARN_ON(IS_ERR(crtc_state)))
750 return PTR_ERR(crtc_state);
752 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
755 plane_state->crtc = crtc;
758 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
760 if (IS_ERR(crtc_state))
761 return PTR_ERR(crtc_state);
762 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
766 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
767 plane_state, crtc->base.id);
769 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
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.
787 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
788 struct drm_framebuffer *fb)
791 drm_framebuffer_unreference(plane_state->fb);
793 drm_framebuffer_reference(fb);
794 plane_state->fb = fb;
797 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
798 fb->base.id, plane_state);
800 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
803 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
806 * drm_atomic_set_crtc_for_connector - set crtc for connector
807 * @conn_state: atomic state object for the connector
808 * @crtc: crtc to use for the connector
810 * Changing the assigned crtc for a connector requires us to grab the lock and
811 * state for the new crtc, as needed. This function takes care of all these
812 * details besides updating the pointer in the state object itself.
815 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
816 * then the w/w mutex code has detected a deadlock and the entire atomic
817 * sequence must be restarted. All other errors are fatal.
820 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
821 struct drm_crtc *crtc)
823 struct drm_crtc_state *crtc_state;
826 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
827 if (IS_ERR(crtc_state))
828 return PTR_ERR(crtc_state);
831 conn_state->crtc = crtc;
834 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
835 conn_state, crtc->base.id);
837 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
842 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
845 * drm_atomic_add_affected_connectors - add connectors for crtc
846 * @state: atomic state
849 * This function walks the current configuration and adds all connectors
850 * currently using @crtc to the atomic configuration @state. Note that this
851 * function must acquire the connection mutex. This can potentially cause
852 * unneeded seralization if the update is just for the planes on one crtc. Hence
853 * drivers and helpers should only call this when really needed (e.g. when a
854 * full modeset needs to happen due to some change).
857 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
858 * then the w/w mutex code has detected a deadlock and the entire atomic
859 * sequence must be restarted. All other errors are fatal.
862 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
863 struct drm_crtc *crtc)
865 struct drm_mode_config *config = &state->dev->mode_config;
866 struct drm_connector *connector;
867 struct drm_connector_state *conn_state;
870 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
874 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
875 crtc->base.id, state);
878 * Changed connectors are already in @state, so only need to look at the
879 * current configuration.
881 list_for_each_entry(connector, &config->connector_list, head) {
882 if (connector->state->crtc != crtc)
885 conn_state = drm_atomic_get_connector_state(state, connector);
886 if (IS_ERR(conn_state))
887 return PTR_ERR(conn_state);
892 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
895 * drm_atomic_connectors_for_crtc - count number of connected outputs
896 * @state: atomic state
899 * This function counts all connectors which will be connected to @crtc
900 * according to @state. Useful to recompute the enable state for @crtc.
903 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
904 struct drm_crtc *crtc)
906 int i, num_connected_connectors = 0;
908 for (i = 0; i < state->num_connector; i++) {
909 struct drm_connector_state *conn_state;
911 conn_state = state->connector_states[i];
913 if (conn_state && conn_state->crtc == crtc)
914 num_connected_connectors++;
917 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
918 state, num_connected_connectors, crtc->base.id);
920 return num_connected_connectors;
922 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
925 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
926 * @state: atomic state
928 * This function should be used by legacy entry points which don't understand
929 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
930 * the slowpath completed.
932 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
937 drm_modeset_backoff(state->acquire_ctx);
939 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
943 ret = drm_modeset_lock_all_crtcs(state->dev,
948 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
951 * drm_atomic_check_only - check whether a given config would work
952 * @state: atomic configuration to check
954 * Note that this function can return -EDEADLK if the driver needed to acquire
955 * more locks but encountered a deadlock. The caller must then do the usual w/w
956 * backoff dance and restart. All other errors are fatal.
959 * 0 on success, negative error code on failure.
961 int drm_atomic_check_only(struct drm_atomic_state *state)
963 struct drm_device *dev = state->dev;
964 struct drm_mode_config *config = &dev->mode_config;
965 int nplanes = config->num_total_plane;
966 int ncrtcs = config->num_crtc;
969 DRM_DEBUG_ATOMIC("checking %p\n", state);
971 for (i = 0; i < nplanes; i++) {
972 struct drm_plane *plane = state->planes[i];
977 ret = drm_atomic_plane_check(plane, state->plane_states[i]);
979 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
985 for (i = 0; i < ncrtcs; i++) {
986 struct drm_crtc *crtc = state->crtcs[i];
991 ret = drm_atomic_crtc_check(crtc, state->crtc_states[i]);
993 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
999 if (config->funcs->atomic_check)
1000 ret = config->funcs->atomic_check(state->dev, state);
1002 if (!state->allow_modeset) {
1003 for (i = 0; i < ncrtcs; i++) {
1004 struct drm_crtc *crtc = state->crtcs[i];
1005 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1010 if (crtc_state->mode_changed ||
1011 crtc_state->active_changed) {
1012 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1021 EXPORT_SYMBOL(drm_atomic_check_only);
1024 * drm_atomic_commit - commit configuration atomically
1025 * @state: atomic configuration to check
1027 * Note that this function can return -EDEADLK if the driver needed to acquire
1028 * more locks but encountered a deadlock. The caller must then do the usual w/w
1029 * backoff dance and restart. All other errors are fatal.
1031 * Also note that on successful execution ownership of @state is transferred
1032 * from the caller of this function to the function itself. The caller must not
1033 * free or in any other way access @state. If the function fails then the caller
1034 * must clean up @state itself.
1037 * 0 on success, negative error code on failure.
1039 int drm_atomic_commit(struct drm_atomic_state *state)
1041 struct drm_mode_config *config = &state->dev->mode_config;
1044 ret = drm_atomic_check_only(state);
1048 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1050 return config->funcs->atomic_commit(state->dev, state, false);
1052 EXPORT_SYMBOL(drm_atomic_commit);
1055 * drm_atomic_async_commit - atomic&async configuration commit
1056 * @state: atomic configuration to check
1058 * Note that this function can return -EDEADLK if the driver needed to acquire
1059 * more locks but encountered a deadlock. The caller must then do the usual w/w
1060 * backoff dance and restart. All other errors are fatal.
1062 * Also note that on successful execution ownership of @state is transferred
1063 * from the caller of this function to the function itself. The caller must not
1064 * free or in any other way access @state. If the function fails then the caller
1065 * must clean up @state itself.
1068 * 0 on success, negative error code on failure.
1070 int drm_atomic_async_commit(struct drm_atomic_state *state)
1072 struct drm_mode_config *config = &state->dev->mode_config;
1075 ret = drm_atomic_check_only(state);
1079 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1081 return config->funcs->atomic_commit(state->dev, state, true);
1083 EXPORT_SYMBOL(drm_atomic_async_commit);
1086 * The big monstor ioctl
1089 static struct drm_pending_vblank_event *create_vblank_event(
1090 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1092 struct drm_pending_vblank_event *e = NULL;
1093 unsigned long flags;
1095 spin_lock_irqsave(&dev->event_lock, flags);
1096 if (file_priv->event_space < sizeof e->event) {
1097 spin_unlock_irqrestore(&dev->event_lock, flags);
1100 file_priv->event_space -= sizeof e->event;
1101 spin_unlock_irqrestore(&dev->event_lock, flags);
1103 e = kzalloc(sizeof *e, GFP_KERNEL);
1105 spin_lock_irqsave(&dev->event_lock, flags);
1106 file_priv->event_space += sizeof e->event;
1107 spin_unlock_irqrestore(&dev->event_lock, flags);
1111 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1112 e->event.base.length = sizeof e->event;
1113 e->event.user_data = user_data;
1114 e->base.event = &e->event.base;
1115 e->base.file_priv = file_priv;
1116 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1122 static void destroy_vblank_event(struct drm_device *dev,
1123 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1125 unsigned long flags;
1127 spin_lock_irqsave(&dev->event_lock, flags);
1128 file_priv->event_space += sizeof e->event;
1129 spin_unlock_irqrestore(&dev->event_lock, flags);
1133 static int atomic_set_prop(struct drm_atomic_state *state,
1134 struct drm_mode_object *obj, struct drm_property *prop,
1135 uint64_t prop_value)
1137 struct drm_mode_object *ref;
1140 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1143 switch (obj->type) {
1144 case DRM_MODE_OBJECT_CONNECTOR: {
1145 struct drm_connector *connector = obj_to_connector(obj);
1146 struct drm_connector_state *connector_state;
1148 connector_state = drm_atomic_get_connector_state(state, connector);
1149 if (IS_ERR(connector_state)) {
1150 ret = PTR_ERR(connector_state);
1154 ret = drm_atomic_connector_set_property(connector,
1155 connector_state, prop, prop_value);
1158 case DRM_MODE_OBJECT_CRTC: {
1159 struct drm_crtc *crtc = obj_to_crtc(obj);
1160 struct drm_crtc_state *crtc_state;
1162 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1163 if (IS_ERR(crtc_state)) {
1164 ret = PTR_ERR(crtc_state);
1168 ret = drm_atomic_crtc_set_property(crtc,
1169 crtc_state, prop, prop_value);
1172 case DRM_MODE_OBJECT_PLANE: {
1173 struct drm_plane *plane = obj_to_plane(obj);
1174 struct drm_plane_state *plane_state;
1176 plane_state = drm_atomic_get_plane_state(state, plane);
1177 if (IS_ERR(plane_state)) {
1178 ret = PTR_ERR(plane_state);
1182 ret = drm_atomic_plane_set_property(plane,
1183 plane_state, prop, prop_value);
1191 drm_property_change_valid_put(prop, ref);
1195 int drm_mode_atomic_ioctl(struct drm_device *dev,
1196 void *data, struct drm_file *file_priv)
1198 struct drm_mode_atomic *arg = data;
1199 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1200 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1201 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1202 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1203 unsigned int copied_objs, copied_props;
1204 struct drm_atomic_state *state;
1205 struct drm_modeset_acquire_ctx ctx;
1206 struct drm_plane *plane;
1207 unsigned plane_mask = 0;
1211 /* disallow for drivers not supporting atomic: */
1212 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1215 /* disallow for userspace that has not enabled atomic cap (even
1216 * though this may be a bit overkill, since legacy userspace
1217 * wouldn't know how to call this ioctl)
1219 if (!file_priv->atomic)
1222 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1228 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1229 !dev->mode_config.async_page_flip)
1232 /* can't test and expect an event at the same time. */
1233 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1234 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1237 drm_modeset_acquire_init(&ctx, 0);
1239 state = drm_atomic_state_alloc(dev);
1243 state->acquire_ctx = &ctx;
1244 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1250 for (i = 0; i < arg->count_objs; i++) {
1251 uint32_t obj_id, count_props;
1252 struct drm_mode_object *obj;
1254 if (get_user(obj_id, objs_ptr + copied_objs)) {
1259 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1260 if (!obj || !obj->properties) {
1265 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1266 plane = obj_to_plane(obj);
1267 plane_mask |= (1 << drm_plane_index(plane));
1268 plane->old_fb = plane->fb;
1271 if (get_user(count_props, count_props_ptr + copied_objs)) {
1278 for (j = 0; j < count_props; j++) {
1280 uint64_t prop_value;
1281 struct drm_property *prop;
1283 if (get_user(prop_id, props_ptr + copied_props)) {
1288 prop = drm_property_find(dev, prop_id);
1294 if (copy_from_user(&prop_value,
1295 prop_values_ptr + copied_props,
1296 sizeof(prop_value))) {
1301 ret = atomic_set_prop(state, obj, prop, prop_value);
1309 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1310 int ncrtcs = dev->mode_config.num_crtc;
1312 for (i = 0; i < ncrtcs; i++) {
1313 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1314 struct drm_pending_vblank_event *e;
1319 e = create_vblank_event(dev, file_priv, arg->user_data);
1325 crtc_state->event = e;
1329 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1330 ret = drm_atomic_check_only(state);
1331 /* _check_only() does not free state, unlike _commit() */
1332 drm_atomic_state_free(state);
1333 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1334 ret = drm_atomic_async_commit(state);
1336 ret = drm_atomic_commit(state);
1339 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1340 * locks (ie. while it is still safe to deref plane->state). We
1341 * need to do this here because the driver entry points cannot
1342 * distinguish between legacy and atomic ioctls.
1344 drm_for_each_plane_mask(plane, dev, plane_mask) {
1346 struct drm_framebuffer *new_fb = plane->state->fb;
1348 drm_framebuffer_reference(new_fb);
1350 plane->crtc = plane->state->crtc;
1352 plane->old_fb = NULL;
1354 if (plane->old_fb) {
1355 drm_framebuffer_unreference(plane->old_fb);
1356 plane->old_fb = NULL;
1360 drm_modeset_drop_locks(&ctx);
1361 drm_modeset_acquire_fini(&ctx);
1366 if (ret == -EDEADLK)
1369 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1370 int ncrtcs = dev->mode_config.num_crtc;
1372 for (i = 0; i < ncrtcs; i++) {
1373 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1378 destroy_vblank_event(dev, file_priv, crtc_state->event);
1379 crtc_state->event = NULL;
1383 drm_atomic_state_free(state);
1385 drm_modeset_drop_locks(&ctx);
1386 drm_modeset_acquire_fini(&ctx);
1391 drm_atomic_state_clear(state);
1392 drm_modeset_backoff(&ctx);