blob: bc3487964fb5e710f6d397434866a77bc888ecf7 [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>
Daniel Vetter77ef3852020-08-14 11:38:42 +020037#include <drm/drm_drv.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020038#include <drm/drm_plane_helper.h>
39#include <drm/drm_print.h>
Sean Paul1452c252019-06-12 10:50:19 -040040#include <drm/drm_self_refresh_helper.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020041#include <drm/drm_vblank.h>
42#include <drm/drm_writeback.h>
43
Jose Abreufaf94a02017-05-25 15:19:16 +010044#include "drm_crtc_helper_internal.h"
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020045#include "drm_crtc_internal.h"
46
Daniel Vetter3150c7d2014-11-06 20:53:29 +010047/**
48 * DOC: overview
49 *
50 * This helper library provides implementations of check and commit functions on
51 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
52 * also provides convenience implementations for the atomic state handling
53 * callbacks for drivers which don't need to subclass the drm core structures to
54 * add their own additional internal state.
55 *
56 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020057 * drm_atomic_helper_check() and for the commit callback with
58 * drm_atomic_helper_commit(). But the individual stages and callbacks are
59 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010060 * together with a driver private modeset implementation.
61 *
62 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020063 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
Dafna Hirschfeldee84c582021-03-26 11:32:15 +010064 * drm_atomic_helper_disable_plane(), and the various functions to implement
65 * set_property callbacks. New drivers must not implement these functions
66 * themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010067 *
68 * The atomic helper uses the same function table structures as all other
Daniel Vetterea0dd852016-12-29 21:48:26 +010069 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
70 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
71 * also shares the &struct drm_plane_helper_funcs function table with the plane
Daniel Vetter092d01d2015-12-04 09:45:44 +010072 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010073 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010074static void
75drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010076 struct drm_plane_state *old_plane_state,
Daniel Vetterc2fcd272014-11-05 00:14:14 +010077 struct drm_plane_state *plane_state,
78 struct drm_plane *plane)
79{
80 struct drm_crtc_state *crtc_state;
81
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010082 if (old_plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010083 crtc_state = drm_atomic_get_new_crtc_state(state,
84 old_plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010085
86 if (WARN_ON(!crtc_state))
87 return;
88
89 crtc_state->planes_changed = true;
90 }
91
92 if (plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010093 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010094
95 if (WARN_ON(!crtc_state))
96 return;
97
98 crtc_state->planes_changed = true;
99 }
100}
101
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100102static int handle_conflicting_encoders(struct drm_atomic_state *state,
103 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +0100104{
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100105 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200106 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100107 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100108 struct drm_encoder *encoder;
Fabio M. De Francesco9237ec12021-04-12 14:42:14 +0200109 unsigned int encoder_mask = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100110 int i, ret = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200111
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100112 /*
113 * First loop, find all newly assigned encoders from the connectors
114 * part of the state. If the same encoder is assigned to multiple
115 * connectors bail out.
116 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100117 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100118 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
119 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200120
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100121 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200122 continue;
123
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100124 if (funcs->atomic_best_encoder)
Maxime Ripardeca22ed2020-11-18 10:47:58 +0100125 new_encoder = funcs->atomic_best_encoder(connector,
126 state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200127 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100128 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200129 else
José Roberto de Souzaa92462d2019-09-13 16:28:56 -0700130 new_encoder = drm_connector_get_single_encoder(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100131
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100132 if (new_encoder) {
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300133 if (encoder_mask & drm_encoder_mask(new_encoder)) {
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100134 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
135 new_encoder->base.id, new_encoder->name,
136 connector->base.id, connector->name);
137
138 return -EINVAL;
139 }
140
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300141 encoder_mask |= drm_encoder_mask(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100142 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200143 }
144
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100145 if (!encoder_mask)
146 return 0;
147
148 /*
149 * Second loop, iterate over all connectors not part of the state.
150 *
151 * If a conflicting encoder is found and disable_conflicting_encoders
152 * is not set, an error is returned. Userspace can provide a solution
153 * through the atomic ioctl.
154 *
Thierry Reding42240c92019-12-06 14:53:36 +0100155 * If the flag is set conflicting connectors are removed from the CRTC
156 * and the CRTC is disabled if no encoder is left. This preserves
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100157 * compatibility with the legacy set_config behavior.
158 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100159 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100160 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100161 struct drm_crtc_state *crtc_state;
162
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100163 if (drm_atomic_get_new_connector_state(state, connector))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100164 continue;
165
166 encoder = connector->state->best_encoder;
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300167 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100168 continue;
169
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100170 if (!disable_conflicting_encoders) {
171 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
172 encoder->base.id, encoder->name,
173 connector->state->crtc->base.id,
174 connector->state->crtc->name,
175 connector->base.id, connector->name);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100176 ret = -EINVAL;
177 goto out;
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100178 }
179
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100180 new_conn_state = drm_atomic_get_connector_state(state, connector);
181 if (IS_ERR(new_conn_state)) {
182 ret = PTR_ERR(new_conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100183 goto out;
184 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100185
186 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
187 encoder->base.id, encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100188 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100189 connector->base.id, connector->name);
190
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100191 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100192
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100193 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100194 if (ret)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100195 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100196
197 if (!crtc_state->connector_mask) {
198 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
199 NULL);
200 if (ret < 0)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100201 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100202
203 crtc_state->active = false;
204 }
205 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100206out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100207 drm_connector_list_iter_end(&conn_iter);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100208
Daniel Vetterc36a3252016-12-15 16:58:43 +0100209 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200210}
211
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100212static void
213set_best_encoder(struct drm_atomic_state *state,
214 struct drm_connector_state *conn_state,
215 struct drm_encoder *encoder)
216{
217 struct drm_crtc_state *crtc_state;
218 struct drm_crtc *crtc;
219
220 if (conn_state->best_encoder) {
221 /* Unset the encoder_mask in the old crtc state. */
222 crtc = conn_state->connector->state->crtc;
223
224 /* A NULL crtc is an error here because we should have
Thierry Redingdbe2d2b2019-12-06 14:53:35 +0100225 * duplicated a NULL best_encoder when crtc was NULL.
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100226 * As an exception restoring duplicated atomic state
227 * during resume is allowed, so don't warn when
228 * best_encoder is equal to encoder we intend to set.
229 */
230 WARN_ON(!crtc && encoder != conn_state->best_encoder);
231 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100232 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100233
234 crtc_state->encoder_mask &=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300235 ~drm_encoder_mask(conn_state->best_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100236 }
237 }
238
239 if (encoder) {
240 crtc = conn_state->crtc;
241 WARN_ON(!crtc);
242 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100243 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100244
245 crtc_state->encoder_mask |=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300246 drm_encoder_mask(encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100247 }
248 }
249
250 conn_state->best_encoder = encoder;
251}
252
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100253static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200254steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100255 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200256{
Daniel Vetter623369e2014-09-16 17:50:47 +0200257 struct drm_crtc_state *crtc_state;
258 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100259 struct drm_connector_state *old_connector_state, *new_connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100260 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200261
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100262 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100263 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200264
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100265 if (new_connector_state->best_encoder != encoder)
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100266 continue;
267
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100268 encoder_crtc = old_connector_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200269
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100270 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
271 encoder->base.id, encoder->name,
272 encoder_crtc->base.id, encoder_crtc->name);
273
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100274 set_best_encoder(state, new_connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100275
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100276 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100277 crtc_state->connectors_changed = true;
278
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100279 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200280 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200281}
282
283static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100284update_connector_routing(struct drm_atomic_state *state,
285 struct drm_connector *connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100286 struct drm_connector_state *old_connector_state,
287 struct drm_connector_state *new_connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200288{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200289 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200290 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200291 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200292
Daniel Vetter17a38d92015-02-22 12:24:16 +0100293 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
294 connector->base.id,
295 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200296
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100297 if (old_connector_state->crtc != new_connector_state->crtc) {
298 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100299 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200300 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200301 }
302
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100303 if (new_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100304 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200305 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200306 }
307 }
308
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100309 if (!new_connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100310 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200311 connector->base.id,
312 connector->name);
313
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100314 set_best_encoder(state, new_connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200315
316 return 0;
317 }
318
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400319 crtc_state = drm_atomic_get_new_crtc_state(state,
320 new_connector_state->crtc);
321 /*
322 * For compatibility with legacy users, we want to make sure that
323 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
324 * which would result in anything else must be considered invalid, to
325 * avoid turning on new displays on dead connectors.
326 *
327 * Since the connector can be unregistered at any point during an
328 * atomic check or commit, this is racy. But that's OK: all we care
329 * about is ensuring that userspace can't do anything but shut off the
Matt Roper1e55a532019-02-01 17:23:26 -0800330 * display on a connector that was destroyed after it's been notified,
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400331 * not before.
Lyude Paul022deba2019-02-01 19:20:03 -0500332 *
333 * Additionally, we also want to ignore connector registration when
334 * we're trying to restore an atomic state during system resume since
335 * there's a chance the connector may have been destroyed during the
336 * process, but it's better to ignore that then cause
337 * drm_atomic_helper_resume() to fail.
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400338 */
Lyude Paul022deba2019-02-01 19:20:03 -0500339 if (!state->duplicated && drm_connector_is_unregistered(connector) &&
340 crtc_state->active) {
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400341 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
342 connector->base.id, connector->name);
343 return -EINVAL;
344 }
345
Daniel Vetter623369e2014-09-16 17:50:47 +0200346 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200347
348 if (funcs->atomic_best_encoder)
Maxime Ripardeca22ed2020-11-18 10:47:58 +0100349 new_encoder = funcs->atomic_best_encoder(connector, state);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200350 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200351 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200352 else
José Roberto de Souzaa92462d2019-09-13 16:28:56 -0700353 new_encoder = drm_connector_get_single_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200354
355 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100356 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
357 connector->base.id,
358 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200359 return -EINVAL;
360 }
361
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100362 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
Russell King6ac7c542017-02-13 12:27:03 +0000363 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100364 new_encoder->base.id,
365 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100366 new_connector_state->crtc->base.id,
367 new_connector_state->crtc->name);
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100368 return -EINVAL;
369 }
370
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100371 if (new_encoder == new_connector_state->best_encoder) {
372 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100373
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200374 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100375 connector->base.id,
376 connector->name,
377 new_encoder->base.id,
378 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100379 new_connector_state->crtc->base.id,
380 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200381
382 return 0;
383 }
384
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100385 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200386
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100387 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100388
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200389 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200390
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200391 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100392 connector->base.id,
393 connector->name,
394 new_encoder->base.id,
395 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100396 new_connector_state->crtc->base.id,
397 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200398
399 return 0;
400}
401
402static int
403mode_fixup(struct drm_atomic_state *state)
404{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300405 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100406 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300407 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100408 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200409 int i;
Dan Carpenterf9ad86e2017-02-08 02:46:01 +0300410 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200411
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100412 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
413 if (!new_crtc_state->mode_changed &&
414 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200415 continue;
416
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100417 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200418 }
419
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100420 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200421 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200422 struct drm_encoder *encoder;
Boris Brezillon35a61fe2019-12-03 15:15:07 +0100423 struct drm_bridge *bridge;
Daniel Vetter623369e2014-09-16 17:50:47 +0200424
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100425 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200426
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100427 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200428 continue;
429
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100430 new_crtc_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100431 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200432
433 /*
434 * Each encoder has at most one connector (since we always steal
435 * it away), so we won't call ->mode_fixup twice.
436 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100437 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200438 funcs = encoder->helper_private;
439
Boris Brezillon35a61fe2019-12-03 15:15:07 +0100440 bridge = drm_bridge_chain_get_first_bridge(encoder);
Boris Brezillon5061b8a2020-01-28 14:55:07 +0100441 ret = drm_atomic_bridge_chain_check(bridge,
442 new_crtc_state,
443 new_conn_state);
444 if (ret) {
445 DRM_DEBUG_ATOMIC("Bridge atomic check failed\n");
446 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200447 }
448
Noralf Trønnes28276352016-05-11 18:09:20 +0200449 if (funcs && funcs->atomic_check) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100450 ret = funcs->atomic_check(encoder, new_crtc_state,
451 new_conn_state);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100452 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100453 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
454 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100455 return ret;
456 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200457 } else if (funcs && funcs->mode_fixup) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100458 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
459 &new_crtc_state->adjusted_mode);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100460 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100461 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
462 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100463 return -EINVAL;
464 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200465 }
466 }
467
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100468 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200469 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200470
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100471 if (!new_crtc_state->enable)
Liu Yingf55f1702016-05-27 17:35:54 +0800472 continue;
473
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100474 if (!new_crtc_state->mode_changed &&
475 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200476 continue;
477
478 funcs = crtc->helper_private;
Ville Syrjälä26564be2019-07-08 15:53:08 +0300479 if (!funcs || !funcs->mode_fixup)
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300480 continue;
481
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100482 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
483 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200484 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200485 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
486 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200487 return -EINVAL;
488 }
489 }
490
491 return 0;
492}
493
Jose Abreufaf94a02017-05-25 15:19:16 +0100494static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
495 struct drm_encoder *encoder,
496 struct drm_crtc *crtc,
Laurent Pinchart8518f052018-06-05 03:36:13 +0300497 const struct drm_display_mode *mode)
Jose Abreufaf94a02017-05-25 15:19:16 +0100498{
Boris Brezillon35a61fe2019-12-03 15:15:07 +0100499 struct drm_bridge *bridge;
Jose Abreufaf94a02017-05-25 15:19:16 +0100500 enum drm_mode_status ret;
501
502 ret = drm_encoder_mode_valid(encoder, mode);
503 if (ret != MODE_OK) {
504 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
505 encoder->base.id, encoder->name);
506 return ret;
507 }
508
Boris Brezillon35a61fe2019-12-03 15:15:07 +0100509 bridge = drm_bridge_chain_get_first_bridge(encoder);
Laurent Pinchart12c683e2020-05-26 04:14:48 +0300510 ret = drm_bridge_chain_mode_valid(bridge, &connector->display_info,
511 mode);
Jose Abreufaf94a02017-05-25 15:19:16 +0100512 if (ret != MODE_OK) {
513 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
514 return ret;
515 }
516
517 ret = drm_crtc_mode_valid(crtc, mode);
518 if (ret != MODE_OK) {
519 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
520 crtc->base.id, crtc->name);
521 return ret;
522 }
523
524 return ret;
525}
526
527static int
528mode_valid(struct drm_atomic_state *state)
529{
530 struct drm_connector_state *conn_state;
531 struct drm_connector *connector;
532 int i;
533
534 for_each_new_connector_in_state(state, connector, conn_state, i) {
535 struct drm_encoder *encoder = conn_state->best_encoder;
536 struct drm_crtc *crtc = conn_state->crtc;
537 struct drm_crtc_state *crtc_state;
538 enum drm_mode_status mode_status;
Laurent Pinchart8518f052018-06-05 03:36:13 +0300539 const struct drm_display_mode *mode;
Jose Abreufaf94a02017-05-25 15:19:16 +0100540
541 if (!crtc || !encoder)
542 continue;
543
544 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
545 if (!crtc_state)
546 continue;
547 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
548 continue;
549
550 mode = &crtc_state->mode;
551
552 mode_status = mode_valid_path(connector, encoder, crtc, mode);
553 if (mode_status != MODE_OK)
554 return -EINVAL;
555 }
556
557 return 0;
558}
559
Daniel Vetterd9b13622014-11-26 16:57:41 +0100560/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800561 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100562 * @dev: DRM device
563 * @state: the driver state object
564 *
565 * Check the state object to see if the requested state is physically possible.
Thierry Reding42240c92019-12-06 14:53:36 +0100566 * This does all the CRTC and connector related computations for an atomic
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200567 * update and adds any additional connectors needed for full modesets. It calls
568 * the various per-object callbacks in the follow order:
569 *
570 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
571 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
Thierry Redingdbe2d2b2019-12-06 14:53:35 +0100572 * 3. If it's determined a modeset is needed then all connectors on the affected
Thierry Reding42240c92019-12-06 14:53:36 +0100573 * CRTC are added and &drm_connector_helper_funcs.atomic_check is run on them.
Jose Abreufaf94a02017-05-25 15:19:16 +0100574 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
575 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
576 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
577 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
Thierry Reding42240c92019-12-06 14:53:36 +0100578 * This function is only called when the encoder will be part of a configured CRTC,
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200579 * it must not be used for implementing connector property validation.
580 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
581 * instead.
Thierry Reding42240c92019-12-06 14:53:36 +0100582 * 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 +0200583 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100584 * &drm_crtc_state.mode_changed is set when the input mode is changed.
585 * &drm_crtc_state.connectors_changed is set when a connector is added or
Thierry Reding42240c92019-12-06 14:53:36 +0100586 * removed from the CRTC. &drm_crtc_state.active_changed is set when
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100587 * &drm_crtc_state.active changes, which is used for DPMS.
Thomas Zimmermann7beb6912020-01-29 13:05:17 +0100588 * &drm_crtc_state.no_vblank is set from the result of drm_dev_has_vblank().
Brian Starkeyd807ed12016-10-13 10:47:08 +0100589 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100590 *
591 * IMPORTANT:
592 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100593 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
594 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
Dafna Hirschfeldee84c582021-03-26 11:32:15 +0100595 * without a full modeset) _must_ call this function after that change. It is
596 * permitted to call this function multiple times for the same update, e.g.
597 * when the &drm_crtc_helper_funcs.atomic_check functions depend upon the
598 * adjusted dotclock for fifo space allocation and watermark computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100599 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200600 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100601 * Zero for success or -errno
602 */
603int
Rob Clark934ce1c2014-11-19 16:41:33 -0500604drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200605 struct drm_atomic_state *state)
606{
Daniel Vetter623369e2014-09-16 17:50:47 +0200607 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100608 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300609 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100610 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200611 int i, ret;
Fabio M. De Francesco9237ec12021-04-12 14:42:14 +0200612 unsigned int connectors_mask = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200613
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100614 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200615 bool has_connectors =
616 !!new_crtc_state->connector_mask;
617
Daniel Vetter869e1882017-05-31 10:38:13 +0200618 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
619
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100620 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200621 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
622 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100623 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200624 }
625
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100626 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200627 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
628 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200629
630 /*
631 * For clarity this assignment is done here, but
632 * enable == 0 is only true when there are no
633 * connectors and a NULL mode.
634 *
635 * The other way around is true as well. enable != 0
636 * iff connectors are attached and a mode is set.
637 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100638 new_crtc_state->mode_changed = true;
639 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200640 }
Maarten Lankhorst24d66522017-04-06 13:19:01 +0200641
642 if (old_crtc_state->active != new_crtc_state->active) {
643 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
644 crtc->base.id, crtc->name);
645 new_crtc_state->active_changed = true;
646 }
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200647
648 if (new_crtc_state->enable != has_connectors) {
649 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
650 crtc->base.id, crtc->name);
651
652 return -EINVAL;
653 }
Thomas Zimmermann7beb6912020-01-29 13:05:17 +0100654
655 if (drm_dev_has_vblank(dev))
656 new_crtc_state->no_vblank = false;
657 else
658 new_crtc_state->no_vblank = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200659 }
660
Maarten Lankhorst44596b82017-04-06 13:19:00 +0200661 ret = handle_conflicting_encoders(state, false);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100662 if (ret)
663 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100664
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100665 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200666 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
667
Daniel Vetter869e1882017-05-31 10:38:13 +0200668 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
669
Daniel Vetter623369e2014-09-16 17:50:47 +0200670 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100671 * This only sets crtc->connectors_changed for routing changes,
672 * drivers must set crtc->connectors_changed themselves when
673 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200674 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100675 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100676 old_connector_state,
677 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200678 if (ret)
679 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100680 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100681 new_crtc_state = drm_atomic_get_new_crtc_state(state,
682 old_connector_state->crtc);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100683 if (old_connector_state->link_status !=
684 new_connector_state->link_status)
685 new_crtc_state->connectors_changed = true;
Radhakrishna Sripada47e22ff2018-10-12 11:42:32 -0700686
687 if (old_connector_state->max_requested_bpc !=
688 new_connector_state->max_requested_bpc)
689 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200690 }
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200691
692 if (funcs->atomic_check)
Sean Paul6f3b6272019-06-11 12:08:18 -0400693 ret = funcs->atomic_check(connector, state);
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200694 if (ret)
695 return ret;
696
Ville Syrjäläca52bea2018-06-15 20:07:34 +0300697 connectors_mask |= BIT(i);
Daniel Vetter623369e2014-09-16 17:50:47 +0200698 }
699
700 /*
701 * After all the routing has been prepared we need to add in any
Thierry Reding42240c92019-12-06 14:53:36 +0100702 * connector which is itself unchanged, but whose CRTC changes its
Daniel Vetter623369e2014-09-16 17:50:47 +0200703 * configuration. This must be done before calling mode_fixup in case a
704 * crtc only changed its mode but has the same set of connectors.
705 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100706 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100707 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100708 continue;
709
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200710 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
711 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100712 new_crtc_state->enable ? 'y' : 'n',
713 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200714
715 ret = drm_atomic_add_affected_connectors(state, crtc);
716 if (ret != 0)
717 return ret;
718
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200719 ret = drm_atomic_add_affected_planes(state, crtc);
720 if (ret != 0)
721 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200722 }
723
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200724 /*
725 * Iterate over all connectors again, to make sure atomic_check()
726 * has been called on them when a modeset is forced.
727 */
728 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
729 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
730
731 if (connectors_mask & BIT(i))
732 continue;
733
734 if (funcs->atomic_check)
Sean Paul6f3b6272019-06-11 12:08:18 -0400735 ret = funcs->atomic_check(connector, state);
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200736 if (ret)
737 return ret;
738 }
739
Boris Brezillon75146592020-01-28 14:55:03 +0100740 /*
741 * Iterate over all connectors again, and add all affected bridges to
742 * the state.
743 */
744 for_each_oldnew_connector_in_state(state, connector,
745 old_connector_state,
746 new_connector_state, i) {
747 struct drm_encoder *encoder;
748
749 encoder = old_connector_state->best_encoder;
750 ret = drm_atomic_add_encoder_bridges(state, encoder);
751 if (ret)
752 return ret;
753
754 encoder = new_connector_state->best_encoder;
755 ret = drm_atomic_add_encoder_bridges(state, encoder);
756 if (ret)
757 return ret;
758 }
759
Jose Abreufaf94a02017-05-25 15:19:16 +0100760 ret = mode_valid(state);
761 if (ret)
762 return ret;
763
Daniel Vetter623369e2014-09-16 17:50:47 +0200764 return mode_fixup(state);
765}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100766EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200767
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100768/**
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200769 * drm_atomic_helper_check_plane_state() - Check plane state for validity
770 * @plane_state: plane state to check
Thierry Reding42240c92019-12-06 14:53:36 +0100771 * @crtc_state: CRTC state to check
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200772 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
773 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
774 * @can_position: is it legal to position the plane such that it
Thierry Reding42240c92019-12-06 14:53:36 +0100775 * doesn't cover the entire CRTC? This will generally
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200776 * only be false for primary planes.
Thierry Reding42240c92019-12-06 14:53:36 +0100777 * @can_update_disabled: can the plane be updated while the CRTC
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200778 * is disabled?
779 *
780 * Checks that a desired plane update is valid, and updates various
781 * bits of derived state (clipped coordinates etc.). Drivers that provide
782 * their own plane handling rather than helper-provided implementations may
783 * still wish to call this function to avoid duplication of error checking
784 * code.
785 *
786 * RETURNS:
787 * Zero if update appears valid, error code on failure
788 */
789int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
790 const struct drm_crtc_state *crtc_state,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200791 int min_scale,
792 int max_scale,
793 bool can_position,
794 bool can_update_disabled)
795{
796 struct drm_framebuffer *fb = plane_state->fb;
797 struct drm_rect *src = &plane_state->src;
798 struct drm_rect *dst = &plane_state->dst;
799 unsigned int rotation = plane_state->rotation;
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200800 struct drm_rect clip = {};
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200801 int hscale, vscale;
802
803 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
804
805 *src = drm_plane_state_src(plane_state);
806 *dst = drm_plane_state_dest(plane_state);
807
808 if (!fb) {
809 plane_state->visible = false;
810 return 0;
811 }
812
813 /* crtc should only be NULL when disabling (i.e., !fb) */
814 if (WARN_ON(!plane_state->crtc)) {
815 plane_state->visible = false;
816 return 0;
817 }
818
819 if (!crtc_state->enable && !can_update_disabled) {
820 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
821 return -EINVAL;
822 }
823
824 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
825
826 /* Check scaling */
827 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
828 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
829 if (hscale < 0 || vscale < 0) {
830 DRM_DEBUG_KMS("Invalid scaling of plane\n");
831 drm_rect_debug_print("src: ", &plane_state->src, true);
832 drm_rect_debug_print("dst: ", &plane_state->dst, false);
833 return -ERANGE;
834 }
835
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200836 if (crtc_state->enable)
837 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
838
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +0200839 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200840
841 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
842
843 if (!plane_state->visible)
844 /*
845 * Plane isn't visible; some drivers can handle this
846 * so we just return success here. Drivers that can't
847 * (including those that use the primary plane helper's
848 * update function) will return an error from their
849 * update_plane handler.
850 */
851 return 0;
852
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200853 if (!can_position && !drm_rect_equals(dst, &clip)) {
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200854 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
855 drm_rect_debug_print("dst: ", dst, false);
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200856 drm_rect_debug_print("clip: ", &clip, false);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200857 return -EINVAL;
858 }
859
860 return 0;
861}
862EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
863
864/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800865 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100866 * @dev: DRM device
867 * @state: the driver state object
868 *
869 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100870 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100871 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
872 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100873 *
Thierry Reding42240c92019-12-06 14:53:36 +0100874 * It also sets &drm_crtc_state.planes_changed to indicate that a CRTC has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200875 * updated planes.
876 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200877 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100878 * Zero for success or -errno
879 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100880int
881drm_atomic_helper_check_planes(struct drm_device *dev,
882 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100883{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300884 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100885 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300886 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100887 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100888 int i, ret = 0;
889
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100890 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200891 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100892
Daniel Vetter869e1882017-05-31 10:38:13 +0200893 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
894
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100895 funcs = plane->helper_private;
896
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100897 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100898
Deepak Rawatd9778b42018-08-08 17:36:26 -0700899 drm_atomic_helper_check_plane_damage(state, new_plane_state);
900
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100901 if (!funcs || !funcs->atomic_check)
902 continue;
903
Maxime Ripard7c11b992021-02-19 13:00:24 +0100904 ret = funcs->atomic_check(plane, state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100905 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200906 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
907 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100908 return ret;
909 }
910 }
911
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100912 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200913 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100914
915 funcs = crtc->helper_private;
916
917 if (!funcs || !funcs->atomic_check)
918 continue;
919
Maxime Ripard29b77ad2020-10-28 13:32:21 +0100920 ret = funcs->atomic_check(crtc, state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100921 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200922 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
923 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100924 return ret;
925 }
926 }
927
Daniel Vetterd9b13622014-11-26 16:57:41 +0100928 return ret;
929}
930EXPORT_SYMBOL(drm_atomic_helper_check_planes);
931
932/**
933 * drm_atomic_helper_check - validate state object
934 * @dev: DRM device
935 * @state: the driver state object
936 *
937 * Check the state object to see if the requested state is physically possible.
Thierry Reding42240c92019-12-06 14:53:36 +0100938 * Only CRTCs and planes have check callbacks, so for any additional (global)
Daniel Vetterd9b13622014-11-26 16:57:41 +0100939 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100940 * Drivers without such needs can directly use this as their
941 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100942 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100943 * This just wraps the two parts of the state checking for planes and modeset
944 * state in the default order: First it calls drm_atomic_helper_check_modeset()
945 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100946 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
947 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
948 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100949 *
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200950 * Note that zpos normalization will add all enable planes to the state which
951 * might not desired for some drivers.
952 * For example enable/disable of a cursor plane which have fixed zpos value
953 * would trigger all other enabled planes to be forced to the state change.
954 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200955 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100956 * Zero for success or -errno
957 */
958int drm_atomic_helper_check(struct drm_device *dev,
959 struct drm_atomic_state *state)
960{
961 int ret;
962
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100963 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100964 if (ret)
965 return ret;
966
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200967 if (dev->mode_config.normalize_zpos) {
968 ret = drm_atomic_normalize_zpos(dev, state);
969 if (ret)
970 return ret;
971 }
972
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100973 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500974 if (ret)
975 return ret;
976
Gustavo Padovanfef9df82017-06-30 15:03:17 -0300977 if (state->legacy_cursor_update)
978 state->async_update = !drm_atomic_helper_async_check(dev, state);
979
Sean Paul1452c252019-06-12 10:50:19 -0400980 drm_self_refresh_helper_alter_state(state);
981
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100982 return ret;
983}
984EXPORT_SYMBOL(drm_atomic_helper_check);
985
Sean Paul1452c252019-06-12 10:50:19 -0400986static bool
987crtc_needs_disable(struct drm_crtc_state *old_state,
988 struct drm_crtc_state *new_state)
989{
990 /*
Thierry Reding42240c92019-12-06 14:53:36 +0100991 * No new_state means the CRTC is off, so the only criteria is whether
Sean Paul1452c252019-06-12 10:50:19 -0400992 * it's currently active or in self refresh mode.
993 */
994 if (!new_state)
995 return drm_atomic_crtc_effectively_active(old_state);
996
997 /*
Thierry Reding42240c92019-12-06 14:53:36 +0100998 * We need to run through the crtc_funcs->disable() function if the CRTC
Sean Paul1452c252019-06-12 10:50:19 -0400999 * is currently on, if it's transitioning to self refresh mode, or if
1000 * it's in self refresh mode and needs to be fully disabled.
1001 */
1002 return old_state->active ||
1003 (old_state->self_refresh_active && !new_state->enable) ||
1004 new_state->self_refresh_active;
1005}
1006
Daniel Vetter623369e2014-09-16 17:50:47 +02001007static void
1008disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
1009{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001010 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001011 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001012 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001013 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001014 int i;
1015
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001016 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 +02001017 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001018 struct drm_encoder *encoder;
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001019 struct drm_bridge *bridge;
Daniel Vetter623369e2014-09-16 17:50:47 +02001020
Fabio M. De Francesco1758f402021-04-12 14:42:16 +02001021 /*
1022 * Shut down everything that's in the changeset and currently
1023 * still on. So need to check the old, saved state.
1024 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001025 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +02001026 continue;
1027
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001028 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001029
Sean Paul1452c252019-06-12 10:50:19 -04001030 if (new_conn_state->crtc)
1031 new_crtc_state = drm_atomic_get_new_crtc_state(
1032 old_state,
1033 new_conn_state->crtc);
1034 else
1035 new_crtc_state = NULL;
1036
1037 if (!crtc_needs_disable(old_crtc_state, new_crtc_state) ||
Daniel Vetter2465ff62015-06-18 09:58:55 +02001038 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001039 continue;
1040
Rob Clark46df9ad2014-11-20 15:40:36 -05001041 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001042
Rob Clark46df9ad2014-11-20 15:40:36 -05001043 /* We shouldn't get this far if we didn't previously have
1044 * an encoder.. but WARN_ON() rather than explode.
1045 */
1046 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +02001047 continue;
1048
1049 funcs = encoder->helper_private;
1050
Daniel Vetter17a38d92015-02-22 12:24:16 +01001051 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
1052 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001053
Daniel Vetter623369e2014-09-16 17:50:47 +02001054 /*
1055 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001056 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001057 */
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001058 bridge = drm_bridge_chain_get_first_bridge(encoder);
1059 drm_atomic_bridge_chain_disable(bridge, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001060
1061 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +02001062 if (funcs) {
Sean Paul43c76d72019-06-11 16:49:53 -04001063 if (funcs->atomic_disable)
1064 funcs->atomic_disable(encoder, old_state);
1065 else if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +02001066 funcs->prepare(encoder);
1067 else if (funcs->disable)
1068 funcs->disable(encoder);
1069 else if (funcs->dpms)
1070 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1071 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001072
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001073 drm_atomic_bridge_chain_post_disable(bridge, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001074 }
1075
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001076 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 +02001077 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter84014b02017-10-17 17:27:14 +02001078 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +02001079
1080 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001081 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001082 continue;
1083
Sean Paul1452c252019-06-12 10:50:19 -04001084 if (!crtc_needs_disable(old_crtc_state, new_crtc_state))
Daniel Vetter623369e2014-09-16 17:50:47 +02001085 continue;
1086
1087 funcs = crtc->helper_private;
1088
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001089 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1090 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001091
1092
Daniel Vetter623369e2014-09-16 17:50:47 +02001093 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001094 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +02001095 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +08001096 else if (funcs->atomic_disable)
Maxime Ripard351f9502020-10-08 14:44:08 +02001097 funcs->atomic_disable(crtc, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001098 else if (funcs->disable)
1099 funcs->disable(crtc);
Rodrigo Siqueirafe616922019-03-14 15:48:45 -03001100 else if (funcs->dpms)
Daniel Vetter623369e2014-09-16 17:50:47 +02001101 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Daniel Vetter84014b02017-10-17 17:27:14 +02001102
Daniel Vetter60150022020-05-27 13:11:34 +02001103 if (!drm_dev_has_vblank(dev))
Daniel Vetter84014b02017-10-17 17:27:14 +02001104 continue;
1105
1106 ret = drm_crtc_vblank_get(crtc);
1107 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1108 if (ret == 0)
1109 drm_crtc_vblank_put(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001110 }
1111}
1112
Daniel Vetter4c18d302015-05-12 15:27:37 +02001113/**
1114 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1115 * @dev: DRM device
1116 * @old_state: atomic state object with old state structures
1117 *
1118 * This function updates all the various legacy modeset state pointers in
Ville Syrjälä441959e2020-09-07 15:00:25 +03001119 * connectors, encoders and CRTCs.
Daniel Vetter4c18d302015-05-12 15:27:37 +02001120 *
1121 * Drivers can use this for building their own atomic commit if they don't have
1122 * a pure helper-based modeset implementation.
Daniel Vetter2e2b96e2017-11-08 21:30:07 +01001123 *
1124 * Since these updates are not synchronized with lockings, only code paths
1125 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1126 * legacy state filled out by this helper. Defacto this means this helper and
1127 * the legacy state pointers are only really useful for transitioning an
1128 * existing driver to the atomic world.
Daniel Vetter4c18d302015-05-12 15:27:37 +02001129 */
1130void
1131drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1132 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001133{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001134 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001135 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001136 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001137 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001138 int i;
1139
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001140 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001141 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001142 if (connector->encoder) {
1143 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001144
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001145 connector->encoder->crtc = NULL;
1146 connector->encoder = NULL;
1147 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001148
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001149 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001150 if ((!crtc && old_conn_state->crtc) ||
1151 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001152 int mode = DRM_MODE_DPMS_OFF;
1153
1154 if (crtc && crtc->state->active)
1155 mode = DRM_MODE_DPMS_ON;
1156
1157 connector->dpms = mode;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001158 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001159 }
1160
1161 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001162 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1163 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +02001164 continue;
1165
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001166 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +02001167 continue;
1168
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001169 connector->encoder = new_conn_state->best_encoder;
1170 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +02001171 }
1172
1173 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001174 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +02001175 struct drm_plane *primary = crtc->primary;
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001176 struct drm_plane_state *new_plane_state;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001177
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001178 crtc->mode = new_crtc_state->mode;
1179 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001180
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001181 new_plane_state =
1182 drm_atomic_get_new_plane_state(old_state, primary);
1183
1184 if (new_plane_state && new_plane_state->crtc == crtc) {
1185 crtc->x = new_plane_state->src_x >> 16;
1186 crtc->y = new_plane_state->src_y >> 16;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001187 }
Ville Syrjälä4b31a9c2020-09-07 15:00:24 +03001188 }
Ville Syrjälä4b31a9c2020-09-07 15:00:24 +03001189}
1190EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
1191
1192/**
1193 * drm_atomic_helper_calc_timestamping_constants - update vblank timestamping constants
1194 * @state: atomic state object
1195 *
1196 * Updates the timestamping constants used for precise vblank timestamps
1197 * by calling drm_calc_timestamping_constants() for all enabled crtcs in @state.
1198 */
1199void drm_atomic_helper_calc_timestamping_constants(struct drm_atomic_state *state)
1200{
1201 struct drm_crtc_state *new_crtc_state;
1202 struct drm_crtc *crtc;
1203 int i;
1204
1205 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001206 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +02001207 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001208 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001209 }
1210}
Ville Syrjälä4b31a9c2020-09-07 15:00:24 +03001211EXPORT_SYMBOL(drm_atomic_helper_calc_timestamping_constants);
Daniel Vetter623369e2014-09-16 17:50:47 +02001212
1213static void
1214crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1215{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001216 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001217 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001218 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001219 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001220 int i;
1221
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001222 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001223 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001224
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001225 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +02001226 continue;
1227
1228 funcs = crtc->helper_private;
1229
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001230 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001231 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1232 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001233
Daniel Vetter623369e2014-09-16 17:50:47 +02001234 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001235 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001236 }
1237
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001238 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001239 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001240 struct drm_encoder *encoder;
1241 struct drm_display_mode *mode, *adjusted_mode;
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001242 struct drm_bridge *bridge;
Daniel Vetter623369e2014-09-16 17:50:47 +02001243
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001244 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001245 continue;
1246
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001247 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001248 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001249 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001250 mode = &new_crtc_state->mode;
1251 adjusted_mode = &new_crtc_state->adjusted_mode;
1252
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001253 if (!new_crtc_state->mode_changed)
1254 continue;
1255
Daniel Vetter17a38d92015-02-22 12:24:16 +01001256 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1257 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001258
Daniel Vetter623369e2014-09-16 17:50:47 +02001259 /*
1260 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001261 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001262 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001263 if (funcs && funcs->atomic_mode_set) {
1264 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001265 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001266 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +01001267 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001268 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001269
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001270 bridge = drm_bridge_chain_get_first_bridge(encoder);
1271 drm_bridge_chain_mode_set(bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001272 }
1273}
1274
1275/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001276 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001277 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +01001278 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001279 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001280 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +02001281 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +01001282 *
Thierry Reding42240c92019-12-06 14:53:36 +01001283 * For compatibility with legacy CRTC helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +01001284 * drm_atomic_helper_commit_planes(), which is what the default commit function
1285 * does. But drivers with different needs can group the modeset commits together
1286 * and do the plane commits at the end. This is useful for drivers doing runtime
1287 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001288 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001289void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1290 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001291{
Laurent Pincharta072f802015-02-22 12:24:18 +01001292 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +02001293
1294 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
Ville Syrjälä441959e2020-09-07 15:00:25 +03001295 drm_atomic_helper_calc_timestamping_constants(old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +02001296
Laurent Pincharta072f802015-02-22 12:24:18 +01001297 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001298}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001299EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001300
Brian Starkey935774c2017-03-29 17:42:32 +01001301static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1302 struct drm_atomic_state *old_state)
1303{
1304 struct drm_connector *connector;
1305 struct drm_connector_state *new_conn_state;
1306 int i;
1307
1308 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1309 const struct drm_connector_helper_funcs *funcs;
1310
1311 funcs = connector->helper_private;
Boris Brezillon814bde92018-07-03 09:50:17 +02001312 if (!funcs->atomic_commit)
1313 continue;
Brian Starkey935774c2017-03-29 17:42:32 +01001314
1315 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1316 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
Maxime Ripardeca22ed2020-11-18 10:47:58 +01001317 funcs->atomic_commit(connector, old_state);
Brian Starkey935774c2017-03-29 17:42:32 +01001318 }
1319 }
1320}
1321
Daniel Vetter623369e2014-09-16 17:50:47 +02001322/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001323 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001324 * @dev: DRM device
1325 * @old_state: atomic state object with old state structures
1326 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001327 * This function enables all the outputs with the new configuration which had to
1328 * be turned off for the update.
1329 *
Thierry Reding42240c92019-12-06 14:53:36 +01001330 * For compatibility with legacy CRTC helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +01001331 * drm_atomic_helper_commit_planes(), which is what the default commit function
1332 * does. But drivers with different needs can group the modeset commits together
1333 * and do the plane commits at the end. This is useful for drivers doing runtime
1334 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001335 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001336void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1337 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001338{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001339 struct drm_crtc *crtc;
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001340 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001341 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001342 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001343 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001344 int i;
1345
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001346 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 +02001347 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001348
1349 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001350 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001351 continue;
1352
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001353 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +02001354 continue;
1355
1356 funcs = crtc->helper_private;
1357
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001358 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001359 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1360 crtc->base.id, crtc->name);
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001361 if (funcs->atomic_enable)
Maxime Ripard351f9502020-10-08 14:44:08 +02001362 funcs->atomic_enable(crtc, old_state);
Rodrigo Siqueirafe616922019-03-14 15:48:45 -03001363 else if (funcs->commit)
Daniel Vetteree0a89c2015-01-22 16:36:24 +01001364 funcs->commit(crtc);
1365 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001366 }
1367
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001368 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001369 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001370 struct drm_encoder *encoder;
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001371 struct drm_bridge *bridge;
Daniel Vetter623369e2014-09-16 17:50:47 +02001372
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001373 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001374 continue;
1375
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001376 if (!new_conn_state->crtc->state->active ||
1377 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001378 continue;
1379
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001380 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001381 funcs = encoder->helper_private;
1382
Daniel Vetter17a38d92015-02-22 12:24:16 +01001383 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1384 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001385
Daniel Vetter623369e2014-09-16 17:50:47 +02001386 /*
1387 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001388 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001389 */
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001390 bridge = drm_bridge_chain_get_first_bridge(encoder);
1391 drm_atomic_bridge_chain_pre_enable(bridge, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001392
Noralf Trønnes28276352016-05-11 18:09:20 +02001393 if (funcs) {
Sean Paul43c76d72019-06-11 16:49:53 -04001394 if (funcs->atomic_enable)
1395 funcs->atomic_enable(encoder, old_state);
1396 else if (funcs->enable)
Noralf Trønnes28276352016-05-11 18:09:20 +02001397 funcs->enable(encoder);
1398 else if (funcs->commit)
1399 funcs->commit(encoder);
1400 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001401
Boris Brezillon35a61fe2019-12-03 15:15:07 +01001402 drm_atomic_bridge_chain_enable(bridge, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001403 }
Brian Starkey935774c2017-03-29 17:42:32 +01001404
1405 drm_atomic_helper_commit_writebacks(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001406}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001407EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001408
Rob Clark4c5b7f32016-03-18 19:14:55 -04001409/**
1410 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1411 * @dev: DRM device
1412 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001413 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
Fabio M. De Francesco1758f402021-04-12 14:42:16 +02001414 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001415 *
1416 * For implicit sync, driver should fish the exclusive fence out from the
1417 * incoming fb's and stash it in the drm_plane_state. This is called after
1418 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1419 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001420 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001421 * Note that @pre_swap is needed since the point where we block for fences moves
1422 * around depending upon whether an atomic commit is blocking or
Gustavo Padovan42590372017-04-27 11:35:06 -03001423 * non-blocking. For non-blocking commit all waiting needs to happen after
1424 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001425 * to wait **before** we do anything that can't be easily rolled back. That is
1426 * before we call drm_atomic_helper_swap_state().
1427 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001428 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001429 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001430int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1431 struct drm_atomic_state *state,
1432 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001433{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001434 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001435 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001436 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001437
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001438 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1439 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001440 continue;
1441
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001442 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001443
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001444 /*
1445 * If waiting for fences pre-swap (ie: nonblock), userspace can
1446 * still interrupt the operation. Instead of blocking until the
1447 * timer expires, make the wait interruptible.
1448 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001449 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001450 if (ret)
1451 return ret;
1452
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001453 dma_fence_put(new_plane_state->fence);
1454 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001455 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001456
1457 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001458}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001459EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001460
John Keepingc2409062016-01-19 10:46:58 +00001461/**
Thierry Reding42240c92019-12-06 14:53:36 +01001462 * drm_atomic_helper_wait_for_vblanks - wait for vblank on CRTCs
Rob Clark5ee32292014-11-11 19:38:59 -05001463 * @dev: DRM device
1464 * @old_state: atomic state object with old state structures
1465 *
Thierry Redingdbe2d2b2019-12-06 14:53:35 +01001466 * Helper to, after atomic commit, wait for vblanks on all affected
Thierry Reding42240c92019-12-06 14:53:36 +01001467 * CRTCs (ie. before cleaning up old framebuffers using
Boris Brezillon01086482017-06-02 10:32:06 +02001468 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
Daniel Vetterab58e332014-11-24 20:42:42 +01001469 * framebuffers have actually changed to optimize for the legacy cursor and
1470 * plane update use-case.
Boris Brezillon01086482017-06-02 10:32:06 +02001471 *
1472 * Drivers using the nonblocking commit tracking support initialized by calling
1473 * drm_atomic_helper_setup_commit() should look at
1474 * drm_atomic_helper_wait_for_flip_done() as an alternative.
Rob Clark5ee32292014-11-11 19:38:59 -05001475 */
1476void
1477drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1478 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001479{
1480 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001481 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001482 int i, ret;
Fabio M. De Francesco9237ec12021-04-12 14:42:14 +02001483 unsigned int crtc_mask = 0;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001484
1485 /*
1486 * Legacy cursor ioctls are completely unsynced, and userspace
1487 * relies on that (by doing tons of cursor updates).
1488 */
1489 if (old_state->legacy_cursor_update)
1490 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001491
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001492 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Lucas Stacha76dfe32017-11-29 12:04:31 +01001493 if (!new_crtc_state->active)
Daniel Vetterab58e332014-11-24 20:42:42 +01001494 continue;
1495
Daniel Vetter623369e2014-09-16 17:50:47 +02001496 ret = drm_crtc_vblank_get(crtc);
1497 if (ret != 0)
1498 continue;
1499
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001500 crtc_mask |= drm_crtc_mask(crtc);
1501 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001502 }
1503
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001504 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001505 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001506 continue;
1507
1508 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001509 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001510 drm_crtc_vblank_count(crtc),
Linus Walleijb3198c32019-04-30 11:37:46 +02001511 msecs_to_jiffies(100));
Daniel Vetter623369e2014-09-16 17:50:47 +02001512
Russell King6ac7c542017-02-13 12:27:03 +00001513 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1514 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001515
Daniel Vetter623369e2014-09-16 17:50:47 +02001516 drm_crtc_vblank_put(crtc);
1517 }
1518}
Rob Clark5ee32292014-11-11 19:38:59 -05001519EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001520
1521/**
Boris Brezillon01086482017-06-02 10:32:06 +02001522 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1523 * @dev: DRM device
1524 * @old_state: atomic state object with old state structures
1525 *
Thierry Redingdbe2d2b2019-12-06 14:53:35 +01001526 * Helper to, after atomic commit, wait for page flips on all affected
Boris Brezillon01086482017-06-02 10:32:06 +02001527 * crtcs (ie. before cleaning up old framebuffers using
1528 * drm_atomic_helper_cleanup_planes()). Compared to
Thierry Redingdbe2d2b2019-12-06 14:53:35 +01001529 * drm_atomic_helper_wait_for_vblanks() this waits for the completion on all
Boris Brezillon01086482017-06-02 10:32:06 +02001530 * CRTCs, assuming that cursors-only updates are signalling their completion
1531 * immediately (or using a different path).
1532 *
1533 * This requires that drivers use the nonblocking commit tracking support
1534 * initialized using drm_atomic_helper_setup_commit().
1535 */
1536void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1537 struct drm_atomic_state *old_state)
1538{
Boris Brezillon01086482017-06-02 10:32:06 +02001539 struct drm_crtc *crtc;
1540 int i;
1541
Leo Li4364bcb2018-10-15 09:46:40 -04001542 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1543 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
Boris Brezillon01086482017-06-02 10:32:06 +02001544 int ret;
1545
Leo Li4364bcb2018-10-15 09:46:40 -04001546 crtc = old_state->crtcs[i].ptr;
1547
1548 if (!crtc || !commit)
Boris Brezillon01086482017-06-02 10:32:06 +02001549 continue;
1550
1551 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1552 if (ret == 0)
1553 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1554 crtc->base.id, crtc->name);
1555 }
Ville Syrjälä2de42f72018-11-22 16:34:11 +02001556
1557 if (old_state->fake_commit)
1558 complete_all(&old_state->fake_commit->flip_done);
Boris Brezillon01086482017-06-02 10:32:06 +02001559}
1560EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1561
1562/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001563 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001564 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001565 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001566 * This is the default implementation for the
Maxime Ripard81a099a2017-07-20 15:01:16 +02001567 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1568 * that do not support runtime_pm or do not need the CRTC to be
1569 * enabled to perform a commit. Otherwise, see
1570 * drm_atomic_helper_commit_tail_rpm().
Daniel Vetter623369e2014-09-16 17:50:47 +02001571 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001572 * Note that the default ordering of how the various stages are called is to
Maxime Ripard81a099a2017-07-20 15:01:16 +02001573 * match the legacy modeset helper library closest.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001574 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001575void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001576{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001577 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001578
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001579 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001580
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001581 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001582
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001583 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001584
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001585 drm_atomic_helper_fake_vblank(old_state);
1586
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001587 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001588
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001589 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001590
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001591 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001592}
1593EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1594
Maxime Ripard81a099a2017-07-20 15:01:16 +02001595/**
1596 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1597 * @old_state: new modeset state to be committed
1598 *
1599 * This is an alternative implementation for the
1600 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1601 * that support runtime_pm or need the CRTC to be enabled to perform a
1602 * commit. Otherwise, one should use the default implementation
1603 * drm_atomic_helper_commit_tail().
1604 */
1605void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1606{
1607 struct drm_device *dev = old_state->dev;
1608
1609 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1610
1611 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1612
1613 drm_atomic_helper_commit_planes(dev, old_state,
1614 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1615
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001616 drm_atomic_helper_fake_vblank(old_state);
1617
Maxime Ripard81a099a2017-07-20 15:01:16 +02001618 drm_atomic_helper_commit_hw_done(old_state);
1619
1620 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1621
1622 drm_atomic_helper_cleanup_planes(dev, old_state);
1623}
1624EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1625
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001626static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001627{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001628 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001629 const struct drm_mode_config_helper_funcs *funcs;
Rob Clark86de88c2019-11-04 09:37:36 -08001630 struct drm_crtc_state *new_crtc_state;
1631 struct drm_crtc *crtc;
Sean Pauld4da4e32019-09-18 16:07:29 -04001632 ktime_t start;
1633 s64 commit_time_ms;
Rob Clark86de88c2019-11-04 09:37:36 -08001634 unsigned int i, new_self_refresh_mask = 0;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001635
1636 funcs = dev->mode_config.helper_private;
1637
Sean Pauld4da4e32019-09-18 16:07:29 -04001638 /*
1639 * We're measuring the _entire_ commit, so the time will vary depending
1640 * on how many fences and objects are involved. For the purposes of self
1641 * refresh, this is desirable since it'll give us an idea of how
1642 * congested things are. This will inform our decision on how often we
1643 * should enter self refresh after idle.
1644 *
1645 * These times will be averaged out in the self refresh helpers to avoid
1646 * overreacting over one outlier frame
1647 */
1648 start = ktime_get();
1649
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001650 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001651
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001652 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001653
Rob Clark86de88c2019-11-04 09:37:36 -08001654 /*
1655 * We cannot safely access new_crtc_state after
1656 * drm_atomic_helper_commit_hw_done() so figure out which crtc's have
1657 * self-refresh active beforehand:
1658 */
1659 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i)
1660 if (new_crtc_state->self_refresh_active)
1661 new_self_refresh_mask |= BIT(i);
1662
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001663 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001664 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001665 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001666 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001667
Sean Pauld4da4e32019-09-18 16:07:29 -04001668 commit_time_ms = ktime_ms_delta(ktime_get(), start);
1669 if (commit_time_ms > 0)
1670 drm_self_refresh_helper_update_avg_times(old_state,
Rob Clark86de88c2019-11-04 09:37:36 -08001671 (unsigned long)commit_time_ms,
1672 new_self_refresh_mask);
Sean Pauld4da4e32019-09-18 16:07:29 -04001673
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001674 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001675
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001676 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001677}
1678
1679static void commit_work(struct work_struct *work)
1680{
1681 struct drm_atomic_state *state = container_of(work,
1682 struct drm_atomic_state,
1683 commit_work);
1684 commit_tail(state);
1685}
1686
1687/**
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001688 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1689 * @dev: DRM device
1690 * @state: the driver state object
1691 *
1692 * This helper will check if it is possible to commit the state asynchronously.
1693 * Async commits are not supposed to swap the states like normal sync commits
1694 * but just do in-place changes on the current state.
1695 *
1696 * It will return 0 if the commit can happen in an asynchronous fashion or error
1697 * if not. Note that error just mean it can't be commited asynchronously, if it
1698 * fails the commit should be treated like a normal synchronous commit.
1699 */
1700int drm_atomic_helper_async_check(struct drm_device *dev,
1701 struct drm_atomic_state *state)
1702{
1703 struct drm_crtc *crtc;
1704 struct drm_crtc_state *crtc_state;
Boris Brezillonde2d8db2018-07-24 15:33:00 +02001705 struct drm_plane *plane = NULL;
1706 struct drm_plane_state *old_plane_state = NULL;
1707 struct drm_plane_state *new_plane_state = NULL;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001708 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001709 int i, n_planes = 0;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001710
1711 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1712 if (drm_atomic_crtc_needs_modeset(crtc_state))
1713 return -EINVAL;
1714 }
1715
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001716 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001717 n_planes++;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001718
1719 /* FIXME: we support only single plane updates for now */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001720 if (n_planes != 1)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001721 return -EINVAL;
1722
Boris Brezillon603ba2d2018-07-24 15:32:15 +02001723 if (!new_plane_state->crtc ||
1724 old_plane_state->crtc != new_plane_state->crtc)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001725 return -EINVAL;
1726
1727 funcs = plane->helper_private;
1728 if (!funcs->atomic_async_update)
1729 return -EINVAL;
1730
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001731 if (new_plane_state->fence)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001732 return -EINVAL;
1733
1734 /*
1735 * Don't do an async update if there is an outstanding commit modifying
1736 * the plane. This prevents our async update's changes from getting
1737 * overridden by a previous synchronous update's state.
1738 */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001739 if (old_plane_state->commit &&
Daniel Vetter9073d4e2020-09-25 10:46:51 +02001740 !try_wait_for_completion(&old_plane_state->commit->hw_done)) {
1741 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] inflight previous commit preventing async commit\n",
1742 plane->base.id, plane->name);
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001743 return -EBUSY;
Daniel Vetter9073d4e2020-09-25 10:46:51 +02001744 }
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001745
Maxime Ripard5ddb0bd2021-02-19 13:00:21 +01001746 return funcs->atomic_async_check(plane, state);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001747}
1748EXPORT_SYMBOL(drm_atomic_helper_async_check);
1749
1750/**
1751 * drm_atomic_helper_async_commit - commit state asynchronously
1752 * @dev: DRM device
1753 * @state: the driver state object
1754 *
1755 * This function commits a state asynchronously, i.e., not vblank
1756 * synchronized. It should be used on a state only when
1757 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1758 * the states like normal sync commits, but just do in-place changes on the
1759 * current state.
Helen Koike89a4aac2019-06-03 13:56:10 -03001760 *
1761 * TODO: Implement full swap instead of doing in-place changes.
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001762 */
1763void drm_atomic_helper_async_commit(struct drm_device *dev,
1764 struct drm_atomic_state *state)
1765{
1766 struct drm_plane *plane;
1767 struct drm_plane_state *plane_state;
1768 const struct drm_plane_helper_funcs *funcs;
1769 int i;
1770
1771 for_each_new_plane_in_state(state, plane, plane_state, i) {
Helen Koike89a4aac2019-06-03 13:56:10 -03001772 struct drm_framebuffer *new_fb = plane_state->fb;
1773 struct drm_framebuffer *old_fb = plane->state->fb;
1774
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001775 funcs = plane->helper_private;
Maxime Ripard5ddb0bd2021-02-19 13:00:21 +01001776 funcs->atomic_async_update(plane, state);
Boris Brezillon02edfd92018-03-30 16:55:18 +02001777
1778 /*
1779 * ->atomic_async_update() is supposed to update the
1780 * plane->state in-place, make sure at least common
1781 * properties have been properly updated.
1782 */
Helen Koike89a4aac2019-06-03 13:56:10 -03001783 WARN_ON_ONCE(plane->state->fb != new_fb);
Boris Brezillon02edfd92018-03-30 16:55:18 +02001784 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1785 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1786 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1787 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
Helen Koike89a4aac2019-06-03 13:56:10 -03001788
1789 /*
1790 * Make sure the FBs have been swapped so that cleanups in the
1791 * new_state performs a cleanup in the old FB.
1792 */
1793 WARN_ON_ONCE(plane_state->fb != old_fb);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001794 }
1795}
1796EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1797
1798/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001799 * drm_atomic_helper_commit - commit validated state object
1800 * @dev: DRM device
1801 * @state: the driver state object
1802 * @nonblock: whether nonblocking behavior is requested.
1803 *
1804 * This function commits a with drm_atomic_helper_check() pre-validated state
1805 * object. This can still fail when e.g. the framebuffer reservation fails. This
1806 * function implements nonblocking commits, using
1807 * drm_atomic_helper_setup_commit() and related functions.
1808 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001809 * Committing the actual hardware state is done through the
Matt Roper1e55a532019-02-01 17:23:26 -08001810 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or its default
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001811 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001812 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001813 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001814 * Zero for success or -errno.
1815 */
1816int drm_atomic_helper_commit(struct drm_device *dev,
1817 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001818 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001819{
1820 int ret;
1821
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001822 if (state->async_update) {
1823 ret = drm_atomic_helper_prepare_planes(dev, state);
1824 if (ret)
1825 return ret;
1826
1827 drm_atomic_helper_async_commit(dev, state);
1828 drm_atomic_helper_cleanup_planes(dev, state);
1829
1830 return 0;
1831 }
1832
Daniel Vettera095caa2016-06-08 17:15:36 +02001833 ret = drm_atomic_helper_setup_commit(state, nonblock);
1834 if (ret)
1835 return ret;
1836
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001837 INIT_WORK(&state->commit_work, commit_work);
1838
Daniel Vetter623369e2014-09-16 17:50:47 +02001839 ret = drm_atomic_helper_prepare_planes(dev, state);
1840 if (ret)
1841 return ret;
1842
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001843 if (!nonblock) {
1844 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001845 if (ret)
1846 goto err;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001847 }
1848
Daniel Vetter623369e2014-09-16 17:50:47 +02001849 /*
1850 * This is the point of no return - everything below never fails except
1851 * when the hw goes bonghits. Which means we can commit the new state on
1852 * the software side now.
1853 */
1854
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001855 ret = drm_atomic_helper_swap_state(state, true);
1856 if (ret)
1857 goto err;
Daniel Vetter623369e2014-09-16 17:50:47 +02001858
1859 /*
1860 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001861 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001862 * that the asynchronous work has either been cancelled (if the driver
1863 * supports it, which at least requires that the framebuffers get
1864 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1865 * before the new state gets committed on the software side with
1866 * drm_atomic_helper_swap_state().
1867 *
1868 * This scheme allows new atomic state updates to be prepared and
1869 * checked in parallel to the asynchronous completion of the previous
1870 * update. Which is important since compositors need to figure out the
1871 * composition of the next frame right after having submitted the
1872 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001873 *
1874 * NOTE: Commit work has multiple phases, first hardware commit, then
1875 * cleanup. We want them to overlap, hence need system_unbound_wq to
Kieran Binghama0689e32019-03-12 00:33:07 +00001876 * make sure work items don't artificially stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001877 */
1878
Chris Wilson08536952016-10-14 13:18:18 +01001879 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001880 if (nonblock)
1881 queue_work(system_unbound_wq, &state->commit_work);
1882 else
1883 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001884
1885 return 0;
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001886
1887err:
1888 drm_atomic_helper_cleanup_planes(dev, state);
1889 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +02001890}
1891EXPORT_SYMBOL(drm_atomic_helper_commit);
1892
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001893/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001894 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001895 *
Daniel Vetter0380c682019-12-04 11:00:11 +01001896 * Nonblocking atomic commits should use struct &drm_crtc_commit to sequence
1897 * different operations against each another. Locks, especially struct
1898 * &drm_modeset_lock, should not be held in worker threads or any other
1899 * asynchronous context used to commit the hardware state.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001900 *
Daniel Vetter0380c682019-12-04 11:00:11 +01001901 * drm_atomic_helper_commit() implements the recommended sequence for
1902 * nonblocking commits, using drm_atomic_helper_setup_commit() internally:
1903 *
1904 * 1. Run drm_atomic_helper_prepare_planes(). Since this can fail and we
1905 * need to propagate out of memory/VRAM errors to userspace, it must be called
Daniel Vettere8c833a2014-07-27 18:30:19 +02001906 * synchronously.
1907 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001908 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vetter0380c682019-12-04 11:00:11 +01001909 * might be affected by the new state update. This is handled by
1910 * drm_atomic_helper_setup_commit().
Daniel Vettere8c833a2014-07-27 18:30:19 +02001911 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001912 * Asynchronous workers need to have sufficient parallelism to be able to run
1913 * different atomic commits on different CRTCs in parallel. The simplest way to
Kieran Binghama0689e32019-03-12 00:33:07 +00001914 * achieve this is by running them on the &system_unbound_wq work queue. Note
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001915 * that drivers are not required to split up atomic commits and run an
1916 * individual commit in parallel - userspace is supposed to do that if it cares.
1917 * But it might be beneficial to do that for modesets, since those necessarily
1918 * must be done as one global operation, and enabling or disabling a CRTC can
1919 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001920 *
Daniel Vetter0380c682019-12-04 11:00:11 +01001921 * IMPORTANT: A &drm_atomic_state update for multiple CRTCs is sequenced
1922 * against all CRTCs therein. Therefore for atomic state updates which only flip
1923 * planes the driver must not get the struct &drm_crtc_state of unrelated CRTCs
1924 * in its atomic check code: This would prevent committing of atomic updates to
1925 * multiple CRTCs in parallel. In general, adding additional state structures
1926 * should be avoided as much as possible, because this reduces parallelism in
1927 * (nonblocking) commits, both due to locking and due to commit sequencing
1928 * requirements.
1929 *
Daniel Vettere8c833a2014-07-27 18:30:19 +02001930 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001931 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vetter0380c682019-12-04 11:00:11 +01001932 * locks means concurrent callers never see inconsistent state. Note that commit
1933 * workers do not hold any locks; their access is only coordinated through
1934 * ordering. If workers would access state only through the pointers in the
1935 * free-standing state objects (currently not the case for any driver) then even
1936 * multiple pending commits could be in-flight at the same time.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001937 *
1938 * 4. Schedule a work item to do all subsequent steps, using the split-out
1939 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1940 * then cleaning up the framebuffers after the old framebuffer is no longer
Daniel Vetter0380c682019-12-04 11:00:11 +01001941 * being displayed. The scheduled work should synchronize against other workers
1942 * using the &drm_crtc_commit infrastructure as needed. See
1943 * drm_atomic_helper_setup_commit() for more details.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001944 */
1945
Daniel Vettera095caa2016-06-08 17:15:36 +02001946static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1947{
1948 struct drm_crtc_commit *commit, *stall_commit = NULL;
1949 bool completed = true;
1950 int i;
1951 long ret = 0;
1952
1953 spin_lock(&crtc->commit_lock);
1954 i = 0;
1955 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1956 if (i == 0) {
1957 completed = try_wait_for_completion(&commit->flip_done);
Fabio M. De Francesco1758f402021-04-12 14:42:16 +02001958 /*
1959 * Userspace is not allowed to get ahead of the previous
1960 * commit with nonblocking ones.
1961 */
Daniel Vettera095caa2016-06-08 17:15:36 +02001962 if (!completed && nonblock) {
1963 spin_unlock(&crtc->commit_lock);
Daniel Vetter9073d4e2020-09-25 10:46:51 +02001964 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] busy with a previous commit\n",
1965 crtc->base.id, crtc->name);
1966
Daniel Vettera095caa2016-06-08 17:15:36 +02001967 return -EBUSY;
1968 }
1969 } else if (i == 1) {
Maarten Lankhorstf46640b2017-09-04 12:48:36 +02001970 stall_commit = drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001971 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001972 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001973
1974 i++;
1975 }
1976 spin_unlock(&crtc->commit_lock);
1977
1978 if (!stall_commit)
1979 return 0;
1980
1981 /* We don't want to let commits get ahead of cleanup work too much,
1982 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1983 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001984 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001985 10*HZ);
1986 if (ret == 0)
1987 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1988 crtc->base.id, crtc->name);
1989
1990 drm_crtc_commit_put(stall_commit);
1991
1992 return ret < 0 ? ret : 0;
1993}
1994
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001995static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001996{
1997 struct drm_crtc_commit *commit = container_of(completion,
1998 typeof(*commit),
1999 flip_done);
2000
2001 drm_crtc_commit_put(commit);
2002}
2003
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002004static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
2005{
2006 init_completion(&commit->flip_done);
2007 init_completion(&commit->hw_done);
2008 init_completion(&commit->cleanup_done);
2009 INIT_LIST_HEAD(&commit->commit_entry);
2010 kref_init(&commit->ref);
2011 commit->crtc = crtc;
2012}
2013
2014static struct drm_crtc_commit *
2015crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
2016{
2017 if (crtc) {
2018 struct drm_crtc_state *new_crtc_state;
2019
2020 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
2021
2022 return new_crtc_state->commit;
2023 }
2024
2025 if (!state->fake_commit) {
2026 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
2027 if (!state->fake_commit)
2028 return NULL;
2029
2030 init_commit(state->fake_commit, NULL);
2031 }
2032
2033 return state->fake_commit;
2034}
2035
Daniel Vettera095caa2016-06-08 17:15:36 +02002036/**
2037 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
2038 * @state: new modeset state to be committed
2039 * @nonblock: whether nonblocking behavior is requested.
2040 *
2041 * This function prepares @state to be used by the atomic helper's support for
2042 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002043 * should always call this function from their
2044 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02002045 *
Maxime Ripardddadd402020-12-04 16:11:32 +01002046 * Drivers that need to extend the commit setup to private objects can use the
2047 * &drm_mode_config_helper_funcs.atomic_commit_setup hook.
2048 *
Daniel Vettera095caa2016-06-08 17:15:36 +02002049 * To be able to use this support drivers need to use a few more helper
2050 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
2051 * actually committing the hardware state, and for nonblocking commits this call
2052 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
Matt Roper1e55a532019-02-01 17:23:26 -08002053 * and its stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002054 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02002055 *
2056 * Completion of the hardware commit step must be signalled using
2057 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
2058 * to read or change any permanent software or hardware modeset state. The only
2059 * exception is state protected by other means than &drm_modeset_lock locks.
2060 * Only the free standing @state with pointers to the old state structures can
2061 * be inspected, e.g. to clean up old buffers using
2062 * drm_atomic_helper_cleanup_planes().
2063 *
2064 * At the very end, before cleaning up @state drivers must call
2065 * drm_atomic_helper_commit_cleanup_done().
2066 *
2067 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
Thierry Reding9ac07812017-10-12 16:06:16 +02002068 * complete and easy-to-use default implementation of the atomic_commit() hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02002069 *
2070 * The tracking of asynchronously executed and still pending commits is done
2071 * using the core structure &drm_crtc_commit.
2072 *
2073 * By default there's no need to clean up resources allocated by this function
2074 * explicitly: drm_atomic_state_default_clear() will take care of that
2075 * automatically.
2076 *
2077 * Returns:
2078 *
2079 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
2080 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
2081 */
2082int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
2083 bool nonblock)
2084{
2085 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002086 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002087 struct drm_connector *conn;
2088 struct drm_connector_state *old_conn_state, *new_conn_state;
2089 struct drm_plane *plane;
2090 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002091 struct drm_crtc_commit *commit;
Maxime Ripardddadd402020-12-04 16:11:32 +01002092 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vettera095caa2016-06-08 17:15:36 +02002093 int i, ret;
2094
Maxime Ripardddadd402020-12-04 16:11:32 +01002095 funcs = state->dev->mode_config.helper_private;
2096
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002097 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002098 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
2099 if (!commit)
2100 return -ENOMEM;
2101
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002102 init_commit(commit, crtc);
Daniel Vettera095caa2016-06-08 17:15:36 +02002103
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002104 new_crtc_state->commit = commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002105
2106 ret = stall_checks(crtc, nonblock);
2107 if (ret)
2108 return ret;
2109
Fabio M. De Francesco1758f402021-04-12 14:42:16 +02002110 /*
2111 * Drivers only send out events when at least either current or
Daniel Vettera095caa2016-06-08 17:15:36 +02002112 * new CRTC state is active. Complete right away if everything
Fabio M. De Francesco1758f402021-04-12 14:42:16 +02002113 * stays off.
2114 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002115 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002116 complete_all(&commit->flip_done);
2117 continue;
2118 }
2119
2120 /* Legacy cursor updates are fully unsynced. */
2121 if (state->legacy_cursor_update) {
2122 complete_all(&commit->flip_done);
2123 continue;
2124 }
2125
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002126 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002127 commit->event = kzalloc(sizeof(*commit->event),
2128 GFP_KERNEL);
2129 if (!commit->event)
2130 return -ENOMEM;
2131
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002132 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02002133 }
2134
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002135 new_crtc_state->event->base.completion = &commit->flip_done;
2136 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01002137 drm_crtc_commit_get(commit);
Leo (Sunpeng) Li1c6ceee2018-01-17 12:51:08 +01002138
2139 commit->abort_completion = true;
Leo Li4364bcb2018-10-15 09:46:40 -04002140
2141 state->crtcs[i].commit = commit;
2142 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02002143 }
2144
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002145 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
Fabio M. De Francesco1758f402021-04-12 14:42:16 +02002146 /*
2147 * Userspace is not allowed to get ahead of the previous
2148 * commit with nonblocking ones.
2149 */
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002150 if (nonblock && old_conn_state->commit &&
Daniel Vetter9073d4e2020-09-25 10:46:51 +02002151 !try_wait_for_completion(&old_conn_state->commit->flip_done)) {
2152 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] busy with a previous commit\n",
2153 conn->base.id, conn->name);
2154
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002155 return -EBUSY;
Daniel Vetter9073d4e2020-09-25 10:46:51 +02002156 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002157
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01002158 /* Always track connectors explicitly for e.g. link retraining. */
2159 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002160 if (!commit)
2161 return -ENOMEM;
2162
2163 new_conn_state->commit = drm_crtc_commit_get(commit);
2164 }
2165
2166 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Fabio M. De Francesco1758f402021-04-12 14:42:16 +02002167 /*
2168 * Userspace is not allowed to get ahead of the previous
2169 * commit with nonblocking ones.
2170 */
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002171 if (nonblock && old_plane_state->commit &&
Daniel Vetter9073d4e2020-09-25 10:46:51 +02002172 !try_wait_for_completion(&old_plane_state->commit->flip_done)) {
2173 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] busy with a previous commit\n",
2174 plane->base.id, plane->name);
2175
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002176 return -EBUSY;
Daniel Vetter9073d4e2020-09-25 10:46:51 +02002177 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002178
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01002179 /* Always track planes explicitly for async pageflip support. */
Maarten Lankhorst4edd6082017-10-17 07:20:47 +02002180 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002181 if (!commit)
2182 return -ENOMEM;
2183
2184 new_plane_state->commit = drm_crtc_commit_get(commit);
2185 }
2186
Maxime Ripardddadd402020-12-04 16:11:32 +01002187 if (funcs && funcs->atomic_commit_setup)
2188 return funcs->atomic_commit_setup(state);
2189
Daniel Vettera095caa2016-06-08 17:15:36 +02002190 return 0;
2191}
2192EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2193
Daniel Vettera095caa2016-06-08 17:15:36 +02002194/**
2195 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002196 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002197 *
2198 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002199 * @old_state to both be committed to the hardware (as signalled by
Daniel Vetter0380c682019-12-04 11:00:11 +01002200 * drm_atomic_helper_commit_hw_done()) and executed by the hardware (as signalled
Thierry Reding277b09c2017-10-12 16:08:57 +02002201 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02002202 *
2203 * This is part of the atomic helper support for nonblocking commits, see
2204 * drm_atomic_helper_setup_commit() for an overview.
2205 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002206void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002207{
2208 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002209 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002210 struct drm_plane *plane;
2211 struct drm_plane_state *old_plane_state;
2212 struct drm_connector *conn;
2213 struct drm_connector_state *old_conn_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002214 int i;
2215 long ret;
2216
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002217 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maxime Ripardb99c2c92021-01-11 09:44:01 +01002218 ret = drm_crtc_commit_wait(old_crtc_state->commit);
2219 if (ret)
2220 DRM_ERROR("[CRTC:%d:%s] commit wait timed out\n",
Daniel Vettera095caa2016-06-08 17:15:36 +02002221 crtc->base.id, crtc->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002222 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002223
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002224 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
Maxime Ripardb99c2c92021-01-11 09:44:01 +01002225 ret = drm_crtc_commit_wait(old_conn_state->commit);
2226 if (ret)
2227 DRM_ERROR("[CONNECTOR:%d:%s] commit wait timed out\n",
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002228 conn->base.id, conn->name);
2229 }
2230
2231 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
Maxime Ripardb99c2c92021-01-11 09:44:01 +01002232 ret = drm_crtc_commit_wait(old_plane_state->commit);
2233 if (ret)
2234 DRM_ERROR("[PLANE:%d:%s] commit wait timed out\n",
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002235 plane->base.id, plane->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002236 }
2237}
2238EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2239
2240/**
Boris Brezillonb25c60a2018-07-03 09:50:19 +02002241 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2242 * @old_state: atomic state object with old state structures
2243 *
Thierry Redingdbe2d2b2019-12-06 14:53:35 +01002244 * This function walks all CRTCs and fakes VBLANK events on those with
Boris Brezillonb25c60a2018-07-03 09:50:19 +02002245 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2246 * The primary use of this function is writeback connectors working in oneshot
2247 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2248 * when a job is queued, and any change to the pipeline that does not touch the
2249 * connector is leading to timeouts when calling
2250 * drm_atomic_helper_wait_for_vblanks() or
Thomas Zimmermann7beb6912020-01-29 13:05:17 +01002251 * drm_atomic_helper_wait_for_flip_done(). In addition to writeback
2252 * connectors, this function can also fake VBLANK events for CRTCs without
2253 * VBLANK interrupt.
Boris Brezillonb25c60a2018-07-03 09:50:19 +02002254 *
2255 * This is part of the atomic helper support for nonblocking commits, see
2256 * drm_atomic_helper_setup_commit() for an overview.
2257 */
2258void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2259{
2260 struct drm_crtc_state *new_crtc_state;
2261 struct drm_crtc *crtc;
2262 int i;
2263
2264 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2265 unsigned long flags;
2266
2267 if (!new_crtc_state->no_vblank)
2268 continue;
2269
2270 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2271 if (new_crtc_state->event) {
2272 drm_crtc_send_vblank_event(crtc,
2273 new_crtc_state->event);
2274 new_crtc_state->event = NULL;
2275 }
2276 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2277 }
2278}
2279EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2280
2281/**
Daniel Vettera095caa2016-06-08 17:15:36 +02002282 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002283 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002284 *
2285 * This function is used to signal completion of the hardware commit step. After
2286 * this step the driver is not allowed to read or change any permanent software
2287 * or hardware modeset state. The only exception is state protected by other
2288 * means than &drm_modeset_lock locks.
2289 *
2290 * Drivers should try to postpone any expensive or delayed cleanup work after
2291 * this function is called.
2292 *
2293 * This is part of the atomic helper support for nonblocking commits, see
2294 * drm_atomic_helper_setup_commit() for an overview.
2295 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002296void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002297{
2298 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002299 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002300 struct drm_crtc_commit *commit;
2301 int i;
2302
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002303 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2304 commit = new_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002305 if (!commit)
2306 continue;
2307
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002308 /*
2309 * copy new_crtc_state->commit to old_crtc_state->commit,
2310 * it's unsafe to touch new_crtc_state after hw_done,
2311 * but we still need to do so in cleanup_done().
2312 */
2313 if (old_crtc_state->commit)
2314 drm_crtc_commit_put(old_crtc_state->commit);
2315
2316 old_crtc_state->commit = drm_crtc_commit_get(commit);
2317
Daniel Vettera095caa2016-06-08 17:15:36 +02002318 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002319 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02002320 complete_all(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002321 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002322
2323 if (old_state->fake_commit) {
2324 complete_all(&old_state->fake_commit->hw_done);
2325 complete_all(&old_state->fake_commit->flip_done);
2326 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002327}
2328EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2329
2330/**
2331 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002332 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002333 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002334 * This signals completion of the atomic update @old_state, including any
2335 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01002336 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02002337 *
2338 * This is part of the atomic helper support for nonblocking commits, see
2339 * drm_atomic_helper_setup_commit() for an overview.
2340 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002341void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002342{
2343 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002344 struct drm_crtc_state *old_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002345 struct drm_crtc_commit *commit;
2346 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02002347
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002348 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2349 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002350 if (WARN_ON(!commit))
2351 continue;
2352
Daniel Vettera095caa2016-06-08 17:15:36 +02002353 complete_all(&commit->cleanup_done);
2354 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2355
Daniel Vetter7141fd32017-06-21 11:16:27 +02002356 spin_lock(&crtc->commit_lock);
Daniel Vettera095caa2016-06-08 17:15:36 +02002357 list_del(&commit->commit_entry);
2358 spin_unlock(&crtc->commit_lock);
2359 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002360
Ville Syrjälä10a599f2018-11-22 16:34:12 +02002361 if (old_state->fake_commit) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002362 complete_all(&old_state->fake_commit->cleanup_done);
Ville Syrjälä10a599f2018-11-22 16:34:12 +02002363 WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2364 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002365}
2366EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2367
Daniel Vettere8c833a2014-07-27 18:30:19 +02002368/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002369 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002370 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002371 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002372 *
2373 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002374 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2375 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2376 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002377 *
2378 * Returns:
2379 * 0 on success, negative error code on failure.
2380 */
2381int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2382 struct drm_atomic_state *state)
2383{
Laurent Pinchart9d2230d2019-02-21 03:01:38 +02002384 struct drm_connector *connector;
2385 struct drm_connector_state *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002386 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002387 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002388 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002389
Laurent Pinchart9d2230d2019-02-21 03:01:38 +02002390 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2391 if (!new_conn_state->writeback_job)
2392 continue;
2393
2394 ret = drm_writeback_prepare_job(new_conn_state->writeback_job);
2395 if (ret < 0)
2396 return ret;
2397 }
2398
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002399 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002400 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002401
2402 funcs = plane->helper_private;
2403
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002404 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002405 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002406 if (ret)
2407 goto fail;
2408 }
2409 }
2410
2411 return 0;
2412
2413fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002414 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002415 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002416
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002417 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002418 continue;
2419
2420 funcs = plane->helper_private;
2421
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002422 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002423 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002424 }
2425
2426 return ret;
2427}
2428EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2429
Ville Syrjälä7135ac52016-09-19 16:33:42 +03002430static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002431{
2432 return state->crtc && state->crtc->state->active;
2433}
2434
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002435/**
2436 * drm_atomic_helper_commit_planes - commit plane state
2437 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002438 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08002439 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002440 *
2441 * This function commits the new plane state using the plane and atomic helper
Thierry Reding42240c92019-12-06 14:53:36 +01002442 * functions for planes and CRTCs. It assumes that the atomic state has already
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002443 * been pushed into the relevant object state pointers, since this step can no
2444 * longer fail.
2445 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002446 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002447 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002448 *
2449 * Note that this function does all plane updates across all CRTCs in one step.
2450 * If the hardware can't support this approach look at
2451 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002452 *
2453 * Plane parameters can be updated by applications while the associated CRTC is
2454 * disabled. The DRM/KMS core will store the parameters in the plane state,
2455 * which will be available to the driver when the CRTC is turned on. As a result
2456 * most drivers don't need to be immediately notified of plane updates for a
2457 * disabled CRTC.
2458 *
Liu Ying2b58e982016-08-29 17:12:03 +08002459 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2460 * @flags in order not to receive plane update notifications related to a
2461 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002462 * driver code when the driver and/or hardware can't or just don't need to deal
2463 * with updates on disabled CRTCs, for example when supporting runtime PM.
2464 *
Liu Ying2b58e982016-08-29 17:12:03 +08002465 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2466 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002467 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2468 * call for a plane if the CRTC of the old plane state needs a modesetting
2469 * operation. Of course, the drivers need to disable the planes in their CRTC
2470 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08002471 *
2472 * The drm_atomic_helper_commit() default implementation doesn't set the
2473 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2474 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002475 */
2476void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002477 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08002478 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002479{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002480 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002481 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002482 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002483 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002484 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08002485 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2486 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002487
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002488 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 +02002489 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002490
2491 funcs = crtc->helper_private;
2492
2493 if (!funcs || !funcs->atomic_begin)
2494 continue;
2495
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002496 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002497 continue;
2498
Maxime Ripardf6ebe9f2020-10-28 13:32:22 +01002499 funcs->atomic_begin(crtc, old_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002500 }
2501
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002502 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 +02002503 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002504 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002505
2506 funcs = plane->helper_private;
2507
Thierry Reding3cad4b62014-11-25 13:05:12 +01002508 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002509 continue;
2510
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01002511 disabling = drm_atomic_plane_disabling(old_plane_state,
2512 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002513
2514 if (active_only) {
2515 /*
2516 * Skip planes related to inactive CRTCs. If the plane
2517 * is enabled use the state of the current CRTC. If the
2518 * plane is being disabled use the state of the old
2519 * CRTC to avoid skipping planes being disabled on an
2520 * active CRTC.
2521 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002522 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002523 continue;
2524 if (disabling && !plane_crtc_active(old_plane_state))
2525 continue;
2526 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002527
Thierry Reding407b8bd2014-11-20 12:05:50 +01002528 /*
2529 * Special-case disabling the plane if drivers support it.
2530 */
Liu Ying2b58e982016-08-29 17:12:03 +08002531 if (disabling && funcs->atomic_disable) {
2532 struct drm_crtc_state *crtc_state;
2533
2534 crtc_state = old_plane_state->crtc->state;
2535
2536 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2537 no_disable)
2538 continue;
2539
Maxime Ripard977697e2021-02-19 13:00:29 +01002540 funcs->atomic_disable(plane, old_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002541 } else if (new_plane_state->crtc || disabling) {
Maxime Ripard977697e2021-02-19 13:00:29 +01002542 funcs->atomic_update(plane, old_state);
Liu Ying2b58e982016-08-29 17:12:03 +08002543 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002544 }
2545
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002546 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 +02002547 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002548
2549 funcs = crtc->helper_private;
2550
2551 if (!funcs || !funcs->atomic_flush)
2552 continue;
2553
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002554 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002555 continue;
2556
Maxime Ripardf6ebe9f2020-10-28 13:32:22 +01002557 funcs->atomic_flush(crtc, old_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002558 }
2559}
2560EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2561
2562/**
Thierry Reding42240c92019-12-06 14:53:36 +01002563 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a CRTC
2564 * @old_crtc_state: atomic state object with the old CRTC state
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002565 *
2566 * This function commits the new plane state using the plane and atomic helper
Thierry Reding42240c92019-12-06 14:53:36 +01002567 * functions for planes on the specific CRTC. It assumes that the atomic state
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002568 * has already been pushed into the relevant object state pointers, since this
2569 * step can no longer fail.
2570 *
Thierry Reding42240c92019-12-06 14:53:36 +01002571 * This function is useful when plane updates should be done CRTC-by-CRTC
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002572 * instead of one global step like drm_atomic_helper_commit_planes() does.
2573 *
2574 * This function can only be savely used when planes are not allowed to move
2575 * between different CRTCs because this function doesn't handle inter-CRTC
2576 * depencies. Callers need to ensure that either no such depencies exist,
2577 * resolve them through ordering of commit calls or through some other means.
2578 */
2579void
2580drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2581{
2582 const struct drm_crtc_helper_funcs *crtc_funcs;
2583 struct drm_crtc *crtc = old_crtc_state->crtc;
2584 struct drm_atomic_state *old_state = old_crtc_state->state;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002585 struct drm_crtc_state *new_crtc_state =
2586 drm_atomic_get_new_crtc_state(old_state, crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002587 struct drm_plane *plane;
Fabio M. De Francesco9237ec12021-04-12 14:42:14 +02002588 unsigned int plane_mask;
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002589
2590 plane_mask = old_crtc_state->plane_mask;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002591 plane_mask |= new_crtc_state->plane_mask;
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002592
2593 crtc_funcs = crtc->helper_private;
2594 if (crtc_funcs && crtc_funcs->atomic_begin)
Maxime Ripardf6ebe9f2020-10-28 13:32:22 +01002595 crtc_funcs->atomic_begin(crtc, old_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002596
2597 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2598 struct drm_plane_state *old_plane_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01002599 drm_atomic_get_old_plane_state(old_state, plane);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002600 struct drm_plane_state *new_plane_state =
2601 drm_atomic_get_new_plane_state(old_state, plane);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002602 const struct drm_plane_helper_funcs *plane_funcs;
2603
2604 plane_funcs = plane->helper_private;
2605
2606 if (!old_plane_state || !plane_funcs)
2607 continue;
2608
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002609 WARN_ON(new_plane_state->crtc &&
2610 new_plane_state->crtc != crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002611
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002612 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002613 plane_funcs->atomic_disable)
Maxime Ripard977697e2021-02-19 13:00:29 +01002614 plane_funcs->atomic_disable(plane, old_state);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002615 else if (new_plane_state->crtc ||
2616 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
Maxime Ripard977697e2021-02-19 13:00:29 +01002617 plane_funcs->atomic_update(plane, old_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002618 }
2619
2620 if (crtc_funcs && crtc_funcs->atomic_flush)
Maxime Ripardf6ebe9f2020-10-28 13:32:22 +01002621 crtc_funcs->atomic_flush(crtc, old_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002622}
2623EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2624
2625/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02002626 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08002627 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02002628 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2629 *
2630 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08002631 * used for instance in the CRTC helper atomic_disable callback to disable
2632 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002633 *
2634 * If the atomic-parameter is set the function calls the CRTC's
2635 * atomic_begin hook before and atomic_flush hook after disabling the
2636 * planes.
2637 *
2638 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002639 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002640 */
Liu Ying28500292016-08-26 15:30:39 +08002641void
2642drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2643 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002644{
Liu Ying28500292016-08-26 15:30:39 +08002645 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02002646 const struct drm_crtc_helper_funcs *crtc_funcs =
2647 crtc->helper_private;
2648 struct drm_plane *plane;
2649
2650 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2651 crtc_funcs->atomic_begin(crtc, NULL);
2652
Liu Ying28500292016-08-26 15:30:39 +08002653 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02002654 const struct drm_plane_helper_funcs *plane_funcs =
2655 plane->helper_private;
2656
Liu Ying28500292016-08-26 15:30:39 +08002657 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002658 continue;
2659
2660 WARN_ON(!plane_funcs->atomic_disable);
2661 if (plane_funcs->atomic_disable)
2662 plane_funcs->atomic_disable(plane, NULL);
2663 }
2664
2665 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2666 crtc_funcs->atomic_flush(crtc, NULL);
2667}
2668EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2669
2670/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002671 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2672 * @dev: DRM device
2673 * @old_state: atomic state object with old state structures
2674 *
2675 * This function cleans up plane state, specifically framebuffers, from the old
2676 * configuration. Hence the old configuration must be perserved in @old_state to
2677 * be able to call this function.
2678 *
2679 * This function must also be called on the new state when the atomic update
2680 * fails at any point after calling drm_atomic_helper_prepare_planes().
2681 */
2682void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2683 struct drm_atomic_state *old_state)
2684{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002685 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002686 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002687 int i;
2688
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002689 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 +02002690 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002691 struct drm_plane_state *plane_state;
2692
2693 /*
2694 * This might be called before swapping when commit is aborted,
2695 * in which case we have to cleanup the new state.
2696 */
2697 if (old_plane_state == plane->state)
2698 plane_state = new_plane_state;
2699 else
2700 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002701
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002702 funcs = plane->helper_private;
2703
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002704 if (funcs->cleanup_fb)
2705 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002706 }
2707}
2708EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2709
2710/**
2711 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002712 * @state: atomic state
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002713 * @stall: stall for preceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002714 *
2715 * This function stores the atomic state into the current state pointers in all
2716 * driver objects. It should be called after all failing steps have been done
2717 * and succeeded, but before the actual hardware state is committed.
2718 *
2719 * For cleanup and error recovery the current state for all changed objects will
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002720 * be swapped into @state.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002721 *
2722 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2723 *
2724 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2725 *
2726 * 2. Do any other steps that might fail.
2727 *
2728 * 3. Put the staged state into the current state pointers with this function.
2729 *
2730 * 4. Actually commit the hardware state.
2731 *
Daniel Vetter26196f72015-08-25 16:26:03 +02002732 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002733 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02002734 *
2735 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002736 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2737 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02002738 * don't pass the right state structures to the callbacks.
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002739 *
2740 * Returns:
2741 *
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002742 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2743 * waiting for the previous commits has been interrupted.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002744 */
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002745int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
Daniel Vetter5e84c262016-06-10 00:06:32 +02002746 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002747{
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002748 int i, ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002749 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002750 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002751 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002752 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002753 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002754 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002755 struct drm_crtc_commit *commit;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002756 struct drm_private_obj *obj;
2757 struct drm_private_state *old_obj_state, *new_obj_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002758
2759 if (stall) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002760 /*
2761 * We have to stall for hw_done here before
2762 * drm_atomic_helper_wait_for_dependencies() because flip
2763 * depth > 1 is not yet supported by all drivers. As long as
2764 * obj->state is directly dereferenced anywhere in the drivers
2765 * atomic_commit_tail function, then it's unsafe to swap state
2766 * before drm_atomic_helper_commit_hw_done() is called.
2767 */
2768
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002769 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2770 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002771
2772 if (!commit)
2773 continue;
2774
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002775 ret = wait_for_completion_interruptible(&commit->hw_done);
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002776 if (ret)
2777 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002778 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002779
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002780 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2781 commit = old_conn_state->commit;
2782
2783 if (!commit)
2784 continue;
2785
2786 ret = wait_for_completion_interruptible(&commit->hw_done);
2787 if (ret)
2788 return ret;
2789 }
2790
2791 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2792 commit = old_plane_state->commit;
2793
2794 if (!commit)
2795 continue;
2796
2797 ret = wait_for_completion_interruptible(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002798 if (ret)
2799 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002800 }
2801 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002802
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002803 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002804 WARN_ON(connector->state != old_conn_state);
2805
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002806 old_conn_state->state = state;
2807 new_conn_state->state = NULL;
2808
2809 state->connectors[i].state = old_conn_state;
2810 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002811 }
2812
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002813 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002814 WARN_ON(crtc->state != old_crtc_state);
2815
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002816 old_crtc_state->state = state;
2817 new_crtc_state->state = NULL;
2818
2819 state->crtcs[i].state = old_crtc_state;
2820 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002821
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002822 if (new_crtc_state->commit) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002823 spin_lock(&crtc->commit_lock);
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002824 list_add(&new_crtc_state->commit->commit_entry,
Daniel Vettera095caa2016-06-08 17:15:36 +02002825 &crtc->commit_list);
2826 spin_unlock(&crtc->commit_lock);
2827
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002828 new_crtc_state->commit->event = NULL;
Daniel Vettera095caa2016-06-08 17:15:36 +02002829 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002830 }
2831
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002832 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002833 WARN_ON(plane->state != old_plane_state);
2834
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002835 old_plane_state->state = state;
2836 new_plane_state->state = NULL;
2837
2838 state->planes[i].state = old_plane_state;
2839 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002840 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07002841
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002842 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2843 WARN_ON(obj->state != old_obj_state);
2844
2845 old_obj_state->state = state;
2846 new_obj_state->state = NULL;
2847
2848 state->private_objs[i].state = old_obj_state;
2849 obj->state = new_obj_state;
2850 }
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002851
2852 return 0;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002853}
2854EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002855
2856/**
2857 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2858 * @plane: plane object to update
2859 * @crtc: owning CRTC of owning plane
2860 * @fb: framebuffer to flip onto plane
Thierry Reding42240c92019-12-06 14:53:36 +01002861 * @crtc_x: x offset of primary plane on @crtc
2862 * @crtc_y: y offset of primary plane on @crtc
2863 * @crtc_w: width of primary plane rectangle on @crtc
2864 * @crtc_h: height of primary plane rectangle on @crtc
Daniel Vetter042652e2014-07-27 13:46:52 +02002865 * @src_x: x offset of @fb for panning
2866 * @src_y: y offset of @fb for panning
2867 * @src_w: width of source rectangle in @fb
2868 * @src_h: height of source rectangle in @fb
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002869 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002870 *
2871 * Provides a default plane update handler using the atomic driver interface.
2872 *
2873 * RETURNS:
2874 * Zero on success, error code on failure
2875 */
2876int drm_atomic_helper_update_plane(struct drm_plane *plane,
2877 struct drm_crtc *crtc,
2878 struct drm_framebuffer *fb,
2879 int crtc_x, int crtc_y,
2880 unsigned int crtc_w, unsigned int crtc_h,
2881 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002882 uint32_t src_w, uint32_t src_h,
2883 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002884{
2885 struct drm_atomic_state *state;
2886 struct drm_plane_state *plane_state;
2887 int ret = 0;
2888
2889 state = drm_atomic_state_alloc(plane->dev);
2890 if (!state)
2891 return -ENOMEM;
2892
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002893 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002894 plane_state = drm_atomic_get_plane_state(state, plane);
2895 if (IS_ERR(plane_state)) {
2896 ret = PTR_ERR(plane_state);
2897 goto fail;
2898 }
2899
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002900 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002901 if (ret != 0)
2902 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002903 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002904 plane_state->crtc_x = crtc_x;
2905 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002906 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002907 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002908 plane_state->src_x = src_x;
2909 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002910 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002911 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002912
Daniel Vetter3671c582015-05-04 15:40:52 +02002913 if (plane == crtc->cursor)
2914 state->legacy_cursor_update = true;
2915
Daniel Vetter042652e2014-07-27 13:46:52 +02002916 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002917fail:
Chris Wilson08536952016-10-14 13:18:18 +01002918 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002919 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002920}
2921EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2922
2923/**
2924 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2925 * @plane: plane to disable
Daniel Vetter19315292017-03-22 22:50:43 +01002926 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002927 *
2928 * Provides a default plane disable handler using the atomic driver interface.
2929 *
2930 * RETURNS:
2931 * Zero on success, error code on failure
2932 */
Daniel Vetter19315292017-03-22 22:50:43 +01002933int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2934 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002935{
2936 struct drm_atomic_state *state;
2937 struct drm_plane_state *plane_state;
2938 int ret = 0;
2939
2940 state = drm_atomic_state_alloc(plane->dev);
2941 if (!state)
2942 return -ENOMEM;
2943
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002944 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002945 plane_state = drm_atomic_get_plane_state(state, plane);
2946 if (IS_ERR(plane_state)) {
2947 ret = PTR_ERR(plane_state);
2948 goto fail;
2949 }
2950
Ville Syrjäläa36c0272018-03-22 17:22:58 +02002951 if (plane_state->crtc && plane_state->crtc->cursor == plane)
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002952 plane_state->state->legacy_cursor_update = true;
2953
Rob Clarkbbb1e522015-08-25 15:35:58 -04002954 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002955 if (ret != 0)
2956 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002957
Daniel Vetter042652e2014-07-27 13:46:52 +02002958 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002959fail:
Chris Wilson08536952016-10-14 13:18:18 +01002960 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002961 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002962}
2963EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2964
Daniel Vetter042652e2014-07-27 13:46:52 +02002965/**
2966 * drm_atomic_helper_set_config - set a new config from userspace
2967 * @set: mode set configuration
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002968 * @ctx: lock acquisition context
Daniel Vetter042652e2014-07-27 13:46:52 +02002969 *
Thierry Reding42240c92019-12-06 14:53:36 +01002970 * Provides a default CRTC set_config handler using the atomic driver interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002971 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002972 * NOTE: For backwards compatibility with old userspace this automatically
2973 * resets the "link-status" property to GOOD, to force any link
2974 * re-training. The SETCRTC ioctl does not define whether an update does
2975 * need a full modeset or just a plane update, hence we're allowed to do
Daniel Vetter97e14fb2018-07-09 10:40:08 +02002976 * that. See also drm_connector_set_link_status_property().
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002977 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002978 * Returns:
2979 * Returns 0 on success, negative errno numbers on failure.
2980 */
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002981int drm_atomic_helper_set_config(struct drm_mode_set *set,
2982 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002983{
2984 struct drm_atomic_state *state;
2985 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002986 int ret = 0;
2987
2988 state = drm_atomic_state_alloc(crtc->dev);
2989 if (!state)
2990 return -ENOMEM;
2991
Daniel Vetter38b64412017-03-22 22:50:58 +01002992 state->acquire_ctx = ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002993 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002994 if (ret != 0)
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002995 goto fail;
Daniel Stone819364d2015-05-26 14:36:48 +01002996
Maarten Lankhorst44596b82017-04-06 13:19:00 +02002997 ret = handle_conflicting_encoders(state, true);
2998 if (ret)
Pan Bian43b67302021-01-19 04:11:27 -08002999 goto fail;
Maarten Lankhorst44596b82017-04-06 13:19:00 +02003000
Daniel Vetter042652e2014-07-27 13:46:52 +02003001 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02003002
Daniel Vetter1fa4da02017-03-29 19:41:36 +02003003fail:
Chris Wilson08536952016-10-14 13:18:18 +01003004 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02003005 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02003006}
3007EXPORT_SYMBOL(drm_atomic_helper_set_config);
3008
Sean Paul37406a62019-02-12 12:32:41 -05003009/**
3010 * drm_atomic_helper_disable_all - disable all currently active outputs
3011 * @dev: DRM device
3012 * @ctx: lock acquisition context
3013 *
3014 * Loops through all connectors, finding those that aren't turned off and then
3015 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3016 * that they are connected to.
3017 *
3018 * This is used for example in suspend/resume to disable all currently active
3019 * functions when suspending. If you just want to shut down everything at e.g.
3020 * driver unload, look at drm_atomic_helper_shutdown().
3021 *
3022 * Note that if callers haven't already acquired all modeset locks this might
3023 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3024 *
3025 * Returns:
3026 * 0 on success or a negative error code on failure.
3027 *
3028 * See also:
3029 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3030 * drm_atomic_helper_shutdown().
3031 */
3032int drm_atomic_helper_disable_all(struct drm_device *dev,
3033 struct drm_modeset_acquire_ctx *ctx)
Thierry Reding14942762015-12-02 17:50:04 +01003034{
3035 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003036 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01003037 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003038 struct drm_plane_state *plane_state;
3039 struct drm_plane *plane;
3040 struct drm_crtc_state *crtc_state;
3041 struct drm_crtc *crtc;
3042 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01003043
3044 state = drm_atomic_state_alloc(dev);
3045 if (!state)
3046 return -ENOMEM;
3047
3048 state->acquire_ctx = ctx;
3049
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003050 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01003051 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3052 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003053 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01003054 goto free;
3055 }
3056
3057 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003058
3059 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3060 if (ret < 0)
3061 goto free;
3062
3063 ret = drm_atomic_add_affected_planes(state, crtc);
3064 if (ret < 0)
3065 goto free;
3066
3067 ret = drm_atomic_add_affected_connectors(state, crtc);
3068 if (ret < 0)
3069 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01003070 }
3071
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02003072 for_each_new_connector_in_state(state, conn, conn_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003073 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3074 if (ret < 0)
3075 goto free;
3076 }
3077
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02003078 for_each_new_plane_in_state(state, plane, plane_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003079 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3080 if (ret < 0)
3081 goto free;
3082
3083 drm_atomic_set_fb_for_plane(plane_state, NULL);
3084 }
3085
3086 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01003087free:
Chris Wilson08536952016-10-14 13:18:18 +01003088 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003089 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01003090}
3091EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3092
3093/**
Daniel Vetter18dddad2017-03-21 17:41:49 +01003094 * drm_atomic_helper_shutdown - shutdown all CRTC
3095 * @dev: DRM device
3096 *
3097 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3098 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3099 * that also takes a snapshot of the modeset state to be restored on resume.
3100 *
3101 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3102 * and it is the atomic version of drm_crtc_force_disable_all().
3103 */
3104void drm_atomic_helper_shutdown(struct drm_device *dev)
3105{
3106 struct drm_modeset_acquire_ctx ctx;
3107 int ret;
3108
Sean Paulb7ea04d2018-11-29 10:04:17 -05003109 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003110
Sean Paul37406a62019-02-12 12:32:41 -05003111 ret = drm_atomic_helper_disable_all(dev, &ctx);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003112 if (ret)
3113 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3114
Daniel Vetter77ef3852020-08-14 11:38:42 +02003115 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003116}
3117EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3118
3119/**
Daniel Vetter1d8224e2018-11-28 11:07:28 +01003120 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3121 * @dev: DRM device
3122 * @ctx: lock acquisition context
3123 *
3124 * Makes a copy of the current atomic state by looping over all objects and
3125 * duplicating their respective states. This is used for example by suspend/
3126 * resume support code to save the state prior to suspend such that it can
3127 * be restored upon resume.
3128 *
3129 * Note that this treats atomic state as persistent between save and restore.
3130 * Drivers must make sure that this is possible and won't result in confusion
3131 * or erroneous behaviour.
3132 *
3133 * Note that if callers haven't already acquired all modeset locks this might
3134 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3135 *
3136 * Returns:
3137 * A pointer to the copy of the atomic state object on success or an
3138 * ERR_PTR()-encoded error code on failure.
3139 *
3140 * See also:
3141 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3142 */
3143struct drm_atomic_state *
3144drm_atomic_helper_duplicate_state(struct drm_device *dev,
3145 struct drm_modeset_acquire_ctx *ctx)
3146{
3147 struct drm_atomic_state *state;
3148 struct drm_connector *conn;
3149 struct drm_connector_list_iter conn_iter;
3150 struct drm_plane *plane;
3151 struct drm_crtc *crtc;
3152 int err = 0;
3153
3154 state = drm_atomic_state_alloc(dev);
3155 if (!state)
3156 return ERR_PTR(-ENOMEM);
3157
3158 state->acquire_ctx = ctx;
Lyude Paul022deba2019-02-01 19:20:03 -05003159 state->duplicated = true;
Daniel Vetter1d8224e2018-11-28 11:07:28 +01003160
3161 drm_for_each_crtc(crtc, dev) {
3162 struct drm_crtc_state *crtc_state;
3163
3164 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3165 if (IS_ERR(crtc_state)) {
3166 err = PTR_ERR(crtc_state);
3167 goto free;
3168 }
3169 }
3170
3171 drm_for_each_plane(plane, dev) {
3172 struct drm_plane_state *plane_state;
3173
3174 plane_state = drm_atomic_get_plane_state(state, plane);
3175 if (IS_ERR(plane_state)) {
3176 err = PTR_ERR(plane_state);
3177 goto free;
3178 }
3179 }
3180
3181 drm_connector_list_iter_begin(dev, &conn_iter);
3182 drm_for_each_connector_iter(conn, &conn_iter) {
3183 struct drm_connector_state *conn_state;
3184
3185 conn_state = drm_atomic_get_connector_state(state, conn);
3186 if (IS_ERR(conn_state)) {
3187 err = PTR_ERR(conn_state);
3188 drm_connector_list_iter_end(&conn_iter);
3189 goto free;
3190 }
3191 }
3192 drm_connector_list_iter_end(&conn_iter);
3193
3194 /* clear the acquire context so that it isn't accidentally reused */
3195 state->acquire_ctx = NULL;
3196
3197free:
3198 if (err < 0) {
3199 drm_atomic_state_put(state);
3200 state = ERR_PTR(err);
3201 }
3202
3203 return state;
3204}
3205EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3206
3207/**
Thierry Reding14942762015-12-02 17:50:04 +01003208 * drm_atomic_helper_suspend - subsystem-level suspend helper
3209 * @dev: DRM device
3210 *
3211 * Duplicates the current atomic state, disables all active outputs and then
3212 * returns a pointer to the original atomic state to the caller. Drivers can
3213 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3214 * restore the output configuration that was active at the time the system
3215 * entered suspend.
3216 *
3217 * Note that it is potentially unsafe to use this. The atomic state object
3218 * returned by this function is assumed to be persistent. Drivers must ensure
3219 * that this holds true. Before calling this function, drivers must make sure
3220 * to suspend fbdev emulation so that nothing can be using the device.
3221 *
3222 * Returns:
3223 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3224 * encoded error code on failure. Drivers should store the returned atomic
3225 * state object and pass it to the drm_atomic_helper_resume() helper upon
3226 * resume.
3227 *
3228 * See also:
3229 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003230 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01003231 */
3232struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3233{
3234 struct drm_modeset_acquire_ctx ctx;
3235 struct drm_atomic_state *state;
3236 int err;
3237
Sean Paul615aa3d2018-11-29 15:36:48 -05003238 /* This can never be returned, but it makes the compiler happy */
3239 state = ERR_PTR(-EINVAL);
Thierry Reding14942762015-12-02 17:50:04 +01003240
Sean Paulb7ea04d2018-11-29 10:04:17 -05003241 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
Thierry Reding14942762015-12-02 17:50:04 +01003242
3243 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3244 if (IS_ERR(state))
3245 goto unlock;
3246
3247 err = drm_atomic_helper_disable_all(dev, &ctx);
3248 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003249 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01003250 state = ERR_PTR(err);
3251 goto unlock;
3252 }
3253
3254unlock:
Daniel Vetter77ef3852020-08-14 11:38:42 +02003255 DRM_MODESET_LOCK_ALL_END(dev, ctx, err);
Sean Paulb7ea04d2018-11-29 10:04:17 -05003256 if (err)
3257 return ERR_PTR(err);
Thierry Reding14942762015-12-02 17:50:04 +01003258
Thierry Reding14942762015-12-02 17:50:04 +01003259 return state;
3260}
3261EXPORT_SYMBOL(drm_atomic_helper_suspend);
3262
3263/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003264 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3265 * @state: duplicated atomic state to commit
3266 * @ctx: pointer to acquire_ctx to use for commit.
3267 *
3268 * The state returned by drm_atomic_helper_duplicate_state() and
3269 * drm_atomic_helper_suspend() is partially invalid, and needs to
3270 * be fixed up before commit.
3271 *
3272 * Returns:
3273 * 0 on success or a negative error code on failure.
3274 *
3275 * See also:
3276 * drm_atomic_helper_suspend()
3277 */
3278int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3279 struct drm_modeset_acquire_ctx *ctx)
3280{
Sean Paulaa394b02018-11-29 10:04:14 -05003281 int i, ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003282 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003283 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003284 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003285 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003286 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003287 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003288
3289 state->acquire_ctx = ctx;
3290
Ville Syrjäläe00fb852018-05-25 21:50:45 +03003291 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003292 state->planes[i].old_state = plane->state;
3293
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003294 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003295 state->crtcs[i].old_state = crtc->state;
3296
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003297 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003298 state->connectors[i].old_state = connector->state;
3299
Sean Paulaa394b02018-11-29 10:04:14 -05003300 ret = drm_atomic_commit(state);
3301
3302 state->acquire_ctx = NULL;
3303
3304 return ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003305}
3306EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3307
3308/**
Thierry Reding14942762015-12-02 17:50:04 +01003309 * drm_atomic_helper_resume - subsystem-level resume helper
3310 * @dev: DRM device
3311 * @state: atomic state to resume to
3312 *
3313 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3314 * grabs all modeset locks and commits the atomic state object. This can be
3315 * used in conjunction with the drm_atomic_helper_suspend() helper to
3316 * implement suspend/resume for drivers that support atomic mode-setting.
3317 *
3318 * Returns:
3319 * 0 on success or a negative error code on failure.
3320 *
3321 * See also:
3322 * drm_atomic_helper_suspend()
3323 */
3324int drm_atomic_helper_resume(struct drm_device *dev,
3325 struct drm_atomic_state *state)
3326{
Daniel Vettera5b84442017-04-03 10:32:53 +02003327 struct drm_modeset_acquire_ctx ctx;
Thierry Reding14942762015-12-02 17:50:04 +01003328 int err;
3329
3330 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003331
Sean Paulb7ea04d2018-11-29 10:04:17 -05003332 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
Daniel Vetter869e1882017-05-31 10:38:13 +02003333
Sean Paulb7ea04d2018-11-29 10:04:17 -05003334 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
Daniel Vettera5b84442017-04-03 10:32:53 +02003335
Daniel Vetter77ef3852020-08-14 11:38:42 +02003336 DRM_MODESET_LOCK_ALL_END(dev, ctx, err);
Jeffy Chen6d281b12017-10-09 14:46:41 +08003337 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01003338
3339 return err;
3340}
3341EXPORT_SYMBOL(drm_atomic_helper_resume);
3342
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003343static int page_flip_common(struct drm_atomic_state *state,
3344 struct drm_crtc *crtc,
3345 struct drm_framebuffer *fb,
3346 struct drm_pending_vblank_event *event,
3347 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003348{
3349 struct drm_plane *plane = crtc->primary;
3350 struct drm_plane_state *plane_state;
3351 struct drm_crtc_state *crtc_state;
3352 int ret = 0;
3353
3354 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3355 if (IS_ERR(crtc_state))
3356 return PTR_ERR(crtc_state);
3357
3358 crtc_state->event = event;
Daniel Vetter4d85f452019-09-03 21:06:42 +02003359 crtc_state->async_flip = flags & DRM_MODE_PAGE_FLIP_ASYNC;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003360
3361 plane_state = drm_atomic_get_plane_state(state, plane);
3362 if (IS_ERR(plane_state))
3363 return PTR_ERR(plane_state);
3364
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003365 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3366 if (ret != 0)
3367 return ret;
3368 drm_atomic_set_fb_for_plane(plane_state, fb);
3369
3370 /* Make sure we don't accidentally do a full modeset. */
3371 state->allow_modeset = false;
3372 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00003373 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3374 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003375 return -EINVAL;
3376 }
3377
3378 return ret;
3379}
3380
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003381/**
3382 * drm_atomic_helper_page_flip - execute a legacy page flip
Thierry Reding42240c92019-12-06 14:53:36 +01003383 * @crtc: DRM CRTC
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003384 * @fb: DRM framebuffer
3385 * @event: optional DRM event to signal upon completion
3386 * @flags: flip flags for non-vblank sync'ed updates
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003387 * @ctx: lock acquisition context
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003388 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003389 * Provides a default &drm_crtc_funcs.page_flip implementation
3390 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003391 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003392 * Returns:
3393 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003394 *
3395 * See also:
3396 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003397 */
3398int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3399 struct drm_framebuffer *fb,
3400 struct drm_pending_vblank_event *event,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003401 uint32_t flags,
3402 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003403{
3404 struct drm_plane *plane = crtc->primary;
3405 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003406 int ret = 0;
3407
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003408 state = drm_atomic_state_alloc(plane->dev);
3409 if (!state)
3410 return -ENOMEM;
3411
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003412 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003413
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003414 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003415 if (ret != 0)
3416 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01003417
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02003418 ret = drm_atomic_nonblocking_commit(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003419fail:
Chris Wilson08536952016-10-14 13:18:18 +01003420 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003421 return ret;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003422}
3423EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01003424
3425/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003426 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
Thierry Reding42240c92019-12-06 14:53:36 +01003427 * @crtc: DRM CRTC
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003428 * @fb: DRM framebuffer
3429 * @event: optional DRM event to signal upon completion
3430 * @flags: flip flags for non-vblank sync'ed updates
3431 * @target: specifying the target vblank period when the flip to take effect
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003432 * @ctx: lock acquisition context
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003433 *
3434 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3435 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3436 * target vblank period to flip.
3437 *
3438 * Returns:
3439 * Returns 0 on success, negative errno numbers on failure.
3440 */
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003441int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3442 struct drm_framebuffer *fb,
3443 struct drm_pending_vblank_event *event,
3444 uint32_t flags,
3445 uint32_t target,
3446 struct drm_modeset_acquire_ctx *ctx)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003447{
3448 struct drm_plane *plane = crtc->primary;
3449 struct drm_atomic_state *state;
3450 struct drm_crtc_state *crtc_state;
3451 int ret = 0;
3452
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003453 state = drm_atomic_state_alloc(plane->dev);
3454 if (!state)
3455 return -ENOMEM;
3456
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003457 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003458
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003459 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003460 if (ret != 0)
3461 goto fail;
3462
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01003463 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003464 if (WARN_ON(!crtc_state)) {
3465 ret = -EINVAL;
3466 goto fail;
3467 }
3468 crtc_state->target_vblank = target;
3469
3470 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003471fail:
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003472 drm_atomic_state_put(state);
3473 return ret;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003474}
3475EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
Daniel Vetter1d8224e2018-11-28 11:07:28 +01003476
3477/**
Boris Brezillonf32df582020-01-28 14:55:08 +01003478 * drm_atomic_helper_bridge_propagate_bus_fmt() - Propagate output format to
3479 * the input end of a bridge
3480 * @bridge: bridge control structure
3481 * @bridge_state: new bridge state
3482 * @crtc_state: new CRTC state
3483 * @conn_state: new connector state
3484 * @output_fmt: tested output bus format
3485 * @num_input_fmts: will contain the size of the returned array
3486 *
3487 * This helper is a pluggable implementation of the
3488 * &drm_bridge_funcs.atomic_get_input_bus_fmts operation for bridges that don't
3489 * modify the bus configuration between their input and their output. It
3490 * returns an array of input formats with a single element set to @output_fmt.
3491 *
3492 * RETURNS:
3493 * a valid format array of size @num_input_fmts, or NULL if the allocation
3494 * failed
3495 */
3496u32 *
3497drm_atomic_helper_bridge_propagate_bus_fmt(struct drm_bridge *bridge,
3498 struct drm_bridge_state *bridge_state,
3499 struct drm_crtc_state *crtc_state,
3500 struct drm_connector_state *conn_state,
3501 u32 output_fmt,
3502 unsigned int *num_input_fmts)
3503{
3504 u32 *input_fmts;
3505
3506 input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
3507 if (!input_fmts) {
3508 *num_input_fmts = 0;
3509 return NULL;
3510 }
3511
3512 *num_input_fmts = 1;
3513 input_fmts[0] = output_fmt;
3514 return input_fmts;
3515}
3516EXPORT_SYMBOL(drm_atomic_helper_bridge_propagate_bus_fmt);