linux/drivers/gpu/drm/drm_atomic_helper.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#include <drm/drmP.h>
  29#include <drm/drm_atomic.h>
  30#include <drm/drm_plane_helper.h>
  31#include <drm/drm_crtc_helper.h>
  32#include <drm/drm_atomic_helper.h>
  33#include <linux/dma-fence.h>
  34
  35#include "drm_crtc_internal.h"
  36
  37/**
  38 * DOC: overview
  39 *
  40 * This helper library provides implementations of check and commit functions on
  41 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
  42 * also provides convenience implementations for the atomic state handling
  43 * callbacks for drivers which don't need to subclass the drm core structures to
  44 * add their own additional internal state.
  45 *
  46 * This library also provides default implementations for the check callback in
  47 * drm_atomic_helper_check() and for the commit callback with
  48 * drm_atomic_helper_commit(). But the individual stages and callbacks are
  49 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
  50 * together with a driver private modeset implementation.
  51 *
  52 * This library also provides implementations for all the legacy driver
  53 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
  54 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
  55 * various functions to implement set_property callbacks. New drivers must not
  56 * implement these functions themselves but must use the provided helpers.
  57 *
  58 * The atomic helper uses the same function table structures as all other
  59 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
  60 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
  61 * also shares the &struct drm_plane_helper_funcs function table with the plane
  62 * helpers.
  63 */
  64static void
  65drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
  66                                struct drm_plane_state *plane_state,
  67                                struct drm_plane *plane)
  68{
  69        struct drm_crtc_state *crtc_state;
  70
  71        if (plane->state->crtc) {
  72                crtc_state = drm_atomic_get_existing_crtc_state(state,
  73                                                                plane->state->crtc);
  74
  75                if (WARN_ON(!crtc_state))
  76                        return;
  77
  78                crtc_state->planes_changed = true;
  79        }
  80
  81        if (plane_state->crtc) {
  82                crtc_state = drm_atomic_get_existing_crtc_state(state,
  83                                                                plane_state->crtc);
  84
  85                if (WARN_ON(!crtc_state))
  86                        return;
  87
  88                crtc_state->planes_changed = true;
  89        }
  90}
  91
  92static int handle_conflicting_encoders(struct drm_atomic_state *state,
  93                                       bool disable_conflicting_encoders)
  94{
  95        struct drm_connector_state *conn_state;
  96        struct drm_connector *connector;
  97        struct drm_connector_list_iter conn_iter;
  98        struct drm_encoder *encoder;
  99        unsigned encoder_mask = 0;
 100        int i, ret = 0;
 101
 102        /*
 103         * First loop, find all newly assigned encoders from the connectors
 104         * part of the state. If the same encoder is assigned to multiple
 105         * connectors bail out.
 106         */
 107        for_each_connector_in_state(state, connector, conn_state, i) {
 108                const struct drm_connector_helper_funcs *funcs = connector->helper_private;
 109                struct drm_encoder *new_encoder;
 110
 111                if (!conn_state->crtc)
 112                        continue;
 113
 114                if (funcs->atomic_best_encoder)
 115                        new_encoder = funcs->atomic_best_encoder(connector, conn_state);
 116                else if (funcs->best_encoder)
 117                        new_encoder = funcs->best_encoder(connector);
 118                else
 119                        new_encoder = drm_atomic_helper_best_encoder(connector);
 120
 121                if (new_encoder) {
 122                        if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
 123                                DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
 124                                        new_encoder->base.id, new_encoder->name,
 125                                        connector->base.id, connector->name);
 126
 127                                return -EINVAL;
 128                        }
 129
 130                        encoder_mask |= 1 << drm_encoder_index(new_encoder);
 131                }
 132        }
 133
 134        if (!encoder_mask)
 135                return 0;
 136
 137        /*
 138         * Second loop, iterate over all connectors not part of the state.
 139         *
 140         * If a conflicting encoder is found and disable_conflicting_encoders
 141         * is not set, an error is returned. Userspace can provide a solution
 142         * through the atomic ioctl.
 143         *
 144         * If the flag is set conflicting connectors are removed from the crtc
 145         * and the crtc is disabled if no encoder is left. This preserves
 146         * compatibility with the legacy set_config behavior.
 147         */
 148        drm_connector_list_iter_get(state->dev, &conn_iter);
 149        drm_for_each_connector_iter(connector, &conn_iter) {
 150                struct drm_crtc_state *crtc_state;
 151
 152                if (drm_atomic_get_existing_connector_state(state, connector))
 153                        continue;
 154
 155                encoder = connector->state->best_encoder;
 156                if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
 157                        continue;
 158
 159                if (!disable_conflicting_encoders) {
 160                        DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
 161                                         encoder->base.id, encoder->name,
 162                                         connector->state->crtc->base.id,
 163                                         connector->state->crtc->name,
 164                                         connector->base.id, connector->name);
 165                        ret = -EINVAL;
 166                        goto out;
 167                }
 168
 169                conn_state = drm_atomic_get_connector_state(state, connector);
 170                if (IS_ERR(conn_state)) {
 171                        ret = PTR_ERR(conn_state);
 172                        goto out;
 173                }
 174
 175                DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
 176                                 encoder->base.id, encoder->name,
 177                                 conn_state->crtc->base.id, conn_state->crtc->name,
 178                                 connector->base.id, connector->name);
 179
 180                crtc_state = drm_atomic_get_existing_crtc_state(state, conn_state->crtc);
 181
 182                ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
 183                if (ret)
 184                        goto out;
 185
 186                if (!crtc_state->connector_mask) {
 187                        ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
 188                                                                NULL);
 189                        if (ret < 0)
 190                                goto out;
 191
 192                        crtc_state->active = false;
 193                }
 194        }
 195out:
 196        drm_connector_list_iter_put(&conn_iter);
 197
 198        return ret;
 199}
 200
 201static void
 202set_best_encoder(struct drm_atomic_state *state,
 203                 struct drm_connector_state *conn_state,
 204                 struct drm_encoder *encoder)
 205{
 206        struct drm_crtc_state *crtc_state;
 207        struct drm_crtc *crtc;
 208
 209        if (conn_state->best_encoder) {
 210                /* Unset the encoder_mask in the old crtc state. */
 211                crtc = conn_state->connector->state->crtc;
 212
 213                /* A NULL crtc is an error here because we should have
 214                 *  duplicated a NULL best_encoder when crtc was NULL.
 215                 * As an exception restoring duplicated atomic state
 216                 * during resume is allowed, so don't warn when
 217                 * best_encoder is equal to encoder we intend to set.
 218                 */
 219                WARN_ON(!crtc && encoder != conn_state->best_encoder);
 220                if (crtc) {
 221                        crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
 222
 223                        crtc_state->encoder_mask &=
 224                                ~(1 << drm_encoder_index(conn_state->best_encoder));
 225                }
 226        }
 227
 228        if (encoder) {
 229                crtc = conn_state->crtc;
 230                WARN_ON(!crtc);
 231                if (crtc) {
 232                        crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
 233
 234                        crtc_state->encoder_mask |=
 235                                1 << drm_encoder_index(encoder);
 236                }
 237        }
 238
 239        conn_state->best_encoder = encoder;
 240}
 241
 242static void
 243steal_encoder(struct drm_atomic_state *state,
 244              struct drm_encoder *encoder)
 245{
 246        struct drm_crtc_state *crtc_state;
 247        struct drm_connector *connector;
 248        struct drm_connector_state *connector_state;
 249        int i;
 250
 251        for_each_connector_in_state(state, connector, connector_state, i) {
 252                struct drm_crtc *encoder_crtc;
 253
 254                if (connector_state->best_encoder != encoder)
 255                        continue;
 256
 257                encoder_crtc = connector->state->crtc;
 258
 259                DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
 260                                 encoder->base.id, encoder->name,
 261                                 encoder_crtc->base.id, encoder_crtc->name);
 262
 263                set_best_encoder(state, connector_state, NULL);
 264
 265                crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
 266                crtc_state->connectors_changed = true;
 267
 268                return;
 269        }
 270}
 271
 272static int
 273update_connector_routing(struct drm_atomic_state *state,
 274                         struct drm_connector *connector,
 275                         struct drm_connector_state *connector_state)
 276{
 277        const struct drm_connector_helper_funcs *funcs;
 278        struct drm_encoder *new_encoder;
 279        struct drm_crtc_state *crtc_state;
 280
 281        DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
 282                         connector->base.id,
 283                         connector->name);
 284
 285        if (connector->state->crtc != connector_state->crtc) {
 286                if (connector->state->crtc) {
 287                        crtc_state = drm_atomic_get_existing_crtc_state(state, connector->state->crtc);
 288                        crtc_state->connectors_changed = true;
 289                }
 290
 291                if (connector_state->crtc) {
 292                        crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
 293                        crtc_state->connectors_changed = true;
 294                }
 295        }
 296
 297        if (!connector_state->crtc) {
 298                DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
 299                                connector->base.id,
 300                                connector->name);
 301
 302                set_best_encoder(state, connector_state, NULL);
 303
 304                return 0;
 305        }
 306
 307        funcs = connector->helper_private;
 308
 309        if (funcs->atomic_best_encoder)
 310                new_encoder = funcs->atomic_best_encoder(connector,
 311                                                         connector_state);
 312        else if (funcs->best_encoder)
 313                new_encoder = funcs->best_encoder(connector);
 314        else
 315                new_encoder = drm_atomic_helper_best_encoder(connector);
 316
 317        if (!new_encoder) {
 318                DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
 319                                 connector->base.id,
 320                                 connector->name);
 321                return -EINVAL;
 322        }
 323
 324        if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
 325                DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
 326                                 new_encoder->base.id,
 327                                 new_encoder->name,
 328                                 connector_state->crtc->base.id);
 329                return -EINVAL;
 330        }
 331
 332        if (new_encoder == connector_state->best_encoder) {
 333                set_best_encoder(state, connector_state, new_encoder);
 334
 335                DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
 336                                 connector->base.id,
 337                                 connector->name,
 338                                 new_encoder->base.id,
 339                                 new_encoder->name,
 340                                 connector_state->crtc->base.id,
 341                                 connector_state->crtc->name);
 342
 343                return 0;
 344        }
 345
 346        steal_encoder(state, new_encoder);
 347
 348        set_best_encoder(state, connector_state, new_encoder);
 349
 350        crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
 351        crtc_state->connectors_changed = true;
 352
 353        DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
 354                         connector->base.id,
 355                         connector->name,
 356                         new_encoder->base.id,
 357                         new_encoder->name,
 358                         connector_state->crtc->base.id,
 359                         connector_state->crtc->name);
 360
 361        return 0;
 362}
 363
 364static int
 365mode_fixup(struct drm_atomic_state *state)
 366{
 367        struct drm_crtc *crtc;
 368        struct drm_crtc_state *crtc_state;
 369        struct drm_connector *connector;
 370        struct drm_connector_state *conn_state;
 371        int i;
 372        int ret;
 373
 374        for_each_crtc_in_state(state, crtc, crtc_state, i) {
 375                if (!crtc_state->mode_changed &&
 376                    !crtc_state->connectors_changed)
 377                        continue;
 378
 379                drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
 380        }
 381
 382        for_each_connector_in_state(state, connector, conn_state, i) {
 383                const struct drm_encoder_helper_funcs *funcs;
 384                struct drm_encoder *encoder;
 385
 386                WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
 387
 388                if (!conn_state->crtc || !conn_state->best_encoder)
 389                        continue;
 390
 391                crtc_state = drm_atomic_get_existing_crtc_state(state,
 392                                                                conn_state->crtc);
 393
 394                /*
 395                 * Each encoder has at most one connector (since we always steal
 396                 * it away), so we won't call ->mode_fixup twice.
 397                 */
 398                encoder = conn_state->best_encoder;
 399                funcs = encoder->helper_private;
 400
 401                ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
 402                                &crtc_state->adjusted_mode);
 403                if (!ret) {
 404                        DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
 405                        return -EINVAL;
 406                }
 407
 408                if (funcs && funcs->atomic_check) {
 409                        ret = funcs->atomic_check(encoder, crtc_state,
 410                                                  conn_state);
 411                        if (ret) {
 412                                DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
 413                                                 encoder->base.id, encoder->name);
 414                                return ret;
 415                        }
 416                } else if (funcs && funcs->mode_fixup) {
 417                        ret = funcs->mode_fixup(encoder, &crtc_state->mode,
 418                                                &crtc_state->adjusted_mode);
 419                        if (!ret) {
 420                                DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
 421                                                 encoder->base.id, encoder->name);
 422                                return -EINVAL;
 423                        }
 424                }
 425        }
 426
 427        for_each_crtc_in_state(state, crtc, crtc_state, i) {
 428                const struct drm_crtc_helper_funcs *funcs;
 429
 430                if (!crtc_state->enable)
 431                        continue;
 432
 433                if (!crtc_state->mode_changed &&
 434                    !crtc_state->connectors_changed)
 435                        continue;
 436
 437                funcs = crtc->helper_private;
 438                if (!funcs->mode_fixup)
 439                        continue;
 440
 441                ret = funcs->mode_fixup(crtc, &crtc_state->mode,
 442                                        &crtc_state->adjusted_mode);
 443                if (!ret) {
 444                        DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
 445                                         crtc->base.id, crtc->name);
 446                        return -EINVAL;
 447                }
 448        }
 449
 450        return 0;
 451}
 452
 453/**
 454 * drm_atomic_helper_check_modeset - validate state object for modeset changes
 455 * @dev: DRM device
 456 * @state: the driver state object
 457 *
 458 * Check the state object to see if the requested state is physically possible.
 459 * This does all the crtc and connector related computations for an atomic
 460 * update and adds any additional connectors needed for full modesets and calls
 461 * down into &drm_crtc_helper_funcs.mode_fixup and
 462 * &drm_encoder_helper_funcs.mode_fixup or
 463 * &drm_encoder_helper_funcs.atomic_check functions of the driver backend.
 464 *
 465 * &drm_crtc_state.mode_changed is set when the input mode is changed.
 466 * &drm_crtc_state.connectors_changed is set when a connector is added or
 467 * removed from the crtc.  &drm_crtc_state.active_changed is set when
 468 * &drm_crtc_state.active changes, which is used for DPMS.
 469 * See also: drm_atomic_crtc_needs_modeset()
 470 *
 471 * IMPORTANT:
 472 *
 473 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
 474 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
 475 * without a full modeset) _must_ call this function afterwards after that
 476 * change. It is permitted to call this function multiple times for the same
 477 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
 478 * upon the adjusted dotclock for fifo space allocation and watermark
 479 * computation.
 480 *
 481 * RETURNS:
 482 * Zero for success or -errno
 483 */
 484int
 485drm_atomic_helper_check_modeset(struct drm_device *dev,
 486                                struct drm_atomic_state *state)
 487{
 488        struct drm_crtc *crtc;
 489        struct drm_crtc_state *crtc_state;
 490        struct drm_connector *connector;
 491        struct drm_connector_state *connector_state;
 492        int i, ret;
 493
 494        for_each_crtc_in_state(state, crtc, crtc_state, i) {
 495                if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
 496                        DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
 497                                         crtc->base.id, crtc->name);
 498                        crtc_state->mode_changed = true;
 499                }
 500
 501                if (crtc->state->enable != crtc_state->enable) {
 502                        DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
 503                                         crtc->base.id, crtc->name);
 504
 505                        /*
 506                         * For clarity this assignment is done here, but
 507                         * enable == 0 is only true when there are no
 508                         * connectors and a NULL mode.
 509                         *
 510                         * The other way around is true as well. enable != 0
 511                         * iff connectors are attached and a mode is set.
 512                         */
 513                        crtc_state->mode_changed = true;
 514                        crtc_state->connectors_changed = true;
 515                }
 516        }
 517
 518        ret = handle_conflicting_encoders(state, state->legacy_set_config);
 519        if (ret)
 520                return ret;
 521
 522        for_each_connector_in_state(state, connector, connector_state, i) {
 523                /*
 524                 * This only sets crtc->connectors_changed for routing changes,
 525                 * drivers must set crtc->connectors_changed themselves when
 526                 * connector properties need to be updated.
 527                 */
 528                ret = update_connector_routing(state, connector,
 529                                               connector_state);
 530                if (ret)
 531                        return ret;
 532        }
 533
 534        /*
 535         * After all the routing has been prepared we need to add in any
 536         * connector which is itself unchanged, but who's crtc changes it's
 537         * configuration. This must be done before calling mode_fixup in case a
 538         * crtc only changed its mode but has the same set of connectors.
 539         */
 540        for_each_crtc_in_state(state, crtc, crtc_state, i) {
 541                bool has_connectors =
 542                        !!crtc_state->connector_mask;
 543
 544                /*
 545                 * We must set ->active_changed after walking connectors for
 546                 * otherwise an update that only changes active would result in
 547                 * a full modeset because update_connector_routing force that.
 548                 */
 549                if (crtc->state->active != crtc_state->active) {
 550                        DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
 551                                         crtc->base.id, crtc->name);
 552                        crtc_state->active_changed = true;
 553                }
 554
 555                if (!drm_atomic_crtc_needs_modeset(crtc_state))
 556                        continue;
 557
 558                DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
 559                                 crtc->base.id, crtc->name,
 560                                 crtc_state->enable ? 'y' : 'n',
 561                                 crtc_state->active ? 'y' : 'n');
 562
 563                ret = drm_atomic_add_affected_connectors(state, crtc);
 564                if (ret != 0)
 565                        return ret;
 566
 567                ret = drm_atomic_add_affected_planes(state, crtc);
 568                if (ret != 0)
 569                        return ret;
 570
 571                if (crtc_state->enable != has_connectors) {
 572                        DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
 573                                         crtc->base.id, crtc->name);
 574
 575                        return -EINVAL;
 576                }
 577        }
 578
 579        return mode_fixup(state);
 580}
 581EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
 582
 583/**
 584 * drm_atomic_helper_check_planes - validate state object for planes changes
 585 * @dev: DRM device
 586 * @state: the driver state object
 587 *
 588 * Check the state object to see if the requested state is physically possible.
 589 * This does all the plane update related checks using by calling into the
 590 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
 591 * hooks provided by the driver.
 592 *
 593 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
 594 * updated planes.
 595 *
 596 * RETURNS:
 597 * Zero for success or -errno
 598 */
 599int
 600drm_atomic_helper_check_planes(struct drm_device *dev,
 601                               struct drm_atomic_state *state)
 602{
 603        struct drm_crtc *crtc;
 604        struct drm_crtc_state *crtc_state;
 605        struct drm_plane *plane;
 606        struct drm_plane_state *plane_state;
 607        int i, ret = 0;
 608
 609        for_each_plane_in_state(state, plane, plane_state, i) {
 610                const struct drm_plane_helper_funcs *funcs;
 611
 612                funcs = plane->helper_private;
 613
 614                drm_atomic_helper_plane_changed(state, plane_state, plane);
 615
 616                if (!funcs || !funcs->atomic_check)
 617                        continue;
 618
 619                ret = funcs->atomic_check(plane, plane_state);
 620                if (ret) {
 621                        DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
 622                                         plane->base.id, plane->name);
 623                        return ret;
 624                }
 625        }
 626
 627        for_each_crtc_in_state(state, crtc, crtc_state, i) {
 628                const struct drm_crtc_helper_funcs *funcs;
 629
 630                funcs = crtc->helper_private;
 631
 632                if (!funcs || !funcs->atomic_check)
 633                        continue;
 634
 635                ret = funcs->atomic_check(crtc, crtc_state);
 636                if (ret) {
 637                        DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
 638                                         crtc->base.id, crtc->name);
 639                        return ret;
 640                }
 641        }
 642
 643        return ret;
 644}
 645EXPORT_SYMBOL(drm_atomic_helper_check_planes);
 646
 647/**
 648 * drm_atomic_helper_check - validate state object
 649 * @dev: DRM device
 650 * @state: the driver state object
 651 *
 652 * Check the state object to see if the requested state is physically possible.
 653 * Only crtcs and planes have check callbacks, so for any additional (global)
 654 * checking that a driver needs it can simply wrap that around this function.
 655 * Drivers without such needs can directly use this as their
 656 * &drm_mode_config_funcs.atomic_check callback.
 657 *
 658 * This just wraps the two parts of the state checking for planes and modeset
 659 * state in the default order: First it calls drm_atomic_helper_check_modeset()
 660 * and then drm_atomic_helper_check_planes(). The assumption is that the
 661 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
 662 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
 663 * watermarks.
 664 *
 665 * RETURNS:
 666 * Zero for success or -errno
 667 */
 668int drm_atomic_helper_check(struct drm_device *dev,
 669                            struct drm_atomic_state *state)
 670{
 671        int ret;
 672
 673        ret = drm_atomic_helper_check_modeset(dev, state);
 674        if (ret)
 675                return ret;
 676
 677        ret = drm_atomic_helper_check_planes(dev, state);
 678        if (ret)
 679                return ret;
 680
 681        return ret;
 682}
 683EXPORT_SYMBOL(drm_atomic_helper_check);
 684
 685static void
 686disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
 687{
 688        struct drm_connector *connector;
 689        struct drm_connector_state *old_conn_state;
 690        struct drm_crtc *crtc;
 691        struct drm_crtc_state *old_crtc_state;
 692        int i;
 693
 694        for_each_connector_in_state(old_state, connector, old_conn_state, i) {
 695                const struct drm_encoder_helper_funcs *funcs;
 696                struct drm_encoder *encoder;
 697
 698                /* Shut down everything that's in the changeset and currently
 699                 * still on. So need to check the old, saved state. */
 700                if (!old_conn_state->crtc)
 701                        continue;
 702
 703                old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
 704                                                                    old_conn_state->crtc);
 705
 706                if (!old_crtc_state->active ||
 707                    !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
 708                        continue;
 709
 710                encoder = old_conn_state->best_encoder;
 711
 712                /* We shouldn't get this far if we didn't previously have
 713                 * an encoder.. but WARN_ON() rather than explode.
 714                 */
 715                if (WARN_ON(!encoder))
 716                        continue;
 717
 718                funcs = encoder->helper_private;
 719
 720                DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
 721                                 encoder->base.id, encoder->name);
 722
 723                /*
 724                 * Each encoder has at most one connector (since we always steal
 725                 * it away), so we won't call disable hooks twice.
 726                 */
 727                drm_bridge_disable(encoder->bridge);
 728
 729                /* Right function depends upon target state. */
 730                if (funcs) {
 731                        if (connector->state->crtc && funcs->prepare)
 732                                funcs->prepare(encoder);
 733                        else if (funcs->disable)
 734                                funcs->disable(encoder);
 735                        else if (funcs->dpms)
 736                                funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
 737                }
 738
 739                drm_bridge_post_disable(encoder->bridge);
 740        }
 741
 742        for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
 743                const struct drm_crtc_helper_funcs *funcs;
 744
 745                /* Shut down everything that needs a full modeset. */
 746                if (!drm_atomic_crtc_needs_modeset(crtc->state))
 747                        continue;
 748
 749                if (!old_crtc_state->active)
 750                        continue;
 751
 752                funcs = crtc->helper_private;
 753
 754                DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
 755                                 crtc->base.id, crtc->name);
 756
 757
 758                /* Right function depends upon target state. */
 759                if (crtc->state->enable && funcs->prepare)
 760                        funcs->prepare(crtc);
 761                else if (funcs->atomic_disable)
 762                        funcs->atomic_disable(crtc, old_crtc_state);
 763                else if (funcs->disable)
 764                        funcs->disable(crtc);
 765                else
 766                        funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
 767        }
 768}
 769
 770/**
 771 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
 772 * @dev: DRM device
 773 * @old_state: atomic state object with old state structures
 774 *
 775 * This function updates all the various legacy modeset state pointers in
 776 * connectors, encoders and crtcs. It also updates the timestamping constants
 777 * used for precise vblank timestamps by calling
 778 * drm_calc_timestamping_constants().
 779 *
 780 * Drivers can use this for building their own atomic commit if they don't have
 781 * a pure helper-based modeset implementation.
 782 */
 783void
 784drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
 785                                              struct drm_atomic_state *old_state)
 786{
 787        struct drm_connector *connector;
 788        struct drm_connector_state *old_conn_state;
 789        struct drm_crtc *crtc;
 790        struct drm_crtc_state *old_crtc_state;
 791        int i;
 792
 793        /* clear out existing links and update dpms */
 794        for_each_connector_in_state(old_state, connector, old_conn_state, i) {
 795                if (connector->encoder) {
 796                        WARN_ON(!connector->encoder->crtc);
 797
 798                        connector->encoder->crtc = NULL;
 799                        connector->encoder = NULL;
 800                }
 801
 802                crtc = connector->state->crtc;
 803                if ((!crtc && old_conn_state->crtc) ||
 804                    (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
 805                        struct drm_property *dpms_prop =
 806                                dev->mode_config.dpms_property;
 807                        int mode = DRM_MODE_DPMS_OFF;
 808
 809                        if (crtc && crtc->state->active)
 810                                mode = DRM_MODE_DPMS_ON;
 811
 812                        connector->dpms = mode;
 813                        drm_object_property_set_value(&connector->base,
 814                                                      dpms_prop, mode);
 815                }
 816        }
 817
 818        /* set new links */
 819        for_each_connector_in_state(old_state, connector, old_conn_state, i) {
 820                if (!connector->state->crtc)
 821                        continue;
 822
 823                if (WARN_ON(!connector->state->best_encoder))
 824                        continue;
 825
 826                connector->encoder = connector->state->best_encoder;
 827                connector->encoder->crtc = connector->state->crtc;
 828        }
 829
 830        /* set legacy state in the crtc structure */
 831        for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
 832                struct drm_plane *primary = crtc->primary;
 833
 834                crtc->mode = crtc->state->mode;
 835                crtc->enabled = crtc->state->enable;
 836
 837                if (drm_atomic_get_existing_plane_state(old_state, primary) &&
 838                    primary->state->crtc == crtc) {
 839                        crtc->x = primary->state->src_x >> 16;
 840                        crtc->y = primary->state->src_y >> 16;
 841                }
 842
 843                if (crtc->state->enable)
 844                        drm_calc_timestamping_constants(crtc,
 845                                                        &crtc->state->adjusted_mode);
 846        }
 847}
 848EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
 849
 850static void
 851crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
 852{
 853        struct drm_crtc *crtc;
 854        struct drm_crtc_state *old_crtc_state;
 855        struct drm_connector *connector;
 856        struct drm_connector_state *old_conn_state;
 857        int i;
 858
 859        for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
 860                const struct drm_crtc_helper_funcs *funcs;
 861
 862                if (!crtc->state->mode_changed)
 863                        continue;
 864
 865                funcs = crtc->helper_private;
 866
 867                if (crtc->state->enable && funcs->mode_set_nofb) {
 868                        DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
 869                                         crtc->base.id, crtc->name);
 870
 871                        funcs->mode_set_nofb(crtc);
 872                }
 873        }
 874
 875        for_each_connector_in_state(old_state, connector, old_conn_state, i) {
 876                const struct drm_encoder_helper_funcs *funcs;
 877                struct drm_crtc_state *new_crtc_state;
 878                struct drm_encoder *encoder;
 879                struct drm_display_mode *mode, *adjusted_mode;
 880
 881                if (!connector->state->best_encoder)
 882                        continue;
 883
 884                encoder = connector->state->best_encoder;
 885                funcs = encoder->helper_private;
 886                new_crtc_state = connector->state->crtc->state;
 887                mode = &new_crtc_state->mode;
 888                adjusted_mode = &new_crtc_state->adjusted_mode;
 889
 890                if (!new_crtc_state->mode_changed)
 891                        continue;
 892
 893                DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
 894                                 encoder->base.id, encoder->name);
 895
 896                /*
 897                 * Each encoder has at most one connector (since we always steal
 898                 * it away), so we won't call mode_set hooks twice.
 899                 */
 900                if (funcs && funcs->atomic_mode_set) {
 901                        funcs->atomic_mode_set(encoder, new_crtc_state,
 902                                               connector->state);
 903                } else if (funcs && funcs->mode_set) {
 904                        funcs->mode_set(encoder, mode, adjusted_mode);
 905                }
 906
 907                drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
 908        }
 909}
 910
 911/**
 912 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
 913 * @dev: DRM device
 914 * @old_state: atomic state object with old state structures
 915 *
 916 * This function shuts down all the outputs that need to be shut down and
 917 * prepares them (if required) with the new mode.
 918 *
 919 * For compatibility with legacy crtc helpers this should be called before
 920 * drm_atomic_helper_commit_planes(), which is what the default commit function
 921 * does. But drivers with different needs can group the modeset commits together
 922 * and do the plane commits at the end. This is useful for drivers doing runtime
 923 * PM since planes updates then only happen when the CRTC is actually enabled.
 924 */
 925void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
 926                                               struct drm_atomic_state *old_state)
 927{
 928        disable_outputs(dev, old_state);
 929
 930        drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
 931
 932        crtc_set_mode(dev, old_state);
 933}
 934EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
 935
 936/**
 937 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
 938 * @dev: DRM device
 939 * @old_state: atomic state object with old state structures
 940 *
 941 * This function enables all the outputs with the new configuration which had to
 942 * be turned off for the update.
 943 *
 944 * For compatibility with legacy crtc helpers this should be called after
 945 * drm_atomic_helper_commit_planes(), which is what the default commit function
 946 * does. But drivers with different needs can group the modeset commits together
 947 * and do the plane commits at the end. This is useful for drivers doing runtime
 948 * PM since planes updates then only happen when the CRTC is actually enabled.
 949 */
 950void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
 951                                              struct drm_atomic_state *old_state)
 952{
 953        struct drm_crtc *crtc;
 954        struct drm_crtc_state *old_crtc_state;
 955        struct drm_connector *connector;
 956        struct drm_connector_state *old_conn_state;
 957        int i;
 958
 959        for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
 960                const struct drm_crtc_helper_funcs *funcs;
 961
 962                /* Need to filter out CRTCs where only planes change. */
 963                if (!drm_atomic_crtc_needs_modeset(crtc->state))
 964                        continue;
 965
 966                if (!crtc->state->active)
 967                        continue;
 968
 969                funcs = crtc->helper_private;
 970
 971                if (crtc->state->enable) {
 972                        DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
 973                                         crtc->base.id, crtc->name);
 974
 975                        if (funcs->enable)
 976                                funcs->enable(crtc);
 977                        else
 978                                funcs->commit(crtc);
 979                }
 980        }
 981
 982        for_each_connector_in_state(old_state, connector, old_conn_state, i) {
 983                const struct drm_encoder_helper_funcs *funcs;
 984                struct drm_encoder *encoder;
 985
 986                if (!connector->state->best_encoder)
 987                        continue;
 988
 989                if (!connector->state->crtc->state->active ||
 990                    !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
 991                        continue;
 992
 993                encoder = connector->state->best_encoder;
 994                funcs = encoder->helper_private;
 995
 996                DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
 997                                 encoder->base.id, encoder->name);
 998
 999                /*
1000                 * Each encoder has at most one connector (since we always steal
1001                 * it away), so we won't call enable hooks twice.
1002                 */
1003                drm_bridge_pre_enable(encoder->bridge);
1004
1005                if (funcs) {
1006                        if (funcs->enable)
1007                                funcs->enable(encoder);
1008                        else if (funcs->commit)
1009                                funcs->commit(encoder);
1010                }
1011
1012                drm_bridge_enable(encoder->bridge);
1013        }
1014}
1015EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1016
1017/**
1018 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1019 * @dev: DRM device
1020 * @state: atomic state object with old state structures
1021 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1022 *      Otherwise @state is the old state.
1023 *
1024 * For implicit sync, driver should fish the exclusive fence out from the
1025 * incoming fb's and stash it in the drm_plane_state.  This is called after
1026 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1027 * just uses the atomic state to find the changed planes)
1028 *
1029 * Note that @pre_swap is needed since the point where we block for fences moves
1030 * around depending upon whether an atomic commit is blocking or
1031 * non-blocking. For async commit all waiting needs to happen after
1032 * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1033 * to wait **before** we do anything that can't be easily rolled back. That is
1034 * before we call drm_atomic_helper_swap_state().
1035 *
1036 * Returns zero if success or < 0 if dma_fence_wait() fails.
1037 */
1038int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1039                                      struct drm_atomic_state *state,
1040                                      bool pre_swap)
1041{
1042        struct drm_plane *plane;
1043        struct drm_plane_state *plane_state;
1044        int i, ret;
1045
1046        for_each_plane_in_state(state, plane, plane_state, i) {
1047                if (!pre_swap)
1048                        plane_state = plane->state;
1049
1050                if (!plane_state->fence)
1051                        continue;
1052
1053                WARN_ON(!plane_state->fb);
1054
1055                /*
1056                 * If waiting for fences pre-swap (ie: nonblock), userspace can
1057                 * still interrupt the operation. Instead of blocking until the
1058                 * timer expires, make the wait interruptible.
1059                 */
1060                ret = dma_fence_wait(plane_state->fence, pre_swap);
1061                if (ret)
1062                        return ret;
1063
1064                dma_fence_put(plane_state->fence);
1065                plane_state->fence = NULL;
1066        }
1067
1068        return 0;
1069}
1070EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1071
1072/**
1073 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1074 * @dev: DRM device
1075 * @old_state: atomic state object with old state structures
1076 *
1077 * Helper to, after atomic commit, wait for vblanks on all effected
1078 * crtcs (ie. before cleaning up old framebuffers using
1079 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1080 * framebuffers have actually changed to optimize for the legacy cursor and
1081 * plane update use-case.
1082 */
1083void
1084drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1085                struct drm_atomic_state *old_state)
1086{
1087        struct drm_crtc *crtc;
1088        struct drm_crtc_state *old_crtc_state;
1089        int i, ret;
1090        unsigned crtc_mask = 0;
1091
1092         /*
1093          * Legacy cursor ioctls are completely unsynced, and userspace
1094          * relies on that (by doing tons of cursor updates).
1095          */
1096        if (old_state->legacy_cursor_update)
1097                return;
1098
1099        for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1100                struct drm_crtc_state *new_crtc_state = crtc->state;
1101
1102                if (!new_crtc_state->active || !new_crtc_state->planes_changed)
1103                        continue;
1104
1105                ret = drm_crtc_vblank_get(crtc);
1106                if (ret != 0)
1107                        continue;
1108
1109                crtc_mask |= drm_crtc_mask(crtc);
1110                old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1111        }
1112
1113        for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1114                if (!(crtc_mask & drm_crtc_mask(crtc)))
1115                        continue;
1116
1117                ret = wait_event_timeout(dev->vblank[i].queue,
1118                                old_state->crtcs[i].last_vblank_count !=
1119                                        drm_crtc_vblank_count(crtc),
1120                                msecs_to_jiffies(50));
1121
1122                WARN(!ret, "[CRTC:%d] vblank wait timed out\n", crtc->base.id);
1123
1124                drm_crtc_vblank_put(crtc);
1125        }
1126}
1127EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1128
1129/**
1130 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1131 * @old_state: atomic state object with old state structures
1132 *
1133 * This is the default implementation for the
1134 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
1135 *
1136 * Note that the default ordering of how the various stages are called is to
1137 * match the legacy modeset helper library closest. One peculiarity of that is
1138 * that it doesn't mesh well with runtime PM at all.
1139 *
1140 * For drivers supporting runtime PM the recommended sequence is instead ::
1141 *
1142 *     drm_atomic_helper_commit_modeset_disables(dev, old_state);
1143 *
1144 *     drm_atomic_helper_commit_modeset_enables(dev, old_state);
1145 *
1146 *     drm_atomic_helper_commit_planes(dev, old_state,
1147 *                                     DRM_PLANE_COMMIT_ACTIVE_ONLY);
1148 *
1149 * for committing the atomic update to hardware.  See the kerneldoc entries for
1150 * these three functions for more details.
1151 */
1152void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1153{
1154        struct drm_device *dev = old_state->dev;
1155
1156        drm_atomic_helper_commit_modeset_disables(dev, old_state);
1157
1158        drm_atomic_helper_commit_planes(dev, old_state, 0);
1159
1160        drm_atomic_helper_commit_modeset_enables(dev, old_state);
1161
1162        drm_atomic_helper_commit_hw_done(old_state);
1163
1164        drm_atomic_helper_wait_for_vblanks(dev, old_state);
1165
1166        drm_atomic_helper_cleanup_planes(dev, old_state);
1167}
1168EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1169
1170static void commit_tail(struct drm_atomic_state *old_state)
1171{
1172        struct drm_device *dev = old_state->dev;
1173        struct drm_mode_config_helper_funcs *funcs;
1174
1175        funcs = dev->mode_config.helper_private;
1176
1177        drm_atomic_helper_wait_for_fences(dev, old_state, false);
1178
1179        drm_atomic_helper_wait_for_dependencies(old_state);
1180
1181        if (funcs && funcs->atomic_commit_tail)
1182                funcs->atomic_commit_tail(old_state);
1183        else
1184                drm_atomic_helper_commit_tail(old_state);
1185
1186        drm_atomic_helper_commit_cleanup_done(old_state);
1187
1188        drm_atomic_state_put(old_state);
1189}
1190
1191static void commit_work(struct work_struct *work)
1192{
1193        struct drm_atomic_state *state = container_of(work,
1194                                                      struct drm_atomic_state,
1195                                                      commit_work);
1196        commit_tail(state);
1197}
1198
1199/**
1200 * drm_atomic_helper_commit - commit validated state object
1201 * @dev: DRM device
1202 * @state: the driver state object
1203 * @nonblock: whether nonblocking behavior is requested.
1204 *
1205 * This function commits a with drm_atomic_helper_check() pre-validated state
1206 * object. This can still fail when e.g. the framebuffer reservation fails. This
1207 * function implements nonblocking commits, using
1208 * drm_atomic_helper_setup_commit() and related functions.
1209 *
1210 * Committing the actual hardware state is done through the
1211 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1212 * implementation drm_atomic_helper_commit_tail().
1213 *
1214 * RETURNS:
1215 * Zero for success or -errno.
1216 */
1217int drm_atomic_helper_commit(struct drm_device *dev,
1218                             struct drm_atomic_state *state,
1219                             bool nonblock)
1220{
1221        int ret;
1222
1223        ret = drm_atomic_helper_setup_commit(state, nonblock);
1224        if (ret)
1225                return ret;
1226
1227        INIT_WORK(&state->commit_work, commit_work);
1228
1229        ret = drm_atomic_helper_prepare_planes(dev, state);
1230        if (ret)
1231                return ret;
1232
1233        if (!nonblock) {
1234                ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1235                if (ret) {
1236                        drm_atomic_helper_cleanup_planes(dev, state);
1237                        return ret;
1238                }
1239        }
1240
1241        /*
1242         * This is the point of no return - everything below never fails except
1243         * when the hw goes bonghits. Which means we can commit the new state on
1244         * the software side now.
1245         */
1246
1247        drm_atomic_helper_swap_state(state, true);
1248
1249        /*
1250         * Everything below can be run asynchronously without the need to grab
1251         * any modeset locks at all under one condition: It must be guaranteed
1252         * that the asynchronous work has either been cancelled (if the driver
1253         * supports it, which at least requires that the framebuffers get
1254         * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1255         * before the new state gets committed on the software side with
1256         * drm_atomic_helper_swap_state().
1257         *
1258         * This scheme allows new atomic state updates to be prepared and
1259         * checked in parallel to the asynchronous completion of the previous
1260         * update. Which is important since compositors need to figure out the
1261         * composition of the next frame right after having submitted the
1262         * current layout.
1263         *
1264         * NOTE: Commit work has multiple phases, first hardware commit, then
1265         * cleanup. We want them to overlap, hence need system_unbound_wq to
1266         * make sure work items don't artifically stall on each another.
1267         */
1268
1269        drm_atomic_state_get(state);
1270        if (nonblock)
1271                queue_work(system_unbound_wq, &state->commit_work);
1272        else
1273                commit_tail(state);
1274
1275        return 0;
1276}
1277EXPORT_SYMBOL(drm_atomic_helper_commit);
1278
1279/**
1280 * DOC: implementing nonblocking commit
1281 *
1282 * Nonblocking atomic commits have to be implemented in the following sequence:
1283 *
1284 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1285 * which commit needs to call which can fail, so we want to run it first and
1286 * synchronously.
1287 *
1288 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1289 * might be affected the new state update. This can be done by either cancelling
1290 * or flushing the work items, depending upon whether the driver can deal with
1291 * cancelled updates. Note that it is important to ensure that the framebuffer
1292 * cleanup is still done when cancelling.
1293 *
1294 * Asynchronous workers need to have sufficient parallelism to be able to run
1295 * different atomic commits on different CRTCs in parallel. The simplest way to
1296 * achive this is by running them on the &system_unbound_wq work queue. Note
1297 * that drivers are not required to split up atomic commits and run an
1298 * individual commit in parallel - userspace is supposed to do that if it cares.
1299 * But it might be beneficial to do that for modesets, since those necessarily
1300 * must be done as one global operation, and enabling or disabling a CRTC can
1301 * take a long time. But even that is not required.
1302 *
1303 * 3. The software state is updated synchronously with
1304 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1305 * locks means concurrent callers never see inconsistent state. And doing this
1306 * while it's guaranteed that no relevant nonblocking worker runs means that
1307 * nonblocking workers do not need grab any locks. Actually they must not grab
1308 * locks, for otherwise the work flushing will deadlock.
1309 *
1310 * 4. Schedule a work item to do all subsequent steps, using the split-out
1311 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1312 * then cleaning up the framebuffers after the old framebuffer is no longer
1313 * being displayed.
1314 *
1315 * The above scheme is implemented in the atomic helper libraries in
1316 * drm_atomic_helper_commit() using a bunch of helper functions. See
1317 * drm_atomic_helper_setup_commit() for a starting point.
1318 */
1319
1320static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1321{
1322        struct drm_crtc_commit *commit, *stall_commit = NULL;
1323        bool completed = true;
1324        int i;
1325        long ret = 0;
1326
1327        spin_lock(&crtc->commit_lock);
1328        i = 0;
1329        list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1330                if (i == 0) {
1331                        completed = try_wait_for_completion(&commit->flip_done);
1332                        /* Userspace is not allowed to get ahead of the previous
1333                         * commit with nonblocking ones. */
1334                        if (!completed && nonblock) {
1335                                spin_unlock(&crtc->commit_lock);
1336                                return -EBUSY;
1337                        }
1338                } else if (i == 1) {
1339                        stall_commit = commit;
1340                        drm_crtc_commit_get(stall_commit);
1341                        break;
1342                }
1343
1344                i++;
1345        }
1346        spin_unlock(&crtc->commit_lock);
1347
1348        if (!stall_commit)
1349                return 0;
1350
1351        /* We don't want to let commits get ahead of cleanup work too much,
1352         * stalling on 2nd previous commit means triple-buffer won't ever stall.
1353         */
1354        ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1355                                                        10*HZ);
1356        if (ret == 0)
1357                DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1358                          crtc->base.id, crtc->name);
1359
1360        drm_crtc_commit_put(stall_commit);
1361
1362        return ret < 0 ? ret : 0;
1363}
1364
1365static void release_crtc_commit(struct completion *completion)
1366{
1367        struct drm_crtc_commit *commit = container_of(completion,
1368                                                      typeof(*commit),
1369                                                      flip_done);
1370
1371        drm_crtc_commit_put(commit);
1372}
1373
1374/**
1375 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1376 * @state: new modeset state to be committed
1377 * @nonblock: whether nonblocking behavior is requested.
1378 *
1379 * This function prepares @state to be used by the atomic helper's support for
1380 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1381 * should always call this function from their
1382 * &drm_mode_config_funcs.atomic_commit hook.
1383 *
1384 * To be able to use this support drivers need to use a few more helper
1385 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1386 * actually committing the hardware state, and for nonblocking commits this call
1387 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1388 * and it's stall parameter, for when a driver's commit hooks look at the
1389 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1390 *
1391 * Completion of the hardware commit step must be signalled using
1392 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1393 * to read or change any permanent software or hardware modeset state. The only
1394 * exception is state protected by other means than &drm_modeset_lock locks.
1395 * Only the free standing @state with pointers to the old state structures can
1396 * be inspected, e.g. to clean up old buffers using
1397 * drm_atomic_helper_cleanup_planes().
1398 *
1399 * At the very end, before cleaning up @state drivers must call
1400 * drm_atomic_helper_commit_cleanup_done().
1401 *
1402 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1403 * complete and esay-to-use default implementation of the atomic_commit() hook.
1404 *
1405 * The tracking of asynchronously executed and still pending commits is done
1406 * using the core structure &drm_crtc_commit.
1407 *
1408 * By default there's no need to clean up resources allocated by this function
1409 * explicitly: drm_atomic_state_default_clear() will take care of that
1410 * automatically.
1411 *
1412 * Returns:
1413 *
1414 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1415 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1416 */
1417int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1418                                   bool nonblock)
1419{
1420        struct drm_crtc *crtc;
1421        struct drm_crtc_state *crtc_state;
1422        struct drm_crtc_commit *commit;
1423        int i, ret;
1424
1425        for_each_crtc_in_state(state, crtc, crtc_state, i) {
1426                commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1427                if (!commit)
1428                        return -ENOMEM;
1429
1430                init_completion(&commit->flip_done);
1431                init_completion(&commit->hw_done);
1432                init_completion(&commit->cleanup_done);
1433                INIT_LIST_HEAD(&commit->commit_entry);
1434                kref_init(&commit->ref);
1435                commit->crtc = crtc;
1436
1437                state->crtcs[i].commit = commit;
1438
1439                ret = stall_checks(crtc, nonblock);
1440                if (ret)
1441                        return ret;
1442
1443                /* Drivers only send out events when at least either current or
1444                 * new CRTC state is active. Complete right away if everything
1445                 * stays off. */
1446                if (!crtc->state->active && !crtc_state->active) {
1447                        complete_all(&commit->flip_done);
1448                        continue;
1449                }
1450
1451                /* Legacy cursor updates are fully unsynced. */
1452                if (state->legacy_cursor_update) {
1453                        complete_all(&commit->flip_done);
1454                        continue;
1455                }
1456
1457                if (!crtc_state->event) {
1458                        commit->event = kzalloc(sizeof(*commit->event),
1459                                                GFP_KERNEL);
1460                        if (!commit->event)
1461                                return -ENOMEM;
1462
1463                        crtc_state->event = commit->event;
1464                }
1465
1466                crtc_state->event->base.completion = &commit->flip_done;
1467                crtc_state->event->base.completion_release = release_crtc_commit;
1468                drm_crtc_commit_get(commit);
1469        }
1470
1471        return 0;
1472}
1473EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1474
1475
1476static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1477{
1478        struct drm_crtc_commit *commit;
1479        int i = 0;
1480
1481        list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1482                /* skip the first entry, that's the current commit */
1483                if (i == 1)
1484                        return commit;
1485                i++;
1486        }
1487
1488        return NULL;
1489}
1490
1491/**
1492 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1493 * @old_state: atomic state object with old state structures
1494 *
1495 * This function waits for all preceeding commits that touch the same CRTC as
1496 * @old_state to both be committed to the hardware (as signalled by
1497 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1498 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
1499 *
1500 * This is part of the atomic helper support for nonblocking commits, see
1501 * drm_atomic_helper_setup_commit() for an overview.
1502 */
1503void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1504{
1505        struct drm_crtc *crtc;
1506        struct drm_crtc_state *crtc_state;
1507        struct drm_crtc_commit *commit;
1508        int i;
1509        long ret;
1510
1511        for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1512                spin_lock(&crtc->commit_lock);
1513                commit = preceeding_commit(crtc);
1514                if (commit)
1515                        drm_crtc_commit_get(commit);
1516                spin_unlock(&crtc->commit_lock);
1517
1518                if (!commit)
1519                        continue;
1520
1521                ret = wait_for_completion_timeout(&commit->hw_done,
1522                                                  10*HZ);
1523                if (ret == 0)
1524                        DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1525                                  crtc->base.id, crtc->name);
1526
1527                /* Currently no support for overwriting flips, hence
1528                 * stall for previous one to execute completely. */
1529                ret = wait_for_completion_timeout(&commit->flip_done,
1530                                                  10*HZ);
1531                if (ret == 0)
1532                        DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1533                                  crtc->base.id, crtc->name);
1534
1535                drm_crtc_commit_put(commit);
1536        }
1537}
1538EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1539
1540/**
1541 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1542 * @old_state: atomic state object with old state structures
1543 *
1544 * This function is used to signal completion of the hardware commit step. After
1545 * this step the driver is not allowed to read or change any permanent software
1546 * or hardware modeset state. The only exception is state protected by other
1547 * means than &drm_modeset_lock locks.
1548 *
1549 * Drivers should try to postpone any expensive or delayed cleanup work after
1550 * this function is called.
1551 *
1552 * This is part of the atomic helper support for nonblocking commits, see
1553 * drm_atomic_helper_setup_commit() for an overview.
1554 */
1555void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
1556{
1557        struct drm_crtc *crtc;
1558        struct drm_crtc_state *crtc_state;
1559        struct drm_crtc_commit *commit;
1560        int i;
1561
1562        for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1563                commit = old_state->crtcs[i].commit;
1564                if (!commit)
1565                        continue;
1566
1567                /* backend must have consumed any event by now */
1568                WARN_ON(crtc->state->event);
1569                spin_lock(&crtc->commit_lock);
1570                complete_all(&commit->hw_done);
1571                spin_unlock(&crtc->commit_lock);
1572        }
1573}
1574EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1575
1576/**
1577 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1578 * @old_state: atomic state object with old state structures
1579 *
1580 * This signals completion of the atomic update @old_state, including any
1581 * cleanup work. If used, it must be called right before calling
1582 * drm_atomic_state_put().
1583 *
1584 * This is part of the atomic helper support for nonblocking commits, see
1585 * drm_atomic_helper_setup_commit() for an overview.
1586 */
1587void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
1588{
1589        struct drm_crtc *crtc;
1590        struct drm_crtc_state *crtc_state;
1591        struct drm_crtc_commit *commit;
1592        int i;
1593        long ret;
1594
1595        for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1596                commit = old_state->crtcs[i].commit;
1597                if (WARN_ON(!commit))
1598                        continue;
1599
1600                spin_lock(&crtc->commit_lock);
1601                complete_all(&commit->cleanup_done);
1602                WARN_ON(!try_wait_for_completion(&commit->hw_done));
1603
1604                /* commit_list borrows our reference, need to remove before we
1605                 * clean up our drm_atomic_state. But only after it actually
1606                 * completed, otherwise subsequent commits won't stall properly. */
1607                if (try_wait_for_completion(&commit->flip_done))
1608                        goto del_commit;
1609
1610                spin_unlock(&crtc->commit_lock);
1611
1612                /* We must wait for the vblank event to signal our completion
1613                 * before releasing our reference, since the vblank work does
1614                 * not hold a reference of its own. */
1615                ret = wait_for_completion_timeout(&commit->flip_done,
1616                                                  10*HZ);
1617                if (ret == 0)
1618                        DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1619                                  crtc->base.id, crtc->name);
1620
1621                spin_lock(&crtc->commit_lock);
1622del_commit:
1623                list_del(&commit->commit_entry);
1624                spin_unlock(&crtc->commit_lock);
1625        }
1626}
1627EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1628
1629/**
1630 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1631 * @dev: DRM device
1632 * @state: atomic state object with new state structures
1633 *
1634 * This function prepares plane state, specifically framebuffers, for the new
1635 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1636 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1637 * any already successfully prepared framebuffer.
1638 *
1639 * Returns:
1640 * 0 on success, negative error code on failure.
1641 */
1642int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1643                                     struct drm_atomic_state *state)
1644{
1645        struct drm_plane *plane;
1646        struct drm_plane_state *plane_state;
1647        int ret, i, j;
1648
1649        for_each_plane_in_state(state, plane, plane_state, i) {
1650                const struct drm_plane_helper_funcs *funcs;
1651
1652                funcs = plane->helper_private;
1653
1654                if (funcs->prepare_fb) {
1655                        ret = funcs->prepare_fb(plane, plane_state);
1656                        if (ret)
1657                                goto fail;
1658                }
1659        }
1660
1661        return 0;
1662
1663fail:
1664        for_each_plane_in_state(state, plane, plane_state, j) {
1665                const struct drm_plane_helper_funcs *funcs;
1666
1667                if (j >= i)
1668                        continue;
1669
1670                funcs = plane->helper_private;
1671
1672                if (funcs->cleanup_fb)
1673                        funcs->cleanup_fb(plane, plane_state);
1674        }
1675
1676        return ret;
1677}
1678EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1679
1680static bool plane_crtc_active(const struct drm_plane_state *state)
1681{
1682        return state->crtc && state->crtc->state->active;
1683}
1684
1685/**
1686 * drm_atomic_helper_commit_planes - commit plane state
1687 * @dev: DRM device
1688 * @old_state: atomic state object with old state structures
1689 * @flags: flags for committing plane state
1690 *
1691 * This function commits the new plane state using the plane and atomic helper
1692 * functions for planes and crtcs. It assumes that the atomic state has already
1693 * been pushed into the relevant object state pointers, since this step can no
1694 * longer fail.
1695 *
1696 * It still requires the global state object @old_state to know which planes and
1697 * crtcs need to be updated though.
1698 *
1699 * Note that this function does all plane updates across all CRTCs in one step.
1700 * If the hardware can't support this approach look at
1701 * drm_atomic_helper_commit_planes_on_crtc() instead.
1702 *
1703 * Plane parameters can be updated by applications while the associated CRTC is
1704 * disabled. The DRM/KMS core will store the parameters in the plane state,
1705 * which will be available to the driver when the CRTC is turned on. As a result
1706 * most drivers don't need to be immediately notified of plane updates for a
1707 * disabled CRTC.
1708 *
1709 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1710 * @flags in order not to receive plane update notifications related to a
1711 * disabled CRTC. This avoids the need to manually ignore plane updates in
1712 * driver code when the driver and/or hardware can't or just don't need to deal
1713 * with updates on disabled CRTCs, for example when supporting runtime PM.
1714 *
1715 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1716 * display controllers require to disable a CRTC's planes when the CRTC is
1717 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1718 * call for a plane if the CRTC of the old plane state needs a modesetting
1719 * operation. Of course, the drivers need to disable the planes in their CRTC
1720 * disable callbacks since no one else would do that.
1721 *
1722 * The drm_atomic_helper_commit() default implementation doesn't set the
1723 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1724 * This should not be copied blindly by drivers.
1725 */
1726void drm_atomic_helper_commit_planes(struct drm_device *dev,
1727                                     struct drm_atomic_state *old_state,
1728                                     uint32_t flags)
1729{
1730        struct drm_crtc *crtc;
1731        struct drm_crtc_state *old_crtc_state;
1732        struct drm_plane *plane;
1733        struct drm_plane_state *old_plane_state;
1734        int i;
1735        bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1736        bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
1737
1738        for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1739                const struct drm_crtc_helper_funcs *funcs;
1740
1741                funcs = crtc->helper_private;
1742
1743                if (!funcs || !funcs->atomic_begin)
1744                        continue;
1745
1746                if (active_only && !crtc->state->active)
1747                        continue;
1748
1749                funcs->atomic_begin(crtc, old_crtc_state);
1750        }
1751
1752        for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1753                const struct drm_plane_helper_funcs *funcs;
1754                bool disabling;
1755
1756                funcs = plane->helper_private;
1757
1758                if (!funcs)
1759                        continue;
1760
1761                disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1762
1763                if (active_only) {
1764                        /*
1765                         * Skip planes related to inactive CRTCs. If the plane
1766                         * is enabled use the state of the current CRTC. If the
1767                         * plane is being disabled use the state of the old
1768                         * CRTC to avoid skipping planes being disabled on an
1769                         * active CRTC.
1770                         */
1771                        if (!disabling && !plane_crtc_active(plane->state))
1772                                continue;
1773                        if (disabling && !plane_crtc_active(old_plane_state))
1774                                continue;
1775                }
1776
1777                /*
1778                 * Special-case disabling the plane if drivers support it.
1779                 */
1780                if (disabling && funcs->atomic_disable) {
1781                        struct drm_crtc_state *crtc_state;
1782
1783                        crtc_state = old_plane_state->crtc->state;
1784
1785                        if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1786                            no_disable)
1787                                continue;
1788
1789                        funcs->atomic_disable(plane, old_plane_state);
1790                } else if (plane->state->crtc || disabling) {
1791                        funcs->atomic_update(plane, old_plane_state);
1792                }
1793        }
1794
1795        for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1796                const struct drm_crtc_helper_funcs *funcs;
1797
1798                funcs = crtc->helper_private;
1799
1800                if (!funcs || !funcs->atomic_flush)
1801                        continue;
1802
1803                if (active_only && !crtc->state->active)
1804                        continue;
1805
1806                funcs->atomic_flush(crtc, old_crtc_state);
1807        }
1808}
1809EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1810
1811/**
1812 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1813 * @old_crtc_state: atomic state object with the old crtc state
1814 *
1815 * This function commits the new plane state using the plane and atomic helper
1816 * functions for planes on the specific crtc. It assumes that the atomic state
1817 * has already been pushed into the relevant object state pointers, since this
1818 * step can no longer fail.
1819 *
1820 * This function is useful when plane updates should be done crtc-by-crtc
1821 * instead of one global step like drm_atomic_helper_commit_planes() does.
1822 *
1823 * This function can only be savely used when planes are not allowed to move
1824 * between different CRTCs because this function doesn't handle inter-CRTC
1825 * depencies. Callers need to ensure that either no such depencies exist,
1826 * resolve them through ordering of commit calls or through some other means.
1827 */
1828void
1829drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1830{
1831        const struct drm_crtc_helper_funcs *crtc_funcs;
1832        struct drm_crtc *crtc = old_crtc_state->crtc;
1833        struct drm_atomic_state *old_state = old_crtc_state->state;
1834        struct drm_plane *plane;
1835        unsigned plane_mask;
1836
1837        plane_mask = old_crtc_state->plane_mask;
1838        plane_mask |= crtc->state->plane_mask;
1839
1840        crtc_funcs = crtc->helper_private;
1841        if (crtc_funcs && crtc_funcs->atomic_begin)
1842                crtc_funcs->atomic_begin(crtc, old_crtc_state);
1843
1844        drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1845                struct drm_plane_state *old_plane_state =
1846                        drm_atomic_get_existing_plane_state(old_state, plane);
1847                const struct drm_plane_helper_funcs *plane_funcs;
1848
1849                plane_funcs = plane->helper_private;
1850
1851                if (!old_plane_state || !plane_funcs)
1852                        continue;
1853
1854                WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1855
1856                if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1857                    plane_funcs->atomic_disable)
1858                        plane_funcs->atomic_disable(plane, old_plane_state);
1859                else if (plane->state->crtc ||
1860                         drm_atomic_plane_disabling(plane, old_plane_state))
1861                        plane_funcs->atomic_update(plane, old_plane_state);
1862        }
1863
1864        if (crtc_funcs && crtc_funcs->atomic_flush)
1865                crtc_funcs->atomic_flush(crtc, old_crtc_state);
1866}
1867EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1868
1869/**
1870 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1871 * @old_crtc_state: atomic state object with the old CRTC state
1872 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1873 *
1874 * Disables all planes associated with the given CRTC. This can be
1875 * used for instance in the CRTC helper atomic_disable callback to disable
1876 * all planes.
1877 *
1878 * If the atomic-parameter is set the function calls the CRTC's
1879 * atomic_begin hook before and atomic_flush hook after disabling the
1880 * planes.
1881 *
1882 * It is a bug to call this function without having implemented the
1883 * &drm_plane_helper_funcs.atomic_disable plane hook.
1884 */
1885void
1886drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1887                                         bool atomic)
1888{
1889        struct drm_crtc *crtc = old_crtc_state->crtc;
1890        const struct drm_crtc_helper_funcs *crtc_funcs =
1891                crtc->helper_private;
1892        struct drm_plane *plane;
1893
1894        if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1895                crtc_funcs->atomic_begin(crtc, NULL);
1896
1897        drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
1898                const struct drm_plane_helper_funcs *plane_funcs =
1899                        plane->helper_private;
1900
1901                if (!plane_funcs)
1902                        continue;
1903
1904                WARN_ON(!plane_funcs->atomic_disable);
1905                if (plane_funcs->atomic_disable)
1906                        plane_funcs->atomic_disable(plane, NULL);
1907        }
1908
1909        if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1910                crtc_funcs->atomic_flush(crtc, NULL);
1911}
1912EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1913
1914/**
1915 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1916 * @dev: DRM device
1917 * @old_state: atomic state object with old state structures
1918 *
1919 * This function cleans up plane state, specifically framebuffers, from the old
1920 * configuration. Hence the old configuration must be perserved in @old_state to
1921 * be able to call this function.
1922 *
1923 * This function must also be called on the new state when the atomic update
1924 * fails at any point after calling drm_atomic_helper_prepare_planes().
1925 */
1926void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1927                                      struct drm_atomic_state *old_state)
1928{
1929        struct drm_plane *plane;
1930        struct drm_plane_state *plane_state;
1931        int i;
1932
1933        for_each_plane_in_state(old_state, plane, plane_state, i) {
1934                const struct drm_plane_helper_funcs *funcs;
1935
1936                funcs = plane->helper_private;
1937
1938                if (funcs->cleanup_fb)
1939                        funcs->cleanup_fb(plane, plane_state);
1940        }
1941}
1942EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1943
1944/**
1945 * drm_atomic_helper_swap_state - store atomic state into current sw state
1946 * @state: atomic state
1947 * @stall: stall for proceeding commits
1948 *
1949 * This function stores the atomic state into the current state pointers in all
1950 * driver objects. It should be called after all failing steps have been done
1951 * and succeeded, but before the actual hardware state is committed.
1952 *
1953 * For cleanup and error recovery the current state for all changed objects will
1954 * be swaped into @state.
1955 *
1956 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1957 *
1958 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1959 *
1960 * 2. Do any other steps that might fail.
1961 *
1962 * 3. Put the staged state into the current state pointers with this function.
1963 *
1964 * 4. Actually commit the hardware state.
1965 *
1966 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1967 * contains the old state. Also do any other cleanup required with that state.
1968 *
1969 * @stall must be set when nonblocking commits for this driver directly access
1970 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
1971 * the current atomic helpers this is almost always the case, since the helpers
1972 * don't pass the right state structures to the callbacks.
1973 */
1974void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1975                                  bool stall)
1976{
1977        int i;
1978        long ret;
1979        struct drm_connector *connector;
1980        struct drm_connector_state *conn_state;
1981        struct drm_crtc *crtc;
1982        struct drm_crtc_state *crtc_state;
1983        struct drm_plane *plane;
1984        struct drm_plane_state *plane_state;
1985        struct drm_crtc_commit *commit;
1986
1987        if (stall) {
1988                for_each_crtc_in_state(state, crtc, crtc_state, i) {
1989                        spin_lock(&crtc->commit_lock);
1990                        commit = list_first_entry_or_null(&crtc->commit_list,
1991                                        struct drm_crtc_commit, commit_entry);
1992                        if (commit)
1993                                drm_crtc_commit_get(commit);
1994                        spin_unlock(&crtc->commit_lock);
1995
1996                        if (!commit)
1997                                continue;
1998
1999                        ret = wait_for_completion_timeout(&commit->hw_done,
2000                                                          10*HZ);
2001                        if (ret == 0)
2002                                DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2003                                          crtc->base.id, crtc->name);
2004                        drm_crtc_commit_put(commit);
2005                }
2006        }
2007
2008        for_each_connector_in_state(state, connector, conn_state, i) {
2009                connector->state->state = state;
2010                swap(state->connectors[i].state, connector->state);
2011                connector->state->state = NULL;
2012        }
2013
2014        for_each_crtc_in_state(state, crtc, crtc_state, i) {
2015                crtc->state->state = state;
2016                swap(state->crtcs[i].state, crtc->state);
2017                crtc->state->state = NULL;
2018
2019                if (state->crtcs[i].commit) {
2020                        spin_lock(&crtc->commit_lock);
2021                        list_add(&state->crtcs[i].commit->commit_entry,
2022                                 &crtc->commit_list);
2023                        spin_unlock(&crtc->commit_lock);
2024
2025                        state->crtcs[i].commit->event = NULL;
2026                }
2027        }
2028
2029        for_each_plane_in_state(state, plane, plane_state, i) {
2030                plane->state->state = state;
2031                swap(state->planes[i].state, plane->state);
2032                plane->state->state = NULL;
2033        }
2034}
2035EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2036
2037/**
2038 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2039 * @plane: plane object to update
2040 * @crtc: owning CRTC of owning plane
2041 * @fb: framebuffer to flip onto plane
2042 * @crtc_x: x offset of primary plane on crtc
2043 * @crtc_y: y offset of primary plane on crtc
2044 * @crtc_w: width of primary plane rectangle on crtc
2045 * @crtc_h: height of primary plane rectangle on crtc
2046 * @src_x: x offset of @fb for panning
2047 * @src_y: y offset of @fb for panning
2048 * @src_w: width of source rectangle in @fb
2049 * @src_h: height of source rectangle in @fb
2050 *
2051 * Provides a default plane update handler using the atomic driver interface.
2052 *
2053 * RETURNS:
2054 * Zero on success, error code on failure
2055 */
2056int drm_atomic_helper_update_plane(struct drm_plane *plane,
2057                                   struct drm_crtc *crtc,
2058                                   struct drm_framebuffer *fb,
2059                                   int crtc_x, int crtc_y,
2060                                   unsigned int crtc_w, unsigned int crtc_h,
2061                                   uint32_t src_x, uint32_t src_y,
2062                                   uint32_t src_w, uint32_t src_h)
2063{
2064        struct drm_atomic_state *state;
2065        struct drm_plane_state *plane_state;
2066        int ret = 0;
2067
2068        state = drm_atomic_state_alloc(plane->dev);
2069        if (!state)
2070                return -ENOMEM;
2071
2072        state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2073retry:
2074        plane_state = drm_atomic_get_plane_state(state, plane);
2075        if (IS_ERR(plane_state)) {
2076                ret = PTR_ERR(plane_state);
2077                goto fail;
2078        }
2079
2080        ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2081        if (ret != 0)
2082                goto fail;
2083        drm_atomic_set_fb_for_plane(plane_state, fb);
2084        plane_state->crtc_x = crtc_x;
2085        plane_state->crtc_y = crtc_y;
2086        plane_state->crtc_w = crtc_w;
2087        plane_state->crtc_h = crtc_h;
2088        plane_state->src_x = src_x;
2089        plane_state->src_y = src_y;
2090        plane_state->src_w = src_w;
2091        plane_state->src_h = src_h;
2092
2093        if (plane == crtc->cursor)
2094                state->legacy_cursor_update = true;
2095
2096        ret = drm_atomic_commit(state);
2097fail:
2098        if (ret == -EDEADLK)
2099                goto backoff;
2100
2101        drm_atomic_state_put(state);
2102        return ret;
2103
2104backoff:
2105        drm_atomic_state_clear(state);
2106        drm_atomic_legacy_backoff(state);
2107
2108        /*
2109         * Someone might have exchanged the framebuffer while we dropped locks
2110         * in the backoff code. We need to fix up the fb refcount tracking the
2111         * core does for us.
2112         */
2113        plane->old_fb = plane->fb;
2114
2115        goto retry;
2116}
2117EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2118
2119/**
2120 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2121 * @plane: plane to disable
2122 *
2123 * Provides a default plane disable handler using the atomic driver interface.
2124 *
2125 * RETURNS:
2126 * Zero on success, error code on failure
2127 */
2128int drm_atomic_helper_disable_plane(struct drm_plane *plane)
2129{
2130        struct drm_atomic_state *state;
2131        struct drm_plane_state *plane_state;
2132        int ret = 0;
2133
2134        /*
2135         * FIXME: Without plane->crtc set we can't get at the implicit legacy
2136         * acquire context. The real fix will be to wire the acquire ctx through
2137         * everywhere we need it, but meanwhile prevent chaos by just skipping
2138         * this noop. The critical case is the cursor ioctls which a) only grab
2139         * crtc/cursor-plane locks (so we need the crtc to get at the right
2140         * acquire context) and b) can try to disable the plane multiple times.
2141         */
2142        if (!plane->crtc)
2143                return 0;
2144
2145        state = drm_atomic_state_alloc(plane->dev);
2146        if (!state)
2147                return -ENOMEM;
2148
2149        state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
2150retry:
2151        plane_state = drm_atomic_get_plane_state(state, plane);
2152        if (IS_ERR(plane_state)) {
2153                ret = PTR_ERR(plane_state);
2154                goto fail;
2155        }
2156
2157        if (plane_state->crtc && (plane == plane->crtc->cursor))
2158                plane_state->state->legacy_cursor_update = true;
2159
2160        ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2161        if (ret != 0)
2162                goto fail;
2163
2164        ret = drm_atomic_commit(state);
2165fail:
2166        if (ret == -EDEADLK)
2167                goto backoff;
2168
2169        drm_atomic_state_put(state);
2170        return ret;
2171
2172backoff:
2173        drm_atomic_state_clear(state);
2174        drm_atomic_legacy_backoff(state);
2175
2176        /*
2177         * Someone might have exchanged the framebuffer while we dropped locks
2178         * in the backoff code. We need to fix up the fb refcount tracking the
2179         * core does for us.
2180         */
2181        plane->old_fb = plane->fb;
2182
2183        goto retry;
2184}
2185EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2186
2187/* just used from fb-helper and atomic-helper: */
2188int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2189                struct drm_plane_state *plane_state)
2190{
2191        int ret;
2192
2193        ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2194        if (ret != 0)
2195                return ret;
2196
2197        drm_atomic_set_fb_for_plane(plane_state, NULL);
2198        plane_state->crtc_x = 0;
2199        plane_state->crtc_y = 0;
2200        plane_state->crtc_w = 0;
2201        plane_state->crtc_h = 0;
2202        plane_state->src_x = 0;
2203        plane_state->src_y = 0;
2204        plane_state->src_w = 0;
2205        plane_state->src_h = 0;
2206
2207        return 0;
2208}
2209
2210static int update_output_state(struct drm_atomic_state *state,
2211                               struct drm_mode_set *set)
2212{
2213        struct drm_device *dev = set->crtc->dev;
2214        struct drm_crtc *crtc;
2215        struct drm_crtc_state *crtc_state;
2216        struct drm_connector *connector;
2217        struct drm_connector_state *conn_state;
2218        int ret, i;
2219
2220        ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2221                               state->acquire_ctx);
2222        if (ret)
2223                return ret;
2224
2225        /* First disable all connectors on the target crtc. */
2226        ret = drm_atomic_add_affected_connectors(state, set->crtc);
2227        if (ret)
2228                return ret;
2229
2230        for_each_connector_in_state(state, connector, conn_state, i) {
2231                if (conn_state->crtc == set->crtc) {
2232                        ret = drm_atomic_set_crtc_for_connector(conn_state,
2233                                                                NULL);
2234                        if (ret)
2235                                return ret;
2236                }
2237        }
2238
2239        /* Then set all connectors from set->connectors on the target crtc */
2240        for (i = 0; i < set->num_connectors; i++) {
2241                conn_state = drm_atomic_get_connector_state(state,
2242                                                            set->connectors[i]);
2243                if (IS_ERR(conn_state))
2244                        return PTR_ERR(conn_state);
2245
2246                ret = drm_atomic_set_crtc_for_connector(conn_state,
2247                                                        set->crtc);
2248                if (ret)
2249                        return ret;
2250        }
2251
2252        for_each_crtc_in_state(state, crtc, crtc_state, i) {
2253                /* Don't update ->enable for the CRTC in the set_config request,
2254                 * since a mismatch would indicate a bug in the upper layers.
2255                 * The actual modeset code later on will catch any
2256                 * inconsistencies here. */
2257                if (crtc == set->crtc)
2258                        continue;
2259
2260                if (!crtc_state->connector_mask) {
2261                        ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
2262                                                                NULL);
2263                        if (ret < 0)
2264                                return ret;
2265
2266                        crtc_state->active = false;
2267                }
2268        }
2269
2270        return 0;
2271}
2272
2273/**
2274 * drm_atomic_helper_set_config - set a new config from userspace
2275 * @set: mode set configuration
2276 *
2277 * Provides a default crtc set_config handler using the atomic driver interface.
2278 *
2279 * Returns:
2280 * Returns 0 on success, negative errno numbers on failure.
2281 */
2282int drm_atomic_helper_set_config(struct drm_mode_set *set)
2283{
2284        struct drm_atomic_state *state;
2285        struct drm_crtc *crtc = set->crtc;
2286        int ret = 0;
2287
2288        state = drm_atomic_state_alloc(crtc->dev);
2289        if (!state)
2290                return -ENOMEM;
2291
2292        state->legacy_set_config = true;
2293        state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2294retry:
2295        ret = __drm_atomic_helper_set_config(set, state);
2296        if (ret != 0)
2297                goto fail;
2298
2299        ret = drm_atomic_commit(state);
2300fail:
2301        if (ret == -EDEADLK)
2302                goto backoff;
2303
2304        drm_atomic_state_put(state);
2305        return ret;
2306
2307backoff:
2308        drm_atomic_state_clear(state);
2309        drm_atomic_legacy_backoff(state);
2310
2311        /*
2312         * Someone might have exchanged the framebuffer while we dropped locks
2313         * in the backoff code. We need to fix up the fb refcount tracking the
2314         * core does for us.
2315         */
2316        crtc->primary->old_fb = crtc->primary->fb;
2317
2318        goto retry;
2319}
2320EXPORT_SYMBOL(drm_atomic_helper_set_config);
2321
2322/* just used from fb-helper and atomic-helper: */
2323int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2324                struct drm_atomic_state *state)
2325{
2326        struct drm_crtc_state *crtc_state;
2327        struct drm_plane_state *primary_state;
2328        struct drm_crtc *crtc = set->crtc;
2329        int hdisplay, vdisplay;
2330        int ret;
2331
2332        crtc_state = drm_atomic_get_crtc_state(state, crtc);
2333        if (IS_ERR(crtc_state))
2334                return PTR_ERR(crtc_state);
2335
2336        primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2337        if (IS_ERR(primary_state))
2338                return PTR_ERR(primary_state);
2339
2340        if (!set->mode) {
2341                WARN_ON(set->fb);
2342                WARN_ON(set->num_connectors);
2343
2344                ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2345                if (ret != 0)
2346                        return ret;
2347
2348                crtc_state->active = false;
2349
2350                ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2351                if (ret != 0)
2352                        return ret;
2353
2354                drm_atomic_set_fb_for_plane(primary_state, NULL);
2355
2356                goto commit;
2357        }
2358
2359        WARN_ON(!set->fb);
2360        WARN_ON(!set->num_connectors);
2361
2362        ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2363        if (ret != 0)
2364                return ret;
2365
2366        crtc_state->active = true;
2367
2368        ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2369        if (ret != 0)
2370                return ret;
2371
2372        drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2373
2374        drm_atomic_set_fb_for_plane(primary_state, set->fb);
2375        primary_state->crtc_x = 0;
2376        primary_state->crtc_y = 0;
2377        primary_state->crtc_w = hdisplay;
2378        primary_state->crtc_h = vdisplay;
2379        primary_state->src_x = set->x << 16;
2380        primary_state->src_y = set->y << 16;
2381        if (drm_rotation_90_or_270(primary_state->rotation)) {
2382                primary_state->src_w = vdisplay << 16;
2383                primary_state->src_h = hdisplay << 16;
2384        } else {
2385                primary_state->src_w = hdisplay << 16;
2386                primary_state->src_h = vdisplay << 16;
2387        }
2388
2389commit:
2390        ret = update_output_state(state, set);
2391        if (ret)
2392                return ret;
2393
2394        return 0;
2395}
2396
2397/**
2398 * drm_atomic_helper_disable_all - disable all currently active outputs
2399 * @dev: DRM device
2400 * @ctx: lock acquisition context
2401 *
2402 * Loops through all connectors, finding those that aren't turned off and then
2403 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2404 * that they are connected to.
2405 *
2406 * This is used for example in suspend/resume to disable all currently active
2407 * functions when suspending.
2408 *
2409 * Note that if callers haven't already acquired all modeset locks this might
2410 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2411 *
2412 * Returns:
2413 * 0 on success or a negative error code on failure.
2414 *
2415 * See also:
2416 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2417 */
2418int drm_atomic_helper_disable_all(struct drm_device *dev,
2419                                  struct drm_modeset_acquire_ctx *ctx)
2420{
2421        struct drm_atomic_state *state;
2422        struct drm_connector *conn;
2423        struct drm_connector_list_iter conn_iter;
2424        int err;
2425
2426        state = drm_atomic_state_alloc(dev);
2427        if (!state)
2428                return -ENOMEM;
2429
2430        state->acquire_ctx = ctx;
2431
2432        drm_connector_list_iter_get(dev, &conn_iter);
2433        drm_for_each_connector_iter(conn, &conn_iter) {
2434                struct drm_crtc *crtc = conn->state->crtc;
2435                struct drm_crtc_state *crtc_state;
2436
2437                if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
2438                        continue;
2439
2440                crtc_state = drm_atomic_get_crtc_state(state, crtc);
2441                if (IS_ERR(crtc_state)) {
2442                        err = PTR_ERR(crtc_state);
2443                        goto free;
2444                }
2445
2446                crtc_state->active = false;
2447        }
2448
2449        err = drm_atomic_commit(state);
2450free:
2451        drm_connector_list_iter_put(&conn_iter);
2452        drm_atomic_state_put(state);
2453        return err;
2454}
2455EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2456
2457/**
2458 * drm_atomic_helper_suspend - subsystem-level suspend helper
2459 * @dev: DRM device
2460 *
2461 * Duplicates the current atomic state, disables all active outputs and then
2462 * returns a pointer to the original atomic state to the caller. Drivers can
2463 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2464 * restore the output configuration that was active at the time the system
2465 * entered suspend.
2466 *
2467 * Note that it is potentially unsafe to use this. The atomic state object
2468 * returned by this function is assumed to be persistent. Drivers must ensure
2469 * that this holds true. Before calling this function, drivers must make sure
2470 * to suspend fbdev emulation so that nothing can be using the device.
2471 *
2472 * Returns:
2473 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2474 * encoded error code on failure. Drivers should store the returned atomic
2475 * state object and pass it to the drm_atomic_helper_resume() helper upon
2476 * resume.
2477 *
2478 * See also:
2479 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2480 * drm_atomic_helper_resume()
2481 */
2482struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2483{
2484        struct drm_modeset_acquire_ctx ctx;
2485        struct drm_atomic_state *state;
2486        int err;
2487
2488        drm_modeset_acquire_init(&ctx, 0);
2489
2490retry:
2491        err = drm_modeset_lock_all_ctx(dev, &ctx);
2492        if (err < 0) {
2493                state = ERR_PTR(err);
2494                goto unlock;
2495        }
2496
2497        state = drm_atomic_helper_duplicate_state(dev, &ctx);
2498        if (IS_ERR(state))
2499                goto unlock;
2500
2501        err = drm_atomic_helper_disable_all(dev, &ctx);
2502        if (err < 0) {
2503                drm_atomic_state_put(state);
2504                state = ERR_PTR(err);
2505                goto unlock;
2506        }
2507
2508unlock:
2509        if (PTR_ERR(state) == -EDEADLK) {
2510                drm_modeset_backoff(&ctx);
2511                goto retry;
2512        }
2513
2514        drm_modeset_drop_locks(&ctx);
2515        drm_modeset_acquire_fini(&ctx);
2516        return state;
2517}
2518EXPORT_SYMBOL(drm_atomic_helper_suspend);
2519
2520/**
2521 * drm_atomic_helper_resume - subsystem-level resume helper
2522 * @dev: DRM device
2523 * @state: atomic state to resume to
2524 *
2525 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2526 * grabs all modeset locks and commits the atomic state object. This can be
2527 * used in conjunction with the drm_atomic_helper_suspend() helper to
2528 * implement suspend/resume for drivers that support atomic mode-setting.
2529 *
2530 * Returns:
2531 * 0 on success or a negative error code on failure.
2532 *
2533 * See also:
2534 * drm_atomic_helper_suspend()
2535 */
2536int drm_atomic_helper_resume(struct drm_device *dev,
2537                             struct drm_atomic_state *state)
2538{
2539        struct drm_mode_config *config = &dev->mode_config;
2540        int err;
2541
2542        drm_mode_config_reset(dev);
2543        drm_modeset_lock_all(dev);
2544        state->acquire_ctx = config->acquire_ctx;
2545        err = drm_atomic_commit(state);
2546        drm_modeset_unlock_all(dev);
2547
2548        return err;
2549}
2550EXPORT_SYMBOL(drm_atomic_helper_resume);
2551
2552/**
2553 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2554 * @crtc: DRM crtc
2555 * @property: DRM property
2556 * @val: value of property
2557 *
2558 * Provides a default crtc set_property handler using the atomic driver
2559 * interface.
2560 *
2561 * RETURNS:
2562 * Zero on success, error code on failure
2563 */
2564int
2565drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2566                                    struct drm_property *property,
2567                                    uint64_t val)
2568{
2569        struct drm_atomic_state *state;
2570        struct drm_crtc_state *crtc_state;
2571        int ret = 0;
2572
2573        state = drm_atomic_state_alloc(crtc->dev);
2574        if (!state)
2575                return -ENOMEM;
2576
2577        /* ->set_property is always called with all locks held. */
2578        state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2579retry:
2580        crtc_state = drm_atomic_get_crtc_state(state, crtc);
2581        if (IS_ERR(crtc_state)) {
2582                ret = PTR_ERR(crtc_state);
2583                goto fail;
2584        }
2585
2586        ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2587                        property, val);
2588        if (ret)
2589                goto fail;
2590
2591        ret = drm_atomic_commit(state);
2592fail:
2593        if (ret == -EDEADLK)
2594                goto backoff;
2595
2596        drm_atomic_state_put(state);
2597        return ret;
2598
2599backoff:
2600        drm_atomic_state_clear(state);
2601        drm_atomic_legacy_backoff(state);
2602
2603        goto retry;
2604}
2605EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2606
2607/**
2608 * drm_atomic_helper_plane_set_property - helper for plane properties
2609 * @plane: DRM plane
2610 * @property: DRM property
2611 * @val: value of property
2612 *
2613 * Provides a default plane set_property handler using the atomic driver
2614 * interface.
2615 *
2616 * RETURNS:
2617 * Zero on success, error code on failure
2618 */
2619int
2620drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2621                                    struct drm_property *property,
2622                                    uint64_t val)
2623{
2624        struct drm_atomic_state *state;
2625        struct drm_plane_state *plane_state;
2626        int ret = 0;
2627
2628        state = drm_atomic_state_alloc(plane->dev);
2629        if (!state)
2630                return -ENOMEM;
2631
2632        /* ->set_property is always called with all locks held. */
2633        state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2634retry:
2635        plane_state = drm_atomic_get_plane_state(state, plane);
2636        if (IS_ERR(plane_state)) {
2637                ret = PTR_ERR(plane_state);
2638                goto fail;
2639        }
2640
2641        ret = drm_atomic_plane_set_property(plane, plane_state,
2642                        property, val);
2643        if (ret)
2644                goto fail;
2645
2646        ret = drm_atomic_commit(state);
2647fail:
2648        if (ret == -EDEADLK)
2649                goto backoff;
2650
2651        drm_atomic_state_put(state);
2652        return ret;
2653
2654backoff:
2655        drm_atomic_state_clear(state);
2656        drm_atomic_legacy_backoff(state);
2657
2658        goto retry;
2659}
2660EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2661
2662/**
2663 * drm_atomic_helper_connector_set_property - helper for connector properties
2664 * @connector: DRM connector
2665 * @property: DRM property
2666 * @val: value of property
2667 *
2668 * Provides a default connector set_property handler using the atomic driver
2669 * interface.
2670 *
2671 * RETURNS:
2672 * Zero on success, error code on failure
2673 */
2674int
2675drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2676                                    struct drm_property *property,
2677                                    uint64_t val)
2678{
2679        struct drm_atomic_state *state;
2680        struct drm_connector_state *connector_state;
2681        int ret = 0;
2682
2683        state = drm_atomic_state_alloc(connector->dev);
2684        if (!state)
2685                return -ENOMEM;
2686
2687        /* ->set_property is always called with all locks held. */
2688        state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2689retry:
2690        connector_state = drm_atomic_get_connector_state(state, connector);
2691        if (IS_ERR(connector_state)) {
2692                ret = PTR_ERR(connector_state);
2693                goto fail;
2694        }
2695
2696        ret = drm_atomic_connector_set_property(connector, connector_state,
2697                        property, val);
2698        if (ret)
2699                goto fail;
2700
2701        ret = drm_atomic_commit(state);
2702fail:
2703        if (ret == -EDEADLK)
2704                goto backoff;
2705
2706        drm_atomic_state_put(state);
2707        return ret;
2708
2709backoff:
2710        drm_atomic_state_clear(state);
2711        drm_atomic_legacy_backoff(state);
2712
2713        goto retry;
2714}
2715EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2716
2717static int page_flip_common(
2718                                struct drm_atomic_state *state,
2719                                struct drm_crtc *crtc,
2720                                struct drm_framebuffer *fb,
2721                                struct drm_pending_vblank_event *event)
2722{
2723        struct drm_plane *plane = crtc->primary;
2724        struct drm_plane_state *plane_state;
2725        struct drm_crtc_state *crtc_state;
2726        int ret = 0;
2727
2728        crtc_state = drm_atomic_get_crtc_state(state, crtc);
2729        if (IS_ERR(crtc_state))
2730                return PTR_ERR(crtc_state);
2731
2732        crtc_state->event = event;
2733
2734        plane_state = drm_atomic_get_plane_state(state, plane);
2735        if (IS_ERR(plane_state))
2736                return PTR_ERR(plane_state);
2737
2738
2739        ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2740        if (ret != 0)
2741                return ret;
2742        drm_atomic_set_fb_for_plane(plane_state, fb);
2743
2744        /* Make sure we don't accidentally do a full modeset. */
2745        state->allow_modeset = false;
2746        if (!crtc_state->active) {
2747                DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2748                                 crtc->base.id);
2749                return -EINVAL;
2750        }
2751
2752        return ret;
2753}
2754
2755/**
2756 * drm_atomic_helper_page_flip - execute a legacy page flip
2757 * @crtc: DRM crtc
2758 * @fb: DRM framebuffer
2759 * @event: optional DRM event to signal upon completion
2760 * @flags: flip flags for non-vblank sync'ed updates
2761 *
2762 * Provides a default &drm_crtc_funcs.page_flip implementation
2763 * using the atomic driver interface.
2764 *
2765 * Note that for now so called async page flips (i.e. updates which are not
2766 * synchronized to vblank) are not supported, since the atomic interfaces have
2767 * no provisions for this yet.
2768 *
2769 * Returns:
2770 * Returns 0 on success, negative errno numbers on failure.
2771 *
2772 * See also:
2773 * drm_atomic_helper_page_flip_target()
2774 */
2775int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2776                                struct drm_framebuffer *fb,
2777                                struct drm_pending_vblank_event *event,
2778                                uint32_t flags)
2779{
2780        struct drm_plane *plane = crtc->primary;
2781        struct drm_atomic_state *state;
2782        int ret = 0;
2783
2784        if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2785                return -EINVAL;
2786
2787        state = drm_atomic_state_alloc(plane->dev);
2788        if (!state)
2789                return -ENOMEM;
2790
2791        state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2792
2793retry:
2794        ret = page_flip_common(state, crtc, fb, event);
2795        if (ret != 0)
2796                goto fail;
2797
2798        ret = drm_atomic_nonblocking_commit(state);
2799
2800fail:
2801        if (ret == -EDEADLK)
2802                goto backoff;
2803
2804        drm_atomic_state_put(state);
2805        return ret;
2806
2807backoff:
2808        drm_atomic_state_clear(state);
2809        drm_atomic_legacy_backoff(state);
2810
2811        /*
2812         * Someone might have exchanged the framebuffer while we dropped locks
2813         * in the backoff code. We need to fix up the fb refcount tracking the
2814         * core does for us.
2815         */
2816        plane->old_fb = plane->fb;
2817
2818        goto retry;
2819}
2820EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2821
2822/**
2823 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2824 * @crtc: DRM crtc
2825 * @fb: DRM framebuffer
2826 * @event: optional DRM event to signal upon completion
2827 * @flags: flip flags for non-vblank sync'ed updates
2828 * @target: specifying the target vblank period when the flip to take effect
2829 *
2830 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2831 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2832 * target vblank period to flip.
2833 *
2834 * Returns:
2835 * Returns 0 on success, negative errno numbers on failure.
2836 */
2837int drm_atomic_helper_page_flip_target(
2838                                struct drm_crtc *crtc,
2839                                struct drm_framebuffer *fb,
2840                                struct drm_pending_vblank_event *event,
2841                                uint32_t flags,
2842                                uint32_t target)
2843{
2844        struct drm_plane *plane = crtc->primary;
2845        struct drm_atomic_state *state;
2846        struct drm_crtc_state *crtc_state;
2847        int ret = 0;
2848
2849        if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2850                return -EINVAL;
2851
2852        state = drm_atomic_state_alloc(plane->dev);
2853        if (!state)
2854                return -ENOMEM;
2855
2856        state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2857
2858retry:
2859        ret = page_flip_common(state, crtc, fb, event);
2860        if (ret != 0)
2861                goto fail;
2862
2863        crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
2864        if (WARN_ON(!crtc_state)) {
2865                ret = -EINVAL;
2866                goto fail;
2867        }
2868        crtc_state->target_vblank = target;
2869
2870        ret = drm_atomic_nonblocking_commit(state);
2871
2872fail:
2873        if (ret == -EDEADLK)
2874                goto backoff;
2875
2876        drm_atomic_state_put(state);
2877        return ret;
2878
2879backoff:
2880        drm_atomic_state_clear(state);
2881        drm_atomic_legacy_backoff(state);
2882
2883        /*
2884         * Someone might have exchanged the framebuffer while we dropped locks
2885         * in the backoff code. We need to fix up the fb refcount tracking the
2886         * core does for us.
2887         */
2888        plane->old_fb = plane->fb;
2889
2890        goto retry;
2891}
2892EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2893
2894/**
2895 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2896 * @connector: affected connector
2897 * @mode: DPMS mode
2898 *
2899 * This is the main helper function provided by the atomic helper framework for
2900 * implementing the legacy DPMS connector interface. It computes the new desired
2901 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
2902 * enabled) and updates it.
2903 *
2904 * Returns:
2905 * Returns 0 on success, negative errno numbers on failure.
2906 */
2907int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2908                                     int mode)
2909{
2910        struct drm_mode_config *config = &connector->dev->mode_config;
2911        struct drm_atomic_state *state;
2912        struct drm_crtc_state *crtc_state;
2913        struct drm_crtc *crtc;
2914        struct drm_connector *tmp_connector;
2915        struct drm_connector_list_iter conn_iter;
2916        int ret;
2917        bool active = false;
2918        int old_mode = connector->dpms;
2919
2920        if (mode != DRM_MODE_DPMS_ON)
2921                mode = DRM_MODE_DPMS_OFF;
2922
2923        connector->dpms = mode;
2924        crtc = connector->state->crtc;
2925
2926        if (!crtc)
2927                return 0;
2928
2929        state = drm_atomic_state_alloc(connector->dev);
2930        if (!state)
2931                return -ENOMEM;
2932
2933        state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2934retry:
2935        crtc_state = drm_atomic_get_crtc_state(state, crtc);
2936        if (IS_ERR(crtc_state)) {
2937                ret = PTR_ERR(crtc_state);
2938                goto fail;
2939        }
2940
2941        WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2942
2943        drm_connector_list_iter_get(connector->dev, &conn_iter);
2944        drm_for_each_connector_iter(tmp_connector, &conn_iter) {
2945                if (tmp_connector->state->crtc != crtc)
2946                        continue;
2947
2948                if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2949                        active = true;
2950                        break;
2951                }
2952        }
2953        drm_connector_list_iter_put(&conn_iter);
2954        crtc_state->active = active;
2955
2956        ret = drm_atomic_commit(state);
2957fail:
2958        if (ret == -EDEADLK)
2959                goto backoff;
2960        if (ret != 0)
2961                connector->dpms = old_mode;
2962        drm_atomic_state_put(state);
2963        return ret;
2964
2965backoff:
2966        drm_atomic_state_clear(state);
2967        drm_atomic_legacy_backoff(state);
2968
2969        goto retry;
2970}
2971EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2972
2973/**
2974 * drm_atomic_helper_best_encoder - Helper for
2975 *      &drm_connector_helper_funcs.best_encoder callback
2976 * @connector: Connector control structure
2977 *
2978 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
2979 * connectors that support exactly 1 encoder, statically determined at driver
2980 * init time.
2981 */
2982struct drm_encoder *
2983drm_atomic_helper_best_encoder(struct drm_connector *connector)
2984{
2985        WARN_ON(connector->encoder_ids[1]);
2986        return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
2987}
2988EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
2989
2990/**
2991 * DOC: atomic state reset and initialization
2992 *
2993 * Both the drm core and the atomic helpers assume that there is always the full
2994 * and correct atomic software state for all connectors, CRTCs and planes
2995 * available. Which is a bit a problem on driver load and also after system
2996 * suspend. One way to solve this is to have a hardware state read-out
2997 * infrastructure which reconstructs the full software state (e.g. the i915
2998 * driver).
2999 *
3000 * The simpler solution is to just reset the software state to everything off,
3001 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3002 * the atomic helpers provide default reset implementations for all hooks.
3003 *
3004 * On the upside the precise state tracking of atomic simplifies system suspend
3005 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3006 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3007 * For other drivers the building blocks are split out, see the documentation
3008 * for these functions.
3009 */
3010
3011/**
3012 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3013 * @crtc: drm CRTC
3014 *
3015 * Resets the atomic state for @crtc by freeing the state pointer (which might
3016 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3017 */
3018void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3019{
3020        if (crtc->state)
3021                __drm_atomic_helper_crtc_destroy_state(crtc->state);
3022
3023        kfree(crtc->state);
3024        crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3025
3026        if (crtc->state)
3027                crtc->state->crtc = crtc;
3028}
3029EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3030
3031/**
3032 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3033 * @crtc: CRTC object
3034 * @state: atomic CRTC state
3035 *
3036 * Copies atomic state from a CRTC's current state and resets inferred values.
3037 * This is useful for drivers that subclass the CRTC state.
3038 */
3039void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3040                                              struct drm_crtc_state *state)
3041{
3042        memcpy(state, crtc->state, sizeof(*state));
3043
3044        if (state->mode_blob)
3045                drm_property_reference_blob(state->mode_blob);
3046        if (state->degamma_lut)
3047                drm_property_reference_blob(state->degamma_lut);
3048        if (state->ctm)
3049                drm_property_reference_blob(state->ctm);
3050        if (state->gamma_lut)
3051                drm_property_reference_blob(state->gamma_lut);
3052        state->mode_changed = false;
3053        state->active_changed = false;
3054        state->planes_changed = false;
3055        state->connectors_changed = false;
3056        state->color_mgmt_changed = false;
3057        state->zpos_changed = false;
3058        state->event = NULL;
3059}
3060EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3061
3062/**
3063 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3064 * @crtc: drm CRTC
3065 *
3066 * Default CRTC state duplicate hook for drivers which don't have their own
3067 * subclassed CRTC state structure.
3068 */
3069struct drm_crtc_state *
3070drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3071{
3072        struct drm_crtc_state *state;
3073
3074        if (WARN_ON(!crtc->state))
3075                return NULL;
3076
3077        state = kmalloc(sizeof(*state), GFP_KERNEL);
3078        if (state)
3079                __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3080
3081        return state;
3082}
3083EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3084
3085/**
3086 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3087 * @state: CRTC state object to release
3088 *
3089 * Releases all resources stored in the CRTC state without actually freeing
3090 * the memory of the CRTC state. This is useful for drivers that subclass the
3091 * CRTC state.
3092 */
3093void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3094{
3095        drm_property_unreference_blob(state->mode_blob);
3096        drm_property_unreference_blob(state->degamma_lut);
3097        drm_property_unreference_blob(state->ctm);
3098        drm_property_unreference_blob(state->gamma_lut);
3099}
3100EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3101
3102/**
3103 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3104 * @crtc: drm CRTC
3105 * @state: CRTC state object to release
3106 *
3107 * Default CRTC state destroy hook for drivers which don't have their own
3108 * subclassed CRTC state structure.
3109 */
3110void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3111                                          struct drm_crtc_state *state)
3112{
3113        __drm_atomic_helper_crtc_destroy_state(state);
3114        kfree(state);
3115}
3116EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3117
3118/**
3119 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3120 * @plane: drm plane
3121 *
3122 * Resets the atomic state for @plane by freeing the state pointer (which might
3123 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3124 */
3125void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3126{
3127        if (plane->state)
3128                __drm_atomic_helper_plane_destroy_state(plane->state);
3129
3130        kfree(plane->state);
3131        plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3132
3133        if (plane->state) {
3134                plane->state->plane = plane;
3135                plane->state->rotation = DRM_ROTATE_0;
3136        }
3137}
3138EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3139
3140/**
3141 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3142 * @plane: plane object
3143 * @state: atomic plane state
3144 *
3145 * Copies atomic state from a plane's current state. This is useful for
3146 * drivers that subclass the plane state.
3147 */
3148void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3149                                               struct drm_plane_state *state)
3150{
3151        memcpy(state, plane->state, sizeof(*state));
3152
3153        if (state->fb)
3154                drm_framebuffer_reference(state->fb);
3155
3156        state->fence = NULL;
3157}
3158EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3159
3160/**
3161 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3162 * @plane: drm plane
3163 *
3164 * Default plane state duplicate hook for drivers which don't have their own
3165 * subclassed plane state structure.
3166 */
3167struct drm_plane_state *
3168drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3169{
3170        struct drm_plane_state *state;
3171
3172        if (WARN_ON(!plane->state))
3173                return NULL;
3174
3175        state = kmalloc(sizeof(*state), GFP_KERNEL);
3176        if (state)
3177                __drm_atomic_helper_plane_duplicate_state(plane, state);
3178
3179        return state;
3180}
3181EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3182
3183/**
3184 * __drm_atomic_helper_plane_destroy_state - release plane state
3185 * @state: plane state object to release
3186 *
3187 * Releases all resources stored in the plane state without actually freeing
3188 * the memory of the plane state. This is useful for drivers that subclass the
3189 * plane state.
3190 */
3191void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3192{
3193        if (state->fb)
3194                drm_framebuffer_unreference(state->fb);
3195
3196        if (state->fence)
3197                dma_fence_put(state->fence);
3198}
3199EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3200
3201/**
3202 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3203 * @plane: drm plane
3204 * @state: plane state object to release
3205 *
3206 * Default plane state destroy hook for drivers which don't have their own
3207 * subclassed plane state structure.
3208 */
3209void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3210                                           struct drm_plane_state *state)
3211{
3212        __drm_atomic_helper_plane_destroy_state(state);
3213        kfree(state);
3214}
3215EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3216
3217/**
3218 * __drm_atomic_helper_connector_reset - reset state on connector
3219 * @connector: drm connector
3220 * @conn_state: connector state to assign
3221 *
3222 * Initializes the newly allocated @conn_state and assigns it to
3223 * the &drm_conector->state pointer of @connector, usually required when
3224 * initializing the drivers or when called from the &drm_connector_funcs.reset
3225 * hook.
3226 *
3227 * This is useful for drivers that subclass the connector state.
3228 */
3229void
3230__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3231                                    struct drm_connector_state *conn_state)
3232{
3233        if (conn_state)
3234                conn_state->connector = connector;
3235
3236        connector->state = conn_state;
3237}
3238EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3239
3240/**
3241 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3242 * @connector: drm connector
3243 *
3244 * Resets the atomic state for @connector by freeing the state pointer (which
3245 * might be NULL, e.g. at driver load time) and allocating a new empty state
3246 * object.
3247 */
3248void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3249{
3250        struct drm_connector_state *conn_state =
3251                kzalloc(sizeof(*conn_state), GFP_KERNEL);
3252
3253        if (connector->state)
3254                __drm_atomic_helper_connector_destroy_state(connector->state);
3255
3256        kfree(connector->state);
3257        __drm_atomic_helper_connector_reset(connector, conn_state);
3258}
3259EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3260
3261/**
3262 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3263 * @connector: connector object
3264 * @state: atomic connector state
3265 *
3266 * Copies atomic state from a connector's current state. This is useful for
3267 * drivers that subclass the connector state.
3268 */
3269void
3270__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3271                                            struct drm_connector_state *state)
3272{
3273        memcpy(state, connector->state, sizeof(*state));
3274        if (state->crtc)
3275                drm_connector_reference(connector);
3276}
3277EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3278
3279/**
3280 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3281 * @connector: drm connector
3282 *
3283 * Default connector state duplicate hook for drivers which don't have their own
3284 * subclassed connector state structure.
3285 */
3286struct drm_connector_state *
3287drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3288{
3289        struct drm_connector_state *state;
3290
3291        if (WARN_ON(!connector->state))
3292                return NULL;
3293
3294        state = kmalloc(sizeof(*state), GFP_KERNEL);
3295        if (state)
3296                __drm_atomic_helper_connector_duplicate_state(connector, state);
3297
3298        return state;
3299}
3300EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3301
3302/**
3303 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3304 * @dev: DRM device
3305 * @ctx: lock acquisition context
3306 *
3307 * Makes a copy of the current atomic state by looping over all objects and
3308 * duplicating their respective states. This is used for example by suspend/
3309 * resume support code to save the state prior to suspend such that it can
3310 * be restored upon resume.
3311 *
3312 * Note that this treats atomic state as persistent between save and restore.
3313 * Drivers must make sure that this is possible and won't result in confusion
3314 * or erroneous behaviour.
3315 *
3316 * Note that if callers haven't already acquired all modeset locks this might
3317 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3318 *
3319 * Returns:
3320 * A pointer to the copy of the atomic state object on success or an
3321 * ERR_PTR()-encoded error code on failure.
3322 *
3323 * See also:
3324 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3325 */
3326struct drm_atomic_state *
3327drm_atomic_helper_duplicate_state(struct drm_device *dev,
3328                                  struct drm_modeset_acquire_ctx *ctx)
3329{
3330        struct drm_atomic_state *state;
3331        struct drm_connector *conn;
3332        struct drm_connector_list_iter conn_iter;
3333        struct drm_plane *plane;
3334        struct drm_crtc *crtc;
3335        int err = 0;
3336
3337        state = drm_atomic_state_alloc(dev);
3338        if (!state)
3339                return ERR_PTR(-ENOMEM);
3340
3341        state->acquire_ctx = ctx;
3342
3343        drm_for_each_crtc(crtc, dev) {
3344                struct drm_crtc_state *crtc_state;
3345
3346                crtc_state = drm_atomic_get_crtc_state(state, crtc);
3347                if (IS_ERR(crtc_state)) {
3348                        err = PTR_ERR(crtc_state);
3349                        goto free;
3350                }
3351        }
3352
3353        drm_for_each_plane(plane, dev) {
3354                struct drm_plane_state *plane_state;
3355
3356                plane_state = drm_atomic_get_plane_state(state, plane);
3357                if (IS_ERR(plane_state)) {
3358                        err = PTR_ERR(plane_state);
3359                        goto free;
3360                }
3361        }
3362
3363        drm_connector_list_iter_get(dev, &conn_iter);
3364        drm_for_each_connector_iter(conn, &conn_iter) {
3365                struct drm_connector_state *conn_state;
3366
3367                conn_state = drm_atomic_get_connector_state(state, conn);
3368                if (IS_ERR(conn_state)) {
3369                        err = PTR_ERR(conn_state);
3370                        drm_connector_list_iter_put(&conn_iter);
3371                        goto free;
3372                }
3373        }
3374        drm_connector_list_iter_put(&conn_iter);
3375
3376        /* clear the acquire context so that it isn't accidentally reused */
3377        state->acquire_ctx = NULL;
3378
3379free:
3380        if (err < 0) {
3381                drm_atomic_state_put(state);
3382                state = ERR_PTR(err);
3383        }
3384
3385        return state;
3386}
3387EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3388
3389/**
3390 * __drm_atomic_helper_connector_destroy_state - release connector state
3391 * @state: connector state object to release
3392 *
3393 * Releases all resources stored in the connector state without actually
3394 * freeing the memory of the connector state. This is useful for drivers that
3395 * subclass the connector state.
3396 */
3397void
3398__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3399{
3400        if (state->crtc)
3401                drm_connector_unreference(state->connector);
3402}
3403EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3404
3405/**
3406 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3407 * @connector: drm connector
3408 * @state: connector state object to release
3409 *
3410 * Default connector state destroy hook for drivers which don't have their own
3411 * subclassed connector state structure.
3412 */
3413void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3414                                          struct drm_connector_state *state)
3415{
3416        __drm_atomic_helper_connector_destroy_state(state);
3417        kfree(state);
3418}
3419EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3420
3421/**
3422 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3423 * @crtc: CRTC object
3424 * @red: red correction table
3425 * @green: green correction table
3426 * @blue: green correction table
3427 * @size: size of the tables
3428 *
3429 * Implements support for legacy gamma correction table for drivers
3430 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3431 * properties.
3432 */
3433int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3434                                       u16 *red, u16 *green, u16 *blue,
3435                                       uint32_t size)
3436{
3437        struct drm_device *dev = crtc->dev;
3438        struct drm_mode_config *config = &dev->mode_config;
3439        struct drm_atomic_state *state;
3440        struct drm_crtc_state *crtc_state;
3441        struct drm_property_blob *blob = NULL;
3442        struct drm_color_lut *blob_data;
3443        int i, ret = 0;
3444
3445        state = drm_atomic_state_alloc(crtc->dev);
3446        if (!state)
3447                return -ENOMEM;
3448
3449        blob = drm_property_create_blob(dev,
3450                                        sizeof(struct drm_color_lut) * size,
3451                                        NULL);
3452        if (IS_ERR(blob)) {
3453                ret = PTR_ERR(blob);
3454                blob = NULL;
3455                goto fail;
3456        }
3457
3458        /* Prepare GAMMA_LUT with the legacy values. */
3459        blob_data = (struct drm_color_lut *) blob->data;
3460        for (i = 0; i < size; i++) {
3461                blob_data[i].red = red[i];
3462                blob_data[i].green = green[i];
3463                blob_data[i].blue = blue[i];
3464        }
3465
3466        state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3467retry:
3468        crtc_state = drm_atomic_get_crtc_state(state, crtc);
3469        if (IS_ERR(crtc_state)) {
3470                ret = PTR_ERR(crtc_state);
3471                goto fail;
3472        }
3473
3474        /* Reset DEGAMMA_LUT and CTM properties. */
3475        ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3476                        config->degamma_lut_property, 0);
3477        if (ret)
3478                goto fail;
3479
3480        ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3481                        config->ctm_property, 0);
3482        if (ret)
3483                goto fail;
3484
3485        ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3486                        config->gamma_lut_property, blob->base.id);
3487        if (ret)
3488                goto fail;
3489
3490        ret = drm_atomic_commit(state);
3491fail:
3492        if (ret == -EDEADLK)
3493                goto backoff;
3494
3495        drm_atomic_state_put(state);
3496        drm_property_unreference_blob(blob);
3497        return ret;
3498
3499backoff:
3500        drm_atomic_state_clear(state);
3501        drm_atomic_legacy_backoff(state);
3502
3503        goto retry;
3504}
3505EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);
3506