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->connectors[i] = NULL;
138 state->connector_states[i] = NULL;
141 for (i = 0; i < config->num_crtc; i++) {
142 struct drm_crtc *crtc = state->crtcs[i];
147 crtc->funcs->atomic_destroy_state(crtc,
148 state->crtc_states[i]);
149 state->crtcs[i] = NULL;
150 state->crtc_states[i] = NULL;
153 for (i = 0; i < config->num_total_plane; i++) {
154 struct drm_plane *plane = state->planes[i];
159 plane->funcs->atomic_destroy_state(plane,
160 state->plane_states[i]);
161 state->planes[i] = NULL;
162 state->plane_states[i] = NULL;
165 EXPORT_SYMBOL(drm_atomic_state_clear);
168 * drm_atomic_state_free - free all memory for an atomic state
169 * @state: atomic state to deallocate
171 * This frees all memory associated with an atomic state, including all the
172 * per-object state for planes, crtcs and connectors.
174 void drm_atomic_state_free(struct drm_atomic_state *state)
179 drm_atomic_state_clear(state);
181 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
185 EXPORT_SYMBOL(drm_atomic_state_free);
188 * drm_atomic_get_crtc_state - get crtc state
189 * @state: global atomic state object
190 * @crtc: crtc to get state object for
192 * This function returns the crtc state for the given crtc, allocating it if
193 * needed. It will also grab the relevant crtc lock to make sure that the state
198 * Either the allocated state or the error code encoded into the pointer. When
199 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
200 * entire atomic sequence must be restarted. All other errors are fatal.
202 struct drm_crtc_state *
203 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
204 struct drm_crtc *crtc)
207 struct drm_crtc_state *crtc_state;
209 index = drm_crtc_index(crtc);
211 if (state->crtc_states[index])
212 return state->crtc_states[index];
214 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
218 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
220 return ERR_PTR(-ENOMEM);
222 state->crtc_states[index] = crtc_state;
223 state->crtcs[index] = crtc;
224 crtc_state->state = state;
226 DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
227 crtc->base.id, crtc_state, state);
231 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
234 * drm_atomic_crtc_set_property - set property on CRTC
235 * @crtc: the drm CRTC to set a property on
236 * @state: the state object to update with the new property value
237 * @property: the property to set
238 * @val: the new property value
240 * Use this instead of calling crtc->atomic_set_property directly.
241 * This function handles generic/core properties and calls out to
242 * driver's ->atomic_set_property() for driver properties. To ensure
243 * consistent behavior you must call this function rather than the
244 * driver hook directly.
247 * Zero on success, error code on failure
249 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
250 struct drm_crtc_state *state, struct drm_property *property,
253 struct drm_device *dev = crtc->dev;
254 struct drm_mode_config *config = &dev->mode_config;
256 /* FIXME: Mode prop is missing, which also controls ->enable. */
257 if (property == config->prop_active)
259 else if (crtc->funcs->atomic_set_property)
260 return crtc->funcs->atomic_set_property(crtc, state, property, val);
266 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
269 * This function handles generic/core properties and calls out to
270 * driver's ->atomic_get_property() for driver properties. To ensure
271 * consistent behavior you must call this function rather than the
272 * driver hook directly.
274 int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
275 const struct drm_crtc_state *state,
276 struct drm_property *property, uint64_t *val)
278 struct drm_device *dev = crtc->dev;
279 struct drm_mode_config *config = &dev->mode_config;
281 if (property == config->prop_active)
282 *val = state->active;
283 else if (crtc->funcs->atomic_get_property)
284 return crtc->funcs->atomic_get_property(crtc, state, property, val);
292 * drm_atomic_crtc_check - check crtc state
293 * @crtc: crtc to check
294 * @state: crtc state to check
296 * Provides core sanity checks for crtc state.
299 * Zero on success, error code on failure
301 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
302 struct drm_crtc_state *state)
304 /* NOTE: we explicitly don't enforce constraints such as primary
305 * layer covering entire screen, since that is something we want
306 * to allow (on hw that supports it). For hw that does not, it
307 * should be checked in driver's crtc->atomic_check() vfunc.
309 * TODO: Add generic modeset state checks once we support those.
312 if (state->active && !state->enable) {
313 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
322 * drm_atomic_get_plane_state - get plane state
323 * @state: global atomic state object
324 * @plane: plane to get state object for
326 * This function returns the plane state for the given plane, allocating it if
327 * needed. It will also grab the relevant plane lock to make sure that the state
332 * Either the allocated state or the error code encoded into the pointer. When
333 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
334 * entire atomic sequence must be restarted. All other errors are fatal.
336 struct drm_plane_state *
337 drm_atomic_get_plane_state(struct drm_atomic_state *state,
338 struct drm_plane *plane)
341 struct drm_plane_state *plane_state;
343 index = drm_plane_index(plane);
345 if (state->plane_states[index])
346 return state->plane_states[index];
348 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
352 plane_state = plane->funcs->atomic_duplicate_state(plane);
354 return ERR_PTR(-ENOMEM);
356 state->plane_states[index] = plane_state;
357 state->planes[index] = plane;
358 plane_state->state = state;
360 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
361 plane->base.id, plane_state, state);
363 if (plane_state->crtc) {
364 struct drm_crtc_state *crtc_state;
366 crtc_state = drm_atomic_get_crtc_state(state,
368 if (IS_ERR(crtc_state))
369 return ERR_CAST(crtc_state);
374 EXPORT_SYMBOL(drm_atomic_get_plane_state);
377 * drm_atomic_plane_set_property - set property on plane
378 * @plane: the drm plane to set a property on
379 * @state: the state object to update with the new property value
380 * @property: the property to set
381 * @val: the new property value
383 * Use this instead of calling plane->atomic_set_property directly.
384 * This function handles generic/core properties and calls out to
385 * driver's ->atomic_set_property() for driver properties. To ensure
386 * consistent behavior you must call this function rather than the
387 * driver hook directly.
390 * Zero on success, error code on failure
392 int drm_atomic_plane_set_property(struct drm_plane *plane,
393 struct drm_plane_state *state, struct drm_property *property,
396 struct drm_device *dev = plane->dev;
397 struct drm_mode_config *config = &dev->mode_config;
399 if (property == config->prop_fb_id) {
400 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
401 drm_atomic_set_fb_for_plane(state, fb);
403 drm_framebuffer_unreference(fb);
404 } else if (property == config->prop_crtc_id) {
405 struct drm_crtc *crtc = drm_crtc_find(dev, val);
406 return drm_atomic_set_crtc_for_plane(state, crtc);
407 } else if (property == config->prop_crtc_x) {
408 state->crtc_x = U642I64(val);
409 } else if (property == config->prop_crtc_y) {
410 state->crtc_y = U642I64(val);
411 } else if (property == config->prop_crtc_w) {
413 } else if (property == config->prop_crtc_h) {
415 } else if (property == config->prop_src_x) {
417 } else if (property == config->prop_src_y) {
419 } else if (property == config->prop_src_w) {
421 } else if (property == config->prop_src_h) {
423 } else if (property == config->rotation_property) {
424 state->rotation = val;
425 } else if (plane->funcs->atomic_set_property) {
426 return plane->funcs->atomic_set_property(plane, state,
434 EXPORT_SYMBOL(drm_atomic_plane_set_property);
437 * This function handles generic/core properties and calls out to
438 * driver's ->atomic_get_property() for driver properties. To ensure
439 * consistent behavior you must call this function rather than the
440 * driver hook directly.
443 drm_atomic_plane_get_property(struct drm_plane *plane,
444 const struct drm_plane_state *state,
445 struct drm_property *property, uint64_t *val)
447 struct drm_device *dev = plane->dev;
448 struct drm_mode_config *config = &dev->mode_config;
450 if (property == config->prop_fb_id) {
451 *val = (state->fb) ? state->fb->base.id : 0;
452 } else if (property == config->prop_crtc_id) {
453 *val = (state->crtc) ? state->crtc->base.id : 0;
454 } else if (property == config->prop_crtc_x) {
455 *val = I642U64(state->crtc_x);
456 } else if (property == config->prop_crtc_y) {
457 *val = I642U64(state->crtc_y);
458 } else if (property == config->prop_crtc_w) {
459 *val = state->crtc_w;
460 } else if (property == config->prop_crtc_h) {
461 *val = state->crtc_h;
462 } else if (property == config->prop_src_x) {
464 } else if (property == config->prop_src_y) {
466 } else if (property == config->prop_src_w) {
468 } else if (property == config->prop_src_h) {
470 } else if (property == config->rotation_property) {
471 *val = state->rotation;
472 } else if (plane->funcs->atomic_get_property) {
473 return plane->funcs->atomic_get_property(plane, state, property, val);
482 * drm_atomic_plane_check - check plane state
483 * @plane: plane to check
484 * @state: plane state to check
486 * Provides core sanity checks for plane state.
489 * Zero on success, error code on failure
491 static int drm_atomic_plane_check(struct drm_plane *plane,
492 struct drm_plane_state *state)
494 unsigned int fb_width, fb_height;
497 /* either *both* CRTC and FB must be set, or neither */
498 if (WARN_ON(state->crtc && !state->fb)) {
499 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
501 } else if (WARN_ON(state->fb && !state->crtc)) {
502 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
506 /* if disabled, we don't care about the rest of the state: */
510 /* Check whether this plane is usable on this CRTC */
511 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
512 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
516 /* Check whether this plane supports the fb pixel format. */
517 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
519 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
520 drm_get_format_name(state->fb->pixel_format));
524 /* Give drivers some help against integer overflows */
525 if (state->crtc_w > INT_MAX ||
526 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
527 state->crtc_h > INT_MAX ||
528 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
529 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
530 state->crtc_w, state->crtc_h,
531 state->crtc_x, state->crtc_y);
535 fb_width = state->fb->width << 16;
536 fb_height = state->fb->height << 16;
538 /* Make sure source coordinates are inside the fb. */
539 if (state->src_w > fb_width ||
540 state->src_x > fb_width - state->src_w ||
541 state->src_h > fb_height ||
542 state->src_y > fb_height - state->src_h) {
543 DRM_DEBUG_ATOMIC("Invalid source coordinates "
544 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
545 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
546 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
547 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
548 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
556 * drm_atomic_get_connector_state - get connector state
557 * @state: global atomic state object
558 * @connector: connector to get state object for
560 * This function returns the connector state for the given connector,
561 * allocating it if needed. It will also grab the relevant connector lock to
562 * make sure that the state is consistent.
566 * Either the allocated state or the error code encoded into the pointer. When
567 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
568 * entire atomic sequence must be restarted. All other errors are fatal.
570 struct drm_connector_state *
571 drm_atomic_get_connector_state(struct drm_atomic_state *state,
572 struct drm_connector *connector)
575 struct drm_mode_config *config = &connector->dev->mode_config;
576 struct drm_connector_state *connector_state;
578 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
582 index = drm_connector_index(connector);
585 * Construction of atomic state updates can race with a connector
586 * hot-add which might overflow. In this case flip the table and just
587 * restart the entire ioctl - no one is fast enough to livelock a cpu
588 * with physical hotplug events anyway.
590 * Note that we only grab the indexes once we have the right lock to
591 * prevent hotplug/unplugging of connectors. So removal is no problem,
592 * at most the array is a bit too large.
594 if (index >= state->num_connector) {
595 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
596 return ERR_PTR(-EAGAIN);
599 if (state->connector_states[index])
600 return state->connector_states[index];
602 connector_state = connector->funcs->atomic_duplicate_state(connector);
603 if (!connector_state)
604 return ERR_PTR(-ENOMEM);
606 state->connector_states[index] = connector_state;
607 state->connectors[index] = connector;
608 connector_state->state = state;
610 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
611 connector->base.id, connector_state, state);
613 if (connector_state->crtc) {
614 struct drm_crtc_state *crtc_state;
616 crtc_state = drm_atomic_get_crtc_state(state,
617 connector_state->crtc);
618 if (IS_ERR(crtc_state))
619 return ERR_CAST(crtc_state);
622 return connector_state;
624 EXPORT_SYMBOL(drm_atomic_get_connector_state);
627 * drm_atomic_connector_set_property - set property on connector.
628 * @connector: the drm connector to set a property on
629 * @state: the state object to update with the new property value
630 * @property: the property to set
631 * @val: the new property value
633 * Use this instead of calling connector->atomic_set_property directly.
634 * This function handles generic/core properties and calls out to
635 * driver's ->atomic_set_property() for driver properties. To ensure
636 * consistent behavior you must call this function rather than the
637 * driver hook directly.
640 * Zero on success, error code on failure
642 int drm_atomic_connector_set_property(struct drm_connector *connector,
643 struct drm_connector_state *state, struct drm_property *property,
646 struct drm_device *dev = connector->dev;
647 struct drm_mode_config *config = &dev->mode_config;
649 if (property == config->prop_crtc_id) {
650 struct drm_crtc *crtc = drm_crtc_find(dev, val);
651 return drm_atomic_set_crtc_for_connector(state, crtc);
652 } else if (property == config->dpms_property) {
653 /* setting DPMS property requires special handling, which
654 * is done in legacy setprop path for us. Disallow (for
655 * now?) atomic writes to DPMS property:
658 } else if (connector->funcs->atomic_set_property) {
659 return connector->funcs->atomic_set_property(connector,
660 state, property, val);
665 EXPORT_SYMBOL(drm_atomic_connector_set_property);
668 * This function handles generic/core properties and calls out to
669 * driver's ->atomic_get_property() for driver properties. To ensure
670 * consistent behavior you must call this function rather than the
671 * driver hook directly.
674 drm_atomic_connector_get_property(struct drm_connector *connector,
675 const struct drm_connector_state *state,
676 struct drm_property *property, uint64_t *val)
678 struct drm_device *dev = connector->dev;
679 struct drm_mode_config *config = &dev->mode_config;
681 if (property == config->prop_crtc_id) {
682 *val = (state->crtc) ? state->crtc->base.id : 0;
683 } else if (property == config->dpms_property) {
684 *val = connector->dpms;
685 } else if (connector->funcs->atomic_get_property) {
686 return connector->funcs->atomic_get_property(connector,
687 state, property, val);
695 int drm_atomic_get_property(struct drm_mode_object *obj,
696 struct drm_property *property, uint64_t *val)
698 struct drm_device *dev = property->dev;
702 case DRM_MODE_OBJECT_CONNECTOR: {
703 struct drm_connector *connector = obj_to_connector(obj);
704 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
705 ret = drm_atomic_connector_get_property(connector,
706 connector->state, property, val);
709 case DRM_MODE_OBJECT_CRTC: {
710 struct drm_crtc *crtc = obj_to_crtc(obj);
711 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
712 ret = drm_atomic_crtc_get_property(crtc,
713 crtc->state, property, val);
716 case DRM_MODE_OBJECT_PLANE: {
717 struct drm_plane *plane = obj_to_plane(obj);
718 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
719 ret = drm_atomic_plane_get_property(plane,
720 plane->state, property, val);
732 * drm_atomic_set_crtc_for_plane - set crtc for plane
733 * @plane_state: the plane whose incoming state to update
734 * @crtc: crtc to use for the plane
736 * Changing the assigned crtc for a plane requires us to grab the lock and state
737 * for the new crtc, as needed. This function takes care of all these details
738 * besides updating the pointer in the state object itself.
741 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
742 * then the w/w mutex code has detected a deadlock and the entire atomic
743 * sequence must be restarted. All other errors are fatal.
746 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
747 struct drm_crtc *crtc)
749 struct drm_plane *plane = plane_state->plane;
750 struct drm_crtc_state *crtc_state;
752 if (plane_state->crtc) {
753 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
755 if (WARN_ON(IS_ERR(crtc_state)))
756 return PTR_ERR(crtc_state);
758 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
761 plane_state->crtc = crtc;
764 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
766 if (IS_ERR(crtc_state))
767 return PTR_ERR(crtc_state);
768 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
772 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
773 plane_state, crtc->base.id);
775 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
780 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
783 * drm_atomic_set_fb_for_plane - set crtc for plane
784 * @plane_state: atomic state object for the plane
785 * @fb: fb to use for the plane
787 * Changing the assigned framebuffer for a plane requires us to grab a reference
788 * to the new fb and drop the reference to the old fb, if there is one. This
789 * function takes care of all these details besides updating the pointer in the
790 * state object itself.
793 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
794 struct drm_framebuffer *fb)
797 drm_framebuffer_unreference(plane_state->fb);
799 drm_framebuffer_reference(fb);
800 plane_state->fb = fb;
803 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
804 fb->base.id, plane_state);
806 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
809 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
812 * drm_atomic_set_crtc_for_connector - set crtc for connector
813 * @conn_state: atomic state object for the connector
814 * @crtc: crtc to use for the connector
816 * Changing the assigned crtc for a connector requires us to grab the lock and
817 * state for the new crtc, as needed. This function takes care of all these
818 * details besides updating the pointer in the state object itself.
821 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
822 * then the w/w mutex code has detected a deadlock and the entire atomic
823 * sequence must be restarted. All other errors are fatal.
826 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
827 struct drm_crtc *crtc)
829 struct drm_crtc_state *crtc_state;
832 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
833 if (IS_ERR(crtc_state))
834 return PTR_ERR(crtc_state);
837 conn_state->crtc = crtc;
840 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
841 conn_state, crtc->base.id);
843 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
848 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
851 * drm_atomic_add_affected_connectors - add connectors for crtc
852 * @state: atomic state
855 * This function walks the current configuration and adds all connectors
856 * currently using @crtc to the atomic configuration @state. Note that this
857 * function must acquire the connection mutex. This can potentially cause
858 * unneeded seralization if the update is just for the planes on one crtc. Hence
859 * drivers and helpers should only call this when really needed (e.g. when a
860 * full modeset needs to happen due to some change).
863 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
864 * then the w/w mutex code has detected a deadlock and the entire atomic
865 * sequence must be restarted. All other errors are fatal.
868 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
869 struct drm_crtc *crtc)
871 struct drm_mode_config *config = &state->dev->mode_config;
872 struct drm_connector *connector;
873 struct drm_connector_state *conn_state;
876 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
880 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
881 crtc->base.id, state);
884 * Changed connectors are already in @state, so only need to look at the
885 * current configuration.
887 list_for_each_entry(connector, &config->connector_list, head) {
888 if (connector->state->crtc != crtc)
891 conn_state = drm_atomic_get_connector_state(state, connector);
892 if (IS_ERR(conn_state))
893 return PTR_ERR(conn_state);
898 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
901 * drm_atomic_connectors_for_crtc - count number of connected outputs
902 * @state: atomic state
905 * This function counts all connectors which will be connected to @crtc
906 * according to @state. Useful to recompute the enable state for @crtc.
909 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
910 struct drm_crtc *crtc)
912 int i, num_connected_connectors = 0;
914 for (i = 0; i < state->num_connector; i++) {
915 struct drm_connector_state *conn_state;
917 conn_state = state->connector_states[i];
919 if (conn_state && conn_state->crtc == crtc)
920 num_connected_connectors++;
923 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
924 state, num_connected_connectors, crtc->base.id);
926 return num_connected_connectors;
928 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
931 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
932 * @state: atomic state
934 * This function should be used by legacy entry points which don't understand
935 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
936 * the slowpath completed.
938 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
943 drm_modeset_backoff(state->acquire_ctx);
945 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
949 ret = drm_modeset_lock_all_crtcs(state->dev,
954 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
957 * drm_atomic_check_only - check whether a given config would work
958 * @state: atomic configuration to check
960 * Note that this function can return -EDEADLK if the driver needed to acquire
961 * more locks but encountered a deadlock. The caller must then do the usual w/w
962 * backoff dance and restart. All other errors are fatal.
965 * 0 on success, negative error code on failure.
967 int drm_atomic_check_only(struct drm_atomic_state *state)
969 struct drm_device *dev = state->dev;
970 struct drm_mode_config *config = &dev->mode_config;
971 int nplanes = config->num_total_plane;
972 int ncrtcs = config->num_crtc;
975 DRM_DEBUG_ATOMIC("checking %p\n", state);
977 for (i = 0; i < nplanes; i++) {
978 struct drm_plane *plane = state->planes[i];
983 ret = drm_atomic_plane_check(plane, state->plane_states[i]);
985 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
991 for (i = 0; i < ncrtcs; i++) {
992 struct drm_crtc *crtc = state->crtcs[i];
997 ret = drm_atomic_crtc_check(crtc, state->crtc_states[i]);
999 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1005 if (config->funcs->atomic_check)
1006 ret = config->funcs->atomic_check(state->dev, state);
1008 if (!state->allow_modeset) {
1009 for (i = 0; i < ncrtcs; i++) {
1010 struct drm_crtc *crtc = state->crtcs[i];
1011 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1016 if (crtc_state->mode_changed ||
1017 crtc_state->active_changed) {
1018 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1027 EXPORT_SYMBOL(drm_atomic_check_only);
1030 * drm_atomic_commit - commit configuration atomically
1031 * @state: atomic configuration to check
1033 * Note that this function can return -EDEADLK if the driver needed to acquire
1034 * more locks but encountered a deadlock. The caller must then do the usual w/w
1035 * backoff dance and restart. All other errors are fatal.
1037 * Also note that on successful execution ownership of @state is transferred
1038 * from the caller of this function to the function itself. The caller must not
1039 * free or in any other way access @state. If the function fails then the caller
1040 * must clean up @state itself.
1043 * 0 on success, negative error code on failure.
1045 int drm_atomic_commit(struct drm_atomic_state *state)
1047 struct drm_mode_config *config = &state->dev->mode_config;
1050 ret = drm_atomic_check_only(state);
1054 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1056 return config->funcs->atomic_commit(state->dev, state, false);
1058 EXPORT_SYMBOL(drm_atomic_commit);
1061 * drm_atomic_async_commit - atomic&async configuration commit
1062 * @state: atomic configuration to check
1064 * Note that this function can return -EDEADLK if the driver needed to acquire
1065 * more locks but encountered a deadlock. The caller must then do the usual w/w
1066 * backoff dance and restart. All other errors are fatal.
1068 * Also note that on successful execution ownership of @state is transferred
1069 * from the caller of this function to the function itself. The caller must not
1070 * free or in any other way access @state. If the function fails then the caller
1071 * must clean up @state itself.
1074 * 0 on success, negative error code on failure.
1076 int drm_atomic_async_commit(struct drm_atomic_state *state)
1078 struct drm_mode_config *config = &state->dev->mode_config;
1081 ret = drm_atomic_check_only(state);
1085 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1087 return config->funcs->atomic_commit(state->dev, state, true);
1089 EXPORT_SYMBOL(drm_atomic_async_commit);
1092 * The big monstor ioctl
1095 static struct drm_pending_vblank_event *create_vblank_event(
1096 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1098 struct drm_pending_vblank_event *e = NULL;
1099 unsigned long flags;
1101 spin_lock_irqsave(&dev->event_lock, flags);
1102 if (file_priv->event_space < sizeof e->event) {
1103 spin_unlock_irqrestore(&dev->event_lock, flags);
1106 file_priv->event_space -= sizeof e->event;
1107 spin_unlock_irqrestore(&dev->event_lock, flags);
1109 e = kzalloc(sizeof *e, GFP_KERNEL);
1111 spin_lock_irqsave(&dev->event_lock, flags);
1112 file_priv->event_space += sizeof e->event;
1113 spin_unlock_irqrestore(&dev->event_lock, flags);
1117 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1118 e->event.base.length = sizeof e->event;
1119 e->event.user_data = user_data;
1120 e->base.event = &e->event.base;
1121 e->base.file_priv = file_priv;
1122 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1128 static void destroy_vblank_event(struct drm_device *dev,
1129 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1131 unsigned long flags;
1133 spin_lock_irqsave(&dev->event_lock, flags);
1134 file_priv->event_space += sizeof e->event;
1135 spin_unlock_irqrestore(&dev->event_lock, flags);
1139 static int atomic_set_prop(struct drm_atomic_state *state,
1140 struct drm_mode_object *obj, struct drm_property *prop,
1141 uint64_t prop_value)
1143 struct drm_mode_object *ref;
1146 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1149 switch (obj->type) {
1150 case DRM_MODE_OBJECT_CONNECTOR: {
1151 struct drm_connector *connector = obj_to_connector(obj);
1152 struct drm_connector_state *connector_state;
1154 connector_state = drm_atomic_get_connector_state(state, connector);
1155 if (IS_ERR(connector_state)) {
1156 ret = PTR_ERR(connector_state);
1160 ret = drm_atomic_connector_set_property(connector,
1161 connector_state, prop, prop_value);
1164 case DRM_MODE_OBJECT_CRTC: {
1165 struct drm_crtc *crtc = obj_to_crtc(obj);
1166 struct drm_crtc_state *crtc_state;
1168 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1169 if (IS_ERR(crtc_state)) {
1170 ret = PTR_ERR(crtc_state);
1174 ret = drm_atomic_crtc_set_property(crtc,
1175 crtc_state, prop, prop_value);
1178 case DRM_MODE_OBJECT_PLANE: {
1179 struct drm_plane *plane = obj_to_plane(obj);
1180 struct drm_plane_state *plane_state;
1182 plane_state = drm_atomic_get_plane_state(state, plane);
1183 if (IS_ERR(plane_state)) {
1184 ret = PTR_ERR(plane_state);
1188 ret = drm_atomic_plane_set_property(plane,
1189 plane_state, prop, prop_value);
1197 drm_property_change_valid_put(prop, ref);
1201 int drm_mode_atomic_ioctl(struct drm_device *dev,
1202 void *data, struct drm_file *file_priv)
1204 struct drm_mode_atomic *arg = data;
1205 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1206 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1207 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1208 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1209 unsigned int copied_objs, copied_props;
1210 struct drm_atomic_state *state;
1211 struct drm_modeset_acquire_ctx ctx;
1212 struct drm_plane *plane;
1213 unsigned plane_mask = 0;
1217 /* disallow for drivers not supporting atomic: */
1218 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1221 /* disallow for userspace that has not enabled atomic cap (even
1222 * though this may be a bit overkill, since legacy userspace
1223 * wouldn't know how to call this ioctl)
1225 if (!file_priv->atomic)
1228 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1234 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1235 !dev->mode_config.async_page_flip)
1238 /* can't test and expect an event at the same time. */
1239 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1240 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1243 drm_modeset_acquire_init(&ctx, 0);
1245 state = drm_atomic_state_alloc(dev);
1249 state->acquire_ctx = &ctx;
1250 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1256 for (i = 0; i < arg->count_objs; i++) {
1257 uint32_t obj_id, count_props;
1258 struct drm_mode_object *obj;
1260 if (get_user(obj_id, objs_ptr + copied_objs)) {
1265 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1266 if (!obj || !obj->properties) {
1271 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1272 plane = obj_to_plane(obj);
1273 plane_mask |= (1 << drm_plane_index(plane));
1274 plane->old_fb = plane->fb;
1277 if (get_user(count_props, count_props_ptr + copied_objs)) {
1284 for (j = 0; j < count_props; j++) {
1286 uint64_t prop_value;
1287 struct drm_property *prop;
1289 if (get_user(prop_id, props_ptr + copied_props)) {
1294 prop = drm_property_find(dev, prop_id);
1300 if (copy_from_user(&prop_value,
1301 prop_values_ptr + copied_props,
1302 sizeof(prop_value))) {
1307 ret = atomic_set_prop(state, obj, prop, prop_value);
1315 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1316 int ncrtcs = dev->mode_config.num_crtc;
1318 for (i = 0; i < ncrtcs; i++) {
1319 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1320 struct drm_pending_vblank_event *e;
1325 e = create_vblank_event(dev, file_priv, arg->user_data);
1331 crtc_state->event = e;
1335 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1336 ret = drm_atomic_check_only(state);
1337 /* _check_only() does not free state, unlike _commit() */
1338 drm_atomic_state_free(state);
1339 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1340 ret = drm_atomic_async_commit(state);
1342 ret = drm_atomic_commit(state);
1345 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1346 * locks (ie. while it is still safe to deref plane->state). We
1347 * need to do this here because the driver entry points cannot
1348 * distinguish between legacy and atomic ioctls.
1350 drm_for_each_plane_mask(plane, dev, plane_mask) {
1352 struct drm_framebuffer *new_fb = plane->state->fb;
1354 drm_framebuffer_reference(new_fb);
1356 plane->crtc = plane->state->crtc;
1358 plane->old_fb = NULL;
1360 if (plane->old_fb) {
1361 drm_framebuffer_unreference(plane->old_fb);
1362 plane->old_fb = NULL;
1366 drm_modeset_drop_locks(&ctx);
1367 drm_modeset_acquire_fini(&ctx);
1372 if (ret == -EDEADLK)
1375 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1376 int ncrtcs = dev->mode_config.num_crtc;
1378 for (i = 0; i < ncrtcs; i++) {
1379 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1384 destroy_vblank_event(dev, file_priv, crtc_state->event);
1385 crtc_state->event = NULL;
1389 drm_atomic_state_free(state);
1391 drm_modeset_drop_locks(&ctx);
1392 drm_modeset_acquire_fini(&ctx);
1397 drm_atomic_state_clear(state);
1398 drm_modeset_backoff(&ctx);