/*1* Copyright (C) 2014 Red Hat2* Copyright (C) 2014 Intel Corp.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included in12* all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR18* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,19* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR20* OTHER DEALINGS IN THE SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24* Daniel Vetter <[email protected]>25*/2627#include <linux/export.h>28#include <linux/dma-fence.h>29#include <linux/ktime.h>3031#include <drm/drm_atomic.h>32#include <drm/drm_atomic_helper.h>33#include <drm/drm_atomic_uapi.h>34#include <drm/drm_blend.h>35#include <drm/drm_bridge.h>36#include <drm/drm_damage_helper.h>37#include <drm/drm_device.h>38#include <drm/drm_drv.h>39#include <drm/drm_framebuffer.h>40#include <drm/drm_gem_atomic_helper.h>41#include <drm/drm_panic.h>42#include <drm/drm_print.h>43#include <drm/drm_self_refresh_helper.h>44#include <drm/drm_vblank.h>45#include <drm/drm_writeback.h>4647#include "drm_crtc_helper_internal.h"48#include "drm_crtc_internal.h"4950/**51* DOC: overview52*53* This helper library provides implementations of check and commit functions on54* top of the CRTC modeset helper callbacks and the plane helper callbacks. It55* also provides convenience implementations for the atomic state handling56* callbacks for drivers which don't need to subclass the drm core structures to57* add their own additional internal state.58*59* This library also provides default implementations for the check callback in60* drm_atomic_helper_check() and for the commit callback with61* drm_atomic_helper_commit(). But the individual stages and callbacks are62* exposed to allow drivers to mix and match and e.g. use the plane helpers only63* together with a driver private modeset implementation.64*65* This library also provides implementations for all the legacy driver66* interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),67* drm_atomic_helper_disable_plane(), and the various functions to implement68* set_property callbacks. New drivers must not implement these functions69* themselves but must use the provided helpers.70*71* The atomic helper uses the same function table structures as all other72* modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,73* struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It74* also shares the &struct drm_plane_helper_funcs function table with the plane75* helpers.76*/77static void78drm_atomic_helper_plane_changed(struct drm_atomic_state *state,79struct drm_plane_state *old_plane_state,80struct drm_plane_state *plane_state,81struct drm_plane *plane)82{83struct drm_crtc_state *crtc_state;8485if (old_plane_state->crtc) {86crtc_state = drm_atomic_get_new_crtc_state(state,87old_plane_state->crtc);8889if (WARN_ON(!crtc_state))90return;9192crtc_state->planes_changed = true;93}9495if (plane_state->crtc) {96crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);9798if (WARN_ON(!crtc_state))99return;100101crtc_state->planes_changed = true;102}103}104105static int handle_conflicting_encoders(struct drm_atomic_state *state,106bool disable_conflicting_encoders)107{108struct drm_connector_state *new_conn_state;109struct drm_connector *connector;110struct drm_connector_list_iter conn_iter;111struct drm_encoder *encoder;112unsigned int encoder_mask = 0;113int i, ret = 0;114115/*116* First loop, find all newly assigned encoders from the connectors117* part of the state. If the same encoder is assigned to multiple118* connectors bail out.119*/120for_each_new_connector_in_state(state, connector, new_conn_state, i) {121const struct drm_connector_helper_funcs *funcs = connector->helper_private;122struct drm_encoder *new_encoder;123124if (!new_conn_state->crtc)125continue;126127if (funcs->atomic_best_encoder)128new_encoder = funcs->atomic_best_encoder(connector,129state);130else if (funcs->best_encoder)131new_encoder = funcs->best_encoder(connector);132else133new_encoder = drm_connector_get_single_encoder(connector);134135if (new_encoder) {136if (encoder_mask & drm_encoder_mask(new_encoder)) {137drm_dbg_atomic(connector->dev,138"[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",139new_encoder->base.id, new_encoder->name,140connector->base.id, connector->name);141142return -EINVAL;143}144145encoder_mask |= drm_encoder_mask(new_encoder);146}147}148149if (!encoder_mask)150return 0;151152/*153* Second loop, iterate over all connectors not part of the state.154*155* If a conflicting encoder is found and disable_conflicting_encoders156* is not set, an error is returned. Userspace can provide a solution157* through the atomic ioctl.158*159* If the flag is set conflicting connectors are removed from the CRTC160* and the CRTC is disabled if no encoder is left. This preserves161* compatibility with the legacy set_config behavior.162*/163drm_connector_list_iter_begin(state->dev, &conn_iter);164drm_for_each_connector_iter(connector, &conn_iter) {165struct drm_crtc_state *crtc_state;166167if (drm_atomic_get_new_connector_state(state, connector))168continue;169170encoder = connector->state->best_encoder;171if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))172continue;173174if (!disable_conflicting_encoders) {175drm_dbg_atomic(connector->dev,176"[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",177encoder->base.id, encoder->name,178connector->state->crtc->base.id,179connector->state->crtc->name,180connector->base.id, connector->name);181ret = -EINVAL;182goto out;183}184185new_conn_state = drm_atomic_get_connector_state(state, connector);186if (IS_ERR(new_conn_state)) {187ret = PTR_ERR(new_conn_state);188goto out;189}190191drm_dbg_atomic(connector->dev,192"[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",193encoder->base.id, encoder->name,194new_conn_state->crtc->base.id, new_conn_state->crtc->name,195connector->base.id, connector->name);196197crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);198199ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);200if (ret)201goto out;202203if (!crtc_state->connector_mask) {204ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,205NULL);206if (ret < 0)207goto out;208209crtc_state->active = false;210}211}212out:213drm_connector_list_iter_end(&conn_iter);214215return ret;216}217218static void219set_best_encoder(struct drm_atomic_state *state,220struct drm_connector_state *conn_state,221struct drm_encoder *encoder)222{223struct drm_crtc_state *crtc_state;224struct drm_crtc *crtc;225226if (conn_state->best_encoder) {227/* Unset the encoder_mask in the old crtc state. */228crtc = conn_state->connector->state->crtc;229230/* A NULL crtc is an error here because we should have231* duplicated a NULL best_encoder when crtc was NULL.232* As an exception restoring duplicated atomic state233* during resume is allowed, so don't warn when234* best_encoder is equal to encoder we intend to set.235*/236WARN_ON(!crtc && encoder != conn_state->best_encoder);237if (crtc) {238crtc_state = drm_atomic_get_new_crtc_state(state, crtc);239240crtc_state->encoder_mask &=241~drm_encoder_mask(conn_state->best_encoder);242}243}244245if (encoder) {246crtc = conn_state->crtc;247WARN_ON(!crtc);248if (crtc) {249crtc_state = drm_atomic_get_new_crtc_state(state, crtc);250251crtc_state->encoder_mask |=252drm_encoder_mask(encoder);253}254}255256conn_state->best_encoder = encoder;257}258259static void260steal_encoder(struct drm_atomic_state *state,261struct drm_encoder *encoder)262{263struct drm_crtc_state *crtc_state;264struct drm_connector *connector;265struct drm_connector_state *old_connector_state, *new_connector_state;266int i;267268for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {269struct drm_crtc *encoder_crtc;270271if (new_connector_state->best_encoder != encoder)272continue;273274encoder_crtc = old_connector_state->crtc;275276drm_dbg_atomic(encoder->dev,277"[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",278encoder->base.id, encoder->name,279encoder_crtc->base.id, encoder_crtc->name);280281set_best_encoder(state, new_connector_state, NULL);282283crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);284crtc_state->connectors_changed = true;285286return;287}288}289290static int291update_connector_routing(struct drm_atomic_state *state,292struct drm_connector *connector,293struct drm_connector_state *old_connector_state,294struct drm_connector_state *new_connector_state,295bool added_by_user)296{297const struct drm_connector_helper_funcs *funcs;298struct drm_encoder *new_encoder;299struct drm_crtc_state *crtc_state;300301drm_dbg_atomic(connector->dev, "Updating routing for [CONNECTOR:%d:%s]\n",302connector->base.id, connector->name);303304if (old_connector_state->crtc != new_connector_state->crtc) {305if (old_connector_state->crtc) {306crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);307crtc_state->connectors_changed = true;308}309310if (new_connector_state->crtc) {311crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);312crtc_state->connectors_changed = true;313}314}315316if (!new_connector_state->crtc) {317drm_dbg_atomic(connector->dev, "Disabling [CONNECTOR:%d:%s]\n",318connector->base.id, connector->name);319320set_best_encoder(state, new_connector_state, NULL);321322return 0;323}324325crtc_state = drm_atomic_get_new_crtc_state(state,326new_connector_state->crtc);327/*328* For compatibility with legacy users, we want to make sure that329* we allow DPMS On->Off modesets on unregistered connectors. Modesets330* which would result in anything else must be considered invalid, to331* avoid turning on new displays on dead connectors.332*333* Since the connector can be unregistered at any point during an334* atomic check or commit, this is racy. But that's OK: all we care335* about is ensuring that userspace can't do anything but shut off the336* display on a connector that was destroyed after it's been notified,337* not before.338*339* Additionally, we also want to ignore connector registration when340* we're trying to restore an atomic state during system resume since341* there's a chance the connector may have been destroyed during the342* process, but it's better to ignore that then cause343* drm_atomic_helper_resume() to fail.344*345* Last, we want to ignore connector registration when the connector346* was not pulled in the atomic state by user-space (ie, was pulled347* in by the driver, e.g. when updating a DP-MST stream).348*/349if (!state->duplicated && drm_connector_is_unregistered(connector) &&350added_by_user && crtc_state->active) {351drm_dbg_atomic(connector->dev,352"[CONNECTOR:%d:%s] is not registered\n",353connector->base.id, connector->name);354return -EINVAL;355}356357funcs = connector->helper_private;358359if (funcs->atomic_best_encoder)360new_encoder = funcs->atomic_best_encoder(connector, state);361else if (funcs->best_encoder)362new_encoder = funcs->best_encoder(connector);363else364new_encoder = drm_connector_get_single_encoder(connector);365366if (!new_encoder) {367drm_dbg_atomic(connector->dev,368"No suitable encoder found for [CONNECTOR:%d:%s]\n",369connector->base.id, connector->name);370return -EINVAL;371}372373if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {374drm_dbg_atomic(connector->dev,375"[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",376new_encoder->base.id,377new_encoder->name,378new_connector_state->crtc->base.id,379new_connector_state->crtc->name);380return -EINVAL;381}382383if (new_encoder == new_connector_state->best_encoder) {384set_best_encoder(state, new_connector_state, new_encoder);385386drm_dbg_atomic(connector->dev,387"[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",388connector->base.id,389connector->name,390new_encoder->base.id,391new_encoder->name,392new_connector_state->crtc->base.id,393new_connector_state->crtc->name);394395return 0;396}397398steal_encoder(state, new_encoder);399400set_best_encoder(state, new_connector_state, new_encoder);401402crtc_state->connectors_changed = true;403404drm_dbg_atomic(connector->dev,405"[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",406connector->base.id,407connector->name,408new_encoder->base.id,409new_encoder->name,410new_connector_state->crtc->base.id,411new_connector_state->crtc->name);412413return 0;414}415416static int417mode_fixup(struct drm_atomic_state *state)418{419struct drm_crtc *crtc;420struct drm_crtc_state *new_crtc_state;421struct drm_connector *connector;422struct drm_connector_state *new_conn_state;423int i;424int ret;425426for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {427if (!new_crtc_state->mode_changed &&428!new_crtc_state->connectors_changed)429continue;430431drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);432}433434for_each_new_connector_in_state(state, connector, new_conn_state, i) {435const struct drm_encoder_helper_funcs *funcs;436struct drm_encoder *encoder;437struct drm_bridge *bridge;438439WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);440441if (!new_conn_state->crtc || !new_conn_state->best_encoder)442continue;443444new_crtc_state =445drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);446447/*448* Each encoder has at most one connector (since we always steal449* it away), so we won't call ->mode_fixup twice.450*/451encoder = new_conn_state->best_encoder;452funcs = encoder->helper_private;453454bridge = drm_bridge_chain_get_first_bridge(encoder);455ret = drm_atomic_bridge_chain_check(bridge,456new_crtc_state,457new_conn_state);458drm_bridge_put(bridge);459if (ret) {460drm_dbg_atomic(encoder->dev, "Bridge atomic check failed\n");461return ret;462}463464if (funcs && funcs->atomic_check) {465ret = funcs->atomic_check(encoder, new_crtc_state,466new_conn_state);467if (ret) {468drm_dbg_atomic(encoder->dev,469"[ENCODER:%d:%s] check failed\n",470encoder->base.id, encoder->name);471return ret;472}473} else if (funcs && funcs->mode_fixup) {474ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,475&new_crtc_state->adjusted_mode);476if (!ret) {477drm_dbg_atomic(encoder->dev,478"[ENCODER:%d:%s] fixup failed\n",479encoder->base.id, encoder->name);480return -EINVAL;481}482}483}484485for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {486const struct drm_crtc_helper_funcs *funcs;487488if (!new_crtc_state->enable)489continue;490491if (!new_crtc_state->mode_changed &&492!new_crtc_state->connectors_changed)493continue;494495funcs = crtc->helper_private;496if (!funcs || !funcs->mode_fixup)497continue;498499ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,500&new_crtc_state->adjusted_mode);501if (!ret) {502drm_dbg_atomic(crtc->dev, "[CRTC:%d:%s] fixup failed\n",503crtc->base.id, crtc->name);504return -EINVAL;505}506}507508return 0;509}510511static enum drm_mode_status mode_valid_path(struct drm_connector *connector,512struct drm_encoder *encoder,513struct drm_crtc *crtc,514const struct drm_display_mode *mode)515{516struct drm_bridge *bridge;517enum drm_mode_status ret;518519ret = drm_encoder_mode_valid(encoder, mode);520if (ret != MODE_OK) {521drm_dbg_atomic(encoder->dev,522"[ENCODER:%d:%s] mode_valid() failed\n",523encoder->base.id, encoder->name);524return ret;525}526527bridge = drm_bridge_chain_get_first_bridge(encoder);528ret = drm_bridge_chain_mode_valid(bridge, &connector->display_info,529mode);530drm_bridge_put(bridge);531if (ret != MODE_OK) {532drm_dbg_atomic(encoder->dev, "[BRIDGE] mode_valid() failed\n");533return ret;534}535536ret = drm_crtc_mode_valid(crtc, mode);537if (ret != MODE_OK) {538drm_dbg_atomic(encoder->dev, "[CRTC:%d:%s] mode_valid() failed\n",539crtc->base.id, crtc->name);540return ret;541}542543return ret;544}545546static int547mode_valid(struct drm_atomic_state *state)548{549struct drm_connector_state *conn_state;550struct drm_connector *connector;551int i;552553for_each_new_connector_in_state(state, connector, conn_state, i) {554struct drm_encoder *encoder = conn_state->best_encoder;555struct drm_crtc *crtc = conn_state->crtc;556struct drm_crtc_state *crtc_state;557enum drm_mode_status mode_status;558const struct drm_display_mode *mode;559560if (!crtc || !encoder)561continue;562563crtc_state = drm_atomic_get_new_crtc_state(state, crtc);564if (!crtc_state)565continue;566if (!crtc_state->mode_changed && !crtc_state->connectors_changed)567continue;568569mode = &crtc_state->mode;570571mode_status = mode_valid_path(connector, encoder, crtc, mode);572if (mode_status != MODE_OK)573return -EINVAL;574}575576return 0;577}578579static int drm_atomic_check_valid_clones(struct drm_atomic_state *state,580struct drm_crtc *crtc)581{582struct drm_encoder *drm_enc;583struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,584crtc);585586drm_for_each_encoder_mask(drm_enc, crtc->dev, crtc_state->encoder_mask) {587if (!drm_enc->possible_clones) {588DRM_DEBUG("enc%d possible_clones is 0\n", drm_enc->base.id);589continue;590}591592if ((crtc_state->encoder_mask & drm_enc->possible_clones) !=593crtc_state->encoder_mask) {594DRM_DEBUG("crtc%d failed valid clone check for mask 0x%x\n",595crtc->base.id, crtc_state->encoder_mask);596return -EINVAL;597}598}599600return 0;601}602603/**604* drm_atomic_helper_check_modeset - validate state object for modeset changes605* @dev: DRM device606* @state: the driver state object607*608* Check the state object to see if the requested state is physically possible.609* This does all the CRTC and connector related computations for an atomic610* update and adds any additional connectors needed for full modesets. It calls611* the various per-object callbacks in the follow order:612*613* 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.614* 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.615* 3. If it's determined a modeset is needed then all connectors on the affected616* CRTC are added and &drm_connector_helper_funcs.atomic_check is run on them.617* 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and618* &drm_crtc_helper_funcs.mode_valid are called on the affected components.619* 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.620* 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.621* This function is only called when the encoder will be part of a configured CRTC,622* it must not be used for implementing connector property validation.623* If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called624* instead.625* 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with CRTC constraints.626*627* &drm_crtc_state.mode_changed is set when the input mode is changed.628* &drm_crtc_state.connectors_changed is set when a connector is added or629* removed from the CRTC. &drm_crtc_state.active_changed is set when630* &drm_crtc_state.active changes, which is used for DPMS.631* &drm_crtc_state.no_vblank is set from the result of drm_dev_has_vblank().632* See also: drm_atomic_crtc_needs_modeset()633*634* IMPORTANT:635*636* Drivers which set &drm_crtc_state.mode_changed (e.g. in their637* &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done638* without a full modeset) _must_ call this function after that change. It is639* permitted to call this function multiple times for the same update, e.g.640* when the &drm_crtc_helper_funcs.atomic_check functions depend upon the641* adjusted dotclock for fifo space allocation and watermark computation.642*643* RETURNS:644* Zero for success or -errno645*/646int647drm_atomic_helper_check_modeset(struct drm_device *dev,648struct drm_atomic_state *state)649{650struct drm_crtc *crtc;651struct drm_crtc_state *old_crtc_state, *new_crtc_state;652struct drm_connector *connector;653struct drm_connector_state *old_connector_state, *new_connector_state;654int i, ret;655unsigned int connectors_mask = 0, user_connectors_mask = 0;656657for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i)658user_connectors_mask |= BIT(i);659660for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {661bool has_connectors =662!!new_crtc_state->connector_mask;663664WARN_ON(!drm_modeset_is_locked(&crtc->mutex));665666if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {667drm_dbg_atomic(dev, "[CRTC:%d:%s] mode changed\n",668crtc->base.id, crtc->name);669new_crtc_state->mode_changed = true;670}671672if (old_crtc_state->enable != new_crtc_state->enable) {673drm_dbg_atomic(dev, "[CRTC:%d:%s] enable changed\n",674crtc->base.id, crtc->name);675676/*677* For clarity this assignment is done here, but678* enable == 0 is only true when there are no679* connectors and a NULL mode.680*681* The other way around is true as well. enable != 0682* implies that connectors are attached and a mode is set.683*/684new_crtc_state->mode_changed = true;685new_crtc_state->connectors_changed = true;686}687688if (old_crtc_state->active != new_crtc_state->active) {689drm_dbg_atomic(dev, "[CRTC:%d:%s] active changed\n",690crtc->base.id, crtc->name);691new_crtc_state->active_changed = true;692}693694if (new_crtc_state->enable != has_connectors) {695drm_dbg_atomic(dev, "[CRTC:%d:%s] enabled/connectors mismatch (%d/%d)\n",696crtc->base.id, crtc->name,697new_crtc_state->enable, has_connectors);698699return -EINVAL;700}701702if (drm_dev_has_vblank(dev))703new_crtc_state->no_vblank = false;704else705new_crtc_state->no_vblank = true;706}707708ret = handle_conflicting_encoders(state, false);709if (ret)710return ret;711712for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {713const struct drm_connector_helper_funcs *funcs = connector->helper_private;714715WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));716717/*718* This only sets crtc->connectors_changed for routing changes,719* drivers must set crtc->connectors_changed themselves when720* connector properties need to be updated.721*/722ret = update_connector_routing(state, connector,723old_connector_state,724new_connector_state,725BIT(i) & user_connectors_mask);726if (ret)727return ret;728if (old_connector_state->crtc) {729new_crtc_state = drm_atomic_get_new_crtc_state(state,730old_connector_state->crtc);731if (old_connector_state->link_status !=732new_connector_state->link_status)733new_crtc_state->connectors_changed = true;734735if (old_connector_state->max_requested_bpc !=736new_connector_state->max_requested_bpc)737new_crtc_state->connectors_changed = true;738}739740if (funcs->atomic_check)741ret = funcs->atomic_check(connector, state);742if (ret) {743drm_dbg_atomic(dev,744"[CONNECTOR:%d:%s] driver check failed\n",745connector->base.id, connector->name);746return ret;747}748749connectors_mask |= BIT(i);750}751752/*753* After all the routing has been prepared we need to add in any754* connector which is itself unchanged, but whose CRTC changes its755* configuration. This must be done before calling mode_fixup in case a756* crtc only changed its mode but has the same set of connectors.757*/758for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {759if (!drm_atomic_crtc_needs_modeset(new_crtc_state))760continue;761762drm_dbg_atomic(dev,763"[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",764crtc->base.id, crtc->name,765new_crtc_state->enable ? 'y' : 'n',766new_crtc_state->active ? 'y' : 'n');767768ret = drm_atomic_add_affected_connectors(state, crtc);769if (ret != 0)770return ret;771772ret = drm_atomic_add_affected_planes(state, crtc);773if (ret != 0)774return ret;775776ret = drm_atomic_check_valid_clones(state, crtc);777if (ret != 0)778return ret;779}780781/*782* Iterate over all connectors again, to make sure atomic_check()783* has been called on them when a modeset is forced.784*/785for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {786const struct drm_connector_helper_funcs *funcs = connector->helper_private;787788if (connectors_mask & BIT(i))789continue;790791if (funcs->atomic_check)792ret = funcs->atomic_check(connector, state);793if (ret) {794drm_dbg_atomic(dev,795"[CONNECTOR:%d:%s] driver check failed\n",796connector->base.id, connector->name);797return ret;798}799}800801/*802* Iterate over all connectors again, and add all affected bridges to803* the state.804*/805for_each_oldnew_connector_in_state(state, connector,806old_connector_state,807new_connector_state, i) {808struct drm_encoder *encoder;809810encoder = old_connector_state->best_encoder;811ret = drm_atomic_add_encoder_bridges(state, encoder);812if (ret)813return ret;814815encoder = new_connector_state->best_encoder;816ret = drm_atomic_add_encoder_bridges(state, encoder);817if (ret)818return ret;819}820821ret = mode_valid(state);822if (ret)823return ret;824825return mode_fixup(state);826}827EXPORT_SYMBOL(drm_atomic_helper_check_modeset);828829/**830* drm_atomic_helper_check_wb_connector_state() - Check writeback connector state831* @connector: corresponding connector832* @state: the driver state object833*834* Checks if the writeback connector state is valid, and returns an error if it835* isn't.836*837* RETURNS:838* Zero for success or -errno839*/840int841drm_atomic_helper_check_wb_connector_state(struct drm_connector *connector,842struct drm_atomic_state *state)843{844struct drm_connector_state *conn_state =845drm_atomic_get_new_connector_state(state, connector);846struct drm_writeback_job *wb_job = conn_state->writeback_job;847struct drm_property_blob *pixel_format_blob;848struct drm_framebuffer *fb;849size_t i, nformats;850u32 *formats;851852if (!wb_job || !wb_job->fb)853return 0;854855pixel_format_blob = wb_job->connector->pixel_formats_blob_ptr;856nformats = pixel_format_blob->length / sizeof(u32);857formats = pixel_format_blob->data;858fb = wb_job->fb;859860for (i = 0; i < nformats; i++)861if (fb->format->format == formats[i])862return 0;863864drm_dbg_kms(connector->dev, "Invalid pixel format %p4cc\n", &fb->format->format);865866return -EINVAL;867}868EXPORT_SYMBOL(drm_atomic_helper_check_wb_connector_state);869870/**871* drm_atomic_helper_check_plane_state() - Check plane state for validity872* @plane_state: plane state to check873* @crtc_state: CRTC state to check874* @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point875* @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point876* @can_position: is it legal to position the plane such that it877* doesn't cover the entire CRTC? This will generally878* only be false for primary planes.879* @can_update_disabled: can the plane be updated while the CRTC880* is disabled?881*882* Checks that a desired plane update is valid, and updates various883* bits of derived state (clipped coordinates etc.). Drivers that provide884* their own plane handling rather than helper-provided implementations may885* still wish to call this function to avoid duplication of error checking886* code.887*888* RETURNS:889* Zero if update appears valid, error code on failure890*/891int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,892const struct drm_crtc_state *crtc_state,893int min_scale,894int max_scale,895bool can_position,896bool can_update_disabled)897{898struct drm_framebuffer *fb = plane_state->fb;899struct drm_rect *src = &plane_state->src;900struct drm_rect *dst = &plane_state->dst;901unsigned int rotation = plane_state->rotation;902struct drm_rect clip = {};903int hscale, vscale;904905WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);906907*src = drm_plane_state_src(plane_state);908*dst = drm_plane_state_dest(plane_state);909910if (!fb) {911plane_state->visible = false;912return 0;913}914915/* crtc should only be NULL when disabling (i.e., !fb) */916if (WARN_ON(!plane_state->crtc)) {917plane_state->visible = false;918return 0;919}920921if (!crtc_state->enable && !can_update_disabled) {922drm_dbg_kms(plane_state->plane->dev,923"Cannot update plane of a disabled CRTC.\n");924return -EINVAL;925}926927drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);928929/* Check scaling */930hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);931vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);932if (hscale < 0 || vscale < 0) {933drm_dbg_kms(plane_state->plane->dev,934"Invalid scaling of plane\n");935drm_rect_debug_print("src: ", &plane_state->src, true);936drm_rect_debug_print("dst: ", &plane_state->dst, false);937return -ERANGE;938}939940if (crtc_state->enable)941drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);942943plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);944945drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);946947if (!plane_state->visible)948/*949* Plane isn't visible; some drivers can handle this950* so we just return success here. Drivers that can't951* (including those that use the primary plane helper's952* update function) will return an error from their953* update_plane handler.954*/955return 0;956957if (!can_position && !drm_rect_equals(dst, &clip)) {958drm_dbg_kms(plane_state->plane->dev,959"Plane must cover entire CRTC\n");960drm_rect_debug_print("dst: ", dst, false);961drm_rect_debug_print("clip: ", &clip, false);962return -EINVAL;963}964965return 0;966}967EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);968969/**970* drm_atomic_helper_check_crtc_primary_plane() - Check CRTC state for primary plane971* @crtc_state: CRTC state to check972*973* Checks that a CRTC has at least one primary plane attached to it, which is974* a requirement on some hardware. Note that this only involves the CRTC side975* of the test. To test if the primary plane is visible or if it can be updated976* without the CRTC being enabled, use drm_atomic_helper_check_plane_state() in977* the plane's atomic check.978*979* RETURNS:980* 0 if a primary plane is attached to the CRTC, or an error code otherwise981*/982int drm_atomic_helper_check_crtc_primary_plane(struct drm_crtc_state *crtc_state)983{984struct drm_crtc *crtc = crtc_state->crtc;985struct drm_device *dev = crtc->dev;986struct drm_plane *plane;987988/* needs at least one primary plane to be enabled */989drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {990if (plane->type == DRM_PLANE_TYPE_PRIMARY)991return 0;992}993994drm_dbg_atomic(dev, "[CRTC:%d:%s] primary plane missing\n", crtc->base.id, crtc->name);995996return -EINVAL;997}998EXPORT_SYMBOL(drm_atomic_helper_check_crtc_primary_plane);9991000/**1001* drm_atomic_helper_check_planes - validate state object for planes changes1002* @dev: DRM device1003* @state: the driver state object1004*1005* Check the state object to see if the requested state is physically possible.1006* This does all the plane update related checks using by calling into the1007* &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check1008* hooks provided by the driver.1009*1010* It also sets &drm_crtc_state.planes_changed to indicate that a CRTC has1011* updated planes.1012*1013* RETURNS:1014* Zero for success or -errno1015*/1016int1017drm_atomic_helper_check_planes(struct drm_device *dev,1018struct drm_atomic_state *state)1019{1020struct drm_crtc *crtc;1021struct drm_crtc_state *new_crtc_state;1022struct drm_plane *plane;1023struct drm_plane_state *new_plane_state, *old_plane_state;1024int i, ret = 0;10251026for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {1027const struct drm_plane_helper_funcs *funcs;10281029WARN_ON(!drm_modeset_is_locked(&plane->mutex));10301031funcs = plane->helper_private;10321033drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);10341035drm_atomic_helper_check_plane_damage(state, new_plane_state);10361037if (!funcs || !funcs->atomic_check)1038continue;10391040ret = funcs->atomic_check(plane, state);1041if (ret) {1042drm_dbg_atomic(plane->dev,1043"[PLANE:%d:%s] atomic driver check failed\n",1044plane->base.id, plane->name);1045return ret;1046}1047}10481049for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {1050const struct drm_crtc_helper_funcs *funcs;10511052funcs = crtc->helper_private;10531054if (!funcs || !funcs->atomic_check)1055continue;10561057ret = funcs->atomic_check(crtc, state);1058if (ret) {1059drm_dbg_atomic(crtc->dev,1060"[CRTC:%d:%s] atomic driver check failed\n",1061crtc->base.id, crtc->name);1062return ret;1063}1064}10651066return ret;1067}1068EXPORT_SYMBOL(drm_atomic_helper_check_planes);10691070/**1071* drm_atomic_helper_check - validate state object1072* @dev: DRM device1073* @state: the driver state object1074*1075* Check the state object to see if the requested state is physically possible.1076* Only CRTCs and planes have check callbacks, so for any additional (global)1077* checking that a driver needs it can simply wrap that around this function.1078* Drivers without such needs can directly use this as their1079* &drm_mode_config_funcs.atomic_check callback.1080*1081* This just wraps the two parts of the state checking for planes and modeset1082* state in the default order: First it calls drm_atomic_helper_check_modeset()1083* and then drm_atomic_helper_check_planes(). The assumption is that the1084* @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check1085* functions depend upon an updated adjusted_mode.clock to e.g. properly compute1086* watermarks.1087*1088* Note that zpos normalization will add all enable planes to the state which1089* might not desired for some drivers.1090* For example enable/disable of a cursor plane which have fixed zpos value1091* would trigger all other enabled planes to be forced to the state change.1092*1093* IMPORTANT:1094*1095* As this function calls drm_atomic_helper_check_modeset() internally, its1096* restrictions also apply:1097* Drivers which set &drm_crtc_state.mode_changed (e.g. in their1098* &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done1099* without a full modeset) _must_ call drm_atomic_helper_check_modeset()1100* function again after that change.1101*1102* RETURNS:1103* Zero for success or -errno1104*/1105int drm_atomic_helper_check(struct drm_device *dev,1106struct drm_atomic_state *state)1107{1108int ret;11091110ret = drm_atomic_helper_check_modeset(dev, state);1111if (ret)1112return ret;11131114if (dev->mode_config.normalize_zpos) {1115ret = drm_atomic_normalize_zpos(dev, state);1116if (ret)1117return ret;1118}11191120ret = drm_atomic_helper_check_planes(dev, state);1121if (ret)1122return ret;11231124if (state->legacy_cursor_update)1125state->async_update = !drm_atomic_helper_async_check(dev, state);11261127drm_self_refresh_helper_alter_state(state);11281129return ret;1130}1131EXPORT_SYMBOL(drm_atomic_helper_check);11321133static bool1134crtc_needs_disable(struct drm_crtc_state *old_state,1135struct drm_crtc_state *new_state)1136{1137/*1138* No new_state means the CRTC is off, so the only criteria is whether1139* it's currently active or in self refresh mode.1140*/1141if (!new_state)1142return drm_atomic_crtc_effectively_active(old_state);11431144/*1145* We need to disable bridge(s) and CRTC if we're transitioning out of1146* self-refresh and changing CRTCs at the same time, because the1147* bridge tracks self-refresh status via CRTC state.1148*/1149if (old_state->self_refresh_active &&1150old_state->crtc != new_state->crtc)1151return true;11521153/*1154* We also need to run through the crtc_funcs->disable() function if1155* the CRTC is currently on, if it's transitioning to self refresh1156* mode, or if it's in self refresh mode and needs to be fully1157* disabled.1158*/1159return old_state->active ||1160(old_state->self_refresh_active && !new_state->active) ||1161new_state->self_refresh_active;1162}11631164/**1165* drm_atomic_helper_commit_encoder_bridge_disable - disable bridges and encoder1166* @dev: DRM device1167* @state: the driver state object1168*1169* Loops over all connectors in the current state and if the CRTC needs1170* it, disables the bridge chain all the way, then disables the encoder1171* afterwards.1172*/1173void1174drm_atomic_helper_commit_encoder_bridge_disable(struct drm_device *dev,1175struct drm_atomic_state *state)1176{1177struct drm_connector *connector;1178struct drm_connector_state *old_conn_state, *new_conn_state;1179struct drm_crtc_state *old_crtc_state, *new_crtc_state;1180int i;11811182for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {1183const struct drm_encoder_helper_funcs *funcs;1184struct drm_encoder *encoder;1185struct drm_bridge *bridge;11861187/*1188* Shut down everything that's in the changeset and currently1189* still on. So need to check the old, saved state.1190*/1191if (!old_conn_state->crtc)1192continue;11931194old_crtc_state = drm_atomic_get_old_crtc_state(state, old_conn_state->crtc);11951196if (new_conn_state->crtc)1197new_crtc_state = drm_atomic_get_new_crtc_state(1198state,1199new_conn_state->crtc);1200else1201new_crtc_state = NULL;12021203if (!crtc_needs_disable(old_crtc_state, new_crtc_state) ||1204!drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))1205continue;12061207encoder = old_conn_state->best_encoder;12081209/* We shouldn't get this far if we didn't previously have1210* an encoder.. but WARN_ON() rather than explode.1211*/1212if (WARN_ON(!encoder))1213continue;12141215funcs = encoder->helper_private;12161217drm_dbg_atomic(dev, "disabling [ENCODER:%d:%s]\n",1218encoder->base.id, encoder->name);12191220/*1221* Each encoder has at most one connector (since we always steal1222* it away), so we won't call disable hooks twice.1223*/1224bridge = drm_bridge_chain_get_first_bridge(encoder);1225drm_atomic_bridge_chain_disable(bridge, state);1226drm_bridge_put(bridge);12271228/* Right function depends upon target state. */1229if (funcs) {1230if (funcs->atomic_disable)1231funcs->atomic_disable(encoder, state);1232else if (new_conn_state->crtc && funcs->prepare)1233funcs->prepare(encoder);1234else if (funcs->disable)1235funcs->disable(encoder);1236else if (funcs->dpms)1237funcs->dpms(encoder, DRM_MODE_DPMS_OFF);1238}1239}1240}1241EXPORT_SYMBOL(drm_atomic_helper_commit_encoder_bridge_disable);12421243/**1244* drm_atomic_helper_commit_crtc_disable - disable CRTSs1245* @dev: DRM device1246* @state: the driver state object1247*1248* Loops over all CRTCs in the current state and if the CRTC needs1249* it, disables it.1250*/1251void1252drm_atomic_helper_commit_crtc_disable(struct drm_device *dev, struct drm_atomic_state *state)1253{1254struct drm_crtc *crtc;1255struct drm_crtc_state *old_crtc_state, *new_crtc_state;1256int i;12571258for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {1259const struct drm_crtc_helper_funcs *funcs;1260int ret;12611262/* Shut down everything that needs a full modeset. */1263if (!drm_atomic_crtc_needs_modeset(new_crtc_state))1264continue;12651266if (!crtc_needs_disable(old_crtc_state, new_crtc_state))1267continue;12681269funcs = crtc->helper_private;12701271drm_dbg_atomic(dev, "disabling [CRTC:%d:%s]\n",1272crtc->base.id, crtc->name);127312741275/* Right function depends upon target state. */1276if (new_crtc_state->enable && funcs->prepare)1277funcs->prepare(crtc);1278else if (funcs->atomic_disable)1279funcs->atomic_disable(crtc, state);1280else if (funcs->disable)1281funcs->disable(crtc);1282else if (funcs->dpms)1283funcs->dpms(crtc, DRM_MODE_DPMS_OFF);12841285if (!drm_dev_has_vblank(dev))1286continue;12871288ret = drm_crtc_vblank_get(crtc);1289/*1290* Self-refresh is not a true "disable"; ensure vblank remains1291* enabled.1292*/1293if (new_crtc_state->self_refresh_active)1294WARN_ONCE(ret != 0,1295"driver disabled vblank in self-refresh\n");1296else1297WARN_ONCE(ret != -EINVAL,1298"driver forgot to call drm_crtc_vblank_off()\n");1299if (ret == 0)1300drm_crtc_vblank_put(crtc);1301}1302}1303EXPORT_SYMBOL(drm_atomic_helper_commit_crtc_disable);13041305/**1306* drm_atomic_helper_commit_encoder_bridge_post_disable - post-disable encoder bridges1307* @dev: DRM device1308* @state: the driver state object1309*1310* Loops over all connectors in the current state and if the CRTC needs1311* it, post-disables all encoder bridges.1312*/1313void1314drm_atomic_helper_commit_encoder_bridge_post_disable(struct drm_device *dev, struct drm_atomic_state *state)1315{1316struct drm_connector *connector;1317struct drm_connector_state *old_conn_state, *new_conn_state;1318struct drm_crtc_state *old_crtc_state, *new_crtc_state;1319int i;13201321for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {1322struct drm_encoder *encoder;1323struct drm_bridge *bridge;13241325/*1326* Shut down everything that's in the changeset and currently1327* still on. So need to check the old, saved state.1328*/1329if (!old_conn_state->crtc)1330continue;13311332old_crtc_state = drm_atomic_get_old_crtc_state(state, old_conn_state->crtc);13331334if (new_conn_state->crtc)1335new_crtc_state = drm_atomic_get_new_crtc_state(state,1336new_conn_state->crtc);1337else1338new_crtc_state = NULL;13391340if (!crtc_needs_disable(old_crtc_state, new_crtc_state) ||1341!drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))1342continue;13431344encoder = old_conn_state->best_encoder;13451346/*1347* We shouldn't get this far if we didn't previously have1348* an encoder.. but WARN_ON() rather than explode.1349*/1350if (WARN_ON(!encoder))1351continue;13521353drm_dbg_atomic(dev, "post-disabling bridges [ENCODER:%d:%s]\n",1354encoder->base.id, encoder->name);13551356/*1357* Each encoder has at most one connector (since we always steal1358* it away), so we won't call disable hooks twice.1359*/1360bridge = drm_bridge_chain_get_first_bridge(encoder);1361drm_atomic_bridge_chain_post_disable(bridge, state);1362drm_bridge_put(bridge);1363}1364}1365EXPORT_SYMBOL(drm_atomic_helper_commit_encoder_bridge_post_disable);13661367static void1368disable_outputs(struct drm_device *dev, struct drm_atomic_state *state)1369{1370drm_atomic_helper_commit_encoder_bridge_disable(dev, state);13711372drm_atomic_helper_commit_encoder_bridge_post_disable(dev, state);13731374drm_atomic_helper_commit_crtc_disable(dev, state);1375}13761377/**1378* drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state1379* @dev: DRM device1380* @state: atomic state object being committed1381*1382* This function updates all the various legacy modeset state pointers in1383* connectors, encoders and CRTCs.1384*1385* Drivers can use this for building their own atomic commit if they don't have1386* a pure helper-based modeset implementation.1387*1388* Since these updates are not synchronized with lockings, only code paths1389* called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the1390* legacy state filled out by this helper. Defacto this means this helper and1391* the legacy state pointers are only really useful for transitioning an1392* existing driver to the atomic world.1393*/1394void1395drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,1396struct drm_atomic_state *state)1397{1398struct drm_connector *connector;1399struct drm_connector_state *old_conn_state, *new_conn_state;1400struct drm_crtc *crtc;1401struct drm_crtc_state *new_crtc_state;1402int i;14031404/* clear out existing links and update dpms */1405for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {1406if (connector->encoder) {1407WARN_ON(!connector->encoder->crtc);14081409connector->encoder->crtc = NULL;1410connector->encoder = NULL;1411}14121413crtc = new_conn_state->crtc;1414if ((!crtc && old_conn_state->crtc) ||1415(crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {1416int mode = DRM_MODE_DPMS_OFF;14171418if (crtc && crtc->state->active)1419mode = DRM_MODE_DPMS_ON;14201421connector->dpms = mode;1422}1423}14241425/* set new links */1426for_each_new_connector_in_state(state, connector, new_conn_state, i) {1427if (!new_conn_state->crtc)1428continue;14291430if (WARN_ON(!new_conn_state->best_encoder))1431continue;14321433connector->encoder = new_conn_state->best_encoder;1434connector->encoder->crtc = new_conn_state->crtc;1435}14361437/* set legacy state in the crtc structure */1438for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {1439struct drm_plane *primary = crtc->primary;1440struct drm_plane_state *new_plane_state;14411442crtc->mode = new_crtc_state->mode;1443crtc->enabled = new_crtc_state->enable;14441445new_plane_state =1446drm_atomic_get_new_plane_state(state, primary);14471448if (new_plane_state && new_plane_state->crtc == crtc) {1449crtc->x = new_plane_state->src_x >> 16;1450crtc->y = new_plane_state->src_y >> 16;1451}1452}1453}1454EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);14551456/**1457* drm_atomic_helper_calc_timestamping_constants - update vblank timestamping constants1458* @state: atomic state object1459*1460* Updates the timestamping constants used for precise vblank timestamps1461* by calling drm_calc_timestamping_constants() for all enabled crtcs in @state.1462*/1463void drm_atomic_helper_calc_timestamping_constants(struct drm_atomic_state *state)1464{1465struct drm_crtc_state *new_crtc_state;1466struct drm_crtc *crtc;1467int i;14681469for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {1470if (new_crtc_state->enable)1471drm_calc_timestamping_constants(crtc,1472&new_crtc_state->adjusted_mode);1473}1474}1475EXPORT_SYMBOL(drm_atomic_helper_calc_timestamping_constants);14761477/**1478* drm_atomic_helper_commit_crtc_set_mode - set the new mode1479* @dev: DRM device1480* @state: the driver state object1481*1482* Loops over all connectors in the current state and if the mode has1483* changed, change the mode of the CRTC, then call down the bridge1484* chain and change the mode in all bridges as well.1485*/1486void1487drm_atomic_helper_commit_crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *state)1488{1489struct drm_crtc *crtc;1490struct drm_crtc_state *new_crtc_state;1491struct drm_connector *connector;1492struct drm_connector_state *new_conn_state;1493int i;14941495for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {1496const struct drm_crtc_helper_funcs *funcs;14971498if (!new_crtc_state->mode_changed)1499continue;15001501funcs = crtc->helper_private;15021503if (new_crtc_state->enable && funcs->mode_set_nofb) {1504drm_dbg_atomic(dev, "modeset on [CRTC:%d:%s]\n",1505crtc->base.id, crtc->name);15061507funcs->mode_set_nofb(crtc);1508}1509}15101511for_each_new_connector_in_state(state, connector, new_conn_state, i) {1512const struct drm_encoder_helper_funcs *funcs;1513struct drm_encoder *encoder;1514struct drm_display_mode *mode, *adjusted_mode;1515struct drm_bridge *bridge;15161517if (!new_conn_state->best_encoder)1518continue;15191520encoder = new_conn_state->best_encoder;1521funcs = encoder->helper_private;1522new_crtc_state = new_conn_state->crtc->state;1523mode = &new_crtc_state->mode;1524adjusted_mode = &new_crtc_state->adjusted_mode;15251526if (!new_crtc_state->mode_changed && !new_crtc_state->connectors_changed)1527continue;15281529drm_dbg_atomic(dev, "modeset on [ENCODER:%d:%s]\n",1530encoder->base.id, encoder->name);15311532/*1533* Each encoder has at most one connector (since we always steal1534* it away), so we won't call mode_set hooks twice.1535*/1536if (funcs && funcs->atomic_mode_set) {1537funcs->atomic_mode_set(encoder, new_crtc_state,1538new_conn_state);1539} else if (funcs && funcs->mode_set) {1540funcs->mode_set(encoder, mode, adjusted_mode);1541}15421543bridge = drm_bridge_chain_get_first_bridge(encoder);1544drm_bridge_chain_mode_set(bridge, mode, adjusted_mode);1545drm_bridge_put(bridge);1546}1547}1548EXPORT_SYMBOL(drm_atomic_helper_commit_crtc_set_mode);15491550/**1551* drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs1552* @dev: DRM device1553* @state: atomic state object being committed1554*1555* This function shuts down all the outputs that need to be shut down and1556* prepares them (if required) with the new mode.1557*1558* For compatibility with legacy CRTC helpers this should be called before1559* drm_atomic_helper_commit_planes(), which is what the default commit function1560* does. But drivers with different needs can group the modeset commits together1561* and do the plane commits at the end. This is useful for drivers doing runtime1562* PM since planes updates then only happen when the CRTC is actually enabled.1563*/1564void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,1565struct drm_atomic_state *state)1566{1567disable_outputs(dev, state);15681569drm_atomic_helper_update_legacy_modeset_state(dev, state);1570drm_atomic_helper_calc_timestamping_constants(state);15711572drm_atomic_helper_commit_crtc_set_mode(dev, state);1573}1574EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);15751576/**1577* drm_atomic_helper_commit_writebacks - issue writebacks1578* @dev: DRM device1579* @state: atomic state object being committed1580*1581* This loops over the connectors, checks if the new state requires1582* a writeback job to be issued and in that case issues an atomic1583* commit on each connector.1584*/1585void drm_atomic_helper_commit_writebacks(struct drm_device *dev,1586struct drm_atomic_state *state)1587{1588struct drm_connector *connector;1589struct drm_connector_state *new_conn_state;1590int i;15911592for_each_new_connector_in_state(state, connector, new_conn_state, i) {1593const struct drm_connector_helper_funcs *funcs;15941595funcs = connector->helper_private;1596if (!funcs->atomic_commit)1597continue;15981599if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {1600WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);1601funcs->atomic_commit(connector, state);1602}1603}1604}1605EXPORT_SYMBOL(drm_atomic_helper_commit_writebacks);16061607/**1608* drm_atomic_helper_commit_encoder_bridge_pre_enable - pre-enable bridges1609* @dev: DRM device1610* @state: atomic state object being committed1611*1612* This loops over the connectors and if the CRTC needs it, pre-enables1613* the entire bridge chain.1614*/1615void1616drm_atomic_helper_commit_encoder_bridge_pre_enable(struct drm_device *dev, struct drm_atomic_state *state)1617{1618struct drm_connector *connector;1619struct drm_connector_state *new_conn_state;1620int i;16211622for_each_new_connector_in_state(state, connector, new_conn_state, i) {1623struct drm_encoder *encoder;1624struct drm_bridge *bridge;16251626if (!new_conn_state->best_encoder)1627continue;16281629if (!new_conn_state->crtc->state->active ||1630!drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))1631continue;16321633encoder = new_conn_state->best_encoder;16341635drm_dbg_atomic(dev, "pre-enabling bridges [ENCODER:%d:%s]\n",1636encoder->base.id, encoder->name);16371638/*1639* Each encoder has at most one connector (since we always steal1640* it away), so we won't call enable hooks twice.1641*/1642bridge = drm_bridge_chain_get_first_bridge(encoder);1643drm_atomic_bridge_chain_pre_enable(bridge, state);1644drm_bridge_put(bridge);1645}1646}1647EXPORT_SYMBOL(drm_atomic_helper_commit_encoder_bridge_pre_enable);16481649/**1650* drm_atomic_helper_commit_crtc_enable - enables the CRTCs1651* @dev: DRM device1652* @state: atomic state object being committed1653*1654* This loops over CRTCs in the new state, and of the CRTC needs1655* it, enables it.1656*/1657void1658drm_atomic_helper_commit_crtc_enable(struct drm_device *dev, struct drm_atomic_state *state)1659{1660struct drm_crtc *crtc;1661struct drm_crtc_state *old_crtc_state;1662struct drm_crtc_state *new_crtc_state;1663int i;16641665for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {1666const struct drm_crtc_helper_funcs *funcs;16671668/* Need to filter out CRTCs where only planes change. */1669if (!drm_atomic_crtc_needs_modeset(new_crtc_state))1670continue;16711672if (!new_crtc_state->active)1673continue;16741675funcs = crtc->helper_private;16761677if (new_crtc_state->enable) {1678drm_dbg_atomic(dev, "enabling [CRTC:%d:%s]\n",1679crtc->base.id, crtc->name);1680if (funcs->atomic_enable)1681funcs->atomic_enable(crtc, state);1682else if (funcs->commit)1683funcs->commit(crtc);1684}1685}1686}1687EXPORT_SYMBOL(drm_atomic_helper_commit_crtc_enable);16881689/**1690* drm_atomic_helper_commit_encoder_bridge_enable - enables the bridges1691* @dev: DRM device1692* @state: atomic state object being committed1693*1694* This loops over all connectors in the new state, and of the CRTC needs1695* it, enables the entire bridge chain.1696*/1697void1698drm_atomic_helper_commit_encoder_bridge_enable(struct drm_device *dev, struct drm_atomic_state *state)1699{1700struct drm_connector *connector;1701struct drm_connector_state *new_conn_state;1702int i;17031704for_each_new_connector_in_state(state, connector, new_conn_state, i) {1705const struct drm_encoder_helper_funcs *funcs;1706struct drm_encoder *encoder;1707struct drm_bridge *bridge;17081709if (!new_conn_state->best_encoder)1710continue;17111712if (!new_conn_state->crtc->state->active ||1713!drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))1714continue;17151716encoder = new_conn_state->best_encoder;1717funcs = encoder->helper_private;17181719drm_dbg_atomic(dev, "enabling [ENCODER:%d:%s]\n",1720encoder->base.id, encoder->name);17211722/*1723* Each encoder has at most one connector (since we always steal1724* it away), so we won't call enable hooks twice.1725*/1726bridge = drm_bridge_chain_get_first_bridge(encoder);17271728if (funcs) {1729if (funcs->atomic_enable)1730funcs->atomic_enable(encoder, state);1731else if (funcs->enable)1732funcs->enable(encoder);1733else if (funcs->commit)1734funcs->commit(encoder);1735}17361737drm_atomic_bridge_chain_enable(bridge, state);1738drm_bridge_put(bridge);1739}1740}1741EXPORT_SYMBOL(drm_atomic_helper_commit_encoder_bridge_enable);17421743/**1744* drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs1745* @dev: DRM device1746* @state: atomic state object being committed1747*1748* This function enables all the outputs with the new configuration which had to1749* be turned off for the update.1750*1751* For compatibility with legacy CRTC helpers this should be called after1752* drm_atomic_helper_commit_planes(), which is what the default commit function1753* does. But drivers with different needs can group the modeset commits together1754* and do the plane commits at the end. This is useful for drivers doing runtime1755* PM since planes updates then only happen when the CRTC is actually enabled.1756*/1757void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,1758struct drm_atomic_state *state)1759{1760drm_atomic_helper_commit_crtc_enable(dev, state);17611762drm_atomic_helper_commit_encoder_bridge_pre_enable(dev, state);17631764drm_atomic_helper_commit_encoder_bridge_enable(dev, state);17651766drm_atomic_helper_commit_writebacks(dev, state);1767}1768EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);17691770/*1771* For atomic updates which touch just a single CRTC, calculate the time of the1772* next vblank, and inform all the fences of the deadline.1773*/1774static void set_fence_deadline(struct drm_device *dev,1775struct drm_atomic_state *state)1776{1777struct drm_crtc *crtc;1778struct drm_crtc_state *new_crtc_state;1779struct drm_plane *plane;1780struct drm_plane_state *new_plane_state;1781ktime_t vbltime = 0;1782int i;17831784for_each_new_crtc_in_state (state, crtc, new_crtc_state, i) {1785ktime_t v;17861787if (drm_atomic_crtc_needs_modeset(new_crtc_state))1788continue;17891790if (!new_crtc_state->active)1791continue;17921793if (drm_crtc_next_vblank_start(crtc, &v))1794continue;17951796if (!vbltime || ktime_before(v, vbltime))1797vbltime = v;1798}17991800/* If no CRTCs updated, then nothing to do: */1801if (!vbltime)1802return;18031804for_each_new_plane_in_state (state, plane, new_plane_state, i) {1805if (!new_plane_state->fence)1806continue;1807dma_fence_set_deadline(new_plane_state->fence, vbltime);1808}1809}18101811/**1812* drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state1813* @dev: DRM device1814* @state: atomic state object with old state structures1815* @pre_swap: If true, do an interruptible wait, and @state is the new state.1816* Otherwise @state is the old state.1817*1818* For implicit sync, driver should fish the exclusive fence out from the1819* incoming fb's and stash it in the drm_plane_state. This is called after1820* drm_atomic_helper_swap_state() so it uses the current plane state (and1821* just uses the atomic state to find the changed planes)1822*1823* Note that @pre_swap is needed since the point where we block for fences moves1824* around depending upon whether an atomic commit is blocking or1825* non-blocking. For non-blocking commit all waiting needs to happen after1826* drm_atomic_helper_swap_state() is called, but for blocking commits we want1827* to wait **before** we do anything that can't be easily rolled back. That is1828* before we call drm_atomic_helper_swap_state().1829*1830* Returns zero if success or < 0 if dma_fence_wait() fails.1831*/1832int drm_atomic_helper_wait_for_fences(struct drm_device *dev,1833struct drm_atomic_state *state,1834bool pre_swap)1835{1836struct drm_plane *plane;1837struct drm_plane_state *new_plane_state;1838int i, ret;18391840set_fence_deadline(dev, state);18411842for_each_new_plane_in_state(state, plane, new_plane_state, i) {1843if (!new_plane_state->fence)1844continue;18451846WARN_ON(!new_plane_state->fb);18471848/*1849* If waiting for fences pre-swap (ie: nonblock), userspace can1850* still interrupt the operation. Instead of blocking until the1851* timer expires, make the wait interruptible.1852*/1853ret = dma_fence_wait(new_plane_state->fence, pre_swap);1854if (ret)1855return ret;18561857dma_fence_put(new_plane_state->fence);1858new_plane_state->fence = NULL;1859}18601861return 0;1862}1863EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);18641865/**1866* drm_atomic_helper_wait_for_vblanks - wait for vblank on CRTCs1867* @dev: DRM device1868* @state: atomic state object being committed1869*1870* Helper to, after atomic commit, wait for vblanks on all affected1871* CRTCs (ie. before cleaning up old framebuffers using1872* drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the1873* framebuffers have actually changed to optimize for the legacy cursor and1874* plane update use-case.1875*1876* Drivers using the nonblocking commit tracking support initialized by calling1877* drm_atomic_helper_setup_commit() should look at1878* drm_atomic_helper_wait_for_flip_done() as an alternative.1879*/1880void1881drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,1882struct drm_atomic_state *state)1883{1884struct drm_crtc *crtc;1885struct drm_crtc_state *old_crtc_state, *new_crtc_state;1886int i, ret;1887unsigned int crtc_mask = 0;18881889/*1890* Legacy cursor ioctls are completely unsynced, and userspace1891* relies on that (by doing tons of cursor updates).1892*/1893if (state->legacy_cursor_update)1894return;18951896for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {1897if (!new_crtc_state->active)1898continue;18991900ret = drm_crtc_vblank_get(crtc);1901if (ret != 0)1902continue;19031904crtc_mask |= drm_crtc_mask(crtc);1905state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);1906}19071908for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {1909wait_queue_head_t *queue = drm_crtc_vblank_waitqueue(crtc);19101911if (!(crtc_mask & drm_crtc_mask(crtc)))1912continue;19131914ret = wait_event_timeout(*queue,1915state->crtcs[i].last_vblank_count !=1916drm_crtc_vblank_count(crtc),1917msecs_to_jiffies(100));19181919WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",1920crtc->base.id, crtc->name);19211922drm_crtc_vblank_put(crtc);1923}1924}1925EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);19261927/**1928* drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done1929* @dev: DRM device1930* @state: atomic state object being committed1931*1932* Helper to, after atomic commit, wait for page flips on all affected1933* crtcs (ie. before cleaning up old framebuffers using1934* drm_atomic_helper_cleanup_planes()). Compared to1935* drm_atomic_helper_wait_for_vblanks() this waits for the completion on all1936* CRTCs, assuming that cursors-only updates are signalling their completion1937* immediately (or using a different path).1938*1939* This requires that drivers use the nonblocking commit tracking support1940* initialized using drm_atomic_helper_setup_commit().1941*/1942void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,1943struct drm_atomic_state *state)1944{1945struct drm_crtc *crtc;1946int i;19471948for (i = 0; i < dev->mode_config.num_crtc; i++) {1949struct drm_crtc_commit *commit = state->crtcs[i].commit;1950int ret;19511952crtc = state->crtcs[i].ptr;19531954if (!crtc || !commit)1955continue;19561957ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);1958if (ret == 0)1959drm_err(dev, "[CRTC:%d:%s] flip_done timed out\n",1960crtc->base.id, crtc->name);1961}19621963if (state->fake_commit)1964complete_all(&state->fake_commit->flip_done);1965}1966EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);19671968/**1969* drm_atomic_helper_commit_tail - commit atomic update to hardware1970* @state: atomic state object being committed1971*1972* This is the default implementation for the1973* &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers1974* that do not support runtime_pm or do not need the CRTC to be1975* enabled to perform a commit. Otherwise, see1976* drm_atomic_helper_commit_tail_rpm().1977*1978* Note that the default ordering of how the various stages are called is to1979* match the legacy modeset helper library closest.1980*/1981void drm_atomic_helper_commit_tail(struct drm_atomic_state *state)1982{1983struct drm_device *dev = state->dev;19841985drm_atomic_helper_commit_modeset_disables(dev, state);19861987drm_atomic_helper_commit_planes(dev, state, 0);19881989drm_atomic_helper_commit_modeset_enables(dev, state);19901991drm_atomic_helper_fake_vblank(state);19921993drm_atomic_helper_commit_hw_done(state);19941995drm_atomic_helper_wait_for_vblanks(dev, state);19961997drm_atomic_helper_cleanup_planes(dev, state);1998}1999EXPORT_SYMBOL(drm_atomic_helper_commit_tail);20002001/**2002* drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware2003* @state: new modeset state to be committed2004*2005* This is an alternative implementation for the2006* &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers2007* that support runtime_pm or need the CRTC to be enabled to perform a2008* commit. Otherwise, one should use the default implementation2009* drm_atomic_helper_commit_tail().2010*/2011void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *state)2012{2013struct drm_device *dev = state->dev;20142015drm_atomic_helper_commit_modeset_disables(dev, state);20162017drm_atomic_helper_commit_modeset_enables(dev, state);20182019drm_atomic_helper_commit_planes(dev, state,2020DRM_PLANE_COMMIT_ACTIVE_ONLY);20212022drm_atomic_helper_fake_vblank(state);20232024drm_atomic_helper_commit_hw_done(state);20252026drm_atomic_helper_wait_for_vblanks(dev, state);20272028drm_atomic_helper_cleanup_planes(dev, state);2029}2030EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);20312032static void commit_tail(struct drm_atomic_state *state)2033{2034struct drm_device *dev = state->dev;2035const struct drm_mode_config_helper_funcs *funcs;2036struct drm_crtc_state *new_crtc_state;2037struct drm_crtc *crtc;2038ktime_t start;2039s64 commit_time_ms;2040unsigned int i, new_self_refresh_mask = 0;20412042funcs = dev->mode_config.helper_private;20432044/*2045* We're measuring the _entire_ commit, so the time will vary depending2046* on how many fences and objects are involved. For the purposes of self2047* refresh, this is desirable since it'll give us an idea of how2048* congested things are. This will inform our decision on how often we2049* should enter self refresh after idle.2050*2051* These times will be averaged out in the self refresh helpers to avoid2052* overreacting over one outlier frame2053*/2054start = ktime_get();20552056drm_atomic_helper_wait_for_fences(dev, state, false);20572058drm_atomic_helper_wait_for_dependencies(state);20592060/*2061* We cannot safely access new_crtc_state after2062* drm_atomic_helper_commit_hw_done() so figure out which crtc's have2063* self-refresh active beforehand:2064*/2065for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)2066if (new_crtc_state->self_refresh_active)2067new_self_refresh_mask |= BIT(i);20682069if (funcs && funcs->atomic_commit_tail)2070funcs->atomic_commit_tail(state);2071else2072drm_atomic_helper_commit_tail(state);20732074commit_time_ms = ktime_ms_delta(ktime_get(), start);2075if (commit_time_ms > 0)2076drm_self_refresh_helper_update_avg_times(state,2077(unsigned long)commit_time_ms,2078new_self_refresh_mask);20792080drm_atomic_helper_commit_cleanup_done(state);20812082drm_atomic_state_put(state);2083}20842085static void commit_work(struct work_struct *work)2086{2087struct drm_atomic_state *state = container_of(work,2088struct drm_atomic_state,2089commit_work);2090commit_tail(state);2091}20922093/**2094* drm_atomic_helper_async_check - check if state can be committed asynchronously2095* @dev: DRM device2096* @state: the driver state object2097*2098* This helper will check if it is possible to commit the state asynchronously.2099* Async commits are not supposed to swap the states like normal sync commits2100* but just do in-place changes on the current state.2101*2102* It will return 0 if the commit can happen in an asynchronous fashion or error2103* if not. Note that error just mean it can't be committed asynchronously, if it2104* fails the commit should be treated like a normal synchronous commit.2105*/2106int drm_atomic_helper_async_check(struct drm_device *dev,2107struct drm_atomic_state *state)2108{2109struct drm_crtc *crtc;2110struct drm_crtc_state *crtc_state;2111struct drm_plane *plane = NULL;2112struct drm_plane_state *old_plane_state = NULL;2113struct drm_plane_state *new_plane_state = NULL;2114const struct drm_plane_helper_funcs *funcs;2115int i, ret, n_planes = 0;21162117for_each_new_crtc_in_state(state, crtc, crtc_state, i) {2118if (drm_atomic_crtc_needs_modeset(crtc_state))2119return -EINVAL;2120}21212122for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)2123n_planes++;21242125/* FIXME: we support only single plane updates for now */2126if (n_planes != 1) {2127drm_dbg_atomic(dev,2128"only single plane async updates are supported\n");2129return -EINVAL;2130}21312132if (!new_plane_state->crtc ||2133old_plane_state->crtc != new_plane_state->crtc) {2134drm_dbg_atomic(dev,2135"[PLANE:%d:%s] async update cannot change CRTC\n",2136plane->base.id, plane->name);2137return -EINVAL;2138}21392140funcs = plane->helper_private;2141if (!funcs->atomic_async_update) {2142drm_dbg_atomic(dev,2143"[PLANE:%d:%s] driver does not support async updates\n",2144plane->base.id, plane->name);2145return -EINVAL;2146}21472148if (new_plane_state->fence) {2149drm_dbg_atomic(dev,2150"[PLANE:%d:%s] missing fence for async update\n",2151plane->base.id, plane->name);2152return -EINVAL;2153}21542155/*2156* Don't do an async update if there is an outstanding commit modifying2157* the plane. This prevents our async update's changes from getting2158* overridden by a previous synchronous update's state.2159*/2160if (old_plane_state->commit &&2161!try_wait_for_completion(&old_plane_state->commit->hw_done)) {2162drm_dbg_atomic(dev,2163"[PLANE:%d:%s] inflight previous commit preventing async commit\n",2164plane->base.id, plane->name);2165return -EBUSY;2166}21672168ret = funcs->atomic_async_check(plane, state, false);2169if (ret != 0)2170drm_dbg_atomic(dev,2171"[PLANE:%d:%s] driver async check failed\n",2172plane->base.id, plane->name);2173return ret;2174}2175EXPORT_SYMBOL(drm_atomic_helper_async_check);21762177/**2178* drm_atomic_helper_async_commit - commit state asynchronously2179* @dev: DRM device2180* @state: the driver state object2181*2182* This function commits a state asynchronously, i.e., not vblank2183* synchronized. It should be used on a state only when2184* drm_atomic_async_check() succeeds. Async commits are not supposed to swap2185* the states like normal sync commits, but just do in-place changes on the2186* current state.2187*2188* TODO: Implement full swap instead of doing in-place changes.2189*/2190void drm_atomic_helper_async_commit(struct drm_device *dev,2191struct drm_atomic_state *state)2192{2193struct drm_plane *plane;2194struct drm_plane_state *plane_state;2195const struct drm_plane_helper_funcs *funcs;2196int i;21972198for_each_new_plane_in_state(state, plane, plane_state, i) {2199struct drm_framebuffer *new_fb = plane_state->fb;2200struct drm_framebuffer *old_fb = plane->state->fb;22012202funcs = plane->helper_private;2203funcs->atomic_async_update(plane, state);22042205/*2206* ->atomic_async_update() is supposed to update the2207* plane->state in-place, make sure at least common2208* properties have been properly updated.2209*/2210WARN_ON_ONCE(plane->state->fb != new_fb);2211WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);2212WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);2213WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);2214WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);22152216/*2217* Make sure the FBs have been swapped so that cleanups in the2218* new_state performs a cleanup in the old FB.2219*/2220WARN_ON_ONCE(plane_state->fb != old_fb);2221}2222}2223EXPORT_SYMBOL(drm_atomic_helper_async_commit);22242225/**2226* drm_atomic_helper_commit - commit validated state object2227* @dev: DRM device2228* @state: the driver state object2229* @nonblock: whether nonblocking behavior is requested.2230*2231* This function commits a with drm_atomic_helper_check() pre-validated state2232* object. This can still fail when e.g. the framebuffer reservation fails. This2233* function implements nonblocking commits, using2234* drm_atomic_helper_setup_commit() and related functions.2235*2236* Committing the actual hardware state is done through the2237* &drm_mode_config_helper_funcs.atomic_commit_tail callback, or its default2238* implementation drm_atomic_helper_commit_tail().2239*2240* RETURNS:2241* Zero for success or -errno.2242*/2243int drm_atomic_helper_commit(struct drm_device *dev,2244struct drm_atomic_state *state,2245bool nonblock)2246{2247int ret;22482249if (state->async_update) {2250ret = drm_atomic_helper_prepare_planes(dev, state);2251if (ret)2252return ret;22532254drm_atomic_helper_async_commit(dev, state);2255drm_atomic_helper_unprepare_planes(dev, state);22562257return 0;2258}22592260ret = drm_atomic_helper_setup_commit(state, nonblock);2261if (ret)2262return ret;22632264INIT_WORK(&state->commit_work, commit_work);22652266ret = drm_atomic_helper_prepare_planes(dev, state);2267if (ret)2268return ret;22692270if (!nonblock) {2271ret = drm_atomic_helper_wait_for_fences(dev, state, true);2272if (ret)2273goto err;2274}22752276/*2277* This is the point of no return - everything below never fails except2278* when the hw goes bonghits. Which means we can commit the new state on2279* the software side now.2280*/22812282ret = drm_atomic_helper_swap_state(state, true);2283if (ret)2284goto err;22852286/*2287* Everything below can be run asynchronously without the need to grab2288* any modeset locks at all under one condition: It must be guaranteed2289* that the asynchronous work has either been cancelled (if the driver2290* supports it, which at least requires that the framebuffers get2291* cleaned up with drm_atomic_helper_cleanup_planes()) or completed2292* before the new state gets committed on the software side with2293* drm_atomic_helper_swap_state().2294*2295* This scheme allows new atomic state updates to be prepared and2296* checked in parallel to the asynchronous completion of the previous2297* update. Which is important since compositors need to figure out the2298* composition of the next frame right after having submitted the2299* current layout.2300*2301* NOTE: Commit work has multiple phases, first hardware commit, then2302* cleanup. We want them to overlap, hence need system_unbound_wq to2303* make sure work items don't artificially stall on each another.2304*/23052306drm_atomic_state_get(state);2307if (nonblock)2308queue_work(system_unbound_wq, &state->commit_work);2309else2310commit_tail(state);23112312return 0;23132314err:2315drm_atomic_helper_unprepare_planes(dev, state);2316return ret;2317}2318EXPORT_SYMBOL(drm_atomic_helper_commit);23192320/**2321* DOC: implementing nonblocking commit2322*2323* Nonblocking atomic commits should use struct &drm_crtc_commit to sequence2324* different operations against each another. Locks, especially struct2325* &drm_modeset_lock, should not be held in worker threads or any other2326* asynchronous context used to commit the hardware state.2327*2328* drm_atomic_helper_commit() implements the recommended sequence for2329* nonblocking commits, using drm_atomic_helper_setup_commit() internally:2330*2331* 1. Run drm_atomic_helper_prepare_planes(). Since this can fail and we2332* need to propagate out of memory/VRAM errors to userspace, it must be called2333* synchronously.2334*2335* 2. Synchronize with any outstanding nonblocking commit worker threads which2336* might be affected by the new state update. This is handled by2337* drm_atomic_helper_setup_commit().2338*2339* Asynchronous workers need to have sufficient parallelism to be able to run2340* different atomic commits on different CRTCs in parallel. The simplest way to2341* achieve this is by running them on the &system_unbound_wq work queue. Note2342* that drivers are not required to split up atomic commits and run an2343* individual commit in parallel - userspace is supposed to do that if it cares.2344* But it might be beneficial to do that for modesets, since those necessarily2345* must be done as one global operation, and enabling or disabling a CRTC can2346* take a long time. But even that is not required.2347*2348* IMPORTANT: A &drm_atomic_state update for multiple CRTCs is sequenced2349* against all CRTCs therein. Therefore for atomic state updates which only flip2350* planes the driver must not get the struct &drm_crtc_state of unrelated CRTCs2351* in its atomic check code: This would prevent committing of atomic updates to2352* multiple CRTCs in parallel. In general, adding additional state structures2353* should be avoided as much as possible, because this reduces parallelism in2354* (nonblocking) commits, both due to locking and due to commit sequencing2355* requirements.2356*2357* 3. The software state is updated synchronously with2358* drm_atomic_helper_swap_state(). Doing this under the protection of all modeset2359* locks means concurrent callers never see inconsistent state. Note that commit2360* workers do not hold any locks; their access is only coordinated through2361* ordering. If workers would access state only through the pointers in the2362* free-standing state objects (currently not the case for any driver) then even2363* multiple pending commits could be in-flight at the same time.2364*2365* 4. Schedule a work item to do all subsequent steps, using the split-out2366* commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and2367* then cleaning up the framebuffers after the old framebuffer is no longer2368* being displayed. The scheduled work should synchronize against other workers2369* using the &drm_crtc_commit infrastructure as needed. See2370* drm_atomic_helper_setup_commit() for more details.2371*/23722373static int stall_checks(struct drm_crtc *crtc, bool nonblock)2374{2375struct drm_crtc_commit *commit, *stall_commit = NULL;2376bool completed = true;2377int i;2378long ret = 0;23792380spin_lock(&crtc->commit_lock);2381i = 0;2382list_for_each_entry(commit, &crtc->commit_list, commit_entry) {2383if (i == 0) {2384completed = try_wait_for_completion(&commit->flip_done);2385/*2386* Userspace is not allowed to get ahead of the previous2387* commit with nonblocking ones.2388*/2389if (!completed && nonblock) {2390spin_unlock(&crtc->commit_lock);2391drm_dbg_atomic(crtc->dev,2392"[CRTC:%d:%s] busy with a previous commit\n",2393crtc->base.id, crtc->name);23942395return -EBUSY;2396}2397} else if (i == 1) {2398stall_commit = drm_crtc_commit_get(commit);2399break;2400}24012402i++;2403}2404spin_unlock(&crtc->commit_lock);24052406if (!stall_commit)2407return 0;24082409/* We don't want to let commits get ahead of cleanup work too much,2410* stalling on 2nd previous commit means triple-buffer won't ever stall.2411*/2412ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,241310*HZ);2414if (ret == 0)2415drm_err(crtc->dev, "[CRTC:%d:%s] cleanup_done timed out\n",2416crtc->base.id, crtc->name);24172418drm_crtc_commit_put(stall_commit);24192420return ret < 0 ? ret : 0;2421}24222423static void release_crtc_commit(struct completion *completion)2424{2425struct drm_crtc_commit *commit = container_of(completion,2426typeof(*commit),2427flip_done);24282429drm_crtc_commit_put(commit);2430}24312432static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)2433{2434init_completion(&commit->flip_done);2435init_completion(&commit->hw_done);2436init_completion(&commit->cleanup_done);2437INIT_LIST_HEAD(&commit->commit_entry);2438kref_init(&commit->ref);2439commit->crtc = crtc;2440}24412442static struct drm_crtc_commit *2443crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)2444{2445if (crtc) {2446struct drm_crtc_state *new_crtc_state;24472448new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);24492450return new_crtc_state->commit;2451}24522453if (!state->fake_commit) {2454state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);2455if (!state->fake_commit)2456return NULL;24572458init_commit(state->fake_commit, NULL);2459}24602461return state->fake_commit;2462}24632464/**2465* drm_atomic_helper_setup_commit - setup possibly nonblocking commit2466* @state: new modeset state to be committed2467* @nonblock: whether nonblocking behavior is requested.2468*2469* This function prepares @state to be used by the atomic helper's support for2470* nonblocking commits. Drivers using the nonblocking commit infrastructure2471* should always call this function from their2472* &drm_mode_config_funcs.atomic_commit hook.2473*2474* Drivers that need to extend the commit setup to private objects can use the2475* &drm_mode_config_helper_funcs.atomic_commit_setup hook.2476*2477* To be able to use this support drivers need to use a few more helper2478* functions. drm_atomic_helper_wait_for_dependencies() must be called before2479* actually committing the hardware state, and for nonblocking commits this call2480* must be placed in the async worker. See also drm_atomic_helper_swap_state()2481* and its stall parameter, for when a driver's commit hooks look at the2482* &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.2483*2484* Completion of the hardware commit step must be signalled using2485* drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed2486* to read or change any permanent software or hardware modeset state. The only2487* exception is state protected by other means than &drm_modeset_lock locks.2488* Only the free standing @state with pointers to the old state structures can2489* be inspected, e.g. to clean up old buffers using2490* drm_atomic_helper_cleanup_planes().2491*2492* At the very end, before cleaning up @state drivers must call2493* drm_atomic_helper_commit_cleanup_done().2494*2495* This is all implemented by in drm_atomic_helper_commit(), giving drivers a2496* complete and easy-to-use default implementation of the atomic_commit() hook.2497*2498* The tracking of asynchronously executed and still pending commits is done2499* using the core structure &drm_crtc_commit.2500*2501* By default there's no need to clean up resources allocated by this function2502* explicitly: drm_atomic_state_default_clear() will take care of that2503* automatically.2504*2505* Returns:2506* 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,2507* -ENOMEM on allocation failures and -EINTR when a signal is pending.2508*/2509int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,2510bool nonblock)2511{2512struct drm_crtc *crtc;2513struct drm_crtc_state *old_crtc_state, *new_crtc_state;2514struct drm_connector *conn;2515struct drm_connector_state *old_conn_state, *new_conn_state;2516struct drm_plane *plane;2517struct drm_plane_state *old_plane_state, *new_plane_state;2518struct drm_crtc_commit *commit;2519const struct drm_mode_config_helper_funcs *funcs;2520int i, ret;25212522funcs = state->dev->mode_config.helper_private;25232524for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {2525commit = kzalloc(sizeof(*commit), GFP_KERNEL);2526if (!commit)2527return -ENOMEM;25282529init_commit(commit, crtc);25302531new_crtc_state->commit = commit;25322533ret = stall_checks(crtc, nonblock);2534if (ret)2535return ret;25362537/*2538* Drivers only send out events when at least either current or2539* new CRTC state is active. Complete right away if everything2540* stays off.2541*/2542if (!old_crtc_state->active && !new_crtc_state->active) {2543complete_all(&commit->flip_done);2544continue;2545}25462547/* Legacy cursor updates are fully unsynced. */2548if (state->legacy_cursor_update) {2549complete_all(&commit->flip_done);2550continue;2551}25522553if (!new_crtc_state->event) {2554commit->event = kzalloc(sizeof(*commit->event),2555GFP_KERNEL);2556if (!commit->event)2557return -ENOMEM;25582559new_crtc_state->event = commit->event;2560}25612562new_crtc_state->event->base.completion = &commit->flip_done;2563new_crtc_state->event->base.completion_release = release_crtc_commit;2564drm_crtc_commit_get(commit);25652566commit->abort_completion = true;25672568state->crtcs[i].commit = commit;2569drm_crtc_commit_get(commit);2570}25712572for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {2573/*2574* Userspace is not allowed to get ahead of the previous2575* commit with nonblocking ones.2576*/2577if (nonblock && old_conn_state->commit &&2578!try_wait_for_completion(&old_conn_state->commit->flip_done)) {2579drm_dbg_atomic(conn->dev,2580"[CONNECTOR:%d:%s] busy with a previous commit\n",2581conn->base.id, conn->name);25822583return -EBUSY;2584}25852586/* Always track connectors explicitly for e.g. link retraining. */2587commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);2588if (!commit)2589return -ENOMEM;25902591new_conn_state->commit = drm_crtc_commit_get(commit);2592}25932594for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {2595/*2596* Userspace is not allowed to get ahead of the previous2597* commit with nonblocking ones.2598*/2599if (nonblock && old_plane_state->commit &&2600!try_wait_for_completion(&old_plane_state->commit->flip_done)) {2601drm_dbg_atomic(plane->dev,2602"[PLANE:%d:%s] busy with a previous commit\n",2603plane->base.id, plane->name);26042605return -EBUSY;2606}26072608/* Always track planes explicitly for async pageflip support. */2609commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);2610if (!commit)2611return -ENOMEM;26122613new_plane_state->commit = drm_crtc_commit_get(commit);2614}26152616if (funcs && funcs->atomic_commit_setup)2617return funcs->atomic_commit_setup(state);26182619return 0;2620}2621EXPORT_SYMBOL(drm_atomic_helper_setup_commit);26222623/**2624* drm_atomic_helper_wait_for_dependencies - wait for required preceding commits2625* @state: atomic state object being committed2626*2627* This function waits for all preceding commits that touch the same CRTC as2628* @state to both be committed to the hardware (as signalled by2629* drm_atomic_helper_commit_hw_done()) and executed by the hardware (as signalled2630* by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).2631*2632* This is part of the atomic helper support for nonblocking commits, see2633* drm_atomic_helper_setup_commit() for an overview.2634*/2635void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *state)2636{2637struct drm_crtc *crtc;2638struct drm_crtc_state *old_crtc_state;2639struct drm_plane *plane;2640struct drm_plane_state *old_plane_state;2641struct drm_connector *conn;2642struct drm_connector_state *old_conn_state;2643int i;2644long ret;26452646for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {2647ret = drm_crtc_commit_wait(old_crtc_state->commit);2648if (ret)2649drm_err(crtc->dev,2650"[CRTC:%d:%s] commit wait timed out\n",2651crtc->base.id, crtc->name);2652}26532654for_each_old_connector_in_state(state, conn, old_conn_state, i) {2655ret = drm_crtc_commit_wait(old_conn_state->commit);2656if (ret)2657drm_err(conn->dev,2658"[CONNECTOR:%d:%s] commit wait timed out\n",2659conn->base.id, conn->name);2660}26612662for_each_old_plane_in_state(state, plane, old_plane_state, i) {2663ret = drm_crtc_commit_wait(old_plane_state->commit);2664if (ret)2665drm_err(plane->dev,2666"[PLANE:%d:%s] commit wait timed out\n",2667plane->base.id, plane->name);2668}2669}2670EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);26712672/**2673* drm_atomic_helper_fake_vblank - fake VBLANK events if needed2674* @state: atomic state object being committed2675*2676* This function walks all CRTCs and fakes VBLANK events on those with2677* &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.2678* The primary use of this function is writeback connectors working in oneshot2679* mode and faking VBLANK events. In this case they only fake the VBLANK event2680* when a job is queued, and any change to the pipeline that does not touch the2681* connector is leading to timeouts when calling2682* drm_atomic_helper_wait_for_vblanks() or2683* drm_atomic_helper_wait_for_flip_done(). In addition to writeback2684* connectors, this function can also fake VBLANK events for CRTCs without2685* VBLANK interrupt.2686*2687* This is part of the atomic helper support for nonblocking commits, see2688* drm_atomic_helper_setup_commit() for an overview.2689*/2690void drm_atomic_helper_fake_vblank(struct drm_atomic_state *state)2691{2692struct drm_crtc_state *new_crtc_state;2693struct drm_crtc *crtc;2694int i;26952696for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {2697unsigned long flags;26982699if (!new_crtc_state->no_vblank)2700continue;27012702spin_lock_irqsave(&state->dev->event_lock, flags);2703if (new_crtc_state->event) {2704drm_crtc_send_vblank_event(crtc,2705new_crtc_state->event);2706new_crtc_state->event = NULL;2707}2708spin_unlock_irqrestore(&state->dev->event_lock, flags);2709}2710}2711EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);27122713/**2714* drm_atomic_helper_commit_hw_done - setup possible nonblocking commit2715* @state: atomic state object being committed2716*2717* This function is used to signal completion of the hardware commit step. After2718* this step the driver is not allowed to read or change any permanent software2719* or hardware modeset state. The only exception is state protected by other2720* means than &drm_modeset_lock locks.2721*2722* Drivers should try to postpone any expensive or delayed cleanup work after2723* this function is called.2724*2725* This is part of the atomic helper support for nonblocking commits, see2726* drm_atomic_helper_setup_commit() for an overview.2727*/2728void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *state)2729{2730struct drm_crtc *crtc;2731struct drm_crtc_state *old_crtc_state, *new_crtc_state;2732struct drm_crtc_commit *commit;2733int i;27342735for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {2736commit = new_crtc_state->commit;2737if (!commit)2738continue;27392740/*2741* copy new_crtc_state->commit to old_crtc_state->commit,2742* it's unsafe to touch new_crtc_state after hw_done,2743* but we still need to do so in cleanup_done().2744*/2745if (old_crtc_state->commit)2746drm_crtc_commit_put(old_crtc_state->commit);27472748old_crtc_state->commit = drm_crtc_commit_get(commit);27492750/* backend must have consumed any event by now */2751WARN_ON(new_crtc_state->event);2752complete_all(&commit->hw_done);2753}27542755if (state->fake_commit) {2756complete_all(&state->fake_commit->hw_done);2757complete_all(&state->fake_commit->flip_done);2758}2759}2760EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);27612762/**2763* drm_atomic_helper_commit_cleanup_done - signal completion of commit2764* @state: atomic state object being committed2765*2766* This signals completion of the atomic update @state, including any2767* cleanup work. If used, it must be called right before calling2768* drm_atomic_state_put().2769*2770* This is part of the atomic helper support for nonblocking commits, see2771* drm_atomic_helper_setup_commit() for an overview.2772*/2773void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *state)2774{2775struct drm_crtc *crtc;2776struct drm_crtc_state *old_crtc_state;2777struct drm_crtc_commit *commit;2778int i;27792780for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {2781commit = old_crtc_state->commit;2782if (WARN_ON(!commit))2783continue;27842785complete_all(&commit->cleanup_done);2786WARN_ON(!try_wait_for_completion(&commit->hw_done));27872788spin_lock(&crtc->commit_lock);2789list_del(&commit->commit_entry);2790spin_unlock(&crtc->commit_lock);2791}27922793if (state->fake_commit) {2794complete_all(&state->fake_commit->cleanup_done);2795WARN_ON(!try_wait_for_completion(&state->fake_commit->hw_done));2796}2797}2798EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);27992800/**2801* drm_atomic_helper_prepare_planes - prepare plane resources before commit2802* @dev: DRM device2803* @state: atomic state object with new state structures2804*2805* This function prepares plane state, specifically framebuffers, for the new2806* configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure2807* is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on2808* any already successfully prepared framebuffer.2809*2810* Returns:2811* 0 on success, negative error code on failure.2812*/2813int drm_atomic_helper_prepare_planes(struct drm_device *dev,2814struct drm_atomic_state *state)2815{2816struct drm_connector *connector;2817struct drm_connector_state *new_conn_state;2818struct drm_plane *plane;2819struct drm_plane_state *new_plane_state;2820int ret, i, j;28212822for_each_new_connector_in_state(state, connector, new_conn_state, i) {2823if (!new_conn_state->writeback_job)2824continue;28252826ret = drm_writeback_prepare_job(new_conn_state->writeback_job);2827if (ret < 0)2828return ret;2829}28302831for_each_new_plane_in_state(state, plane, new_plane_state, i) {2832const struct drm_plane_helper_funcs *funcs;28332834funcs = plane->helper_private;28352836if (funcs->prepare_fb) {2837ret = funcs->prepare_fb(plane, new_plane_state);2838if (ret)2839goto fail_prepare_fb;2840} else {2841WARN_ON_ONCE(funcs->cleanup_fb);28422843if (!drm_core_check_feature(dev, DRIVER_GEM))2844continue;28452846ret = drm_gem_plane_helper_prepare_fb(plane, new_plane_state);2847if (ret)2848goto fail_prepare_fb;2849}2850}28512852for_each_new_plane_in_state(state, plane, new_plane_state, i) {2853const struct drm_plane_helper_funcs *funcs = plane->helper_private;28542855if (funcs->begin_fb_access) {2856ret = funcs->begin_fb_access(plane, new_plane_state);2857if (ret)2858goto fail_begin_fb_access;2859}2860}28612862return 0;28632864fail_begin_fb_access:2865for_each_new_plane_in_state(state, plane, new_plane_state, j) {2866const struct drm_plane_helper_funcs *funcs = plane->helper_private;28672868if (j >= i)2869continue;28702871if (funcs->end_fb_access)2872funcs->end_fb_access(plane, new_plane_state);2873}2874i = j; /* set i to upper limit to cleanup all planes */2875fail_prepare_fb:2876for_each_new_plane_in_state(state, plane, new_plane_state, j) {2877const struct drm_plane_helper_funcs *funcs;28782879if (j >= i)2880continue;28812882funcs = plane->helper_private;28832884if (funcs->cleanup_fb)2885funcs->cleanup_fb(plane, new_plane_state);2886}28872888return ret;2889}2890EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);28912892/**2893* drm_atomic_helper_unprepare_planes - release plane resources on aborts2894* @dev: DRM device2895* @state: atomic state object with old state structures2896*2897* This function cleans up plane state, specifically framebuffers, from the2898* atomic state. It undoes the effects of drm_atomic_helper_prepare_planes()2899* when aborting an atomic commit. For cleaning up after a successful commit2900* use drm_atomic_helper_cleanup_planes().2901*/2902void drm_atomic_helper_unprepare_planes(struct drm_device *dev,2903struct drm_atomic_state *state)2904{2905struct drm_plane *plane;2906struct drm_plane_state *new_plane_state;2907int i;29082909for_each_new_plane_in_state(state, plane, new_plane_state, i) {2910const struct drm_plane_helper_funcs *funcs = plane->helper_private;29112912if (funcs->end_fb_access)2913funcs->end_fb_access(plane, new_plane_state);2914}29152916for_each_new_plane_in_state(state, plane, new_plane_state, i) {2917const struct drm_plane_helper_funcs *funcs = plane->helper_private;29182919if (funcs->cleanup_fb)2920funcs->cleanup_fb(plane, new_plane_state);2921}2922}2923EXPORT_SYMBOL(drm_atomic_helper_unprepare_planes);29242925static bool plane_crtc_active(const struct drm_plane_state *state)2926{2927return state->crtc && state->crtc->state->active;2928}29292930/**2931* drm_atomic_helper_commit_planes - commit plane state2932* @dev: DRM device2933* @state: atomic state object being committed2934* @flags: flags for committing plane state2935*2936* This function commits the new plane state using the plane and atomic helper2937* functions for planes and CRTCs. It assumes that the atomic state has already2938* been pushed into the relevant object state pointers, since this step can no2939* longer fail.2940*2941* It still requires the global state object @state to know which planes and2942* crtcs need to be updated though.2943*2944* Note that this function does all plane updates across all CRTCs in one step.2945* If the hardware can't support this approach look at2946* drm_atomic_helper_commit_planes_on_crtc() instead.2947*2948* Plane parameters can be updated by applications while the associated CRTC is2949* disabled. The DRM/KMS core will store the parameters in the plane state,2950* which will be available to the driver when the CRTC is turned on. As a result2951* most drivers don't need to be immediately notified of plane updates for a2952* disabled CRTC.2953*2954* Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in2955* @flags in order not to receive plane update notifications related to a2956* disabled CRTC. This avoids the need to manually ignore plane updates in2957* driver code when the driver and/or hardware can't or just don't need to deal2958* with updates on disabled CRTCs, for example when supporting runtime PM.2959*2960* Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant2961* display controllers require to disable a CRTC's planes when the CRTC is2962* disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable2963* call for a plane if the CRTC of the old plane state needs a modesetting2964* operation. Of course, the drivers need to disable the planes in their CRTC2965* disable callbacks since no one else would do that.2966*2967* The drm_atomic_helper_commit() default implementation doesn't set the2968* ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.2969* This should not be copied blindly by drivers.2970*/2971void drm_atomic_helper_commit_planes(struct drm_device *dev,2972struct drm_atomic_state *state,2973uint32_t flags)2974{2975struct drm_crtc *crtc;2976struct drm_crtc_state *old_crtc_state, *new_crtc_state;2977struct drm_plane *plane;2978struct drm_plane_state *old_plane_state, *new_plane_state;2979int i;2980bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;2981bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;29822983for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {2984const struct drm_crtc_helper_funcs *funcs;29852986funcs = crtc->helper_private;29872988if (!funcs || !funcs->atomic_begin)2989continue;29902991if (active_only && !new_crtc_state->active)2992continue;29932994funcs->atomic_begin(crtc, state);2995}29962997for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {2998const struct drm_plane_helper_funcs *funcs;2999bool disabling;30003001funcs = plane->helper_private;30023003if (!funcs)3004continue;30053006disabling = drm_atomic_plane_disabling(old_plane_state,3007new_plane_state);30083009if (active_only) {3010/*3011* Skip planes related to inactive CRTCs. If the plane3012* is enabled use the state of the current CRTC. If the3013* plane is being disabled use the state of the old3014* CRTC to avoid skipping planes being disabled on an3015* active CRTC.3016*/3017if (!disabling && !plane_crtc_active(new_plane_state))3018continue;3019if (disabling && !plane_crtc_active(old_plane_state))3020continue;3021}30223023/*3024* Special-case disabling the plane if drivers support it.3025*/3026if (disabling && funcs->atomic_disable) {3027struct drm_crtc_state *crtc_state;30283029crtc_state = old_plane_state->crtc->state;30303031if (drm_atomic_crtc_needs_modeset(crtc_state) &&3032no_disable)3033continue;30343035funcs->atomic_disable(plane, state);3036} else if (new_plane_state->crtc || disabling) {3037funcs->atomic_update(plane, state);30383039if (!disabling && funcs->atomic_enable) {3040if (drm_atomic_plane_enabling(old_plane_state, new_plane_state))3041funcs->atomic_enable(plane, state);3042}3043}3044}30453046for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {3047const struct drm_crtc_helper_funcs *funcs;30483049funcs = crtc->helper_private;30503051if (!funcs || !funcs->atomic_flush)3052continue;30533054if (active_only && !new_crtc_state->active)3055continue;30563057funcs->atomic_flush(crtc, state);3058}30593060/*3061* Signal end of framebuffer access here before hw_done. After hw_done,3062* a later commit might have already released the plane state.3063*/3064for_each_old_plane_in_state(state, plane, old_plane_state, i) {3065const struct drm_plane_helper_funcs *funcs = plane->helper_private;30663067if (funcs->end_fb_access)3068funcs->end_fb_access(plane, old_plane_state);3069}3070}3071EXPORT_SYMBOL(drm_atomic_helper_commit_planes);30723073/**3074* drm_atomic_helper_commit_planes_on_crtc - commit plane state for a CRTC3075* @old_crtc_state: atomic state object with the old CRTC state3076*3077* This function commits the new plane state using the plane and atomic helper3078* functions for planes on the specific CRTC. It assumes that the atomic state3079* has already been pushed into the relevant object state pointers, since this3080* step can no longer fail.3081*3082* This function is useful when plane updates should be done CRTC-by-CRTC3083* instead of one global step like drm_atomic_helper_commit_planes() does.3084*3085* This function can only be savely used when planes are not allowed to move3086* between different CRTCs because this function doesn't handle inter-CRTC3087* dependencies. Callers need to ensure that either no such dependencies exist,3088* resolve them through ordering of commit calls or through some other means.3089*/3090void3091drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)3092{3093const struct drm_crtc_helper_funcs *crtc_funcs;3094struct drm_crtc *crtc = old_crtc_state->crtc;3095struct drm_atomic_state *old_state = old_crtc_state->state;3096struct drm_crtc_state *new_crtc_state =3097drm_atomic_get_new_crtc_state(old_state, crtc);3098struct drm_plane *plane;3099unsigned int plane_mask;31003101plane_mask = old_crtc_state->plane_mask;3102plane_mask |= new_crtc_state->plane_mask;31033104crtc_funcs = crtc->helper_private;3105if (crtc_funcs && crtc_funcs->atomic_begin)3106crtc_funcs->atomic_begin(crtc, old_state);31073108drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {3109struct drm_plane_state *old_plane_state =3110drm_atomic_get_old_plane_state(old_state, plane);3111struct drm_plane_state *new_plane_state =3112drm_atomic_get_new_plane_state(old_state, plane);3113const struct drm_plane_helper_funcs *plane_funcs;3114bool disabling;31153116plane_funcs = plane->helper_private;31173118if (!old_plane_state || !plane_funcs)3119continue;31203121WARN_ON(new_plane_state->crtc &&3122new_plane_state->crtc != crtc);31233124disabling = drm_atomic_plane_disabling(old_plane_state, new_plane_state);31253126if (disabling && plane_funcs->atomic_disable) {3127plane_funcs->atomic_disable(plane, old_state);3128} else if (new_plane_state->crtc || disabling) {3129plane_funcs->atomic_update(plane, old_state);31303131if (!disabling && plane_funcs->atomic_enable) {3132if (drm_atomic_plane_enabling(old_plane_state, new_plane_state))3133plane_funcs->atomic_enable(plane, old_state);3134}3135}3136}31373138if (crtc_funcs && crtc_funcs->atomic_flush)3139crtc_funcs->atomic_flush(crtc, old_state);3140}3141EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);31423143/**3144* drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes3145* @old_crtc_state: atomic state object with the old CRTC state3146* @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks3147*3148* Disables all planes associated with the given CRTC. This can be3149* used for instance in the CRTC helper atomic_disable callback to disable3150* all planes.3151*3152* If the atomic-parameter is set the function calls the CRTC's3153* atomic_begin hook before and atomic_flush hook after disabling the3154* planes.3155*3156* It is a bug to call this function without having implemented the3157* &drm_plane_helper_funcs.atomic_disable plane hook.3158*/3159void3160drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,3161bool atomic)3162{3163struct drm_crtc *crtc = old_crtc_state->crtc;3164const struct drm_crtc_helper_funcs *crtc_funcs =3165crtc->helper_private;3166struct drm_plane *plane;31673168if (atomic && crtc_funcs && crtc_funcs->atomic_begin)3169crtc_funcs->atomic_begin(crtc, NULL);31703171drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {3172const struct drm_plane_helper_funcs *plane_funcs =3173plane->helper_private;31743175if (!plane_funcs)3176continue;31773178WARN_ON(!plane_funcs->atomic_disable);3179if (plane_funcs->atomic_disable)3180plane_funcs->atomic_disable(plane, NULL);3181}31823183if (atomic && crtc_funcs && crtc_funcs->atomic_flush)3184crtc_funcs->atomic_flush(crtc, NULL);3185}3186EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);31873188/**3189* drm_atomic_helper_cleanup_planes - cleanup plane resources after commit3190* @dev: DRM device3191* @state: atomic state object being committed3192*3193* This function cleans up plane state, specifically framebuffers, from the old3194* configuration. Hence the old configuration must be perserved in @state to3195* be able to call this function.3196*3197* This function may not be called on the new state when the atomic update3198* fails at any point after calling drm_atomic_helper_prepare_planes(). Use3199* drm_atomic_helper_unprepare_planes() in this case.3200*/3201void drm_atomic_helper_cleanup_planes(struct drm_device *dev,3202struct drm_atomic_state *state)3203{3204struct drm_plane *plane;3205struct drm_plane_state *old_plane_state;3206int i;32073208for_each_old_plane_in_state(state, plane, old_plane_state, i) {3209const struct drm_plane_helper_funcs *funcs = plane->helper_private;32103211if (funcs->cleanup_fb)3212funcs->cleanup_fb(plane, old_plane_state);3213}3214}3215EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);32163217/**3218* drm_atomic_helper_swap_state - store atomic state into current sw state3219* @state: atomic state3220* @stall: stall for preceding commits3221*3222* This function stores the atomic state into the current state pointers in all3223* driver objects. It should be called after all failing steps have been done3224* and succeeded, but before the actual hardware state is committed.3225*3226* For cleanup and error recovery the current state for all changed objects will3227* be swapped into @state.3228*3229* With that sequence it fits perfectly into the plane prepare/cleanup sequence:3230*3231* 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.3232*3233* 2. Do any other steps that might fail.3234*3235* 3. Put the staged state into the current state pointers with this function.3236*3237* 4. Actually commit the hardware state.3238*3239* 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 33240* contains the old state. Also do any other cleanup required with that state.3241*3242* @stall must be set when nonblocking commits for this driver directly access3243* the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With3244* the current atomic helpers this is almost always the case, since the helpers3245* don't pass the right state structures to the callbacks.3246*3247* Returns:3248* Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the3249* waiting for the previous commits has been interrupted.3250*/3251int drm_atomic_helper_swap_state(struct drm_atomic_state *state,3252bool stall)3253{3254int i, ret;3255unsigned long flags = 0;3256struct drm_connector *connector;3257struct drm_connector_state *old_conn_state, *new_conn_state;3258struct drm_crtc *crtc;3259struct drm_crtc_state *old_crtc_state, *new_crtc_state;3260struct drm_plane *plane;3261struct drm_plane_state *old_plane_state, *new_plane_state;3262struct drm_colorop *colorop;3263struct drm_colorop_state *old_colorop_state, *new_colorop_state;3264struct drm_crtc_commit *commit;3265struct drm_private_obj *obj;3266struct drm_private_state *old_obj_state, *new_obj_state;32673268if (stall) {3269/*3270* We have to stall for hw_done here before3271* drm_atomic_helper_wait_for_dependencies() because flip3272* depth > 1 is not yet supported by all drivers. As long as3273* obj->state is directly dereferenced anywhere in the drivers3274* atomic_commit_tail function, then it's unsafe to swap state3275* before drm_atomic_helper_commit_hw_done() is called.3276*/32773278for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {3279commit = old_crtc_state->commit;32803281if (!commit)3282continue;32833284ret = wait_for_completion_interruptible(&commit->hw_done);3285if (ret)3286return ret;3287}32883289for_each_old_connector_in_state(state, connector, old_conn_state, i) {3290commit = old_conn_state->commit;32913292if (!commit)3293continue;32943295ret = wait_for_completion_interruptible(&commit->hw_done);3296if (ret)3297return ret;3298}32993300for_each_old_plane_in_state(state, plane, old_plane_state, i) {3301commit = old_plane_state->commit;33023303if (!commit)3304continue;33053306ret = wait_for_completion_interruptible(&commit->hw_done);3307if (ret)3308return ret;3309}3310}33113312for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {3313WARN_ON(connector->state != old_conn_state);33143315old_conn_state->state = state;3316new_conn_state->state = NULL;33173318state->connectors[i].state_to_destroy = old_conn_state;3319connector->state = new_conn_state;3320}33213322for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {3323WARN_ON(crtc->state != old_crtc_state);33243325old_crtc_state->state = state;3326new_crtc_state->state = NULL;33273328state->crtcs[i].state_to_destroy = old_crtc_state;3329crtc->state = new_crtc_state;33303331if (new_crtc_state->commit) {3332spin_lock(&crtc->commit_lock);3333list_add(&new_crtc_state->commit->commit_entry,3334&crtc->commit_list);3335spin_unlock(&crtc->commit_lock);33363337new_crtc_state->commit->event = NULL;3338}3339}33403341for_each_oldnew_colorop_in_state(state, colorop, old_colorop_state, new_colorop_state, i) {3342WARN_ON(colorop->state != old_colorop_state);33433344old_colorop_state->state = state;3345new_colorop_state->state = NULL;33463347state->colorops[i].state = old_colorop_state;3348colorop->state = new_colorop_state;3349}33503351drm_panic_lock(state->dev, flags);3352for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {3353WARN_ON(plane->state != old_plane_state);33543355old_plane_state->state = state;3356new_plane_state->state = NULL;33573358state->planes[i].state_to_destroy = old_plane_state;3359plane->state = new_plane_state;3360}3361drm_panic_unlock(state->dev, flags);33623363for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {3364WARN_ON(obj->state != old_obj_state);33653366old_obj_state->state = state;3367new_obj_state->state = NULL;33683369state->private_objs[i].state_to_destroy = old_obj_state;3370obj->state = new_obj_state;3371}33723373return 0;3374}3375EXPORT_SYMBOL(drm_atomic_helper_swap_state);33763377/**3378* drm_atomic_helper_update_plane - Helper for primary plane update using atomic3379* @plane: plane object to update3380* @crtc: owning CRTC of owning plane3381* @fb: framebuffer to flip onto plane3382* @crtc_x: x offset of primary plane on @crtc3383* @crtc_y: y offset of primary plane on @crtc3384* @crtc_w: width of primary plane rectangle on @crtc3385* @crtc_h: height of primary plane rectangle on @crtc3386* @src_x: x offset of @fb for panning3387* @src_y: y offset of @fb for panning3388* @src_w: width of source rectangle in @fb3389* @src_h: height of source rectangle in @fb3390* @ctx: lock acquire context3391*3392* Provides a default plane update handler using the atomic driver interface.3393*3394* RETURNS:3395* Zero on success, error code on failure3396*/3397int drm_atomic_helper_update_plane(struct drm_plane *plane,3398struct drm_crtc *crtc,3399struct drm_framebuffer *fb,3400int crtc_x, int crtc_y,3401unsigned int crtc_w, unsigned int crtc_h,3402uint32_t src_x, uint32_t src_y,3403uint32_t src_w, uint32_t src_h,3404struct drm_modeset_acquire_ctx *ctx)3405{3406struct drm_atomic_state *state;3407struct drm_plane_state *plane_state;3408int ret = 0;34093410state = drm_atomic_state_alloc(plane->dev);3411if (!state)3412return -ENOMEM;34133414state->acquire_ctx = ctx;3415plane_state = drm_atomic_get_plane_state(state, plane);3416if (IS_ERR(plane_state)) {3417ret = PTR_ERR(plane_state);3418goto fail;3419}34203421ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);3422if (ret != 0)3423goto fail;3424drm_atomic_set_fb_for_plane(plane_state, fb);3425plane_state->crtc_x = crtc_x;3426plane_state->crtc_y = crtc_y;3427plane_state->crtc_w = crtc_w;3428plane_state->crtc_h = crtc_h;3429plane_state->src_x = src_x;3430plane_state->src_y = src_y;3431plane_state->src_w = src_w;3432plane_state->src_h = src_h;34333434if (plane == crtc->cursor)3435state->legacy_cursor_update = true;34363437ret = drm_atomic_commit(state);3438fail:3439drm_atomic_state_put(state);3440return ret;3441}3442EXPORT_SYMBOL(drm_atomic_helper_update_plane);34433444/**3445* drm_atomic_helper_disable_plane - Helper for primary plane disable using atomic3446* @plane: plane to disable3447* @ctx: lock acquire context3448*3449* Provides a default plane disable handler using the atomic driver interface.3450*3451* RETURNS:3452* Zero on success, error code on failure3453*/3454int drm_atomic_helper_disable_plane(struct drm_plane *plane,3455struct drm_modeset_acquire_ctx *ctx)3456{3457struct drm_atomic_state *state;3458struct drm_plane_state *plane_state;3459int ret = 0;34603461state = drm_atomic_state_alloc(plane->dev);3462if (!state)3463return -ENOMEM;34643465state->acquire_ctx = ctx;3466plane_state = drm_atomic_get_plane_state(state, plane);3467if (IS_ERR(plane_state)) {3468ret = PTR_ERR(plane_state);3469goto fail;3470}34713472if (plane_state->crtc && plane_state->crtc->cursor == plane)3473plane_state->state->legacy_cursor_update = true;34743475ret = __drm_atomic_helper_disable_plane(plane, plane_state);3476if (ret != 0)3477goto fail;34783479ret = drm_atomic_commit(state);3480fail:3481drm_atomic_state_put(state);3482return ret;3483}3484EXPORT_SYMBOL(drm_atomic_helper_disable_plane);34853486/**3487* drm_atomic_helper_set_config - set a new config from userspace3488* @set: mode set configuration3489* @ctx: lock acquisition context3490*3491* Provides a default CRTC set_config handler using the atomic driver interface.3492*3493* NOTE: For backwards compatibility with old userspace this automatically3494* resets the "link-status" property to GOOD, to force any link3495* re-training. The SETCRTC ioctl does not define whether an update does3496* need a full modeset or just a plane update, hence we're allowed to do3497* that. See also drm_connector_set_link_status_property().3498*3499* Returns:3500* Returns 0 on success, negative errno numbers on failure.3501*/3502int drm_atomic_helper_set_config(struct drm_mode_set *set,3503struct drm_modeset_acquire_ctx *ctx)3504{3505struct drm_atomic_state *state;3506struct drm_crtc *crtc = set->crtc;3507int ret = 0;35083509state = drm_atomic_state_alloc(crtc->dev);3510if (!state)3511return -ENOMEM;35123513state->acquire_ctx = ctx;3514ret = __drm_atomic_helper_set_config(set, state);3515if (ret != 0)3516goto fail;35173518ret = handle_conflicting_encoders(state, true);3519if (ret)3520goto fail;35213522ret = drm_atomic_commit(state);35233524fail:3525drm_atomic_state_put(state);3526return ret;3527}3528EXPORT_SYMBOL(drm_atomic_helper_set_config);35293530/**3531* drm_atomic_helper_disable_all - disable all currently active outputs3532* @dev: DRM device3533* @ctx: lock acquisition context3534*3535* Loops through all connectors, finding those that aren't turned off and then3536* turns them off by setting their DPMS mode to OFF and deactivating the CRTC3537* that they are connected to.3538*3539* This is used for example in suspend/resume to disable all currently active3540* functions when suspending. If you just want to shut down everything at e.g.3541* driver unload, look at drm_atomic_helper_shutdown().3542*3543* Note that if callers haven't already acquired all modeset locks this might3544* return -EDEADLK, which must be handled by calling drm_modeset_backoff().3545*3546* Returns:3547* 0 on success or a negative error code on failure.3548*3549* See also:3550* drm_atomic_helper_suspend(), drm_atomic_helper_resume() and3551* drm_atomic_helper_shutdown().3552*/3553int drm_atomic_helper_disable_all(struct drm_device *dev,3554struct drm_modeset_acquire_ctx *ctx)3555{3556struct drm_atomic_state *state;3557struct drm_connector_state *conn_state;3558struct drm_connector *conn;3559struct drm_plane_state *plane_state;3560struct drm_plane *plane;3561struct drm_crtc_state *crtc_state;3562struct drm_crtc *crtc;3563int ret, i;35643565state = drm_atomic_state_alloc(dev);3566if (!state)3567return -ENOMEM;35683569state->acquire_ctx = ctx;35703571drm_for_each_crtc(crtc, dev) {3572crtc_state = drm_atomic_get_crtc_state(state, crtc);3573if (IS_ERR(crtc_state)) {3574ret = PTR_ERR(crtc_state);3575goto free;3576}35773578crtc_state->active = false;35793580ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);3581if (ret < 0)3582goto free;35833584ret = drm_atomic_add_affected_planes(state, crtc);3585if (ret < 0)3586goto free;35873588ret = drm_atomic_add_affected_connectors(state, crtc);3589if (ret < 0)3590goto free;3591}35923593for_each_new_connector_in_state(state, conn, conn_state, i) {3594ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);3595if (ret < 0)3596goto free;3597}35983599for_each_new_plane_in_state(state, plane, plane_state, i) {3600ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);3601if (ret < 0)3602goto free;36033604drm_atomic_set_fb_for_plane(plane_state, NULL);3605}36063607ret = drm_atomic_commit(state);3608free:3609drm_atomic_state_put(state);3610return ret;3611}3612EXPORT_SYMBOL(drm_atomic_helper_disable_all);36133614/**3615* drm_atomic_helper_reset_crtc - reset the active outputs of a CRTC3616* @crtc: DRM CRTC3617* @ctx: lock acquisition context3618*3619* Reset the active outputs by indicating that connectors have changed.3620* This implies a reset of all active components available between the CRTC and3621* connectors.3622*3623* A variant of this function exists with3624* drm_bridge_helper_reset_crtc(), dedicated to bridges.3625*3626* NOTE: This relies on resetting &drm_crtc_state.connectors_changed.3627* For drivers which optimize out unnecessary modesets this will result in3628* a no-op commit, achieving nothing.3629*3630* Returns:3631* 0 on success or a negative error code on failure.3632*/3633int drm_atomic_helper_reset_crtc(struct drm_crtc *crtc,3634struct drm_modeset_acquire_ctx *ctx)3635{3636struct drm_atomic_state *state;3637struct drm_crtc_state *crtc_state;3638int ret;36393640state = drm_atomic_state_alloc(crtc->dev);3641if (!state)3642return -ENOMEM;36433644state->acquire_ctx = ctx;36453646crtc_state = drm_atomic_get_crtc_state(state, crtc);3647if (IS_ERR(crtc_state)) {3648ret = PTR_ERR(crtc_state);3649goto out;3650}36513652crtc_state->connectors_changed = true;36533654ret = drm_atomic_commit(state);3655out:3656drm_atomic_state_put(state);36573658return ret;3659}3660EXPORT_SYMBOL(drm_atomic_helper_reset_crtc);36613662/**3663* drm_atomic_helper_shutdown - shutdown all CRTC3664* @dev: DRM device3665*3666* This shuts down all CRTC, which is useful for driver unloading. Shutdown on3667* suspend should instead be handled with drm_atomic_helper_suspend(), since3668* that also takes a snapshot of the modeset state to be restored on resume.3669*3670* This is just a convenience wrapper around drm_atomic_helper_disable_all(),3671* and it is the atomic version of drm_helper_force_disable_all().3672*/3673void drm_atomic_helper_shutdown(struct drm_device *dev)3674{3675struct drm_modeset_acquire_ctx ctx;3676int ret;36773678if (dev == NULL)3679return;36803681DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);36823683ret = drm_atomic_helper_disable_all(dev, &ctx);3684if (ret)3685drm_err(dev,3686"Disabling all crtc's during unload failed with %i\n",3687ret);36883689DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);3690}3691EXPORT_SYMBOL(drm_atomic_helper_shutdown);36923693/**3694* drm_atomic_helper_duplicate_state - duplicate an atomic state object3695* @dev: DRM device3696* @ctx: lock acquisition context3697*3698* Makes a copy of the current atomic state by looping over all objects and3699* duplicating their respective states. This is used for example by suspend/3700* resume support code to save the state prior to suspend such that it can3701* be restored upon resume.3702*3703* Note that this treats atomic state as persistent between save and restore.3704* Drivers must make sure that this is possible and won't result in confusion3705* or erroneous behaviour.3706*3707* Note that if callers haven't already acquired all modeset locks this might3708* return -EDEADLK, which must be handled by calling drm_modeset_backoff().3709*3710* Returns:3711* A pointer to the copy of the atomic state object on success or an3712* ERR_PTR()-encoded error code on failure.3713*3714* See also:3715* drm_atomic_helper_suspend(), drm_atomic_helper_resume()3716*/3717struct drm_atomic_state *3718drm_atomic_helper_duplicate_state(struct drm_device *dev,3719struct drm_modeset_acquire_ctx *ctx)3720{3721struct drm_atomic_state *state;3722struct drm_connector *conn;3723struct drm_connector_list_iter conn_iter;3724struct drm_plane *plane;3725struct drm_crtc *crtc;3726int err = 0;37273728state = drm_atomic_state_alloc(dev);3729if (!state)3730return ERR_PTR(-ENOMEM);37313732state->acquire_ctx = ctx;3733state->duplicated = true;37343735drm_for_each_crtc(crtc, dev) {3736struct drm_crtc_state *crtc_state;37373738crtc_state = drm_atomic_get_crtc_state(state, crtc);3739if (IS_ERR(crtc_state)) {3740err = PTR_ERR(crtc_state);3741goto free;3742}3743}37443745drm_for_each_plane(plane, dev) {3746struct drm_plane_state *plane_state;37473748plane_state = drm_atomic_get_plane_state(state, plane);3749if (IS_ERR(plane_state)) {3750err = PTR_ERR(plane_state);3751goto free;3752}3753}37543755drm_connector_list_iter_begin(dev, &conn_iter);3756drm_for_each_connector_iter(conn, &conn_iter) {3757struct drm_connector_state *conn_state;37583759conn_state = drm_atomic_get_connector_state(state, conn);3760if (IS_ERR(conn_state)) {3761err = PTR_ERR(conn_state);3762drm_connector_list_iter_end(&conn_iter);3763goto free;3764}3765}3766drm_connector_list_iter_end(&conn_iter);37673768/* clear the acquire context so that it isn't accidentally reused */3769state->acquire_ctx = NULL;37703771free:3772if (err < 0) {3773drm_atomic_state_put(state);3774state = ERR_PTR(err);3775}37763777return state;3778}3779EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);37803781/**3782* drm_atomic_helper_suspend - subsystem-level suspend helper3783* @dev: DRM device3784*3785* Duplicates the current atomic state, disables all active outputs and then3786* returns a pointer to the original atomic state to the caller. Drivers can3787* pass this pointer to the drm_atomic_helper_resume() helper upon resume to3788* restore the output configuration that was active at the time the system3789* entered suspend.3790*3791* Note that it is potentially unsafe to use this. The atomic state object3792* returned by this function is assumed to be persistent. Drivers must ensure3793* that this holds true. Before calling this function, drivers must make sure3794* to suspend fbdev emulation so that nothing can be using the device.3795*3796* Returns:3797* A pointer to a copy of the state before suspend on success or an ERR_PTR()-3798* encoded error code on failure. Drivers should store the returned atomic3799* state object and pass it to the drm_atomic_helper_resume() helper upon3800* resume.3801*3802* See also:3803* drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),3804* drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()3805*/3806struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)3807{3808struct drm_modeset_acquire_ctx ctx;3809struct drm_atomic_state *state;3810int err;38113812/* This can never be returned, but it makes the compiler happy */3813state = ERR_PTR(-EINVAL);38143815DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);38163817state = drm_atomic_helper_duplicate_state(dev, &ctx);3818if (IS_ERR(state))3819goto unlock;38203821err = drm_atomic_helper_disable_all(dev, &ctx);3822if (err < 0) {3823drm_atomic_state_put(state);3824state = ERR_PTR(err);3825goto unlock;3826}38273828unlock:3829DRM_MODESET_LOCK_ALL_END(dev, ctx, err);3830if (err)3831return ERR_PTR(err);38323833return state;3834}3835EXPORT_SYMBOL(drm_atomic_helper_suspend);38363837/**3838* drm_atomic_helper_commit_duplicated_state - commit duplicated state3839* @state: duplicated atomic state to commit3840* @ctx: pointer to acquire_ctx to use for commit.3841*3842* The state returned by drm_atomic_helper_duplicate_state() and3843* drm_atomic_helper_suspend() is partially invalid, and needs to3844* be fixed up before commit.3845*3846* Returns:3847* 0 on success or a negative error code on failure.3848*3849* See also:3850* drm_atomic_helper_suspend()3851*/3852int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,3853struct drm_modeset_acquire_ctx *ctx)3854{3855int i, ret;3856struct drm_plane *plane;3857struct drm_plane_state *new_plane_state;3858struct drm_connector *connector;3859struct drm_connector_state *new_conn_state;3860struct drm_crtc *crtc;3861struct drm_crtc_state *new_crtc_state;38623863state->acquire_ctx = ctx;38643865for_each_new_plane_in_state(state, plane, new_plane_state, i)3866state->planes[i].old_state = plane->state;38673868for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)3869state->crtcs[i].old_state = crtc->state;38703871for_each_new_connector_in_state(state, connector, new_conn_state, i)3872state->connectors[i].old_state = connector->state;38733874ret = drm_atomic_commit(state);38753876state->acquire_ctx = NULL;38773878return ret;3879}3880EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);38813882/**3883* drm_atomic_helper_resume - subsystem-level resume helper3884* @dev: DRM device3885* @state: atomic state to resume to3886*3887* Calls drm_mode_config_reset() to synchronize hardware and software states,3888* grabs all modeset locks and commits the atomic state object. This can be3889* used in conjunction with the drm_atomic_helper_suspend() helper to3890* implement suspend/resume for drivers that support atomic mode-setting.3891*3892* Returns:3893* 0 on success or a negative error code on failure.3894*3895* See also:3896* drm_atomic_helper_suspend()3897*/3898int drm_atomic_helper_resume(struct drm_device *dev,3899struct drm_atomic_state *state)3900{3901struct drm_modeset_acquire_ctx ctx;3902int err;39033904drm_mode_config_reset(dev);39053906DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);39073908err = drm_atomic_helper_commit_duplicated_state(state, &ctx);39093910DRM_MODESET_LOCK_ALL_END(dev, ctx, err);3911drm_atomic_state_put(state);39123913return err;3914}3915EXPORT_SYMBOL(drm_atomic_helper_resume);39163917static int page_flip_common(struct drm_atomic_state *state,3918struct drm_crtc *crtc,3919struct drm_framebuffer *fb,3920struct drm_pending_vblank_event *event,3921uint32_t flags)3922{3923struct drm_plane *plane = crtc->primary;3924struct drm_plane_state *plane_state;3925struct drm_crtc_state *crtc_state;3926int ret = 0;39273928crtc_state = drm_atomic_get_crtc_state(state, crtc);3929if (IS_ERR(crtc_state))3930return PTR_ERR(crtc_state);39313932crtc_state->event = event;3933crtc_state->async_flip = flags & DRM_MODE_PAGE_FLIP_ASYNC;39343935plane_state = drm_atomic_get_plane_state(state, plane);3936if (IS_ERR(plane_state))3937return PTR_ERR(plane_state);39383939ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);3940if (ret != 0)3941return ret;3942drm_atomic_set_fb_for_plane(plane_state, fb);39433944/* Make sure we don't accidentally do a full modeset. */3945state->allow_modeset = false;3946if (!crtc_state->active) {3947drm_dbg_atomic(crtc->dev,3948"[CRTC:%d:%s] disabled, rejecting legacy flip\n",3949crtc->base.id, crtc->name);3950return -EINVAL;3951}39523953return ret;3954}39553956/**3957* drm_atomic_helper_page_flip - execute a legacy page flip3958* @crtc: DRM CRTC3959* @fb: DRM framebuffer3960* @event: optional DRM event to signal upon completion3961* @flags: flip flags for non-vblank sync'ed updates3962* @ctx: lock acquisition context3963*3964* Provides a default &drm_crtc_funcs.page_flip implementation3965* using the atomic driver interface.3966*3967* Returns:3968* Returns 0 on success, negative errno numbers on failure.3969*3970* See also:3971* drm_atomic_helper_page_flip_target()3972*/3973int drm_atomic_helper_page_flip(struct drm_crtc *crtc,3974struct drm_framebuffer *fb,3975struct drm_pending_vblank_event *event,3976uint32_t flags,3977struct drm_modeset_acquire_ctx *ctx)3978{3979struct drm_plane *plane = crtc->primary;3980struct drm_atomic_state *state;3981int ret = 0;39823983state = drm_atomic_state_alloc(plane->dev);3984if (!state)3985return -ENOMEM;39863987state->acquire_ctx = ctx;39883989ret = page_flip_common(state, crtc, fb, event, flags);3990if (ret != 0)3991goto fail;39923993ret = drm_atomic_nonblocking_commit(state);3994fail:3995drm_atomic_state_put(state);3996return ret;3997}3998EXPORT_SYMBOL(drm_atomic_helper_page_flip);39994000/**4001* drm_atomic_helper_page_flip_target - do page flip on target vblank period.4002* @crtc: DRM CRTC4003* @fb: DRM framebuffer4004* @event: optional DRM event to signal upon completion4005* @flags: flip flags for non-vblank sync'ed updates4006* @target: specifying the target vblank period when the flip to take effect4007* @ctx: lock acquisition context4008*4009* Provides a default &drm_crtc_funcs.page_flip_target implementation.4010* Similar to drm_atomic_helper_page_flip() with extra parameter to specify4011* target vblank period to flip.4012*4013* Returns:4014* Returns 0 on success, negative errno numbers on failure.4015*/4016int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,4017struct drm_framebuffer *fb,4018struct drm_pending_vblank_event *event,4019uint32_t flags,4020uint32_t target,4021struct drm_modeset_acquire_ctx *ctx)4022{4023struct drm_plane *plane = crtc->primary;4024struct drm_atomic_state *state;4025struct drm_crtc_state *crtc_state;4026int ret = 0;40274028state = drm_atomic_state_alloc(plane->dev);4029if (!state)4030return -ENOMEM;40314032state->acquire_ctx = ctx;40334034ret = page_flip_common(state, crtc, fb, event, flags);4035if (ret != 0)4036goto fail;40374038crtc_state = drm_atomic_get_new_crtc_state(state, crtc);4039if (WARN_ON(!crtc_state)) {4040ret = -EINVAL;4041goto fail;4042}4043crtc_state->target_vblank = target;40444045ret = drm_atomic_nonblocking_commit(state);4046fail:4047drm_atomic_state_put(state);4048return ret;4049}4050EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);40514052/**4053* drm_atomic_helper_bridge_propagate_bus_fmt() - Propagate output format to4054* the input end of a bridge4055* @bridge: bridge control structure4056* @bridge_state: new bridge state4057* @crtc_state: new CRTC state4058* @conn_state: new connector state4059* @output_fmt: tested output bus format4060* @num_input_fmts: will contain the size of the returned array4061*4062* This helper is a pluggable implementation of the4063* &drm_bridge_funcs.atomic_get_input_bus_fmts operation for bridges that don't4064* modify the bus configuration between their input and their output. It4065* returns an array of input formats with a single element set to @output_fmt.4066*4067* RETURNS:4068* a valid format array of size @num_input_fmts, or NULL if the allocation4069* failed4070*/4071u32 *4072drm_atomic_helper_bridge_propagate_bus_fmt(struct drm_bridge *bridge,4073struct drm_bridge_state *bridge_state,4074struct drm_crtc_state *crtc_state,4075struct drm_connector_state *conn_state,4076u32 output_fmt,4077unsigned int *num_input_fmts)4078{4079u32 *input_fmts;40804081input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);4082if (!input_fmts) {4083*num_input_fmts = 0;4084return NULL;4085}40864087*num_input_fmts = 1;4088input_fmts[0] = output_fmt;4089return input_fmts;4090}4091EXPORT_SYMBOL(drm_atomic_helper_bridge_propagate_bus_fmt);409240934094