blob: 93b0221d5d0fd41ae0602cfe9559683eba2365af [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
28#include <drm/drmP.h>
29#include <drm/drm_atomic.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020032#include <drm/drm_atomic_helper.h>
Chris Wilsonf54d1862016-10-25 13:00:45 +010033#include <linux/dma-fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Jose Abreufaf94a02017-05-25 15:19:16 +010035#include "drm_crtc_helper_internal.h"
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020036#include "drm_crtc_internal.h"
37
Daniel Vetter3150c7d2014-11-06 20:53:29 +010038/**
39 * DOC: overview
40 *
41 * This helper library provides implementations of check and commit functions on
42 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
43 * also provides convenience implementations for the atomic state handling
44 * callbacks for drivers which don't need to subclass the drm core structures to
45 * add their own additional internal state.
46 *
47 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020048 * drm_atomic_helper_check() and for the commit callback with
49 * drm_atomic_helper_commit(). But the individual stages and callbacks are
50 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010051 * together with a driver private modeset implementation.
52 *
53 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020054 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
55 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010056 * various functions to implement set_property callbacks. New drivers must not
57 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010058 *
59 * The atomic helper uses the same function table structures as all other
Daniel Vetterea0dd852016-12-29 21:48:26 +010060 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
61 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
62 * also shares the &struct drm_plane_helper_funcs function table with the plane
Daniel Vetter092d01d2015-12-04 09:45:44 +010063 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010064 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010065static void
66drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010067 struct drm_plane_state *old_plane_state,
Daniel Vetterc2fcd272014-11-05 00:14:14 +010068 struct drm_plane_state *plane_state,
69 struct drm_plane *plane)
70{
71 struct drm_crtc_state *crtc_state;
72
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010073 if (old_plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010074 crtc_state = drm_atomic_get_new_crtc_state(state,
75 old_plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010076
77 if (WARN_ON(!crtc_state))
78 return;
79
80 crtc_state->planes_changed = true;
81 }
82
83 if (plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010084 crtc_state = drm_atomic_get_new_crtc_state(state, 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
Maarten Lankhorst8248b652016-03-03 10:17:40 +010093static int handle_conflicting_encoders(struct drm_atomic_state *state,
94 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +010095{
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010096 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +020097 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +010098 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst40616a22016-03-03 10:17:39 +010099 struct drm_encoder *encoder;
100 unsigned encoder_mask = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100101 int i, ret = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200102
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100103 /*
104 * First loop, find all newly assigned encoders from the connectors
105 * part of the state. If the same encoder is assigned to multiple
106 * connectors bail out.
107 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100108 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100109 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
110 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200111
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100112 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200113 continue;
114
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100115 if (funcs->atomic_best_encoder)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100116 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200117 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100118 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200119 else
120 new_encoder = drm_atomic_helper_best_encoder(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100121
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100122 if (new_encoder) {
123 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
124 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
125 new_encoder->base.id, new_encoder->name,
126 connector->base.id, connector->name);
127
128 return -EINVAL;
129 }
130
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100131 encoder_mask |= 1 << drm_encoder_index(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100132 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200133 }
134
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100135 if (!encoder_mask)
136 return 0;
137
138 /*
139 * Second loop, iterate over all connectors not part of the state.
140 *
141 * If a conflicting encoder is found and disable_conflicting_encoders
142 * is not set, an error is returned. Userspace can provide a solution
143 * through the atomic ioctl.
144 *
145 * If the flag is set conflicting connectors are removed from the crtc
146 * and the crtc is disabled if no encoder is left. This preserves
147 * compatibility with the legacy set_config behavior.
148 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100149 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100150 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100151 struct drm_crtc_state *crtc_state;
152
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100153 if (drm_atomic_get_new_connector_state(state, connector))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100154 continue;
155
156 encoder = connector->state->best_encoder;
157 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
158 continue;
159
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100160 if (!disable_conflicting_encoders) {
161 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
162 encoder->base.id, encoder->name,
163 connector->state->crtc->base.id,
164 connector->state->crtc->name,
165 connector->base.id, connector->name);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100166 ret = -EINVAL;
167 goto out;
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100168 }
169
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100170 new_conn_state = drm_atomic_get_connector_state(state, connector);
171 if (IS_ERR(new_conn_state)) {
172 ret = PTR_ERR(new_conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100173 goto out;
174 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100175
176 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
177 encoder->base.id, encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100178 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100179 connector->base.id, connector->name);
180
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100181 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100182
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100183 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100184 if (ret)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100185 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100186
187 if (!crtc_state->connector_mask) {
188 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
189 NULL);
190 if (ret < 0)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100191 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100192
193 crtc_state->active = false;
194 }
195 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100196out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100197 drm_connector_list_iter_end(&conn_iter);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100198
Daniel Vetterc36a3252016-12-15 16:58:43 +0100199 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200200}
201
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100202static void
203set_best_encoder(struct drm_atomic_state *state,
204 struct drm_connector_state *conn_state,
205 struct drm_encoder *encoder)
206{
207 struct drm_crtc_state *crtc_state;
208 struct drm_crtc *crtc;
209
210 if (conn_state->best_encoder) {
211 /* Unset the encoder_mask in the old crtc state. */
212 crtc = conn_state->connector->state->crtc;
213
214 /* A NULL crtc is an error here because we should have
215 * duplicated a NULL best_encoder when crtc was NULL.
216 * As an exception restoring duplicated atomic state
217 * during resume is allowed, so don't warn when
218 * best_encoder is equal to encoder we intend to set.
219 */
220 WARN_ON(!crtc && encoder != conn_state->best_encoder);
221 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100222 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100223
224 crtc_state->encoder_mask &=
225 ~(1 << drm_encoder_index(conn_state->best_encoder));
226 }
227 }
228
229 if (encoder) {
230 crtc = conn_state->crtc;
231 WARN_ON(!crtc);
232 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100233 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100234
235 crtc_state->encoder_mask |=
236 1 << drm_encoder_index(encoder);
237 }
238 }
239
240 conn_state->best_encoder = encoder;
241}
242
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100243static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200244steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100245 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200246{
Daniel Vetter623369e2014-09-16 17:50:47 +0200247 struct drm_crtc_state *crtc_state;
248 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100249 struct drm_connector_state *old_connector_state, *new_connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100250 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200251
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100252 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100253 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200254
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100255 if (new_connector_state->best_encoder != encoder)
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100256 continue;
257
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100258 encoder_crtc = old_connector_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200259
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100260 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
261 encoder->base.id, encoder->name,
262 encoder_crtc->base.id, encoder_crtc->name);
263
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100264 set_best_encoder(state, new_connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100265
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100266 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100267 crtc_state->connectors_changed = true;
268
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100269 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200270 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200271}
272
273static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100274update_connector_routing(struct drm_atomic_state *state,
275 struct drm_connector *connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100276 struct drm_connector_state *old_connector_state,
277 struct drm_connector_state *new_connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200278{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200279 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200280 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200281 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200282
Daniel Vetter17a38d92015-02-22 12:24:16 +0100283 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
284 connector->base.id,
285 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200286
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100287 if (old_connector_state->crtc != new_connector_state->crtc) {
288 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100289 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200290 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200291 }
292
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100293 if (new_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100294 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200295 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200296 }
297 }
298
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100299 if (!new_connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100300 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200301 connector->base.id,
302 connector->name);
303
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100304 set_best_encoder(state, new_connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200305
306 return 0;
307 }
308
309 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200310
311 if (funcs->atomic_best_encoder)
312 new_encoder = funcs->atomic_best_encoder(connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100313 new_connector_state);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200314 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200315 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200316 else
317 new_encoder = drm_atomic_helper_best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200318
319 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100320 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
321 connector->base.id,
322 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200323 return -EINVAL;
324 }
325
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100326 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
Russell King6ac7c542017-02-13 12:27:03 +0000327 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100328 new_encoder->base.id,
329 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100330 new_connector_state->crtc->base.id,
331 new_connector_state->crtc->name);
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100332 return -EINVAL;
333 }
334
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100335 if (new_encoder == new_connector_state->best_encoder) {
336 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100337
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200338 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100339 connector->base.id,
340 connector->name,
341 new_encoder->base.id,
342 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100343 new_connector_state->crtc->base.id,
344 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200345
346 return 0;
347 }
348
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100349 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200350
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100351 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100352
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100353 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200354 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200355
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200356 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100357 connector->base.id,
358 connector->name,
359 new_encoder->base.id,
360 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100361 new_connector_state->crtc->base.id,
362 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200363
364 return 0;
365}
366
367static int
368mode_fixup(struct drm_atomic_state *state)
369{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300370 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100371 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300372 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100373 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200374 int i;
Dan Carpenterf9ad86e2017-02-08 02:46:01 +0300375 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200376
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100377 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
378 if (!new_crtc_state->mode_changed &&
379 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200380 continue;
381
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100382 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200383 }
384
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100385 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200386 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200387 struct drm_encoder *encoder;
388
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100389 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200390
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100391 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200392 continue;
393
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100394 new_crtc_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100395 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200396
397 /*
398 * Each encoder has at most one connector (since we always steal
399 * it away), so we won't call ->mode_fixup twice.
400 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100401 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200402 funcs = encoder->helper_private;
403
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100404 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
405 &new_crtc_state->adjusted_mode);
Archit Taneja862e6862015-05-21 11:03:16 +0530406 if (!ret) {
407 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
408 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200409 }
410
Noralf Trønnes28276352016-05-11 18:09:20 +0200411 if (funcs && funcs->atomic_check) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100412 ret = funcs->atomic_check(encoder, new_crtc_state,
413 new_conn_state);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100414 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100415 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
416 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100417 return ret;
418 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200419 } else if (funcs && funcs->mode_fixup) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100420 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
421 &new_crtc_state->adjusted_mode);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100422 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100423 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
424 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100425 return -EINVAL;
426 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200427 }
428 }
429
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100430 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200431 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200432
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100433 if (!new_crtc_state->enable)
Liu Yingf55f1702016-05-27 17:35:54 +0800434 continue;
435
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100436 if (!new_crtc_state->mode_changed &&
437 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200438 continue;
439
440 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300441 if (!funcs->mode_fixup)
442 continue;
443
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100444 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
445 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200446 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200447 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
448 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200449 return -EINVAL;
450 }
451 }
452
453 return 0;
454}
455
Jose Abreufaf94a02017-05-25 15:19:16 +0100456static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
457 struct drm_encoder *encoder,
458 struct drm_crtc *crtc,
459 struct drm_display_mode *mode)
460{
461 enum drm_mode_status ret;
462
463 ret = drm_encoder_mode_valid(encoder, mode);
464 if (ret != MODE_OK) {
465 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
466 encoder->base.id, encoder->name);
467 return ret;
468 }
469
470 ret = drm_bridge_mode_valid(encoder->bridge, mode);
471 if (ret != MODE_OK) {
472 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
473 return ret;
474 }
475
476 ret = drm_crtc_mode_valid(crtc, mode);
477 if (ret != MODE_OK) {
478 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
479 crtc->base.id, crtc->name);
480 return ret;
481 }
482
483 return ret;
484}
485
486static int
487mode_valid(struct drm_atomic_state *state)
488{
489 struct drm_connector_state *conn_state;
490 struct drm_connector *connector;
491 int i;
492
493 for_each_new_connector_in_state(state, connector, conn_state, i) {
494 struct drm_encoder *encoder = conn_state->best_encoder;
495 struct drm_crtc *crtc = conn_state->crtc;
496 struct drm_crtc_state *crtc_state;
497 enum drm_mode_status mode_status;
498 struct drm_display_mode *mode;
499
500 if (!crtc || !encoder)
501 continue;
502
503 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
504 if (!crtc_state)
505 continue;
506 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
507 continue;
508
509 mode = &crtc_state->mode;
510
511 mode_status = mode_valid_path(connector, encoder, crtc, mode);
512 if (mode_status != MODE_OK)
513 return -EINVAL;
514 }
515
516 return 0;
517}
518
Daniel Vetterd9b13622014-11-26 16:57:41 +0100519/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800520 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100521 * @dev: DRM device
522 * @state: the driver state object
523 *
524 * Check the state object to see if the requested state is physically possible.
525 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200526 * update and adds any additional connectors needed for full modesets. It calls
527 * the various per-object callbacks in the follow order:
528 *
529 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
530 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
531 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
532 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
Jose Abreufaf94a02017-05-25 15:19:16 +0100533 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
534 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
535 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
536 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200537 * This function is only called when the encoder will be part of a configured crtc,
538 * it must not be used for implementing connector property validation.
539 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
540 * instead.
Jose Abreufaf94a02017-05-25 15:19:16 +0100541 * 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 +0200542 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100543 * &drm_crtc_state.mode_changed is set when the input mode is changed.
544 * &drm_crtc_state.connectors_changed is set when a connector is added or
545 * removed from the crtc. &drm_crtc_state.active_changed is set when
546 * &drm_crtc_state.active changes, which is used for DPMS.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100547 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100548 *
549 * IMPORTANT:
550 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100551 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
552 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
553 * without a full modeset) _must_ call this function afterwards after that
554 * change. It is permitted to call this function multiple times for the same
555 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
556 * upon the adjusted dotclock for fifo space allocation and watermark
557 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100558 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200559 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100560 * Zero for success or -errno
561 */
562int
Rob Clark934ce1c2014-11-19 16:41:33 -0500563drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200564 struct drm_atomic_state *state)
565{
Daniel Vetter623369e2014-09-16 17:50:47 +0200566 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100567 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300568 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100569 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200570 int i, ret;
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200571 unsigned connectors_mask = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200572
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100573 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200574 bool has_connectors =
575 !!new_crtc_state->connector_mask;
576
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100577 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200578 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
579 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100580 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200581 }
582
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100583 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200584 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
585 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200586
587 /*
588 * For clarity this assignment is done here, but
589 * enable == 0 is only true when there are no
590 * connectors and a NULL mode.
591 *
592 * The other way around is true as well. enable != 0
593 * iff connectors are attached and a mode is set.
594 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100595 new_crtc_state->mode_changed = true;
596 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200597 }
Maarten Lankhorst24d66522017-04-06 13:19:01 +0200598
599 if (old_crtc_state->active != new_crtc_state->active) {
600 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
601 crtc->base.id, crtc->name);
602 new_crtc_state->active_changed = true;
603 }
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200604
605 if (new_crtc_state->enable != has_connectors) {
606 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
607 crtc->base.id, crtc->name);
608
609 return -EINVAL;
610 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200611 }
612
Maarten Lankhorst44596b82017-04-06 13:19:00 +0200613 ret = handle_conflicting_encoders(state, false);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100614 if (ret)
615 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100616
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100617 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200618 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
619
Daniel Vetter623369e2014-09-16 17:50:47 +0200620 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100621 * This only sets crtc->connectors_changed for routing changes,
622 * drivers must set crtc->connectors_changed themselves when
623 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200624 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100625 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100626 old_connector_state,
627 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200628 if (ret)
629 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100630 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100631 new_crtc_state = drm_atomic_get_new_crtc_state(state,
632 old_connector_state->crtc);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100633 if (old_connector_state->link_status !=
634 new_connector_state->link_status)
635 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200636 }
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200637
638 if (funcs->atomic_check)
639 ret = funcs->atomic_check(connector, new_connector_state);
640 if (ret)
641 return ret;
642
643 connectors_mask += BIT(i);
Daniel Vetter623369e2014-09-16 17:50:47 +0200644 }
645
646 /*
647 * After all the routing has been prepared we need to add in any
648 * connector which is itself unchanged, but who's crtc changes it's
649 * configuration. This must be done before calling mode_fixup in case a
650 * crtc only changed its mode but has the same set of connectors.
651 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100652 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100653 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100654 continue;
655
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200656 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
657 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100658 new_crtc_state->enable ? 'y' : 'n',
659 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200660
661 ret = drm_atomic_add_affected_connectors(state, crtc);
662 if (ret != 0)
663 return ret;
664
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200665 ret = drm_atomic_add_affected_planes(state, crtc);
666 if (ret != 0)
667 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200668 }
669
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200670 /*
671 * Iterate over all connectors again, to make sure atomic_check()
672 * has been called on them when a modeset is forced.
673 */
674 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
675 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
676
677 if (connectors_mask & BIT(i))
678 continue;
679
680 if (funcs->atomic_check)
681 ret = funcs->atomic_check(connector, new_connector_state);
682 if (ret)
683 return ret;
684 }
685
Jose Abreufaf94a02017-05-25 15:19:16 +0100686 ret = mode_valid(state);
687 if (ret)
688 return ret;
689
Daniel Vetter623369e2014-09-16 17:50:47 +0200690 return mode_fixup(state);
691}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100692EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200693
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100694/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800695 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100696 * @dev: DRM device
697 * @state: the driver state object
698 *
699 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100700 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100701 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
702 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100703 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100704 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200705 * updated planes.
706 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200707 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100708 * Zero for success or -errno
709 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100710int
711drm_atomic_helper_check_planes(struct drm_device *dev,
712 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100713{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300714 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100715 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300716 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100717 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100718 int i, ret = 0;
719
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100720 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200721 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100722
723 funcs = plane->helper_private;
724
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100725 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100726
727 if (!funcs || !funcs->atomic_check)
728 continue;
729
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100730 ret = funcs->atomic_check(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100731 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200732 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
733 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100734 return ret;
735 }
736 }
737
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100738 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200739 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100740
741 funcs = crtc->helper_private;
742
743 if (!funcs || !funcs->atomic_check)
744 continue;
745
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100746 ret = funcs->atomic_check(crtc, new_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100747 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200748 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
749 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100750 return ret;
751 }
752 }
753
Daniel Vetterd9b13622014-11-26 16:57:41 +0100754 return ret;
755}
756EXPORT_SYMBOL(drm_atomic_helper_check_planes);
757
758/**
759 * drm_atomic_helper_check - validate state object
760 * @dev: DRM device
761 * @state: the driver state object
762 *
763 * Check the state object to see if the requested state is physically possible.
764 * Only crtcs and planes have check callbacks, so for any additional (global)
765 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100766 * Drivers without such needs can directly use this as their
767 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100768 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100769 * This just wraps the two parts of the state checking for planes and modeset
770 * state in the default order: First it calls drm_atomic_helper_check_modeset()
771 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100772 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
773 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
774 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100775 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200776 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100777 * Zero for success or -errno
778 */
779int drm_atomic_helper_check(struct drm_device *dev,
780 struct drm_atomic_state *state)
781{
782 int ret;
783
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100784 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100785 if (ret)
786 return ret;
787
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100788 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500789 if (ret)
790 return ret;
791
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100792 return ret;
793}
794EXPORT_SYMBOL(drm_atomic_helper_check);
795
Daniel Vetter623369e2014-09-16 17:50:47 +0200796static void
797disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
798{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300799 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100800 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300801 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100802 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200803 int i;
804
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100805 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 +0200806 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200807 struct drm_encoder *encoder;
808
Daniel Vetter623369e2014-09-16 17:50:47 +0200809 /* Shut down everything that's in the changeset and currently
810 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300811 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200812 continue;
813
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100814 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100815
Daniel Vetter4218a322015-03-26 22:18:40 +0100816 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200817 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100818 continue;
819
Rob Clark46df9ad2014-11-20 15:40:36 -0500820 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200821
Rob Clark46df9ad2014-11-20 15:40:36 -0500822 /* We shouldn't get this far if we didn't previously have
823 * an encoder.. but WARN_ON() rather than explode.
824 */
825 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200826 continue;
827
828 funcs = encoder->helper_private;
829
Daniel Vetter17a38d92015-02-22 12:24:16 +0100830 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
831 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100832
Daniel Vetter623369e2014-09-16 17:50:47 +0200833 /*
834 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800835 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200836 */
Archit Taneja862e6862015-05-21 11:03:16 +0530837 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200838
839 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200840 if (funcs) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100841 if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +0200842 funcs->prepare(encoder);
843 else if (funcs->disable)
844 funcs->disable(encoder);
845 else if (funcs->dpms)
846 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
847 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200848
Archit Taneja862e6862015-05-21 11:03:16 +0530849 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200850 }
851
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100852 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 +0200853 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200854
855 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100856 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100857 continue;
858
859 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200860 continue;
861
862 funcs = crtc->helper_private;
863
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200864 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
865 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100866
867
Daniel Vetter623369e2014-09-16 17:50:47 +0200868 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100869 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200870 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +0800871 else if (funcs->atomic_disable)
872 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200873 else if (funcs->disable)
874 funcs->disable(crtc);
875 else
876 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
877 }
878}
879
Daniel Vetter4c18d302015-05-12 15:27:37 +0200880/**
881 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
882 * @dev: DRM device
883 * @old_state: atomic state object with old state structures
884 *
885 * This function updates all the various legacy modeset state pointers in
886 * connectors, encoders and crtcs. It also updates the timestamping constants
887 * used for precise vblank timestamps by calling
888 * drm_calc_timestamping_constants().
889 *
890 * Drivers can use this for building their own atomic commit if they don't have
891 * a pure helper-based modeset implementation.
892 */
893void
894drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
895 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200896{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300897 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100898 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300899 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100900 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200901 int i;
902
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200903 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100904 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200905 if (connector->encoder) {
906 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200907
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200908 connector->encoder->crtc = NULL;
909 connector->encoder = NULL;
910 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200911
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100912 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200913 if ((!crtc && old_conn_state->crtc) ||
914 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
915 struct drm_property *dpms_prop =
916 dev->mode_config.dpms_property;
917 int mode = DRM_MODE_DPMS_OFF;
918
919 if (crtc && crtc->state->active)
920 mode = DRM_MODE_DPMS_ON;
921
922 connector->dpms = mode;
923 drm_object_property_set_value(&connector->base,
924 dpms_prop, mode);
925 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200926 }
927
928 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100929 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
930 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200931 continue;
932
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100933 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200934 continue;
935
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100936 connector->encoder = new_conn_state->best_encoder;
937 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200938 }
939
940 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100941 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200942 struct drm_plane *primary = crtc->primary;
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100943 struct drm_plane_state *new_plane_state;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200944
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100945 crtc->mode = new_crtc_state->mode;
946 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200947
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100948 new_plane_state =
949 drm_atomic_get_new_plane_state(old_state, primary);
950
951 if (new_plane_state && new_plane_state->crtc == crtc) {
952 crtc->x = new_plane_state->src_x >> 16;
953 crtc->y = new_plane_state->src_y >> 16;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200954 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200955
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100956 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200957 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100958 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200959 }
960}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200961EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200962
963static void
964crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
965{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300966 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100967 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300968 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100969 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200970 int i;
971
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100972 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200973 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200974
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100975 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200976 continue;
977
978 funcs = crtc->helper_private;
979
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100980 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200981 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
982 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100983
Daniel Vetter623369e2014-09-16 17:50:47 +0200984 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100985 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200986 }
987
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100988 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200989 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200990 struct drm_encoder *encoder;
991 struct drm_display_mode *mode, *adjusted_mode;
992
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100993 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200994 continue;
995
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100996 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200997 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100998 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200999 mode = &new_crtc_state->mode;
1000 adjusted_mode = &new_crtc_state->adjusted_mode;
1001
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001002 if (!new_crtc_state->mode_changed)
1003 continue;
1004
Daniel Vetter17a38d92015-02-22 12:24:16 +01001005 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1006 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001007
Daniel Vetter623369e2014-09-16 17:50:47 +02001008 /*
1009 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001010 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001011 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001012 if (funcs && funcs->atomic_mode_set) {
1013 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001014 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001015 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +01001016 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001017 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001018
Archit Taneja862e6862015-05-21 11:03:16 +05301019 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001020 }
1021}
1022
1023/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001024 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001025 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +01001026 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001027 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001028 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +02001029 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +01001030 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +03001031 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +01001032 * drm_atomic_helper_commit_planes(), which is what the default commit function
1033 * does. But drivers with different needs can group the modeset commits together
1034 * and do the plane commits at the end. This is useful for drivers doing runtime
1035 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001036 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001037void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1038 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001039{
Laurent Pincharta072f802015-02-22 12:24:18 +01001040 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +02001041
1042 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1043
Laurent Pincharta072f802015-02-22 12:24:18 +01001044 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001045}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001046EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001047
1048/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001049 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001050 * @dev: DRM device
1051 * @old_state: atomic state object with old state structures
1052 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001053 * This function enables all the outputs with the new configuration which had to
1054 * be turned off for the update.
1055 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +03001056 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +01001057 * drm_atomic_helper_commit_planes(), which is what the default commit function
1058 * does. But drivers with different needs can group the modeset commits together
1059 * and do the plane commits at the end. This is useful for drivers doing runtime
1060 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001061 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001062void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1063 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001064{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001065 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001066 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001067 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001068 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001069 int i;
1070
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001071 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001072 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001073
1074 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001075 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001076 continue;
1077
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001078 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +02001079 continue;
1080
1081 funcs = crtc->helper_private;
1082
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001083 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001084 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1085 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001086
Daniel Vetteree0a89c2015-01-22 16:36:24 +01001087 if (funcs->enable)
1088 funcs->enable(crtc);
1089 else
1090 funcs->commit(crtc);
1091 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001092 }
1093
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001094 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001095 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001096 struct drm_encoder *encoder;
1097
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001098 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001099 continue;
1100
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001101 if (!new_conn_state->crtc->state->active ||
1102 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001103 continue;
1104
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001105 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001106 funcs = encoder->helper_private;
1107
Daniel Vetter17a38d92015-02-22 12:24:16 +01001108 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1109 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001110
Daniel Vetter623369e2014-09-16 17:50:47 +02001111 /*
1112 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001113 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001114 */
Archit Taneja862e6862015-05-21 11:03:16 +05301115 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001116
Noralf Trønnes28276352016-05-11 18:09:20 +02001117 if (funcs) {
1118 if (funcs->enable)
1119 funcs->enable(encoder);
1120 else if (funcs->commit)
1121 funcs->commit(encoder);
1122 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001123
Archit Taneja862e6862015-05-21 11:03:16 +05301124 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001125 }
1126}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001127EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001128
Rob Clark4c5b7f32016-03-18 19:14:55 -04001129/**
1130 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1131 * @dev: DRM device
1132 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001133 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1134 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001135 *
1136 * For implicit sync, driver should fish the exclusive fence out from the
1137 * incoming fb's and stash it in the drm_plane_state. This is called after
1138 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1139 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001140 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001141 * Note that @pre_swap is needed since the point where we block for fences moves
1142 * around depending upon whether an atomic commit is blocking or
Gustavo Padovan42590372017-04-27 11:35:06 -03001143 * non-blocking. For non-blocking commit all waiting needs to happen after
1144 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001145 * to wait **before** we do anything that can't be easily rolled back. That is
1146 * before we call drm_atomic_helper_swap_state().
1147 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001148 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001149 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001150int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1151 struct drm_atomic_state *state,
1152 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001153{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001154 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001155 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001156 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001157
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001158 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1159 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001160 continue;
1161
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001162 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001163
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001164 /*
1165 * If waiting for fences pre-swap (ie: nonblock), userspace can
1166 * still interrupt the operation. Instead of blocking until the
1167 * timer expires, make the wait interruptible.
1168 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001169 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001170 if (ret)
1171 return ret;
1172
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001173 dma_fence_put(new_plane_state->fence);
1174 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001175 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001176
1177 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001178}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001179EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001180
John Keepingc2409062016-01-19 10:46:58 +00001181/**
Rob Clark5ee32292014-11-11 19:38:59 -05001182 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1183 * @dev: DRM device
1184 * @old_state: atomic state object with old state structures
1185 *
1186 * Helper to, after atomic commit, wait for vblanks on all effected
1187 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001188 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1189 * framebuffers have actually changed to optimize for the legacy cursor and
1190 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001191 */
1192void
1193drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1194 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001195{
1196 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001197 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001198 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001199 unsigned crtc_mask = 0;
1200
1201 /*
1202 * Legacy cursor ioctls are completely unsynced, and userspace
1203 * relies on that (by doing tons of cursor updates).
1204 */
1205 if (old_state->legacy_cursor_update)
1206 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001207
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001208 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta3fbb532016-12-08 14:45:27 +01001209 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
Daniel Vetterab58e332014-11-24 20:42:42 +01001210 continue;
1211
Daniel Vetter623369e2014-09-16 17:50:47 +02001212 ret = drm_crtc_vblank_get(crtc);
1213 if (ret != 0)
1214 continue;
1215
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001216 crtc_mask |= drm_crtc_mask(crtc);
1217 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001218 }
1219
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001220 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001221 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001222 continue;
1223
1224 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001225 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001226 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001227 msecs_to_jiffies(50));
1228
Russell King6ac7c542017-02-13 12:27:03 +00001229 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1230 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001231
Daniel Vetter623369e2014-09-16 17:50:47 +02001232 drm_crtc_vblank_put(crtc);
1233 }
1234}
Rob Clark5ee32292014-11-11 19:38:59 -05001235EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001236
1237/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001238 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001239 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001240 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001241 * This is the default implementation for the
1242 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
Daniel Vetter623369e2014-09-16 17:50:47 +02001243 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001244 * Note that the default ordering of how the various stages are called is to
1245 * match the legacy modeset helper library closest. One peculiarity of that is
1246 * that it doesn't mesh well with runtime PM at all.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001247 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001248 * For drivers supporting runtime PM the recommended sequence is instead ::
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001249 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001250 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001251 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001252 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001253 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001254 * drm_atomic_helper_commit_planes(dev, old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001255 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001256 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001257 * for committing the atomic update to hardware. See the kerneldoc entries for
1258 * these three functions for more details.
1259 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001260void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001261{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001262 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001263
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001264 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001265
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001266 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001267
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001268 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001269
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001270 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001271
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001272 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001273
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001274 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001275}
1276EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1277
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001278static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001279{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001280 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001281 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001282
1283 funcs = dev->mode_config.helper_private;
1284
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001285 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001286
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001287 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001288
1289 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001290 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001291 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001292 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001293
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001294 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001295
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001296 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001297}
1298
1299static void commit_work(struct work_struct *work)
1300{
1301 struct drm_atomic_state *state = container_of(work,
1302 struct drm_atomic_state,
1303 commit_work);
1304 commit_tail(state);
1305}
1306
1307/**
1308 * drm_atomic_helper_commit - commit validated state object
1309 * @dev: DRM device
1310 * @state: the driver state object
1311 * @nonblock: whether nonblocking behavior is requested.
1312 *
1313 * This function commits a with drm_atomic_helper_check() pre-validated state
1314 * object. This can still fail when e.g. the framebuffer reservation fails. This
1315 * function implements nonblocking commits, using
1316 * drm_atomic_helper_setup_commit() and related functions.
1317 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001318 * Committing the actual hardware state is done through the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001319 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1320 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001321 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001322 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001323 * Zero for success or -errno.
1324 */
1325int drm_atomic_helper_commit(struct drm_device *dev,
1326 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001327 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001328{
1329 int ret;
1330
Daniel Vettera095caa2016-06-08 17:15:36 +02001331 ret = drm_atomic_helper_setup_commit(state, nonblock);
1332 if (ret)
1333 return ret;
1334
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001335 INIT_WORK(&state->commit_work, commit_work);
1336
Daniel Vetter623369e2014-09-16 17:50:47 +02001337 ret = drm_atomic_helper_prepare_planes(dev, state);
1338 if (ret)
1339 return ret;
1340
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001341 if (!nonblock) {
1342 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001343 if (ret) {
1344 drm_atomic_helper_cleanup_planes(dev, state);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001345 return ret;
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001346 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001347 }
1348
Daniel Vetter623369e2014-09-16 17:50:47 +02001349 /*
1350 * This is the point of no return - everything below never fails except
1351 * when the hw goes bonghits. Which means we can commit the new state on
1352 * the software side now.
1353 */
1354
Daniel Vetter5e84c262016-06-10 00:06:32 +02001355 drm_atomic_helper_swap_state(state, true);
Daniel Vetter623369e2014-09-16 17:50:47 +02001356
1357 /*
1358 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001359 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001360 * that the asynchronous work has either been cancelled (if the driver
1361 * supports it, which at least requires that the framebuffers get
1362 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1363 * before the new state gets committed on the software side with
1364 * drm_atomic_helper_swap_state().
1365 *
1366 * This scheme allows new atomic state updates to be prepared and
1367 * checked in parallel to the asynchronous completion of the previous
1368 * update. Which is important since compositors need to figure out the
1369 * composition of the next frame right after having submitted the
1370 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001371 *
1372 * NOTE: Commit work has multiple phases, first hardware commit, then
1373 * cleanup. We want them to overlap, hence need system_unbound_wq to
1374 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001375 */
1376
Chris Wilson08536952016-10-14 13:18:18 +01001377 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001378 if (nonblock)
1379 queue_work(system_unbound_wq, &state->commit_work);
1380 else
1381 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001382
1383 return 0;
1384}
1385EXPORT_SYMBOL(drm_atomic_helper_commit);
1386
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001387/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001388 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001389 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001390 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001391 *
1392 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1393 * which commit needs to call which can fail, so we want to run it first and
1394 * synchronously.
1395 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001396 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001397 * might be affected the new state update. This can be done by either cancelling
1398 * or flushing the work items, depending upon whether the driver can deal with
1399 * cancelled updates. Note that it is important to ensure that the framebuffer
1400 * cleanup is still done when cancelling.
1401 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001402 * Asynchronous workers need to have sufficient parallelism to be able to run
1403 * different atomic commits on different CRTCs in parallel. The simplest way to
1404 * achive this is by running them on the &system_unbound_wq work queue. Note
1405 * that drivers are not required to split up atomic commits and run an
1406 * individual commit in parallel - userspace is supposed to do that if it cares.
1407 * But it might be beneficial to do that for modesets, since those necessarily
1408 * must be done as one global operation, and enabling or disabling a CRTC can
1409 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001410 *
1411 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001412 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001413 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001414 * while it's guaranteed that no relevant nonblocking worker runs means that
1415 * nonblocking workers do not need grab any locks. Actually they must not grab
1416 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001417 *
1418 * 4. Schedule a work item to do all subsequent steps, using the split-out
1419 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1420 * then cleaning up the framebuffers after the old framebuffer is no longer
1421 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001422 *
1423 * The above scheme is implemented in the atomic helper libraries in
1424 * drm_atomic_helper_commit() using a bunch of helper functions. See
1425 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001426 */
1427
Daniel Vettera095caa2016-06-08 17:15:36 +02001428static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1429{
1430 struct drm_crtc_commit *commit, *stall_commit = NULL;
1431 bool completed = true;
1432 int i;
1433 long ret = 0;
1434
1435 spin_lock(&crtc->commit_lock);
1436 i = 0;
1437 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1438 if (i == 0) {
1439 completed = try_wait_for_completion(&commit->flip_done);
1440 /* Userspace is not allowed to get ahead of the previous
1441 * commit with nonblocking ones. */
1442 if (!completed && nonblock) {
1443 spin_unlock(&crtc->commit_lock);
1444 return -EBUSY;
1445 }
1446 } else if (i == 1) {
1447 stall_commit = commit;
1448 drm_crtc_commit_get(stall_commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001449 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001450 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001451
1452 i++;
1453 }
1454 spin_unlock(&crtc->commit_lock);
1455
1456 if (!stall_commit)
1457 return 0;
1458
1459 /* We don't want to let commits get ahead of cleanup work too much,
1460 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1461 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001462 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001463 10*HZ);
1464 if (ret == 0)
1465 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1466 crtc->base.id, crtc->name);
1467
1468 drm_crtc_commit_put(stall_commit);
1469
1470 return ret < 0 ? ret : 0;
1471}
1472
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001473static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001474{
1475 struct drm_crtc_commit *commit = container_of(completion,
1476 typeof(*commit),
1477 flip_done);
1478
1479 drm_crtc_commit_put(commit);
1480}
1481
Daniel Vettera095caa2016-06-08 17:15:36 +02001482/**
1483 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1484 * @state: new modeset state to be committed
1485 * @nonblock: whether nonblocking behavior is requested.
1486 *
1487 * This function prepares @state to be used by the atomic helper's support for
1488 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001489 * should always call this function from their
1490 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001491 *
1492 * To be able to use this support drivers need to use a few more helper
1493 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1494 * actually committing the hardware state, and for nonblocking commits this call
1495 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1496 * and it's stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001497 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001498 *
1499 * Completion of the hardware commit step must be signalled using
1500 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1501 * to read or change any permanent software or hardware modeset state. The only
1502 * exception is state protected by other means than &drm_modeset_lock locks.
1503 * Only the free standing @state with pointers to the old state structures can
1504 * be inspected, e.g. to clean up old buffers using
1505 * drm_atomic_helper_cleanup_planes().
1506 *
1507 * At the very end, before cleaning up @state drivers must call
1508 * drm_atomic_helper_commit_cleanup_done().
1509 *
1510 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1511 * complete and esay-to-use default implementation of the atomic_commit() hook.
1512 *
1513 * The tracking of asynchronously executed and still pending commits is done
1514 * using the core structure &drm_crtc_commit.
1515 *
1516 * By default there's no need to clean up resources allocated by this function
1517 * explicitly: drm_atomic_state_default_clear() will take care of that
1518 * automatically.
1519 *
1520 * Returns:
1521 *
1522 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1523 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1524 */
1525int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1526 bool nonblock)
1527{
1528 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001529 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001530 struct drm_crtc_commit *commit;
1531 int i, ret;
1532
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001533 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001534 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1535 if (!commit)
1536 return -ENOMEM;
1537
1538 init_completion(&commit->flip_done);
1539 init_completion(&commit->hw_done);
1540 init_completion(&commit->cleanup_done);
1541 INIT_LIST_HEAD(&commit->commit_entry);
1542 kref_init(&commit->ref);
1543 commit->crtc = crtc;
1544
1545 state->crtcs[i].commit = commit;
1546
1547 ret = stall_checks(crtc, nonblock);
1548 if (ret)
1549 return ret;
1550
1551 /* Drivers only send out events when at least either current or
1552 * new CRTC state is active. Complete right away if everything
1553 * stays off. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001554 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001555 complete_all(&commit->flip_done);
1556 continue;
1557 }
1558
1559 /* Legacy cursor updates are fully unsynced. */
1560 if (state->legacy_cursor_update) {
1561 complete_all(&commit->flip_done);
1562 continue;
1563 }
1564
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001565 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001566 commit->event = kzalloc(sizeof(*commit->event),
1567 GFP_KERNEL);
1568 if (!commit->event)
1569 return -ENOMEM;
1570
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001571 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02001572 }
1573
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001574 new_crtc_state->event->base.completion = &commit->flip_done;
1575 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01001576 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001577 }
1578
1579 return 0;
1580}
1581EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1582
1583
1584static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1585{
1586 struct drm_crtc_commit *commit;
1587 int i = 0;
1588
1589 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1590 /* skip the first entry, that's the current commit */
1591 if (i == 1)
1592 return commit;
1593 i++;
1594 }
1595
1596 return NULL;
1597}
1598
1599/**
1600 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001601 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001602 *
1603 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001604 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02001605 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001606 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02001607 *
1608 * This is part of the atomic helper support for nonblocking commits, see
1609 * drm_atomic_helper_setup_commit() for an overview.
1610 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001611void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001612{
1613 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001614 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001615 struct drm_crtc_commit *commit;
1616 int i;
1617 long ret;
1618
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001619 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001620 spin_lock(&crtc->commit_lock);
1621 commit = preceeding_commit(crtc);
1622 if (commit)
1623 drm_crtc_commit_get(commit);
1624 spin_unlock(&crtc->commit_lock);
1625
1626 if (!commit)
1627 continue;
1628
1629 ret = wait_for_completion_timeout(&commit->hw_done,
1630 10*HZ);
1631 if (ret == 0)
1632 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1633 crtc->base.id, crtc->name);
1634
1635 /* Currently no support for overwriting flips, hence
1636 * stall for previous one to execute completely. */
1637 ret = wait_for_completion_timeout(&commit->flip_done,
1638 10*HZ);
1639 if (ret == 0)
1640 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1641 crtc->base.id, crtc->name);
1642
1643 drm_crtc_commit_put(commit);
1644 }
1645}
1646EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1647
1648/**
1649 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001650 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001651 *
1652 * This function is used to signal completion of the hardware commit step. After
1653 * this step the driver is not allowed to read or change any permanent software
1654 * or hardware modeset state. The only exception is state protected by other
1655 * means than &drm_modeset_lock locks.
1656 *
1657 * Drivers should try to postpone any expensive or delayed cleanup work after
1658 * this function is called.
1659 *
1660 * This is part of the atomic helper support for nonblocking commits, see
1661 * drm_atomic_helper_setup_commit() for an overview.
1662 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001663void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001664{
1665 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001666 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001667 struct drm_crtc_commit *commit;
1668 int i;
1669
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001670 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001671 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001672 if (!commit)
1673 continue;
1674
1675 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001676 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02001677 spin_lock(&crtc->commit_lock);
1678 complete_all(&commit->hw_done);
1679 spin_unlock(&crtc->commit_lock);
1680 }
1681}
1682EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1683
1684/**
1685 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001686 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001687 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001688 * This signals completion of the atomic update @old_state, including any
1689 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01001690 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02001691 *
1692 * This is part of the atomic helper support for nonblocking commits, see
1693 * drm_atomic_helper_setup_commit() for an overview.
1694 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001695void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001696{
1697 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001698 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001699 struct drm_crtc_commit *commit;
1700 int i;
1701 long ret;
1702
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001703 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001704 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001705 if (WARN_ON(!commit))
1706 continue;
1707
1708 spin_lock(&crtc->commit_lock);
1709 complete_all(&commit->cleanup_done);
1710 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1711
1712 /* commit_list borrows our reference, need to remove before we
1713 * clean up our drm_atomic_state. But only after it actually
1714 * completed, otherwise subsequent commits won't stall properly. */
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001715 if (try_wait_for_completion(&commit->flip_done))
1716 goto del_commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001717
1718 spin_unlock(&crtc->commit_lock);
1719
1720 /* We must wait for the vblank event to signal our completion
1721 * before releasing our reference, since the vblank work does
1722 * not hold a reference of its own. */
1723 ret = wait_for_completion_timeout(&commit->flip_done,
1724 10*HZ);
1725 if (ret == 0)
1726 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1727 crtc->base.id, crtc->name);
1728
1729 spin_lock(&crtc->commit_lock);
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001730del_commit:
Daniel Vettera095caa2016-06-08 17:15:36 +02001731 list_del(&commit->commit_entry);
1732 spin_unlock(&crtc->commit_lock);
1733 }
1734}
1735EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1736
Daniel Vettere8c833a2014-07-27 18:30:19 +02001737/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001738 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001739 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001740 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001741 *
1742 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001743 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1744 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1745 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001746 *
1747 * Returns:
1748 * 0 on success, negative error code on failure.
1749 */
1750int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1751 struct drm_atomic_state *state)
1752{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001753 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001754 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001755 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001756
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001757 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001758 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001759
1760 funcs = plane->helper_private;
1761
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001762 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001763 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001764 if (ret)
1765 goto fail;
1766 }
1767 }
1768
1769 return 0;
1770
1771fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001772 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001773 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001774
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001775 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001776 continue;
1777
1778 funcs = plane->helper_private;
1779
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001780 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001781 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001782 }
1783
1784 return ret;
1785}
1786EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1787
Ville Syrjälä7135ac52016-09-19 16:33:42 +03001788static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001789{
1790 return state->crtc && state->crtc->state->active;
1791}
1792
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001793/**
1794 * drm_atomic_helper_commit_planes - commit plane state
1795 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001796 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08001797 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001798 *
1799 * This function commits the new plane state using the plane and atomic helper
1800 * functions for planes and crtcs. It assumes that the atomic state has already
1801 * been pushed into the relevant object state pointers, since this step can no
1802 * longer fail.
1803 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001804 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001805 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001806 *
1807 * Note that this function does all plane updates across all CRTCs in one step.
1808 * If the hardware can't support this approach look at
1809 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001810 *
1811 * Plane parameters can be updated by applications while the associated CRTC is
1812 * disabled. The DRM/KMS core will store the parameters in the plane state,
1813 * which will be available to the driver when the CRTC is turned on. As a result
1814 * most drivers don't need to be immediately notified of plane updates for a
1815 * disabled CRTC.
1816 *
Liu Ying2b58e982016-08-29 17:12:03 +08001817 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1818 * @flags in order not to receive plane update notifications related to a
1819 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001820 * driver code when the driver and/or hardware can't or just don't need to deal
1821 * with updates on disabled CRTCs, for example when supporting runtime PM.
1822 *
Liu Ying2b58e982016-08-29 17:12:03 +08001823 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1824 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001825 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1826 * call for a plane if the CRTC of the old plane state needs a modesetting
1827 * operation. Of course, the drivers need to disable the planes in their CRTC
1828 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08001829 *
1830 * The drm_atomic_helper_commit() default implementation doesn't set the
1831 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1832 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001833 */
1834void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001835 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001836 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001837{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001838 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001839 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001840 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001841 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001842 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08001843 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1844 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001845
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001846 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 +02001847 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001848
1849 funcs = crtc->helper_private;
1850
1851 if (!funcs || !funcs->atomic_begin)
1852 continue;
1853
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001854 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001855 continue;
1856
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001857 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001858 }
1859
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001860 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 +02001861 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001862 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001863
1864 funcs = plane->helper_private;
1865
Thierry Reding3cad4b62014-11-25 13:05:12 +01001866 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001867 continue;
1868
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001869 disabling = drm_atomic_plane_disabling(old_plane_state,
1870 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001871
1872 if (active_only) {
1873 /*
1874 * Skip planes related to inactive CRTCs. If the plane
1875 * is enabled use the state of the current CRTC. If the
1876 * plane is being disabled use the state of the old
1877 * CRTC to avoid skipping planes being disabled on an
1878 * active CRTC.
1879 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001880 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001881 continue;
1882 if (disabling && !plane_crtc_active(old_plane_state))
1883 continue;
1884 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001885
Thierry Reding407b8bd2014-11-20 12:05:50 +01001886 /*
1887 * Special-case disabling the plane if drivers support it.
1888 */
Liu Ying2b58e982016-08-29 17:12:03 +08001889 if (disabling && funcs->atomic_disable) {
1890 struct drm_crtc_state *crtc_state;
1891
1892 crtc_state = old_plane_state->crtc->state;
1893
1894 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1895 no_disable)
1896 continue;
1897
Thierry Reding407b8bd2014-11-20 12:05:50 +01001898 funcs->atomic_disable(plane, old_plane_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001899 } else if (new_plane_state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01001900 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08001901 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001902 }
1903
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001904 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 +02001905 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001906
1907 funcs = crtc->helper_private;
1908
1909 if (!funcs || !funcs->atomic_flush)
1910 continue;
1911
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001912 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001913 continue;
1914
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001915 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001916 }
1917}
1918EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1919
1920/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001921 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1922 * @old_crtc_state: atomic state object with the old crtc state
1923 *
1924 * This function commits the new plane state using the plane and atomic helper
1925 * functions for planes on the specific crtc. It assumes that the atomic state
1926 * has already been pushed into the relevant object state pointers, since this
1927 * step can no longer fail.
1928 *
1929 * This function is useful when plane updates should be done crtc-by-crtc
1930 * instead of one global step like drm_atomic_helper_commit_planes() does.
1931 *
1932 * This function can only be savely used when planes are not allowed to move
1933 * between different CRTCs because this function doesn't handle inter-CRTC
1934 * depencies. Callers need to ensure that either no such depencies exist,
1935 * resolve them through ordering of commit calls or through some other means.
1936 */
1937void
1938drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1939{
1940 const struct drm_crtc_helper_funcs *crtc_funcs;
1941 struct drm_crtc *crtc = old_crtc_state->crtc;
1942 struct drm_atomic_state *old_state = old_crtc_state->state;
1943 struct drm_plane *plane;
1944 unsigned plane_mask;
1945
1946 plane_mask = old_crtc_state->plane_mask;
1947 plane_mask |= crtc->state->plane_mask;
1948
1949 crtc_funcs = crtc->helper_private;
1950 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001951 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001952
1953 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1954 struct drm_plane_state *old_plane_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001955 drm_atomic_get_old_plane_state(old_state, plane);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001956 const struct drm_plane_helper_funcs *plane_funcs;
1957
1958 plane_funcs = plane->helper_private;
1959
1960 if (!old_plane_state || !plane_funcs)
1961 continue;
1962
1963 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1964
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001965 if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001966 plane_funcs->atomic_disable)
1967 plane_funcs->atomic_disable(plane, old_plane_state);
1968 else if (plane->state->crtc ||
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001969 drm_atomic_plane_disabling(old_plane_state, plane->state))
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001970 plane_funcs->atomic_update(plane, old_plane_state);
1971 }
1972
1973 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001974 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001975}
1976EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1977
1978/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001979 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08001980 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02001981 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1982 *
1983 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08001984 * used for instance in the CRTC helper atomic_disable callback to disable
1985 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001986 *
1987 * If the atomic-parameter is set the function calls the CRTC's
1988 * atomic_begin hook before and atomic_flush hook after disabling the
1989 * planes.
1990 *
1991 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001992 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001993 */
Liu Ying28500292016-08-26 15:30:39 +08001994void
1995drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1996 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001997{
Liu Ying28500292016-08-26 15:30:39 +08001998 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02001999 const struct drm_crtc_helper_funcs *crtc_funcs =
2000 crtc->helper_private;
2001 struct drm_plane *plane;
2002
2003 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2004 crtc_funcs->atomic_begin(crtc, NULL);
2005
Liu Ying28500292016-08-26 15:30:39 +08002006 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02002007 const struct drm_plane_helper_funcs *plane_funcs =
2008 plane->helper_private;
2009
Liu Ying28500292016-08-26 15:30:39 +08002010 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002011 continue;
2012
2013 WARN_ON(!plane_funcs->atomic_disable);
2014 if (plane_funcs->atomic_disable)
2015 plane_funcs->atomic_disable(plane, NULL);
2016 }
2017
2018 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2019 crtc_funcs->atomic_flush(crtc, NULL);
2020}
2021EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2022
2023/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002024 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2025 * @dev: DRM device
2026 * @old_state: atomic state object with old state structures
2027 *
2028 * This function cleans up plane state, specifically framebuffers, from the old
2029 * configuration. Hence the old configuration must be perserved in @old_state to
2030 * be able to call this function.
2031 *
2032 * This function must also be called on the new state when the atomic update
2033 * fails at any point after calling drm_atomic_helper_prepare_planes().
2034 */
2035void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2036 struct drm_atomic_state *old_state)
2037{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002038 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002039 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002040 int i;
2041
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002042 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 +02002043 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002044 struct drm_plane_state *plane_state;
2045
2046 /*
2047 * This might be called before swapping when commit is aborted,
2048 * in which case we have to cleanup the new state.
2049 */
2050 if (old_plane_state == plane->state)
2051 plane_state = new_plane_state;
2052 else
2053 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002054
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002055 funcs = plane->helper_private;
2056
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002057 if (funcs->cleanup_fb)
2058 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002059 }
2060}
2061EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2062
2063/**
2064 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002065 * @state: atomic state
Daniel Vetter5e84c262016-06-10 00:06:32 +02002066 * @stall: stall for proceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002067 *
2068 * This function stores the atomic state into the current state pointers in all
2069 * driver objects. It should be called after all failing steps have been done
2070 * and succeeded, but before the actual hardware state is committed.
2071 *
2072 * For cleanup and error recovery the current state for all changed objects will
2073 * be swaped into @state.
2074 *
2075 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2076 *
2077 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2078 *
2079 * 2. Do any other steps that might fail.
2080 *
2081 * 3. Put the staged state into the current state pointers with this function.
2082 *
2083 * 4. Actually commit the hardware state.
2084 *
Daniel Vetter26196f72015-08-25 16:26:03 +02002085 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002086 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02002087 *
2088 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002089 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2090 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02002091 * don't pass the right state structures to the callbacks.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002092 */
Daniel Vetter5e84c262016-06-10 00:06:32 +02002093void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2094 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002095{
2096 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02002097 long ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002098 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002099 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002100 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002101 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002102 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002103 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002104 struct drm_crtc_commit *commit;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07002105 void *obj, *obj_state;
2106 const struct drm_private_state_funcs *funcs;
Daniel Vettera095caa2016-06-08 17:15:36 +02002107
2108 if (stall) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002109 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002110 spin_lock(&crtc->commit_lock);
2111 commit = list_first_entry_or_null(&crtc->commit_list,
2112 struct drm_crtc_commit, commit_entry);
2113 if (commit)
2114 drm_crtc_commit_get(commit);
2115 spin_unlock(&crtc->commit_lock);
2116
2117 if (!commit)
2118 continue;
2119
2120 ret = wait_for_completion_timeout(&commit->hw_done,
2121 10*HZ);
2122 if (ret == 0)
2123 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2124 crtc->base.id, crtc->name);
2125 drm_crtc_commit_put(commit);
2126 }
2127 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002128
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002129 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002130 WARN_ON(connector->state != old_conn_state);
2131
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002132 old_conn_state->state = state;
2133 new_conn_state->state = NULL;
2134
2135 state->connectors[i].state = old_conn_state;
2136 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002137 }
2138
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002139 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002140 WARN_ON(crtc->state != old_crtc_state);
2141
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002142 old_crtc_state->state = state;
2143 new_crtc_state->state = NULL;
2144
2145 state->crtcs[i].state = old_crtc_state;
2146 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002147
2148 if (state->crtcs[i].commit) {
2149 spin_lock(&crtc->commit_lock);
2150 list_add(&state->crtcs[i].commit->commit_entry,
2151 &crtc->commit_list);
2152 spin_unlock(&crtc->commit_lock);
2153
2154 state->crtcs[i].commit->event = NULL;
2155 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002156 }
2157
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002158 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002159 WARN_ON(plane->state != old_plane_state);
2160
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002161 old_plane_state->state = state;
2162 new_plane_state->state = NULL;
2163
2164 state->planes[i].state = old_plane_state;
2165 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002166 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07002167
2168 __for_each_private_obj(state, obj, obj_state, i, funcs)
2169 funcs->swap_state(obj, &state->private_objs[i].obj_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002170}
2171EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002172
2173/**
2174 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2175 * @plane: plane object to update
2176 * @crtc: owning CRTC of owning plane
2177 * @fb: framebuffer to flip onto plane
2178 * @crtc_x: x offset of primary plane on crtc
2179 * @crtc_y: y offset of primary plane on crtc
2180 * @crtc_w: width of primary plane rectangle on crtc
2181 * @crtc_h: height of primary plane rectangle on crtc
2182 * @src_x: x offset of @fb for panning
2183 * @src_y: y offset of @fb for panning
2184 * @src_w: width of source rectangle in @fb
2185 * @src_h: height of source rectangle in @fb
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002186 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002187 *
2188 * Provides a default plane update handler using the atomic driver interface.
2189 *
2190 * RETURNS:
2191 * Zero on success, error code on failure
2192 */
2193int drm_atomic_helper_update_plane(struct drm_plane *plane,
2194 struct drm_crtc *crtc,
2195 struct drm_framebuffer *fb,
2196 int crtc_x, int crtc_y,
2197 unsigned int crtc_w, unsigned int crtc_h,
2198 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002199 uint32_t src_w, uint32_t src_h,
2200 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002201{
2202 struct drm_atomic_state *state;
2203 struct drm_plane_state *plane_state;
2204 int ret = 0;
2205
2206 state = drm_atomic_state_alloc(plane->dev);
2207 if (!state)
2208 return -ENOMEM;
2209
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002210 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002211 plane_state = drm_atomic_get_plane_state(state, plane);
2212 if (IS_ERR(plane_state)) {
2213 ret = PTR_ERR(plane_state);
2214 goto fail;
2215 }
2216
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002217 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002218 if (ret != 0)
2219 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002220 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002221 plane_state->crtc_x = crtc_x;
2222 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002223 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002224 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002225 plane_state->src_x = src_x;
2226 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002227 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002228 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002229
Daniel Vetter3671c582015-05-04 15:40:52 +02002230 if (plane == crtc->cursor)
2231 state->legacy_cursor_update = true;
2232
Daniel Vetter042652e2014-07-27 13:46:52 +02002233 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002234fail:
Chris Wilson08536952016-10-14 13:18:18 +01002235 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002236 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002237}
2238EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2239
2240/**
2241 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2242 * @plane: plane to disable
Daniel Vetter19315292017-03-22 22:50:43 +01002243 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002244 *
2245 * Provides a default plane disable handler using the atomic driver interface.
2246 *
2247 * RETURNS:
2248 * Zero on success, error code on failure
2249 */
Daniel Vetter19315292017-03-22 22:50:43 +01002250int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2251 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002252{
2253 struct drm_atomic_state *state;
2254 struct drm_plane_state *plane_state;
2255 int ret = 0;
2256
2257 state = drm_atomic_state_alloc(plane->dev);
2258 if (!state)
2259 return -ENOMEM;
2260
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002261 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002262 plane_state = drm_atomic_get_plane_state(state, plane);
2263 if (IS_ERR(plane_state)) {
2264 ret = PTR_ERR(plane_state);
2265 goto fail;
2266 }
2267
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002268 if (plane_state->crtc && (plane == plane->crtc->cursor))
2269 plane_state->state->legacy_cursor_update = true;
2270
Rob Clarkbbb1e522015-08-25 15:35:58 -04002271 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002272 if (ret != 0)
2273 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002274
Daniel Vetter042652e2014-07-27 13:46:52 +02002275 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002276fail:
Chris Wilson08536952016-10-14 13:18:18 +01002277 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002278 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002279}
2280EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2281
Rob Clarkbbb1e522015-08-25 15:35:58 -04002282/* just used from fb-helper and atomic-helper: */
2283int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2284 struct drm_plane_state *plane_state)
2285{
2286 int ret;
2287
2288 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2289 if (ret != 0)
2290 return ret;
2291
2292 drm_atomic_set_fb_for_plane(plane_state, NULL);
2293 plane_state->crtc_x = 0;
2294 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002295 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002296 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002297 plane_state->src_x = 0;
2298 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002299 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002300 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002301
Rob Clarkbbb1e522015-08-25 15:35:58 -04002302 return 0;
2303}
2304
Daniel Vetter042652e2014-07-27 13:46:52 +02002305static int update_output_state(struct drm_atomic_state *state,
2306 struct drm_mode_set *set)
2307{
2308 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002309 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002310 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002311 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002312 struct drm_connector_state *new_conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002313 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002314
2315 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2316 state->acquire_ctx);
2317 if (ret)
2318 return ret;
2319
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002320 /* First disable all connectors on the target crtc. */
2321 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2322 if (ret)
2323 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002324
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002325 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2326 if (new_conn_state->crtc == set->crtc) {
2327 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Daniel Vetter042652e2014-07-27 13:46:52 +02002328 NULL);
2329 if (ret)
2330 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002331
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002332 /* Make sure legacy setCrtc always re-trains */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002333 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
Daniel Vetter042652e2014-07-27 13:46:52 +02002334 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002335 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002336
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002337 /* Then set all connectors from set->connectors on the target crtc */
2338 for (i = 0; i < set->num_connectors; i++) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002339 new_conn_state = drm_atomic_get_connector_state(state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002340 set->connectors[i]);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002341 if (IS_ERR(new_conn_state))
2342 return PTR_ERR(new_conn_state);
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002343
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002344 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002345 set->crtc);
2346 if (ret)
2347 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002348 }
2349
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002350 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002351 /* Don't update ->enable for the CRTC in the set_config request,
2352 * since a mismatch would indicate a bug in the upper layers.
2353 * The actual modeset code later on will catch any
2354 * inconsistencies here. */
2355 if (crtc == set->crtc)
2356 continue;
2357
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002358 if (!new_crtc_state->connector_mask) {
2359 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002360 NULL);
2361 if (ret < 0)
2362 return ret;
2363
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002364 new_crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002365 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002366 }
2367
2368 return 0;
2369}
2370
2371/**
2372 * drm_atomic_helper_set_config - set a new config from userspace
2373 * @set: mode set configuration
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002374 * @ctx: lock acquisition context
Daniel Vetter042652e2014-07-27 13:46:52 +02002375 *
2376 * Provides a default crtc set_config handler using the atomic driver interface.
2377 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002378 * NOTE: For backwards compatibility with old userspace this automatically
2379 * resets the "link-status" property to GOOD, to force any link
2380 * re-training. The SETCRTC ioctl does not define whether an update does
2381 * need a full modeset or just a plane update, hence we're allowed to do
2382 * that. See also drm_mode_connector_set_link_status_property().
2383 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002384 * Returns:
2385 * Returns 0 on success, negative errno numbers on failure.
2386 */
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002387int drm_atomic_helper_set_config(struct drm_mode_set *set,
2388 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002389{
2390 struct drm_atomic_state *state;
2391 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002392 int ret = 0;
2393
2394 state = drm_atomic_state_alloc(crtc->dev);
2395 if (!state)
2396 return -ENOMEM;
2397
Daniel Vetter38b64412017-03-22 22:50:58 +01002398 state->acquire_ctx = ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002399 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002400 if (ret != 0)
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002401 goto fail;
Daniel Stone819364d2015-05-26 14:36:48 +01002402
Maarten Lankhorst44596b82017-04-06 13:19:00 +02002403 ret = handle_conflicting_encoders(state, true);
2404 if (ret)
2405 return ret;
2406
Daniel Vetter042652e2014-07-27 13:46:52 +02002407 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002408
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002409fail:
Chris Wilson08536952016-10-14 13:18:18 +01002410 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002411 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002412}
2413EXPORT_SYMBOL(drm_atomic_helper_set_config);
2414
Rob Clarkbbb1e522015-08-25 15:35:58 -04002415/* just used from fb-helper and atomic-helper: */
2416int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2417 struct drm_atomic_state *state)
2418{
2419 struct drm_crtc_state *crtc_state;
2420 struct drm_plane_state *primary_state;
2421 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002422 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002423 int ret;
2424
2425 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2426 if (IS_ERR(crtc_state))
2427 return PTR_ERR(crtc_state);
2428
2429 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2430 if (IS_ERR(primary_state))
2431 return PTR_ERR(primary_state);
2432
2433 if (!set->mode) {
2434 WARN_ON(set->fb);
2435 WARN_ON(set->num_connectors);
2436
2437 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2438 if (ret != 0)
2439 return ret;
2440
2441 crtc_state->active = false;
2442
2443 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2444 if (ret != 0)
2445 return ret;
2446
2447 drm_atomic_set_fb_for_plane(primary_state, NULL);
2448
2449 goto commit;
2450 }
2451
2452 WARN_ON(!set->fb);
2453 WARN_ON(!set->num_connectors);
2454
2455 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2456 if (ret != 0)
2457 return ret;
2458
2459 crtc_state->active = true;
2460
2461 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2462 if (ret != 0)
2463 return ret;
2464
Daniel Vetter196cd5d2017-01-25 07:26:56 +01002465 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Ville Syrjälä83926112015-11-16 17:02:34 +02002466
Rob Clarkbbb1e522015-08-25 15:35:58 -04002467 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2468 primary_state->crtc_x = 0;
2469 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02002470 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002471 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002472 primary_state->src_x = set->x << 16;
2473 primary_state->src_y = set->y << 16;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002474 if (drm_rotation_90_or_270(primary_state->rotation)) {
Ville Syrjälä83926112015-11-16 17:02:34 +02002475 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002476 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002477 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02002478 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002479 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002480 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04002481
2482commit:
2483 ret = update_output_state(state, set);
2484 if (ret)
2485 return ret;
2486
2487 return 0;
2488}
2489
Daniel Vetter042652e2014-07-27 13:46:52 +02002490/**
Thierry Reding14942762015-12-02 17:50:04 +01002491 * drm_atomic_helper_disable_all - disable all currently active outputs
2492 * @dev: DRM device
2493 * @ctx: lock acquisition context
2494 *
2495 * Loops through all connectors, finding those that aren't turned off and then
2496 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2497 * that they are connected to.
2498 *
2499 * This is used for example in suspend/resume to disable all currently active
Daniel Vetter18dddad2017-03-21 17:41:49 +01002500 * functions when suspending. If you just want to shut down everything at e.g.
2501 * driver unload, look at drm_atomic_helper_shutdown().
Thierry Reding14942762015-12-02 17:50:04 +01002502 *
2503 * Note that if callers haven't already acquired all modeset locks this might
2504 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2505 *
2506 * Returns:
2507 * 0 on success or a negative error code on failure.
2508 *
2509 * See also:
Daniel Vetter18dddad2017-03-21 17:41:49 +01002510 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
2511 * drm_atomic_helper_shutdown().
Thierry Reding14942762015-12-02 17:50:04 +01002512 */
2513int drm_atomic_helper_disable_all(struct drm_device *dev,
2514 struct drm_modeset_acquire_ctx *ctx)
2515{
2516 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002517 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01002518 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002519 struct drm_plane_state *plane_state;
2520 struct drm_plane *plane;
2521 struct drm_crtc_state *crtc_state;
2522 struct drm_crtc *crtc;
2523 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01002524
2525 state = drm_atomic_state_alloc(dev);
2526 if (!state)
2527 return -ENOMEM;
2528
2529 state->acquire_ctx = ctx;
2530
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002531 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01002532 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2533 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002534 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01002535 goto free;
2536 }
2537
2538 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002539
2540 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2541 if (ret < 0)
2542 goto free;
2543
2544 ret = drm_atomic_add_affected_planes(state, crtc);
2545 if (ret < 0)
2546 goto free;
2547
2548 ret = drm_atomic_add_affected_connectors(state, crtc);
2549 if (ret < 0)
2550 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01002551 }
2552
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002553 for_each_connector_in_state(state, conn, conn_state, i) {
2554 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2555 if (ret < 0)
2556 goto free;
2557 }
2558
2559 for_each_plane_in_state(state, plane, plane_state, i) {
2560 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2561 if (ret < 0)
2562 goto free;
2563
2564 drm_atomic_set_fb_for_plane(plane_state, NULL);
2565 }
2566
2567 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01002568free:
Chris Wilson08536952016-10-14 13:18:18 +01002569 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002570 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01002571}
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002572
Thierry Reding14942762015-12-02 17:50:04 +01002573EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2574
2575/**
Daniel Vetter18dddad2017-03-21 17:41:49 +01002576 * drm_atomic_helper_shutdown - shutdown all CRTC
2577 * @dev: DRM device
2578 *
2579 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
2580 * suspend should instead be handled with drm_atomic_helper_suspend(), since
2581 * that also takes a snapshot of the modeset state to be restored on resume.
2582 *
2583 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
2584 * and it is the atomic version of drm_crtc_force_disable_all().
2585 */
2586void drm_atomic_helper_shutdown(struct drm_device *dev)
2587{
2588 struct drm_modeset_acquire_ctx ctx;
2589 int ret;
2590
2591 drm_modeset_acquire_init(&ctx, 0);
2592 while (1) {
2593 ret = drm_modeset_lock_all_ctx(dev, &ctx);
2594 if (!ret)
2595 ret = drm_atomic_helper_disable_all(dev, &ctx);
2596
2597 if (ret != -EDEADLK)
2598 break;
2599
2600 drm_modeset_backoff(&ctx);
2601 }
2602
2603 if (ret)
2604 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
2605
2606 drm_modeset_drop_locks(&ctx);
2607 drm_modeset_acquire_fini(&ctx);
2608}
2609EXPORT_SYMBOL(drm_atomic_helper_shutdown);
2610
2611/**
Thierry Reding14942762015-12-02 17:50:04 +01002612 * drm_atomic_helper_suspend - subsystem-level suspend helper
2613 * @dev: DRM device
2614 *
2615 * Duplicates the current atomic state, disables all active outputs and then
2616 * returns a pointer to the original atomic state to the caller. Drivers can
2617 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2618 * restore the output configuration that was active at the time the system
2619 * entered suspend.
2620 *
2621 * Note that it is potentially unsafe to use this. The atomic state object
2622 * returned by this function is assumed to be persistent. Drivers must ensure
2623 * that this holds true. Before calling this function, drivers must make sure
2624 * to suspend fbdev emulation so that nothing can be using the device.
2625 *
2626 * Returns:
2627 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2628 * encoded error code on failure. Drivers should store the returned atomic
2629 * state object and pass it to the drm_atomic_helper_resume() helper upon
2630 * resume.
2631 *
2632 * See also:
2633 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002634 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01002635 */
2636struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2637{
2638 struct drm_modeset_acquire_ctx ctx;
2639 struct drm_atomic_state *state;
2640 int err;
2641
2642 drm_modeset_acquire_init(&ctx, 0);
2643
2644retry:
2645 err = drm_modeset_lock_all_ctx(dev, &ctx);
2646 if (err < 0) {
2647 state = ERR_PTR(err);
2648 goto unlock;
2649 }
2650
2651 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2652 if (IS_ERR(state))
2653 goto unlock;
2654
2655 err = drm_atomic_helper_disable_all(dev, &ctx);
2656 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01002657 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01002658 state = ERR_PTR(err);
2659 goto unlock;
2660 }
2661
2662unlock:
2663 if (PTR_ERR(state) == -EDEADLK) {
2664 drm_modeset_backoff(&ctx);
2665 goto retry;
2666 }
2667
2668 drm_modeset_drop_locks(&ctx);
2669 drm_modeset_acquire_fini(&ctx);
2670 return state;
2671}
2672EXPORT_SYMBOL(drm_atomic_helper_suspend);
2673
2674/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002675 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2676 * @state: duplicated atomic state to commit
2677 * @ctx: pointer to acquire_ctx to use for commit.
2678 *
2679 * The state returned by drm_atomic_helper_duplicate_state() and
2680 * drm_atomic_helper_suspend() is partially invalid, and needs to
2681 * be fixed up before commit.
2682 *
2683 * Returns:
2684 * 0 on success or a negative error code on failure.
2685 *
2686 * See also:
2687 * drm_atomic_helper_suspend()
2688 */
2689int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2690 struct drm_modeset_acquire_ctx *ctx)
2691{
2692 int i;
2693 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002694 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002695 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002696 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002697 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002698 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002699
2700 state->acquire_ctx = ctx;
2701
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002702 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002703 state->planes[i].old_state = plane->state;
2704
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002705 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002706 state->crtcs[i].old_state = crtc->state;
2707
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002708 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002709 state->connectors[i].old_state = connector->state;
2710
2711 return drm_atomic_commit(state);
2712}
2713EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2714
2715/**
Thierry Reding14942762015-12-02 17:50:04 +01002716 * drm_atomic_helper_resume - subsystem-level resume helper
2717 * @dev: DRM device
2718 * @state: atomic state to resume to
2719 *
2720 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2721 * grabs all modeset locks and commits the atomic state object. This can be
2722 * used in conjunction with the drm_atomic_helper_suspend() helper to
2723 * implement suspend/resume for drivers that support atomic mode-setting.
2724 *
2725 * Returns:
2726 * 0 on success or a negative error code on failure.
2727 *
2728 * See also:
2729 * drm_atomic_helper_suspend()
2730 */
2731int drm_atomic_helper_resume(struct drm_device *dev,
2732 struct drm_atomic_state *state)
2733{
Daniel Vettera5b84442017-04-03 10:32:53 +02002734 struct drm_modeset_acquire_ctx ctx;
Thierry Reding14942762015-12-02 17:50:04 +01002735 int err;
2736
2737 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002738
Daniel Vettera5b84442017-04-03 10:32:53 +02002739 drm_modeset_acquire_init(&ctx, 0);
2740 while (1) {
2741 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
2742 if (err != -EDEADLK)
2743 break;
2744
2745 drm_modeset_backoff(&ctx);
2746 }
2747
2748 drm_modeset_drop_locks(&ctx);
2749 drm_modeset_acquire_fini(&ctx);
Thierry Reding14942762015-12-02 17:50:04 +01002750
2751 return err;
2752}
2753EXPORT_SYMBOL(drm_atomic_helper_resume);
2754
2755/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002756 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002757 * @crtc: DRM crtc
2758 * @property: DRM property
2759 * @val: value of property
2760 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002761 * Provides a default crtc set_property handler using the atomic driver
2762 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002763 *
2764 * RETURNS:
2765 * Zero on success, error code on failure
2766 */
2767int
2768drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2769 struct drm_property *property,
2770 uint64_t val)
2771{
2772 struct drm_atomic_state *state;
2773 struct drm_crtc_state *crtc_state;
2774 int ret = 0;
2775
2776 state = drm_atomic_state_alloc(crtc->dev);
2777 if (!state)
2778 return -ENOMEM;
2779
2780 /* ->set_property is always called with all locks held. */
2781 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2782retry:
2783 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2784 if (IS_ERR(crtc_state)) {
2785 ret = PTR_ERR(crtc_state);
2786 goto fail;
2787 }
2788
Rob Clark40ecc692014-12-18 16:01:46 -05002789 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2790 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002791 if (ret)
2792 goto fail;
2793
2794 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002795fail:
2796 if (ret == -EDEADLK)
2797 goto backoff;
2798
Chris Wilson08536952016-10-14 13:18:18 +01002799 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002800 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002801
Daniel Vetter042652e2014-07-27 13:46:52 +02002802backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002803 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002804 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002805
2806 goto retry;
2807}
2808EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2809
2810/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002811 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002812 * @plane: DRM plane
2813 * @property: DRM property
2814 * @val: value of property
2815 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002816 * Provides a default plane set_property handler using the atomic driver
2817 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002818 *
2819 * RETURNS:
2820 * Zero on success, error code on failure
2821 */
2822int
2823drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2824 struct drm_property *property,
2825 uint64_t val)
2826{
2827 struct drm_atomic_state *state;
2828 struct drm_plane_state *plane_state;
2829 int ret = 0;
2830
2831 state = drm_atomic_state_alloc(plane->dev);
2832 if (!state)
2833 return -ENOMEM;
2834
2835 /* ->set_property is always called with all locks held. */
2836 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2837retry:
2838 plane_state = drm_atomic_get_plane_state(state, plane);
2839 if (IS_ERR(plane_state)) {
2840 ret = PTR_ERR(plane_state);
2841 goto fail;
2842 }
2843
Rob Clark40ecc692014-12-18 16:01:46 -05002844 ret = drm_atomic_plane_set_property(plane, plane_state,
2845 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002846 if (ret)
2847 goto fail;
2848
2849 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002850fail:
2851 if (ret == -EDEADLK)
2852 goto backoff;
2853
Chris Wilson08536952016-10-14 13:18:18 +01002854 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002855 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002856
Daniel Vetter042652e2014-07-27 13:46:52 +02002857backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002858 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002859 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002860
2861 goto retry;
2862}
2863EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2864
2865/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002866 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002867 * @connector: DRM connector
2868 * @property: DRM property
2869 * @val: value of property
2870 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002871 * Provides a default connector set_property handler using the atomic driver
2872 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002873 *
2874 * RETURNS:
2875 * Zero on success, error code on failure
2876 */
2877int
2878drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2879 struct drm_property *property,
2880 uint64_t val)
2881{
2882 struct drm_atomic_state *state;
2883 struct drm_connector_state *connector_state;
2884 int ret = 0;
2885
2886 state = drm_atomic_state_alloc(connector->dev);
2887 if (!state)
2888 return -ENOMEM;
2889
2890 /* ->set_property is always called with all locks held. */
2891 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2892retry:
2893 connector_state = drm_atomic_get_connector_state(state, connector);
2894 if (IS_ERR(connector_state)) {
2895 ret = PTR_ERR(connector_state);
2896 goto fail;
2897 }
2898
Rob Clark40ecc692014-12-18 16:01:46 -05002899 ret = drm_atomic_connector_set_property(connector, connector_state,
2900 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002901 if (ret)
2902 goto fail;
2903
2904 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002905fail:
2906 if (ret == -EDEADLK)
2907 goto backoff;
2908
Chris Wilson08536952016-10-14 13:18:18 +01002909 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002910 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002911
Daniel Vetter042652e2014-07-27 13:46:52 +02002912backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002913 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002914 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002915
2916 goto retry;
2917}
2918EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002919
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002920static int page_flip_common(
2921 struct drm_atomic_state *state,
2922 struct drm_crtc *crtc,
2923 struct drm_framebuffer *fb,
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002924 struct drm_pending_vblank_event *event,
2925 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002926{
2927 struct drm_plane *plane = crtc->primary;
2928 struct drm_plane_state *plane_state;
2929 struct drm_crtc_state *crtc_state;
2930 int ret = 0;
2931
2932 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2933 if (IS_ERR(crtc_state))
2934 return PTR_ERR(crtc_state);
2935
2936 crtc_state->event = event;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002937 crtc_state->pageflip_flags = flags;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002938
2939 plane_state = drm_atomic_get_plane_state(state, plane);
2940 if (IS_ERR(plane_state))
2941 return PTR_ERR(plane_state);
2942
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002943 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2944 if (ret != 0)
2945 return ret;
2946 drm_atomic_set_fb_for_plane(plane_state, fb);
2947
2948 /* Make sure we don't accidentally do a full modeset. */
2949 state->allow_modeset = false;
2950 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00002951 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
2952 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002953 return -EINVAL;
2954 }
2955
2956 return ret;
2957}
2958
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002959/**
2960 * drm_atomic_helper_page_flip - execute a legacy page flip
2961 * @crtc: DRM crtc
2962 * @fb: DRM framebuffer
2963 * @event: optional DRM event to signal upon completion
2964 * @flags: flip flags for non-vblank sync'ed updates
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002965 * @ctx: lock acquisition context
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002966 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002967 * Provides a default &drm_crtc_funcs.page_flip implementation
2968 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002969 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002970 * Returns:
2971 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002972 *
2973 * See also:
2974 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002975 */
2976int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2977 struct drm_framebuffer *fb,
2978 struct drm_pending_vblank_event *event,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002979 uint32_t flags,
2980 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002981{
2982 struct drm_plane *plane = crtc->primary;
2983 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002984 int ret = 0;
2985
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002986 state = drm_atomic_state_alloc(plane->dev);
2987 if (!state)
2988 return -ENOMEM;
2989
Daniel Vetter043e7fb2017-03-22 22:50:51 +01002990 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002991
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002992 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002993 if (ret != 0)
2994 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01002995
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002996 ret = drm_atomic_nonblocking_commit(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002997fail:
Chris Wilson08536952016-10-14 13:18:18 +01002998 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002999 return ret;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003000}
3001EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01003002
3003/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003004 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3005 * @crtc: DRM crtc
3006 * @fb: DRM framebuffer
3007 * @event: optional DRM event to signal upon completion
3008 * @flags: flip flags for non-vblank sync'ed updates
3009 * @target: specifying the target vblank period when the flip to take effect
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003010 * @ctx: lock acquisition context
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003011 *
3012 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3013 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3014 * target vblank period to flip.
3015 *
3016 * Returns:
3017 * Returns 0 on success, negative errno numbers on failure.
3018 */
3019int drm_atomic_helper_page_flip_target(
3020 struct drm_crtc *crtc,
3021 struct drm_framebuffer *fb,
3022 struct drm_pending_vblank_event *event,
3023 uint32_t flags,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003024 uint32_t target,
3025 struct drm_modeset_acquire_ctx *ctx)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003026{
3027 struct drm_plane *plane = crtc->primary;
3028 struct drm_atomic_state *state;
3029 struct drm_crtc_state *crtc_state;
3030 int ret = 0;
3031
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003032 state = drm_atomic_state_alloc(plane->dev);
3033 if (!state)
3034 return -ENOMEM;
3035
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003036 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003037
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003038 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003039 if (ret != 0)
3040 goto fail;
3041
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01003042 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003043 if (WARN_ON(!crtc_state)) {
3044 ret = -EINVAL;
3045 goto fail;
3046 }
3047 crtc_state->target_vblank = target;
3048
3049 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003050fail:
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003051 drm_atomic_state_put(state);
3052 return ret;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003053}
3054EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3055
3056/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003057 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
3058 * @connector: affected connector
3059 * @mode: DPMS mode
3060 *
3061 * This is the main helper function provided by the atomic helper framework for
3062 * implementing the legacy DPMS connector interface. It computes the new desired
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003063 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
3064 * enabled) and updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003065 *
3066 * Returns:
3067 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003068 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003069int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
3070 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003071{
3072 struct drm_mode_config *config = &connector->dev->mode_config;
3073 struct drm_atomic_state *state;
3074 struct drm_crtc_state *crtc_state;
3075 struct drm_crtc *crtc;
3076 struct drm_connector *tmp_connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003077 struct drm_connector_list_iter conn_iter;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003078 int ret;
3079 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003080 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003081
3082 if (mode != DRM_MODE_DPMS_ON)
3083 mode = DRM_MODE_DPMS_OFF;
3084
3085 connector->dpms = mode;
3086 crtc = connector->state->crtc;
3087
3088 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003089 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003090
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003091 state = drm_atomic_state_alloc(connector->dev);
3092 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003093 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003094
Daniel Vetterb260ac32017-04-03 10:32:52 +02003095 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003096retry:
3097 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003098 if (IS_ERR(crtc_state)) {
3099 ret = PTR_ERR(crtc_state);
3100 goto fail;
3101 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003102
3103 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
3104
Thierry Redingb982dab2017-02-28 15:46:43 +01003105 drm_connector_list_iter_begin(connector->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003106 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
John Hunter0388df02015-03-17 15:30:28 +08003107 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003108 continue;
3109
John Hunter0388df02015-03-17 15:30:28 +08003110 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003111 active = true;
3112 break;
3113 }
3114 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003115 drm_connector_list_iter_end(&conn_iter);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003116 crtc_state->active = active;
3117
3118 ret = drm_atomic_commit(state);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003119fail:
3120 if (ret == -EDEADLK)
3121 goto backoff;
Marta Lofstedt8f5040e2016-12-05 14:04:08 +02003122 if (ret != 0)
3123 connector->dpms = old_mode;
Chris Wilson08536952016-10-14 13:18:18 +01003124 drm_atomic_state_put(state);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003125 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003126
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003127backoff:
3128 drm_atomic_state_clear(state);
3129 drm_atomic_legacy_backoff(state);
3130
3131 goto retry;
3132}
3133EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3134
3135/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003136 * drm_atomic_helper_best_encoder - Helper for
3137 * &drm_connector_helper_funcs.best_encoder callback
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003138 * @connector: Connector control structure
3139 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003140 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003141 * connectors that support exactly 1 encoder, statically determined at driver
3142 * init time.
3143 */
3144struct drm_encoder *
3145drm_atomic_helper_best_encoder(struct drm_connector *connector)
3146{
3147 WARN_ON(connector->encoder_ids[1]);
3148 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3149}
3150EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3151
3152/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003153 * DOC: atomic state reset and initialization
3154 *
3155 * Both the drm core and the atomic helpers assume that there is always the full
3156 * and correct atomic software state for all connectors, CRTCs and planes
3157 * available. Which is a bit a problem on driver load and also after system
3158 * suspend. One way to solve this is to have a hardware state read-out
3159 * infrastructure which reconstructs the full software state (e.g. the i915
3160 * driver).
3161 *
3162 * The simpler solution is to just reset the software state to everything off,
3163 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3164 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01003165 *
3166 * On the upside the precise state tracking of atomic simplifies system suspend
3167 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3168 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3169 * For other drivers the building blocks are split out, see the documentation
3170 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003171 */
3172
3173/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003174 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
Daniel Vetterd4617012014-11-03 15:56:43 +01003175 * @crtc: drm CRTC
3176 *
3177 * Resets the atomic state for @crtc by freeing the state pointer (which might
3178 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3179 */
3180void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3181{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003182 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003183 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003184
Daniel Vetterd4617012014-11-03 15:56:43 +01003185 kfree(crtc->state);
3186 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003187
3188 if (crtc->state)
3189 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01003190}
3191EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3192
3193/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003194 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3195 * @crtc: CRTC object
3196 * @state: atomic CRTC state
3197 *
3198 * Copies atomic state from a CRTC's current state and resets inferred values.
3199 * This is useful for drivers that subclass the CRTC state.
3200 */
3201void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3202 struct drm_crtc_state *state)
3203{
3204 memcpy(state, crtc->state, sizeof(*state));
3205
Daniel Stone99cf4a22015-05-25 19:11:51 +01003206 if (state->mode_blob)
Thierry Reding6472e502017-02-28 15:46:42 +01003207 drm_property_blob_get(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003208 if (state->degamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003209 drm_property_blob_get(state->degamma_lut);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003210 if (state->ctm)
Thierry Reding6472e502017-02-28 15:46:42 +01003211 drm_property_blob_get(state->ctm);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003212 if (state->gamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003213 drm_property_blob_get(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003214 state->mode_changed = false;
3215 state->active_changed = false;
3216 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02003217 state->connectors_changed = false;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003218 state->color_mgmt_changed = false;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02003219 state->zpos_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01003220 state->event = NULL;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003221 state->pageflip_flags = 0;
Thierry Redingf5e78402015-01-28 14:54:32 +01003222}
3223EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3224
3225/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003226 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3227 * @crtc: drm CRTC
3228 *
3229 * Default CRTC state duplicate hook for drivers which don't have their own
3230 * subclassed CRTC state structure.
3231 */
3232struct drm_crtc_state *
3233drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3234{
3235 struct drm_crtc_state *state;
3236
3237 if (WARN_ON(!crtc->state))
3238 return NULL;
3239
Thierry Redingf5e78402015-01-28 14:54:32 +01003240 state = kmalloc(sizeof(*state), GFP_KERNEL);
3241 if (state)
3242 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003243
3244 return state;
3245}
3246EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3247
3248/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003249 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
Thierry Redingf5e78402015-01-28 14:54:32 +01003250 * @state: CRTC state object to release
3251 *
3252 * Releases all resources stored in the CRTC state without actually freeing
3253 * the memory of the CRTC state. This is useful for drivers that subclass the
3254 * CRTC state.
3255 */
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003256void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003257{
Thierry Reding6472e502017-02-28 15:46:42 +01003258 drm_property_blob_put(state->mode_blob);
3259 drm_property_blob_put(state->degamma_lut);
3260 drm_property_blob_put(state->ctm);
3261 drm_property_blob_put(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003262}
3263EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3264
3265/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003266 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3267 * @crtc: drm CRTC
3268 * @state: CRTC state object to release
3269 *
3270 * Default CRTC state destroy hook for drivers which don't have their own
3271 * subclassed CRTC state structure.
3272 */
3273void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3274 struct drm_crtc_state *state)
3275{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003276 __drm_atomic_helper_crtc_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003277 kfree(state);
3278}
3279EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3280
3281/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003282 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
Daniel Vetterd4617012014-11-03 15:56:43 +01003283 * @plane: drm plane
3284 *
3285 * Resets the atomic state for @plane by freeing the state pointer (which might
3286 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3287 */
3288void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3289{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003290 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +02003291 __drm_atomic_helper_plane_destroy_state(plane->state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003292
Daniel Vetterd4617012014-11-03 15:56:43 +01003293 kfree(plane->state);
3294 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003295
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003296 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003297 plane->state->plane = plane;
Robert Fossc2c446a2017-05-19 16:50:17 -04003298 plane->state->rotation = DRM_MODE_ROTATE_0;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003299 }
Daniel Vetterd4617012014-11-03 15:56:43 +01003300}
3301EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3302
3303/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003304 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3305 * @plane: plane object
3306 * @state: atomic plane state
3307 *
3308 * Copies atomic state from a plane's current state. This is useful for
3309 * drivers that subclass the plane state.
3310 */
3311void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3312 struct drm_plane_state *state)
3313{
3314 memcpy(state, plane->state, sizeof(*state));
3315
3316 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003317 drm_framebuffer_get(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003318
3319 state->fence = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003320}
3321EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3322
3323/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003324 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3325 * @plane: drm plane
3326 *
3327 * Default plane state duplicate hook for drivers which don't have their own
3328 * subclassed plane state structure.
3329 */
3330struct drm_plane_state *
3331drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3332{
Daniel Vetter321ebf02014-11-04 22:57:27 +01003333 struct drm_plane_state *state;
3334
Daniel Vetterd4617012014-11-03 15:56:43 +01003335 if (WARN_ON(!plane->state))
3336 return NULL;
3337
Thierry Redingf5e78402015-01-28 14:54:32 +01003338 state = kmalloc(sizeof(*state), GFP_KERNEL);
3339 if (state)
3340 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003341
3342 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003343}
3344EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3345
3346/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003347 * __drm_atomic_helper_plane_destroy_state - release plane state
Thierry Redingf5e78402015-01-28 14:54:32 +01003348 * @state: plane state object to release
3349 *
3350 * Releases all resources stored in the plane state without actually freeing
3351 * the memory of the plane state. This is useful for drivers that subclass the
3352 * plane state.
3353 */
Daniel Vetter2f701692016-05-09 16:34:10 +02003354void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003355{
3356 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003357 drm_framebuffer_put(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003358
3359 if (state->fence)
3360 dma_fence_put(state->fence);
Thierry Redingf5e78402015-01-28 14:54:32 +01003361}
3362EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3363
3364/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003365 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3366 * @plane: drm plane
3367 * @state: plane state object to release
3368 *
3369 * Default plane state destroy hook for drivers which don't have their own
3370 * subclassed plane state structure.
3371 */
3372void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01003373 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01003374{
Daniel Vetter2f701692016-05-09 16:34:10 +02003375 __drm_atomic_helper_plane_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003376 kfree(state);
3377}
3378EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3379
3380/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003381 * __drm_atomic_helper_connector_reset - reset state on connector
3382 * @connector: drm connector
3383 * @conn_state: connector state to assign
3384 *
3385 * Initializes the newly allocated @conn_state and assigns it to
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003386 * the &drm_conector->state pointer of @connector, usually required when
3387 * initializing the drivers or when called from the &drm_connector_funcs.reset
3388 * hook.
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003389 *
3390 * This is useful for drivers that subclass the connector state.
3391 */
3392void
3393__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3394 struct drm_connector_state *conn_state)
3395{
3396 if (conn_state)
3397 conn_state->connector = connector;
3398
3399 connector->state = conn_state;
3400}
3401EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3402
3403/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003404 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
Daniel Vetterd4617012014-11-03 15:56:43 +01003405 * @connector: drm connector
3406 *
3407 * Resets the atomic state for @connector by freeing the state pointer (which
3408 * might be NULL, e.g. at driver load time) and allocating a new empty state
3409 * object.
3410 */
3411void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3412{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003413 struct drm_connector_state *conn_state =
3414 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003415
Daniel Vetterb0b55112016-04-22 22:10:29 +02003416 if (connector->state)
Daniel Vetterfabd9102016-05-09 16:34:11 +02003417 __drm_atomic_helper_connector_destroy_state(connector->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003418
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003419 kfree(connector->state);
3420 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003421}
3422EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3423
3424/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003425 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3426 * @connector: connector object
3427 * @state: atomic connector state
3428 *
3429 * Copies atomic state from a connector's current state. This is useful for
3430 * drivers that subclass the connector state.
3431 */
3432void
3433__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3434 struct drm_connector_state *state)
3435{
3436 memcpy(state, connector->state, sizeof(*state));
Dave Airlied2307de2016-04-27 11:27:39 +10003437 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003438 drm_connector_get(connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003439}
3440EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3441
3442/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003443 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3444 * @connector: drm connector
3445 *
3446 * Default connector state duplicate hook for drivers which don't have their own
3447 * subclassed connector state structure.
3448 */
3449struct drm_connector_state *
3450drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3451{
Thierry Redingf5e78402015-01-28 14:54:32 +01003452 struct drm_connector_state *state;
3453
Daniel Vetterd4617012014-11-03 15:56:43 +01003454 if (WARN_ON(!connector->state))
3455 return NULL;
3456
Thierry Redingf5e78402015-01-28 14:54:32 +01003457 state = kmalloc(sizeof(*state), GFP_KERNEL);
3458 if (state)
3459 __drm_atomic_helper_connector_duplicate_state(connector, state);
3460
3461 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003462}
3463EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3464
3465/**
Thierry Reding397fd772015-09-08 15:00:45 +02003466 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3467 * @dev: DRM device
3468 * @ctx: lock acquisition context
3469 *
3470 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01003471 * duplicating their respective states. This is used for example by suspend/
3472 * resume support code to save the state prior to suspend such that it can
3473 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02003474 *
3475 * Note that this treats atomic state as persistent between save and restore.
3476 * Drivers must make sure that this is possible and won't result in confusion
3477 * or erroneous behaviour.
3478 *
3479 * Note that if callers haven't already acquired all modeset locks this might
3480 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3481 *
3482 * Returns:
3483 * A pointer to the copy of the atomic state object on success or an
3484 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01003485 *
3486 * See also:
3487 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02003488 */
3489struct drm_atomic_state *
3490drm_atomic_helper_duplicate_state(struct drm_device *dev,
3491 struct drm_modeset_acquire_ctx *ctx)
3492{
3493 struct drm_atomic_state *state;
3494 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003495 struct drm_connector_list_iter conn_iter;
Thierry Reding397fd772015-09-08 15:00:45 +02003496 struct drm_plane *plane;
3497 struct drm_crtc *crtc;
3498 int err = 0;
3499
3500 state = drm_atomic_state_alloc(dev);
3501 if (!state)
3502 return ERR_PTR(-ENOMEM);
3503
3504 state->acquire_ctx = ctx;
3505
3506 drm_for_each_crtc(crtc, dev) {
3507 struct drm_crtc_state *crtc_state;
3508
3509 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3510 if (IS_ERR(crtc_state)) {
3511 err = PTR_ERR(crtc_state);
3512 goto free;
3513 }
3514 }
3515
3516 drm_for_each_plane(plane, dev) {
3517 struct drm_plane_state *plane_state;
3518
3519 plane_state = drm_atomic_get_plane_state(state, plane);
3520 if (IS_ERR(plane_state)) {
3521 err = PTR_ERR(plane_state);
3522 goto free;
3523 }
3524 }
3525
Thierry Redingb982dab2017-02-28 15:46:43 +01003526 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003527 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding397fd772015-09-08 15:00:45 +02003528 struct drm_connector_state *conn_state;
3529
3530 conn_state = drm_atomic_get_connector_state(state, conn);
3531 if (IS_ERR(conn_state)) {
3532 err = PTR_ERR(conn_state);
Thierry Redingb982dab2017-02-28 15:46:43 +01003533 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003534 goto free;
3535 }
3536 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003537 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003538
3539 /* clear the acquire context so that it isn't accidentally reused */
3540 state->acquire_ctx = NULL;
3541
3542free:
3543 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003544 drm_atomic_state_put(state);
Thierry Reding397fd772015-09-08 15:00:45 +02003545 state = ERR_PTR(err);
3546 }
3547
3548 return state;
3549}
3550EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3551
3552/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003553 * __drm_atomic_helper_connector_destroy_state - release connector state
Thierry Redingf5e78402015-01-28 14:54:32 +01003554 * @state: connector state object to release
3555 *
3556 * Releases all resources stored in the connector state without actually
3557 * freeing the memory of the connector state. This is useful for drivers that
3558 * subclass the connector state.
3559 */
3560void
Daniel Vetterfabd9102016-05-09 16:34:11 +02003561__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003562{
Dave Airlied2307de2016-04-27 11:27:39 +10003563 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003564 drm_connector_put(state->connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003565}
3566EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3567
3568/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003569 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3570 * @connector: drm connector
3571 * @state: connector state object to release
3572 *
3573 * Default connector state destroy hook for drivers which don't have their own
3574 * subclassed connector state structure.
3575 */
3576void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3577 struct drm_connector_state *state)
3578{
Daniel Vetterfabd9102016-05-09 16:34:11 +02003579 __drm_atomic_helper_connector_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003580 kfree(state);
3581}
3582EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003583
3584/**
3585 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3586 * @crtc: CRTC object
3587 * @red: red correction table
3588 * @green: green correction table
3589 * @blue: green correction table
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003590 * @size: size of the tables
Daniel Vetter6d124ff2017-04-03 10:33:01 +02003591 * @ctx: lock acquire context
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003592 *
3593 * Implements support for legacy gamma correction table for drivers
3594 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
Daniel Vetter2e381782017-04-12 17:20:06 +02003595 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3596 * how the atomic color management and gamma tables work.
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003597 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003598int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3599 u16 *red, u16 *green, u16 *blue,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02003600 uint32_t size,
3601 struct drm_modeset_acquire_ctx *ctx)
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003602{
3603 struct drm_device *dev = crtc->dev;
3604 struct drm_mode_config *config = &dev->mode_config;
3605 struct drm_atomic_state *state;
3606 struct drm_crtc_state *crtc_state;
3607 struct drm_property_blob *blob = NULL;
3608 struct drm_color_lut *blob_data;
3609 int i, ret = 0;
3610
3611 state = drm_atomic_state_alloc(crtc->dev);
3612 if (!state)
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003613 return -ENOMEM;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003614
3615 blob = drm_property_create_blob(dev,
3616 sizeof(struct drm_color_lut) * size,
3617 NULL);
Lionel Landwerlin562c5b42016-03-10 12:04:21 +00003618 if (IS_ERR(blob)) {
3619 ret = PTR_ERR(blob);
Lionel Landwerlinc1f415c2016-03-11 12:17:26 +00003620 blob = NULL;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003621 goto fail;
3622 }
3623
3624 /* Prepare GAMMA_LUT with the legacy values. */
3625 blob_data = (struct drm_color_lut *) blob->data;
3626 for (i = 0; i < size; i++) {
3627 blob_data[i].red = red[i];
3628 blob_data[i].green = green[i];
3629 blob_data[i].blue = blue[i];
3630 }
3631
Daniel Vetter3a09f732017-04-03 10:33:02 +02003632 state->acquire_ctx = ctx;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003633 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3634 if (IS_ERR(crtc_state)) {
3635 ret = PTR_ERR(crtc_state);
3636 goto fail;
3637 }
3638
3639 /* Reset DEGAMMA_LUT and CTM properties. */
3640 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3641 config->degamma_lut_property, 0);
3642 if (ret)
3643 goto fail;
3644
3645 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3646 config->ctm_property, 0);
3647 if (ret)
3648 goto fail;
3649
3650 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3651 config->gamma_lut_property, blob->base.id);
3652 if (ret)
3653 goto fail;
3654
3655 ret = drm_atomic_commit(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003656
Daniel Vetter3a09f732017-04-03 10:33:02 +02003657fail:
Chris Wilson08536952016-10-14 13:18:18 +01003658 drm_atomic_state_put(state);
Thierry Reding6472e502017-02-28 15:46:42 +01003659 drm_property_blob_put(blob);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003660 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003661}
3662EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);