linux/drivers/gpu/drm/drm_atomic.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2014 Red Hat
   3 * Copyright (C) 2014 Intel Corp.
   4 *
   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:
  11 *
  12 * The above copyright notice and this permission notice shall be included in
  13 * all copies or substantial portions of the Software.
  14 *
  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.
  22 *
  23 * Authors:
  24 * Rob Clark <robdclark@gmail.com>
  25 * Daniel Vetter <daniel.vetter@ffwll.ch>
  26 */
  27
  28
  29#include <drm/drmP.h>
  30#include <drm/drm_atomic.h>
  31#include <drm/drm_plane_helper.h>
  32
  33static void kfree_state(struct drm_atomic_state *state)
  34{
  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);
  42}
  43
  44/**
  45 * drm_atomic_state_alloc - allocate atomic state
  46 * @dev: DRM device
  47 *
  48 * This allocates an empty atomic state to track updates.
  49 */
  50struct drm_atomic_state *
  51drm_atomic_state_alloc(struct drm_device *dev)
  52{
  53        struct drm_atomic_state *state;
  54
  55        state = kzalloc(sizeof(*state), GFP_KERNEL);
  56        if (!state)
  57                return NULL;
  58
  59        /* TODO legacy paths should maybe do a better job about
  60         * setting this appropriately?
  61         */
  62        state->allow_modeset = true;
  63
  64        state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
  65
  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;
  92
  93        state->dev = dev;
  94
  95        DRM_DEBUG_ATOMIC("Allocate atomic state %p\n", state);
  96
  97        return state;
  98fail:
  99        kfree_state(state);
 100
 101        return NULL;
 102}
 103EXPORT_SYMBOL(drm_atomic_state_alloc);
 104
 105/**
 106 * drm_atomic_state_clear - clear state object
 107 * @state: atomic state
 108 *
 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.
 115 *
 116 * Hence we must clear all cached state and completely start over, using this
 117 * function.
 118 */
 119void drm_atomic_state_clear(struct drm_atomic_state *state)
 120{
 121        struct drm_device *dev = state->dev;
 122        struct drm_mode_config *config = &dev->mode_config;
 123        int i;
 124
 125        DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
 126
 127        for (i = 0; i < state->num_connector; i++) {
 128                struct drm_connector *connector = state->connectors[i];
 129
 130                if (!connector)
 131                        continue;
 132
 133                WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
 134
 135                connector->funcs->atomic_destroy_state(connector,
 136                                                       state->connector_states[i]);
 137                state->connectors[i] = NULL;
 138                state->connector_states[i] = NULL;
 139        }
 140
 141        for (i = 0; i < config->num_crtc; i++) {
 142                struct drm_crtc *crtc = state->crtcs[i];
 143
 144                if (!crtc)
 145                        continue;
 146
 147                crtc->funcs->atomic_destroy_state(crtc,
 148                                                  state->crtc_states[i]);
 149                state->crtcs[i] = NULL;
 150                state->crtc_states[i] = NULL;
 151        }
 152
 153        for (i = 0; i < config->num_total_plane; i++) {
 154                struct drm_plane *plane = state->planes[i];
 155
 156                if (!plane)
 157                        continue;
 158
 159                plane->funcs->atomic_destroy_state(plane,
 160                                                   state->plane_states[i]);
 161                state->planes[i] = NULL;
 162                state->plane_states[i] = NULL;
 163        }
 164}
 165EXPORT_SYMBOL(drm_atomic_state_clear);
 166
 167/**
 168 * drm_atomic_state_free - free all memory for an atomic state
 169 * @state: atomic state to deallocate
 170 *
 171 * This frees all memory associated with an atomic state, including all the
 172 * per-object state for planes, crtcs and connectors.
 173 */
 174void drm_atomic_state_free(struct drm_atomic_state *state)
 175{
 176        if (!state)
 177                return;
 178
 179        drm_atomic_state_clear(state);
 180
 181        DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
 182
 183        kfree_state(state);
 184}
 185EXPORT_SYMBOL(drm_atomic_state_free);
 186
 187/**
 188 * drm_atomic_get_crtc_state - get crtc state
 189 * @state: global atomic state object
 190 * @crtc: crtc to get state object for
 191 *
 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.
 195 *
 196 * Returns:
 197 *
 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.
 201 */
 202struct drm_crtc_state *
 203drm_atomic_get_crtc_state(struct drm_atomic_state *state,
 204                          struct drm_crtc *crtc)
 205{
 206        int ret, index;
 207        struct drm_crtc_state *crtc_state;
 208
 209        index = drm_crtc_index(crtc);
 210
 211        if (state->crtc_states[index])
 212                return state->crtc_states[index];
 213
 214        ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
 215        if (ret)
 216                return ERR_PTR(ret);
 217
 218        crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
 219        if (!crtc_state)
 220                return ERR_PTR(-ENOMEM);
 221
 222        state->crtc_states[index] = crtc_state;
 223        state->crtcs[index] = crtc;
 224        crtc_state->state = state;
 225
 226        DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
 227                         crtc->base.id, crtc_state, state);
 228
 229        return crtc_state;
 230}
 231EXPORT_SYMBOL(drm_atomic_get_crtc_state);
 232
 233/**
 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
 239 *
 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.
 245 *
 246 * RETURNS:
 247 * Zero on success, error code on failure
 248 */
 249int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
 250                struct drm_crtc_state *state, struct drm_property *property,
 251                uint64_t val)
 252{
 253        struct drm_device *dev = crtc->dev;
 254        struct drm_mode_config *config = &dev->mode_config;
 255
 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;
 263
 264        return 0;
 265}
 266EXPORT_SYMBOL(drm_atomic_crtc_set_property);
 267
 268/*
 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.
 273 */
 274int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
 275                const struct drm_crtc_state *state,
 276                struct drm_property *property, uint64_t *val)
 277{
 278        struct drm_device *dev = crtc->dev;
 279        struct drm_mode_config *config = &dev->mode_config;
 280
 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;
 287
 288        return 0;
 289}
 290
 291/**
 292 * drm_atomic_crtc_check - check crtc state
 293 * @crtc: crtc to check
 294 * @state: crtc state to check
 295 *
 296 * Provides core sanity checks for crtc state.
 297 *
 298 * RETURNS:
 299 * Zero on success, error code on failure
 300 */
 301static int drm_atomic_crtc_check(struct drm_crtc *crtc,
 302                struct drm_crtc_state *state)
 303{
 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.
 308         *
 309         * TODO: Add generic modeset state checks once we support those.
 310         */
 311
 312        if (state->active && !state->enable) {
 313                DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
 314                                 crtc->base.id);
 315                return -EINVAL;
 316        }
 317
 318        return 0;
 319}
 320
 321/**
 322 * drm_atomic_get_plane_state - get plane state
 323 * @state: global atomic state object
 324 * @plane: plane to get state object for
 325 *
 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.
 329 *
 330 * Returns:
 331 *
 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.
 335 */
 336struct drm_plane_state *
 337drm_atomic_get_plane_state(struct drm_atomic_state *state,
 338                          struct drm_plane *plane)
 339{
 340        int ret, index;
 341        struct drm_plane_state *plane_state;
 342
 343        index = drm_plane_index(plane);
 344
 345        if (state->plane_states[index])
 346                return state->plane_states[index];
 347
 348        ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
 349        if (ret)
 350                return ERR_PTR(ret);
 351
 352        plane_state = plane->funcs->atomic_duplicate_state(plane);
 353        if (!plane_state)
 354                return ERR_PTR(-ENOMEM);
 355
 356        state->plane_states[index] = plane_state;
 357        state->planes[index] = plane;
 358        plane_state->state = state;
 359
 360        DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
 361                         plane->base.id, plane_state, state);
 362
 363        if (plane_state->crtc) {
 364                struct drm_crtc_state *crtc_state;
 365
 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);
 370        }
 371
 372        return plane_state;
 373}
 374EXPORT_SYMBOL(drm_atomic_get_plane_state);
 375
 376/**
 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
 382 *
 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.
 388 *
 389 * RETURNS:
 390 * Zero on success, error code on failure
 391 */
 392int drm_atomic_plane_set_property(struct drm_plane *plane,
 393                struct drm_plane_state *state, struct drm_property *property,
 394                uint64_t val)
 395{
 396        struct drm_device *dev = plane->dev;
 397        struct drm_mode_config *config = &dev->mode_config;
 398
 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;
 430        }
 431
 432        return 0;
 433}
 434EXPORT_SYMBOL(drm_atomic_plane_set_property);
 435
 436/*
 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.
 441 */
 442static int
 443drm_atomic_plane_get_property(struct drm_plane *plane,
 444                const struct drm_plane_state *state,
 445                struct drm_property *property, uint64_t *val)
 446{
 447        struct drm_device *dev = plane->dev;
 448        struct drm_mode_config *config = &dev->mode_config;
 449
 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;
 476        }
 477
 478        return 0;
 479}
 480
 481/**
 482 * drm_atomic_plane_check - check plane state
 483 * @plane: plane to check
 484 * @state: plane state to check
 485 *
 486 * Provides core sanity checks for plane state.
 487 *
 488 * RETURNS:
 489 * Zero on success, error code on failure
 490 */
 491static int drm_atomic_plane_check(struct drm_plane *plane,
 492                struct drm_plane_state *state)
 493{
 494        unsigned int fb_width, fb_height;
 495        int ret;
 496
 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;
 504        }
 505
 506        /* if disabled, we don't care about the rest of the state: */
 507        if (!state->crtc)
 508                return 0;
 509
 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;
 514        }
 515
 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;
 522        }
 523
 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;
 533        }
 534
 535        fb_width = state->fb->width << 16;
 536        fb_height = state->fb->height << 16;
 537
 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;
 550        }
 551
 552        return 0;
 553}
 554
 555/**
 556 * drm_atomic_get_connector_state - get connector state
 557 * @state: global atomic state object
 558 * @connector: connector to get state object for
 559 *
 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.
 563 *
 564 * Returns:
 565 *
 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.
 569 */
 570struct drm_connector_state *
 571drm_atomic_get_connector_state(struct drm_atomic_state *state,
 572                          struct drm_connector *connector)
 573{
 574        int ret, index;
 575        struct drm_mode_config *config = &connector->dev->mode_config;
 576        struct drm_connector_state *connector_state;
 577
 578        ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
 579        if (ret)
 580                return ERR_PTR(ret);
 581
 582        index = drm_connector_index(connector);
 583
 584        /*
 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.
 589         *
 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.
 593         */
 594        if (index >= state->num_connector) {
 595                DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
 596                return ERR_PTR(-EAGAIN);
 597        }
 598
 599        if (state->connector_states[index])
 600                return state->connector_states[index];
 601
 602        connector_state = connector->funcs->atomic_duplicate_state(connector);
 603        if (!connector_state)
 604                return ERR_PTR(-ENOMEM);
 605
 606        state->connector_states[index] = connector_state;
 607        state->connectors[index] = connector;
 608        connector_state->state = state;
 609
 610        DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
 611                         connector->base.id, connector_state, state);
 612
 613        if (connector_state->crtc) {
 614                struct drm_crtc_state *crtc_state;
 615
 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);
 620        }
 621
 622        return connector_state;
 623}
 624EXPORT_SYMBOL(drm_atomic_get_connector_state);
 625
 626/**
 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
 632 *
 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.
 638 *
 639 * RETURNS:
 640 * Zero on success, error code on failure
 641 */
 642int drm_atomic_connector_set_property(struct drm_connector *connector,
 643                struct drm_connector_state *state, struct drm_property *property,
 644                uint64_t val)
 645{
 646        struct drm_device *dev = connector->dev;
 647        struct drm_mode_config *config = &dev->mode_config;
 648
 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:
 656                 */
 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;
 663        }
 664}
 665EXPORT_SYMBOL(drm_atomic_connector_set_property);
 666
 667/*
 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.
 672 */
 673static int
 674drm_atomic_connector_get_property(struct drm_connector *connector,
 675                const struct drm_connector_state *state,
 676                struct drm_property *property, uint64_t *val)
 677{
 678        struct drm_device *dev = connector->dev;
 679        struct drm_mode_config *config = &dev->mode_config;
 680
 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;
 690        }
 691
 692        return 0;
 693}
 694
 695int drm_atomic_get_property(struct drm_mode_object *obj,
 696                struct drm_property *property, uint64_t *val)
 697{
 698        struct drm_device *dev = property->dev;
 699        int ret;
 700
 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;
 708        }
 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;
 715        }
 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;
 722        }
 723        default:
 724                ret = -EINVAL;
 725                break;
 726        }
 727
 728        return ret;
 729}
 730
 731/**
 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
 735 *
 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.
 739 *
 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.
 744 */
 745int
 746drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
 747                              struct drm_crtc *crtc)
 748{
 749        struct drm_plane *plane = plane_state->plane;
 750        struct drm_crtc_state *crtc_state;
 751
 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);
 757
 758                crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
 759        }
 760
 761        plane_state->crtc = crtc;
 762
 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));
 769        }
 770
 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);
 777
 778        return 0;
 779}
 780EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
 781
 782/**
 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
 786 *
 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.
 791 */
 792void
 793drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
 794                            struct drm_framebuffer *fb)
 795{
 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;
 801
 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);
 808}
 809EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
 810
 811/**
 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
 815 *
 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.
 819 *
 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.
 824 */
 825int
 826drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
 827                                  struct drm_crtc *crtc)
 828{
 829        struct drm_crtc_state *crtc_state;
 830
 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);
 835        }
 836
 837        conn_state->crtc = crtc;
 838
 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);
 845
 846        return 0;
 847}
 848EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
 849
 850/**
 851 * drm_atomic_add_affected_connectors - add connectors for crtc
 852 * @state: atomic state
 853 * @crtc: DRM crtc
 854 *
 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).
 861 *
 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.
 866 */
 867int
 868drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
 869                                   struct drm_crtc *crtc)
 870{
 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;
 875
 876        ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
 877        if (ret)
 878                return ret;
 879
 880        DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
 881                         crtc->base.id, state);
 882
 883        /*
 884         * Changed connectors are already in @state, so only need to look at the
 885         * current configuration.
 886         */
 887        list_for_each_entry(connector, &config->connector_list, head) {
 888                if (connector->state->crtc != crtc)
 889                        continue;
 890
 891                conn_state = drm_atomic_get_connector_state(state, connector);
 892                if (IS_ERR(conn_state))
 893                        return PTR_ERR(conn_state);
 894        }
 895
 896        return 0;
 897}
 898EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
 899
 900/**
 901 * drm_atomic_connectors_for_crtc - count number of connected outputs
 902 * @state: atomic state
 903 * @crtc: DRM crtc
 904 *
 905 * This function counts all connectors which will be connected to @crtc
 906 * according to @state. Useful to recompute the enable state for @crtc.
 907 */
 908int
 909drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
 910                               struct drm_crtc *crtc)
 911{
 912        struct drm_connector *connector;
 913        struct drm_connector_state *conn_state;
 914
 915        int i, num_connected_connectors = 0;
 916
 917        for_each_connector_in_state(state, connector, conn_state, i) {
 918                if (conn_state->crtc == crtc)
 919                        num_connected_connectors++;
 920        }
 921
 922        DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
 923                         state, num_connected_connectors, crtc->base.id);
 924
 925        return num_connected_connectors;
 926}
 927EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
 928
 929/**
 930 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
 931 * @state: atomic state
 932 *
 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.
 936 */
 937void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
 938{
 939        int ret;
 940
 941retry:
 942        drm_modeset_backoff(state->acquire_ctx);
 943
 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;
 952}
 953EXPORT_SYMBOL(drm_atomic_legacy_backoff);
 954
 955/**
 956 * drm_atomic_check_only - check whether a given config would work
 957 * @state: atomic configuration to check
 958 *
 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.
 962 *
 963 * Returns:
 964 * 0 on success, negative error code on failure.
 965 */
 966int drm_atomic_check_only(struct drm_atomic_state *state)
 967{
 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;
 975
 976        DRM_DEBUG_ATOMIC("checking %p\n", state);
 977
 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;
 984                }
 985        }
 986
 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;
 993                }
 994        }
 995
 996        if (config->funcs->atomic_check)
 997                ret = config->funcs->atomic_check(state->dev, state);
 998
 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;
1006                        }
1007                }
1008        }
1009
1010        return ret;
1011}
1012EXPORT_SYMBOL(drm_atomic_check_only);
1013
1014/**
1015 * drm_atomic_commit - commit configuration atomically
1016 * @state: atomic configuration to check
1017 *
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.
1021 *
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.
1026 *
1027 * Returns:
1028 * 0 on success, negative error code on failure.
1029 */
1030int drm_atomic_commit(struct drm_atomic_state *state)
1031{
1032        struct drm_mode_config *config = &state->dev->mode_config;
1033        int ret;
1034
1035        ret = drm_atomic_check_only(state);
1036        if (ret)
1037                return ret;
1038
1039        DRM_DEBUG_ATOMIC("commiting %p\n", state);
1040
1041        return config->funcs->atomic_commit(state->dev, state, false);
1042}
1043EXPORT_SYMBOL(drm_atomic_commit);
1044
1045/**
1046 * drm_atomic_async_commit - atomic&async configuration commit
1047 * @state: atomic configuration to check
1048 *
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.
1052 *
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.
1057 *
1058 * Returns:
1059 * 0 on success, negative error code on failure.
1060 */
1061int drm_atomic_async_commit(struct drm_atomic_state *state)
1062{
1063        struct drm_mode_config *config = &state->dev->mode_config;
1064        int ret;
1065
1066        ret = drm_atomic_check_only(state);
1067        if (ret)
1068                return ret;
1069
1070        DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1071
1072        return config->funcs->atomic_commit(state->dev, state, true);
1073}
1074EXPORT_SYMBOL(drm_atomic_async_commit);
1075
1076/*
1077 * The big monstor ioctl
1078 */
1079
1080static struct drm_pending_vblank_event *create_vblank_event(
1081                struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1082{
1083        struct drm_pending_vblank_event *e = NULL;
1084        unsigned long flags;
1085
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;
1090        }
1091        file_priv->event_space -= sizeof e->event;
1092        spin_unlock_irqrestore(&dev->event_lock, flags);
1093
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;
1100        }
1101
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;
1108
1109out:
1110        return e;
1111}
1112
1113static void destroy_vblank_event(struct drm_device *dev,
1114                struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1115{
1116        unsigned long flags;
1117
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);
1122}
1123
1124static int atomic_set_prop(struct drm_atomic_state *state,
1125                struct drm_mode_object *obj, struct drm_property *prop,
1126                uint64_t prop_value)
1127{
1128        struct drm_mode_object *ref;
1129        int ret;
1130
1131        if (!drm_property_change_valid_get(prop, prop_value, &ref))
1132                return -EINVAL;
1133
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;
1138
1139                connector_state = drm_atomic_get_connector_state(state, connector);
1140                if (IS_ERR(connector_state)) {
1141                        ret = PTR_ERR(connector_state);
1142                        break;
1143                }
1144
1145                ret = drm_atomic_connector_set_property(connector,
1146                                connector_state, prop, prop_value);
1147                break;
1148        }
1149        case DRM_MODE_OBJECT_CRTC: {
1150                struct drm_crtc *crtc = obj_to_crtc(obj);
1151                struct drm_crtc_state *crtc_state;
1152
1153                crtc_state = drm_atomic_get_crtc_state(state, crtc);
1154                if (IS_ERR(crtc_state)) {
1155                        ret = PTR_ERR(crtc_state);
1156                        break;
1157                }
1158
1159                ret = drm_atomic_crtc_set_property(crtc,
1160                                crtc_state, prop, prop_value);
1161                break;
1162        }
1163        case DRM_MODE_OBJECT_PLANE: {
1164                struct drm_plane *plane = obj_to_plane(obj);
1165                struct drm_plane_state *plane_state;
1166
1167                plane_state = drm_atomic_get_plane_state(state, plane);
1168                if (IS_ERR(plane_state)) {
1169                        ret = PTR_ERR(plane_state);
1170                        break;
1171                }
1172
1173                ret = drm_atomic_plane_set_property(plane,
1174                                plane_state, prop, prop_value);
1175                break;
1176        }
1177        default:
1178                ret = -EINVAL;
1179                break;
1180        }
1181
1182        drm_property_change_valid_put(prop, ref);
1183        return ret;
1184}
1185
1186int drm_mode_atomic_ioctl(struct drm_device *dev,
1187                          void *data, struct drm_file *file_priv)
1188{
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;
1203
1204        /* disallow for drivers not supporting atomic: */
1205        if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1206                return -EINVAL;
1207
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)
1211         */
1212        if (!file_priv->atomic)
1213                return -EINVAL;
1214
1215        if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1216                return -EINVAL;
1217
1218        if (arg->reserved)
1219                return -EINVAL;
1220
1221        if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1222                        !dev->mode_config.async_page_flip)
1223                return -EINVAL;
1224
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;
1229
1230        drm_modeset_acquire_init(&ctx, 0);
1231
1232        state = drm_atomic_state_alloc(dev);
1233        if (!state)
1234                return -ENOMEM;
1235
1236        state->acquire_ctx = &ctx;
1237        state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1238
1239retry:
1240        copied_objs = 0;
1241        copied_props = 0;
1242
1243        for (i = 0; i < arg->count_objs; i++) {
1244                uint32_t obj_id, count_props;
1245                struct drm_mode_object *obj;
1246
1247                if (get_user(obj_id, objs_ptr + copied_objs)) {
1248                        ret = -EFAULT;
1249                        goto fail;
1250                }
1251
1252                obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1253                if (!obj || !obj->properties) {
1254                        ret = -ENOENT;
1255                        goto fail;
1256                }
1257
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;
1262                }
1263
1264                if (get_user(count_props, count_props_ptr + copied_objs)) {
1265                        ret = -EFAULT;
1266                        goto fail;
1267                }
1268
1269                copied_objs++;
1270
1271                for (j = 0; j < count_props; j++) {
1272                        uint32_t prop_id;
1273                        uint64_t prop_value;
1274                        struct drm_property *prop;
1275
1276                        if (get_user(prop_id, props_ptr + copied_props)) {
1277                                ret = -EFAULT;
1278                                goto fail;
1279                        }
1280
1281                        prop = drm_property_find(dev, prop_id);
1282                        if (!prop) {
1283                                ret = -ENOENT;
1284                                goto fail;
1285                        }
1286
1287                        if (copy_from_user(&prop_value,
1288                                           prop_values_ptr + copied_props,
1289                                           sizeof(prop_value))) {
1290                                ret = -EFAULT;
1291                                goto fail;
1292                        }
1293
1294                        ret = atomic_set_prop(state, obj, prop, prop_value);
1295                        if (ret)
1296                                goto fail;
1297
1298                        copied_props++;
1299                }
1300        }
1301
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;
1305
1306                        e = create_vblank_event(dev, file_priv, arg->user_data);
1307                        if (!e) {
1308                                ret = -ENOMEM;
1309                                goto fail;
1310                        }
1311
1312                        crtc_state->event = e;
1313                }
1314        }
1315
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);
1324        }
1325
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.
1330         */
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;
1340                }
1341                if (plane->old_fb) {
1342                        drm_framebuffer_unreference(plane->old_fb);
1343                        plane->old_fb = NULL;
1344                }
1345        }
1346
1347        drm_modeset_drop_locks(&ctx);
1348        drm_modeset_acquire_fini(&ctx);
1349
1350        return ret;
1351
1352fail:
1353        if (ret == -EDEADLK)
1354                goto backoff;
1355
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;
1360                }
1361        }
1362
1363        drm_atomic_state_free(state);
1364
1365        drm_modeset_drop_locks(&ctx);
1366        drm_modeset_acquire_fini(&ctx);
1367
1368        return ret;
1369
1370backoff:
1371        drm_atomic_state_clear(state);
1372        drm_modeset_backoff(&ctx);
1373
1374        goto retry;
1375}
1376