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_set_mode_for_crtc - set mode for CRTC
294 * @state: the CRTC whose incoming state to update
295 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
297 * Set a mode (originating from the kernel) on the desired CRTC state. Does
298 * not change any other state properties, including enable, active, or
302 * Zero on success, error code on failure. Cannot return -EDEADLK.
304 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
305 struct drm_display_mode *mode)
307 struct drm_mode_modeinfo umode;
309 /* Early return for no change. */
310 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
313 if (state->mode_blob)
314 drm_property_unreference_blob(state->mode_blob);
315 state->mode_blob = NULL;
318 drm_mode_convert_to_umode(&umode, mode);
320 drm_property_create_blob(state->crtc->dev,
323 if (IS_ERR(state->mode_blob))
324 return PTR_ERR(state->mode_blob);
326 drm_mode_copy(&state->mode, mode);
327 state->enable = true;
328 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
331 memset(&state->mode, 0, sizeof(state->mode));
332 state->enable = false;
333 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
339 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
342 * drm_atomic_crtc_set_property - set property on CRTC
343 * @crtc: the drm CRTC to set a property on
344 * @state: the state object to update with the new property value
345 * @property: the property to set
346 * @val: the new property value
348 * Use this instead of calling crtc->atomic_set_property directly.
349 * This function handles generic/core properties and calls out to
350 * driver's ->atomic_set_property() for driver properties. To ensure
351 * consistent behavior you must call this function rather than the
352 * driver hook directly.
355 * Zero on success, error code on failure
357 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
358 struct drm_crtc_state *state, struct drm_property *property,
361 struct drm_device *dev = crtc->dev;
362 struct drm_mode_config *config = &dev->mode_config;
364 /* FIXME: Mode prop is missing, which also controls ->enable. */
365 if (property == config->prop_active)
367 else if (crtc->funcs->atomic_set_property)
368 return crtc->funcs->atomic_set_property(crtc, state, property, val);
374 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
377 * This function handles generic/core properties and calls out to
378 * driver's ->atomic_get_property() for driver properties. To ensure
379 * consistent behavior you must call this function rather than the
380 * driver hook directly.
382 int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
383 const struct drm_crtc_state *state,
384 struct drm_property *property, uint64_t *val)
386 struct drm_device *dev = crtc->dev;
387 struct drm_mode_config *config = &dev->mode_config;
389 if (property == config->prop_active)
390 *val = state->active;
391 else if (crtc->funcs->atomic_get_property)
392 return crtc->funcs->atomic_get_property(crtc, state, property, val);
400 * drm_atomic_crtc_check - check crtc state
401 * @crtc: crtc to check
402 * @state: crtc state to check
404 * Provides core sanity checks for crtc state.
407 * Zero on success, error code on failure
409 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
410 struct drm_crtc_state *state)
412 /* NOTE: we explicitly don't enforce constraints such as primary
413 * layer covering entire screen, since that is something we want
414 * to allow (on hw that supports it). For hw that does not, it
415 * should be checked in driver's crtc->atomic_check() vfunc.
417 * TODO: Add generic modeset state checks once we support those.
420 if (state->active && !state->enable) {
421 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
426 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
427 * as this is a kernel-internal detail that userspace should never
428 * be able to trigger. */
429 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
430 WARN_ON(state->enable && !state->mode_blob)) {
431 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled without mode blob\n",
436 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
437 WARN_ON(!state->enable && state->mode_blob)) {
438 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled with mode blob\n",
447 * drm_atomic_get_plane_state - get plane state
448 * @state: global atomic state object
449 * @plane: plane to get state object for
451 * This function returns the plane state for the given plane, allocating it if
452 * needed. It will also grab the relevant plane lock to make sure that the state
457 * Either the allocated state or the error code encoded into the pointer. When
458 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
459 * entire atomic sequence must be restarted. All other errors are fatal.
461 struct drm_plane_state *
462 drm_atomic_get_plane_state(struct drm_atomic_state *state,
463 struct drm_plane *plane)
465 int ret, index = drm_plane_index(plane);
466 struct drm_plane_state *plane_state;
468 plane_state = drm_atomic_get_existing_plane_state(state, plane);
472 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
476 plane_state = plane->funcs->atomic_duplicate_state(plane);
478 return ERR_PTR(-ENOMEM);
480 state->plane_states[index] = plane_state;
481 state->planes[index] = plane;
482 plane_state->state = state;
484 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
485 plane->base.id, plane_state, state);
487 if (plane_state->crtc) {
488 struct drm_crtc_state *crtc_state;
490 crtc_state = drm_atomic_get_crtc_state(state,
492 if (IS_ERR(crtc_state))
493 return ERR_CAST(crtc_state);
498 EXPORT_SYMBOL(drm_atomic_get_plane_state);
501 * drm_atomic_plane_set_property - set property on plane
502 * @plane: the drm plane to set a property on
503 * @state: the state object to update with the new property value
504 * @property: the property to set
505 * @val: the new property value
507 * Use this instead of calling plane->atomic_set_property directly.
508 * This function handles generic/core properties and calls out to
509 * driver's ->atomic_set_property() for driver properties. To ensure
510 * consistent behavior you must call this function rather than the
511 * driver hook directly.
514 * Zero on success, error code on failure
516 int drm_atomic_plane_set_property(struct drm_plane *plane,
517 struct drm_plane_state *state, struct drm_property *property,
520 struct drm_device *dev = plane->dev;
521 struct drm_mode_config *config = &dev->mode_config;
523 if (property == config->prop_fb_id) {
524 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
525 drm_atomic_set_fb_for_plane(state, fb);
527 drm_framebuffer_unreference(fb);
528 } else if (property == config->prop_crtc_id) {
529 struct drm_crtc *crtc = drm_crtc_find(dev, val);
530 return drm_atomic_set_crtc_for_plane(state, crtc);
531 } else if (property == config->prop_crtc_x) {
532 state->crtc_x = U642I64(val);
533 } else if (property == config->prop_crtc_y) {
534 state->crtc_y = U642I64(val);
535 } else if (property == config->prop_crtc_w) {
537 } else if (property == config->prop_crtc_h) {
539 } else if (property == config->prop_src_x) {
541 } else if (property == config->prop_src_y) {
543 } else if (property == config->prop_src_w) {
545 } else if (property == config->prop_src_h) {
547 } else if (property == config->rotation_property) {
548 state->rotation = val;
549 } else if (plane->funcs->atomic_set_property) {
550 return plane->funcs->atomic_set_property(plane, state,
558 EXPORT_SYMBOL(drm_atomic_plane_set_property);
561 * This function handles generic/core properties and calls out to
562 * driver's ->atomic_get_property() for driver properties. To ensure
563 * consistent behavior you must call this function rather than the
564 * driver hook directly.
567 drm_atomic_plane_get_property(struct drm_plane *plane,
568 const struct drm_plane_state *state,
569 struct drm_property *property, uint64_t *val)
571 struct drm_device *dev = plane->dev;
572 struct drm_mode_config *config = &dev->mode_config;
574 if (property == config->prop_fb_id) {
575 *val = (state->fb) ? state->fb->base.id : 0;
576 } else if (property == config->prop_crtc_id) {
577 *val = (state->crtc) ? state->crtc->base.id : 0;
578 } else if (property == config->prop_crtc_x) {
579 *val = I642U64(state->crtc_x);
580 } else if (property == config->prop_crtc_y) {
581 *val = I642U64(state->crtc_y);
582 } else if (property == config->prop_crtc_w) {
583 *val = state->crtc_w;
584 } else if (property == config->prop_crtc_h) {
585 *val = state->crtc_h;
586 } else if (property == config->prop_src_x) {
588 } else if (property == config->prop_src_y) {
590 } else if (property == config->prop_src_w) {
592 } else if (property == config->prop_src_h) {
594 } else if (property == config->rotation_property) {
595 *val = state->rotation;
596 } else if (plane->funcs->atomic_get_property) {
597 return plane->funcs->atomic_get_property(plane, state, property, val);
606 * drm_atomic_plane_check - check plane state
607 * @plane: plane to check
608 * @state: plane state to check
610 * Provides core sanity checks for plane state.
613 * Zero on success, error code on failure
615 static int drm_atomic_plane_check(struct drm_plane *plane,
616 struct drm_plane_state *state)
618 unsigned int fb_width, fb_height;
621 /* either *both* CRTC and FB must be set, or neither */
622 if (WARN_ON(state->crtc && !state->fb)) {
623 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
625 } else if (WARN_ON(state->fb && !state->crtc)) {
626 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
630 /* if disabled, we don't care about the rest of the state: */
634 /* Check whether this plane is usable on this CRTC */
635 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
636 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
640 /* Check whether this plane supports the fb pixel format. */
641 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
643 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
644 drm_get_format_name(state->fb->pixel_format));
648 /* Give drivers some help against integer overflows */
649 if (state->crtc_w > INT_MAX ||
650 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
651 state->crtc_h > INT_MAX ||
652 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
653 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
654 state->crtc_w, state->crtc_h,
655 state->crtc_x, state->crtc_y);
659 fb_width = state->fb->width << 16;
660 fb_height = state->fb->height << 16;
662 /* Make sure source coordinates are inside the fb. */
663 if (state->src_w > fb_width ||
664 state->src_x > fb_width - state->src_w ||
665 state->src_h > fb_height ||
666 state->src_y > fb_height - state->src_h) {
667 DRM_DEBUG_ATOMIC("Invalid source coordinates "
668 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
669 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
670 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
671 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
672 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
680 * drm_atomic_get_connector_state - get connector state
681 * @state: global atomic state object
682 * @connector: connector to get state object for
684 * This function returns the connector state for the given connector,
685 * allocating it if needed. It will also grab the relevant connector lock to
686 * make sure that the state is consistent.
690 * Either the allocated state or the error code encoded into the pointer. When
691 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
692 * entire atomic sequence must be restarted. All other errors are fatal.
694 struct drm_connector_state *
695 drm_atomic_get_connector_state(struct drm_atomic_state *state,
696 struct drm_connector *connector)
699 struct drm_mode_config *config = &connector->dev->mode_config;
700 struct drm_connector_state *connector_state;
702 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
706 index = drm_connector_index(connector);
709 * Construction of atomic state updates can race with a connector
710 * hot-add which might overflow. In this case flip the table and just
711 * restart the entire ioctl - no one is fast enough to livelock a cpu
712 * with physical hotplug events anyway.
714 * Note that we only grab the indexes once we have the right lock to
715 * prevent hotplug/unplugging of connectors. So removal is no problem,
716 * at most the array is a bit too large.
718 if (index >= state->num_connector) {
719 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
720 return ERR_PTR(-EAGAIN);
723 if (state->connector_states[index])
724 return state->connector_states[index];
726 connector_state = connector->funcs->atomic_duplicate_state(connector);
727 if (!connector_state)
728 return ERR_PTR(-ENOMEM);
730 state->connector_states[index] = connector_state;
731 state->connectors[index] = connector;
732 connector_state->state = state;
734 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
735 connector->base.id, connector_state, state);
737 if (connector_state->crtc) {
738 struct drm_crtc_state *crtc_state;
740 crtc_state = drm_atomic_get_crtc_state(state,
741 connector_state->crtc);
742 if (IS_ERR(crtc_state))
743 return ERR_CAST(crtc_state);
746 return connector_state;
748 EXPORT_SYMBOL(drm_atomic_get_connector_state);
751 * drm_atomic_connector_set_property - set property on connector.
752 * @connector: the drm connector to set a property on
753 * @state: the state object to update with the new property value
754 * @property: the property to set
755 * @val: the new property value
757 * Use this instead of calling connector->atomic_set_property directly.
758 * This function handles generic/core properties and calls out to
759 * driver's ->atomic_set_property() for driver properties. To ensure
760 * consistent behavior you must call this function rather than the
761 * driver hook directly.
764 * Zero on success, error code on failure
766 int drm_atomic_connector_set_property(struct drm_connector *connector,
767 struct drm_connector_state *state, struct drm_property *property,
770 struct drm_device *dev = connector->dev;
771 struct drm_mode_config *config = &dev->mode_config;
773 if (property == config->prop_crtc_id) {
774 struct drm_crtc *crtc = drm_crtc_find(dev, val);
775 return drm_atomic_set_crtc_for_connector(state, crtc);
776 } else if (property == config->dpms_property) {
777 /* setting DPMS property requires special handling, which
778 * is done in legacy setprop path for us. Disallow (for
779 * now?) atomic writes to DPMS property:
782 } else if (connector->funcs->atomic_set_property) {
783 return connector->funcs->atomic_set_property(connector,
784 state, property, val);
789 EXPORT_SYMBOL(drm_atomic_connector_set_property);
792 * This function handles generic/core properties and calls out to
793 * driver's ->atomic_get_property() for driver properties. To ensure
794 * consistent behavior you must call this function rather than the
795 * driver hook directly.
798 drm_atomic_connector_get_property(struct drm_connector *connector,
799 const struct drm_connector_state *state,
800 struct drm_property *property, uint64_t *val)
802 struct drm_device *dev = connector->dev;
803 struct drm_mode_config *config = &dev->mode_config;
805 if (property == config->prop_crtc_id) {
806 *val = (state->crtc) ? state->crtc->base.id : 0;
807 } else if (property == config->dpms_property) {
808 *val = connector->dpms;
809 } else if (connector->funcs->atomic_get_property) {
810 return connector->funcs->atomic_get_property(connector,
811 state, property, val);
819 int drm_atomic_get_property(struct drm_mode_object *obj,
820 struct drm_property *property, uint64_t *val)
822 struct drm_device *dev = property->dev;
826 case DRM_MODE_OBJECT_CONNECTOR: {
827 struct drm_connector *connector = obj_to_connector(obj);
828 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
829 ret = drm_atomic_connector_get_property(connector,
830 connector->state, property, val);
833 case DRM_MODE_OBJECT_CRTC: {
834 struct drm_crtc *crtc = obj_to_crtc(obj);
835 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
836 ret = drm_atomic_crtc_get_property(crtc,
837 crtc->state, property, val);
840 case DRM_MODE_OBJECT_PLANE: {
841 struct drm_plane *plane = obj_to_plane(obj);
842 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
843 ret = drm_atomic_plane_get_property(plane,
844 plane->state, property, val);
856 * drm_atomic_set_crtc_for_plane - set crtc for plane
857 * @plane_state: the plane whose incoming state to update
858 * @crtc: crtc to use for the plane
860 * Changing the assigned crtc for a plane requires us to grab the lock and state
861 * for the new crtc, as needed. This function takes care of all these details
862 * besides updating the pointer in the state object itself.
865 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
866 * then the w/w mutex code has detected a deadlock and the entire atomic
867 * sequence must be restarted. All other errors are fatal.
870 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
871 struct drm_crtc *crtc)
873 struct drm_plane *plane = plane_state->plane;
874 struct drm_crtc_state *crtc_state;
876 if (plane_state->crtc) {
877 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
879 if (WARN_ON(IS_ERR(crtc_state)))
880 return PTR_ERR(crtc_state);
882 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
885 plane_state->crtc = crtc;
888 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
890 if (IS_ERR(crtc_state))
891 return PTR_ERR(crtc_state);
892 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
896 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
897 plane_state, crtc->base.id);
899 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
904 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
907 * drm_atomic_set_fb_for_plane - set framebuffer for plane
908 * @plane_state: atomic state object for the plane
909 * @fb: fb to use for the plane
911 * Changing the assigned framebuffer for a plane requires us to grab a reference
912 * to the new fb and drop the reference to the old fb, if there is one. This
913 * function takes care of all these details besides updating the pointer in the
914 * state object itself.
917 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
918 struct drm_framebuffer *fb)
921 drm_framebuffer_unreference(plane_state->fb);
923 drm_framebuffer_reference(fb);
924 plane_state->fb = fb;
927 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
928 fb->base.id, plane_state);
930 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
933 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
936 * drm_atomic_set_crtc_for_connector - set crtc for connector
937 * @conn_state: atomic state object for the connector
938 * @crtc: crtc to use for the connector
940 * Changing the assigned crtc for a connector requires us to grab the lock and
941 * state for the new crtc, as needed. This function takes care of all these
942 * details besides updating the pointer in the state object itself.
945 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
946 * then the w/w mutex code has detected a deadlock and the entire atomic
947 * sequence must be restarted. All other errors are fatal.
950 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
951 struct drm_crtc *crtc)
953 struct drm_crtc_state *crtc_state;
956 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
957 if (IS_ERR(crtc_state))
958 return PTR_ERR(crtc_state);
961 conn_state->crtc = crtc;
964 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
965 conn_state, crtc->base.id);
967 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
972 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
975 * drm_atomic_add_affected_connectors - add connectors for crtc
976 * @state: atomic state
979 * This function walks the current configuration and adds all connectors
980 * currently using @crtc to the atomic configuration @state. Note that this
981 * function must acquire the connection mutex. This can potentially cause
982 * unneeded seralization if the update is just for the planes on one crtc. Hence
983 * drivers and helpers should only call this when really needed (e.g. when a
984 * full modeset needs to happen due to some change).
987 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
988 * then the w/w mutex code has detected a deadlock and the entire atomic
989 * sequence must be restarted. All other errors are fatal.
992 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
993 struct drm_crtc *crtc)
995 struct drm_mode_config *config = &state->dev->mode_config;
996 struct drm_connector *connector;
997 struct drm_connector_state *conn_state;
1000 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1004 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
1005 crtc->base.id, state);
1008 * Changed connectors are already in @state, so only need to look at the
1009 * current configuration.
1011 list_for_each_entry(connector, &config->connector_list, head) {
1012 if (connector->state->crtc != crtc)
1015 conn_state = drm_atomic_get_connector_state(state, connector);
1016 if (IS_ERR(conn_state))
1017 return PTR_ERR(conn_state);
1022 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1025 * drm_atomic_add_affected_planes - add planes for crtc
1026 * @state: atomic state
1029 * This function walks the current configuration and adds all planes
1030 * currently used by @crtc to the atomic configuration @state. This is useful
1031 * when an atomic commit also needs to check all currently enabled plane on
1032 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1033 * to avoid special code to force-enable all planes.
1035 * Since acquiring a plane state will always also acquire the w/w mutex of the
1036 * current CRTC for that plane (if there is any) adding all the plane states for
1037 * a CRTC will not reduce parallism of atomic updates.
1040 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1041 * then the w/w mutex code has detected a deadlock and the entire atomic
1042 * sequence must be restarted. All other errors are fatal.
1045 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1046 struct drm_crtc *crtc)
1048 struct drm_plane *plane;
1050 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1052 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1053 struct drm_plane_state *plane_state =
1054 drm_atomic_get_plane_state(state, plane);
1056 if (IS_ERR(plane_state))
1057 return PTR_ERR(plane_state);
1061 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1064 * drm_atomic_connectors_for_crtc - count number of connected outputs
1065 * @state: atomic state
1068 * This function counts all connectors which will be connected to @crtc
1069 * according to @state. Useful to recompute the enable state for @crtc.
1072 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
1073 struct drm_crtc *crtc)
1075 struct drm_connector *connector;
1076 struct drm_connector_state *conn_state;
1078 int i, num_connected_connectors = 0;
1080 for_each_connector_in_state(state, connector, conn_state, i) {
1081 if (conn_state->crtc == crtc)
1082 num_connected_connectors++;
1085 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
1086 state, num_connected_connectors, crtc->base.id);
1088 return num_connected_connectors;
1090 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
1093 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1094 * @state: atomic state
1096 * This function should be used by legacy entry points which don't understand
1097 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1098 * the slowpath completed.
1100 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1105 drm_modeset_backoff(state->acquire_ctx);
1107 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1108 state->acquire_ctx);
1111 ret = drm_modeset_lock_all_crtcs(state->dev,
1112 state->acquire_ctx);
1116 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1119 * drm_atomic_check_only - check whether a given config would work
1120 * @state: atomic configuration to check
1122 * Note that this function can return -EDEADLK if the driver needed to acquire
1123 * more locks but encountered a deadlock. The caller must then do the usual w/w
1124 * backoff dance and restart. All other errors are fatal.
1127 * 0 on success, negative error code on failure.
1129 int drm_atomic_check_only(struct drm_atomic_state *state)
1131 struct drm_device *dev = state->dev;
1132 struct drm_mode_config *config = &dev->mode_config;
1133 struct drm_plane *plane;
1134 struct drm_plane_state *plane_state;
1135 struct drm_crtc *crtc;
1136 struct drm_crtc_state *crtc_state;
1139 DRM_DEBUG_ATOMIC("checking %p\n", state);
1141 for_each_plane_in_state(state, plane, plane_state, i) {
1142 ret = drm_atomic_plane_check(plane, plane_state);
1144 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
1150 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1151 ret = drm_atomic_crtc_check(crtc, crtc_state);
1153 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1159 if (config->funcs->atomic_check)
1160 ret = config->funcs->atomic_check(state->dev, state);
1162 if (!state->allow_modeset) {
1163 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1164 if (crtc_state->mode_changed ||
1165 crtc_state->active_changed) {
1166 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1175 EXPORT_SYMBOL(drm_atomic_check_only);
1178 * drm_atomic_commit - commit configuration atomically
1179 * @state: atomic configuration to check
1181 * Note that this function can return -EDEADLK if the driver needed to acquire
1182 * more locks but encountered a deadlock. The caller must then do the usual w/w
1183 * backoff dance and restart. All other errors are fatal.
1185 * Also note that on successful execution ownership of @state is transferred
1186 * from the caller of this function to the function itself. The caller must not
1187 * free or in any other way access @state. If the function fails then the caller
1188 * must clean up @state itself.
1191 * 0 on success, negative error code on failure.
1193 int drm_atomic_commit(struct drm_atomic_state *state)
1195 struct drm_mode_config *config = &state->dev->mode_config;
1198 ret = drm_atomic_check_only(state);
1202 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1204 return config->funcs->atomic_commit(state->dev, state, false);
1206 EXPORT_SYMBOL(drm_atomic_commit);
1209 * drm_atomic_async_commit - atomic&async configuration commit
1210 * @state: atomic configuration to check
1212 * Note that this function can return -EDEADLK if the driver needed to acquire
1213 * more locks but encountered a deadlock. The caller must then do the usual w/w
1214 * backoff dance and restart. All other errors are fatal.
1216 * Also note that on successful execution ownership of @state is transferred
1217 * from the caller of this function to the function itself. The caller must not
1218 * free or in any other way access @state. If the function fails then the caller
1219 * must clean up @state itself.
1222 * 0 on success, negative error code on failure.
1224 int drm_atomic_async_commit(struct drm_atomic_state *state)
1226 struct drm_mode_config *config = &state->dev->mode_config;
1229 ret = drm_atomic_check_only(state);
1233 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1235 return config->funcs->atomic_commit(state->dev, state, true);
1237 EXPORT_SYMBOL(drm_atomic_async_commit);
1240 * The big monstor ioctl
1243 static struct drm_pending_vblank_event *create_vblank_event(
1244 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1246 struct drm_pending_vblank_event *e = NULL;
1247 unsigned long flags;
1249 spin_lock_irqsave(&dev->event_lock, flags);
1250 if (file_priv->event_space < sizeof e->event) {
1251 spin_unlock_irqrestore(&dev->event_lock, flags);
1254 file_priv->event_space -= sizeof e->event;
1255 spin_unlock_irqrestore(&dev->event_lock, flags);
1257 e = kzalloc(sizeof *e, GFP_KERNEL);
1259 spin_lock_irqsave(&dev->event_lock, flags);
1260 file_priv->event_space += sizeof e->event;
1261 spin_unlock_irqrestore(&dev->event_lock, flags);
1265 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1266 e->event.base.length = sizeof e->event;
1267 e->event.user_data = user_data;
1268 e->base.event = &e->event.base;
1269 e->base.file_priv = file_priv;
1270 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1276 static void destroy_vblank_event(struct drm_device *dev,
1277 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1279 unsigned long flags;
1281 spin_lock_irqsave(&dev->event_lock, flags);
1282 file_priv->event_space += sizeof e->event;
1283 spin_unlock_irqrestore(&dev->event_lock, flags);
1287 static int atomic_set_prop(struct drm_atomic_state *state,
1288 struct drm_mode_object *obj, struct drm_property *prop,
1289 uint64_t prop_value)
1291 struct drm_mode_object *ref;
1294 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1297 switch (obj->type) {
1298 case DRM_MODE_OBJECT_CONNECTOR: {
1299 struct drm_connector *connector = obj_to_connector(obj);
1300 struct drm_connector_state *connector_state;
1302 connector_state = drm_atomic_get_connector_state(state, connector);
1303 if (IS_ERR(connector_state)) {
1304 ret = PTR_ERR(connector_state);
1308 ret = drm_atomic_connector_set_property(connector,
1309 connector_state, prop, prop_value);
1312 case DRM_MODE_OBJECT_CRTC: {
1313 struct drm_crtc *crtc = obj_to_crtc(obj);
1314 struct drm_crtc_state *crtc_state;
1316 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1317 if (IS_ERR(crtc_state)) {
1318 ret = PTR_ERR(crtc_state);
1322 ret = drm_atomic_crtc_set_property(crtc,
1323 crtc_state, prop, prop_value);
1326 case DRM_MODE_OBJECT_PLANE: {
1327 struct drm_plane *plane = obj_to_plane(obj);
1328 struct drm_plane_state *plane_state;
1330 plane_state = drm_atomic_get_plane_state(state, plane);
1331 if (IS_ERR(plane_state)) {
1332 ret = PTR_ERR(plane_state);
1336 ret = drm_atomic_plane_set_property(plane,
1337 plane_state, prop, prop_value);
1345 drm_property_change_valid_put(prop, ref);
1349 int drm_mode_atomic_ioctl(struct drm_device *dev,
1350 void *data, struct drm_file *file_priv)
1352 struct drm_mode_atomic *arg = data;
1353 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1354 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1355 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1356 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1357 unsigned int copied_objs, copied_props;
1358 struct drm_atomic_state *state;
1359 struct drm_modeset_acquire_ctx ctx;
1360 struct drm_plane *plane;
1361 struct drm_crtc *crtc;
1362 struct drm_crtc_state *crtc_state;
1363 unsigned plane_mask = 0;
1367 /* disallow for drivers not supporting atomic: */
1368 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1371 /* disallow for userspace that has not enabled atomic cap (even
1372 * though this may be a bit overkill, since legacy userspace
1373 * wouldn't know how to call this ioctl)
1375 if (!file_priv->atomic)
1378 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1384 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1385 !dev->mode_config.async_page_flip)
1388 /* can't test and expect an event at the same time. */
1389 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1390 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1393 drm_modeset_acquire_init(&ctx, 0);
1395 state = drm_atomic_state_alloc(dev);
1399 state->acquire_ctx = &ctx;
1400 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1406 for (i = 0; i < arg->count_objs; i++) {
1407 uint32_t obj_id, count_props;
1408 struct drm_mode_object *obj;
1410 if (get_user(obj_id, objs_ptr + copied_objs)) {
1415 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1416 if (!obj || !obj->properties) {
1421 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1422 plane = obj_to_plane(obj);
1423 plane_mask |= (1 << drm_plane_index(plane));
1424 plane->old_fb = plane->fb;
1427 if (get_user(count_props, count_props_ptr + copied_objs)) {
1434 for (j = 0; j < count_props; j++) {
1436 uint64_t prop_value;
1437 struct drm_property *prop;
1439 if (get_user(prop_id, props_ptr + copied_props)) {
1444 prop = drm_property_find(dev, prop_id);
1450 if (copy_from_user(&prop_value,
1451 prop_values_ptr + copied_props,
1452 sizeof(prop_value))) {
1457 ret = atomic_set_prop(state, obj, prop, prop_value);
1465 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1466 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1467 struct drm_pending_vblank_event *e;
1469 e = create_vblank_event(dev, file_priv, arg->user_data);
1475 crtc_state->event = e;
1479 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1480 ret = drm_atomic_check_only(state);
1481 /* _check_only() does not free state, unlike _commit() */
1482 drm_atomic_state_free(state);
1483 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1484 ret = drm_atomic_async_commit(state);
1486 ret = drm_atomic_commit(state);
1489 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1490 * locks (ie. while it is still safe to deref plane->state). We
1491 * need to do this here because the driver entry points cannot
1492 * distinguish between legacy and atomic ioctls.
1494 drm_for_each_plane_mask(plane, dev, plane_mask) {
1496 struct drm_framebuffer *new_fb = plane->state->fb;
1498 drm_framebuffer_reference(new_fb);
1500 plane->crtc = plane->state->crtc;
1502 plane->old_fb = NULL;
1504 if (plane->old_fb) {
1505 drm_framebuffer_unreference(plane->old_fb);
1506 plane->old_fb = NULL;
1510 drm_modeset_drop_locks(&ctx);
1511 drm_modeset_acquire_fini(&ctx);
1516 if (ret == -EDEADLK)
1519 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1520 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1521 destroy_vblank_event(dev, file_priv, crtc_state->event);
1522 crtc_state->event = NULL;
1526 drm_atomic_state_free(state);
1528 drm_modeset_drop_locks(&ctx);
1529 drm_modeset_acquire_fini(&ctx);
1534 drm_atomic_state_clear(state);
1535 drm_modeset_backoff(&ctx);