Linux 4.1.16
[linux/fpc-iii.git] / drivers / gpu / drm / drm_atomic.c
blob6e3b78ee7d16b824e67421446b407d064fd77be8
1 /*
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.
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
29 #include <drm/drmP.h>
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);
37 kfree(state->crtcs);
38 kfree(state->crtc_states);
39 kfree(state->planes);
40 kfree(state->plane_states);
41 kfree(state);
44 /**
45 * drm_atomic_state_alloc - allocate atomic state
46 * @dev: DRM device
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);
56 if (!state)
57 return NULL;
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);
68 if (!state->crtcs)
69 goto fail;
70 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
71 sizeof(*state->crtc_states), GFP_KERNEL);
72 if (!state->crtc_states)
73 goto fail;
74 state->planes = kcalloc(dev->mode_config.num_total_plane,
75 sizeof(*state->planes), GFP_KERNEL);
76 if (!state->planes)
77 goto fail;
78 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
79 sizeof(*state->plane_states), GFP_KERNEL);
80 if (!state->plane_states)
81 goto fail;
82 state->connectors = kcalloc(state->num_connector,
83 sizeof(*state->connectors),
84 GFP_KERNEL);
85 if (!state->connectors)
86 goto fail;
87 state->connector_states = kcalloc(state->num_connector,
88 sizeof(*state->connector_states),
89 GFP_KERNEL);
90 if (!state->connector_states)
91 goto fail;
93 state->dev = dev;
95 DRM_DEBUG_ATOMIC("Allocate atomic state %p\n", state);
97 return state;
98 fail:
99 kfree_state(state);
101 return NULL;
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
114 * relies on.
116 * Hence we must clear all cached state and completely start over, using this
117 * function.
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;
123 int i;
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];
130 if (!connector)
131 continue;
133 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
135 connector->funcs->atomic_destroy_state(connector,
136 state->connector_states[i]);
137 state->connectors[i] = NULL;
138 state->connector_states[i] = NULL;
141 for (i = 0; i < config->num_crtc; i++) {
142 struct drm_crtc *crtc = state->crtcs[i];
144 if (!crtc)
145 continue;
147 crtc->funcs->atomic_destroy_state(crtc,
148 state->crtc_states[i]);
149 state->crtcs[i] = NULL;
150 state->crtc_states[i] = NULL;
153 for (i = 0; i < config->num_total_plane; i++) {
154 struct drm_plane *plane = state->planes[i];
156 if (!plane)
157 continue;
159 plane->funcs->atomic_destroy_state(plane,
160 state->plane_states[i]);
161 state->planes[i] = NULL;
162 state->plane_states[i] = NULL;
165 EXPORT_SYMBOL(drm_atomic_state_clear);
168 * drm_atomic_state_free - free all memory for an atomic state
169 * @state: atomic state to deallocate
171 * This frees all memory associated with an atomic state, including all the
172 * per-object state for planes, crtcs and connectors.
174 void drm_atomic_state_free(struct drm_atomic_state *state)
176 if (!state)
177 return;
179 drm_atomic_state_clear(state);
181 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
183 kfree_state(state);
185 EXPORT_SYMBOL(drm_atomic_state_free);
188 * drm_atomic_get_crtc_state - get crtc state
189 * @state: global atomic state object
190 * @crtc: crtc to get state object for
192 * This function returns the crtc state for the given crtc, allocating it if
193 * needed. It will also grab the relevant crtc lock to make sure that the state
194 * is consistent.
196 * Returns:
198 * Either the allocated state or the error code encoded into the pointer. When
199 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
200 * entire atomic sequence must be restarted. All other errors are fatal.
202 struct drm_crtc_state *
203 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
204 struct drm_crtc *crtc)
206 int ret, index;
207 struct drm_crtc_state *crtc_state;
209 index = drm_crtc_index(crtc);
211 if (state->crtc_states[index])
212 return state->crtc_states[index];
214 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
215 if (ret)
216 return ERR_PTR(ret);
218 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
219 if (!crtc_state)
220 return ERR_PTR(-ENOMEM);
222 state->crtc_states[index] = crtc_state;
223 state->crtcs[index] = crtc;
224 crtc_state->state = state;
226 DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
227 crtc->base.id, crtc_state, state);
229 return crtc_state;
231 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
234 * drm_atomic_crtc_set_property - set property on CRTC
235 * @crtc: the drm CRTC to set a property on
236 * @state: the state object to update with the new property value
237 * @property: the property to set
238 * @val: the new property value
240 * Use this instead of calling crtc->atomic_set_property directly.
241 * This function handles generic/core properties and calls out to
242 * driver's ->atomic_set_property() for driver properties. To ensure
243 * consistent behavior you must call this function rather than the
244 * driver hook directly.
246 * RETURNS:
247 * Zero on success, error code on failure
249 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
250 struct drm_crtc_state *state, struct drm_property *property,
251 uint64_t val)
253 struct drm_device *dev = crtc->dev;
254 struct drm_mode_config *config = &dev->mode_config;
256 /* FIXME: Mode prop is missing, which also controls ->enable. */
257 if (property == config->prop_active)
258 state->active = val;
259 else if (crtc->funcs->atomic_set_property)
260 return crtc->funcs->atomic_set_property(crtc, state, property, val);
261 else
262 return -EINVAL;
264 return 0;
266 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
269 * This function handles generic/core properties and calls out to
270 * driver's ->atomic_get_property() for driver properties. To ensure
271 * consistent behavior you must call this function rather than the
272 * driver hook directly.
274 int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
275 const struct drm_crtc_state *state,
276 struct drm_property *property, uint64_t *val)
278 struct drm_device *dev = crtc->dev;
279 struct drm_mode_config *config = &dev->mode_config;
281 if (property == config->prop_active)
282 *val = state->active;
283 else if (crtc->funcs->atomic_get_property)
284 return crtc->funcs->atomic_get_property(crtc, state, property, val);
285 else
286 return -EINVAL;
288 return 0;
292 * drm_atomic_crtc_check - check crtc state
293 * @crtc: crtc to check
294 * @state: crtc state to check
296 * Provides core sanity checks for crtc state.
298 * RETURNS:
299 * Zero on success, error code on failure
301 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
302 struct drm_crtc_state *state)
304 /* NOTE: we explicitly don't enforce constraints such as primary
305 * layer covering entire screen, since that is something we want
306 * to allow (on hw that supports it). For hw that does not, it
307 * should be checked in driver's crtc->atomic_check() vfunc.
309 * TODO: Add generic modeset state checks once we support those.
312 if (state->active && !state->enable) {
313 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
314 crtc->base.id);
315 return -EINVAL;
318 return 0;
322 * drm_atomic_get_plane_state - get plane state
323 * @state: global atomic state object
324 * @plane: plane to get state object for
326 * This function returns the plane state for the given plane, allocating it if
327 * needed. It will also grab the relevant plane lock to make sure that the state
328 * is consistent.
330 * Returns:
332 * Either the allocated state or the error code encoded into the pointer. When
333 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
334 * entire atomic sequence must be restarted. All other errors are fatal.
336 struct drm_plane_state *
337 drm_atomic_get_plane_state(struct drm_atomic_state *state,
338 struct drm_plane *plane)
340 int ret, index;
341 struct drm_plane_state *plane_state;
343 index = drm_plane_index(plane);
345 if (state->plane_states[index])
346 return state->plane_states[index];
348 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
349 if (ret)
350 return ERR_PTR(ret);
352 plane_state = plane->funcs->atomic_duplicate_state(plane);
353 if (!plane_state)
354 return ERR_PTR(-ENOMEM);
356 state->plane_states[index] = plane_state;
357 state->planes[index] = plane;
358 plane_state->state = state;
360 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
361 plane->base.id, plane_state, state);
363 if (plane_state->crtc) {
364 struct drm_crtc_state *crtc_state;
366 crtc_state = drm_atomic_get_crtc_state(state,
367 plane_state->crtc);
368 if (IS_ERR(crtc_state))
369 return ERR_CAST(crtc_state);
372 return plane_state;
374 EXPORT_SYMBOL(drm_atomic_get_plane_state);
377 * drm_atomic_plane_set_property - set property on plane
378 * @plane: the drm plane to set a property on
379 * @state: the state object to update with the new property value
380 * @property: the property to set
381 * @val: the new property value
383 * Use this instead of calling plane->atomic_set_property directly.
384 * This function handles generic/core properties and calls out to
385 * driver's ->atomic_set_property() for driver properties. To ensure
386 * consistent behavior you must call this function rather than the
387 * driver hook directly.
389 * RETURNS:
390 * Zero on success, error code on failure
392 int drm_atomic_plane_set_property(struct drm_plane *plane,
393 struct drm_plane_state *state, struct drm_property *property,
394 uint64_t val)
396 struct drm_device *dev = plane->dev;
397 struct drm_mode_config *config = &dev->mode_config;
399 if (property == config->prop_fb_id) {
400 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
401 drm_atomic_set_fb_for_plane(state, fb);
402 if (fb)
403 drm_framebuffer_unreference(fb);
404 } else if (property == config->prop_crtc_id) {
405 struct drm_crtc *crtc = drm_crtc_find(dev, val);
406 return drm_atomic_set_crtc_for_plane(state, crtc);
407 } else if (property == config->prop_crtc_x) {
408 state->crtc_x = U642I64(val);
409 } else if (property == config->prop_crtc_y) {
410 state->crtc_y = U642I64(val);
411 } else if (property == config->prop_crtc_w) {
412 state->crtc_w = val;
413 } else if (property == config->prop_crtc_h) {
414 state->crtc_h = val;
415 } else if (property == config->prop_src_x) {
416 state->src_x = val;
417 } else if (property == config->prop_src_y) {
418 state->src_y = val;
419 } else if (property == config->prop_src_w) {
420 state->src_w = val;
421 } else if (property == config->prop_src_h) {
422 state->src_h = val;
423 } else if (property == config->rotation_property) {
424 state->rotation = val;
425 } else if (plane->funcs->atomic_set_property) {
426 return plane->funcs->atomic_set_property(plane, state,
427 property, val);
428 } else {
429 return -EINVAL;
432 return 0;
434 EXPORT_SYMBOL(drm_atomic_plane_set_property);
437 * This function handles generic/core properties and calls out to
438 * driver's ->atomic_get_property() for driver properties. To ensure
439 * consistent behavior you must call this function rather than the
440 * driver hook directly.
442 static int
443 drm_atomic_plane_get_property(struct drm_plane *plane,
444 const struct drm_plane_state *state,
445 struct drm_property *property, uint64_t *val)
447 struct drm_device *dev = plane->dev;
448 struct drm_mode_config *config = &dev->mode_config;
450 if (property == config->prop_fb_id) {
451 *val = (state->fb) ? state->fb->base.id : 0;
452 } else if (property == config->prop_crtc_id) {
453 *val = (state->crtc) ? state->crtc->base.id : 0;
454 } else if (property == config->prop_crtc_x) {
455 *val = I642U64(state->crtc_x);
456 } else if (property == config->prop_crtc_y) {
457 *val = I642U64(state->crtc_y);
458 } else if (property == config->prop_crtc_w) {
459 *val = state->crtc_w;
460 } else if (property == config->prop_crtc_h) {
461 *val = state->crtc_h;
462 } else if (property == config->prop_src_x) {
463 *val = state->src_x;
464 } else if (property == config->prop_src_y) {
465 *val = state->src_y;
466 } else if (property == config->prop_src_w) {
467 *val = state->src_w;
468 } else if (property == config->prop_src_h) {
469 *val = state->src_h;
470 } else if (property == config->rotation_property) {
471 *val = state->rotation;
472 } else if (plane->funcs->atomic_get_property) {
473 return plane->funcs->atomic_get_property(plane, state, property, val);
474 } else {
475 return -EINVAL;
478 return 0;
482 * drm_atomic_plane_check - check plane state
483 * @plane: plane to check
484 * @state: plane state to check
486 * Provides core sanity checks for plane state.
488 * RETURNS:
489 * Zero on success, error code on failure
491 static int drm_atomic_plane_check(struct drm_plane *plane,
492 struct drm_plane_state *state)
494 unsigned int fb_width, fb_height;
495 int ret;
497 /* either *both* CRTC and FB must be set, or neither */
498 if (WARN_ON(state->crtc && !state->fb)) {
499 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
500 return -EINVAL;
501 } else if (WARN_ON(state->fb && !state->crtc)) {
502 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
503 return -EINVAL;
506 /* if disabled, we don't care about the rest of the state: */
507 if (!state->crtc)
508 return 0;
510 /* Check whether this plane is usable on this CRTC */
511 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
512 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
513 return -EINVAL;
516 /* Check whether this plane supports the fb pixel format. */
517 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
518 if (ret) {
519 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
520 drm_get_format_name(state->fb->pixel_format));
521 return ret;
524 /* Give drivers some help against integer overflows */
525 if (state->crtc_w > INT_MAX ||
526 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
527 state->crtc_h > INT_MAX ||
528 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
529 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
530 state->crtc_w, state->crtc_h,
531 state->crtc_x, state->crtc_y);
532 return -ERANGE;
535 fb_width = state->fb->width << 16;
536 fb_height = state->fb->height << 16;
538 /* Make sure source coordinates are inside the fb. */
539 if (state->src_w > fb_width ||
540 state->src_x > fb_width - state->src_w ||
541 state->src_h > fb_height ||
542 state->src_y > fb_height - state->src_h) {
543 DRM_DEBUG_ATOMIC("Invalid source coordinates "
544 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
545 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
546 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
547 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
548 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
549 return -ENOSPC;
552 return 0;
556 * drm_atomic_get_connector_state - get connector state
557 * @state: global atomic state object
558 * @connector: connector to get state object for
560 * This function returns the connector state for the given connector,
561 * allocating it if needed. It will also grab the relevant connector lock to
562 * make sure that the state is consistent.
564 * Returns:
566 * Either the allocated state or the error code encoded into the pointer. When
567 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
568 * entire atomic sequence must be restarted. All other errors are fatal.
570 struct drm_connector_state *
571 drm_atomic_get_connector_state(struct drm_atomic_state *state,
572 struct drm_connector *connector)
574 int ret, index;
575 struct drm_mode_config *config = &connector->dev->mode_config;
576 struct drm_connector_state *connector_state;
578 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
579 if (ret)
580 return ERR_PTR(ret);
582 index = drm_connector_index(connector);
585 * Construction of atomic state updates can race with a connector
586 * hot-add which might overflow. In this case flip the table and just
587 * restart the entire ioctl - no one is fast enough to livelock a cpu
588 * with physical hotplug events anyway.
590 * Note that we only grab the indexes once we have the right lock to
591 * prevent hotplug/unplugging of connectors. So removal is no problem,
592 * at most the array is a bit too large.
594 if (index >= state->num_connector) {
595 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
596 return ERR_PTR(-EAGAIN);
599 if (state->connector_states[index])
600 return state->connector_states[index];
602 connector_state = connector->funcs->atomic_duplicate_state(connector);
603 if (!connector_state)
604 return ERR_PTR(-ENOMEM);
606 state->connector_states[index] = connector_state;
607 state->connectors[index] = connector;
608 connector_state->state = state;
610 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
611 connector->base.id, connector_state, state);
613 if (connector_state->crtc) {
614 struct drm_crtc_state *crtc_state;
616 crtc_state = drm_atomic_get_crtc_state(state,
617 connector_state->crtc);
618 if (IS_ERR(crtc_state))
619 return ERR_CAST(crtc_state);
622 return connector_state;
624 EXPORT_SYMBOL(drm_atomic_get_connector_state);
627 * drm_atomic_connector_set_property - set property on connector.
628 * @connector: the drm connector to set a property on
629 * @state: the state object to update with the new property value
630 * @property: the property to set
631 * @val: the new property value
633 * Use this instead of calling connector->atomic_set_property directly.
634 * This function handles generic/core properties and calls out to
635 * driver's ->atomic_set_property() for driver properties. To ensure
636 * consistent behavior you must call this function rather than the
637 * driver hook directly.
639 * RETURNS:
640 * Zero on success, error code on failure
642 int drm_atomic_connector_set_property(struct drm_connector *connector,
643 struct drm_connector_state *state, struct drm_property *property,
644 uint64_t val)
646 struct drm_device *dev = connector->dev;
647 struct drm_mode_config *config = &dev->mode_config;
649 if (property == config->prop_crtc_id) {
650 struct drm_crtc *crtc = drm_crtc_find(dev, val);
651 return drm_atomic_set_crtc_for_connector(state, crtc);
652 } else if (property == config->dpms_property) {
653 /* setting DPMS property requires special handling, which
654 * is done in legacy setprop path for us. Disallow (for
655 * now?) atomic writes to DPMS property:
657 return -EINVAL;
658 } else if (connector->funcs->atomic_set_property) {
659 return connector->funcs->atomic_set_property(connector,
660 state, property, val);
661 } else {
662 return -EINVAL;
665 EXPORT_SYMBOL(drm_atomic_connector_set_property);
668 * This function handles generic/core properties and calls out to
669 * driver's ->atomic_get_property() for driver properties. To ensure
670 * consistent behavior you must call this function rather than the
671 * driver hook directly.
673 static int
674 drm_atomic_connector_get_property(struct drm_connector *connector,
675 const struct drm_connector_state *state,
676 struct drm_property *property, uint64_t *val)
678 struct drm_device *dev = connector->dev;
679 struct drm_mode_config *config = &dev->mode_config;
681 if (property == config->prop_crtc_id) {
682 *val = (state->crtc) ? state->crtc->base.id : 0;
683 } else if (property == config->dpms_property) {
684 *val = connector->dpms;
685 } else if (connector->funcs->atomic_get_property) {
686 return connector->funcs->atomic_get_property(connector,
687 state, property, val);
688 } else {
689 return -EINVAL;
692 return 0;
695 int drm_atomic_get_property(struct drm_mode_object *obj,
696 struct drm_property *property, uint64_t *val)
698 struct drm_device *dev = property->dev;
699 int ret;
701 switch (obj->type) {
702 case DRM_MODE_OBJECT_CONNECTOR: {
703 struct drm_connector *connector = obj_to_connector(obj);
704 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
705 ret = drm_atomic_connector_get_property(connector,
706 connector->state, property, val);
707 break;
709 case DRM_MODE_OBJECT_CRTC: {
710 struct drm_crtc *crtc = obj_to_crtc(obj);
711 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
712 ret = drm_atomic_crtc_get_property(crtc,
713 crtc->state, property, val);
714 break;
716 case DRM_MODE_OBJECT_PLANE: {
717 struct drm_plane *plane = obj_to_plane(obj);
718 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
719 ret = drm_atomic_plane_get_property(plane,
720 plane->state, property, val);
721 break;
723 default:
724 ret = -EINVAL;
725 break;
728 return ret;
732 * drm_atomic_set_crtc_for_plane - set crtc for plane
733 * @plane_state: the plane whose incoming state to update
734 * @crtc: crtc to use for the plane
736 * Changing the assigned crtc for a plane requires us to grab the lock and state
737 * for the new crtc, as needed. This function takes care of all these details
738 * besides updating the pointer in the state object itself.
740 * Returns:
741 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
742 * then the w/w mutex code has detected a deadlock and the entire atomic
743 * sequence must be restarted. All other errors are fatal.
746 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
747 struct drm_crtc *crtc)
749 struct drm_plane *plane = plane_state->plane;
750 struct drm_crtc_state *crtc_state;
752 if (plane_state->crtc) {
753 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
754 plane_state->crtc);
755 if (WARN_ON(IS_ERR(crtc_state)))
756 return PTR_ERR(crtc_state);
758 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
761 plane_state->crtc = crtc;
763 if (crtc) {
764 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
765 crtc);
766 if (IS_ERR(crtc_state))
767 return PTR_ERR(crtc_state);
768 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
771 if (crtc)
772 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
773 plane_state, crtc->base.id);
774 else
775 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
776 plane_state);
778 return 0;
780 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
783 * drm_atomic_set_fb_for_plane - set framebuffer for plane
784 * @plane_state: atomic state object for the plane
785 * @fb: fb to use for the plane
787 * Changing the assigned framebuffer for a plane requires us to grab a reference
788 * to the new fb and drop the reference to the old fb, if there is one. This
789 * function takes care of all these details besides updating the pointer in the
790 * state object itself.
792 void
793 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
794 struct drm_framebuffer *fb)
796 if (plane_state->fb)
797 drm_framebuffer_unreference(plane_state->fb);
798 if (fb)
799 drm_framebuffer_reference(fb);
800 plane_state->fb = fb;
802 if (fb)
803 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
804 fb->base.id, plane_state);
805 else
806 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
807 plane_state);
809 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
812 * drm_atomic_set_crtc_for_connector - set crtc for connector
813 * @conn_state: atomic state object for the connector
814 * @crtc: crtc to use for the connector
816 * Changing the assigned crtc for a connector requires us to grab the lock and
817 * state for the new crtc, as needed. This function takes care of all these
818 * details besides updating the pointer in the state object itself.
820 * Returns:
821 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
822 * then the w/w mutex code has detected a deadlock and the entire atomic
823 * sequence must be restarted. All other errors are fatal.
826 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
827 struct drm_crtc *crtc)
829 struct drm_crtc_state *crtc_state;
831 if (crtc) {
832 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
833 if (IS_ERR(crtc_state))
834 return PTR_ERR(crtc_state);
837 conn_state->crtc = crtc;
839 if (crtc)
840 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
841 conn_state, crtc->base.id);
842 else
843 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
844 conn_state);
846 return 0;
848 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
851 * drm_atomic_add_affected_connectors - add connectors for crtc
852 * @state: atomic state
853 * @crtc: DRM crtc
855 * This function walks the current configuration and adds all connectors
856 * currently using @crtc to the atomic configuration @state. Note that this
857 * function must acquire the connection mutex. This can potentially cause
858 * unneeded seralization if the update is just for the planes on one crtc. Hence
859 * drivers and helpers should only call this when really needed (e.g. when a
860 * full modeset needs to happen due to some change).
862 * Returns:
863 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
864 * then the w/w mutex code has detected a deadlock and the entire atomic
865 * sequence must be restarted. All other errors are fatal.
868 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
869 struct drm_crtc *crtc)
871 struct drm_mode_config *config = &state->dev->mode_config;
872 struct drm_connector *connector;
873 struct drm_connector_state *conn_state;
874 int ret;
876 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
877 if (ret)
878 return ret;
880 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
881 crtc->base.id, state);
884 * Changed connectors are already in @state, so only need to look at the
885 * current configuration.
887 list_for_each_entry(connector, &config->connector_list, head) {
888 if (connector->state->crtc != crtc)
889 continue;
891 conn_state = drm_atomic_get_connector_state(state, connector);
892 if (IS_ERR(conn_state))
893 return PTR_ERR(conn_state);
896 return 0;
898 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
901 * drm_atomic_connectors_for_crtc - count number of connected outputs
902 * @state: atomic state
903 * @crtc: DRM crtc
905 * This function counts all connectors which will be connected to @crtc
906 * according to @state. Useful to recompute the enable state for @crtc.
909 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
910 struct drm_crtc *crtc)
912 struct drm_connector *connector;
913 struct drm_connector_state *conn_state;
915 int i, num_connected_connectors = 0;
917 for_each_connector_in_state(state, connector, conn_state, i) {
918 if (conn_state->crtc == crtc)
919 num_connected_connectors++;
922 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
923 state, num_connected_connectors, crtc->base.id);
925 return num_connected_connectors;
927 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
930 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
931 * @state: atomic state
933 * This function should be used by legacy entry points which don't understand
934 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
935 * the slowpath completed.
937 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
939 int ret;
941 retry:
942 drm_modeset_backoff(state->acquire_ctx);
944 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
945 state->acquire_ctx);
946 if (ret)
947 goto retry;
948 ret = drm_modeset_lock_all_crtcs(state->dev,
949 state->acquire_ctx);
950 if (ret)
951 goto retry;
953 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
956 * drm_atomic_check_only - check whether a given config would work
957 * @state: atomic configuration to check
959 * Note that this function can return -EDEADLK if the driver needed to acquire
960 * more locks but encountered a deadlock. The caller must then do the usual w/w
961 * backoff dance and restart. All other errors are fatal.
963 * Returns:
964 * 0 on success, negative error code on failure.
966 int drm_atomic_check_only(struct drm_atomic_state *state)
968 struct drm_device *dev = state->dev;
969 struct drm_mode_config *config = &dev->mode_config;
970 struct drm_plane *plane;
971 struct drm_plane_state *plane_state;
972 struct drm_crtc *crtc;
973 struct drm_crtc_state *crtc_state;
974 int i, ret = 0;
976 DRM_DEBUG_ATOMIC("checking %p\n", state);
978 for_each_plane_in_state(state, plane, plane_state, i) {
979 ret = drm_atomic_plane_check(plane, plane_state);
980 if (ret) {
981 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
982 plane->base.id);
983 return ret;
987 for_each_crtc_in_state(state, crtc, crtc_state, i) {
988 ret = drm_atomic_crtc_check(crtc, crtc_state);
989 if (ret) {
990 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
991 crtc->base.id);
992 return ret;
996 if (config->funcs->atomic_check)
997 ret = config->funcs->atomic_check(state->dev, state);
999 if (!state->allow_modeset) {
1000 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1001 if (crtc_state->mode_changed ||
1002 crtc_state->active_changed) {
1003 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1004 crtc->base.id);
1005 return -EINVAL;
1010 return ret;
1012 EXPORT_SYMBOL(drm_atomic_check_only);
1015 * drm_atomic_commit - commit configuration atomically
1016 * @state: atomic configuration to check
1018 * Note that this function can return -EDEADLK if the driver needed to acquire
1019 * more locks but encountered a deadlock. The caller must then do the usual w/w
1020 * backoff dance and restart. All other errors are fatal.
1022 * Also note that on successful execution ownership of @state is transferred
1023 * from the caller of this function to the function itself. The caller must not
1024 * free or in any other way access @state. If the function fails then the caller
1025 * must clean up @state itself.
1027 * Returns:
1028 * 0 on success, negative error code on failure.
1030 int drm_atomic_commit(struct drm_atomic_state *state)
1032 struct drm_mode_config *config = &state->dev->mode_config;
1033 int ret;
1035 ret = drm_atomic_check_only(state);
1036 if (ret)
1037 return ret;
1039 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1041 return config->funcs->atomic_commit(state->dev, state, false);
1043 EXPORT_SYMBOL(drm_atomic_commit);
1046 * drm_atomic_async_commit - atomic&async configuration commit
1047 * @state: atomic configuration to check
1049 * Note that this function can return -EDEADLK if the driver needed to acquire
1050 * more locks but encountered a deadlock. The caller must then do the usual w/w
1051 * backoff dance and restart. All other errors are fatal.
1053 * Also note that on successful execution ownership of @state is transferred
1054 * from the caller of this function to the function itself. The caller must not
1055 * free or in any other way access @state. If the function fails then the caller
1056 * must clean up @state itself.
1058 * Returns:
1059 * 0 on success, negative error code on failure.
1061 int drm_atomic_async_commit(struct drm_atomic_state *state)
1063 struct drm_mode_config *config = &state->dev->mode_config;
1064 int ret;
1066 ret = drm_atomic_check_only(state);
1067 if (ret)
1068 return ret;
1070 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1072 return config->funcs->atomic_commit(state->dev, state, true);
1074 EXPORT_SYMBOL(drm_atomic_async_commit);
1077 * The big monstor ioctl
1080 static struct drm_pending_vblank_event *create_vblank_event(
1081 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1083 struct drm_pending_vblank_event *e = NULL;
1084 unsigned long flags;
1086 spin_lock_irqsave(&dev->event_lock, flags);
1087 if (file_priv->event_space < sizeof e->event) {
1088 spin_unlock_irqrestore(&dev->event_lock, flags);
1089 goto out;
1091 file_priv->event_space -= sizeof e->event;
1092 spin_unlock_irqrestore(&dev->event_lock, flags);
1094 e = kzalloc(sizeof *e, GFP_KERNEL);
1095 if (e == NULL) {
1096 spin_lock_irqsave(&dev->event_lock, flags);
1097 file_priv->event_space += sizeof e->event;
1098 spin_unlock_irqrestore(&dev->event_lock, flags);
1099 goto out;
1102 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1103 e->event.base.length = sizeof e->event;
1104 e->event.user_data = user_data;
1105 e->base.event = &e->event.base;
1106 e->base.file_priv = file_priv;
1107 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1109 out:
1110 return e;
1113 static void destroy_vblank_event(struct drm_device *dev,
1114 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1116 unsigned long flags;
1118 spin_lock_irqsave(&dev->event_lock, flags);
1119 file_priv->event_space += sizeof e->event;
1120 spin_unlock_irqrestore(&dev->event_lock, flags);
1121 kfree(e);
1124 static int atomic_set_prop(struct drm_atomic_state *state,
1125 struct drm_mode_object *obj, struct drm_property *prop,
1126 uint64_t prop_value)
1128 struct drm_mode_object *ref;
1129 int ret;
1131 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1132 return -EINVAL;
1134 switch (obj->type) {
1135 case DRM_MODE_OBJECT_CONNECTOR: {
1136 struct drm_connector *connector = obj_to_connector(obj);
1137 struct drm_connector_state *connector_state;
1139 connector_state = drm_atomic_get_connector_state(state, connector);
1140 if (IS_ERR(connector_state)) {
1141 ret = PTR_ERR(connector_state);
1142 break;
1145 ret = drm_atomic_connector_set_property(connector,
1146 connector_state, prop, prop_value);
1147 break;
1149 case DRM_MODE_OBJECT_CRTC: {
1150 struct drm_crtc *crtc = obj_to_crtc(obj);
1151 struct drm_crtc_state *crtc_state;
1153 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1154 if (IS_ERR(crtc_state)) {
1155 ret = PTR_ERR(crtc_state);
1156 break;
1159 ret = drm_atomic_crtc_set_property(crtc,
1160 crtc_state, prop, prop_value);
1161 break;
1163 case DRM_MODE_OBJECT_PLANE: {
1164 struct drm_plane *plane = obj_to_plane(obj);
1165 struct drm_plane_state *plane_state;
1167 plane_state = drm_atomic_get_plane_state(state, plane);
1168 if (IS_ERR(plane_state)) {
1169 ret = PTR_ERR(plane_state);
1170 break;
1173 ret = drm_atomic_plane_set_property(plane,
1174 plane_state, prop, prop_value);
1175 break;
1177 default:
1178 ret = -EINVAL;
1179 break;
1182 drm_property_change_valid_put(prop, ref);
1183 return ret;
1186 int drm_mode_atomic_ioctl(struct drm_device *dev,
1187 void *data, struct drm_file *file_priv)
1189 struct drm_mode_atomic *arg = data;
1190 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1191 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1192 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1193 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1194 unsigned int copied_objs, copied_props;
1195 struct drm_atomic_state *state;
1196 struct drm_modeset_acquire_ctx ctx;
1197 struct drm_plane *plane;
1198 struct drm_crtc *crtc;
1199 struct drm_crtc_state *crtc_state;
1200 unsigned plane_mask = 0;
1201 int ret = 0;
1202 unsigned int i, j;
1204 /* disallow for drivers not supporting atomic: */
1205 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1206 return -EINVAL;
1208 /* disallow for userspace that has not enabled atomic cap (even
1209 * though this may be a bit overkill, since legacy userspace
1210 * wouldn't know how to call this ioctl)
1212 if (!file_priv->atomic)
1213 return -EINVAL;
1215 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1216 return -EINVAL;
1218 if (arg->reserved)
1219 return -EINVAL;
1221 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1222 !dev->mode_config.async_page_flip)
1223 return -EINVAL;
1225 /* can't test and expect an event at the same time. */
1226 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1227 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1228 return -EINVAL;
1230 drm_modeset_acquire_init(&ctx, 0);
1232 state = drm_atomic_state_alloc(dev);
1233 if (!state)
1234 return -ENOMEM;
1236 state->acquire_ctx = &ctx;
1237 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1239 retry:
1240 copied_objs = 0;
1241 copied_props = 0;
1243 for (i = 0; i < arg->count_objs; i++) {
1244 uint32_t obj_id, count_props;
1245 struct drm_mode_object *obj;
1247 if (get_user(obj_id, objs_ptr + copied_objs)) {
1248 ret = -EFAULT;
1249 goto fail;
1252 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1253 if (!obj || !obj->properties) {
1254 ret = -ENOENT;
1255 goto fail;
1258 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1259 plane = obj_to_plane(obj);
1260 plane_mask |= (1 << drm_plane_index(plane));
1261 plane->old_fb = plane->fb;
1264 if (get_user(count_props, count_props_ptr + copied_objs)) {
1265 ret = -EFAULT;
1266 goto fail;
1269 copied_objs++;
1271 for (j = 0; j < count_props; j++) {
1272 uint32_t prop_id;
1273 uint64_t prop_value;
1274 struct drm_property *prop;
1276 if (get_user(prop_id, props_ptr + copied_props)) {
1277 ret = -EFAULT;
1278 goto fail;
1281 prop = drm_property_find(dev, prop_id);
1282 if (!prop) {
1283 ret = -ENOENT;
1284 goto fail;
1287 if (copy_from_user(&prop_value,
1288 prop_values_ptr + copied_props,
1289 sizeof(prop_value))) {
1290 ret = -EFAULT;
1291 goto fail;
1294 ret = atomic_set_prop(state, obj, prop, prop_value);
1295 if (ret)
1296 goto fail;
1298 copied_props++;
1302 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1303 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1304 struct drm_pending_vblank_event *e;
1306 e = create_vblank_event(dev, file_priv, arg->user_data);
1307 if (!e) {
1308 ret = -ENOMEM;
1309 goto fail;
1312 crtc_state->event = e;
1316 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1317 ret = drm_atomic_check_only(state);
1318 /* _check_only() does not free state, unlike _commit() */
1319 drm_atomic_state_free(state);
1320 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1321 ret = drm_atomic_async_commit(state);
1322 } else {
1323 ret = drm_atomic_commit(state);
1326 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1327 * locks (ie. while it is still safe to deref plane->state). We
1328 * need to do this here because the driver entry points cannot
1329 * distinguish between legacy and atomic ioctls.
1331 drm_for_each_plane_mask(plane, dev, plane_mask) {
1332 if (ret == 0) {
1333 struct drm_framebuffer *new_fb = plane->state->fb;
1334 if (new_fb)
1335 drm_framebuffer_reference(new_fb);
1336 plane->fb = new_fb;
1337 plane->crtc = plane->state->crtc;
1338 } else {
1339 plane->old_fb = NULL;
1341 if (plane->old_fb) {
1342 drm_framebuffer_unreference(plane->old_fb);
1343 plane->old_fb = NULL;
1347 drm_modeset_drop_locks(&ctx);
1348 drm_modeset_acquire_fini(&ctx);
1350 return ret;
1352 fail:
1353 if (ret == -EDEADLK)
1354 goto backoff;
1356 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1357 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1358 destroy_vblank_event(dev, file_priv, crtc_state->event);
1359 crtc_state->event = NULL;
1363 drm_atomic_state_free(state);
1365 drm_modeset_drop_locks(&ctx);
1366 drm_modeset_acquire_fini(&ctx);
1368 return ret;
1370 backoff:
1371 drm_atomic_state_clear(state);
1372 drm_modeset_backoff(&ctx);
1374 goto retry;