blob: 254f6d0fad7c19eb244ff537ff6d234ef2054bb7 [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>
Daniel Vettere2330f02014-10-29 11:34:56 +010033#include <linux/fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Daniel Vetter3150c7d2014-11-06 20:53:29 +010035/**
36 * DOC: overview
37 *
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
43 *
44 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020045 * drm_atomic_helper_check() and for the commit callback with
46 * drm_atomic_helper_commit(). But the individual stages and callbacks are
47 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010048 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020051 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
52 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010053 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010055 *
56 * The atomic helper uses the same function table structures as all other
57 * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58 * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59 * also shares the struct &drm_plane_helper_funcs function table with the plane
60 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010061 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010062static void
63drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 struct drm_plane_state *plane_state,
65 struct drm_plane *plane)
66{
67 struct drm_crtc_state *crtc_state;
68
69 if (plane->state->crtc) {
Rob Clark4b08eae2014-12-08 10:45:43 -050070 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
Daniel Vetterc2fcd272014-11-05 00:14:14 +010071
72 if (WARN_ON(!crtc_state))
73 return;
74
75 crtc_state->planes_changed = true;
76 }
77
78 if (plane_state->crtc) {
79 crtc_state =
80 state->crtc_states[drm_crtc_index(plane_state->crtc)];
81
82 if (WARN_ON(!crtc_state))
83 return;
84
85 crtc_state->planes_changed = true;
86 }
87}
88
Daniel Vetter97ac3202015-12-03 10:49:14 +010089static bool
90check_pending_encoder_assignment(struct drm_atomic_state *state,
Daniel Vetter07bad492015-12-08 09:49:18 +010091 struct drm_encoder *new_encoder)
Daniel Vetter97ac3202015-12-03 10:49:14 +010092{
93 struct drm_connector *connector;
94 struct drm_connector_state *conn_state;
95 int i;
96
97 for_each_connector_in_state(state, connector, conn_state, i) {
98 if (conn_state->best_encoder != new_encoder)
99 continue;
100
101 /* encoder already assigned and we're trying to re-steal it! */
102 if (connector->state->best_encoder != conn_state->best_encoder)
103 return false;
104 }
105
106 return true;
107}
108
Daniel Vetter623369e2014-09-16 17:50:47 +0200109static struct drm_crtc *
110get_current_crtc_for_encoder(struct drm_device *dev,
111 struct drm_encoder *encoder)
112{
113 struct drm_mode_config *config = &dev->mode_config;
114 struct drm_connector *connector;
115
116 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
117
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +0200118 drm_for_each_connector(connector, dev) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200119 if (connector->state->best_encoder != encoder)
120 continue;
121
122 return connector->state->crtc;
123 }
124
125 return NULL;
126}
127
128static int
129steal_encoder(struct drm_atomic_state *state,
130 struct drm_encoder *encoder,
131 struct drm_crtc *encoder_crtc)
132{
133 struct drm_mode_config *config = &state->dev->mode_config;
134 struct drm_crtc_state *crtc_state;
135 struct drm_connector *connector;
136 struct drm_connector_state *connector_state;
137
138 /*
139 * We can only steal an encoder coming from a connector, which means we
140 * must already hold the connection_mutex.
141 */
142 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
143
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200144 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100145 encoder->base.id, encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200146 encoder_crtc->base.id, encoder_crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200147
148 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
149 if (IS_ERR(crtc_state))
150 return PTR_ERR(crtc_state);
151
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200152 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200153
154 list_for_each_entry(connector, &config->connector_list, head) {
155 if (connector->state->best_encoder != encoder)
156 continue;
157
Daniel Vetter17a38d92015-02-22 12:24:16 +0100158 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
159 connector->base.id,
160 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200161
162 connector_state = drm_atomic_get_connector_state(state,
163 connector);
164 if (IS_ERR(connector_state))
165 return PTR_ERR(connector_state);
166
Daniel Vetter623369e2014-09-16 17:50:47 +0200167 connector_state->best_encoder = NULL;
168 }
169
170 return 0;
171}
172
173static int
174update_connector_routing(struct drm_atomic_state *state, int conn_idx)
175{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200176 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200177 struct drm_encoder *new_encoder;
178 struct drm_crtc *encoder_crtc;
179 struct drm_connector *connector;
180 struct drm_connector_state *connector_state;
181 struct drm_crtc_state *crtc_state;
182 int idx, ret;
183
184 connector = state->connectors[conn_idx];
185 connector_state = state->connector_states[conn_idx];
186
187 if (!connector)
188 return 0;
189
Daniel Vetter17a38d92015-02-22 12:24:16 +0100190 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
191 connector->base.id,
192 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200193
194 if (connector->state->crtc != connector_state->crtc) {
195 if (connector->state->crtc) {
196 idx = drm_crtc_index(connector->state->crtc);
197
198 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200199 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200200 }
201
202 if (connector_state->crtc) {
203 idx = drm_crtc_index(connector_state->crtc);
204
205 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200206 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200207 }
208 }
209
210 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100211 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200212 connector->base.id,
213 connector->name);
214
215 connector_state->best_encoder = NULL;
216
217 return 0;
218 }
219
220 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200221
222 if (funcs->atomic_best_encoder)
223 new_encoder = funcs->atomic_best_encoder(connector,
224 connector_state);
225 else
226 new_encoder = funcs->best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200227
228 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100229 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
230 connector->base.id,
231 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200232 return -EINVAL;
233 }
234
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100235 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
236 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
237 new_encoder->base.id,
238 new_encoder->name,
239 connector_state->crtc->base.id);
240 return -EINVAL;
241 }
242
Daniel Vetter623369e2014-09-16 17:50:47 +0200243 if (new_encoder == connector_state->best_encoder) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200244 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100245 connector->base.id,
246 connector->name,
247 new_encoder->base.id,
248 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200249 connector_state->crtc->base.id,
250 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200251
252 return 0;
253 }
254
Daniel Vetter07bad492015-12-08 09:49:18 +0100255 if (!check_pending_encoder_assignment(state, new_encoder)) {
Daniel Vetter97ac3202015-12-03 10:49:14 +0100256 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
257 connector->base.id,
258 connector->name);
259 return -EINVAL;
260 }
261
Daniel Vetter623369e2014-09-16 17:50:47 +0200262 encoder_crtc = get_current_crtc_for_encoder(state->dev,
263 new_encoder);
264
265 if (encoder_crtc) {
266 ret = steal_encoder(state, new_encoder, encoder_crtc);
267 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100268 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
269 connector->base.id,
270 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200271 return ret;
272 }
273 }
274
Daniel Vetter6ea76f3c2015-08-03 17:24:11 +0200275 if (WARN_ON(!connector_state->crtc))
276 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200277
Daniel Vetter623369e2014-09-16 17:50:47 +0200278 connector_state->best_encoder = new_encoder;
279 idx = drm_crtc_index(connector_state->crtc);
280
281 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200282 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200283
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200284 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100285 connector->base.id,
286 connector->name,
287 new_encoder->base.id,
288 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200289 connector_state->crtc->base.id,
290 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200291
292 return 0;
293}
294
295static int
296mode_fixup(struct drm_atomic_state *state)
297{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300298 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200299 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300300 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200301 struct drm_connector_state *conn_state;
302 int i;
303 bool ret;
304
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300305 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200306 if (!crtc_state->mode_changed &&
307 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200308 continue;
309
310 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
311 }
312
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300313 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200314 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200315 struct drm_encoder *encoder;
316
Daniel Vetter623369e2014-09-16 17:50:47 +0200317 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
318
319 if (!conn_state->crtc || !conn_state->best_encoder)
320 continue;
321
322 crtc_state =
323 state->crtc_states[drm_crtc_index(conn_state->crtc)];
324
325 /*
326 * Each encoder has at most one connector (since we always steal
327 * it away), so we won't call ->mode_fixup twice.
328 */
329 encoder = conn_state->best_encoder;
330 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300331 if (!funcs)
332 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200333
Archit Taneja862e6862015-05-21 11:03:16 +0530334 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
335 &crtc_state->adjusted_mode);
336 if (!ret) {
337 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
338 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200339 }
340
Thierry Reding4cd4df82014-12-03 16:44:34 +0100341 if (funcs->atomic_check) {
342 ret = funcs->atomic_check(encoder, crtc_state,
343 conn_state);
344 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100345 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
346 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100347 return ret;
348 }
Inki Dae84524912015-08-11 21:23:49 +0900349 } else if (funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100350 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
351 &crtc_state->adjusted_mode);
352 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100353 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
354 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100355 return -EINVAL;
356 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200357 }
358 }
359
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300360 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200361 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200362
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200363 if (!crtc_state->mode_changed &&
364 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200365 continue;
366
367 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300368 if (!funcs->mode_fixup)
369 continue;
370
Daniel Vetter623369e2014-09-16 17:50:47 +0200371 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
372 &crtc_state->adjusted_mode);
373 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200374 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
375 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200376 return -EINVAL;
377 }
378 }
379
380 return 0;
381}
382
Daniel Vetterd9b13622014-11-26 16:57:41 +0100383/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800384 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100385 * @dev: DRM device
386 * @state: the driver state object
387 *
388 * Check the state object to see if the requested state is physically possible.
389 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200390 * update and adds any additional connectors needed for full modesets and calls
391 * down into ->mode_fixup functions of the driver backend.
392 *
393 * crtc_state->mode_changed is set when the input mode is changed.
394 * crtc_state->connectors_changed is set when a connector is added or
395 * removed from the crtc.
396 * crtc_state->active_changed is set when crtc_state->active changes,
397 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100398 *
399 * IMPORTANT:
400 *
401 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
402 * plane update can't be done without a full modeset) _must_ call this function
403 * afterwards after that change. It is permitted to call this function multiple
404 * times for the same update, e.g. when the ->atomic_check functions depend upon
405 * the adjusted dotclock for fifo space allocation and watermark computation.
406 *
407 * RETURNS
408 * Zero for success or -errno
409 */
410int
Rob Clark934ce1c2014-11-19 16:41:33 -0500411drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200412 struct drm_atomic_state *state)
413{
Daniel Vetter623369e2014-09-16 17:50:47 +0200414 struct drm_crtc *crtc;
415 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300416 struct drm_connector *connector;
417 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200418 int i, ret;
419
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300420 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200421 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200422 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
423 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200424 crtc_state->mode_changed = true;
425 }
426
427 if (crtc->state->enable != crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200428 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
429 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200430
431 /*
432 * For clarity this assignment is done here, but
433 * enable == 0 is only true when there are no
434 * connectors and a NULL mode.
435 *
436 * The other way around is true as well. enable != 0
437 * iff connectors are attached and a mode is set.
438 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200439 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200440 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200441 }
442 }
443
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300444 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200445 /*
446 * This only sets crtc->mode_changed for routing changes,
447 * drivers must set crtc->mode_changed themselves when connector
448 * properties need to be updated.
449 */
450 ret = update_connector_routing(state, i);
451 if (ret)
452 return ret;
453 }
454
455 /*
456 * After all the routing has been prepared we need to add in any
457 * connector which is itself unchanged, but who's crtc changes it's
458 * configuration. This must be done before calling mode_fixup in case a
459 * crtc only changed its mode but has the same set of connectors.
460 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300461 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100462 bool has_connectors =
463 !!crtc_state->connector_mask;
Daniel Vetter623369e2014-09-16 17:50:47 +0200464
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100465 /*
466 * We must set ->active_changed after walking connectors for
467 * otherwise an update that only changes active would result in
468 * a full modeset because update_connector_routing force that.
469 */
470 if (crtc->state->active != crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200471 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
472 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100473 crtc_state->active_changed = true;
474 }
475
Daniel Vetter2465ff62015-06-18 09:58:55 +0200476 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100477 continue;
478
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200479 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
480 crtc->base.id, crtc->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100481 crtc_state->enable ? 'y' : 'n',
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200482 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200483
484 ret = drm_atomic_add_affected_connectors(state, crtc);
485 if (ret != 0)
486 return ret;
487
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200488 ret = drm_atomic_add_affected_planes(state, crtc);
489 if (ret != 0)
490 return ret;
491
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100492 if (crtc_state->enable != has_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200493 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
494 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200495
496 return -EINVAL;
497 }
498 }
499
500 return mode_fixup(state);
501}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100502EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200503
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100504/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800505 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100506 * @dev: DRM device
507 * @state: the driver state object
508 *
509 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100510 * This does all the plane update related checks using by calling into the
511 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100512 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200513 * It also sets crtc_state->planes_changed to indicate that a crtc has
514 * updated planes.
515 *
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100516 * RETURNS
517 * Zero for success or -errno
518 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100519int
520drm_atomic_helper_check_planes(struct drm_device *dev,
521 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100522{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300523 struct drm_crtc *crtc;
524 struct drm_crtc_state *crtc_state;
525 struct drm_plane *plane;
526 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100527 int i, ret = 0;
528
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300529 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200530 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100531
532 funcs = plane->helper_private;
533
534 drm_atomic_helper_plane_changed(state, plane_state, plane);
535
536 if (!funcs || !funcs->atomic_check)
537 continue;
538
539 ret = funcs->atomic_check(plane, plane_state);
540 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200541 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
542 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100543 return ret;
544 }
545 }
546
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300547 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200548 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100549
550 funcs = crtc->helper_private;
551
552 if (!funcs || !funcs->atomic_check)
553 continue;
554
555 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
556 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200557 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
558 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100559 return ret;
560 }
561 }
562
Daniel Vetterd9b13622014-11-26 16:57:41 +0100563 return ret;
564}
565EXPORT_SYMBOL(drm_atomic_helper_check_planes);
566
567/**
568 * drm_atomic_helper_check - validate state object
569 * @dev: DRM device
570 * @state: the driver state object
571 *
572 * Check the state object to see if the requested state is physically possible.
573 * Only crtcs and planes have check callbacks, so for any additional (global)
574 * checking that a driver needs it can simply wrap that around this function.
575 * Drivers without such needs can directly use this as their ->atomic_check()
576 * callback.
577 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100578 * This just wraps the two parts of the state checking for planes and modeset
579 * state in the default order: First it calls drm_atomic_helper_check_modeset()
580 * and then drm_atomic_helper_check_planes(). The assumption is that the
581 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
582 * e.g. properly compute watermarks.
583 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100584 * RETURNS
585 * Zero for success or -errno
586 */
587int drm_atomic_helper_check(struct drm_device *dev,
588 struct drm_atomic_state *state)
589{
590 int ret;
591
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100592 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100593 if (ret)
594 return ret;
595
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100596 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500597 if (ret)
598 return ret;
599
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100600 return ret;
601}
602EXPORT_SYMBOL(drm_atomic_helper_check);
603
Daniel Vetter623369e2014-09-16 17:50:47 +0200604static void
605disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
606{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300607 struct drm_connector *connector;
608 struct drm_connector_state *old_conn_state;
609 struct drm_crtc *crtc;
610 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200611 int i;
612
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300613 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200614 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200615 struct drm_encoder *encoder;
616
Daniel Vetter623369e2014-09-16 17:50:47 +0200617 /* Shut down everything that's in the changeset and currently
618 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300619 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200620 continue;
621
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100622 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
623
Daniel Vetter4218a322015-03-26 22:18:40 +0100624 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200625 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100626 continue;
627
Rob Clark46df9ad2014-11-20 15:40:36 -0500628 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200629
Rob Clark46df9ad2014-11-20 15:40:36 -0500630 /* We shouldn't get this far if we didn't previously have
631 * an encoder.. but WARN_ON() rather than explode.
632 */
633 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200634 continue;
635
636 funcs = encoder->helper_private;
637
Daniel Vetter17a38d92015-02-22 12:24:16 +0100638 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
639 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100640
Daniel Vetter623369e2014-09-16 17:50:47 +0200641 /*
642 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800643 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200644 */
Archit Taneja862e6862015-05-21 11:03:16 +0530645 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200646
647 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100648 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200649 funcs->prepare(encoder);
650 else if (funcs->disable)
651 funcs->disable(encoder);
652 else
653 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
654
Archit Taneja862e6862015-05-21 11:03:16 +0530655 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200656 }
657
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300658 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200659 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200660
661 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200662 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100663 continue;
664
665 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200666 continue;
667
668 funcs = crtc->helper_private;
669
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200670 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
671 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100672
673
Daniel Vetter623369e2014-09-16 17:50:47 +0200674 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100675 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200676 funcs->prepare(crtc);
677 else if (funcs->disable)
678 funcs->disable(crtc);
679 else
680 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
681 }
682}
683
Daniel Vetter4c18d302015-05-12 15:27:37 +0200684/**
685 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
686 * @dev: DRM device
687 * @old_state: atomic state object with old state structures
688 *
689 * This function updates all the various legacy modeset state pointers in
690 * connectors, encoders and crtcs. It also updates the timestamping constants
691 * used for precise vblank timestamps by calling
692 * drm_calc_timestamping_constants().
693 *
694 * Drivers can use this for building their own atomic commit if they don't have
695 * a pure helper-based modeset implementation.
696 */
697void
698drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
699 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200700{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300701 struct drm_connector *connector;
702 struct drm_connector_state *old_conn_state;
703 struct drm_crtc *crtc;
704 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200705 int i;
706
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200707 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300708 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200709 if (connector->encoder) {
710 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200711
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200712 connector->encoder->crtc = NULL;
713 connector->encoder = NULL;
714 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200715
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200716 crtc = connector->state->crtc;
717 if ((!crtc && old_conn_state->crtc) ||
718 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
719 struct drm_property *dpms_prop =
720 dev->mode_config.dpms_property;
721 int mode = DRM_MODE_DPMS_OFF;
722
723 if (crtc && crtc->state->active)
724 mode = DRM_MODE_DPMS_ON;
725
726 connector->dpms = mode;
727 drm_object_property_set_value(&connector->base,
728 dpms_prop, mode);
729 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200730 }
731
732 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300733 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
734 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200735 continue;
736
737 if (WARN_ON(!connector->state->best_encoder))
738 continue;
739
740 connector->encoder = connector->state->best_encoder;
741 connector->encoder->crtc = connector->state->crtc;
742 }
743
744 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300745 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200746 struct drm_plane *primary = crtc->primary;
747
Daniel Vetter623369e2014-09-16 17:50:47 +0200748 crtc->mode = crtc->state->mode;
749 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200750
751 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
752 primary->state->crtc == crtc) {
753 crtc->x = primary->state->src_x >> 16;
754 crtc->y = primary->state->src_y >> 16;
755 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200756
757 if (crtc->state->enable)
758 drm_calc_timestamping_constants(crtc,
759 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200760 }
761}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200762EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200763
764static void
765crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
766{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300767 struct drm_crtc *crtc;
768 struct drm_crtc_state *old_crtc_state;
769 struct drm_connector *connector;
770 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200771 int i;
772
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300773 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200774 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200775
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300776 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200777 continue;
778
779 funcs = crtc->helper_private;
780
Daniel Vetterc982bd92015-02-22 12:24:20 +0100781 if (crtc->state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200782 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
783 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100784
Daniel Vetter623369e2014-09-16 17:50:47 +0200785 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100786 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200787 }
788
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300789 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200790 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200791 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200792 struct drm_encoder *encoder;
793 struct drm_display_mode *mode, *adjusted_mode;
794
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300795 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200796 continue;
797
798 encoder = connector->state->best_encoder;
799 funcs = encoder->helper_private;
800 new_crtc_state = connector->state->crtc->state;
801 mode = &new_crtc_state->mode;
802 adjusted_mode = &new_crtc_state->adjusted_mode;
803
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100804 if (!new_crtc_state->mode_changed)
805 continue;
806
Daniel Vetter17a38d92015-02-22 12:24:16 +0100807 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
808 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100809
Daniel Vetter623369e2014-09-16 17:50:47 +0200810 /*
811 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800812 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200813 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100814 if (funcs->mode_set)
815 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200816
Archit Taneja862e6862015-05-21 11:03:16 +0530817 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200818 }
819}
820
821/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100822 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200823 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100824 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200825 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100826 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200827 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100828 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300829 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100830 * drm_atomic_helper_commit_planes(), which is what the default commit function
831 * does. But drivers with different needs can group the modeset commits together
832 * and do the plane commits at the end. This is useful for drivers doing runtime
833 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200834 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100835void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
836 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200837{
Laurent Pincharta072f802015-02-22 12:24:18 +0100838 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200839
840 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
841
Laurent Pincharta072f802015-02-22 12:24:18 +0100842 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200843}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100844EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200845
846/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100847 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200848 * @dev: DRM device
849 * @old_state: atomic state object with old state structures
850 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100851 * This function enables all the outputs with the new configuration which had to
852 * be turned off for the update.
853 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300854 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100855 * drm_atomic_helper_commit_planes(), which is what the default commit function
856 * does. But drivers with different needs can group the modeset commits together
857 * and do the plane commits at the end. This is useful for drivers doing runtime
858 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200859 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100860void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
861 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200862{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300863 struct drm_crtc *crtc;
864 struct drm_crtc_state *old_crtc_state;
865 struct drm_connector *connector;
866 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200867 int i;
868
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300869 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200870 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200871
872 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200873 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100874 continue;
875
876 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200877 continue;
878
879 funcs = crtc->helper_private;
880
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100881 if (crtc->state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200882 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
883 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100884
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100885 if (funcs->enable)
886 funcs->enable(crtc);
887 else
888 funcs->commit(crtc);
889 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200890 }
891
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300892 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200893 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200894 struct drm_encoder *encoder;
895
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300896 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200897 continue;
898
Daniel Vetter4218a322015-03-26 22:18:40 +0100899 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200900 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100901 continue;
902
Daniel Vetter623369e2014-09-16 17:50:47 +0200903 encoder = connector->state->best_encoder;
904 funcs = encoder->helper_private;
905
Daniel Vetter17a38d92015-02-22 12:24:16 +0100906 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
907 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100908
Daniel Vetter623369e2014-09-16 17:50:47 +0200909 /*
910 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800911 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200912 */
Archit Taneja862e6862015-05-21 11:03:16 +0530913 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200914
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100915 if (funcs->enable)
916 funcs->enable(encoder);
917 else
918 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200919
Archit Taneja862e6862015-05-21 11:03:16 +0530920 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200921 }
922}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100923EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200924
Daniel Vettere2330f02014-10-29 11:34:56 +0100925static void wait_for_fences(struct drm_device *dev,
926 struct drm_atomic_state *state)
927{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300928 struct drm_plane *plane;
929 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +0100930 int i;
931
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300932 for_each_plane_in_state(state, plane, plane_state, i) {
933 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +0100934 continue;
935
936 WARN_ON(!plane->state->fb);
937
938 fence_wait(plane->state->fence, false);
939 fence_put(plane->state->fence);
940 plane->state->fence = NULL;
941 }
942}
943
John Keepingc2409062016-01-19 10:46:58 +0000944/**
945 * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
946 * @dev: DRM device
947 * @old_state: atomic state object with old state structures
948 * @crtc: DRM crtc
949 *
950 * Checks whether the framebuffer used for this CRTC changes as a result of
951 * the atomic update. This is useful for drivers which cannot use
952 * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
953 * functionality.
954 *
955 * Returns:
956 * true if the framebuffer changed.
957 */
958bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
959 struct drm_atomic_state *old_state,
960 struct drm_crtc *crtc)
Daniel Vetterab58e332014-11-24 20:42:42 +0100961{
962 struct drm_plane *plane;
963 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +0100964 int i;
965
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300966 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +0100967 if (plane->state->crtc != crtc &&
968 old_plane_state->crtc != crtc)
969 continue;
970
971 if (plane->state->fb != old_plane_state->fb)
972 return true;
973 }
974
975 return false;
976}
John Keepingc2409062016-01-19 10:46:58 +0000977EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
Daniel Vetterab58e332014-11-24 20:42:42 +0100978
Rob Clark5ee32292014-11-11 19:38:59 -0500979/**
980 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
981 * @dev: DRM device
982 * @old_state: atomic state object with old state structures
983 *
984 * Helper to, after atomic commit, wait for vblanks on all effected
985 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100986 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
987 * framebuffers have actually changed to optimize for the legacy cursor and
988 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500989 */
990void
991drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
992 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200993{
994 struct drm_crtc *crtc;
995 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200996 int i, ret;
997
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300998 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200999 /* No one cares about the old state, so abuse it for tracking
1000 * and store whether we hold a vblank reference (and should do a
1001 * vblank wait) in the ->enable boolean. */
1002 old_crtc_state->enable = false;
1003
1004 if (!crtc->state->enable)
1005 continue;
1006
Daniel Vetterf02ad902015-01-22 16:36:23 +01001007 /* Legacy cursor ioctls are completely unsynced, and userspace
1008 * relies on that (by doing tons of cursor updates). */
1009 if (old_state->legacy_cursor_update)
1010 continue;
1011
John Keepingc2409062016-01-19 10:46:58 +00001012 if (!drm_atomic_helper_framebuffer_changed(dev,
1013 old_state, crtc))
Daniel Vetterab58e332014-11-24 20:42:42 +01001014 continue;
1015
Daniel Vetter623369e2014-09-16 17:50:47 +02001016 ret = drm_crtc_vblank_get(crtc);
1017 if (ret != 0)
1018 continue;
1019
1020 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +02001021 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001022 }
1023
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001024 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1025 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +02001026 continue;
1027
1028 ret = wait_event_timeout(dev->vblank[i].queue,
1029 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001030 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001031 msecs_to_jiffies(50));
1032
1033 drm_crtc_vblank_put(crtc);
1034 }
1035}
Rob Clark5ee32292014-11-11 19:38:59 -05001036EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001037
1038/**
1039 * drm_atomic_helper_commit - commit validated state object
1040 * @dev: DRM device
1041 * @state: the driver state object
1042 * @async: asynchronous commit
1043 *
1044 * This function commits a with drm_atomic_helper_check() pre-validated state
1045 * object. This can still fail when e.g. the framebuffer reservation fails. For
1046 * now this doesn't implement asynchronous commits.
1047 *
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001048 * Note that right now this function does not support async commits, and hence
1049 * driver writers must implement their own version for now. Also note that the
1050 * default ordering of how the various stages are called is to match the legacy
1051 * modeset helper library closest. One peculiarity of that is that it doesn't
1052 * mesh well with runtime PM at all.
1053 *
1054 * For drivers supporting runtime PM the recommended sequence is
1055 *
1056 * drm_atomic_helper_commit_modeset_disables(dev, state);
1057 *
1058 * drm_atomic_helper_commit_modeset_enables(dev, state);
1059 *
1060 * drm_atomic_helper_commit_planes(dev, state, true);
1061 *
1062 * See the kerneldoc entries for these three functions for more details.
1063 *
Daniel Vetter623369e2014-09-16 17:50:47 +02001064 * RETURNS
1065 * Zero for success or -errno.
1066 */
1067int drm_atomic_helper_commit(struct drm_device *dev,
1068 struct drm_atomic_state *state,
1069 bool async)
1070{
1071 int ret;
1072
1073 if (async)
1074 return -EBUSY;
1075
1076 ret = drm_atomic_helper_prepare_planes(dev, state);
1077 if (ret)
1078 return ret;
1079
1080 /*
1081 * This is the point of no return - everything below never fails except
1082 * when the hw goes bonghits. Which means we can commit the new state on
1083 * the software side now.
1084 */
1085
1086 drm_atomic_helper_swap_state(dev, state);
1087
1088 /*
1089 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001090 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001091 * that the asynchronous work has either been cancelled (if the driver
1092 * supports it, which at least requires that the framebuffers get
1093 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1094 * before the new state gets committed on the software side with
1095 * drm_atomic_helper_swap_state().
1096 *
1097 * This scheme allows new atomic state updates to be prepared and
1098 * checked in parallel to the asynchronous completion of the previous
1099 * update. Which is important since compositors need to figure out the
1100 * composition of the next frame right after having submitted the
1101 * current layout.
1102 */
1103
Daniel Vettere2330f02014-10-29 11:34:56 +01001104 wait_for_fences(dev, state);
1105
Daniel Vetter1af434a2015-02-22 12:24:19 +01001106 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001107
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001108 drm_atomic_helper_commit_planes(dev, state, false);
Daniel Vetter623369e2014-09-16 17:50:47 +02001109
Daniel Vetter1af434a2015-02-22 12:24:19 +01001110 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001111
Rob Clark5ee32292014-11-11 19:38:59 -05001112 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001113
1114 drm_atomic_helper_cleanup_planes(dev, state);
1115
1116 drm_atomic_state_free(state);
1117
1118 return 0;
1119}
1120EXPORT_SYMBOL(drm_atomic_helper_commit);
1121
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001122/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001123 * DOC: implementing async commit
1124 *
1125 * For now the atomic helpers don't support async commit directly. If there is
1126 * real need it could be added though, using the dma-buf fence infrastructure
1127 * for generic synchronization with outstanding rendering.
1128 *
1129 * For now drivers have to implement async commit themselves, with the following
1130 * sequence being the recommended one:
1131 *
1132 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1133 * which commit needs to call which can fail, so we want to run it first and
1134 * synchronously.
1135 *
1136 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1137 * might be affected the new state update. This can be done by either cancelling
1138 * or flushing the work items, depending upon whether the driver can deal with
1139 * cancelled updates. Note that it is important to ensure that the framebuffer
1140 * cleanup is still done when cancelling.
1141 *
1142 * For sufficient parallelism it is recommended to have a work item per crtc
1143 * (for updates which don't touch global state) and a global one. Then we only
1144 * need to synchronize with the crtc work items for changed crtcs and the global
1145 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1146 *
1147 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001148 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001149 * locks means concurrent callers never see inconsistent state. And doing this
1150 * while it's guaranteed that no relevant async worker runs means that async
1151 * workers do not need grab any locks. Actually they must not grab locks, for
1152 * otherwise the work flushing will deadlock.
1153 *
1154 * 4. Schedule a work item to do all subsequent steps, using the split-out
1155 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1156 * then cleaning up the framebuffers after the old framebuffer is no longer
1157 * being displayed.
1158 */
1159
1160/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001161 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001162 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001163 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001164 *
1165 * This function prepares plane state, specifically framebuffers, for the new
1166 * configuration. If any failure is encountered this function will call
1167 * ->cleanup_fb on any already successfully prepared framebuffer.
1168 *
1169 * Returns:
1170 * 0 on success, negative error code on failure.
1171 */
1172int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1173 struct drm_atomic_state *state)
1174{
1175 int nplanes = dev->mode_config.num_total_plane;
1176 int ret, i;
1177
1178 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001179 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001180 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001181 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001182
1183 if (!plane)
1184 continue;
1185
1186 funcs = plane->helper_private;
1187
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001188 if (funcs->prepare_fb) {
1189 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001190 if (ret)
1191 goto fail;
1192 }
1193 }
1194
1195 return 0;
1196
1197fail:
1198 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001199 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001200 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001201 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001202
1203 if (!plane)
1204 continue;
1205
1206 funcs = plane->helper_private;
1207
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001208 if (funcs->cleanup_fb)
1209 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001210
1211 }
1212
1213 return ret;
1214}
1215EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1216
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001217bool plane_crtc_active(struct drm_plane_state *state)
1218{
1219 return state->crtc && state->crtc->state->active;
1220}
1221
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001222/**
1223 * drm_atomic_helper_commit_planes - commit plane state
1224 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001225 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001226 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001227 *
1228 * This function commits the new plane state using the plane and atomic helper
1229 * functions for planes and crtcs. It assumes that the atomic state has already
1230 * been pushed into the relevant object state pointers, since this step can no
1231 * longer fail.
1232 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001233 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001234 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001235 *
1236 * Note that this function does all plane updates across all CRTCs in one step.
1237 * If the hardware can't support this approach look at
1238 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001239 *
1240 * Plane parameters can be updated by applications while the associated CRTC is
1241 * disabled. The DRM/KMS core will store the parameters in the plane state,
1242 * which will be available to the driver when the CRTC is turned on. As a result
1243 * most drivers don't need to be immediately notified of plane updates for a
1244 * disabled CRTC.
1245 *
1246 * Unless otherwise needed, drivers are advised to set the @active_only
1247 * parameters to true in order not to receive plane update notifications related
1248 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1249 * driver code when the driver and/or hardware can't or just don't need to deal
1250 * with updates on disabled CRTCs, for example when supporting runtime PM.
1251 *
1252 * The drm_atomic_helper_commit() default implementation only sets @active_only
1253 * to false to most closely match the behaviour of the legacy helpers. This should
1254 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001255 */
1256void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001257 struct drm_atomic_state *old_state,
1258 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001259{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001260 struct drm_crtc *crtc;
1261 struct drm_crtc_state *old_crtc_state;
1262 struct drm_plane *plane;
1263 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001264 int i;
1265
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001266 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001267 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001268
1269 funcs = crtc->helper_private;
1270
1271 if (!funcs || !funcs->atomic_begin)
1272 continue;
1273
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001274 if (active_only && !crtc->state->active)
1275 continue;
1276
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001277 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001278 }
1279
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001280 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001281 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001282 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001283
1284 funcs = plane->helper_private;
1285
Thierry Reding3cad4b62014-11-25 13:05:12 +01001286 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001287 continue;
1288
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001289 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1290
1291 if (active_only) {
1292 /*
1293 * Skip planes related to inactive CRTCs. If the plane
1294 * is enabled use the state of the current CRTC. If the
1295 * plane is being disabled use the state of the old
1296 * CRTC to avoid skipping planes being disabled on an
1297 * active CRTC.
1298 */
1299 if (!disabling && !plane_crtc_active(plane->state))
1300 continue;
1301 if (disabling && !plane_crtc_active(old_plane_state))
1302 continue;
1303 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001304
Thierry Reding407b8bd2014-11-20 12:05:50 +01001305 /*
1306 * Special-case disabling the plane if drivers support it.
1307 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001308 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001309 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001310 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001311 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001312 }
1313
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001314 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001315 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001316
1317 funcs = crtc->helper_private;
1318
1319 if (!funcs || !funcs->atomic_flush)
1320 continue;
1321
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001322 if (active_only && !crtc->state->active)
1323 continue;
1324
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001325 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001326 }
1327}
1328EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1329
1330/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001331 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1332 * @old_crtc_state: atomic state object with the old crtc state
1333 *
1334 * This function commits the new plane state using the plane and atomic helper
1335 * functions for planes on the specific crtc. It assumes that the atomic state
1336 * has already been pushed into the relevant object state pointers, since this
1337 * step can no longer fail.
1338 *
1339 * This function is useful when plane updates should be done crtc-by-crtc
1340 * instead of one global step like drm_atomic_helper_commit_planes() does.
1341 *
1342 * This function can only be savely used when planes are not allowed to move
1343 * between different CRTCs because this function doesn't handle inter-CRTC
1344 * depencies. Callers need to ensure that either no such depencies exist,
1345 * resolve them through ordering of commit calls or through some other means.
1346 */
1347void
1348drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1349{
1350 const struct drm_crtc_helper_funcs *crtc_funcs;
1351 struct drm_crtc *crtc = old_crtc_state->crtc;
1352 struct drm_atomic_state *old_state = old_crtc_state->state;
1353 struct drm_plane *plane;
1354 unsigned plane_mask;
1355
1356 plane_mask = old_crtc_state->plane_mask;
1357 plane_mask |= crtc->state->plane_mask;
1358
1359 crtc_funcs = crtc->helper_private;
1360 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001361 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001362
1363 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1364 struct drm_plane_state *old_plane_state =
1365 drm_atomic_get_existing_plane_state(old_state, plane);
1366 const struct drm_plane_helper_funcs *plane_funcs;
1367
1368 plane_funcs = plane->helper_private;
1369
1370 if (!old_plane_state || !plane_funcs)
1371 continue;
1372
1373 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1374
1375 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1376 plane_funcs->atomic_disable)
1377 plane_funcs->atomic_disable(plane, old_plane_state);
1378 else if (plane->state->crtc ||
1379 drm_atomic_plane_disabling(plane, old_plane_state))
1380 plane_funcs->atomic_update(plane, old_plane_state);
1381 }
1382
1383 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001384 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001385}
1386EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1387
1388/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001389 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1390 * @crtc: CRTC
1391 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1392 *
1393 * Disables all planes associated with the given CRTC. This can be
1394 * used for instance in the CRTC helper disable callback to disable
1395 * all planes before shutting down the display pipeline.
1396 *
1397 * If the atomic-parameter is set the function calls the CRTC's
1398 * atomic_begin hook before and atomic_flush hook after disabling the
1399 * planes.
1400 *
1401 * It is a bug to call this function without having implemented the
1402 * ->atomic_disable() plane hook.
1403 */
1404void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1405 bool atomic)
1406{
1407 const struct drm_crtc_helper_funcs *crtc_funcs =
1408 crtc->helper_private;
1409 struct drm_plane *plane;
1410
1411 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1412 crtc_funcs->atomic_begin(crtc, NULL);
1413
1414 drm_for_each_plane(plane, crtc->dev) {
1415 const struct drm_plane_helper_funcs *plane_funcs =
1416 plane->helper_private;
1417
1418 if (plane->state->crtc != crtc || !plane_funcs)
1419 continue;
1420
1421 WARN_ON(!plane_funcs->atomic_disable);
1422 if (plane_funcs->atomic_disable)
1423 plane_funcs->atomic_disable(plane, NULL);
1424 }
1425
1426 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1427 crtc_funcs->atomic_flush(crtc, NULL);
1428}
1429EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1430
1431/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001432 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1433 * @dev: DRM device
1434 * @old_state: atomic state object with old state structures
1435 *
1436 * This function cleans up plane state, specifically framebuffers, from the old
1437 * configuration. Hence the old configuration must be perserved in @old_state to
1438 * be able to call this function.
1439 *
1440 * This function must also be called on the new state when the atomic update
1441 * fails at any point after calling drm_atomic_helper_prepare_planes().
1442 */
1443void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1444 struct drm_atomic_state *old_state)
1445{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001446 struct drm_plane *plane;
1447 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001448 int i;
1449
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001450 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001451 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001452
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001453 funcs = plane->helper_private;
1454
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001455 if (funcs->cleanup_fb)
1456 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001457 }
1458}
1459EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1460
1461/**
1462 * drm_atomic_helper_swap_state - store atomic state into current sw state
1463 * @dev: DRM device
1464 * @state: atomic state
1465 *
1466 * This function stores the atomic state into the current state pointers in all
1467 * driver objects. It should be called after all failing steps have been done
1468 * and succeeded, but before the actual hardware state is committed.
1469 *
1470 * For cleanup and error recovery the current state for all changed objects will
1471 * be swaped into @state.
1472 *
1473 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1474 *
1475 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1476 *
1477 * 2. Do any other steps that might fail.
1478 *
1479 * 3. Put the staged state into the current state pointers with this function.
1480 *
1481 * 4. Actually commit the hardware state.
1482 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001483 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001484 * contains the old state. Also do any other cleanup required with that state.
1485 */
1486void drm_atomic_helper_swap_state(struct drm_device *dev,
1487 struct drm_atomic_state *state)
1488{
1489 int i;
1490
1491 for (i = 0; i < dev->mode_config.num_connector; i++) {
1492 struct drm_connector *connector = state->connectors[i];
1493
1494 if (!connector)
1495 continue;
1496
1497 connector->state->state = state;
1498 swap(state->connector_states[i], connector->state);
1499 connector->state->state = NULL;
1500 }
1501
1502 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1503 struct drm_crtc *crtc = state->crtcs[i];
1504
1505 if (!crtc)
1506 continue;
1507
1508 crtc->state->state = state;
1509 swap(state->crtc_states[i], crtc->state);
1510 crtc->state->state = NULL;
1511 }
1512
1513 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1514 struct drm_plane *plane = state->planes[i];
1515
1516 if (!plane)
1517 continue;
1518
1519 plane->state->state = state;
1520 swap(state->plane_states[i], plane->state);
1521 plane->state->state = NULL;
1522 }
1523}
1524EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001525
1526/**
1527 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1528 * @plane: plane object to update
1529 * @crtc: owning CRTC of owning plane
1530 * @fb: framebuffer to flip onto plane
1531 * @crtc_x: x offset of primary plane on crtc
1532 * @crtc_y: y offset of primary plane on crtc
1533 * @crtc_w: width of primary plane rectangle on crtc
1534 * @crtc_h: height of primary plane rectangle on crtc
1535 * @src_x: x offset of @fb for panning
1536 * @src_y: y offset of @fb for panning
1537 * @src_w: width of source rectangle in @fb
1538 * @src_h: height of source rectangle in @fb
1539 *
1540 * Provides a default plane update handler using the atomic driver interface.
1541 *
1542 * RETURNS:
1543 * Zero on success, error code on failure
1544 */
1545int drm_atomic_helper_update_plane(struct drm_plane *plane,
1546 struct drm_crtc *crtc,
1547 struct drm_framebuffer *fb,
1548 int crtc_x, int crtc_y,
1549 unsigned int crtc_w, unsigned int crtc_h,
1550 uint32_t src_x, uint32_t src_y,
1551 uint32_t src_w, uint32_t src_h)
1552{
1553 struct drm_atomic_state *state;
1554 struct drm_plane_state *plane_state;
1555 int ret = 0;
1556
1557 state = drm_atomic_state_alloc(plane->dev);
1558 if (!state)
1559 return -ENOMEM;
1560
1561 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1562retry:
1563 plane_state = drm_atomic_get_plane_state(state, plane);
1564 if (IS_ERR(plane_state)) {
1565 ret = PTR_ERR(plane_state);
1566 goto fail;
1567 }
1568
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001569 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001570 if (ret != 0)
1571 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001572 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001573 plane_state->crtc_x = crtc_x;
1574 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001575 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001576 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001577 plane_state->src_x = src_x;
1578 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001579 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001580 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001581
Daniel Vetter3671c582015-05-04 15:40:52 +02001582 if (plane == crtc->cursor)
1583 state->legacy_cursor_update = true;
1584
Daniel Vetter042652e2014-07-27 13:46:52 +02001585 ret = drm_atomic_commit(state);
1586 if (ret != 0)
1587 goto fail;
1588
1589 /* Driver takes ownership of state on successful commit. */
1590 return 0;
1591fail:
1592 if (ret == -EDEADLK)
1593 goto backoff;
1594
1595 drm_atomic_state_free(state);
1596
1597 return ret;
1598backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001599 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001600 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001601
1602 /*
1603 * Someone might have exchanged the framebuffer while we dropped locks
1604 * in the backoff code. We need to fix up the fb refcount tracking the
1605 * core does for us.
1606 */
1607 plane->old_fb = plane->fb;
1608
1609 goto retry;
1610}
1611EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1612
1613/**
1614 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1615 * @plane: plane to disable
1616 *
1617 * Provides a default plane disable handler using the atomic driver interface.
1618 *
1619 * RETURNS:
1620 * Zero on success, error code on failure
1621 */
1622int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1623{
1624 struct drm_atomic_state *state;
1625 struct drm_plane_state *plane_state;
1626 int ret = 0;
1627
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001628 /*
1629 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1630 * acquire context. The real fix will be to wire the acquire ctx through
1631 * everywhere we need it, but meanwhile prevent chaos by just skipping
1632 * this noop. The critical case is the cursor ioctls which a) only grab
1633 * crtc/cursor-plane locks (so we need the crtc to get at the right
1634 * acquire context) and b) can try to disable the plane multiple times.
1635 */
1636 if (!plane->crtc)
1637 return 0;
1638
Daniel Vetter042652e2014-07-27 13:46:52 +02001639 state = drm_atomic_state_alloc(plane->dev);
1640 if (!state)
1641 return -ENOMEM;
1642
1643 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1644retry:
1645 plane_state = drm_atomic_get_plane_state(state, plane);
1646 if (IS_ERR(plane_state)) {
1647 ret = PTR_ERR(plane_state);
1648 goto fail;
1649 }
1650
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01001651 if (plane_state->crtc && (plane == plane->crtc->cursor))
1652 plane_state->state->legacy_cursor_update = true;
1653
Rob Clarkbbb1e522015-08-25 15:35:58 -04001654 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001655 if (ret != 0)
1656 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01001657
Daniel Vetter042652e2014-07-27 13:46:52 +02001658 ret = drm_atomic_commit(state);
1659 if (ret != 0)
1660 goto fail;
1661
1662 /* Driver takes ownership of state on successful commit. */
1663 return 0;
1664fail:
1665 if (ret == -EDEADLK)
1666 goto backoff;
1667
1668 drm_atomic_state_free(state);
1669
1670 return ret;
1671backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001672 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001673 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001674
1675 /*
1676 * Someone might have exchanged the framebuffer while we dropped locks
1677 * in the backoff code. We need to fix up the fb refcount tracking the
1678 * core does for us.
1679 */
1680 plane->old_fb = plane->fb;
1681
1682 goto retry;
1683}
1684EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1685
Rob Clarkbbb1e522015-08-25 15:35:58 -04001686/* just used from fb-helper and atomic-helper: */
1687int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1688 struct drm_plane_state *plane_state)
1689{
1690 int ret;
1691
1692 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1693 if (ret != 0)
1694 return ret;
1695
1696 drm_atomic_set_fb_for_plane(plane_state, NULL);
1697 plane_state->crtc_x = 0;
1698 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001699 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001700 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001701 plane_state->src_x = 0;
1702 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001703 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001704 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001705
Rob Clarkbbb1e522015-08-25 15:35:58 -04001706 return 0;
1707}
1708
Daniel Vetter042652e2014-07-27 13:46:52 +02001709static int update_output_state(struct drm_atomic_state *state,
1710 struct drm_mode_set *set)
1711{
1712 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001713 struct drm_crtc *crtc;
1714 struct drm_crtc_state *crtc_state;
1715 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001716 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001717 int ret, i, j;
1718
1719 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1720 state->acquire_ctx);
1721 if (ret)
1722 return ret;
1723
1724 /* First grab all affected connector/crtc states. */
1725 for (i = 0; i < set->num_connectors; i++) {
1726 conn_state = drm_atomic_get_connector_state(state,
1727 set->connectors[i]);
1728 if (IS_ERR(conn_state))
1729 return PTR_ERR(conn_state);
1730 }
1731
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001732 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001733 ret = drm_atomic_add_affected_connectors(state, crtc);
1734 if (ret)
1735 return ret;
1736 }
1737
1738 /* Then recompute connector->crtc links and crtc enabling state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001739 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001740 if (conn_state->crtc == set->crtc) {
1741 ret = drm_atomic_set_crtc_for_connector(conn_state,
1742 NULL);
1743 if (ret)
1744 return ret;
1745 }
1746
1747 for (j = 0; j < set->num_connectors; j++) {
1748 if (set->connectors[j] == connector) {
1749 ret = drm_atomic_set_crtc_for_connector(conn_state,
1750 set->crtc);
1751 if (ret)
1752 return ret;
1753 break;
1754 }
1755 }
1756 }
1757
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001758 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001759 /* Don't update ->enable for the CRTC in the set_config request,
1760 * since a mismatch would indicate a bug in the upper layers.
1761 * The actual modeset code later on will catch any
1762 * inconsistencies here. */
1763 if (crtc == set->crtc)
1764 continue;
1765
Maarten Lankhorst14de6c42016-01-04 12:53:20 +01001766 if (!crtc_state->connector_mask) {
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001767 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1768 NULL);
1769 if (ret < 0)
1770 return ret;
1771
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001772 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001773 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001774 }
1775
1776 return 0;
1777}
1778
1779/**
1780 * drm_atomic_helper_set_config - set a new config from userspace
1781 * @set: mode set configuration
1782 *
1783 * Provides a default crtc set_config handler using the atomic driver interface.
1784 *
1785 * Returns:
1786 * Returns 0 on success, negative errno numbers on failure.
1787 */
1788int drm_atomic_helper_set_config(struct drm_mode_set *set)
1789{
1790 struct drm_atomic_state *state;
1791 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02001792 int ret = 0;
1793
1794 state = drm_atomic_state_alloc(crtc->dev);
1795 if (!state)
1796 return -ENOMEM;
1797
1798 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1799retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04001800 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01001801 if (ret != 0)
1802 goto fail;
1803
Daniel Vetter042652e2014-07-27 13:46:52 +02001804 ret = drm_atomic_commit(state);
1805 if (ret != 0)
1806 goto fail;
1807
1808 /* Driver takes ownership of state on successful commit. */
1809 return 0;
1810fail:
1811 if (ret == -EDEADLK)
1812 goto backoff;
1813
1814 drm_atomic_state_free(state);
1815
1816 return ret;
1817backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001818 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001819 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001820
1821 /*
1822 * Someone might have exchanged the framebuffer while we dropped locks
1823 * in the backoff code. We need to fix up the fb refcount tracking the
1824 * core does for us.
1825 */
1826 crtc->primary->old_fb = crtc->primary->fb;
1827
1828 goto retry;
1829}
1830EXPORT_SYMBOL(drm_atomic_helper_set_config);
1831
Rob Clarkbbb1e522015-08-25 15:35:58 -04001832/* just used from fb-helper and atomic-helper: */
1833int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1834 struct drm_atomic_state *state)
1835{
1836 struct drm_crtc_state *crtc_state;
1837 struct drm_plane_state *primary_state;
1838 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02001839 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001840 int ret;
1841
1842 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1843 if (IS_ERR(crtc_state))
1844 return PTR_ERR(crtc_state);
1845
1846 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1847 if (IS_ERR(primary_state))
1848 return PTR_ERR(primary_state);
1849
1850 if (!set->mode) {
1851 WARN_ON(set->fb);
1852 WARN_ON(set->num_connectors);
1853
1854 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1855 if (ret != 0)
1856 return ret;
1857
1858 crtc_state->active = false;
1859
1860 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1861 if (ret != 0)
1862 return ret;
1863
1864 drm_atomic_set_fb_for_plane(primary_state, NULL);
1865
1866 goto commit;
1867 }
1868
1869 WARN_ON(!set->fb);
1870 WARN_ON(!set->num_connectors);
1871
1872 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1873 if (ret != 0)
1874 return ret;
1875
1876 crtc_state->active = true;
1877
1878 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1879 if (ret != 0)
1880 return ret;
1881
Ville Syrjälä83926112015-11-16 17:02:34 +02001882 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1883
Rob Clarkbbb1e522015-08-25 15:35:58 -04001884 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1885 primary_state->crtc_x = 0;
1886 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02001887 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001888 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001889 primary_state->src_x = set->x << 16;
1890 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001891 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02001892 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001893 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001894 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02001895 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001896 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001897 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04001898
1899commit:
1900 ret = update_output_state(state, set);
1901 if (ret)
1902 return ret;
1903
1904 return 0;
1905}
1906
Daniel Vetter042652e2014-07-27 13:46:52 +02001907/**
Thierry Reding14942762015-12-02 17:50:04 +01001908 * drm_atomic_helper_disable_all - disable all currently active outputs
1909 * @dev: DRM device
1910 * @ctx: lock acquisition context
1911 *
1912 * Loops through all connectors, finding those that aren't turned off and then
1913 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1914 * that they are connected to.
1915 *
1916 * This is used for example in suspend/resume to disable all currently active
1917 * functions when suspending.
1918 *
1919 * Note that if callers haven't already acquired all modeset locks this might
1920 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1921 *
1922 * Returns:
1923 * 0 on success or a negative error code on failure.
1924 *
1925 * See also:
1926 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1927 */
1928int drm_atomic_helper_disable_all(struct drm_device *dev,
1929 struct drm_modeset_acquire_ctx *ctx)
1930{
1931 struct drm_atomic_state *state;
1932 struct drm_connector *conn;
1933 int err;
1934
1935 state = drm_atomic_state_alloc(dev);
1936 if (!state)
1937 return -ENOMEM;
1938
1939 state->acquire_ctx = ctx;
1940
1941 drm_for_each_connector(conn, dev) {
1942 struct drm_crtc *crtc = conn->state->crtc;
1943 struct drm_crtc_state *crtc_state;
1944
1945 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1946 continue;
1947
1948 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1949 if (IS_ERR(crtc_state)) {
1950 err = PTR_ERR(crtc_state);
1951 goto free;
1952 }
1953
1954 crtc_state->active = false;
1955 }
1956
1957 err = drm_atomic_commit(state);
1958
1959free:
1960 if (err < 0)
1961 drm_atomic_state_free(state);
1962
1963 return err;
1964}
1965EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1966
1967/**
1968 * drm_atomic_helper_suspend - subsystem-level suspend helper
1969 * @dev: DRM device
1970 *
1971 * Duplicates the current atomic state, disables all active outputs and then
1972 * returns a pointer to the original atomic state to the caller. Drivers can
1973 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1974 * restore the output configuration that was active at the time the system
1975 * entered suspend.
1976 *
1977 * Note that it is potentially unsafe to use this. The atomic state object
1978 * returned by this function is assumed to be persistent. Drivers must ensure
1979 * that this holds true. Before calling this function, drivers must make sure
1980 * to suspend fbdev emulation so that nothing can be using the device.
1981 *
1982 * Returns:
1983 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1984 * encoded error code on failure. Drivers should store the returned atomic
1985 * state object and pass it to the drm_atomic_helper_resume() helper upon
1986 * resume.
1987 *
1988 * See also:
1989 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1990 * drm_atomic_helper_resume()
1991 */
1992struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1993{
1994 struct drm_modeset_acquire_ctx ctx;
1995 struct drm_atomic_state *state;
1996 int err;
1997
1998 drm_modeset_acquire_init(&ctx, 0);
1999
2000retry:
2001 err = drm_modeset_lock_all_ctx(dev, &ctx);
2002 if (err < 0) {
2003 state = ERR_PTR(err);
2004 goto unlock;
2005 }
2006
2007 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2008 if (IS_ERR(state))
2009 goto unlock;
2010
2011 err = drm_atomic_helper_disable_all(dev, &ctx);
2012 if (err < 0) {
2013 drm_atomic_state_free(state);
2014 state = ERR_PTR(err);
2015 goto unlock;
2016 }
2017
2018unlock:
2019 if (PTR_ERR(state) == -EDEADLK) {
2020 drm_modeset_backoff(&ctx);
2021 goto retry;
2022 }
2023
2024 drm_modeset_drop_locks(&ctx);
2025 drm_modeset_acquire_fini(&ctx);
2026 return state;
2027}
2028EXPORT_SYMBOL(drm_atomic_helper_suspend);
2029
2030/**
2031 * drm_atomic_helper_resume - subsystem-level resume helper
2032 * @dev: DRM device
2033 * @state: atomic state to resume to
2034 *
2035 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2036 * grabs all modeset locks and commits the atomic state object. This can be
2037 * used in conjunction with the drm_atomic_helper_suspend() helper to
2038 * implement suspend/resume for drivers that support atomic mode-setting.
2039 *
2040 * Returns:
2041 * 0 on success or a negative error code on failure.
2042 *
2043 * See also:
2044 * drm_atomic_helper_suspend()
2045 */
2046int drm_atomic_helper_resume(struct drm_device *dev,
2047 struct drm_atomic_state *state)
2048{
2049 struct drm_mode_config *config = &dev->mode_config;
2050 int err;
2051
2052 drm_mode_config_reset(dev);
2053 drm_modeset_lock_all(dev);
2054 state->acquire_ctx = config->acquire_ctx;
2055 err = drm_atomic_commit(state);
2056 drm_modeset_unlock_all(dev);
2057
2058 return err;
2059}
2060EXPORT_SYMBOL(drm_atomic_helper_resume);
2061
2062/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002063 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002064 * @crtc: DRM crtc
2065 * @property: DRM property
2066 * @val: value of property
2067 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002068 * Provides a default crtc set_property handler using the atomic driver
2069 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002070 *
2071 * RETURNS:
2072 * Zero on success, error code on failure
2073 */
2074int
2075drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2076 struct drm_property *property,
2077 uint64_t val)
2078{
2079 struct drm_atomic_state *state;
2080 struct drm_crtc_state *crtc_state;
2081 int ret = 0;
2082
2083 state = drm_atomic_state_alloc(crtc->dev);
2084 if (!state)
2085 return -ENOMEM;
2086
2087 /* ->set_property is always called with all locks held. */
2088 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2089retry:
2090 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2091 if (IS_ERR(crtc_state)) {
2092 ret = PTR_ERR(crtc_state);
2093 goto fail;
2094 }
2095
Rob Clark40ecc692014-12-18 16:01:46 -05002096 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2097 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002098 if (ret)
2099 goto fail;
2100
2101 ret = drm_atomic_commit(state);
2102 if (ret != 0)
2103 goto fail;
2104
2105 /* Driver takes ownership of state on successful commit. */
2106 return 0;
2107fail:
2108 if (ret == -EDEADLK)
2109 goto backoff;
2110
2111 drm_atomic_state_free(state);
2112
2113 return ret;
2114backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002115 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002116 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002117
2118 goto retry;
2119}
2120EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2121
2122/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002123 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002124 * @plane: DRM plane
2125 * @property: DRM property
2126 * @val: value of property
2127 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002128 * Provides a default plane set_property handler using the atomic driver
2129 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002130 *
2131 * RETURNS:
2132 * Zero on success, error code on failure
2133 */
2134int
2135drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2136 struct drm_property *property,
2137 uint64_t val)
2138{
2139 struct drm_atomic_state *state;
2140 struct drm_plane_state *plane_state;
2141 int ret = 0;
2142
2143 state = drm_atomic_state_alloc(plane->dev);
2144 if (!state)
2145 return -ENOMEM;
2146
2147 /* ->set_property is always called with all locks held. */
2148 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2149retry:
2150 plane_state = drm_atomic_get_plane_state(state, plane);
2151 if (IS_ERR(plane_state)) {
2152 ret = PTR_ERR(plane_state);
2153 goto fail;
2154 }
2155
Rob Clark40ecc692014-12-18 16:01:46 -05002156 ret = drm_atomic_plane_set_property(plane, plane_state,
2157 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002158 if (ret)
2159 goto fail;
2160
2161 ret = drm_atomic_commit(state);
2162 if (ret != 0)
2163 goto fail;
2164
2165 /* Driver takes ownership of state on successful commit. */
2166 return 0;
2167fail:
2168 if (ret == -EDEADLK)
2169 goto backoff;
2170
2171 drm_atomic_state_free(state);
2172
2173 return ret;
2174backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002175 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002176 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002177
2178 goto retry;
2179}
2180EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2181
2182/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002183 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002184 * @connector: DRM connector
2185 * @property: DRM property
2186 * @val: value of property
2187 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002188 * Provides a default connector set_property handler using the atomic driver
2189 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002190 *
2191 * RETURNS:
2192 * Zero on success, error code on failure
2193 */
2194int
2195drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2196 struct drm_property *property,
2197 uint64_t val)
2198{
2199 struct drm_atomic_state *state;
2200 struct drm_connector_state *connector_state;
2201 int ret = 0;
2202
2203 state = drm_atomic_state_alloc(connector->dev);
2204 if (!state)
2205 return -ENOMEM;
2206
2207 /* ->set_property is always called with all locks held. */
2208 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2209retry:
2210 connector_state = drm_atomic_get_connector_state(state, connector);
2211 if (IS_ERR(connector_state)) {
2212 ret = PTR_ERR(connector_state);
2213 goto fail;
2214 }
2215
Rob Clark40ecc692014-12-18 16:01:46 -05002216 ret = drm_atomic_connector_set_property(connector, connector_state,
2217 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002218 if (ret)
2219 goto fail;
2220
2221 ret = drm_atomic_commit(state);
2222 if (ret != 0)
2223 goto fail;
2224
2225 /* Driver takes ownership of state on successful commit. */
2226 return 0;
2227fail:
2228 if (ret == -EDEADLK)
2229 goto backoff;
2230
2231 drm_atomic_state_free(state);
2232
2233 return ret;
2234backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002235 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002236 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002237
2238 goto retry;
2239}
2240EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002241
2242/**
2243 * drm_atomic_helper_page_flip - execute a legacy page flip
2244 * @crtc: DRM crtc
2245 * @fb: DRM framebuffer
2246 * @event: optional DRM event to signal upon completion
2247 * @flags: flip flags for non-vblank sync'ed updates
2248 *
2249 * Provides a default page flip implementation using the atomic driver interface.
2250 *
2251 * Note that for now so called async page flips (i.e. updates which are not
2252 * synchronized to vblank) are not supported, since the atomic interfaces have
2253 * no provisions for this yet.
2254 *
2255 * Returns:
2256 * Returns 0 on success, negative errno numbers on failure.
2257 */
2258int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2259 struct drm_framebuffer *fb,
2260 struct drm_pending_vblank_event *event,
2261 uint32_t flags)
2262{
2263 struct drm_plane *plane = crtc->primary;
2264 struct drm_atomic_state *state;
2265 struct drm_plane_state *plane_state;
2266 struct drm_crtc_state *crtc_state;
2267 int ret = 0;
2268
2269 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2270 return -EINVAL;
2271
2272 state = drm_atomic_state_alloc(plane->dev);
2273 if (!state)
2274 return -ENOMEM;
2275
2276 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2277retry:
2278 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2279 if (IS_ERR(crtc_state)) {
2280 ret = PTR_ERR(crtc_state);
2281 goto fail;
2282 }
2283 crtc_state->event = event;
2284
2285 plane_state = drm_atomic_get_plane_state(state, plane);
2286 if (IS_ERR(plane_state)) {
2287 ret = PTR_ERR(plane_state);
2288 goto fail;
2289 }
2290
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002291 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002292 if (ret != 0)
2293 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002294 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002295
Daniel Vetter4cba6852015-12-08 09:49:20 +01002296 /* Make sure we don't accidentally do a full modeset. */
2297 state->allow_modeset = false;
2298 if (!crtc_state->active) {
2299 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2300 crtc->base.id);
2301 ret = -EINVAL;
2302 goto fail;
2303 }
2304
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002305 ret = drm_atomic_async_commit(state);
2306 if (ret != 0)
2307 goto fail;
2308
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002309 /* Driver takes ownership of state on successful async commit. */
2310 return 0;
2311fail:
2312 if (ret == -EDEADLK)
2313 goto backoff;
2314
2315 drm_atomic_state_free(state);
2316
2317 return ret;
2318backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002319 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002320 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002321
2322 /*
2323 * Someone might have exchanged the framebuffer while we dropped locks
2324 * in the backoff code. We need to fix up the fb refcount tracking the
2325 * core does for us.
2326 */
2327 plane->old_fb = plane->fb;
2328
2329 goto retry;
2330}
2331EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002332
2333/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002334 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2335 * @connector: affected connector
2336 * @mode: DPMS mode
2337 *
2338 * This is the main helper function provided by the atomic helper framework for
2339 * implementing the legacy DPMS connector interface. It computes the new desired
2340 * ->active state for the corresponding CRTC (if the connector is enabled) and
2341 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002342 *
2343 * Returns:
2344 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002345 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002346int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2347 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002348{
2349 struct drm_mode_config *config = &connector->dev->mode_config;
2350 struct drm_atomic_state *state;
2351 struct drm_crtc_state *crtc_state;
2352 struct drm_crtc *crtc;
2353 struct drm_connector *tmp_connector;
2354 int ret;
2355 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002356 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002357
2358 if (mode != DRM_MODE_DPMS_ON)
2359 mode = DRM_MODE_DPMS_OFF;
2360
2361 connector->dpms = mode;
2362 crtc = connector->state->crtc;
2363
2364 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002365 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002366
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002367 state = drm_atomic_state_alloc(connector->dev);
2368 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002369 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002370
2371 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2372retry:
2373 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002374 if (IS_ERR(crtc_state)) {
2375 ret = PTR_ERR(crtc_state);
2376 goto fail;
2377 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002378
2379 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2380
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002381 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002382 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002383 continue;
2384
John Hunter0388df02015-03-17 15:30:28 +08002385 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002386 active = true;
2387 break;
2388 }
2389 }
2390 crtc_state->active = active;
2391
2392 ret = drm_atomic_commit(state);
2393 if (ret != 0)
2394 goto fail;
2395
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002396 /* Driver takes ownership of state on successful commit. */
2397 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002398fail:
2399 if (ret == -EDEADLK)
2400 goto backoff;
2401
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002402 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002403 drm_atomic_state_free(state);
2404
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002405 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002406backoff:
2407 drm_atomic_state_clear(state);
2408 drm_atomic_legacy_backoff(state);
2409
2410 goto retry;
2411}
2412EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2413
2414/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002415 * DOC: atomic state reset and initialization
2416 *
2417 * Both the drm core and the atomic helpers assume that there is always the full
2418 * and correct atomic software state for all connectors, CRTCs and planes
2419 * available. Which is a bit a problem on driver load and also after system
2420 * suspend. One way to solve this is to have a hardware state read-out
2421 * infrastructure which reconstructs the full software state (e.g. the i915
2422 * driver).
2423 *
2424 * The simpler solution is to just reset the software state to everything off,
2425 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2426 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01002427 *
2428 * On the upside the precise state tracking of atomic simplifies system suspend
2429 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2430 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2431 * For other drivers the building blocks are split out, see the documentation
2432 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002433 */
2434
2435/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002436 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2437 * @crtc: drm CRTC
2438 *
2439 * Resets the atomic state for @crtc by freeing the state pointer (which might
2440 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2441 */
2442void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2443{
Markus Elfring5f911902015-11-06 12:03:46 +01002444 if (crtc->state)
Daniel Stone99cf4a22015-05-25 19:11:51 +01002445 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002446 kfree(crtc->state);
2447 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002448
2449 if (crtc->state)
2450 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002451}
2452EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2453
2454/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002455 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2456 * @crtc: CRTC object
2457 * @state: atomic CRTC state
2458 *
2459 * Copies atomic state from a CRTC's current state and resets inferred values.
2460 * This is useful for drivers that subclass the CRTC state.
2461 */
2462void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2463 struct drm_crtc_state *state)
2464{
2465 memcpy(state, crtc->state, sizeof(*state));
2466
Daniel Stone99cf4a22015-05-25 19:11:51 +01002467 if (state->mode_blob)
2468 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002469 state->mode_changed = false;
2470 state->active_changed = false;
2471 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002472 state->connectors_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002473 state->event = NULL;
2474}
2475EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2476
2477/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002478 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2479 * @crtc: drm CRTC
2480 *
2481 * Default CRTC state duplicate hook for drivers which don't have their own
2482 * subclassed CRTC state structure.
2483 */
2484struct drm_crtc_state *
2485drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2486{
2487 struct drm_crtc_state *state;
2488
2489 if (WARN_ON(!crtc->state))
2490 return NULL;
2491
Thierry Redingf5e78402015-01-28 14:54:32 +01002492 state = kmalloc(sizeof(*state), GFP_KERNEL);
2493 if (state)
2494 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002495
2496 return state;
2497}
2498EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2499
2500/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002501 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2502 * @crtc: CRTC object
2503 * @state: CRTC state object to release
2504 *
2505 * Releases all resources stored in the CRTC state without actually freeing
2506 * the memory of the CRTC state. This is useful for drivers that subclass the
2507 * CRTC state.
2508 */
2509void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2510 struct drm_crtc_state *state)
2511{
Markus Elfring5f911902015-11-06 12:03:46 +01002512 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002513}
2514EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2515
2516/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002517 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2518 * @crtc: drm CRTC
2519 * @state: CRTC state object to release
2520 *
2521 * Default CRTC state destroy hook for drivers which don't have their own
2522 * subclassed CRTC state structure.
2523 */
2524void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2525 struct drm_crtc_state *state)
2526{
Thierry Redingf5e78402015-01-28 14:54:32 +01002527 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002528 kfree(state);
2529}
2530EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2531
2532/**
2533 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2534 * @plane: drm plane
2535 *
2536 * Resets the atomic state for @plane by freeing the state pointer (which might
2537 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2538 */
2539void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2540{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002541 if (plane->state && plane->state->fb)
2542 drm_framebuffer_unreference(plane->state->fb);
2543
Daniel Vetterd4617012014-11-03 15:56:43 +01002544 kfree(plane->state);
2545 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002546
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01002547 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002548 plane->state->plane = plane;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01002549 plane->state->rotation = BIT(DRM_ROTATE_0);
2550 }
Daniel Vetterd4617012014-11-03 15:56:43 +01002551}
2552EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2553
2554/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002555 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2556 * @plane: plane object
2557 * @state: atomic plane state
2558 *
2559 * Copies atomic state from a plane's current state. This is useful for
2560 * drivers that subclass the plane state.
2561 */
2562void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2563 struct drm_plane_state *state)
2564{
2565 memcpy(state, plane->state, sizeof(*state));
2566
2567 if (state->fb)
2568 drm_framebuffer_reference(state->fb);
2569}
2570EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2571
2572/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002573 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2574 * @plane: drm plane
2575 *
2576 * Default plane state duplicate hook for drivers which don't have their own
2577 * subclassed plane state structure.
2578 */
2579struct drm_plane_state *
2580drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2581{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002582 struct drm_plane_state *state;
2583
Daniel Vetterd4617012014-11-03 15:56:43 +01002584 if (WARN_ON(!plane->state))
2585 return NULL;
2586
Thierry Redingf5e78402015-01-28 14:54:32 +01002587 state = kmalloc(sizeof(*state), GFP_KERNEL);
2588 if (state)
2589 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002590
2591 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002592}
2593EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2594
2595/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002596 * __drm_atomic_helper_plane_destroy_state - release plane state
2597 * @plane: plane object
2598 * @state: plane state object to release
2599 *
2600 * Releases all resources stored in the plane state without actually freeing
2601 * the memory of the plane state. This is useful for drivers that subclass the
2602 * plane state.
2603 */
2604void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2605 struct drm_plane_state *state)
2606{
2607 if (state->fb)
2608 drm_framebuffer_unreference(state->fb);
2609}
2610EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2611
2612/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002613 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2614 * @plane: drm plane
2615 * @state: plane state object to release
2616 *
2617 * Default plane state destroy hook for drivers which don't have their own
2618 * subclassed plane state structure.
2619 */
2620void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002621 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002622{
Thierry Redingf5e78402015-01-28 14:54:32 +01002623 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002624 kfree(state);
2625}
2626EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2627
2628/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002629 * __drm_atomic_helper_connector_reset - reset state on connector
2630 * @connector: drm connector
2631 * @conn_state: connector state to assign
2632 *
2633 * Initializes the newly allocated @conn_state and assigns it to
2634 * #connector ->state, usually required when initializing the drivers
2635 * or when called from the ->reset hook.
2636 *
2637 * This is useful for drivers that subclass the connector state.
2638 */
2639void
2640__drm_atomic_helper_connector_reset(struct drm_connector *connector,
2641 struct drm_connector_state *conn_state)
2642{
2643 if (conn_state)
2644 conn_state->connector = connector;
2645
2646 connector->state = conn_state;
2647}
2648EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
2649
2650/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002651 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2652 * @connector: drm connector
2653 *
2654 * Resets the atomic state for @connector by freeing the state pointer (which
2655 * might be NULL, e.g. at driver load time) and allocating a new empty state
2656 * object.
2657 */
2658void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2659{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002660 struct drm_connector_state *conn_state =
2661 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002662
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002663 kfree(connector->state);
2664 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002665}
2666EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2667
2668/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002669 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2670 * @connector: connector object
2671 * @state: atomic connector state
2672 *
2673 * Copies atomic state from a connector's current state. This is useful for
2674 * drivers that subclass the connector state.
2675 */
2676void
2677__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2678 struct drm_connector_state *state)
2679{
2680 memcpy(state, connector->state, sizeof(*state));
2681}
2682EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2683
2684/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002685 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2686 * @connector: drm connector
2687 *
2688 * Default connector state duplicate hook for drivers which don't have their own
2689 * subclassed connector state structure.
2690 */
2691struct drm_connector_state *
2692drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2693{
Thierry Redingf5e78402015-01-28 14:54:32 +01002694 struct drm_connector_state *state;
2695
Daniel Vetterd4617012014-11-03 15:56:43 +01002696 if (WARN_ON(!connector->state))
2697 return NULL;
2698
Thierry Redingf5e78402015-01-28 14:54:32 +01002699 state = kmalloc(sizeof(*state), GFP_KERNEL);
2700 if (state)
2701 __drm_atomic_helper_connector_duplicate_state(connector, state);
2702
2703 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002704}
2705EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2706
2707/**
Thierry Reding397fd772015-09-08 15:00:45 +02002708 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2709 * @dev: DRM device
2710 * @ctx: lock acquisition context
2711 *
2712 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01002713 * duplicating their respective states. This is used for example by suspend/
2714 * resume support code to save the state prior to suspend such that it can
2715 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02002716 *
2717 * Note that this treats atomic state as persistent between save and restore.
2718 * Drivers must make sure that this is possible and won't result in confusion
2719 * or erroneous behaviour.
2720 *
2721 * Note that if callers haven't already acquired all modeset locks this might
2722 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2723 *
2724 * Returns:
2725 * A pointer to the copy of the atomic state object on success or an
2726 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01002727 *
2728 * See also:
2729 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02002730 */
2731struct drm_atomic_state *
2732drm_atomic_helper_duplicate_state(struct drm_device *dev,
2733 struct drm_modeset_acquire_ctx *ctx)
2734{
2735 struct drm_atomic_state *state;
2736 struct drm_connector *conn;
2737 struct drm_plane *plane;
2738 struct drm_crtc *crtc;
2739 int err = 0;
2740
2741 state = drm_atomic_state_alloc(dev);
2742 if (!state)
2743 return ERR_PTR(-ENOMEM);
2744
2745 state->acquire_ctx = ctx;
2746
2747 drm_for_each_crtc(crtc, dev) {
2748 struct drm_crtc_state *crtc_state;
2749
2750 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2751 if (IS_ERR(crtc_state)) {
2752 err = PTR_ERR(crtc_state);
2753 goto free;
2754 }
2755 }
2756
2757 drm_for_each_plane(plane, dev) {
2758 struct drm_plane_state *plane_state;
2759
2760 plane_state = drm_atomic_get_plane_state(state, plane);
2761 if (IS_ERR(plane_state)) {
2762 err = PTR_ERR(plane_state);
2763 goto free;
2764 }
2765 }
2766
2767 drm_for_each_connector(conn, dev) {
2768 struct drm_connector_state *conn_state;
2769
2770 conn_state = drm_atomic_get_connector_state(state, conn);
2771 if (IS_ERR(conn_state)) {
2772 err = PTR_ERR(conn_state);
2773 goto free;
2774 }
2775 }
2776
2777 /* clear the acquire context so that it isn't accidentally reused */
2778 state->acquire_ctx = NULL;
2779
2780free:
2781 if (err < 0) {
2782 drm_atomic_state_free(state);
2783 state = ERR_PTR(err);
2784 }
2785
2786 return state;
2787}
2788EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2789
2790/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002791 * __drm_atomic_helper_connector_destroy_state - release connector state
2792 * @connector: connector object
2793 * @state: connector state object to release
2794 *
2795 * Releases all resources stored in the connector state without actually
2796 * freeing the memory of the connector state. This is useful for drivers that
2797 * subclass the connector state.
2798 */
2799void
2800__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2801 struct drm_connector_state *state)
2802{
2803 /*
2804 * This is currently a placeholder so that drivers that subclass the
2805 * state will automatically do the right thing if code is ever added
2806 * to this function.
2807 */
2808}
2809EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2810
2811/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002812 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2813 * @connector: drm connector
2814 * @state: connector state object to release
2815 *
2816 * Default connector state destroy hook for drivers which don't have their own
2817 * subclassed connector state structure.
2818 */
2819void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2820 struct drm_connector_state *state)
2821{
Thierry Redingf5e78402015-01-28 14:54:32 +01002822 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002823 kfree(state);
2824}
2825EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);