blob: d7b73cd89b79453a1bdaf4c10618e3625a9d9a56 [file] [log] [blame]
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence.h>
Sean Pauld4da4e32019-09-18 16:07:29 -040029#include <linux/ktime.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010030
Sam Ravnborg0500c042019-05-26 19:35:35 +020031#include <drm/drm_atomic.h>
32#include <drm/drm_atomic_helper.h>
33#include <drm/drm_atomic_uapi.h>
Boris Brezillonee68c742019-08-26 17:26:29 +020034#include <drm/drm_bridge.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020035#include <drm/drm_damage_helper.h>
36#include <drm/drm_device.h>
37#include <drm/drm_plane_helper.h>
38#include <drm/drm_print.h>
Sean Paul1452c252019-06-12 10:50:19 -040039#include <drm/drm_self_refresh_helper.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020040#include <drm/drm_vblank.h>
41#include <drm/drm_writeback.h>
42
Jose Abreufaf94a02017-05-25 15:19:16 +010043#include "drm_crtc_helper_internal.h"
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020044#include "drm_crtc_internal.h"
45
Daniel Vetter3150c7d2014-11-06 20:53:29 +010046/**
47 * DOC: overview
48 *
49 * This helper library provides implementations of check and commit functions on
50 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
51 * also provides convenience implementations for the atomic state handling
52 * callbacks for drivers which don't need to subclass the drm core structures to
53 * add their own additional internal state.
54 *
55 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020056 * drm_atomic_helper_check() and for the commit callback with
57 * drm_atomic_helper_commit(). But the individual stages and callbacks are
58 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010059 * together with a driver private modeset implementation.
60 *
61 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020062 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
63 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010064 * various functions to implement set_property callbacks. New drivers must not
65 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010066 *
67 * The atomic helper uses the same function table structures as all other
Daniel Vetterea0dd852016-12-29 21:48:26 +010068 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
69 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
70 * also shares the &struct drm_plane_helper_funcs function table with the plane
Daniel Vetter092d01d2015-12-04 09:45:44 +010071 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010072 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010073static void
74drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010075 struct drm_plane_state *old_plane_state,
Daniel Vetterc2fcd272014-11-05 00:14:14 +010076 struct drm_plane_state *plane_state,
77 struct drm_plane *plane)
78{
79 struct drm_crtc_state *crtc_state;
80
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010081 if (old_plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010082 crtc_state = drm_atomic_get_new_crtc_state(state,
83 old_plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010084
85 if (WARN_ON(!crtc_state))
86 return;
87
88 crtc_state->planes_changed = true;
89 }
90
91 if (plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010092 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010093
94 if (WARN_ON(!crtc_state))
95 return;
96
97 crtc_state->planes_changed = true;
98 }
99}
100
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100101static int handle_conflicting_encoders(struct drm_atomic_state *state,
102 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +0100103{
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100104 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200105 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100106 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100107 struct drm_encoder *encoder;
108 unsigned encoder_mask = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100109 int i, ret = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200110
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100111 /*
112 * First loop, find all newly assigned encoders from the connectors
113 * part of the state. If the same encoder is assigned to multiple
114 * connectors bail out.
115 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100116 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100117 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
118 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200119
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100120 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200121 continue;
122
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100123 if (funcs->atomic_best_encoder)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100124 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200125 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100126 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200127 else
José Roberto de Souzaa92462d2019-09-13 16:28:56 -0700128 new_encoder = drm_connector_get_single_encoder(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100129
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100130 if (new_encoder) {
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300131 if (encoder_mask & drm_encoder_mask(new_encoder)) {
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100132 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
133 new_encoder->base.id, new_encoder->name,
134 connector->base.id, connector->name);
135
136 return -EINVAL;
137 }
138
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300139 encoder_mask |= drm_encoder_mask(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100140 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200141 }
142
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100143 if (!encoder_mask)
144 return 0;
145
146 /*
147 * Second loop, iterate over all connectors not part of the state.
148 *
149 * If a conflicting encoder is found and disable_conflicting_encoders
150 * is not set, an error is returned. Userspace can provide a solution
151 * through the atomic ioctl.
152 *
Thierry Reding42240c92019-12-06 14:53:36 +0100153 * If the flag is set conflicting connectors are removed from the CRTC
154 * and the CRTC is disabled if no encoder is left. This preserves
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100155 * compatibility with the legacy set_config behavior.
156 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100157 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100158 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100159 struct drm_crtc_state *crtc_state;
160
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100161 if (drm_atomic_get_new_connector_state(state, connector))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100162 continue;
163
164 encoder = connector->state->best_encoder;
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300165 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100166 continue;
167
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100168 if (!disable_conflicting_encoders) {
169 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
170 encoder->base.id, encoder->name,
171 connector->state->crtc->base.id,
172 connector->state->crtc->name,
173 connector->base.id, connector->name);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100174 ret = -EINVAL;
175 goto out;
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100176 }
177
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100178 new_conn_state = drm_atomic_get_connector_state(state, connector);
179 if (IS_ERR(new_conn_state)) {
180 ret = PTR_ERR(new_conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100181 goto out;
182 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100183
184 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
185 encoder->base.id, encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100186 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100187 connector->base.id, connector->name);
188
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100189 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100190
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100191 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100192 if (ret)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100193 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100194
195 if (!crtc_state->connector_mask) {
196 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
197 NULL);
198 if (ret < 0)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100199 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100200
201 crtc_state->active = false;
202 }
203 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100204out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100205 drm_connector_list_iter_end(&conn_iter);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100206
Daniel Vetterc36a3252016-12-15 16:58:43 +0100207 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200208}
209
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100210static void
211set_best_encoder(struct drm_atomic_state *state,
212 struct drm_connector_state *conn_state,
213 struct drm_encoder *encoder)
214{
215 struct drm_crtc_state *crtc_state;
216 struct drm_crtc *crtc;
217
218 if (conn_state->best_encoder) {
219 /* Unset the encoder_mask in the old crtc state. */
220 crtc = conn_state->connector->state->crtc;
221
222 /* A NULL crtc is an error here because we should have
Thierry Redingdbe2d2b2019-12-06 14:53:35 +0100223 * duplicated a NULL best_encoder when crtc was NULL.
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100224 * As an exception restoring duplicated atomic state
225 * during resume is allowed, so don't warn when
226 * best_encoder is equal to encoder we intend to set.
227 */
228 WARN_ON(!crtc && encoder != conn_state->best_encoder);
229 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100230 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100231
232 crtc_state->encoder_mask &=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300233 ~drm_encoder_mask(conn_state->best_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100234 }
235 }
236
237 if (encoder) {
238 crtc = conn_state->crtc;
239 WARN_ON(!crtc);
240 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100241 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100242
243 crtc_state->encoder_mask |=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300244 drm_encoder_mask(encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100245 }
246 }
247
248 conn_state->best_encoder = encoder;
249}
250
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100251static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200252steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100253 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200254{
Daniel Vetter623369e2014-09-16 17:50:47 +0200255 struct drm_crtc_state *crtc_state;
256 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100257 struct drm_connector_state *old_connector_state, *new_connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100258 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200259
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100260 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100261 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200262
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100263 if (new_connector_state->best_encoder != encoder)
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100264 continue;
265
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100266 encoder_crtc = old_connector_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200267
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100268 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
269 encoder->base.id, encoder->name,
270 encoder_crtc->base.id, encoder_crtc->name);
271
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100272 set_best_encoder(state, new_connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100273
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100274 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100275 crtc_state->connectors_changed = true;
276
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100277 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200278 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200279}
280
281static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100282update_connector_routing(struct drm_atomic_state *state,
283 struct drm_connector *connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100284 struct drm_connector_state *old_connector_state,
285 struct drm_connector_state *new_connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200286{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200287 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200288 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200289 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200290
Daniel Vetter17a38d92015-02-22 12:24:16 +0100291 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
292 connector->base.id,
293 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200294
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100295 if (old_connector_state->crtc != new_connector_state->crtc) {
296 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100297 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200298 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200299 }
300
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100301 if (new_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100302 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200303 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200304 }
305 }
306
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100307 if (!new_connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100308 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200309 connector->base.id,
310 connector->name);
311
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100312 set_best_encoder(state, new_connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200313
314 return 0;
315 }
316
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400317 crtc_state = drm_atomic_get_new_crtc_state(state,
318 new_connector_state->crtc);
319 /*
320 * For compatibility with legacy users, we want to make sure that
321 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
322 * which would result in anything else must be considered invalid, to
323 * avoid turning on new displays on dead connectors.
324 *
325 * Since the connector can be unregistered at any point during an
326 * atomic check or commit, this is racy. But that's OK: all we care
327 * about is ensuring that userspace can't do anything but shut off the
Matt Roper1e55a532019-02-01 17:23:26 -0800328 * display on a connector that was destroyed after it's been notified,
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400329 * not before.
Lyude Paul022deba2019-02-01 19:20:03 -0500330 *
331 * Additionally, we also want to ignore connector registration when
332 * we're trying to restore an atomic state during system resume since
333 * there's a chance the connector may have been destroyed during the
334 * process, but it's better to ignore that then cause
335 * drm_atomic_helper_resume() to fail.
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400336 */
Lyude Paul022deba2019-02-01 19:20:03 -0500337 if (!state->duplicated && drm_connector_is_unregistered(connector) &&
338 crtc_state->active) {
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400339 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
340 connector->base.id, connector->name);
341 return -EINVAL;
342 }
343
Daniel Vetter623369e2014-09-16 17:50:47 +0200344 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200345
346 if (funcs->atomic_best_encoder)
347 new_encoder = funcs->atomic_best_encoder(connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100348 new_connector_state);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200349 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200350 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200351 else
José Roberto de Souzaa92462d2019-09-13 16:28:56 -0700352 new_encoder = drm_connector_get_single_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200353
354 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100355 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
356 connector->base.id,
357 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200358 return -EINVAL;
359 }
360
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100361 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
Russell King6ac7c542017-02-13 12:27:03 +0000362 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100363 new_encoder->base.id,
364 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100365 new_connector_state->crtc->base.id,
366 new_connector_state->crtc->name);
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100367 return -EINVAL;
368 }
369
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100370 if (new_encoder == new_connector_state->best_encoder) {
371 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100372
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200373 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100374 connector->base.id,
375 connector->name,
376 new_encoder->base.id,
377 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100378 new_connector_state->crtc->base.id,
379 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200380
381 return 0;
382 }
383
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100384 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200385
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100386 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100387
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200388 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200389
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200390 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100391 connector->base.id,
392 connector->name,
393 new_encoder->base.id,
394 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100395 new_connector_state->crtc->base.id,
396 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200397
398 return 0;
399}
400
401static int
402mode_fixup(struct drm_atomic_state *state)
403{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300404 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100405 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300406 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100407 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200408 int i;
Dan Carpenterf9ad86e2017-02-08 02:46:01 +0300409 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200410
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100411 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
412 if (!new_crtc_state->mode_changed &&
413 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200414 continue;
415
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100416 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200417 }
418
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100419 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200420 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200421 struct drm_encoder *encoder;
Boris Brezillon35a61fe2019-12-03 15:15:07 +0100422 struct drm_bridge *bridge;
Daniel Vetter623369e2014-09-16 17:50:47 +0200423
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100424 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200425
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100426 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200427 continue;
428
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100429 new_crtc_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100430 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200431
432 /*
433 * Each encoder has at most one connector (since we always steal
434 * it away), so we won't call ->mode_fixup twice.
435 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100436 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200437 funcs = encoder->helper_private;
438
Boris Brezillon35a61fe2019-12-03 15:15:07 +0100439 bridge = drm_bridge_chain_get_first_bridge(encoder);
Boris Brezillon8de679a2020-01-07 19:58:05 +0100440 ret = drm_bridge_chain_mode_fixup(bridge,
441 &new_crtc_state->mode,
442 &new_crtc_state->adjusted_mode);
443 if (!ret) {
444 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
445 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200446 }
447
Noralf Trønnes28276352016-05-11 18:09:20 +0200448 if (funcs && funcs->atomic_check) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100449 ret = funcs->atomic_check(encoder, new_crtc_state,
450 new_conn_state);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100451 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100452 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
453 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100454 return ret;
455 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200456 } else if (funcs && funcs->mode_fixup) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100457 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
458 &new_crtc_state->adjusted_mode);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100459 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100460 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
461 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100462 return -EINVAL;
463 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200464 }
465 }
466
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100467 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200468 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200469
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100470 if (!new_crtc_state->enable)
Liu Yingf55f1702016-05-27 17:35:54 +0800471 continue;
472
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100473 if (!new_crtc_state->mode_changed &&
474 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200475 continue;
476
477 funcs = crtc->helper_private;
Ville Syrjälä26564be2019-07-08 15:53:08 +0300478 if (!funcs || !funcs->mode_fixup)
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300479 continue;
480
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100481 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
482 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200483 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200484 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
485 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200486 return -EINVAL;
487 }
488 }
489
490 return 0;
491}
492
Jose Abreufaf94a02017-05-25 15:19:16 +0100493static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
494 struct drm_encoder *encoder,
495 struct drm_crtc *crtc,
Laurent Pinchart8518f052018-06-05 03:36:13 +0300496 const struct drm_display_mode *mode)
Jose Abreufaf94a02017-05-25 15:19:16 +0100497{
Boris Brezillon35a61fe2019-12-03 15:15:07 +0100498 struct drm_bridge *bridge;
Jose Abreufaf94a02017-05-25 15:19:16 +0100499 enum drm_mode_status ret;
500
501 ret = drm_encoder_mode_valid(encoder, mode);
502 if (ret != MODE_OK) {
503 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
504 encoder->base.id, encoder->name);
505 return ret;
506 }
507
Boris Brezillon35a61fe2019-12-03 15:15:07 +0100508 bridge = drm_bridge_chain_get_first_bridge(encoder);
509 ret = drm_bridge_chain_mode_valid(bridge, mode);
Jose Abreufaf94a02017-05-25 15:19:16 +0100510 if (ret != MODE_OK) {
511 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
512 return ret;
513 }
514
515 ret = drm_crtc_mode_valid(crtc, mode);
516 if (ret != MODE_OK) {
517 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
518 crtc->base.id, crtc->name);
519 return ret;
520 }
521
522 return ret;
523}
524
525static int
526mode_valid(struct drm_atomic_state *state)
527{
528 struct drm_connector_state *conn_state;
529 struct drm_connector *connector;
530 int i;
531
532 for_each_new_connector_in_state(state, connector, conn_state, i) {
533 struct drm_encoder *encoder = conn_state->best_encoder;
534 struct drm_crtc *crtc = conn_state->crtc;
535 struct drm_crtc_state *crtc_state;
536 enum drm_mode_status mode_status;
Laurent Pinchart8518f052018-06-05 03:36:13 +0300537 const struct drm_display_mode *mode;
Jose Abreufaf94a02017-05-25 15:19:16 +0100538
539 if (!crtc || !encoder)
540 continue;
541
542 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
543 if (!crtc_state)
544 continue;
545 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
546 continue;
547
548 mode = &crtc_state->mode;
549
550 mode_status = mode_valid_path(connector, encoder, crtc, mode);
551 if (mode_status != MODE_OK)
552 return -EINVAL;
553 }
554
555 return 0;
556}
557
Daniel Vetterd9b13622014-11-26 16:57:41 +0100558/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800559 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100560 * @dev: DRM device
561 * @state: the driver state object
562 *
563 * Check the state object to see if the requested state is physically possible.
Thierry Reding42240c92019-12-06 14:53:36 +0100564 * This does all the CRTC and connector related computations for an atomic
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200565 * update and adds any additional connectors needed for full modesets. It calls
566 * the various per-object callbacks in the follow order:
567 *
568 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
569 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
Thierry Redingdbe2d2b2019-12-06 14:53:35 +0100570 * 3. If it's determined a modeset is needed then all connectors on the affected
Thierry Reding42240c92019-12-06 14:53:36 +0100571 * CRTC are added and &drm_connector_helper_funcs.atomic_check is run on them.
Jose Abreufaf94a02017-05-25 15:19:16 +0100572 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
573 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
574 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
575 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
Thierry Reding42240c92019-12-06 14:53:36 +0100576 * This function is only called when the encoder will be part of a configured CRTC,
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200577 * it must not be used for implementing connector property validation.
578 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
579 * instead.
Thierry Reding42240c92019-12-06 14:53:36 +0100580 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with CRTC constraints.
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200581 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100582 * &drm_crtc_state.mode_changed is set when the input mode is changed.
583 * &drm_crtc_state.connectors_changed is set when a connector is added or
Thierry Reding42240c92019-12-06 14:53:36 +0100584 * removed from the CRTC. &drm_crtc_state.active_changed is set when
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100585 * &drm_crtc_state.active changes, which is used for DPMS.
Thomas Zimmermann7beb6912020-01-29 13:05:17 +0100586 * &drm_crtc_state.no_vblank is set from the result of drm_dev_has_vblank().
Brian Starkeyd807ed12016-10-13 10:47:08 +0100587 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100588 *
589 * IMPORTANT:
590 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100591 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
592 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
593 * without a full modeset) _must_ call this function afterwards after that
594 * change. It is permitted to call this function multiple times for the same
595 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
596 * upon the adjusted dotclock for fifo space allocation and watermark
597 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100598 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200599 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100600 * Zero for success or -errno
601 */
602int
Rob Clark934ce1c2014-11-19 16:41:33 -0500603drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200604 struct drm_atomic_state *state)
605{
Daniel Vetter623369e2014-09-16 17:50:47 +0200606 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100607 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300608 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100609 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200610 int i, ret;
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200611 unsigned connectors_mask = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200612
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100613 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200614 bool has_connectors =
615 !!new_crtc_state->connector_mask;
616
Daniel Vetter869e1882017-05-31 10:38:13 +0200617 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
618
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100619 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200620 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
621 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100622 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200623 }
624
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100625 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200626 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
627 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200628
629 /*
630 * For clarity this assignment is done here, but
631 * enable == 0 is only true when there are no
632 * connectors and a NULL mode.
633 *
634 * The other way around is true as well. enable != 0
635 * iff connectors are attached and a mode is set.
636 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100637 new_crtc_state->mode_changed = true;
638 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200639 }
Maarten Lankhorst24d66522017-04-06 13:19:01 +0200640
641 if (old_crtc_state->active != new_crtc_state->active) {
642 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
643 crtc->base.id, crtc->name);
644 new_crtc_state->active_changed = true;
645 }
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200646
647 if (new_crtc_state->enable != has_connectors) {
648 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
649 crtc->base.id, crtc->name);
650
651 return -EINVAL;
652 }
Thomas Zimmermann7beb6912020-01-29 13:05:17 +0100653
654 if (drm_dev_has_vblank(dev))
655 new_crtc_state->no_vblank = false;
656 else
657 new_crtc_state->no_vblank = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200658 }
659
Maarten Lankhorst44596b82017-04-06 13:19:00 +0200660 ret = handle_conflicting_encoders(state, false);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100661 if (ret)
662 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100663
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100664 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200665 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
666
Daniel Vetter869e1882017-05-31 10:38:13 +0200667 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
668
Daniel Vetter623369e2014-09-16 17:50:47 +0200669 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100670 * This only sets crtc->connectors_changed for routing changes,
671 * drivers must set crtc->connectors_changed themselves when
672 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200673 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100674 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100675 old_connector_state,
676 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200677 if (ret)
678 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100679 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100680 new_crtc_state = drm_atomic_get_new_crtc_state(state,
681 old_connector_state->crtc);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100682 if (old_connector_state->link_status !=
683 new_connector_state->link_status)
684 new_crtc_state->connectors_changed = true;
Radhakrishna Sripada47e22ff2018-10-12 11:42:32 -0700685
686 if (old_connector_state->max_requested_bpc !=
687 new_connector_state->max_requested_bpc)
688 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200689 }
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200690
691 if (funcs->atomic_check)
Sean Paul6f3b6272019-06-11 12:08:18 -0400692 ret = funcs->atomic_check(connector, state);
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200693 if (ret)
694 return ret;
695
Ville Syrjäläca52bea2018-06-15 20:07:34 +0300696 connectors_mask |= BIT(i);
Daniel Vetter623369e2014-09-16 17:50:47 +0200697 }
698
699 /*
700 * After all the routing has been prepared we need to add in any
Thierry Reding42240c92019-12-06 14:53:36 +0100701 * connector which is itself unchanged, but whose CRTC changes its
Daniel Vetter623369e2014-09-16 17:50:47 +0200702 * configuration. This must be done before calling mode_fixup in case a
703 * crtc only changed its mode but has the same set of connectors.
704 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100705 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100706 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100707 continue;
708
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200709 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
710 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100711 new_crtc_state->enable ? 'y' : 'n',
712 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200713
714 ret = drm_atomic_add_affected_connectors(state, crtc);
715 if (ret != 0)
716 return ret;
717
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200718 ret = drm_atomic_add_affected_planes(state, crtc);
719 if (ret != 0)
720 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200721 }
722
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200723 /*
724 * Iterate over all connectors again, to make sure atomic_check()
725 * has been called on them when a modeset is forced.
726 */
727 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
728 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
729
730 if (connectors_mask & BIT(i))
731 continue;
732
733 if (funcs->atomic_check)
Sean Paul6f3b6272019-06-11 12:08:18 -0400734 ret = funcs->atomic_check(connector, state);
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200735 if (ret)
736 return ret;
737 }
738
Jose Abreufaf94a02017-05-25 15:19:16 +0100739 ret = mode_valid(state);
740 if (ret)
741 return ret;
742
Daniel Vetter623369e2014-09-16 17:50:47 +0200743 return mode_fixup(state);
744}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100745EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200746
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100747/**
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200748 * drm_atomic_helper_check_plane_state() - Check plane state for validity
749 * @plane_state: plane state to check
Thierry Reding42240c92019-12-06 14:53:36 +0100750 * @crtc_state: CRTC state to check
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200751 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
752 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
753 * @can_position: is it legal to position the plane such that it
Thierry Reding42240c92019-12-06 14:53:36 +0100754 * doesn't cover the entire CRTC? This will generally
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200755 * only be false for primary planes.
Thierry Reding42240c92019-12-06 14:53:36 +0100756 * @can_update_disabled: can the plane be updated while the CRTC
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200757 * is disabled?
758 *
759 * Checks that a desired plane update is valid, and updates various
760 * bits of derived state (clipped coordinates etc.). Drivers that provide
761 * their own plane handling rather than helper-provided implementations may
762 * still wish to call this function to avoid duplication of error checking
763 * code.
764 *
765 * RETURNS:
766 * Zero if update appears valid, error code on failure
767 */
768int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
769 const struct drm_crtc_state *crtc_state,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200770 int min_scale,
771 int max_scale,
772 bool can_position,
773 bool can_update_disabled)
774{
775 struct drm_framebuffer *fb = plane_state->fb;
776 struct drm_rect *src = &plane_state->src;
777 struct drm_rect *dst = &plane_state->dst;
778 unsigned int rotation = plane_state->rotation;
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200779 struct drm_rect clip = {};
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200780 int hscale, vscale;
781
782 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
783
784 *src = drm_plane_state_src(plane_state);
785 *dst = drm_plane_state_dest(plane_state);
786
787 if (!fb) {
788 plane_state->visible = false;
789 return 0;
790 }
791
792 /* crtc should only be NULL when disabling (i.e., !fb) */
793 if (WARN_ON(!plane_state->crtc)) {
794 plane_state->visible = false;
795 return 0;
796 }
797
798 if (!crtc_state->enable && !can_update_disabled) {
799 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
800 return -EINVAL;
801 }
802
803 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
804
805 /* Check scaling */
806 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
807 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
808 if (hscale < 0 || vscale < 0) {
809 DRM_DEBUG_KMS("Invalid scaling of plane\n");
810 drm_rect_debug_print("src: ", &plane_state->src, true);
811 drm_rect_debug_print("dst: ", &plane_state->dst, false);
812 return -ERANGE;
813 }
814
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200815 if (crtc_state->enable)
816 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
817
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +0200818 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200819
820 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
821
822 if (!plane_state->visible)
823 /*
824 * Plane isn't visible; some drivers can handle this
825 * so we just return success here. Drivers that can't
826 * (including those that use the primary plane helper's
827 * update function) will return an error from their
828 * update_plane handler.
829 */
830 return 0;
831
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200832 if (!can_position && !drm_rect_equals(dst, &clip)) {
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200833 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
834 drm_rect_debug_print("dst: ", dst, false);
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200835 drm_rect_debug_print("clip: ", &clip, false);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200836 return -EINVAL;
837 }
838
839 return 0;
840}
841EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
842
843/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800844 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100845 * @dev: DRM device
846 * @state: the driver state object
847 *
848 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100849 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100850 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
851 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100852 *
Thierry Reding42240c92019-12-06 14:53:36 +0100853 * It also sets &drm_crtc_state.planes_changed to indicate that a CRTC has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200854 * updated planes.
855 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200856 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100857 * Zero for success or -errno
858 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100859int
860drm_atomic_helper_check_planes(struct drm_device *dev,
861 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100862{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300863 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100864 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300865 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100866 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100867 int i, ret = 0;
868
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100869 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200870 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100871
Daniel Vetter869e1882017-05-31 10:38:13 +0200872 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
873
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100874 funcs = plane->helper_private;
875
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100876 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100877
Deepak Rawatd9778b42018-08-08 17:36:26 -0700878 drm_atomic_helper_check_plane_damage(state, new_plane_state);
879
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100880 if (!funcs || !funcs->atomic_check)
881 continue;
882
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100883 ret = funcs->atomic_check(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100884 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200885 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
886 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100887 return ret;
888 }
889 }
890
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100891 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200892 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100893
894 funcs = crtc->helper_private;
895
896 if (!funcs || !funcs->atomic_check)
897 continue;
898
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100899 ret = funcs->atomic_check(crtc, new_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100900 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200901 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
902 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100903 return ret;
904 }
905 }
906
Daniel Vetterd9b13622014-11-26 16:57:41 +0100907 return ret;
908}
909EXPORT_SYMBOL(drm_atomic_helper_check_planes);
910
911/**
912 * drm_atomic_helper_check - validate state object
913 * @dev: DRM device
914 * @state: the driver state object
915 *
916 * Check the state object to see if the requested state is physically possible.
Thierry Reding42240c92019-12-06 14:53:36 +0100917 * Only CRTCs and planes have check callbacks, so for any additional (global)
Daniel Vetterd9b13622014-11-26 16:57:41 +0100918 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100919 * Drivers without such needs can directly use this as their
920 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100921 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100922 * This just wraps the two parts of the state checking for planes and modeset
923 * state in the default order: First it calls drm_atomic_helper_check_modeset()
924 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100925 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
926 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
927 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100928 *
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200929 * Note that zpos normalization will add all enable planes to the state which
930 * might not desired for some drivers.
931 * For example enable/disable of a cursor plane which have fixed zpos value
932 * would trigger all other enabled planes to be forced to the state change.
933 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200934 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100935 * Zero for success or -errno
936 */
937int drm_atomic_helper_check(struct drm_device *dev,
938 struct drm_atomic_state *state)
939{
940 int ret;
941
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100942 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100943 if (ret)
944 return ret;
945
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200946 if (dev->mode_config.normalize_zpos) {
947 ret = drm_atomic_normalize_zpos(dev, state);
948 if (ret)
949 return ret;
950 }
951
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100952 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500953 if (ret)
954 return ret;
955
Gustavo Padovanfef9df82017-06-30 15:03:17 -0300956 if (state->legacy_cursor_update)
957 state->async_update = !drm_atomic_helper_async_check(dev, state);
958
Sean Paul1452c252019-06-12 10:50:19 -0400959 drm_self_refresh_helper_alter_state(state);
960
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100961 return ret;
962}
963EXPORT_SYMBOL(drm_atomic_helper_check);
964
Sean Paul1452c252019-06-12 10:50:19 -0400965static bool
966crtc_needs_disable(struct drm_crtc_state *old_state,
967 struct drm_crtc_state *new_state)
968{
969 /*
Thierry Reding42240c92019-12-06 14:53:36 +0100970 * No new_state means the CRTC is off, so the only criteria is whether
Sean Paul1452c252019-06-12 10:50:19 -0400971 * it's currently active or in self refresh mode.
972 */
973 if (!new_state)
974 return drm_atomic_crtc_effectively_active(old_state);
975
976 /*
Thierry Reding42240c92019-12-06 14:53:36 +0100977 * We need to run through the crtc_funcs->disable() function if the CRTC
Sean Paul1452c252019-06-12 10:50:19 -0400978 * is currently on, if it's transitioning to self refresh mode, or if
979 * it's in self refresh mode and needs to be fully disabled.
980 */
981 return old_state->active ||
982 (old_state->self_refresh_active && !new_state->enable) ||
983 new_state->self_refresh_active;
984}
985
Daniel Vetter623369e2014-09-16 17:50:47 +0200986static void
987disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
988{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300989 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100990 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300991 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100992 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200993 int i;
994
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100995 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200996 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200997 struct drm_encoder *encoder;
Boris Brezillon35a61fe2019-12-03 15:15:07 +0100998 struct drm_bridge *bridge;
Daniel Vetter623369e2014-09-16 17:50:47 +0200999
Daniel Vetter623369e2014-09-16 17:50:47 +02001000 /* Shut down everything that's in the changeset and currently
1001 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001002 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +02001003 continue;
1004
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001005 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001006
Sean Paul1452c252019-06-12 10:50:19 -04001007 if (new_conn_state->crtc)
1008 new_crtc_state = drm_atomic_get_new_crtc_state(
1009 old_state,
1010 new_conn_state->crtc);
1011 else
1012 new_crtc_state = NULL;
1013
1014 if (!crtc_needs_disable(old_crtc_state, new_crtc_state) ||
Daniel Vetter2465ff62015-06-18 09:58:55 +02001015 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001016 continue;
1017
Rob Clark46df9ad2014-11-20 15:40:36 -05001018 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001019
Rob Clark46df9ad2014-11-20 15:40:36 -05001020 /* We shouldn't get this far if we didn't previously have
1021 * an encoder.. but WARN_ON() rather than explode.
1022 */
1023 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +02001024 continue;
1025
1026 funcs = encoder->helper_private;
1027
Daniel Vetter17a38d92015-02-22 12:24:16 +01001028 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
1029 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001030
Daniel Vetter623369e2014-09-16 17:50:47 +02001031 /*
1032 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001033 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001034 */
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001035 bridge = drm_bridge_chain_get_first_bridge(encoder);
1036 drm_atomic_bridge_chain_disable(bridge, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001037
1038 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +02001039 if (funcs) {
Sean Paul43c76d72019-06-11 16:49:53 -04001040 if (funcs->atomic_disable)
1041 funcs->atomic_disable(encoder, old_state);
1042 else if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +02001043 funcs->prepare(encoder);
1044 else if (funcs->disable)
1045 funcs->disable(encoder);
1046 else if (funcs->dpms)
1047 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1048 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001049
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001050 drm_atomic_bridge_chain_post_disable(bridge, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001051 }
1052
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001053 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001054 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter84014b02017-10-17 17:27:14 +02001055 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +02001056
1057 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001058 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001059 continue;
1060
Sean Paul1452c252019-06-12 10:50:19 -04001061 if (!crtc_needs_disable(old_crtc_state, new_crtc_state))
Daniel Vetter623369e2014-09-16 17:50:47 +02001062 continue;
1063
1064 funcs = crtc->helper_private;
1065
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001066 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1067 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001068
1069
Daniel Vetter623369e2014-09-16 17:50:47 +02001070 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001071 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +02001072 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +08001073 else if (funcs->atomic_disable)
1074 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001075 else if (funcs->disable)
1076 funcs->disable(crtc);
Rodrigo Siqueirafe616922019-03-14 15:48:45 -03001077 else if (funcs->dpms)
Daniel Vetter623369e2014-09-16 17:50:47 +02001078 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Daniel Vetter84014b02017-10-17 17:27:14 +02001079
1080 if (!(dev->irq_enabled && dev->num_crtcs))
1081 continue;
1082
1083 ret = drm_crtc_vblank_get(crtc);
1084 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1085 if (ret == 0)
1086 drm_crtc_vblank_put(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001087 }
1088}
1089
Daniel Vetter4c18d302015-05-12 15:27:37 +02001090/**
1091 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1092 * @dev: DRM device
1093 * @old_state: atomic state object with old state structures
1094 *
1095 * This function updates all the various legacy modeset state pointers in
Thierry Reding42240c92019-12-06 14:53:36 +01001096 * connectors, encoders and CRTCs. It also updates the timestamping constants
Daniel Vetter4c18d302015-05-12 15:27:37 +02001097 * used for precise vblank timestamps by calling
1098 * drm_calc_timestamping_constants().
1099 *
1100 * Drivers can use this for building their own atomic commit if they don't have
1101 * a pure helper-based modeset implementation.
Daniel Vetter2e2b96e2017-11-08 21:30:07 +01001102 *
1103 * Since these updates are not synchronized with lockings, only code paths
1104 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1105 * legacy state filled out by this helper. Defacto this means this helper and
1106 * the legacy state pointers are only really useful for transitioning an
1107 * existing driver to the atomic world.
Daniel Vetter4c18d302015-05-12 15:27:37 +02001108 */
1109void
1110drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1111 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001112{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001113 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001114 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001115 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001116 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001117 int i;
1118
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001119 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001120 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001121 if (connector->encoder) {
1122 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001123
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001124 connector->encoder->crtc = NULL;
1125 connector->encoder = NULL;
1126 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001127
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001128 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001129 if ((!crtc && old_conn_state->crtc) ||
1130 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001131 int mode = DRM_MODE_DPMS_OFF;
1132
1133 if (crtc && crtc->state->active)
1134 mode = DRM_MODE_DPMS_ON;
1135
1136 connector->dpms = mode;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001137 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001138 }
1139
1140 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001141 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1142 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +02001143 continue;
1144
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001145 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +02001146 continue;
1147
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001148 connector->encoder = new_conn_state->best_encoder;
1149 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +02001150 }
1151
1152 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001153 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +02001154 struct drm_plane *primary = crtc->primary;
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001155 struct drm_plane_state *new_plane_state;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001156
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001157 crtc->mode = new_crtc_state->mode;
1158 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001159
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001160 new_plane_state =
1161 drm_atomic_get_new_plane_state(old_state, primary);
1162
1163 if (new_plane_state && new_plane_state->crtc == crtc) {
1164 crtc->x = new_plane_state->src_x >> 16;
1165 crtc->y = new_plane_state->src_y >> 16;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001166 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +02001167
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001168 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +02001169 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001170 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001171 }
1172}
Daniel Vetter4c18d302015-05-12 15:27:37 +02001173EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001174
1175static void
1176crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1177{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001178 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001179 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001180 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001181 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001182 int i;
1183
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001184 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001185 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001186
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001187 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +02001188 continue;
1189
1190 funcs = crtc->helper_private;
1191
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001192 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001193 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1194 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001195
Daniel Vetter623369e2014-09-16 17:50:47 +02001196 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001197 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001198 }
1199
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001200 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001201 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001202 struct drm_encoder *encoder;
1203 struct drm_display_mode *mode, *adjusted_mode;
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001204 struct drm_bridge *bridge;
Daniel Vetter623369e2014-09-16 17:50:47 +02001205
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001206 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001207 continue;
1208
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001209 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001210 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001211 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001212 mode = &new_crtc_state->mode;
1213 adjusted_mode = &new_crtc_state->adjusted_mode;
1214
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001215 if (!new_crtc_state->mode_changed)
1216 continue;
1217
Daniel Vetter17a38d92015-02-22 12:24:16 +01001218 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1219 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001220
Daniel Vetter623369e2014-09-16 17:50:47 +02001221 /*
1222 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001223 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001224 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001225 if (funcs && funcs->atomic_mode_set) {
1226 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001227 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001228 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +01001229 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001230 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001231
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001232 bridge = drm_bridge_chain_get_first_bridge(encoder);
1233 drm_bridge_chain_mode_set(bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001234 }
1235}
1236
1237/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001238 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001239 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +01001240 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001241 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001242 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +02001243 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +01001244 *
Thierry Reding42240c92019-12-06 14:53:36 +01001245 * For compatibility with legacy CRTC helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +01001246 * drm_atomic_helper_commit_planes(), which is what the default commit function
1247 * does. But drivers with different needs can group the modeset commits together
1248 * and do the plane commits at the end. This is useful for drivers doing runtime
1249 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001250 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001251void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1252 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001253{
Laurent Pincharta072f802015-02-22 12:24:18 +01001254 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +02001255
1256 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1257
Laurent Pincharta072f802015-02-22 12:24:18 +01001258 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001259}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001260EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001261
Brian Starkey935774c2017-03-29 17:42:32 +01001262static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1263 struct drm_atomic_state *old_state)
1264{
1265 struct drm_connector *connector;
1266 struct drm_connector_state *new_conn_state;
1267 int i;
1268
1269 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1270 const struct drm_connector_helper_funcs *funcs;
1271
1272 funcs = connector->helper_private;
Boris Brezillon814bde92018-07-03 09:50:17 +02001273 if (!funcs->atomic_commit)
1274 continue;
Brian Starkey935774c2017-03-29 17:42:32 +01001275
1276 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1277 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
Boris Brezillon425132f2018-07-03 09:50:16 +02001278 funcs->atomic_commit(connector, new_conn_state);
Brian Starkey935774c2017-03-29 17:42:32 +01001279 }
1280 }
1281}
1282
Daniel Vetter623369e2014-09-16 17:50:47 +02001283/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001284 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001285 * @dev: DRM device
1286 * @old_state: atomic state object with old state structures
1287 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001288 * This function enables all the outputs with the new configuration which had to
1289 * be turned off for the update.
1290 *
Thierry Reding42240c92019-12-06 14:53:36 +01001291 * For compatibility with legacy CRTC helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +01001292 * drm_atomic_helper_commit_planes(), which is what the default commit function
1293 * does. But drivers with different needs can group the modeset commits together
1294 * and do the plane commits at the end. This is useful for drivers doing runtime
1295 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001296 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001297void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1298 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001299{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001300 struct drm_crtc *crtc;
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001301 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001302 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001303 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001304 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001305 int i;
1306
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001307 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001308 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001309
1310 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001311 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001312 continue;
1313
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001314 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +02001315 continue;
1316
1317 funcs = crtc->helper_private;
1318
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001319 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001320 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1321 crtc->base.id, crtc->name);
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001322 if (funcs->atomic_enable)
1323 funcs->atomic_enable(crtc, old_crtc_state);
Rodrigo Siqueirafe616922019-03-14 15:48:45 -03001324 else if (funcs->commit)
Daniel Vetteree0a89c2015-01-22 16:36:24 +01001325 funcs->commit(crtc);
1326 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001327 }
1328
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001329 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001330 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001331 struct drm_encoder *encoder;
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001332 struct drm_bridge *bridge;
Daniel Vetter623369e2014-09-16 17:50:47 +02001333
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001334 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001335 continue;
1336
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001337 if (!new_conn_state->crtc->state->active ||
1338 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001339 continue;
1340
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001341 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001342 funcs = encoder->helper_private;
1343
Daniel Vetter17a38d92015-02-22 12:24:16 +01001344 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1345 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001346
Daniel Vetter623369e2014-09-16 17:50:47 +02001347 /*
1348 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001349 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001350 */
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001351 bridge = drm_bridge_chain_get_first_bridge(encoder);
1352 drm_atomic_bridge_chain_pre_enable(bridge, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001353
Noralf Trønnes28276352016-05-11 18:09:20 +02001354 if (funcs) {
Sean Paul43c76d72019-06-11 16:49:53 -04001355 if (funcs->atomic_enable)
1356 funcs->atomic_enable(encoder, old_state);
1357 else if (funcs->enable)
Noralf Trønnes28276352016-05-11 18:09:20 +02001358 funcs->enable(encoder);
1359 else if (funcs->commit)
1360 funcs->commit(encoder);
1361 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001362
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001363 drm_atomic_bridge_chain_enable(bridge, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001364 }
Brian Starkey935774c2017-03-29 17:42:32 +01001365
1366 drm_atomic_helper_commit_writebacks(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001367}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001368EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001369
Rob Clark4c5b7f32016-03-18 19:14:55 -04001370/**
1371 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1372 * @dev: DRM device
1373 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001374 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1375 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001376 *
1377 * For implicit sync, driver should fish the exclusive fence out from the
1378 * incoming fb's and stash it in the drm_plane_state. This is called after
1379 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1380 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001381 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001382 * Note that @pre_swap is needed since the point where we block for fences moves
1383 * around depending upon whether an atomic commit is blocking or
Gustavo Padovan42590372017-04-27 11:35:06 -03001384 * non-blocking. For non-blocking commit all waiting needs to happen after
1385 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001386 * to wait **before** we do anything that can't be easily rolled back. That is
1387 * before we call drm_atomic_helper_swap_state().
1388 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001389 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001390 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001391int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1392 struct drm_atomic_state *state,
1393 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001394{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001395 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001396 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001397 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001398
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001399 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1400 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001401 continue;
1402
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001403 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001404
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001405 /*
1406 * If waiting for fences pre-swap (ie: nonblock), userspace can
1407 * still interrupt the operation. Instead of blocking until the
1408 * timer expires, make the wait interruptible.
1409 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001410 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001411 if (ret)
1412 return ret;
1413
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001414 dma_fence_put(new_plane_state->fence);
1415 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001416 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001417
1418 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001419}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001420EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001421
John Keepingc2409062016-01-19 10:46:58 +00001422/**
Thierry Reding42240c92019-12-06 14:53:36 +01001423 * drm_atomic_helper_wait_for_vblanks - wait for vblank on CRTCs
Rob Clark5ee32292014-11-11 19:38:59 -05001424 * @dev: DRM device
1425 * @old_state: atomic state object with old state structures
1426 *
Thierry Redingdbe2d2b2019-12-06 14:53:35 +01001427 * Helper to, after atomic commit, wait for vblanks on all affected
Thierry Reding42240c92019-12-06 14:53:36 +01001428 * CRTCs (ie. before cleaning up old framebuffers using
Boris Brezillon01086482017-06-02 10:32:06 +02001429 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
Daniel Vetterab58e332014-11-24 20:42:42 +01001430 * framebuffers have actually changed to optimize for the legacy cursor and
1431 * plane update use-case.
Boris Brezillon01086482017-06-02 10:32:06 +02001432 *
1433 * Drivers using the nonblocking commit tracking support initialized by calling
1434 * drm_atomic_helper_setup_commit() should look at
1435 * drm_atomic_helper_wait_for_flip_done() as an alternative.
Rob Clark5ee32292014-11-11 19:38:59 -05001436 */
1437void
1438drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1439 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001440{
1441 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001442 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001443 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001444 unsigned crtc_mask = 0;
1445
1446 /*
1447 * Legacy cursor ioctls are completely unsynced, and userspace
1448 * relies on that (by doing tons of cursor updates).
1449 */
1450 if (old_state->legacy_cursor_update)
1451 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001452
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001453 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Lucas Stacha76dfe32017-11-29 12:04:31 +01001454 if (!new_crtc_state->active)
Daniel Vetterab58e332014-11-24 20:42:42 +01001455 continue;
1456
Daniel Vetter623369e2014-09-16 17:50:47 +02001457 ret = drm_crtc_vblank_get(crtc);
1458 if (ret != 0)
1459 continue;
1460
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001461 crtc_mask |= drm_crtc_mask(crtc);
1462 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001463 }
1464
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001465 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001466 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001467 continue;
1468
1469 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001470 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001471 drm_crtc_vblank_count(crtc),
Linus Walleijb3198c32019-04-30 11:37:46 +02001472 msecs_to_jiffies(100));
Daniel Vetter623369e2014-09-16 17:50:47 +02001473
Russell King6ac7c542017-02-13 12:27:03 +00001474 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1475 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001476
Daniel Vetter623369e2014-09-16 17:50:47 +02001477 drm_crtc_vblank_put(crtc);
1478 }
1479}
Rob Clark5ee32292014-11-11 19:38:59 -05001480EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001481
1482/**
Boris Brezillon01086482017-06-02 10:32:06 +02001483 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1484 * @dev: DRM device
1485 * @old_state: atomic state object with old state structures
1486 *
Thierry Redingdbe2d2b2019-12-06 14:53:35 +01001487 * Helper to, after atomic commit, wait for page flips on all affected
Boris Brezillon01086482017-06-02 10:32:06 +02001488 * crtcs (ie. before cleaning up old framebuffers using
1489 * drm_atomic_helper_cleanup_planes()). Compared to
Thierry Redingdbe2d2b2019-12-06 14:53:35 +01001490 * drm_atomic_helper_wait_for_vblanks() this waits for the completion on all
Boris Brezillon01086482017-06-02 10:32:06 +02001491 * CRTCs, assuming that cursors-only updates are signalling their completion
1492 * immediately (or using a different path).
1493 *
1494 * This requires that drivers use the nonblocking commit tracking support
1495 * initialized using drm_atomic_helper_setup_commit().
1496 */
1497void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1498 struct drm_atomic_state *old_state)
1499{
Boris Brezillon01086482017-06-02 10:32:06 +02001500 struct drm_crtc *crtc;
1501 int i;
1502
Leo Li4364bcb2018-10-15 09:46:40 -04001503 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1504 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
Boris Brezillon01086482017-06-02 10:32:06 +02001505 int ret;
1506
Leo Li4364bcb2018-10-15 09:46:40 -04001507 crtc = old_state->crtcs[i].ptr;
1508
1509 if (!crtc || !commit)
Boris Brezillon01086482017-06-02 10:32:06 +02001510 continue;
1511
1512 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1513 if (ret == 0)
1514 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1515 crtc->base.id, crtc->name);
1516 }
Ville Syrjälä2de42f72018-11-22 16:34:11 +02001517
1518 if (old_state->fake_commit)
1519 complete_all(&old_state->fake_commit->flip_done);
Boris Brezillon01086482017-06-02 10:32:06 +02001520}
1521EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1522
1523/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001524 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001525 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001526 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001527 * This is the default implementation for the
Maxime Ripard81a099a2017-07-20 15:01:16 +02001528 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1529 * that do not support runtime_pm or do not need the CRTC to be
1530 * enabled to perform a commit. Otherwise, see
1531 * drm_atomic_helper_commit_tail_rpm().
Daniel Vetter623369e2014-09-16 17:50:47 +02001532 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001533 * Note that the default ordering of how the various stages are called is to
Maxime Ripard81a099a2017-07-20 15:01:16 +02001534 * match the legacy modeset helper library closest.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001535 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001536void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001537{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001538 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001539
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001540 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001541
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001542 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001543
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001544 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001545
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001546 drm_atomic_helper_fake_vblank(old_state);
1547
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001548 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001549
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001550 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001551
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001552 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001553}
1554EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1555
Maxime Ripard81a099a2017-07-20 15:01:16 +02001556/**
1557 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1558 * @old_state: new modeset state to be committed
1559 *
1560 * This is an alternative implementation for the
1561 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1562 * that support runtime_pm or need the CRTC to be enabled to perform a
1563 * commit. Otherwise, one should use the default implementation
1564 * drm_atomic_helper_commit_tail().
1565 */
1566void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1567{
1568 struct drm_device *dev = old_state->dev;
1569
1570 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1571
1572 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1573
1574 drm_atomic_helper_commit_planes(dev, old_state,
1575 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1576
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001577 drm_atomic_helper_fake_vblank(old_state);
1578
Maxime Ripard81a099a2017-07-20 15:01:16 +02001579 drm_atomic_helper_commit_hw_done(old_state);
1580
1581 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1582
1583 drm_atomic_helper_cleanup_planes(dev, old_state);
1584}
1585EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1586
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001587static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001588{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001589 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001590 const struct drm_mode_config_helper_funcs *funcs;
Rob Clark86de88c2019-11-04 09:37:36 -08001591 struct drm_crtc_state *new_crtc_state;
1592 struct drm_crtc *crtc;
Sean Pauld4da4e32019-09-18 16:07:29 -04001593 ktime_t start;
1594 s64 commit_time_ms;
Rob Clark86de88c2019-11-04 09:37:36 -08001595 unsigned int i, new_self_refresh_mask = 0;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001596
1597 funcs = dev->mode_config.helper_private;
1598
Sean Pauld4da4e32019-09-18 16:07:29 -04001599 /*
1600 * We're measuring the _entire_ commit, so the time will vary depending
1601 * on how many fences and objects are involved. For the purposes of self
1602 * refresh, this is desirable since it'll give us an idea of how
1603 * congested things are. This will inform our decision on how often we
1604 * should enter self refresh after idle.
1605 *
1606 * These times will be averaged out in the self refresh helpers to avoid
1607 * overreacting over one outlier frame
1608 */
1609 start = ktime_get();
1610
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001611 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001612
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001613 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001614
Rob Clark86de88c2019-11-04 09:37:36 -08001615 /*
1616 * We cannot safely access new_crtc_state after
1617 * drm_atomic_helper_commit_hw_done() so figure out which crtc's have
1618 * self-refresh active beforehand:
1619 */
1620 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i)
1621 if (new_crtc_state->self_refresh_active)
1622 new_self_refresh_mask |= BIT(i);
1623
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001624 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001625 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001626 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001627 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001628
Sean Pauld4da4e32019-09-18 16:07:29 -04001629 commit_time_ms = ktime_ms_delta(ktime_get(), start);
1630 if (commit_time_ms > 0)
1631 drm_self_refresh_helper_update_avg_times(old_state,
Rob Clark86de88c2019-11-04 09:37:36 -08001632 (unsigned long)commit_time_ms,
1633 new_self_refresh_mask);
Sean Pauld4da4e32019-09-18 16:07:29 -04001634
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001635 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001636
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001637 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001638}
1639
1640static void commit_work(struct work_struct *work)
1641{
1642 struct drm_atomic_state *state = container_of(work,
1643 struct drm_atomic_state,
1644 commit_work);
1645 commit_tail(state);
1646}
1647
1648/**
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001649 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1650 * @dev: DRM device
1651 * @state: the driver state object
1652 *
1653 * This helper will check if it is possible to commit the state asynchronously.
1654 * Async commits are not supposed to swap the states like normal sync commits
1655 * but just do in-place changes on the current state.
1656 *
1657 * It will return 0 if the commit can happen in an asynchronous fashion or error
1658 * if not. Note that error just mean it can't be commited asynchronously, if it
1659 * fails the commit should be treated like a normal synchronous commit.
1660 */
1661int drm_atomic_helper_async_check(struct drm_device *dev,
1662 struct drm_atomic_state *state)
1663{
1664 struct drm_crtc *crtc;
1665 struct drm_crtc_state *crtc_state;
Boris Brezillonde2d8db2018-07-24 15:33:00 +02001666 struct drm_plane *plane = NULL;
1667 struct drm_plane_state *old_plane_state = NULL;
1668 struct drm_plane_state *new_plane_state = NULL;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001669 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001670 int i, n_planes = 0;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001671
1672 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1673 if (drm_atomic_crtc_needs_modeset(crtc_state))
1674 return -EINVAL;
1675 }
1676
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001677 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001678 n_planes++;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001679
1680 /* FIXME: we support only single plane updates for now */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001681 if (n_planes != 1)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001682 return -EINVAL;
1683
Boris Brezillon603ba2d2018-07-24 15:32:15 +02001684 if (!new_plane_state->crtc ||
1685 old_plane_state->crtc != new_plane_state->crtc)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001686 return -EINVAL;
1687
1688 funcs = plane->helper_private;
1689 if (!funcs->atomic_async_update)
1690 return -EINVAL;
1691
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001692 if (new_plane_state->fence)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001693 return -EINVAL;
1694
1695 /*
1696 * Don't do an async update if there is an outstanding commit modifying
1697 * the plane. This prevents our async update's changes from getting
1698 * overridden by a previous synchronous update's state.
1699 */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001700 if (old_plane_state->commit &&
1701 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1702 return -EBUSY;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001703
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001704 return funcs->atomic_async_check(plane, new_plane_state);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001705}
1706EXPORT_SYMBOL(drm_atomic_helper_async_check);
1707
1708/**
1709 * drm_atomic_helper_async_commit - commit state asynchronously
1710 * @dev: DRM device
1711 * @state: the driver state object
1712 *
1713 * This function commits a state asynchronously, i.e., not vblank
1714 * synchronized. It should be used on a state only when
1715 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1716 * the states like normal sync commits, but just do in-place changes on the
1717 * current state.
Helen Koike89a4aac2019-06-03 13:56:10 -03001718 *
1719 * TODO: Implement full swap instead of doing in-place changes.
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001720 */
1721void drm_atomic_helper_async_commit(struct drm_device *dev,
1722 struct drm_atomic_state *state)
1723{
1724 struct drm_plane *plane;
1725 struct drm_plane_state *plane_state;
1726 const struct drm_plane_helper_funcs *funcs;
1727 int i;
1728
1729 for_each_new_plane_in_state(state, plane, plane_state, i) {
Helen Koike89a4aac2019-06-03 13:56:10 -03001730 struct drm_framebuffer *new_fb = plane_state->fb;
1731 struct drm_framebuffer *old_fb = plane->state->fb;
1732
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001733 funcs = plane->helper_private;
1734 funcs->atomic_async_update(plane, plane_state);
Boris Brezillon02edfd92018-03-30 16:55:18 +02001735
1736 /*
1737 * ->atomic_async_update() is supposed to update the
1738 * plane->state in-place, make sure at least common
1739 * properties have been properly updated.
1740 */
Helen Koike89a4aac2019-06-03 13:56:10 -03001741 WARN_ON_ONCE(plane->state->fb != new_fb);
Boris Brezillon02edfd92018-03-30 16:55:18 +02001742 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1743 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1744 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1745 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
Helen Koike89a4aac2019-06-03 13:56:10 -03001746
1747 /*
1748 * Make sure the FBs have been swapped so that cleanups in the
1749 * new_state performs a cleanup in the old FB.
1750 */
1751 WARN_ON_ONCE(plane_state->fb != old_fb);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001752 }
1753}
1754EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1755
1756/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001757 * drm_atomic_helper_commit - commit validated state object
1758 * @dev: DRM device
1759 * @state: the driver state object
1760 * @nonblock: whether nonblocking behavior is requested.
1761 *
1762 * This function commits a with drm_atomic_helper_check() pre-validated state
1763 * object. This can still fail when e.g. the framebuffer reservation fails. This
1764 * function implements nonblocking commits, using
1765 * drm_atomic_helper_setup_commit() and related functions.
1766 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001767 * Committing the actual hardware state is done through the
Matt Roper1e55a532019-02-01 17:23:26 -08001768 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or its default
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001769 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001770 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001771 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001772 * Zero for success or -errno.
1773 */
1774int drm_atomic_helper_commit(struct drm_device *dev,
1775 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001776 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001777{
1778 int ret;
1779
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001780 if (state->async_update) {
1781 ret = drm_atomic_helper_prepare_planes(dev, state);
1782 if (ret)
1783 return ret;
1784
1785 drm_atomic_helper_async_commit(dev, state);
1786 drm_atomic_helper_cleanup_planes(dev, state);
1787
1788 return 0;
1789 }
1790
Daniel Vettera095caa2016-06-08 17:15:36 +02001791 ret = drm_atomic_helper_setup_commit(state, nonblock);
1792 if (ret)
1793 return ret;
1794
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001795 INIT_WORK(&state->commit_work, commit_work);
1796
Daniel Vetter623369e2014-09-16 17:50:47 +02001797 ret = drm_atomic_helper_prepare_planes(dev, state);
1798 if (ret)
1799 return ret;
1800
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001801 if (!nonblock) {
1802 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001803 if (ret)
1804 goto err;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001805 }
1806
Daniel Vetter623369e2014-09-16 17:50:47 +02001807 /*
1808 * This is the point of no return - everything below never fails except
1809 * when the hw goes bonghits. Which means we can commit the new state on
1810 * the software side now.
1811 */
1812
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001813 ret = drm_atomic_helper_swap_state(state, true);
1814 if (ret)
1815 goto err;
Daniel Vetter623369e2014-09-16 17:50:47 +02001816
1817 /*
1818 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001819 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001820 * that the asynchronous work has either been cancelled (if the driver
1821 * supports it, which at least requires that the framebuffers get
1822 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1823 * before the new state gets committed on the software side with
1824 * drm_atomic_helper_swap_state().
1825 *
1826 * This scheme allows new atomic state updates to be prepared and
1827 * checked in parallel to the asynchronous completion of the previous
1828 * update. Which is important since compositors need to figure out the
1829 * composition of the next frame right after having submitted the
1830 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001831 *
1832 * NOTE: Commit work has multiple phases, first hardware commit, then
1833 * cleanup. We want them to overlap, hence need system_unbound_wq to
Kieran Binghama0689e32019-03-12 00:33:07 +00001834 * make sure work items don't artificially stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001835 */
1836
Chris Wilson08536952016-10-14 13:18:18 +01001837 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001838 if (nonblock)
1839 queue_work(system_unbound_wq, &state->commit_work);
1840 else
1841 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001842
1843 return 0;
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001844
1845err:
1846 drm_atomic_helper_cleanup_planes(dev, state);
1847 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +02001848}
1849EXPORT_SYMBOL(drm_atomic_helper_commit);
1850
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001851/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001852 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001853 *
Daniel Vetter0380c682019-12-04 11:00:11 +01001854 * Nonblocking atomic commits should use struct &drm_crtc_commit to sequence
1855 * different operations against each another. Locks, especially struct
1856 * &drm_modeset_lock, should not be held in worker threads or any other
1857 * asynchronous context used to commit the hardware state.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001858 *
Daniel Vetter0380c682019-12-04 11:00:11 +01001859 * drm_atomic_helper_commit() implements the recommended sequence for
1860 * nonblocking commits, using drm_atomic_helper_setup_commit() internally:
1861 *
1862 * 1. Run drm_atomic_helper_prepare_planes(). Since this can fail and we
1863 * need to propagate out of memory/VRAM errors to userspace, it must be called
Daniel Vettere8c833a2014-07-27 18:30:19 +02001864 * synchronously.
1865 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001866 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vetter0380c682019-12-04 11:00:11 +01001867 * might be affected by the new state update. This is handled by
1868 * drm_atomic_helper_setup_commit().
Daniel Vettere8c833a2014-07-27 18:30:19 +02001869 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001870 * Asynchronous workers need to have sufficient parallelism to be able to run
1871 * different atomic commits on different CRTCs in parallel. The simplest way to
Kieran Binghama0689e32019-03-12 00:33:07 +00001872 * achieve this is by running them on the &system_unbound_wq work queue. Note
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001873 * that drivers are not required to split up atomic commits and run an
1874 * individual commit in parallel - userspace is supposed to do that if it cares.
1875 * But it might be beneficial to do that for modesets, since those necessarily
1876 * must be done as one global operation, and enabling or disabling a CRTC can
1877 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001878 *
Daniel Vetter0380c682019-12-04 11:00:11 +01001879 * IMPORTANT: A &drm_atomic_state update for multiple CRTCs is sequenced
1880 * against all CRTCs therein. Therefore for atomic state updates which only flip
1881 * planes the driver must not get the struct &drm_crtc_state of unrelated CRTCs
1882 * in its atomic check code: This would prevent committing of atomic updates to
1883 * multiple CRTCs in parallel. In general, adding additional state structures
1884 * should be avoided as much as possible, because this reduces parallelism in
1885 * (nonblocking) commits, both due to locking and due to commit sequencing
1886 * requirements.
1887 *
Daniel Vettere8c833a2014-07-27 18:30:19 +02001888 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001889 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vetter0380c682019-12-04 11:00:11 +01001890 * locks means concurrent callers never see inconsistent state. Note that commit
1891 * workers do not hold any locks; their access is only coordinated through
1892 * ordering. If workers would access state only through the pointers in the
1893 * free-standing state objects (currently not the case for any driver) then even
1894 * multiple pending commits could be in-flight at the same time.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001895 *
1896 * 4. Schedule a work item to do all subsequent steps, using the split-out
1897 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1898 * then cleaning up the framebuffers after the old framebuffer is no longer
Daniel Vetter0380c682019-12-04 11:00:11 +01001899 * being displayed. The scheduled work should synchronize against other workers
1900 * using the &drm_crtc_commit infrastructure as needed. See
1901 * drm_atomic_helper_setup_commit() for more details.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001902 */
1903
Daniel Vettera095caa2016-06-08 17:15:36 +02001904static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1905{
1906 struct drm_crtc_commit *commit, *stall_commit = NULL;
1907 bool completed = true;
1908 int i;
1909 long ret = 0;
1910
1911 spin_lock(&crtc->commit_lock);
1912 i = 0;
1913 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1914 if (i == 0) {
1915 completed = try_wait_for_completion(&commit->flip_done);
1916 /* Userspace is not allowed to get ahead of the previous
1917 * commit with nonblocking ones. */
1918 if (!completed && nonblock) {
1919 spin_unlock(&crtc->commit_lock);
1920 return -EBUSY;
1921 }
1922 } else if (i == 1) {
Maarten Lankhorstf46640b2017-09-04 12:48:36 +02001923 stall_commit = drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001924 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001925 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001926
1927 i++;
1928 }
1929 spin_unlock(&crtc->commit_lock);
1930
1931 if (!stall_commit)
1932 return 0;
1933
1934 /* We don't want to let commits get ahead of cleanup work too much,
1935 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1936 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001937 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001938 10*HZ);
1939 if (ret == 0)
1940 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1941 crtc->base.id, crtc->name);
1942
1943 drm_crtc_commit_put(stall_commit);
1944
1945 return ret < 0 ? ret : 0;
1946}
1947
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001948static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001949{
1950 struct drm_crtc_commit *commit = container_of(completion,
1951 typeof(*commit),
1952 flip_done);
1953
1954 drm_crtc_commit_put(commit);
1955}
1956
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001957static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1958{
1959 init_completion(&commit->flip_done);
1960 init_completion(&commit->hw_done);
1961 init_completion(&commit->cleanup_done);
1962 INIT_LIST_HEAD(&commit->commit_entry);
1963 kref_init(&commit->ref);
1964 commit->crtc = crtc;
1965}
1966
1967static struct drm_crtc_commit *
1968crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1969{
1970 if (crtc) {
1971 struct drm_crtc_state *new_crtc_state;
1972
1973 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1974
1975 return new_crtc_state->commit;
1976 }
1977
1978 if (!state->fake_commit) {
1979 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1980 if (!state->fake_commit)
1981 return NULL;
1982
1983 init_commit(state->fake_commit, NULL);
1984 }
1985
1986 return state->fake_commit;
1987}
1988
Daniel Vettera095caa2016-06-08 17:15:36 +02001989/**
1990 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1991 * @state: new modeset state to be committed
1992 * @nonblock: whether nonblocking behavior is requested.
1993 *
1994 * This function prepares @state to be used by the atomic helper's support for
1995 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001996 * should always call this function from their
1997 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001998 *
1999 * To be able to use this support drivers need to use a few more helper
2000 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
2001 * actually committing the hardware state, and for nonblocking commits this call
2002 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
Matt Roper1e55a532019-02-01 17:23:26 -08002003 * and its stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002004 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02002005 *
2006 * Completion of the hardware commit step must be signalled using
2007 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
2008 * to read or change any permanent software or hardware modeset state. The only
2009 * exception is state protected by other means than &drm_modeset_lock locks.
2010 * Only the free standing @state with pointers to the old state structures can
2011 * be inspected, e.g. to clean up old buffers using
2012 * drm_atomic_helper_cleanup_planes().
2013 *
2014 * At the very end, before cleaning up @state drivers must call
2015 * drm_atomic_helper_commit_cleanup_done().
2016 *
2017 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
Thierry Reding9ac07812017-10-12 16:06:16 +02002018 * complete and easy-to-use default implementation of the atomic_commit() hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02002019 *
2020 * The tracking of asynchronously executed and still pending commits is done
2021 * using the core structure &drm_crtc_commit.
2022 *
2023 * By default there's no need to clean up resources allocated by this function
2024 * explicitly: drm_atomic_state_default_clear() will take care of that
2025 * automatically.
2026 *
2027 * Returns:
2028 *
2029 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
2030 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
2031 */
2032int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
2033 bool nonblock)
2034{
2035 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002036 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002037 struct drm_connector *conn;
2038 struct drm_connector_state *old_conn_state, *new_conn_state;
2039 struct drm_plane *plane;
2040 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002041 struct drm_crtc_commit *commit;
2042 int i, ret;
2043
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002044 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002045 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
2046 if (!commit)
2047 return -ENOMEM;
2048
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002049 init_commit(commit, crtc);
Daniel Vettera095caa2016-06-08 17:15:36 +02002050
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002051 new_crtc_state->commit = commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002052
2053 ret = stall_checks(crtc, nonblock);
2054 if (ret)
2055 return ret;
2056
2057 /* Drivers only send out events when at least either current or
2058 * new CRTC state is active. Complete right away if everything
2059 * stays off. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002060 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002061 complete_all(&commit->flip_done);
2062 continue;
2063 }
2064
2065 /* Legacy cursor updates are fully unsynced. */
2066 if (state->legacy_cursor_update) {
2067 complete_all(&commit->flip_done);
2068 continue;
2069 }
2070
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002071 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002072 commit->event = kzalloc(sizeof(*commit->event),
2073 GFP_KERNEL);
2074 if (!commit->event)
2075 return -ENOMEM;
2076
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002077 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02002078 }
2079
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002080 new_crtc_state->event->base.completion = &commit->flip_done;
2081 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01002082 drm_crtc_commit_get(commit);
Leo (Sunpeng) Li1c6ceee2018-01-17 12:51:08 +01002083
2084 commit->abort_completion = true;
Leo Li4364bcb2018-10-15 09:46:40 -04002085
2086 state->crtcs[i].commit = commit;
2087 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02002088 }
2089
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002090 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002091 /* Userspace is not allowed to get ahead of the previous
2092 * commit with nonblocking ones. */
2093 if (nonblock && old_conn_state->commit &&
2094 !try_wait_for_completion(&old_conn_state->commit->flip_done))
2095 return -EBUSY;
2096
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01002097 /* Always track connectors explicitly for e.g. link retraining. */
2098 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002099 if (!commit)
2100 return -ENOMEM;
2101
2102 new_conn_state->commit = drm_crtc_commit_get(commit);
2103 }
2104
2105 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002106 /* Userspace is not allowed to get ahead of the previous
2107 * commit with nonblocking ones. */
2108 if (nonblock && old_plane_state->commit &&
2109 !try_wait_for_completion(&old_plane_state->commit->flip_done))
2110 return -EBUSY;
2111
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01002112 /* Always track planes explicitly for async pageflip support. */
Maarten Lankhorst4edd6082017-10-17 07:20:47 +02002113 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002114 if (!commit)
2115 return -ENOMEM;
2116
2117 new_plane_state->commit = drm_crtc_commit_get(commit);
2118 }
2119
Daniel Vettera095caa2016-06-08 17:15:36 +02002120 return 0;
2121}
2122EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2123
Daniel Vettera095caa2016-06-08 17:15:36 +02002124/**
2125 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002126 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002127 *
2128 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002129 * @old_state to both be committed to the hardware (as signalled by
Daniel Vetter0380c682019-12-04 11:00:11 +01002130 * drm_atomic_helper_commit_hw_done()) and executed by the hardware (as signalled
Thierry Reding277b09c2017-10-12 16:08:57 +02002131 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02002132 *
2133 * This is part of the atomic helper support for nonblocking commits, see
2134 * drm_atomic_helper_setup_commit() for an overview.
2135 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002136void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002137{
2138 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002139 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002140 struct drm_plane *plane;
2141 struct drm_plane_state *old_plane_state;
2142 struct drm_connector *conn;
2143 struct drm_connector_state *old_conn_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002144 struct drm_crtc_commit *commit;
2145 int i;
2146 long ret;
2147
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002148 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2149 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002150
2151 if (!commit)
2152 continue;
2153
2154 ret = wait_for_completion_timeout(&commit->hw_done,
2155 10*HZ);
2156 if (ret == 0)
2157 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2158 crtc->base.id, crtc->name);
2159
2160 /* Currently no support for overwriting flips, hence
2161 * stall for previous one to execute completely. */
2162 ret = wait_for_completion_timeout(&commit->flip_done,
2163 10*HZ);
2164 if (ret == 0)
2165 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2166 crtc->base.id, crtc->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002167 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002168
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002169 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2170 commit = old_conn_state->commit;
2171
2172 if (!commit)
2173 continue;
2174
2175 ret = wait_for_completion_timeout(&commit->hw_done,
2176 10*HZ);
2177 if (ret == 0)
2178 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2179 conn->base.id, conn->name);
2180
2181 /* Currently no support for overwriting flips, hence
2182 * stall for previous one to execute completely. */
2183 ret = wait_for_completion_timeout(&commit->flip_done,
2184 10*HZ);
2185 if (ret == 0)
2186 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2187 conn->base.id, conn->name);
2188 }
2189
2190 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2191 commit = old_plane_state->commit;
2192
2193 if (!commit)
2194 continue;
2195
2196 ret = wait_for_completion_timeout(&commit->hw_done,
2197 10*HZ);
2198 if (ret == 0)
2199 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2200 plane->base.id, plane->name);
2201
2202 /* Currently no support for overwriting flips, hence
2203 * stall for previous one to execute completely. */
2204 ret = wait_for_completion_timeout(&commit->flip_done,
2205 10*HZ);
2206 if (ret == 0)
2207 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2208 plane->base.id, plane->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002209 }
2210}
2211EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2212
2213/**
Boris Brezillonb25c60a2018-07-03 09:50:19 +02002214 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2215 * @old_state: atomic state object with old state structures
2216 *
Thierry Redingdbe2d2b2019-12-06 14:53:35 +01002217 * This function walks all CRTCs and fakes VBLANK events on those with
Boris Brezillonb25c60a2018-07-03 09:50:19 +02002218 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2219 * The primary use of this function is writeback connectors working in oneshot
2220 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2221 * when a job is queued, and any change to the pipeline that does not touch the
2222 * connector is leading to timeouts when calling
2223 * drm_atomic_helper_wait_for_vblanks() or
Thomas Zimmermann7beb6912020-01-29 13:05:17 +01002224 * drm_atomic_helper_wait_for_flip_done(). In addition to writeback
2225 * connectors, this function can also fake VBLANK events for CRTCs without
2226 * VBLANK interrupt.
Boris Brezillonb25c60a2018-07-03 09:50:19 +02002227 *
2228 * This is part of the atomic helper support for nonblocking commits, see
2229 * drm_atomic_helper_setup_commit() for an overview.
2230 */
2231void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2232{
2233 struct drm_crtc_state *new_crtc_state;
2234 struct drm_crtc *crtc;
2235 int i;
2236
2237 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2238 unsigned long flags;
2239
2240 if (!new_crtc_state->no_vblank)
2241 continue;
2242
2243 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2244 if (new_crtc_state->event) {
2245 drm_crtc_send_vblank_event(crtc,
2246 new_crtc_state->event);
2247 new_crtc_state->event = NULL;
2248 }
2249 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2250 }
2251}
2252EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2253
2254/**
Daniel Vettera095caa2016-06-08 17:15:36 +02002255 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002256 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002257 *
2258 * This function is used to signal completion of the hardware commit step. After
2259 * this step the driver is not allowed to read or change any permanent software
2260 * or hardware modeset state. The only exception is state protected by other
2261 * means than &drm_modeset_lock locks.
2262 *
2263 * Drivers should try to postpone any expensive or delayed cleanup work after
2264 * this function is called.
2265 *
2266 * This is part of the atomic helper support for nonblocking commits, see
2267 * drm_atomic_helper_setup_commit() for an overview.
2268 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002269void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002270{
2271 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002272 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002273 struct drm_crtc_commit *commit;
2274 int i;
2275
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002276 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2277 commit = new_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002278 if (!commit)
2279 continue;
2280
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002281 /*
2282 * copy new_crtc_state->commit to old_crtc_state->commit,
2283 * it's unsafe to touch new_crtc_state after hw_done,
2284 * but we still need to do so in cleanup_done().
2285 */
2286 if (old_crtc_state->commit)
2287 drm_crtc_commit_put(old_crtc_state->commit);
2288
2289 old_crtc_state->commit = drm_crtc_commit_get(commit);
2290
Daniel Vettera095caa2016-06-08 17:15:36 +02002291 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002292 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02002293 complete_all(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002294 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002295
2296 if (old_state->fake_commit) {
2297 complete_all(&old_state->fake_commit->hw_done);
2298 complete_all(&old_state->fake_commit->flip_done);
2299 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002300}
2301EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2302
2303/**
2304 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002305 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002306 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002307 * This signals completion of the atomic update @old_state, including any
2308 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01002309 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02002310 *
2311 * This is part of the atomic helper support for nonblocking commits, see
2312 * drm_atomic_helper_setup_commit() for an overview.
2313 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002314void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002315{
2316 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002317 struct drm_crtc_state *old_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002318 struct drm_crtc_commit *commit;
2319 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02002320
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002321 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2322 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002323 if (WARN_ON(!commit))
2324 continue;
2325
Daniel Vettera095caa2016-06-08 17:15:36 +02002326 complete_all(&commit->cleanup_done);
2327 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2328
Daniel Vetter7141fd32017-06-21 11:16:27 +02002329 spin_lock(&crtc->commit_lock);
Daniel Vettera095caa2016-06-08 17:15:36 +02002330 list_del(&commit->commit_entry);
2331 spin_unlock(&crtc->commit_lock);
2332 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002333
Ville Syrjälä10a599f2018-11-22 16:34:12 +02002334 if (old_state->fake_commit) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002335 complete_all(&old_state->fake_commit->cleanup_done);
Ville Syrjälä10a599f2018-11-22 16:34:12 +02002336 WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2337 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002338}
2339EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2340
Daniel Vettere8c833a2014-07-27 18:30:19 +02002341/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002342 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002343 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002344 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002345 *
2346 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002347 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2348 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2349 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002350 *
2351 * Returns:
2352 * 0 on success, negative error code on failure.
2353 */
2354int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2355 struct drm_atomic_state *state)
2356{
Laurent Pinchart9d2230d2019-02-21 03:01:38 +02002357 struct drm_connector *connector;
2358 struct drm_connector_state *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002359 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002360 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002361 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002362
Laurent Pinchart9d2230d2019-02-21 03:01:38 +02002363 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2364 if (!new_conn_state->writeback_job)
2365 continue;
2366
2367 ret = drm_writeback_prepare_job(new_conn_state->writeback_job);
2368 if (ret < 0)
2369 return ret;
2370 }
2371
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002372 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002373 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002374
2375 funcs = plane->helper_private;
2376
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002377 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002378 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002379 if (ret)
2380 goto fail;
2381 }
2382 }
2383
2384 return 0;
2385
2386fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002387 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002388 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002389
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002390 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002391 continue;
2392
2393 funcs = plane->helper_private;
2394
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002395 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002396 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002397 }
2398
2399 return ret;
2400}
2401EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2402
Ville Syrjälä7135ac52016-09-19 16:33:42 +03002403static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002404{
2405 return state->crtc && state->crtc->state->active;
2406}
2407
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002408/**
2409 * drm_atomic_helper_commit_planes - commit plane state
2410 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002411 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08002412 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002413 *
2414 * This function commits the new plane state using the plane and atomic helper
Thierry Reding42240c92019-12-06 14:53:36 +01002415 * functions for planes and CRTCs. It assumes that the atomic state has already
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002416 * been pushed into the relevant object state pointers, since this step can no
2417 * longer fail.
2418 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002419 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002420 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002421 *
2422 * Note that this function does all plane updates across all CRTCs in one step.
2423 * If the hardware can't support this approach look at
2424 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002425 *
2426 * Plane parameters can be updated by applications while the associated CRTC is
2427 * disabled. The DRM/KMS core will store the parameters in the plane state,
2428 * which will be available to the driver when the CRTC is turned on. As a result
2429 * most drivers don't need to be immediately notified of plane updates for a
2430 * disabled CRTC.
2431 *
Liu Ying2b58e982016-08-29 17:12:03 +08002432 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2433 * @flags in order not to receive plane update notifications related to a
2434 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002435 * driver code when the driver and/or hardware can't or just don't need to deal
2436 * with updates on disabled CRTCs, for example when supporting runtime PM.
2437 *
Liu Ying2b58e982016-08-29 17:12:03 +08002438 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2439 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002440 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2441 * call for a plane if the CRTC of the old plane state needs a modesetting
2442 * operation. Of course, the drivers need to disable the planes in their CRTC
2443 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08002444 *
2445 * The drm_atomic_helper_commit() default implementation doesn't set the
2446 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2447 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002448 */
2449void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002450 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08002451 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002452{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002453 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002454 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002455 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002456 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002457 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08002458 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2459 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002460
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002461 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002462 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002463
2464 funcs = crtc->helper_private;
2465
2466 if (!funcs || !funcs->atomic_begin)
2467 continue;
2468
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002469 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002470 continue;
2471
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002472 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002473 }
2474
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002475 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002476 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002477 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002478
2479 funcs = plane->helper_private;
2480
Thierry Reding3cad4b62014-11-25 13:05:12 +01002481 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002482 continue;
2483
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01002484 disabling = drm_atomic_plane_disabling(old_plane_state,
2485 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002486
2487 if (active_only) {
2488 /*
2489 * Skip planes related to inactive CRTCs. If the plane
2490 * is enabled use the state of the current CRTC. If the
2491 * plane is being disabled use the state of the old
2492 * CRTC to avoid skipping planes being disabled on an
2493 * active CRTC.
2494 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002495 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002496 continue;
2497 if (disabling && !plane_crtc_active(old_plane_state))
2498 continue;
2499 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002500
Thierry Reding407b8bd2014-11-20 12:05:50 +01002501 /*
2502 * Special-case disabling the plane if drivers support it.
2503 */
Liu Ying2b58e982016-08-29 17:12:03 +08002504 if (disabling && funcs->atomic_disable) {
2505 struct drm_crtc_state *crtc_state;
2506
2507 crtc_state = old_plane_state->crtc->state;
2508
2509 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2510 no_disable)
2511 continue;
2512
Thierry Reding407b8bd2014-11-20 12:05:50 +01002513 funcs->atomic_disable(plane, old_plane_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002514 } else if (new_plane_state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01002515 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08002516 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002517 }
2518
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002519 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002520 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002521
2522 funcs = crtc->helper_private;
2523
2524 if (!funcs || !funcs->atomic_flush)
2525 continue;
2526
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002527 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002528 continue;
2529
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002530 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002531 }
2532}
2533EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2534
2535/**
Thierry Reding42240c92019-12-06 14:53:36 +01002536 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a CRTC
2537 * @old_crtc_state: atomic state object with the old CRTC state
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002538 *
2539 * This function commits the new plane state using the plane and atomic helper
Thierry Reding42240c92019-12-06 14:53:36 +01002540 * functions for planes on the specific CRTC. It assumes that the atomic state
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002541 * has already been pushed into the relevant object state pointers, since this
2542 * step can no longer fail.
2543 *
Thierry Reding42240c92019-12-06 14:53:36 +01002544 * This function is useful when plane updates should be done CRTC-by-CRTC
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002545 * instead of one global step like drm_atomic_helper_commit_planes() does.
2546 *
2547 * This function can only be savely used when planes are not allowed to move
2548 * between different CRTCs because this function doesn't handle inter-CRTC
2549 * depencies. Callers need to ensure that either no such depencies exist,
2550 * resolve them through ordering of commit calls or through some other means.
2551 */
2552void
2553drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2554{
2555 const struct drm_crtc_helper_funcs *crtc_funcs;
2556 struct drm_crtc *crtc = old_crtc_state->crtc;
2557 struct drm_atomic_state *old_state = old_crtc_state->state;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002558 struct drm_crtc_state *new_crtc_state =
2559 drm_atomic_get_new_crtc_state(old_state, crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002560 struct drm_plane *plane;
2561 unsigned plane_mask;
2562
2563 plane_mask = old_crtc_state->plane_mask;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002564 plane_mask |= new_crtc_state->plane_mask;
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002565
2566 crtc_funcs = crtc->helper_private;
2567 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002568 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002569
2570 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2571 struct drm_plane_state *old_plane_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01002572 drm_atomic_get_old_plane_state(old_state, plane);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002573 struct drm_plane_state *new_plane_state =
2574 drm_atomic_get_new_plane_state(old_state, plane);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002575 const struct drm_plane_helper_funcs *plane_funcs;
2576
2577 plane_funcs = plane->helper_private;
2578
2579 if (!old_plane_state || !plane_funcs)
2580 continue;
2581
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002582 WARN_ON(new_plane_state->crtc &&
2583 new_plane_state->crtc != crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002584
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002585 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002586 plane_funcs->atomic_disable)
2587 plane_funcs->atomic_disable(plane, old_plane_state);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002588 else if (new_plane_state->crtc ||
2589 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002590 plane_funcs->atomic_update(plane, old_plane_state);
2591 }
2592
2593 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002594 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002595}
2596EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2597
2598/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02002599 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08002600 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02002601 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2602 *
2603 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08002604 * used for instance in the CRTC helper atomic_disable callback to disable
2605 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002606 *
2607 * If the atomic-parameter is set the function calls the CRTC's
2608 * atomic_begin hook before and atomic_flush hook after disabling the
2609 * planes.
2610 *
2611 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002612 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002613 */
Liu Ying28500292016-08-26 15:30:39 +08002614void
2615drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2616 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002617{
Liu Ying28500292016-08-26 15:30:39 +08002618 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02002619 const struct drm_crtc_helper_funcs *crtc_funcs =
2620 crtc->helper_private;
2621 struct drm_plane *plane;
2622
2623 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2624 crtc_funcs->atomic_begin(crtc, NULL);
2625
Liu Ying28500292016-08-26 15:30:39 +08002626 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02002627 const struct drm_plane_helper_funcs *plane_funcs =
2628 plane->helper_private;
2629
Liu Ying28500292016-08-26 15:30:39 +08002630 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002631 continue;
2632
2633 WARN_ON(!plane_funcs->atomic_disable);
2634 if (plane_funcs->atomic_disable)
2635 plane_funcs->atomic_disable(plane, NULL);
2636 }
2637
2638 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2639 crtc_funcs->atomic_flush(crtc, NULL);
2640}
2641EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2642
2643/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002644 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2645 * @dev: DRM device
2646 * @old_state: atomic state object with old state structures
2647 *
2648 * This function cleans up plane state, specifically framebuffers, from the old
2649 * configuration. Hence the old configuration must be perserved in @old_state to
2650 * be able to call this function.
2651 *
2652 * This function must also be called on the new state when the atomic update
2653 * fails at any point after calling drm_atomic_helper_prepare_planes().
2654 */
2655void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2656 struct drm_atomic_state *old_state)
2657{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002658 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002659 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002660 int i;
2661
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002662 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002663 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002664 struct drm_plane_state *plane_state;
2665
2666 /*
2667 * This might be called before swapping when commit is aborted,
2668 * in which case we have to cleanup the new state.
2669 */
2670 if (old_plane_state == plane->state)
2671 plane_state = new_plane_state;
2672 else
2673 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002674
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002675 funcs = plane->helper_private;
2676
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002677 if (funcs->cleanup_fb)
2678 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002679 }
2680}
2681EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2682
2683/**
2684 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002685 * @state: atomic state
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002686 * @stall: stall for preceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002687 *
2688 * This function stores the atomic state into the current state pointers in all
2689 * driver objects. It should be called after all failing steps have been done
2690 * and succeeded, but before the actual hardware state is committed.
2691 *
2692 * For cleanup and error recovery the current state for all changed objects will
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002693 * be swapped into @state.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002694 *
2695 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2696 *
2697 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2698 *
2699 * 2. Do any other steps that might fail.
2700 *
2701 * 3. Put the staged state into the current state pointers with this function.
2702 *
2703 * 4. Actually commit the hardware state.
2704 *
Daniel Vetter26196f72015-08-25 16:26:03 +02002705 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002706 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02002707 *
2708 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002709 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2710 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02002711 * don't pass the right state structures to the callbacks.
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002712 *
2713 * Returns:
2714 *
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002715 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2716 * waiting for the previous commits has been interrupted.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002717 */
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002718int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
Daniel Vetter5e84c262016-06-10 00:06:32 +02002719 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002720{
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002721 int i, ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002722 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002723 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002724 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002725 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002726 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002727 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002728 struct drm_crtc_commit *commit;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002729 struct drm_private_obj *obj;
2730 struct drm_private_state *old_obj_state, *new_obj_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002731
2732 if (stall) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002733 /*
2734 * We have to stall for hw_done here before
2735 * drm_atomic_helper_wait_for_dependencies() because flip
2736 * depth > 1 is not yet supported by all drivers. As long as
2737 * obj->state is directly dereferenced anywhere in the drivers
2738 * atomic_commit_tail function, then it's unsafe to swap state
2739 * before drm_atomic_helper_commit_hw_done() is called.
2740 */
2741
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002742 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2743 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002744
2745 if (!commit)
2746 continue;
2747
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002748 ret = wait_for_completion_interruptible(&commit->hw_done);
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002749 if (ret)
2750 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002751 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002752
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002753 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2754 commit = old_conn_state->commit;
2755
2756 if (!commit)
2757 continue;
2758
2759 ret = wait_for_completion_interruptible(&commit->hw_done);
2760 if (ret)
2761 return ret;
2762 }
2763
2764 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2765 commit = old_plane_state->commit;
2766
2767 if (!commit)
2768 continue;
2769
2770 ret = wait_for_completion_interruptible(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002771 if (ret)
2772 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002773 }
2774 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002775
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002776 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002777 WARN_ON(connector->state != old_conn_state);
2778
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002779 old_conn_state->state = state;
2780 new_conn_state->state = NULL;
2781
2782 state->connectors[i].state = old_conn_state;
2783 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002784 }
2785
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002786 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002787 WARN_ON(crtc->state != old_crtc_state);
2788
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002789 old_crtc_state->state = state;
2790 new_crtc_state->state = NULL;
2791
2792 state->crtcs[i].state = old_crtc_state;
2793 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002794
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002795 if (new_crtc_state->commit) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002796 spin_lock(&crtc->commit_lock);
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002797 list_add(&new_crtc_state->commit->commit_entry,
Daniel Vettera095caa2016-06-08 17:15:36 +02002798 &crtc->commit_list);
2799 spin_unlock(&crtc->commit_lock);
2800
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002801 new_crtc_state->commit->event = NULL;
Daniel Vettera095caa2016-06-08 17:15:36 +02002802 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002803 }
2804
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002805 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002806 WARN_ON(plane->state != old_plane_state);
2807
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002808 old_plane_state->state = state;
2809 new_plane_state->state = NULL;
2810
2811 state->planes[i].state = old_plane_state;
2812 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002813 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07002814
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002815 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2816 WARN_ON(obj->state != old_obj_state);
2817
2818 old_obj_state->state = state;
2819 new_obj_state->state = NULL;
2820
2821 state->private_objs[i].state = old_obj_state;
2822 obj->state = new_obj_state;
2823 }
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002824
2825 return 0;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002826}
2827EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002828
2829/**
2830 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2831 * @plane: plane object to update
2832 * @crtc: owning CRTC of owning plane
2833 * @fb: framebuffer to flip onto plane
Thierry Reding42240c92019-12-06 14:53:36 +01002834 * @crtc_x: x offset of primary plane on @crtc
2835 * @crtc_y: y offset of primary plane on @crtc
2836 * @crtc_w: width of primary plane rectangle on @crtc
2837 * @crtc_h: height of primary plane rectangle on @crtc
Daniel Vetter042652e2014-07-27 13:46:52 +02002838 * @src_x: x offset of @fb for panning
2839 * @src_y: y offset of @fb for panning
2840 * @src_w: width of source rectangle in @fb
2841 * @src_h: height of source rectangle in @fb
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002842 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002843 *
2844 * Provides a default plane update handler using the atomic driver interface.
2845 *
2846 * RETURNS:
2847 * Zero on success, error code on failure
2848 */
2849int drm_atomic_helper_update_plane(struct drm_plane *plane,
2850 struct drm_crtc *crtc,
2851 struct drm_framebuffer *fb,
2852 int crtc_x, int crtc_y,
2853 unsigned int crtc_w, unsigned int crtc_h,
2854 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002855 uint32_t src_w, uint32_t src_h,
2856 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002857{
2858 struct drm_atomic_state *state;
2859 struct drm_plane_state *plane_state;
2860 int ret = 0;
2861
2862 state = drm_atomic_state_alloc(plane->dev);
2863 if (!state)
2864 return -ENOMEM;
2865
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002866 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002867 plane_state = drm_atomic_get_plane_state(state, plane);
2868 if (IS_ERR(plane_state)) {
2869 ret = PTR_ERR(plane_state);
2870 goto fail;
2871 }
2872
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002873 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002874 if (ret != 0)
2875 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002876 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002877 plane_state->crtc_x = crtc_x;
2878 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002879 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002880 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002881 plane_state->src_x = src_x;
2882 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002883 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002884 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002885
Daniel Vetter3671c582015-05-04 15:40:52 +02002886 if (plane == crtc->cursor)
2887 state->legacy_cursor_update = true;
2888
Daniel Vetter042652e2014-07-27 13:46:52 +02002889 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002890fail:
Chris Wilson08536952016-10-14 13:18:18 +01002891 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002892 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002893}
2894EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2895
2896/**
2897 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2898 * @plane: plane to disable
Daniel Vetter19315292017-03-22 22:50:43 +01002899 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002900 *
2901 * Provides a default plane disable handler using the atomic driver interface.
2902 *
2903 * RETURNS:
2904 * Zero on success, error code on failure
2905 */
Daniel Vetter19315292017-03-22 22:50:43 +01002906int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2907 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002908{
2909 struct drm_atomic_state *state;
2910 struct drm_plane_state *plane_state;
2911 int ret = 0;
2912
2913 state = drm_atomic_state_alloc(plane->dev);
2914 if (!state)
2915 return -ENOMEM;
2916
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002917 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002918 plane_state = drm_atomic_get_plane_state(state, plane);
2919 if (IS_ERR(plane_state)) {
2920 ret = PTR_ERR(plane_state);
2921 goto fail;
2922 }
2923
Ville Syrjäläa36c0272018-03-22 17:22:58 +02002924 if (plane_state->crtc && plane_state->crtc->cursor == plane)
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002925 plane_state->state->legacy_cursor_update = true;
2926
Rob Clarkbbb1e522015-08-25 15:35:58 -04002927 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002928 if (ret != 0)
2929 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002930
Daniel Vetter042652e2014-07-27 13:46:52 +02002931 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002932fail:
Chris Wilson08536952016-10-14 13:18:18 +01002933 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002934 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002935}
2936EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2937
Daniel Vetter042652e2014-07-27 13:46:52 +02002938/**
2939 * drm_atomic_helper_set_config - set a new config from userspace
2940 * @set: mode set configuration
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002941 * @ctx: lock acquisition context
Daniel Vetter042652e2014-07-27 13:46:52 +02002942 *
Thierry Reding42240c92019-12-06 14:53:36 +01002943 * Provides a default CRTC set_config handler using the atomic driver interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002944 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002945 * NOTE: For backwards compatibility with old userspace this automatically
2946 * resets the "link-status" property to GOOD, to force any link
2947 * re-training. The SETCRTC ioctl does not define whether an update does
2948 * need a full modeset or just a plane update, hence we're allowed to do
Daniel Vetter97e14fb2018-07-09 10:40:08 +02002949 * that. See also drm_connector_set_link_status_property().
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002950 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002951 * Returns:
2952 * Returns 0 on success, negative errno numbers on failure.
2953 */
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002954int drm_atomic_helper_set_config(struct drm_mode_set *set,
2955 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002956{
2957 struct drm_atomic_state *state;
2958 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002959 int ret = 0;
2960
2961 state = drm_atomic_state_alloc(crtc->dev);
2962 if (!state)
2963 return -ENOMEM;
2964
Daniel Vetter38b64412017-03-22 22:50:58 +01002965 state->acquire_ctx = ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002966 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002967 if (ret != 0)
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002968 goto fail;
Daniel Stone819364d2015-05-26 14:36:48 +01002969
Maarten Lankhorst44596b82017-04-06 13:19:00 +02002970 ret = handle_conflicting_encoders(state, true);
2971 if (ret)
2972 return ret;
2973
Daniel Vetter042652e2014-07-27 13:46:52 +02002974 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002975
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002976fail:
Chris Wilson08536952016-10-14 13:18:18 +01002977 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002978 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002979}
2980EXPORT_SYMBOL(drm_atomic_helper_set_config);
2981
Sean Paul37406a62019-02-12 12:32:41 -05002982/**
2983 * drm_atomic_helper_disable_all - disable all currently active outputs
2984 * @dev: DRM device
2985 * @ctx: lock acquisition context
2986 *
2987 * Loops through all connectors, finding those that aren't turned off and then
2988 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2989 * that they are connected to.
2990 *
2991 * This is used for example in suspend/resume to disable all currently active
2992 * functions when suspending. If you just want to shut down everything at e.g.
2993 * driver unload, look at drm_atomic_helper_shutdown().
2994 *
2995 * Note that if callers haven't already acquired all modeset locks this might
2996 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2997 *
2998 * Returns:
2999 * 0 on success or a negative error code on failure.
3000 *
3001 * See also:
3002 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3003 * drm_atomic_helper_shutdown().
3004 */
3005int drm_atomic_helper_disable_all(struct drm_device *dev,
3006 struct drm_modeset_acquire_ctx *ctx)
Thierry Reding14942762015-12-02 17:50:04 +01003007{
3008 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003009 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01003010 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003011 struct drm_plane_state *plane_state;
3012 struct drm_plane *plane;
3013 struct drm_crtc_state *crtc_state;
3014 struct drm_crtc *crtc;
3015 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01003016
3017 state = drm_atomic_state_alloc(dev);
3018 if (!state)
3019 return -ENOMEM;
3020
3021 state->acquire_ctx = ctx;
3022
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003023 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01003024 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3025 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003026 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01003027 goto free;
3028 }
3029
3030 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003031
3032 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3033 if (ret < 0)
3034 goto free;
3035
3036 ret = drm_atomic_add_affected_planes(state, crtc);
3037 if (ret < 0)
3038 goto free;
3039
3040 ret = drm_atomic_add_affected_connectors(state, crtc);
3041 if (ret < 0)
3042 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01003043 }
3044
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02003045 for_each_new_connector_in_state(state, conn, conn_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003046 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3047 if (ret < 0)
3048 goto free;
3049 }
3050
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02003051 for_each_new_plane_in_state(state, plane, plane_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003052 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3053 if (ret < 0)
3054 goto free;
3055
3056 drm_atomic_set_fb_for_plane(plane_state, NULL);
3057 }
3058
3059 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01003060free:
Chris Wilson08536952016-10-14 13:18:18 +01003061 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003062 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01003063}
3064EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3065
3066/**
Daniel Vetter18dddad2017-03-21 17:41:49 +01003067 * drm_atomic_helper_shutdown - shutdown all CRTC
3068 * @dev: DRM device
3069 *
3070 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3071 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3072 * that also takes a snapshot of the modeset state to be restored on resume.
3073 *
3074 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3075 * and it is the atomic version of drm_crtc_force_disable_all().
3076 */
3077void drm_atomic_helper_shutdown(struct drm_device *dev)
3078{
3079 struct drm_modeset_acquire_ctx ctx;
3080 int ret;
3081
Sean Paulb7ea04d2018-11-29 10:04:17 -05003082 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003083
Sean Paul37406a62019-02-12 12:32:41 -05003084 ret = drm_atomic_helper_disable_all(dev, &ctx);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003085 if (ret)
3086 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3087
Sean Paulb7ea04d2018-11-29 10:04:17 -05003088 DRM_MODESET_LOCK_ALL_END(ctx, ret);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003089}
3090EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3091
3092/**
Daniel Vetter1d8224e2018-11-28 11:07:28 +01003093 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3094 * @dev: DRM device
3095 * @ctx: lock acquisition context
3096 *
3097 * Makes a copy of the current atomic state by looping over all objects and
3098 * duplicating their respective states. This is used for example by suspend/
3099 * resume support code to save the state prior to suspend such that it can
3100 * be restored upon resume.
3101 *
3102 * Note that this treats atomic state as persistent between save and restore.
3103 * Drivers must make sure that this is possible and won't result in confusion
3104 * or erroneous behaviour.
3105 *
3106 * Note that if callers haven't already acquired all modeset locks this might
3107 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3108 *
3109 * Returns:
3110 * A pointer to the copy of the atomic state object on success or an
3111 * ERR_PTR()-encoded error code on failure.
3112 *
3113 * See also:
3114 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3115 */
3116struct drm_atomic_state *
3117drm_atomic_helper_duplicate_state(struct drm_device *dev,
3118 struct drm_modeset_acquire_ctx *ctx)
3119{
3120 struct drm_atomic_state *state;
3121 struct drm_connector *conn;
3122 struct drm_connector_list_iter conn_iter;
3123 struct drm_plane *plane;
3124 struct drm_crtc *crtc;
3125 int err = 0;
3126
3127 state = drm_atomic_state_alloc(dev);
3128 if (!state)
3129 return ERR_PTR(-ENOMEM);
3130
3131 state->acquire_ctx = ctx;
Lyude Paul022deba2019-02-01 19:20:03 -05003132 state->duplicated = true;
Daniel Vetter1d8224e2018-11-28 11:07:28 +01003133
3134 drm_for_each_crtc(crtc, dev) {
3135 struct drm_crtc_state *crtc_state;
3136
3137 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3138 if (IS_ERR(crtc_state)) {
3139 err = PTR_ERR(crtc_state);
3140 goto free;
3141 }
3142 }
3143
3144 drm_for_each_plane(plane, dev) {
3145 struct drm_plane_state *plane_state;
3146
3147 plane_state = drm_atomic_get_plane_state(state, plane);
3148 if (IS_ERR(plane_state)) {
3149 err = PTR_ERR(plane_state);
3150 goto free;
3151 }
3152 }
3153
3154 drm_connector_list_iter_begin(dev, &conn_iter);
3155 drm_for_each_connector_iter(conn, &conn_iter) {
3156 struct drm_connector_state *conn_state;
3157
3158 conn_state = drm_atomic_get_connector_state(state, conn);
3159 if (IS_ERR(conn_state)) {
3160 err = PTR_ERR(conn_state);
3161 drm_connector_list_iter_end(&conn_iter);
3162 goto free;
3163 }
3164 }
3165 drm_connector_list_iter_end(&conn_iter);
3166
3167 /* clear the acquire context so that it isn't accidentally reused */
3168 state->acquire_ctx = NULL;
3169
3170free:
3171 if (err < 0) {
3172 drm_atomic_state_put(state);
3173 state = ERR_PTR(err);
3174 }
3175
3176 return state;
3177}
3178EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3179
3180/**
Thierry Reding14942762015-12-02 17:50:04 +01003181 * drm_atomic_helper_suspend - subsystem-level suspend helper
3182 * @dev: DRM device
3183 *
3184 * Duplicates the current atomic state, disables all active outputs and then
3185 * returns a pointer to the original atomic state to the caller. Drivers can
3186 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3187 * restore the output configuration that was active at the time the system
3188 * entered suspend.
3189 *
3190 * Note that it is potentially unsafe to use this. The atomic state object
3191 * returned by this function is assumed to be persistent. Drivers must ensure
3192 * that this holds true. Before calling this function, drivers must make sure
3193 * to suspend fbdev emulation so that nothing can be using the device.
3194 *
3195 * Returns:
3196 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3197 * encoded error code on failure. Drivers should store the returned atomic
3198 * state object and pass it to the drm_atomic_helper_resume() helper upon
3199 * resume.
3200 *
3201 * See also:
3202 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003203 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01003204 */
3205struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3206{
3207 struct drm_modeset_acquire_ctx ctx;
3208 struct drm_atomic_state *state;
3209 int err;
3210
Sean Paul615aa3d2018-11-29 15:36:48 -05003211 /* This can never be returned, but it makes the compiler happy */
3212 state = ERR_PTR(-EINVAL);
Thierry Reding14942762015-12-02 17:50:04 +01003213
Sean Paulb7ea04d2018-11-29 10:04:17 -05003214 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
Thierry Reding14942762015-12-02 17:50:04 +01003215
3216 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3217 if (IS_ERR(state))
3218 goto unlock;
3219
3220 err = drm_atomic_helper_disable_all(dev, &ctx);
3221 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003222 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01003223 state = ERR_PTR(err);
3224 goto unlock;
3225 }
3226
3227unlock:
Sean Paulb7ea04d2018-11-29 10:04:17 -05003228 DRM_MODESET_LOCK_ALL_END(ctx, err);
3229 if (err)
3230 return ERR_PTR(err);
Thierry Reding14942762015-12-02 17:50:04 +01003231
Thierry Reding14942762015-12-02 17:50:04 +01003232 return state;
3233}
3234EXPORT_SYMBOL(drm_atomic_helper_suspend);
3235
3236/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003237 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3238 * @state: duplicated atomic state to commit
3239 * @ctx: pointer to acquire_ctx to use for commit.
3240 *
3241 * The state returned by drm_atomic_helper_duplicate_state() and
3242 * drm_atomic_helper_suspend() is partially invalid, and needs to
3243 * be fixed up before commit.
3244 *
3245 * Returns:
3246 * 0 on success or a negative error code on failure.
3247 *
3248 * See also:
3249 * drm_atomic_helper_suspend()
3250 */
3251int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3252 struct drm_modeset_acquire_ctx *ctx)
3253{
Sean Paulaa394b02018-11-29 10:04:14 -05003254 int i, ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003255 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003256 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003257 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003258 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003259 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003260 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003261
3262 state->acquire_ctx = ctx;
3263
Ville Syrjäläe00fb852018-05-25 21:50:45 +03003264 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003265 state->planes[i].old_state = plane->state;
3266
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003267 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003268 state->crtcs[i].old_state = crtc->state;
3269
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003270 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003271 state->connectors[i].old_state = connector->state;
3272
Sean Paulaa394b02018-11-29 10:04:14 -05003273 ret = drm_atomic_commit(state);
3274
3275 state->acquire_ctx = NULL;
3276
3277 return ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003278}
3279EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3280
3281/**
Thierry Reding14942762015-12-02 17:50:04 +01003282 * drm_atomic_helper_resume - subsystem-level resume helper
3283 * @dev: DRM device
3284 * @state: atomic state to resume to
3285 *
3286 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3287 * grabs all modeset locks and commits the atomic state object. This can be
3288 * used in conjunction with the drm_atomic_helper_suspend() helper to
3289 * implement suspend/resume for drivers that support atomic mode-setting.
3290 *
3291 * Returns:
3292 * 0 on success or a negative error code on failure.
3293 *
3294 * See also:
3295 * drm_atomic_helper_suspend()
3296 */
3297int drm_atomic_helper_resume(struct drm_device *dev,
3298 struct drm_atomic_state *state)
3299{
Daniel Vettera5b84442017-04-03 10:32:53 +02003300 struct drm_modeset_acquire_ctx ctx;
Thierry Reding14942762015-12-02 17:50:04 +01003301 int err;
3302
3303 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003304
Sean Paulb7ea04d2018-11-29 10:04:17 -05003305 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
Daniel Vetter869e1882017-05-31 10:38:13 +02003306
Sean Paulb7ea04d2018-11-29 10:04:17 -05003307 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
Daniel Vettera5b84442017-04-03 10:32:53 +02003308
Sean Paulb7ea04d2018-11-29 10:04:17 -05003309 DRM_MODESET_LOCK_ALL_END(ctx, err);
Jeffy Chen6d281b12017-10-09 14:46:41 +08003310 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01003311
3312 return err;
3313}
3314EXPORT_SYMBOL(drm_atomic_helper_resume);
3315
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003316static int page_flip_common(struct drm_atomic_state *state,
3317 struct drm_crtc *crtc,
3318 struct drm_framebuffer *fb,
3319 struct drm_pending_vblank_event *event,
3320 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003321{
3322 struct drm_plane *plane = crtc->primary;
3323 struct drm_plane_state *plane_state;
3324 struct drm_crtc_state *crtc_state;
3325 int ret = 0;
3326
3327 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3328 if (IS_ERR(crtc_state))
3329 return PTR_ERR(crtc_state);
3330
3331 crtc_state->event = event;
Daniel Vetter4d85f452019-09-03 21:06:42 +02003332 crtc_state->async_flip = flags & DRM_MODE_PAGE_FLIP_ASYNC;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003333
3334 plane_state = drm_atomic_get_plane_state(state, plane);
3335 if (IS_ERR(plane_state))
3336 return PTR_ERR(plane_state);
3337
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003338 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3339 if (ret != 0)
3340 return ret;
3341 drm_atomic_set_fb_for_plane(plane_state, fb);
3342
3343 /* Make sure we don't accidentally do a full modeset. */
3344 state->allow_modeset = false;
3345 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00003346 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3347 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003348 return -EINVAL;
3349 }
3350
3351 return ret;
3352}
3353
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003354/**
3355 * drm_atomic_helper_page_flip - execute a legacy page flip
Thierry Reding42240c92019-12-06 14:53:36 +01003356 * @crtc: DRM CRTC
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003357 * @fb: DRM framebuffer
3358 * @event: optional DRM event to signal upon completion
3359 * @flags: flip flags for non-vblank sync'ed updates
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003360 * @ctx: lock acquisition context
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003361 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003362 * Provides a default &drm_crtc_funcs.page_flip implementation
3363 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003364 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003365 * Returns:
3366 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003367 *
3368 * See also:
3369 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003370 */
3371int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3372 struct drm_framebuffer *fb,
3373 struct drm_pending_vblank_event *event,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003374 uint32_t flags,
3375 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003376{
3377 struct drm_plane *plane = crtc->primary;
3378 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003379 int ret = 0;
3380
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003381 state = drm_atomic_state_alloc(plane->dev);
3382 if (!state)
3383 return -ENOMEM;
3384
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003385 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003386
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003387 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003388 if (ret != 0)
3389 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01003390
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02003391 ret = drm_atomic_nonblocking_commit(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003392fail:
Chris Wilson08536952016-10-14 13:18:18 +01003393 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003394 return ret;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003395}
3396EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01003397
3398/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003399 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
Thierry Reding42240c92019-12-06 14:53:36 +01003400 * @crtc: DRM CRTC
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003401 * @fb: DRM framebuffer
3402 * @event: optional DRM event to signal upon completion
3403 * @flags: flip flags for non-vblank sync'ed updates
3404 * @target: specifying the target vblank period when the flip to take effect
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003405 * @ctx: lock acquisition context
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003406 *
3407 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3408 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3409 * target vblank period to flip.
3410 *
3411 * Returns:
3412 * Returns 0 on success, negative errno numbers on failure.
3413 */
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003414int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3415 struct drm_framebuffer *fb,
3416 struct drm_pending_vblank_event *event,
3417 uint32_t flags,
3418 uint32_t target,
3419 struct drm_modeset_acquire_ctx *ctx)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003420{
3421 struct drm_plane *plane = crtc->primary;
3422 struct drm_atomic_state *state;
3423 struct drm_crtc_state *crtc_state;
3424 int ret = 0;
3425
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003426 state = drm_atomic_state_alloc(plane->dev);
3427 if (!state)
3428 return -ENOMEM;
3429
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003430 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003431
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003432 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003433 if (ret != 0)
3434 goto fail;
3435
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01003436 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003437 if (WARN_ON(!crtc_state)) {
3438 ret = -EINVAL;
3439 goto fail;
3440 }
3441 crtc_state->target_vblank = target;
3442
3443 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003444fail:
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003445 drm_atomic_state_put(state);
3446 return ret;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003447}
3448EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
Daniel Vetter1d8224e2018-11-28 11:07:28 +01003449
3450/**
3451 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3452 * @crtc: CRTC object
3453 * @red: red correction table
3454 * @green: green correction table
3455 * @blue: green correction table
3456 * @size: size of the tables
3457 * @ctx: lock acquire context
3458 *
3459 * Implements support for legacy gamma correction table for drivers
3460 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3461 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3462 * how the atomic color management and gamma tables work.
3463 */
3464int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3465 u16 *red, u16 *green, u16 *blue,
3466 uint32_t size,
3467 struct drm_modeset_acquire_ctx *ctx)
3468{
3469 struct drm_device *dev = crtc->dev;
3470 struct drm_atomic_state *state;
3471 struct drm_crtc_state *crtc_state;
3472 struct drm_property_blob *blob = NULL;
3473 struct drm_color_lut *blob_data;
3474 int i, ret = 0;
3475 bool replaced;
3476
3477 state = drm_atomic_state_alloc(crtc->dev);
3478 if (!state)
3479 return -ENOMEM;
3480
3481 blob = drm_property_create_blob(dev,
3482 sizeof(struct drm_color_lut) * size,
3483 NULL);
3484 if (IS_ERR(blob)) {
3485 ret = PTR_ERR(blob);
3486 blob = NULL;
3487 goto fail;
3488 }
3489
3490 /* Prepare GAMMA_LUT with the legacy values. */
3491 blob_data = blob->data;
3492 for (i = 0; i < size; i++) {
3493 blob_data[i].red = red[i];
3494 blob_data[i].green = green[i];
3495 blob_data[i].blue = blue[i];
3496 }
3497
3498 state->acquire_ctx = ctx;
3499 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3500 if (IS_ERR(crtc_state)) {
3501 ret = PTR_ERR(crtc_state);
3502 goto fail;
3503 }
3504
3505 /* Reset DEGAMMA_LUT and CTM properties. */
3506 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3507 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3508 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3509 crtc_state->color_mgmt_changed |= replaced;
3510
3511 ret = drm_atomic_commit(state);
3512
3513fail:
3514 drm_atomic_state_put(state);
3515 drm_property_blob_put(blob);
3516 return ret;
3517}
3518EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);