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 struct drm_device *dev = crtc->dev;
270 struct drm_mode_config *config = &dev->mode_config;
272 if (property == config->prop_active)
273 *val = state->active;
274 else if (crtc->funcs->atomic_get_property)
275 return crtc->funcs->atomic_get_property(crtc, state, property, val);
283 * drm_atomic_crtc_check - check crtc state
284 * @crtc: crtc to check
285 * @state: crtc state to check
287 * Provides core sanity checks for crtc state.
290 * Zero on success, error code on failure
292 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
293 struct drm_crtc_state *state)
295 /* NOTE: we explicitly don't enforce constraints such as primary
296 * layer covering entire screen, since that is something we want
297 * to allow (on hw that supports it). For hw that does not, it
298 * should be checked in driver's crtc->atomic_check() vfunc.
300 * TODO: Add generic modeset state checks once we support those.
303 if (state->active && !state->enable) {
304 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
313 * drm_atomic_get_plane_state - get plane state
314 * @state: global atomic state object
315 * @plane: plane to get state object for
317 * This function returns the plane state for the given plane, allocating it if
318 * needed. It will also grab the relevant plane lock to make sure that the state
323 * Either the allocated state or the error code encoded into the pointer. When
324 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
325 * entire atomic sequence must be restarted. All other errors are fatal.
327 struct drm_plane_state *
328 drm_atomic_get_plane_state(struct drm_atomic_state *state,
329 struct drm_plane *plane)
332 struct drm_plane_state *plane_state;
334 index = drm_plane_index(plane);
336 if (state->plane_states[index])
337 return state->plane_states[index];
339 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
343 plane_state = plane->funcs->atomic_duplicate_state(plane);
345 return ERR_PTR(-ENOMEM);
347 state->plane_states[index] = plane_state;
348 state->planes[index] = plane;
349 plane_state->state = state;
351 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
352 plane->base.id, plane_state, state);
354 if (plane_state->crtc) {
355 struct drm_crtc_state *crtc_state;
357 crtc_state = drm_atomic_get_crtc_state(state,
359 if (IS_ERR(crtc_state))
360 return ERR_CAST(crtc_state);
365 EXPORT_SYMBOL(drm_atomic_get_plane_state);
368 * drm_atomic_plane_set_property - set property on plane
369 * @plane: the drm plane to set a property on
370 * @state: the state object to update with the new property value
371 * @property: the property to set
372 * @val: the new property value
374 * Use this instead of calling plane->atomic_set_property directly.
375 * This function handles generic/core properties and calls out to
376 * driver's ->atomic_set_property() for driver properties. To ensure
377 * consistent behavior you must call this function rather than the
378 * driver hook directly.
381 * Zero on success, error code on failure
383 int drm_atomic_plane_set_property(struct drm_plane *plane,
384 struct drm_plane_state *state, struct drm_property *property,
387 struct drm_device *dev = plane->dev;
388 struct drm_mode_config *config = &dev->mode_config;
390 if (property == config->prop_fb_id) {
391 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
392 drm_atomic_set_fb_for_plane(state, fb);
394 drm_framebuffer_unreference(fb);
395 } else if (property == config->prop_crtc_id) {
396 struct drm_crtc *crtc = drm_crtc_find(dev, val);
397 return drm_atomic_set_crtc_for_plane(state, crtc);
398 } else if (property == config->prop_crtc_x) {
399 state->crtc_x = U642I64(val);
400 } else if (property == config->prop_crtc_y) {
401 state->crtc_y = U642I64(val);
402 } else if (property == config->prop_crtc_w) {
404 } else if (property == config->prop_crtc_h) {
406 } else if (property == config->prop_src_x) {
408 } else if (property == config->prop_src_y) {
410 } else if (property == config->prop_src_w) {
412 } else if (property == config->prop_src_h) {
414 } else if (property == config->rotation_property) {
415 state->rotation = val;
416 } else if (plane->funcs->atomic_set_property) {
417 return plane->funcs->atomic_set_property(plane, state,
425 EXPORT_SYMBOL(drm_atomic_plane_set_property);
428 * This function handles generic/core properties and calls out to
429 * driver's ->atomic_get_property() for driver properties. To ensure
430 * consistent behavior you must call this function rather than the
431 * driver hook directly.
434 drm_atomic_plane_get_property(struct drm_plane *plane,
435 const struct drm_plane_state *state,
436 struct drm_property *property, uint64_t *val)
438 struct drm_device *dev = plane->dev;
439 struct drm_mode_config *config = &dev->mode_config;
441 if (property == config->prop_fb_id) {
442 *val = (state->fb) ? state->fb->base.id : 0;
443 } else if (property == config->prop_crtc_id) {
444 *val = (state->crtc) ? state->crtc->base.id : 0;
445 } else if (property == config->prop_crtc_x) {
446 *val = I642U64(state->crtc_x);
447 } else if (property == config->prop_crtc_y) {
448 *val = I642U64(state->crtc_y);
449 } else if (property == config->prop_crtc_w) {
450 *val = state->crtc_w;
451 } else if (property == config->prop_crtc_h) {
452 *val = state->crtc_h;
453 } else if (property == config->prop_src_x) {
455 } else if (property == config->prop_src_y) {
457 } else if (property == config->prop_src_w) {
459 } else if (property == config->prop_src_h) {
461 } else if (property == config->rotation_property) {
462 *val = state->rotation;
463 } else if (plane->funcs->atomic_get_property) {
464 return plane->funcs->atomic_get_property(plane, state, property, val);
473 * drm_atomic_plane_check - check plane state
474 * @plane: plane to check
475 * @state: plane state to check
477 * Provides core sanity checks for plane state.
480 * Zero on success, error code on failure
482 static int drm_atomic_plane_check(struct drm_plane *plane,
483 struct drm_plane_state *state)
485 unsigned int fb_width, fb_height;
488 /* either *both* CRTC and FB must be set, or neither */
489 if (WARN_ON(state->crtc && !state->fb)) {
490 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
492 } else if (WARN_ON(state->fb && !state->crtc)) {
493 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
497 /* if disabled, we don't care about the rest of the state: */
501 /* Check whether this plane is usable on this CRTC */
502 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
503 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
507 /* Check whether this plane supports the fb pixel format. */
508 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
510 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
511 drm_get_format_name(state->fb->pixel_format));
515 /* Give drivers some help against integer overflows */
516 if (state->crtc_w > INT_MAX ||
517 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
518 state->crtc_h > INT_MAX ||
519 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
520 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
521 state->crtc_w, state->crtc_h,
522 state->crtc_x, state->crtc_y);
526 fb_width = state->fb->width << 16;
527 fb_height = state->fb->height << 16;
529 /* Make sure source coordinates are inside the fb. */
530 if (state->src_w > fb_width ||
531 state->src_x > fb_width - state->src_w ||
532 state->src_h > fb_height ||
533 state->src_y > fb_height - state->src_h) {
534 DRM_DEBUG_ATOMIC("Invalid source coordinates "
535 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
536 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
537 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
538 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
539 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
547 * drm_atomic_get_connector_state - get connector state
548 * @state: global atomic state object
549 * @connector: connector to get state object for
551 * This function returns the connector state for the given connector,
552 * allocating it if needed. It will also grab the relevant connector lock to
553 * make sure that the state is consistent.
557 * Either the allocated state or the error code encoded into the pointer. When
558 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
559 * entire atomic sequence must be restarted. All other errors are fatal.
561 struct drm_connector_state *
562 drm_atomic_get_connector_state(struct drm_atomic_state *state,
563 struct drm_connector *connector)
566 struct drm_mode_config *config = &connector->dev->mode_config;
567 struct drm_connector_state *connector_state;
569 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
573 index = drm_connector_index(connector);
576 * Construction of atomic state updates can race with a connector
577 * hot-add which might overflow. In this case flip the table and just
578 * restart the entire ioctl - no one is fast enough to livelock a cpu
579 * with physical hotplug events anyway.
581 * Note that we only grab the indexes once we have the right lock to
582 * prevent hotplug/unplugging of connectors. So removal is no problem,
583 * at most the array is a bit too large.
585 if (index >= state->num_connector) {
586 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
587 return ERR_PTR(-EAGAIN);
590 if (state->connector_states[index])
591 return state->connector_states[index];
593 connector_state = connector->funcs->atomic_duplicate_state(connector);
594 if (!connector_state)
595 return ERR_PTR(-ENOMEM);
597 state->connector_states[index] = connector_state;
598 state->connectors[index] = connector;
599 connector_state->state = state;
601 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
602 connector->base.id, connector_state, state);
604 if (connector_state->crtc) {
605 struct drm_crtc_state *crtc_state;
607 crtc_state = drm_atomic_get_crtc_state(state,
608 connector_state->crtc);
609 if (IS_ERR(crtc_state))
610 return ERR_CAST(crtc_state);
613 return connector_state;
615 EXPORT_SYMBOL(drm_atomic_get_connector_state);
618 * drm_atomic_connector_set_property - set property on connector.
619 * @connector: the drm connector to set a property on
620 * @state: the state object to update with the new property value
621 * @property: the property to set
622 * @val: the new property value
624 * Use this instead of calling connector->atomic_set_property directly.
625 * This function handles generic/core properties and calls out to
626 * driver's ->atomic_set_property() for driver properties. To ensure
627 * consistent behavior you must call this function rather than the
628 * driver hook directly.
631 * Zero on success, error code on failure
633 int drm_atomic_connector_set_property(struct drm_connector *connector,
634 struct drm_connector_state *state, struct drm_property *property,
637 struct drm_device *dev = connector->dev;
638 struct drm_mode_config *config = &dev->mode_config;
640 if (property == config->prop_crtc_id) {
641 struct drm_crtc *crtc = drm_crtc_find(dev, val);
642 return drm_atomic_set_crtc_for_connector(state, crtc);
643 } else if (property == config->dpms_property) {
644 /* setting DPMS property requires special handling, which
645 * is done in legacy setprop path for us. Disallow (for
646 * now?) atomic writes to DPMS property:
649 } else if (connector->funcs->atomic_set_property) {
650 return connector->funcs->atomic_set_property(connector,
651 state, property, val);
656 EXPORT_SYMBOL(drm_atomic_connector_set_property);
659 * This function handles generic/core properties and calls out to
660 * driver's ->atomic_get_property() for driver properties. To ensure
661 * consistent behavior you must call this function rather than the
662 * driver hook directly.
665 drm_atomic_connector_get_property(struct drm_connector *connector,
666 const struct drm_connector_state *state,
667 struct drm_property *property, uint64_t *val)
669 struct drm_device *dev = connector->dev;
670 struct drm_mode_config *config = &dev->mode_config;
672 if (property == config->prop_crtc_id) {
673 *val = (state->crtc) ? state->crtc->base.id : 0;
674 } else if (property == config->dpms_property) {
675 *val = connector->dpms;
676 } else if (connector->funcs->atomic_get_property) {
677 return connector->funcs->atomic_get_property(connector,
678 state, property, val);
686 int drm_atomic_get_property(struct drm_mode_object *obj,
687 struct drm_property *property, uint64_t *val)
689 struct drm_device *dev = property->dev;
693 case DRM_MODE_OBJECT_CONNECTOR: {
694 struct drm_connector *connector = obj_to_connector(obj);
695 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
696 ret = drm_atomic_connector_get_property(connector,
697 connector->state, property, val);
700 case DRM_MODE_OBJECT_CRTC: {
701 struct drm_crtc *crtc = obj_to_crtc(obj);
702 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
703 ret = drm_atomic_crtc_get_property(crtc,
704 crtc->state, property, val);
707 case DRM_MODE_OBJECT_PLANE: {
708 struct drm_plane *plane = obj_to_plane(obj);
709 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
710 ret = drm_atomic_plane_get_property(plane,
711 plane->state, property, val);
723 * drm_atomic_set_crtc_for_plane - set crtc for plane
724 * @plane_state: the plane whose incoming state to update
725 * @crtc: crtc to use for the plane
727 * Changing the assigned crtc for a plane requires us to grab the lock and state
728 * for the new crtc, as needed. This function takes care of all these details
729 * besides updating the pointer in the state object itself.
732 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
733 * then the w/w mutex code has detected a deadlock and the entire atomic
734 * sequence must be restarted. All other errors are fatal.
737 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
738 struct drm_crtc *crtc)
740 struct drm_plane *plane = plane_state->plane;
741 struct drm_crtc_state *crtc_state;
743 if (plane_state->crtc) {
744 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
746 if (WARN_ON(IS_ERR(crtc_state)))
747 return PTR_ERR(crtc_state);
749 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
752 plane_state->crtc = crtc;
755 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
757 if (IS_ERR(crtc_state))
758 return PTR_ERR(crtc_state);
759 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
763 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
764 plane_state, crtc->base.id);
766 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
771 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
774 * drm_atomic_set_fb_for_plane - set crtc for plane
775 * @plane_state: atomic state object for the plane
776 * @fb: fb to use for the plane
778 * Changing the assigned framebuffer for a plane requires us to grab a reference
779 * to the new fb and drop the reference to the old fb, if there is one. This
780 * function takes care of all these details besides updating the pointer in the
781 * state object itself.
784 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
785 struct drm_framebuffer *fb)
788 drm_framebuffer_unreference(plane_state->fb);
790 drm_framebuffer_reference(fb);
791 plane_state->fb = fb;
794 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
795 fb->base.id, plane_state);
797 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
800 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
803 * drm_atomic_set_crtc_for_connector - set crtc for connector
804 * @conn_state: atomic state object for the connector
805 * @crtc: crtc to use for the connector
807 * Changing the assigned crtc for a connector requires us to grab the lock and
808 * state for the new crtc, as needed. This function takes care of all these
809 * details besides updating the pointer in the state object itself.
812 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
813 * then the w/w mutex code has detected a deadlock and the entire atomic
814 * sequence must be restarted. All other errors are fatal.
817 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
818 struct drm_crtc *crtc)
820 struct drm_crtc_state *crtc_state;
823 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
824 if (IS_ERR(crtc_state))
825 return PTR_ERR(crtc_state);
828 conn_state->crtc = crtc;
831 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
832 conn_state, crtc->base.id);
834 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
839 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
842 * drm_atomic_add_affected_connectors - add connectors for crtc
843 * @state: atomic state
846 * This function walks the current configuration and adds all connectors
847 * currently using @crtc to the atomic configuration @state. Note that this
848 * function must acquire the connection mutex. This can potentially cause
849 * unneeded seralization if the update is just for the planes on one crtc. Hence
850 * drivers and helpers should only call this when really needed (e.g. when a
851 * full modeset needs to happen due to some change).
854 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
855 * then the w/w mutex code has detected a deadlock and the entire atomic
856 * sequence must be restarted. All other errors are fatal.
859 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
860 struct drm_crtc *crtc)
862 struct drm_mode_config *config = &state->dev->mode_config;
863 struct drm_connector *connector;
864 struct drm_connector_state *conn_state;
867 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
871 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
872 crtc->base.id, state);
875 * Changed connectors are already in @state, so only need to look at the
876 * current configuration.
878 list_for_each_entry(connector, &config->connector_list, head) {
879 if (connector->state->crtc != crtc)
882 conn_state = drm_atomic_get_connector_state(state, connector);
883 if (IS_ERR(conn_state))
884 return PTR_ERR(conn_state);
889 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
892 * drm_atomic_connectors_for_crtc - count number of connected outputs
893 * @state: atomic state
896 * This function counts all connectors which will be connected to @crtc
897 * according to @state. Useful to recompute the enable state for @crtc.
900 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
901 struct drm_crtc *crtc)
903 int i, num_connected_connectors = 0;
905 for (i = 0; i < state->num_connector; i++) {
906 struct drm_connector_state *conn_state;
908 conn_state = state->connector_states[i];
910 if (conn_state && conn_state->crtc == crtc)
911 num_connected_connectors++;
914 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
915 state, num_connected_connectors, crtc->base.id);
917 return num_connected_connectors;
919 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
922 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
923 * @state: atomic state
925 * This function should be used by legacy entry points which don't understand
926 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
927 * the slowpath completed.
929 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
934 drm_modeset_backoff(state->acquire_ctx);
936 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
940 ret = drm_modeset_lock_all_crtcs(state->dev,
945 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
948 * drm_atomic_check_only - check whether a given config would work
949 * @state: atomic configuration to check
951 * Note that this function can return -EDEADLK if the driver needed to acquire
952 * more locks but encountered a deadlock. The caller must then do the usual w/w
953 * backoff dance and restart. All other errors are fatal.
956 * 0 on success, negative error code on failure.
958 int drm_atomic_check_only(struct drm_atomic_state *state)
960 struct drm_device *dev = state->dev;
961 struct drm_mode_config *config = &dev->mode_config;
962 int nplanes = config->num_total_plane;
963 int ncrtcs = config->num_crtc;
966 DRM_DEBUG_ATOMIC("checking %p\n", state);
968 for (i = 0; i < nplanes; i++) {
969 struct drm_plane *plane = state->planes[i];
974 ret = drm_atomic_plane_check(plane, state->plane_states[i]);
976 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
982 for (i = 0; i < ncrtcs; i++) {
983 struct drm_crtc *crtc = state->crtcs[i];
988 ret = drm_atomic_crtc_check(crtc, state->crtc_states[i]);
990 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
996 if (config->funcs->atomic_check)
997 ret = config->funcs->atomic_check(state->dev, state);
999 if (!state->allow_modeset) {
1000 for (i = 0; i < ncrtcs; i++) {
1001 struct drm_crtc *crtc = state->crtcs[i];
1002 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1007 if (crtc_state->mode_changed ||
1008 crtc_state->active_changed) {
1009 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1018 EXPORT_SYMBOL(drm_atomic_check_only);
1021 * drm_atomic_commit - commit configuration atomically
1022 * @state: atomic configuration to check
1024 * Note that this function can return -EDEADLK if the driver needed to acquire
1025 * more locks but encountered a deadlock. The caller must then do the usual w/w
1026 * backoff dance and restart. All other errors are fatal.
1028 * Also note that on successful execution ownership of @state is transferred
1029 * from the caller of this function to the function itself. The caller must not
1030 * free or in any other way access @state. If the function fails then the caller
1031 * must clean up @state itself.
1034 * 0 on success, negative error code on failure.
1036 int drm_atomic_commit(struct drm_atomic_state *state)
1038 struct drm_mode_config *config = &state->dev->mode_config;
1041 ret = drm_atomic_check_only(state);
1045 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1047 return config->funcs->atomic_commit(state->dev, state, false);
1049 EXPORT_SYMBOL(drm_atomic_commit);
1052 * drm_atomic_async_commit - atomic&async configuration commit
1053 * @state: atomic configuration to check
1055 * Note that this function can return -EDEADLK if the driver needed to acquire
1056 * more locks but encountered a deadlock. The caller must then do the usual w/w
1057 * backoff dance and restart. All other errors are fatal.
1059 * Also note that on successful execution ownership of @state is transferred
1060 * from the caller of this function to the function itself. The caller must not
1061 * free or in any other way access @state. If the function fails then the caller
1062 * must clean up @state itself.
1065 * 0 on success, negative error code on failure.
1067 int drm_atomic_async_commit(struct drm_atomic_state *state)
1069 struct drm_mode_config *config = &state->dev->mode_config;
1072 ret = drm_atomic_check_only(state);
1076 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1078 return config->funcs->atomic_commit(state->dev, state, true);
1080 EXPORT_SYMBOL(drm_atomic_async_commit);
1083 * The big monstor ioctl
1086 static struct drm_pending_vblank_event *create_vblank_event(
1087 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1089 struct drm_pending_vblank_event *e = NULL;
1090 unsigned long flags;
1092 spin_lock_irqsave(&dev->event_lock, flags);
1093 if (file_priv->event_space < sizeof e->event) {
1094 spin_unlock_irqrestore(&dev->event_lock, flags);
1097 file_priv->event_space -= sizeof e->event;
1098 spin_unlock_irqrestore(&dev->event_lock, flags);
1100 e = kzalloc(sizeof *e, GFP_KERNEL);
1102 spin_lock_irqsave(&dev->event_lock, flags);
1103 file_priv->event_space += sizeof e->event;
1104 spin_unlock_irqrestore(&dev->event_lock, flags);
1108 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1109 e->event.base.length = sizeof e->event;
1110 e->event.user_data = user_data;
1111 e->base.event = &e->event.base;
1112 e->base.file_priv = file_priv;
1113 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1119 static void destroy_vblank_event(struct drm_device *dev,
1120 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1122 unsigned long flags;
1124 spin_lock_irqsave(&dev->event_lock, flags);
1125 file_priv->event_space += sizeof e->event;
1126 spin_unlock_irqrestore(&dev->event_lock, flags);
1130 static int atomic_set_prop(struct drm_atomic_state *state,
1131 struct drm_mode_object *obj, struct drm_property *prop,
1132 uint64_t prop_value)
1134 struct drm_mode_object *ref;
1137 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1140 switch (obj->type) {
1141 case DRM_MODE_OBJECT_CONNECTOR: {
1142 struct drm_connector *connector = obj_to_connector(obj);
1143 struct drm_connector_state *connector_state;
1145 connector_state = drm_atomic_get_connector_state(state, connector);
1146 if (IS_ERR(connector_state)) {
1147 ret = PTR_ERR(connector_state);
1151 ret = drm_atomic_connector_set_property(connector,
1152 connector_state, prop, prop_value);
1155 case DRM_MODE_OBJECT_CRTC: {
1156 struct drm_crtc *crtc = obj_to_crtc(obj);
1157 struct drm_crtc_state *crtc_state;
1159 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1160 if (IS_ERR(crtc_state)) {
1161 ret = PTR_ERR(crtc_state);
1165 ret = drm_atomic_crtc_set_property(crtc,
1166 crtc_state, prop, prop_value);
1169 case DRM_MODE_OBJECT_PLANE: {
1170 struct drm_plane *plane = obj_to_plane(obj);
1171 struct drm_plane_state *plane_state;
1173 plane_state = drm_atomic_get_plane_state(state, plane);
1174 if (IS_ERR(plane_state)) {
1175 ret = PTR_ERR(plane_state);
1179 ret = drm_atomic_plane_set_property(plane,
1180 plane_state, prop, prop_value);
1188 drm_property_change_valid_put(prop, ref);
1192 int drm_mode_atomic_ioctl(struct drm_device *dev,
1193 void *data, struct drm_file *file_priv)
1195 struct drm_mode_atomic *arg = data;
1196 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1197 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1198 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1199 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1200 unsigned int copied_objs, copied_props;
1201 struct drm_atomic_state *state;
1202 struct drm_modeset_acquire_ctx ctx;
1203 struct drm_plane *plane;
1204 unsigned plane_mask = 0;
1208 /* disallow for drivers not supporting atomic: */
1209 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1212 /* disallow for userspace that has not enabled atomic cap (even
1213 * though this may be a bit overkill, since legacy userspace
1214 * wouldn't know how to call this ioctl)
1216 if (!file_priv->atomic)
1219 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1225 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1226 !dev->mode_config.async_page_flip)
1229 /* can't test and expect an event at the same time. */
1230 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1231 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1234 drm_modeset_acquire_init(&ctx, 0);
1236 state = drm_atomic_state_alloc(dev);
1240 state->acquire_ctx = &ctx;
1241 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1247 for (i = 0; i < arg->count_objs; i++) {
1248 uint32_t obj_id, count_props;
1249 struct drm_mode_object *obj;
1251 if (get_user(obj_id, objs_ptr + copied_objs)) {
1256 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1257 if (!obj || !obj->properties) {
1262 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1263 plane = obj_to_plane(obj);
1264 plane_mask |= (1 << drm_plane_index(plane));
1265 plane->old_fb = plane->fb;
1268 if (get_user(count_props, count_props_ptr + copied_objs)) {
1275 for (j = 0; j < count_props; j++) {
1277 uint64_t prop_value;
1278 struct drm_property *prop;
1280 if (get_user(prop_id, props_ptr + copied_props)) {
1285 prop = drm_property_find(dev, prop_id);
1291 if (copy_from_user(&prop_value,
1292 prop_values_ptr + copied_props,
1293 sizeof(prop_value))) {
1298 ret = atomic_set_prop(state, obj, prop, prop_value);
1306 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1307 int ncrtcs = dev->mode_config.num_crtc;
1309 for (i = 0; i < ncrtcs; i++) {
1310 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1311 struct drm_pending_vblank_event *e;
1316 e = create_vblank_event(dev, file_priv, arg->user_data);
1322 crtc_state->event = e;
1326 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1327 ret = drm_atomic_check_only(state);
1328 /* _check_only() does not free state, unlike _commit() */
1329 drm_atomic_state_free(state);
1330 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1331 ret = drm_atomic_async_commit(state);
1333 ret = drm_atomic_commit(state);
1336 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1337 * locks (ie. while it is still safe to deref plane->state). We
1338 * need to do this here because the driver entry points cannot
1339 * distinguish between legacy and atomic ioctls.
1341 drm_for_each_plane_mask(plane, dev, plane_mask) {
1343 struct drm_framebuffer *new_fb = plane->state->fb;
1345 drm_framebuffer_reference(new_fb);
1347 plane->crtc = plane->state->crtc;
1349 plane->old_fb = NULL;
1351 if (plane->old_fb) {
1352 drm_framebuffer_unreference(plane->old_fb);
1353 plane->old_fb = NULL;
1357 drm_modeset_drop_locks(&ctx);
1358 drm_modeset_acquire_fini(&ctx);
1363 if (ret == -EDEADLK)
1366 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1367 int ncrtcs = dev->mode_config.num_crtc;
1369 for (i = 0; i < ncrtcs; i++) {
1370 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1375 destroy_vblank_event(dev, file_priv, crtc_state->event);
1376 crtc_state->event = NULL;
1380 drm_atomic_state_free(state);
1382 drm_modeset_drop_locks(&ctx);
1383 drm_modeset_acquire_fini(&ctx);
1388 drm_atomic_state_clear(state);
1389 drm_modeset_backoff(&ctx);