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>
34 * drm_atomic_state_default_release -
35 * release memory initialized by drm_atomic_state_init
36 * @state: atomic state
38 * Free all the memory allocated by drm_atomic_state_init.
39 * This is useful for drivers that subclass the atomic state.
41 void drm_atomic_state_default_release(struct drm_atomic_state *state)
43 kfree(state->connectors);
44 kfree(state->connector_states);
46 kfree(state->crtc_states);
48 kfree(state->plane_states);
50 EXPORT_SYMBOL(drm_atomic_state_default_release);
53 * drm_atomic_state_init - init new atomic state
55 * @state: atomic state
57 * Default implementation for filling in a new atomic state.
58 * This is useful for drivers that subclass the atomic state.
61 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
63 /* TODO legacy paths should maybe do a better job about
64 * setting this appropriately?
66 state->allow_modeset = true;
68 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
70 state->crtcs = kcalloc(dev->mode_config.num_crtc,
71 sizeof(*state->crtcs), GFP_KERNEL);
74 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
75 sizeof(*state->crtc_states), GFP_KERNEL);
76 if (!state->crtc_states)
78 state->planes = kcalloc(dev->mode_config.num_total_plane,
79 sizeof(*state->planes), GFP_KERNEL);
82 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
83 sizeof(*state->plane_states), GFP_KERNEL);
84 if (!state->plane_states)
86 state->connectors = kcalloc(state->num_connector,
87 sizeof(*state->connectors),
89 if (!state->connectors)
91 state->connector_states = kcalloc(state->num_connector,
92 sizeof(*state->connector_states),
94 if (!state->connector_states)
99 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
103 drm_atomic_state_default_release(state);
106 EXPORT_SYMBOL(drm_atomic_state_init);
109 * drm_atomic_state_alloc - allocate atomic state
112 * This allocates an empty atomic state to track updates.
114 struct drm_atomic_state *
115 drm_atomic_state_alloc(struct drm_device *dev)
117 struct drm_mode_config *config = &dev->mode_config;
118 struct drm_atomic_state *state;
120 if (!config->funcs->atomic_state_alloc) {
121 state = kzalloc(sizeof(*state), GFP_KERNEL);
124 if (drm_atomic_state_init(dev, state) < 0) {
131 return config->funcs->atomic_state_alloc(dev);
133 EXPORT_SYMBOL(drm_atomic_state_alloc);
136 * drm_atomic_state_default_clear - clear base atomic state
137 * @state: atomic state
139 * Default implementation for clearing atomic state.
140 * This is useful for drivers that subclass the atomic state.
142 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
144 struct drm_device *dev = state->dev;
145 struct drm_mode_config *config = &dev->mode_config;
148 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
150 for (i = 0; i < state->num_connector; i++) {
151 struct drm_connector *connector = state->connectors[i];
156 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
158 connector->funcs->atomic_destroy_state(connector,
159 state->connector_states[i]);
160 state->connectors[i] = NULL;
161 state->connector_states[i] = NULL;
164 for (i = 0; i < config->num_crtc; i++) {
165 struct drm_crtc *crtc = state->crtcs[i];
170 crtc->funcs->atomic_destroy_state(crtc,
171 state->crtc_states[i]);
172 state->crtcs[i] = NULL;
173 state->crtc_states[i] = NULL;
176 for (i = 0; i < config->num_total_plane; i++) {
177 struct drm_plane *plane = state->planes[i];
182 plane->funcs->atomic_destroy_state(plane,
183 state->plane_states[i]);
184 state->planes[i] = NULL;
185 state->plane_states[i] = NULL;
188 EXPORT_SYMBOL(drm_atomic_state_default_clear);
191 * drm_atomic_state_clear - clear state object
192 * @state: atomic state
194 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
195 * all locks. So someone else could sneak in and change the current modeset
196 * configuration. Which means that all the state assembled in @state is no
197 * longer an atomic update to the current state, but to some arbitrary earlier
198 * state. Which could break assumptions the driver's ->atomic_check likely
201 * Hence we must clear all cached state and completely start over, using this
204 void drm_atomic_state_clear(struct drm_atomic_state *state)
206 struct drm_device *dev = state->dev;
207 struct drm_mode_config *config = &dev->mode_config;
209 if (config->funcs->atomic_state_clear)
210 config->funcs->atomic_state_clear(state);
212 drm_atomic_state_default_clear(state);
214 EXPORT_SYMBOL(drm_atomic_state_clear);
217 * drm_atomic_state_free - free all memory for an atomic state
218 * @state: atomic state to deallocate
220 * This frees all memory associated with an atomic state, including all the
221 * per-object state for planes, crtcs and connectors.
223 void drm_atomic_state_free(struct drm_atomic_state *state)
225 struct drm_device *dev;
226 struct drm_mode_config *config;
232 config = &dev->mode_config;
234 drm_atomic_state_clear(state);
236 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
238 if (config->funcs->atomic_state_free) {
239 config->funcs->atomic_state_free(state);
241 drm_atomic_state_default_release(state);
245 EXPORT_SYMBOL(drm_atomic_state_free);
248 * drm_atomic_get_crtc_state - get crtc state
249 * @state: global atomic state object
250 * @crtc: crtc to get state object for
252 * This function returns the crtc state for the given crtc, allocating it if
253 * needed. It will also grab the relevant crtc lock to make sure that the state
258 * Either the allocated state or the error code encoded into the pointer. When
259 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
260 * entire atomic sequence must be restarted. All other errors are fatal.
262 struct drm_crtc_state *
263 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
264 struct drm_crtc *crtc)
266 int ret, index = drm_crtc_index(crtc);
267 struct drm_crtc_state *crtc_state;
269 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
273 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
277 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
279 return ERR_PTR(-ENOMEM);
281 state->crtc_states[index] = crtc_state;
282 state->crtcs[index] = crtc;
283 crtc_state->state = state;
285 DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
286 crtc->base.id, crtc_state, state);
290 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
293 * drm_atomic_crtc_set_property - set property on CRTC
294 * @crtc: the drm CRTC to set a property on
295 * @state: the state object to update with the new property value
296 * @property: the property to set
297 * @val: the new property value
299 * Use this instead of calling crtc->atomic_set_property directly.
300 * This function handles generic/core properties and calls out to
301 * driver's ->atomic_set_property() for driver properties. To ensure
302 * consistent behavior you must call this function rather than the
303 * driver hook directly.
306 * Zero on success, error code on failure
308 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
309 struct drm_crtc_state *state, struct drm_property *property,
312 struct drm_device *dev = crtc->dev;
313 struct drm_mode_config *config = &dev->mode_config;
315 /* FIXME: Mode prop is missing, which also controls ->enable. */
316 if (property == config->prop_active)
318 else if (crtc->funcs->atomic_set_property)
319 return crtc->funcs->atomic_set_property(crtc, state, property, val);
325 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
328 * This function handles generic/core properties and calls out to
329 * driver's ->atomic_get_property() for driver properties. To ensure
330 * consistent behavior you must call this function rather than the
331 * driver hook directly.
333 int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
334 const struct drm_crtc_state *state,
335 struct drm_property *property, uint64_t *val)
337 struct drm_device *dev = crtc->dev;
338 struct drm_mode_config *config = &dev->mode_config;
340 if (property == config->prop_active)
341 *val = state->active;
342 else if (crtc->funcs->atomic_get_property)
343 return crtc->funcs->atomic_get_property(crtc, state, property, val);
351 * drm_atomic_crtc_check - check crtc state
352 * @crtc: crtc to check
353 * @state: crtc state to check
355 * Provides core sanity checks for crtc state.
358 * Zero on success, error code on failure
360 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
361 struct drm_crtc_state *state)
363 /* NOTE: we explicitly don't enforce constraints such as primary
364 * layer covering entire screen, since that is something we want
365 * to allow (on hw that supports it). For hw that does not, it
366 * should be checked in driver's crtc->atomic_check() vfunc.
368 * TODO: Add generic modeset state checks once we support those.
371 if (state->active && !state->enable) {
372 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
381 * drm_atomic_get_plane_state - get plane state
382 * @state: global atomic state object
383 * @plane: plane to get state object for
385 * This function returns the plane state for the given plane, allocating it if
386 * needed. It will also grab the relevant plane lock to make sure that the state
391 * Either the allocated state or the error code encoded into the pointer. When
392 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
393 * entire atomic sequence must be restarted. All other errors are fatal.
395 struct drm_plane_state *
396 drm_atomic_get_plane_state(struct drm_atomic_state *state,
397 struct drm_plane *plane)
399 int ret, index = drm_plane_index(plane);
400 struct drm_plane_state *plane_state;
402 plane_state = drm_atomic_get_existing_plane_state(state, plane);
406 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
410 plane_state = plane->funcs->atomic_duplicate_state(plane);
412 return ERR_PTR(-ENOMEM);
414 state->plane_states[index] = plane_state;
415 state->planes[index] = plane;
416 plane_state->state = state;
418 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
419 plane->base.id, plane_state, state);
421 if (plane_state->crtc) {
422 struct drm_crtc_state *crtc_state;
424 crtc_state = drm_atomic_get_crtc_state(state,
426 if (IS_ERR(crtc_state))
427 return ERR_CAST(crtc_state);
432 EXPORT_SYMBOL(drm_atomic_get_plane_state);
435 * drm_atomic_plane_set_property - set property on plane
436 * @plane: the drm plane to set a property on
437 * @state: the state object to update with the new property value
438 * @property: the property to set
439 * @val: the new property value
441 * Use this instead of calling plane->atomic_set_property directly.
442 * This function handles generic/core properties and calls out to
443 * driver's ->atomic_set_property() for driver properties. To ensure
444 * consistent behavior you must call this function rather than the
445 * driver hook directly.
448 * Zero on success, error code on failure
450 int drm_atomic_plane_set_property(struct drm_plane *plane,
451 struct drm_plane_state *state, struct drm_property *property,
454 struct drm_device *dev = plane->dev;
455 struct drm_mode_config *config = &dev->mode_config;
457 if (property == config->prop_fb_id) {
458 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
459 drm_atomic_set_fb_for_plane(state, fb);
461 drm_framebuffer_unreference(fb);
462 } else if (property == config->prop_crtc_id) {
463 struct drm_crtc *crtc = drm_crtc_find(dev, val);
464 return drm_atomic_set_crtc_for_plane(state, crtc);
465 } else if (property == config->prop_crtc_x) {
466 state->crtc_x = U642I64(val);
467 } else if (property == config->prop_crtc_y) {
468 state->crtc_y = U642I64(val);
469 } else if (property == config->prop_crtc_w) {
471 } else if (property == config->prop_crtc_h) {
473 } else if (property == config->prop_src_x) {
475 } else if (property == config->prop_src_y) {
477 } else if (property == config->prop_src_w) {
479 } else if (property == config->prop_src_h) {
481 } else if (property == config->rotation_property) {
482 state->rotation = val;
483 } else if (plane->funcs->atomic_set_property) {
484 return plane->funcs->atomic_set_property(plane, state,
492 EXPORT_SYMBOL(drm_atomic_plane_set_property);
495 * This function handles generic/core properties and calls out to
496 * driver's ->atomic_get_property() for driver properties. To ensure
497 * consistent behavior you must call this function rather than the
498 * driver hook directly.
501 drm_atomic_plane_get_property(struct drm_plane *plane,
502 const struct drm_plane_state *state,
503 struct drm_property *property, uint64_t *val)
505 struct drm_device *dev = plane->dev;
506 struct drm_mode_config *config = &dev->mode_config;
508 if (property == config->prop_fb_id) {
509 *val = (state->fb) ? state->fb->base.id : 0;
510 } else if (property == config->prop_crtc_id) {
511 *val = (state->crtc) ? state->crtc->base.id : 0;
512 } else if (property == config->prop_crtc_x) {
513 *val = I642U64(state->crtc_x);
514 } else if (property == config->prop_crtc_y) {
515 *val = I642U64(state->crtc_y);
516 } else if (property == config->prop_crtc_w) {
517 *val = state->crtc_w;
518 } else if (property == config->prop_crtc_h) {
519 *val = state->crtc_h;
520 } else if (property == config->prop_src_x) {
522 } else if (property == config->prop_src_y) {
524 } else if (property == config->prop_src_w) {
526 } else if (property == config->prop_src_h) {
528 } else if (property == config->rotation_property) {
529 *val = state->rotation;
530 } else if (plane->funcs->atomic_get_property) {
531 return plane->funcs->atomic_get_property(plane, state, property, val);
540 * drm_atomic_plane_check - check plane state
541 * @plane: plane to check
542 * @state: plane state to check
544 * Provides core sanity checks for plane state.
547 * Zero on success, error code on failure
549 static int drm_atomic_plane_check(struct drm_plane *plane,
550 struct drm_plane_state *state)
552 unsigned int fb_width, fb_height;
555 /* either *both* CRTC and FB must be set, or neither */
556 if (WARN_ON(state->crtc && !state->fb)) {
557 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
559 } else if (WARN_ON(state->fb && !state->crtc)) {
560 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
564 /* if disabled, we don't care about the rest of the state: */
568 /* Check whether this plane is usable on this CRTC */
569 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
570 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
574 /* Check whether this plane supports the fb pixel format. */
575 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
577 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
578 drm_get_format_name(state->fb->pixel_format));
582 /* Give drivers some help against integer overflows */
583 if (state->crtc_w > INT_MAX ||
584 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
585 state->crtc_h > INT_MAX ||
586 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
587 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
588 state->crtc_w, state->crtc_h,
589 state->crtc_x, state->crtc_y);
593 fb_width = state->fb->width << 16;
594 fb_height = state->fb->height << 16;
596 /* Make sure source coordinates are inside the fb. */
597 if (state->src_w > fb_width ||
598 state->src_x > fb_width - state->src_w ||
599 state->src_h > fb_height ||
600 state->src_y > fb_height - state->src_h) {
601 DRM_DEBUG_ATOMIC("Invalid source coordinates "
602 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
603 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
604 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
605 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
606 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
614 * drm_atomic_get_connector_state - get connector state
615 * @state: global atomic state object
616 * @connector: connector to get state object for
618 * This function returns the connector state for the given connector,
619 * allocating it if needed. It will also grab the relevant connector lock to
620 * make sure that the state is consistent.
624 * Either the allocated state or the error code encoded into the pointer. When
625 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
626 * entire atomic sequence must be restarted. All other errors are fatal.
628 struct drm_connector_state *
629 drm_atomic_get_connector_state(struct drm_atomic_state *state,
630 struct drm_connector *connector)
633 struct drm_mode_config *config = &connector->dev->mode_config;
634 struct drm_connector_state *connector_state;
636 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
640 index = drm_connector_index(connector);
643 * Construction of atomic state updates can race with a connector
644 * hot-add which might overflow. In this case flip the table and just
645 * restart the entire ioctl - no one is fast enough to livelock a cpu
646 * with physical hotplug events anyway.
648 * Note that we only grab the indexes once we have the right lock to
649 * prevent hotplug/unplugging of connectors. So removal is no problem,
650 * at most the array is a bit too large.
652 if (index >= state->num_connector) {
653 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
654 return ERR_PTR(-EAGAIN);
657 if (state->connector_states[index])
658 return state->connector_states[index];
660 connector_state = connector->funcs->atomic_duplicate_state(connector);
661 if (!connector_state)
662 return ERR_PTR(-ENOMEM);
664 state->connector_states[index] = connector_state;
665 state->connectors[index] = connector;
666 connector_state->state = state;
668 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
669 connector->base.id, connector_state, state);
671 if (connector_state->crtc) {
672 struct drm_crtc_state *crtc_state;
674 crtc_state = drm_atomic_get_crtc_state(state,
675 connector_state->crtc);
676 if (IS_ERR(crtc_state))
677 return ERR_CAST(crtc_state);
680 return connector_state;
682 EXPORT_SYMBOL(drm_atomic_get_connector_state);
685 * drm_atomic_connector_set_property - set property on connector.
686 * @connector: the drm connector to set a property on
687 * @state: the state object to update with the new property value
688 * @property: the property to set
689 * @val: the new property value
691 * Use this instead of calling connector->atomic_set_property directly.
692 * This function handles generic/core properties and calls out to
693 * driver's ->atomic_set_property() for driver properties. To ensure
694 * consistent behavior you must call this function rather than the
695 * driver hook directly.
698 * Zero on success, error code on failure
700 int drm_atomic_connector_set_property(struct drm_connector *connector,
701 struct drm_connector_state *state, struct drm_property *property,
704 struct drm_device *dev = connector->dev;
705 struct drm_mode_config *config = &dev->mode_config;
707 if (property == config->prop_crtc_id) {
708 struct drm_crtc *crtc = drm_crtc_find(dev, val);
709 return drm_atomic_set_crtc_for_connector(state, crtc);
710 } else if (property == config->dpms_property) {
711 /* setting DPMS property requires special handling, which
712 * is done in legacy setprop path for us. Disallow (for
713 * now?) atomic writes to DPMS property:
716 } else if (connector->funcs->atomic_set_property) {
717 return connector->funcs->atomic_set_property(connector,
718 state, property, val);
723 EXPORT_SYMBOL(drm_atomic_connector_set_property);
726 * This function handles generic/core properties and calls out to
727 * driver's ->atomic_get_property() for driver properties. To ensure
728 * consistent behavior you must call this function rather than the
729 * driver hook directly.
732 drm_atomic_connector_get_property(struct drm_connector *connector,
733 const struct drm_connector_state *state,
734 struct drm_property *property, uint64_t *val)
736 struct drm_device *dev = connector->dev;
737 struct drm_mode_config *config = &dev->mode_config;
739 if (property == config->prop_crtc_id) {
740 *val = (state->crtc) ? state->crtc->base.id : 0;
741 } else if (property == config->dpms_property) {
742 *val = connector->dpms;
743 } else if (connector->funcs->atomic_get_property) {
744 return connector->funcs->atomic_get_property(connector,
745 state, property, val);
753 int drm_atomic_get_property(struct drm_mode_object *obj,
754 struct drm_property *property, uint64_t *val)
756 struct drm_device *dev = property->dev;
760 case DRM_MODE_OBJECT_CONNECTOR: {
761 struct drm_connector *connector = obj_to_connector(obj);
762 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
763 ret = drm_atomic_connector_get_property(connector,
764 connector->state, property, val);
767 case DRM_MODE_OBJECT_CRTC: {
768 struct drm_crtc *crtc = obj_to_crtc(obj);
769 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
770 ret = drm_atomic_crtc_get_property(crtc,
771 crtc->state, property, val);
774 case DRM_MODE_OBJECT_PLANE: {
775 struct drm_plane *plane = obj_to_plane(obj);
776 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
777 ret = drm_atomic_plane_get_property(plane,
778 plane->state, property, val);
790 * drm_atomic_set_crtc_for_plane - set crtc for plane
791 * @plane_state: the plane whose incoming state to update
792 * @crtc: crtc to use for the plane
794 * Changing the assigned crtc for a plane requires us to grab the lock and state
795 * for the new crtc, as needed. This function takes care of all these details
796 * besides updating the pointer in the state object itself.
799 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
800 * then the w/w mutex code has detected a deadlock and the entire atomic
801 * sequence must be restarted. All other errors are fatal.
804 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
805 struct drm_crtc *crtc)
807 struct drm_plane *plane = plane_state->plane;
808 struct drm_crtc_state *crtc_state;
810 if (plane_state->crtc) {
811 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
813 if (WARN_ON(IS_ERR(crtc_state)))
814 return PTR_ERR(crtc_state);
816 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
819 plane_state->crtc = crtc;
822 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
824 if (IS_ERR(crtc_state))
825 return PTR_ERR(crtc_state);
826 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
830 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
831 plane_state, crtc->base.id);
833 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
838 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
841 * drm_atomic_set_fb_for_plane - set framebuffer for plane
842 * @plane_state: atomic state object for the plane
843 * @fb: fb to use for the plane
845 * Changing the assigned framebuffer for a plane requires us to grab a reference
846 * to the new fb and drop the reference to the old fb, if there is one. This
847 * function takes care of all these details besides updating the pointer in the
848 * state object itself.
851 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
852 struct drm_framebuffer *fb)
855 drm_framebuffer_unreference(plane_state->fb);
857 drm_framebuffer_reference(fb);
858 plane_state->fb = fb;
861 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
862 fb->base.id, plane_state);
864 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
867 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
870 * drm_atomic_set_crtc_for_connector - set crtc for connector
871 * @conn_state: atomic state object for the connector
872 * @crtc: crtc to use for the connector
874 * Changing the assigned crtc for a connector requires us to grab the lock and
875 * state for the new crtc, as needed. This function takes care of all these
876 * details besides updating the pointer in the state object itself.
879 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
880 * then the w/w mutex code has detected a deadlock and the entire atomic
881 * sequence must be restarted. All other errors are fatal.
884 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
885 struct drm_crtc *crtc)
887 struct drm_crtc_state *crtc_state;
890 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
891 if (IS_ERR(crtc_state))
892 return PTR_ERR(crtc_state);
895 conn_state->crtc = crtc;
898 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
899 conn_state, crtc->base.id);
901 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
906 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
909 * drm_atomic_add_affected_connectors - add connectors for crtc
910 * @state: atomic state
913 * This function walks the current configuration and adds all connectors
914 * currently using @crtc to the atomic configuration @state. Note that this
915 * function must acquire the connection mutex. This can potentially cause
916 * unneeded seralization if the update is just for the planes on one crtc. Hence
917 * drivers and helpers should only call this when really needed (e.g. when a
918 * full modeset needs to happen due to some change).
921 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
922 * then the w/w mutex code has detected a deadlock and the entire atomic
923 * sequence must be restarted. All other errors are fatal.
926 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
927 struct drm_crtc *crtc)
929 struct drm_mode_config *config = &state->dev->mode_config;
930 struct drm_connector *connector;
931 struct drm_connector_state *conn_state;
934 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
938 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
939 crtc->base.id, state);
942 * Changed connectors are already in @state, so only need to look at the
943 * current configuration.
945 list_for_each_entry(connector, &config->connector_list, head) {
946 if (connector->state->crtc != crtc)
949 conn_state = drm_atomic_get_connector_state(state, connector);
950 if (IS_ERR(conn_state))
951 return PTR_ERR(conn_state);
956 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
959 * drm_atomic_add_affected_planes - add planes for crtc
960 * @state: atomic state
963 * This function walks the current configuration and adds all planes
964 * currently used by @crtc to the atomic configuration @state. This is useful
965 * when an atomic commit also needs to check all currently enabled plane on
966 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
967 * to avoid special code to force-enable all planes.
969 * Since acquiring a plane state will always also acquire the w/w mutex of the
970 * current CRTC for that plane (if there is any) adding all the plane states for
971 * a CRTC will not reduce parallism of atomic updates.
974 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
975 * then the w/w mutex code has detected a deadlock and the entire atomic
976 * sequence must be restarted. All other errors are fatal.
979 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
980 struct drm_crtc *crtc)
982 struct drm_plane *plane;
984 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
986 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
987 struct drm_plane_state *plane_state =
988 drm_atomic_get_plane_state(state, plane);
990 if (IS_ERR(plane_state))
991 return PTR_ERR(plane_state);
995 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
998 * drm_atomic_connectors_for_crtc - count number of connected outputs
999 * @state: atomic state
1002 * This function counts all connectors which will be connected to @crtc
1003 * according to @state. Useful to recompute the enable state for @crtc.
1006 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
1007 struct drm_crtc *crtc)
1009 struct drm_connector *connector;
1010 struct drm_connector_state *conn_state;
1012 int i, num_connected_connectors = 0;
1014 for_each_connector_in_state(state, connector, conn_state, i) {
1015 if (conn_state->crtc == crtc)
1016 num_connected_connectors++;
1019 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
1020 state, num_connected_connectors, crtc->base.id);
1022 return num_connected_connectors;
1024 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
1027 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1028 * @state: atomic state
1030 * This function should be used by legacy entry points which don't understand
1031 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1032 * the slowpath completed.
1034 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1039 drm_modeset_backoff(state->acquire_ctx);
1041 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1042 state->acquire_ctx);
1045 ret = drm_modeset_lock_all_crtcs(state->dev,
1046 state->acquire_ctx);
1050 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1053 * drm_atomic_check_only - check whether a given config would work
1054 * @state: atomic configuration to check
1056 * Note that this function can return -EDEADLK if the driver needed to acquire
1057 * more locks but encountered a deadlock. The caller must then do the usual w/w
1058 * backoff dance and restart. All other errors are fatal.
1061 * 0 on success, negative error code on failure.
1063 int drm_atomic_check_only(struct drm_atomic_state *state)
1065 struct drm_device *dev = state->dev;
1066 struct drm_mode_config *config = &dev->mode_config;
1067 struct drm_plane *plane;
1068 struct drm_plane_state *plane_state;
1069 struct drm_crtc *crtc;
1070 struct drm_crtc_state *crtc_state;
1073 DRM_DEBUG_ATOMIC("checking %p\n", state);
1075 for_each_plane_in_state(state, plane, plane_state, i) {
1076 ret = drm_atomic_plane_check(plane, plane_state);
1078 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
1084 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1085 ret = drm_atomic_crtc_check(crtc, crtc_state);
1087 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1093 if (config->funcs->atomic_check)
1094 ret = config->funcs->atomic_check(state->dev, state);
1096 if (!state->allow_modeset) {
1097 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1098 if (crtc_state->mode_changed ||
1099 crtc_state->active_changed) {
1100 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1109 EXPORT_SYMBOL(drm_atomic_check_only);
1112 * drm_atomic_commit - commit configuration atomically
1113 * @state: atomic configuration to check
1115 * Note that this function can return -EDEADLK if the driver needed to acquire
1116 * more locks but encountered a deadlock. The caller must then do the usual w/w
1117 * backoff dance and restart. All other errors are fatal.
1119 * Also note that on successful execution ownership of @state is transferred
1120 * from the caller of this function to the function itself. The caller must not
1121 * free or in any other way access @state. If the function fails then the caller
1122 * must clean up @state itself.
1125 * 0 on success, negative error code on failure.
1127 int drm_atomic_commit(struct drm_atomic_state *state)
1129 struct drm_mode_config *config = &state->dev->mode_config;
1132 ret = drm_atomic_check_only(state);
1136 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1138 return config->funcs->atomic_commit(state->dev, state, false);
1140 EXPORT_SYMBOL(drm_atomic_commit);
1143 * drm_atomic_async_commit - atomic&async configuration commit
1144 * @state: atomic configuration to check
1146 * Note that this function can return -EDEADLK if the driver needed to acquire
1147 * more locks but encountered a deadlock. The caller must then do the usual w/w
1148 * backoff dance and restart. All other errors are fatal.
1150 * Also note that on successful execution ownership of @state is transferred
1151 * from the caller of this function to the function itself. The caller must not
1152 * free or in any other way access @state. If the function fails then the caller
1153 * must clean up @state itself.
1156 * 0 on success, negative error code on failure.
1158 int drm_atomic_async_commit(struct drm_atomic_state *state)
1160 struct drm_mode_config *config = &state->dev->mode_config;
1163 ret = drm_atomic_check_only(state);
1167 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1169 return config->funcs->atomic_commit(state->dev, state, true);
1171 EXPORT_SYMBOL(drm_atomic_async_commit);
1174 * The big monstor ioctl
1177 static struct drm_pending_vblank_event *create_vblank_event(
1178 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1180 struct drm_pending_vblank_event *e = NULL;
1181 unsigned long flags;
1183 spin_lock_irqsave(&dev->event_lock, flags);
1184 if (file_priv->event_space < sizeof e->event) {
1185 spin_unlock_irqrestore(&dev->event_lock, flags);
1188 file_priv->event_space -= sizeof e->event;
1189 spin_unlock_irqrestore(&dev->event_lock, flags);
1191 e = kzalloc(sizeof *e, GFP_KERNEL);
1193 spin_lock_irqsave(&dev->event_lock, flags);
1194 file_priv->event_space += sizeof e->event;
1195 spin_unlock_irqrestore(&dev->event_lock, flags);
1199 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1200 e->event.base.length = sizeof e->event;
1201 e->event.user_data = user_data;
1202 e->base.event = &e->event.base;
1203 e->base.file_priv = file_priv;
1204 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1210 static void destroy_vblank_event(struct drm_device *dev,
1211 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1213 unsigned long flags;
1215 spin_lock_irqsave(&dev->event_lock, flags);
1216 file_priv->event_space += sizeof e->event;
1217 spin_unlock_irqrestore(&dev->event_lock, flags);
1221 static int atomic_set_prop(struct drm_atomic_state *state,
1222 struct drm_mode_object *obj, struct drm_property *prop,
1223 uint64_t prop_value)
1225 struct drm_mode_object *ref;
1228 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1231 switch (obj->type) {
1232 case DRM_MODE_OBJECT_CONNECTOR: {
1233 struct drm_connector *connector = obj_to_connector(obj);
1234 struct drm_connector_state *connector_state;
1236 connector_state = drm_atomic_get_connector_state(state, connector);
1237 if (IS_ERR(connector_state)) {
1238 ret = PTR_ERR(connector_state);
1242 ret = drm_atomic_connector_set_property(connector,
1243 connector_state, prop, prop_value);
1246 case DRM_MODE_OBJECT_CRTC: {
1247 struct drm_crtc *crtc = obj_to_crtc(obj);
1248 struct drm_crtc_state *crtc_state;
1250 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1251 if (IS_ERR(crtc_state)) {
1252 ret = PTR_ERR(crtc_state);
1256 ret = drm_atomic_crtc_set_property(crtc,
1257 crtc_state, prop, prop_value);
1260 case DRM_MODE_OBJECT_PLANE: {
1261 struct drm_plane *plane = obj_to_plane(obj);
1262 struct drm_plane_state *plane_state;
1264 plane_state = drm_atomic_get_plane_state(state, plane);
1265 if (IS_ERR(plane_state)) {
1266 ret = PTR_ERR(plane_state);
1270 ret = drm_atomic_plane_set_property(plane,
1271 plane_state, prop, prop_value);
1279 drm_property_change_valid_put(prop, ref);
1283 int drm_mode_atomic_ioctl(struct drm_device *dev,
1284 void *data, struct drm_file *file_priv)
1286 struct drm_mode_atomic *arg = data;
1287 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1288 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1289 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1290 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1291 unsigned int copied_objs, copied_props;
1292 struct drm_atomic_state *state;
1293 struct drm_modeset_acquire_ctx ctx;
1294 struct drm_plane *plane;
1295 struct drm_crtc *crtc;
1296 struct drm_crtc_state *crtc_state;
1297 unsigned plane_mask = 0;
1301 /* disallow for drivers not supporting atomic: */
1302 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1305 /* disallow for userspace that has not enabled atomic cap (even
1306 * though this may be a bit overkill, since legacy userspace
1307 * wouldn't know how to call this ioctl)
1309 if (!file_priv->atomic)
1312 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1318 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1319 !dev->mode_config.async_page_flip)
1322 /* can't test and expect an event at the same time. */
1323 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1324 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1327 drm_modeset_acquire_init(&ctx, 0);
1329 state = drm_atomic_state_alloc(dev);
1333 state->acquire_ctx = &ctx;
1334 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1340 for (i = 0; i < arg->count_objs; i++) {
1341 uint32_t obj_id, count_props;
1342 struct drm_mode_object *obj;
1344 if (get_user(obj_id, objs_ptr + copied_objs)) {
1349 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1350 if (!obj || !obj->properties) {
1355 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1356 plane = obj_to_plane(obj);
1357 plane_mask |= (1 << drm_plane_index(plane));
1358 plane->old_fb = plane->fb;
1361 if (get_user(count_props, count_props_ptr + copied_objs)) {
1368 for (j = 0; j < count_props; j++) {
1370 uint64_t prop_value;
1371 struct drm_property *prop;
1373 if (get_user(prop_id, props_ptr + copied_props)) {
1378 prop = drm_property_find(dev, prop_id);
1384 if (copy_from_user(&prop_value,
1385 prop_values_ptr + copied_props,
1386 sizeof(prop_value))) {
1391 ret = atomic_set_prop(state, obj, prop, prop_value);
1399 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1400 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1401 struct drm_pending_vblank_event *e;
1403 e = create_vblank_event(dev, file_priv, arg->user_data);
1409 crtc_state->event = e;
1413 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1414 ret = drm_atomic_check_only(state);
1415 /* _check_only() does not free state, unlike _commit() */
1416 drm_atomic_state_free(state);
1417 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1418 ret = drm_atomic_async_commit(state);
1420 ret = drm_atomic_commit(state);
1423 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1424 * locks (ie. while it is still safe to deref plane->state). We
1425 * need to do this here because the driver entry points cannot
1426 * distinguish between legacy and atomic ioctls.
1428 drm_for_each_plane_mask(plane, dev, plane_mask) {
1430 struct drm_framebuffer *new_fb = plane->state->fb;
1432 drm_framebuffer_reference(new_fb);
1434 plane->crtc = plane->state->crtc;
1436 plane->old_fb = NULL;
1438 if (plane->old_fb) {
1439 drm_framebuffer_unreference(plane->old_fb);
1440 plane->old_fb = NULL;
1444 drm_modeset_drop_locks(&ctx);
1445 drm_modeset_acquire_fini(&ctx);
1450 if (ret == -EDEADLK)
1453 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1454 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1455 destroy_vblank_event(dev, file_priv, crtc_state->event);
1456 crtc_state->event = NULL;
1460 drm_atomic_state_free(state);
1462 drm_modeset_drop_locks(&ctx);
1463 drm_modeset_acquire_fini(&ctx);
1468 drm_atomic_state_clear(state);
1469 drm_modeset_backoff(&ctx);