blob: ab275499d2a39e4010759e965b426e6bc1e08cd4 [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,
91 struct drm_encoder *new_encoder,
92 struct drm_connector *new_connector)
93{
94 struct drm_connector *connector;
95 struct drm_connector_state *conn_state;
96 int i;
97
98 for_each_connector_in_state(state, connector, conn_state, i) {
99 if (conn_state->best_encoder != new_encoder)
100 continue;
101
102 /* encoder already assigned and we're trying to re-steal it! */
103 if (connector->state->best_encoder != conn_state->best_encoder)
104 return false;
105 }
106
107 return true;
108}
109
Daniel Vetter623369e2014-09-16 17:50:47 +0200110static struct drm_crtc *
111get_current_crtc_for_encoder(struct drm_device *dev,
112 struct drm_encoder *encoder)
113{
114 struct drm_mode_config *config = &dev->mode_config;
115 struct drm_connector *connector;
116
117 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +0200119 drm_for_each_connector(connector, dev) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200120 if (connector->state->best_encoder != encoder)
121 continue;
122
123 return connector->state->crtc;
124 }
125
126 return NULL;
127}
128
129static int
130steal_encoder(struct drm_atomic_state *state,
131 struct drm_encoder *encoder,
132 struct drm_crtc *encoder_crtc)
133{
134 struct drm_mode_config *config = &state->dev->mode_config;
135 struct drm_crtc_state *crtc_state;
136 struct drm_connector *connector;
137 struct drm_connector_state *connector_state;
Daniel Vetter042652e2014-07-27 13:46:52 +0200138 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200139
140 /*
141 * We can only steal an encoder coming from a connector, which means we
142 * must already hold the connection_mutex.
143 */
144 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
145
Daniel Vetter17a38d92015-02-22 12:24:16 +0100146 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
147 encoder->base.id, encoder->name,
148 encoder_crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200149
150 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
151 if (IS_ERR(crtc_state))
152 return PTR_ERR(crtc_state);
153
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200154 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200155
156 list_for_each_entry(connector, &config->connector_list, head) {
157 if (connector->state->best_encoder != encoder)
158 continue;
159
Daniel Vetter17a38d92015-02-22 12:24:16 +0100160 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
161 connector->base.id,
162 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200163
164 connector_state = drm_atomic_get_connector_state(state,
165 connector);
166 if (IS_ERR(connector_state))
167 return PTR_ERR(connector_state);
168
Daniel Vetter042652e2014-07-27 13:46:52 +0200169 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
170 if (ret)
171 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200172 connector_state->best_encoder = NULL;
173 }
174
175 return 0;
176}
177
178static int
179update_connector_routing(struct drm_atomic_state *state, int conn_idx)
180{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200181 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200182 struct drm_encoder *new_encoder;
183 struct drm_crtc *encoder_crtc;
184 struct drm_connector *connector;
185 struct drm_connector_state *connector_state;
186 struct drm_crtc_state *crtc_state;
187 int idx, ret;
188
189 connector = state->connectors[conn_idx];
190 connector_state = state->connector_states[conn_idx];
191
192 if (!connector)
193 return 0;
194
Daniel Vetter17a38d92015-02-22 12:24:16 +0100195 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
196 connector->base.id,
197 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200198
199 if (connector->state->crtc != connector_state->crtc) {
200 if (connector->state->crtc) {
201 idx = drm_crtc_index(connector->state->crtc);
202
203 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200204 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200205 }
206
207 if (connector_state->crtc) {
208 idx = drm_crtc_index(connector_state->crtc);
209
210 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200211 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200212 }
213 }
214
215 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100216 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200217 connector->base.id,
218 connector->name);
219
220 connector_state->best_encoder = NULL;
221
222 return 0;
223 }
224
225 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200226
227 if (funcs->atomic_best_encoder)
228 new_encoder = funcs->atomic_best_encoder(connector,
229 connector_state);
230 else
231 new_encoder = funcs->best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200232
233 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100234 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
235 connector->base.id,
236 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200237 return -EINVAL;
238 }
239
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100240 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
241 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
242 new_encoder->base.id,
243 new_encoder->name,
244 connector_state->crtc->base.id);
245 return -EINVAL;
246 }
247
Daniel Vetter623369e2014-09-16 17:50:47 +0200248 if (new_encoder == connector_state->best_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100249 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
250 connector->base.id,
251 connector->name,
252 new_encoder->base.id,
253 new_encoder->name,
254 connector_state->crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200255
256 return 0;
257 }
258
Daniel Vetter97ac3202015-12-03 10:49:14 +0100259 if (!check_pending_encoder_assignment(state, new_encoder, connector)) {
260 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
261 connector->base.id,
262 connector->name);
263 return -EINVAL;
264 }
265
Daniel Vetter623369e2014-09-16 17:50:47 +0200266 encoder_crtc = get_current_crtc_for_encoder(state->dev,
267 new_encoder);
268
269 if (encoder_crtc) {
270 ret = steal_encoder(state, new_encoder, encoder_crtc);
271 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100272 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
273 connector->base.id,
274 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200275 return ret;
276 }
277 }
278
Daniel Vetter6ea76f3c2015-08-03 17:24:11 +0200279 if (WARN_ON(!connector_state->crtc))
280 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200281
Daniel Vetter623369e2014-09-16 17:50:47 +0200282 connector_state->best_encoder = new_encoder;
283 idx = drm_crtc_index(connector_state->crtc);
284
285 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200286 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200287
Daniel Vetter17a38d92015-02-22 12:24:16 +0100288 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
289 connector->base.id,
290 connector->name,
291 new_encoder->base.id,
292 new_encoder->name,
293 connector_state->crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200294
295 return 0;
296}
297
298static int
299mode_fixup(struct drm_atomic_state *state)
300{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300301 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200302 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300303 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200304 struct drm_connector_state *conn_state;
305 int i;
306 bool ret;
307
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300308 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200309 if (!crtc_state->mode_changed &&
310 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200311 continue;
312
313 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
314 }
315
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300316 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200317 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200318 struct drm_encoder *encoder;
319
Daniel Vetter623369e2014-09-16 17:50:47 +0200320 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
321
322 if (!conn_state->crtc || !conn_state->best_encoder)
323 continue;
324
325 crtc_state =
326 state->crtc_states[drm_crtc_index(conn_state->crtc)];
327
328 /*
329 * Each encoder has at most one connector (since we always steal
330 * it away), so we won't call ->mode_fixup twice.
331 */
332 encoder = conn_state->best_encoder;
333 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300334 if (!funcs)
335 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200336
Archit Taneja862e6862015-05-21 11:03:16 +0530337 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
338 &crtc_state->adjusted_mode);
339 if (!ret) {
340 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
341 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200342 }
343
Thierry Reding4cd4df82014-12-03 16:44:34 +0100344 if (funcs->atomic_check) {
345 ret = funcs->atomic_check(encoder, crtc_state,
346 conn_state);
347 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100348 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
349 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100350 return ret;
351 }
Inki Dae84524912015-08-11 21:23:49 +0900352 } else if (funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100353 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
354 &crtc_state->adjusted_mode);
355 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100356 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
357 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100358 return -EINVAL;
359 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200360 }
361 }
362
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300363 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200364 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200365
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200366 if (!crtc_state->mode_changed &&
367 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200368 continue;
369
370 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300371 if (!funcs->mode_fixup)
372 continue;
373
Daniel Vetter623369e2014-09-16 17:50:47 +0200374 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
375 &crtc_state->adjusted_mode);
376 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100377 DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
378 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200379 return -EINVAL;
380 }
381 }
382
383 return 0;
384}
385
Daniel Vetterd9b13622014-11-26 16:57:41 +0100386/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800387 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100388 * @dev: DRM device
389 * @state: the driver state object
390 *
391 * Check the state object to see if the requested state is physically possible.
392 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200393 * update and adds any additional connectors needed for full modesets and calls
394 * down into ->mode_fixup functions of the driver backend.
395 *
396 * crtc_state->mode_changed is set when the input mode is changed.
397 * crtc_state->connectors_changed is set when a connector is added or
398 * removed from the crtc.
399 * crtc_state->active_changed is set when crtc_state->active changes,
400 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100401 *
402 * IMPORTANT:
403 *
404 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
405 * plane update can't be done without a full modeset) _must_ call this function
406 * afterwards after that change. It is permitted to call this function multiple
407 * times for the same update, e.g. when the ->atomic_check functions depend upon
408 * the adjusted dotclock for fifo space allocation and watermark computation.
409 *
410 * RETURNS
411 * Zero for success or -errno
412 */
413int
Rob Clark934ce1c2014-11-19 16:41:33 -0500414drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200415 struct drm_atomic_state *state)
416{
Daniel Vetter623369e2014-09-16 17:50:47 +0200417 struct drm_crtc *crtc;
418 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300419 struct drm_connector *connector;
420 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200421 int i, ret;
422
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300423 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200424 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100425 DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
426 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200427 crtc_state->mode_changed = true;
428 }
429
430 if (crtc->state->enable != crtc_state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100431 DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
432 crtc->base.id);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200433
434 /*
435 * For clarity this assignment is done here, but
436 * enable == 0 is only true when there are no
437 * connectors and a NULL mode.
438 *
439 * The other way around is true as well. enable != 0
440 * iff connectors are attached and a mode is set.
441 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200442 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200443 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200444 }
445 }
446
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300447 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200448 /*
449 * This only sets crtc->mode_changed for routing changes,
450 * drivers must set crtc->mode_changed themselves when connector
451 * properties need to be updated.
452 */
453 ret = update_connector_routing(state, i);
454 if (ret)
455 return ret;
456 }
457
458 /*
459 * After all the routing has been prepared we need to add in any
460 * connector which is itself unchanged, but who's crtc changes it's
461 * configuration. This must be done before calling mode_fixup in case a
462 * crtc only changed its mode but has the same set of connectors.
463 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300464 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200465 int num_connectors;
466
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100467 /*
468 * We must set ->active_changed after walking connectors for
469 * otherwise an update that only changes active would result in
470 * a full modeset because update_connector_routing force that.
471 */
472 if (crtc->state->active != crtc_state->active) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100473 DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
474 crtc->base.id);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100475 crtc_state->active_changed = true;
476 }
477
Daniel Vetter2465ff62015-06-18 09:58:55 +0200478 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100479 continue;
480
Daniel Vetter17a38d92015-02-22 12:24:16 +0100481 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
482 crtc->base.id,
483 crtc_state->enable ? 'y' : 'n',
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100484 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200485
486 ret = drm_atomic_add_affected_connectors(state, crtc);
487 if (ret != 0)
488 return ret;
489
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200490 ret = drm_atomic_add_affected_planes(state, crtc);
491 if (ret != 0)
492 return ret;
493
Daniel Vetter623369e2014-09-16 17:50:47 +0200494 num_connectors = drm_atomic_connectors_for_crtc(state,
495 crtc);
496
497 if (crtc_state->enable != !!num_connectors) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100498 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
499 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200500
501 return -EINVAL;
502 }
503 }
504
505 return mode_fixup(state);
506}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100507EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200508
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100509/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800510 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100511 * @dev: DRM device
512 * @state: the driver state object
513 *
514 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100515 * This does all the plane update related checks using by calling into the
516 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100517 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200518 * It also sets crtc_state->planes_changed to indicate that a crtc has
519 * updated planes.
520 *
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100521 * RETURNS
522 * Zero for success or -errno
523 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100524int
525drm_atomic_helper_check_planes(struct drm_device *dev,
526 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100527{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300528 struct drm_crtc *crtc;
529 struct drm_crtc_state *crtc_state;
530 struct drm_plane *plane;
531 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100532 int i, ret = 0;
533
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300534 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200535 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100536
537 funcs = plane->helper_private;
538
539 drm_atomic_helper_plane_changed(state, plane_state, plane);
540
541 if (!funcs || !funcs->atomic_check)
542 continue;
543
544 ret = funcs->atomic_check(plane, plane_state);
545 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100546 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
547 plane->base.id);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100548 return ret;
549 }
550 }
551
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300552 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200553 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100554
555 funcs = crtc->helper_private;
556
557 if (!funcs || !funcs->atomic_check)
558 continue;
559
560 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
561 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100562 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
563 crtc->base.id);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100564 return ret;
565 }
566 }
567
Daniel Vetterd9b13622014-11-26 16:57:41 +0100568 return ret;
569}
570EXPORT_SYMBOL(drm_atomic_helper_check_planes);
571
572/**
573 * drm_atomic_helper_check - validate state object
574 * @dev: DRM device
575 * @state: the driver state object
576 *
577 * Check the state object to see if the requested state is physically possible.
578 * Only crtcs and planes have check callbacks, so for any additional (global)
579 * checking that a driver needs it can simply wrap that around this function.
580 * Drivers without such needs can directly use this as their ->atomic_check()
581 * callback.
582 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100583 * This just wraps the two parts of the state checking for planes and modeset
584 * state in the default order: First it calls drm_atomic_helper_check_modeset()
585 * and then drm_atomic_helper_check_planes(). The assumption is that the
586 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
587 * e.g. properly compute watermarks.
588 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100589 * RETURNS
590 * Zero for success or -errno
591 */
592int drm_atomic_helper_check(struct drm_device *dev,
593 struct drm_atomic_state *state)
594{
595 int ret;
596
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100597 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100598 if (ret)
599 return ret;
600
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100601 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500602 if (ret)
603 return ret;
604
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100605 return ret;
606}
607EXPORT_SYMBOL(drm_atomic_helper_check);
608
Daniel Vetter623369e2014-09-16 17:50:47 +0200609static void
610disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
611{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300612 struct drm_connector *connector;
613 struct drm_connector_state *old_conn_state;
614 struct drm_crtc *crtc;
615 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200616 int i;
617
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300618 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200619 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200620 struct drm_encoder *encoder;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100621 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200622
Daniel Vetter623369e2014-09-16 17:50:47 +0200623 /* Shut down everything that's in the changeset and currently
624 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300625 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200626 continue;
627
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100628 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
629
Daniel Vetter4218a322015-03-26 22:18:40 +0100630 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200631 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100632 continue;
633
Rob Clark46df9ad2014-11-20 15:40:36 -0500634 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200635
Rob Clark46df9ad2014-11-20 15:40:36 -0500636 /* We shouldn't get this far if we didn't previously have
637 * an encoder.. but WARN_ON() rather than explode.
638 */
639 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200640 continue;
641
642 funcs = encoder->helper_private;
643
Daniel Vetter17a38d92015-02-22 12:24:16 +0100644 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
645 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100646
Daniel Vetter623369e2014-09-16 17:50:47 +0200647 /*
648 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800649 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200650 */
Archit Taneja862e6862015-05-21 11:03:16 +0530651 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200652
653 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100654 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200655 funcs->prepare(encoder);
656 else if (funcs->disable)
657 funcs->disable(encoder);
658 else
659 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
660
Archit Taneja862e6862015-05-21 11:03:16 +0530661 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200662 }
663
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300664 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200665 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200666
667 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200668 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100669 continue;
670
671 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200672 continue;
673
674 funcs = crtc->helper_private;
675
Daniel Vetter17a38d92015-02-22 12:24:16 +0100676 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
677 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100678
679
Daniel Vetter623369e2014-09-16 17:50:47 +0200680 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100681 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200682 funcs->prepare(crtc);
683 else if (funcs->disable)
684 funcs->disable(crtc);
685 else
686 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
687 }
688}
689
Daniel Vetter4c18d302015-05-12 15:27:37 +0200690/**
691 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
692 * @dev: DRM device
693 * @old_state: atomic state object with old state structures
694 *
695 * This function updates all the various legacy modeset state pointers in
696 * connectors, encoders and crtcs. It also updates the timestamping constants
697 * used for precise vblank timestamps by calling
698 * drm_calc_timestamping_constants().
699 *
700 * Drivers can use this for building their own atomic commit if they don't have
701 * a pure helper-based modeset implementation.
702 */
703void
704drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
705 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200706{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300707 struct drm_connector *connector;
708 struct drm_connector_state *old_conn_state;
709 struct drm_crtc *crtc;
710 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200711 int i;
712
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200713 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300714 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200715 if (connector->encoder) {
716 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200717
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200718 connector->encoder->crtc = NULL;
719 connector->encoder = NULL;
720 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200721
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200722 crtc = connector->state->crtc;
723 if ((!crtc && old_conn_state->crtc) ||
724 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
725 struct drm_property *dpms_prop =
726 dev->mode_config.dpms_property;
727 int mode = DRM_MODE_DPMS_OFF;
728
729 if (crtc && crtc->state->active)
730 mode = DRM_MODE_DPMS_ON;
731
732 connector->dpms = mode;
733 drm_object_property_set_value(&connector->base,
734 dpms_prop, mode);
735 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200736 }
737
738 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300739 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
740 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200741 continue;
742
743 if (WARN_ON(!connector->state->best_encoder))
744 continue;
745
746 connector->encoder = connector->state->best_encoder;
747 connector->encoder->crtc = connector->state->crtc;
748 }
749
750 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300751 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200752 struct drm_plane *primary = crtc->primary;
753
Daniel Vetter623369e2014-09-16 17:50:47 +0200754 crtc->mode = crtc->state->mode;
755 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200756
757 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
758 primary->state->crtc == crtc) {
759 crtc->x = primary->state->src_x >> 16;
760 crtc->y = primary->state->src_y >> 16;
761 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200762
763 if (crtc->state->enable)
764 drm_calc_timestamping_constants(crtc,
765 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200766 }
767}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200768EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200769
770static void
771crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
772{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300773 struct drm_crtc *crtc;
774 struct drm_crtc_state *old_crtc_state;
775 struct drm_connector *connector;
776 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200777 int i;
778
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300779 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200780 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200781
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300782 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200783 continue;
784
785 funcs = crtc->helper_private;
786
Daniel Vetterc982bd92015-02-22 12:24:20 +0100787 if (crtc->state->enable && funcs->mode_set_nofb) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100788 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
789 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100790
Daniel Vetter623369e2014-09-16 17:50:47 +0200791 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100792 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200793 }
794
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300795 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200796 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200797 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200798 struct drm_encoder *encoder;
799 struct drm_display_mode *mode, *adjusted_mode;
800
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300801 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200802 continue;
803
804 encoder = connector->state->best_encoder;
805 funcs = encoder->helper_private;
806 new_crtc_state = connector->state->crtc->state;
807 mode = &new_crtc_state->mode;
808 adjusted_mode = &new_crtc_state->adjusted_mode;
809
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100810 if (!new_crtc_state->mode_changed)
811 continue;
812
Daniel Vetter17a38d92015-02-22 12:24:16 +0100813 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
814 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100815
Daniel Vetter623369e2014-09-16 17:50:47 +0200816 /*
817 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800818 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200819 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100820 if (funcs->mode_set)
821 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200822
Archit Taneja862e6862015-05-21 11:03:16 +0530823 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200824 }
825}
826
827/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100828 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200829 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100830 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200831 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100832 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200833 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100834 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300835 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100836 * drm_atomic_helper_commit_planes(), which is what the default commit function
837 * does. But drivers with different needs can group the modeset commits together
838 * and do the plane commits at the end. This is useful for drivers doing runtime
839 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200840 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100841void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
842 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200843{
Laurent Pincharta072f802015-02-22 12:24:18 +0100844 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200845
846 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
847
Laurent Pincharta072f802015-02-22 12:24:18 +0100848 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200849}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100850EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200851
852/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100853 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200854 * @dev: DRM device
855 * @old_state: atomic state object with old state structures
856 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100857 * This function enables all the outputs with the new configuration which had to
858 * be turned off for the update.
859 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300860 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100861 * drm_atomic_helper_commit_planes(), which is what the default commit function
862 * does. But drivers with different needs can group the modeset commits together
863 * and do the plane commits at the end. This is useful for drivers doing runtime
864 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200865 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100866void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
867 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200868{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300869 struct drm_crtc *crtc;
870 struct drm_crtc_state *old_crtc_state;
871 struct drm_connector *connector;
872 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200873 int i;
874
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300875 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200876 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200877
878 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200879 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100880 continue;
881
882 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200883 continue;
884
885 funcs = crtc->helper_private;
886
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100887 if (crtc->state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100888 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
889 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100890
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100891 if (funcs->enable)
892 funcs->enable(crtc);
893 else
894 funcs->commit(crtc);
895 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200896 }
897
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300898 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200899 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200900 struct drm_encoder *encoder;
901
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300902 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200903 continue;
904
Daniel Vetter4218a322015-03-26 22:18:40 +0100905 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200906 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100907 continue;
908
Daniel Vetter623369e2014-09-16 17:50:47 +0200909 encoder = connector->state->best_encoder;
910 funcs = encoder->helper_private;
911
Daniel Vetter17a38d92015-02-22 12:24:16 +0100912 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
913 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100914
Daniel Vetter623369e2014-09-16 17:50:47 +0200915 /*
916 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800917 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200918 */
Archit Taneja862e6862015-05-21 11:03:16 +0530919 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200920
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100921 if (funcs->enable)
922 funcs->enable(encoder);
923 else
924 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200925
Archit Taneja862e6862015-05-21 11:03:16 +0530926 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200927 }
928}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100929EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200930
Daniel Vettere2330f02014-10-29 11:34:56 +0100931static void wait_for_fences(struct drm_device *dev,
932 struct drm_atomic_state *state)
933{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300934 struct drm_plane *plane;
935 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +0100936 int i;
937
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300938 for_each_plane_in_state(state, plane, plane_state, i) {
939 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +0100940 continue;
941
942 WARN_ON(!plane->state->fb);
943
944 fence_wait(plane->state->fence, false);
945 fence_put(plane->state->fence);
946 plane->state->fence = NULL;
947 }
948}
949
Daniel Vetterab58e332014-11-24 20:42:42 +0100950static bool framebuffer_changed(struct drm_device *dev,
951 struct drm_atomic_state *old_state,
952 struct drm_crtc *crtc)
953{
954 struct drm_plane *plane;
955 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +0100956 int i;
957
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300958 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +0100959 if (plane->state->crtc != crtc &&
960 old_plane_state->crtc != crtc)
961 continue;
962
963 if (plane->state->fb != old_plane_state->fb)
964 return true;
965 }
966
967 return false;
968}
969
Rob Clark5ee32292014-11-11 19:38:59 -0500970/**
971 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
972 * @dev: DRM device
973 * @old_state: atomic state object with old state structures
974 *
975 * Helper to, after atomic commit, wait for vblanks on all effected
976 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100977 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
978 * framebuffers have actually changed to optimize for the legacy cursor and
979 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500980 */
981void
982drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
983 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200984{
985 struct drm_crtc *crtc;
986 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200987 int i, ret;
988
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300989 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200990 /* No one cares about the old state, so abuse it for tracking
991 * and store whether we hold a vblank reference (and should do a
992 * vblank wait) in the ->enable boolean. */
993 old_crtc_state->enable = false;
994
995 if (!crtc->state->enable)
996 continue;
997
Daniel Vetterf02ad902015-01-22 16:36:23 +0100998 /* Legacy cursor ioctls are completely unsynced, and userspace
999 * relies on that (by doing tons of cursor updates). */
1000 if (old_state->legacy_cursor_update)
1001 continue;
1002
Daniel Vetterab58e332014-11-24 20:42:42 +01001003 if (!framebuffer_changed(dev, old_state, crtc))
1004 continue;
1005
Daniel Vetter623369e2014-09-16 17:50:47 +02001006 ret = drm_crtc_vblank_get(crtc);
1007 if (ret != 0)
1008 continue;
1009
1010 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +02001011 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001012 }
1013
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001014 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1015 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +02001016 continue;
1017
1018 ret = wait_event_timeout(dev->vblank[i].queue,
1019 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001020 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001021 msecs_to_jiffies(50));
1022
1023 drm_crtc_vblank_put(crtc);
1024 }
1025}
Rob Clark5ee32292014-11-11 19:38:59 -05001026EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001027
1028/**
1029 * drm_atomic_helper_commit - commit validated state object
1030 * @dev: DRM device
1031 * @state: the driver state object
1032 * @async: asynchronous commit
1033 *
1034 * This function commits a with drm_atomic_helper_check() pre-validated state
1035 * object. This can still fail when e.g. the framebuffer reservation fails. For
1036 * now this doesn't implement asynchronous commits.
1037 *
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001038 * Note that right now this function does not support async commits, and hence
1039 * driver writers must implement their own version for now. Also note that the
1040 * default ordering of how the various stages are called is to match the legacy
1041 * modeset helper library closest. One peculiarity of that is that it doesn't
1042 * mesh well with runtime PM at all.
1043 *
1044 * For drivers supporting runtime PM the recommended sequence is
1045 *
1046 * drm_atomic_helper_commit_modeset_disables(dev, state);
1047 *
1048 * drm_atomic_helper_commit_modeset_enables(dev, state);
1049 *
1050 * drm_atomic_helper_commit_planes(dev, state, true);
1051 *
1052 * See the kerneldoc entries for these three functions for more details.
1053 *
Daniel Vetter623369e2014-09-16 17:50:47 +02001054 * RETURNS
1055 * Zero for success or -errno.
1056 */
1057int drm_atomic_helper_commit(struct drm_device *dev,
1058 struct drm_atomic_state *state,
1059 bool async)
1060{
1061 int ret;
1062
1063 if (async)
1064 return -EBUSY;
1065
1066 ret = drm_atomic_helper_prepare_planes(dev, state);
1067 if (ret)
1068 return ret;
1069
1070 /*
1071 * This is the point of no return - everything below never fails except
1072 * when the hw goes bonghits. Which means we can commit the new state on
1073 * the software side now.
1074 */
1075
1076 drm_atomic_helper_swap_state(dev, state);
1077
1078 /*
1079 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001080 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001081 * that the asynchronous work has either been cancelled (if the driver
1082 * supports it, which at least requires that the framebuffers get
1083 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1084 * before the new state gets committed on the software side with
1085 * drm_atomic_helper_swap_state().
1086 *
1087 * This scheme allows new atomic state updates to be prepared and
1088 * checked in parallel to the asynchronous completion of the previous
1089 * update. Which is important since compositors need to figure out the
1090 * composition of the next frame right after having submitted the
1091 * current layout.
1092 */
1093
Daniel Vettere2330f02014-10-29 11:34:56 +01001094 wait_for_fences(dev, state);
1095
Daniel Vetter1af434a2015-02-22 12:24:19 +01001096 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001097
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001098 drm_atomic_helper_commit_planes(dev, state, false);
Daniel Vetter623369e2014-09-16 17:50:47 +02001099
Daniel Vetter1af434a2015-02-22 12:24:19 +01001100 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001101
Rob Clark5ee32292014-11-11 19:38:59 -05001102 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001103
1104 drm_atomic_helper_cleanup_planes(dev, state);
1105
1106 drm_atomic_state_free(state);
1107
1108 return 0;
1109}
1110EXPORT_SYMBOL(drm_atomic_helper_commit);
1111
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001112/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001113 * DOC: implementing async commit
1114 *
1115 * For now the atomic helpers don't support async commit directly. If there is
1116 * real need it could be added though, using the dma-buf fence infrastructure
1117 * for generic synchronization with outstanding rendering.
1118 *
1119 * For now drivers have to implement async commit themselves, with the following
1120 * sequence being the recommended one:
1121 *
1122 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1123 * which commit needs to call which can fail, so we want to run it first and
1124 * synchronously.
1125 *
1126 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1127 * might be affected the new state update. This can be done by either cancelling
1128 * or flushing the work items, depending upon whether the driver can deal with
1129 * cancelled updates. Note that it is important to ensure that the framebuffer
1130 * cleanup is still done when cancelling.
1131 *
1132 * For sufficient parallelism it is recommended to have a work item per crtc
1133 * (for updates which don't touch global state) and a global one. Then we only
1134 * need to synchronize with the crtc work items for changed crtcs and the global
1135 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1136 *
1137 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001138 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001139 * locks means concurrent callers never see inconsistent state. And doing this
1140 * while it's guaranteed that no relevant async worker runs means that async
1141 * workers do not need grab any locks. Actually they must not grab locks, for
1142 * otherwise the work flushing will deadlock.
1143 *
1144 * 4. Schedule a work item to do all subsequent steps, using the split-out
1145 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1146 * then cleaning up the framebuffers after the old framebuffer is no longer
1147 * being displayed.
1148 */
1149
1150/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001151 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001152 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001153 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001154 *
1155 * This function prepares plane state, specifically framebuffers, for the new
1156 * configuration. If any failure is encountered this function will call
1157 * ->cleanup_fb on any already successfully prepared framebuffer.
1158 *
1159 * Returns:
1160 * 0 on success, negative error code on failure.
1161 */
1162int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1163 struct drm_atomic_state *state)
1164{
1165 int nplanes = dev->mode_config.num_total_plane;
1166 int ret, i;
1167
1168 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001169 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001170 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001171 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001172
1173 if (!plane)
1174 continue;
1175
1176 funcs = plane->helper_private;
1177
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001178 if (funcs->prepare_fb) {
1179 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001180 if (ret)
1181 goto fail;
1182 }
1183 }
1184
1185 return 0;
1186
1187fail:
1188 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001189 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001190 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001191 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001192
1193 if (!plane)
1194 continue;
1195
1196 funcs = plane->helper_private;
1197
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001198 if (funcs->cleanup_fb)
1199 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001200
1201 }
1202
1203 return ret;
1204}
1205EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1206
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001207bool plane_crtc_active(struct drm_plane_state *state)
1208{
1209 return state->crtc && state->crtc->state->active;
1210}
1211
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001212/**
1213 * drm_atomic_helper_commit_planes - commit plane state
1214 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001215 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001216 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001217 *
1218 * This function commits the new plane state using the plane and atomic helper
1219 * functions for planes and crtcs. It assumes that the atomic state has already
1220 * been pushed into the relevant object state pointers, since this step can no
1221 * longer fail.
1222 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001223 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001224 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001225 *
1226 * Note that this function does all plane updates across all CRTCs in one step.
1227 * If the hardware can't support this approach look at
1228 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001229 *
1230 * Plane parameters can be updated by applications while the associated CRTC is
1231 * disabled. The DRM/KMS core will store the parameters in the plane state,
1232 * which will be available to the driver when the CRTC is turned on. As a result
1233 * most drivers don't need to be immediately notified of plane updates for a
1234 * disabled CRTC.
1235 *
1236 * Unless otherwise needed, drivers are advised to set the @active_only
1237 * parameters to true in order not to receive plane update notifications related
1238 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1239 * driver code when the driver and/or hardware can't or just don't need to deal
1240 * with updates on disabled CRTCs, for example when supporting runtime PM.
1241 *
1242 * The drm_atomic_helper_commit() default implementation only sets @active_only
1243 * to false to most closely match the behaviour of the legacy helpers. This should
1244 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001245 */
1246void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001247 struct drm_atomic_state *old_state,
1248 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001249{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001250 struct drm_crtc *crtc;
1251 struct drm_crtc_state *old_crtc_state;
1252 struct drm_plane *plane;
1253 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001254 int i;
1255
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001256 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001257 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001258
1259 funcs = crtc->helper_private;
1260
1261 if (!funcs || !funcs->atomic_begin)
1262 continue;
1263
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001264 if (active_only && !crtc->state->active)
1265 continue;
1266
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001267 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001268 }
1269
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001270 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001271 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001272 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001273
1274 funcs = plane->helper_private;
1275
Thierry Reding3cad4b62014-11-25 13:05:12 +01001276 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001277 continue;
1278
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001279 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1280
1281 if (active_only) {
1282 /*
1283 * Skip planes related to inactive CRTCs. If the plane
1284 * is enabled use the state of the current CRTC. If the
1285 * plane is being disabled use the state of the old
1286 * CRTC to avoid skipping planes being disabled on an
1287 * active CRTC.
1288 */
1289 if (!disabling && !plane_crtc_active(plane->state))
1290 continue;
1291 if (disabling && !plane_crtc_active(old_plane_state))
1292 continue;
1293 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001294
Thierry Reding407b8bd2014-11-20 12:05:50 +01001295 /*
1296 * Special-case disabling the plane if drivers support it.
1297 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001298 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001299 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001300 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001301 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001302 }
1303
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001304 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001305 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001306
1307 funcs = crtc->helper_private;
1308
1309 if (!funcs || !funcs->atomic_flush)
1310 continue;
1311
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001312 if (active_only && !crtc->state->active)
1313 continue;
1314
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001315 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001316 }
1317}
1318EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1319
1320/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001321 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1322 * @old_crtc_state: atomic state object with the old crtc state
1323 *
1324 * This function commits the new plane state using the plane and atomic helper
1325 * functions for planes on the specific crtc. It assumes that the atomic state
1326 * has already been pushed into the relevant object state pointers, since this
1327 * step can no longer fail.
1328 *
1329 * This function is useful when plane updates should be done crtc-by-crtc
1330 * instead of one global step like drm_atomic_helper_commit_planes() does.
1331 *
1332 * This function can only be savely used when planes are not allowed to move
1333 * between different CRTCs because this function doesn't handle inter-CRTC
1334 * depencies. Callers need to ensure that either no such depencies exist,
1335 * resolve them through ordering of commit calls or through some other means.
1336 */
1337void
1338drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1339{
1340 const struct drm_crtc_helper_funcs *crtc_funcs;
1341 struct drm_crtc *crtc = old_crtc_state->crtc;
1342 struct drm_atomic_state *old_state = old_crtc_state->state;
1343 struct drm_plane *plane;
1344 unsigned plane_mask;
1345
1346 plane_mask = old_crtc_state->plane_mask;
1347 plane_mask |= crtc->state->plane_mask;
1348
1349 crtc_funcs = crtc->helper_private;
1350 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001351 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001352
1353 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1354 struct drm_plane_state *old_plane_state =
1355 drm_atomic_get_existing_plane_state(old_state, plane);
1356 const struct drm_plane_helper_funcs *plane_funcs;
1357
1358 plane_funcs = plane->helper_private;
1359
1360 if (!old_plane_state || !plane_funcs)
1361 continue;
1362
1363 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1364
1365 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1366 plane_funcs->atomic_disable)
1367 plane_funcs->atomic_disable(plane, old_plane_state);
1368 else if (plane->state->crtc ||
1369 drm_atomic_plane_disabling(plane, old_plane_state))
1370 plane_funcs->atomic_update(plane, old_plane_state);
1371 }
1372
1373 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001374 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001375}
1376EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1377
1378/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001379 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1380 * @crtc: CRTC
1381 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1382 *
1383 * Disables all planes associated with the given CRTC. This can be
1384 * used for instance in the CRTC helper disable callback to disable
1385 * all planes before shutting down the display pipeline.
1386 *
1387 * If the atomic-parameter is set the function calls the CRTC's
1388 * atomic_begin hook before and atomic_flush hook after disabling the
1389 * planes.
1390 *
1391 * It is a bug to call this function without having implemented the
1392 * ->atomic_disable() plane hook.
1393 */
1394void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1395 bool atomic)
1396{
1397 const struct drm_crtc_helper_funcs *crtc_funcs =
1398 crtc->helper_private;
1399 struct drm_plane *plane;
1400
1401 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1402 crtc_funcs->atomic_begin(crtc, NULL);
1403
1404 drm_for_each_plane(plane, crtc->dev) {
1405 const struct drm_plane_helper_funcs *plane_funcs =
1406 plane->helper_private;
1407
1408 if (plane->state->crtc != crtc || !plane_funcs)
1409 continue;
1410
1411 WARN_ON(!plane_funcs->atomic_disable);
1412 if (plane_funcs->atomic_disable)
1413 plane_funcs->atomic_disable(plane, NULL);
1414 }
1415
1416 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1417 crtc_funcs->atomic_flush(crtc, NULL);
1418}
1419EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1420
1421/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001422 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1423 * @dev: DRM device
1424 * @old_state: atomic state object with old state structures
1425 *
1426 * This function cleans up plane state, specifically framebuffers, from the old
1427 * configuration. Hence the old configuration must be perserved in @old_state to
1428 * be able to call this function.
1429 *
1430 * This function must also be called on the new state when the atomic update
1431 * fails at any point after calling drm_atomic_helper_prepare_planes().
1432 */
1433void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1434 struct drm_atomic_state *old_state)
1435{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001436 struct drm_plane *plane;
1437 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001438 int i;
1439
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001440 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001441 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001442
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001443 funcs = plane->helper_private;
1444
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001445 if (funcs->cleanup_fb)
1446 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001447 }
1448}
1449EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1450
1451/**
1452 * drm_atomic_helper_swap_state - store atomic state into current sw state
1453 * @dev: DRM device
1454 * @state: atomic state
1455 *
1456 * This function stores the atomic state into the current state pointers in all
1457 * driver objects. It should be called after all failing steps have been done
1458 * and succeeded, but before the actual hardware state is committed.
1459 *
1460 * For cleanup and error recovery the current state for all changed objects will
1461 * be swaped into @state.
1462 *
1463 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1464 *
1465 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1466 *
1467 * 2. Do any other steps that might fail.
1468 *
1469 * 3. Put the staged state into the current state pointers with this function.
1470 *
1471 * 4. Actually commit the hardware state.
1472 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001473 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001474 * contains the old state. Also do any other cleanup required with that state.
1475 */
1476void drm_atomic_helper_swap_state(struct drm_device *dev,
1477 struct drm_atomic_state *state)
1478{
1479 int i;
1480
1481 for (i = 0; i < dev->mode_config.num_connector; i++) {
1482 struct drm_connector *connector = state->connectors[i];
1483
1484 if (!connector)
1485 continue;
1486
1487 connector->state->state = state;
1488 swap(state->connector_states[i], connector->state);
1489 connector->state->state = NULL;
1490 }
1491
1492 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1493 struct drm_crtc *crtc = state->crtcs[i];
1494
1495 if (!crtc)
1496 continue;
1497
1498 crtc->state->state = state;
1499 swap(state->crtc_states[i], crtc->state);
1500 crtc->state->state = NULL;
1501 }
1502
1503 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1504 struct drm_plane *plane = state->planes[i];
1505
1506 if (!plane)
1507 continue;
1508
1509 plane->state->state = state;
1510 swap(state->plane_states[i], plane->state);
1511 plane->state->state = NULL;
1512 }
1513}
1514EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001515
1516/**
1517 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1518 * @plane: plane object to update
1519 * @crtc: owning CRTC of owning plane
1520 * @fb: framebuffer to flip onto plane
1521 * @crtc_x: x offset of primary plane on crtc
1522 * @crtc_y: y offset of primary plane on crtc
1523 * @crtc_w: width of primary plane rectangle on crtc
1524 * @crtc_h: height of primary plane rectangle on crtc
1525 * @src_x: x offset of @fb for panning
1526 * @src_y: y offset of @fb for panning
1527 * @src_w: width of source rectangle in @fb
1528 * @src_h: height of source rectangle in @fb
1529 *
1530 * Provides a default plane update handler using the atomic driver interface.
1531 *
1532 * RETURNS:
1533 * Zero on success, error code on failure
1534 */
1535int drm_atomic_helper_update_plane(struct drm_plane *plane,
1536 struct drm_crtc *crtc,
1537 struct drm_framebuffer *fb,
1538 int crtc_x, int crtc_y,
1539 unsigned int crtc_w, unsigned int crtc_h,
1540 uint32_t src_x, uint32_t src_y,
1541 uint32_t src_w, uint32_t src_h)
1542{
1543 struct drm_atomic_state *state;
1544 struct drm_plane_state *plane_state;
1545 int ret = 0;
1546
1547 state = drm_atomic_state_alloc(plane->dev);
1548 if (!state)
1549 return -ENOMEM;
1550
1551 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1552retry:
1553 plane_state = drm_atomic_get_plane_state(state, plane);
1554 if (IS_ERR(plane_state)) {
1555 ret = PTR_ERR(plane_state);
1556 goto fail;
1557 }
1558
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001559 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001560 if (ret != 0)
1561 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001562 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001563 plane_state->crtc_x = crtc_x;
1564 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001565 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001566 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001567 plane_state->src_x = src_x;
1568 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001569 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001570 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001571
Daniel Vetter3671c582015-05-04 15:40:52 +02001572 if (plane == crtc->cursor)
1573 state->legacy_cursor_update = true;
1574
Daniel Vetter042652e2014-07-27 13:46:52 +02001575 ret = drm_atomic_commit(state);
1576 if (ret != 0)
1577 goto fail;
1578
1579 /* Driver takes ownership of state on successful commit. */
1580 return 0;
1581fail:
1582 if (ret == -EDEADLK)
1583 goto backoff;
1584
1585 drm_atomic_state_free(state);
1586
1587 return ret;
1588backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001589 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001590 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001591
1592 /*
1593 * Someone might have exchanged the framebuffer while we dropped locks
1594 * in the backoff code. We need to fix up the fb refcount tracking the
1595 * core does for us.
1596 */
1597 plane->old_fb = plane->fb;
1598
1599 goto retry;
1600}
1601EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1602
1603/**
1604 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1605 * @plane: plane to disable
1606 *
1607 * Provides a default plane disable handler using the atomic driver interface.
1608 *
1609 * RETURNS:
1610 * Zero on success, error code on failure
1611 */
1612int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1613{
1614 struct drm_atomic_state *state;
1615 struct drm_plane_state *plane_state;
1616 int ret = 0;
1617
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001618 /*
1619 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1620 * acquire context. The real fix will be to wire the acquire ctx through
1621 * everywhere we need it, but meanwhile prevent chaos by just skipping
1622 * this noop. The critical case is the cursor ioctls which a) only grab
1623 * crtc/cursor-plane locks (so we need the crtc to get at the right
1624 * acquire context) and b) can try to disable the plane multiple times.
1625 */
1626 if (!plane->crtc)
1627 return 0;
1628
Daniel Vetter042652e2014-07-27 13:46:52 +02001629 state = drm_atomic_state_alloc(plane->dev);
1630 if (!state)
1631 return -ENOMEM;
1632
1633 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1634retry:
1635 plane_state = drm_atomic_get_plane_state(state, plane);
1636 if (IS_ERR(plane_state)) {
1637 ret = PTR_ERR(plane_state);
1638 goto fail;
1639 }
1640
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01001641 if (plane_state->crtc && (plane == plane->crtc->cursor))
1642 plane_state->state->legacy_cursor_update = true;
1643
Rob Clarkbbb1e522015-08-25 15:35:58 -04001644 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001645 if (ret != 0)
1646 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01001647
Daniel Vetter042652e2014-07-27 13:46:52 +02001648 ret = drm_atomic_commit(state);
1649 if (ret != 0)
1650 goto fail;
1651
1652 /* Driver takes ownership of state on successful commit. */
1653 return 0;
1654fail:
1655 if (ret == -EDEADLK)
1656 goto backoff;
1657
1658 drm_atomic_state_free(state);
1659
1660 return ret;
1661backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001662 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001663 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001664
1665 /*
1666 * Someone might have exchanged the framebuffer while we dropped locks
1667 * in the backoff code. We need to fix up the fb refcount tracking the
1668 * core does for us.
1669 */
1670 plane->old_fb = plane->fb;
1671
1672 goto retry;
1673}
1674EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1675
Rob Clarkbbb1e522015-08-25 15:35:58 -04001676/* just used from fb-helper and atomic-helper: */
1677int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1678 struct drm_plane_state *plane_state)
1679{
1680 int ret;
1681
1682 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1683 if (ret != 0)
1684 return ret;
1685
1686 drm_atomic_set_fb_for_plane(plane_state, NULL);
1687 plane_state->crtc_x = 0;
1688 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001689 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001690 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001691 plane_state->src_x = 0;
1692 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001693 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001694 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001695
Rob Clarkbbb1e522015-08-25 15:35:58 -04001696 return 0;
1697}
1698
Daniel Vetter042652e2014-07-27 13:46:52 +02001699static int update_output_state(struct drm_atomic_state *state,
1700 struct drm_mode_set *set)
1701{
1702 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001703 struct drm_crtc *crtc;
1704 struct drm_crtc_state *crtc_state;
1705 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001706 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001707 int ret, i, j;
1708
1709 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1710 state->acquire_ctx);
1711 if (ret)
1712 return ret;
1713
1714 /* First grab all affected connector/crtc states. */
1715 for (i = 0; i < set->num_connectors; i++) {
1716 conn_state = drm_atomic_get_connector_state(state,
1717 set->connectors[i]);
1718 if (IS_ERR(conn_state))
1719 return PTR_ERR(conn_state);
1720 }
1721
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001722 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001723 ret = drm_atomic_add_affected_connectors(state, crtc);
1724 if (ret)
1725 return ret;
1726 }
1727
1728 /* Then recompute connector->crtc links and crtc enabling state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001729 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001730 if (conn_state->crtc == set->crtc) {
1731 ret = drm_atomic_set_crtc_for_connector(conn_state,
1732 NULL);
1733 if (ret)
1734 return ret;
1735 }
1736
1737 for (j = 0; j < set->num_connectors; j++) {
1738 if (set->connectors[j] == connector) {
1739 ret = drm_atomic_set_crtc_for_connector(conn_state,
1740 set->crtc);
1741 if (ret)
1742 return ret;
1743 break;
1744 }
1745 }
1746 }
1747
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001748 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001749 /* Don't update ->enable for the CRTC in the set_config request,
1750 * since a mismatch would indicate a bug in the upper layers.
1751 * The actual modeset code later on will catch any
1752 * inconsistencies here. */
1753 if (crtc == set->crtc)
1754 continue;
1755
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001756 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1757 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1758 NULL);
1759 if (ret < 0)
1760 return ret;
1761
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001762 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001763 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001764 }
1765
1766 return 0;
1767}
1768
1769/**
1770 * drm_atomic_helper_set_config - set a new config from userspace
1771 * @set: mode set configuration
1772 *
1773 * Provides a default crtc set_config handler using the atomic driver interface.
1774 *
1775 * Returns:
1776 * Returns 0 on success, negative errno numbers on failure.
1777 */
1778int drm_atomic_helper_set_config(struct drm_mode_set *set)
1779{
1780 struct drm_atomic_state *state;
1781 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02001782 int ret = 0;
1783
1784 state = drm_atomic_state_alloc(crtc->dev);
1785 if (!state)
1786 return -ENOMEM;
1787
1788 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1789retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04001790 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01001791 if (ret != 0)
1792 goto fail;
1793
Daniel Vetter042652e2014-07-27 13:46:52 +02001794 ret = drm_atomic_commit(state);
1795 if (ret != 0)
1796 goto fail;
1797
1798 /* Driver takes ownership of state on successful commit. */
1799 return 0;
1800fail:
1801 if (ret == -EDEADLK)
1802 goto backoff;
1803
1804 drm_atomic_state_free(state);
1805
1806 return ret;
1807backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001808 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001809 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001810
1811 /*
1812 * Someone might have exchanged the framebuffer while we dropped locks
1813 * in the backoff code. We need to fix up the fb refcount tracking the
1814 * core does for us.
1815 */
1816 crtc->primary->old_fb = crtc->primary->fb;
1817
1818 goto retry;
1819}
1820EXPORT_SYMBOL(drm_atomic_helper_set_config);
1821
Rob Clarkbbb1e522015-08-25 15:35:58 -04001822/* just used from fb-helper and atomic-helper: */
1823int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1824 struct drm_atomic_state *state)
1825{
1826 struct drm_crtc_state *crtc_state;
1827 struct drm_plane_state *primary_state;
1828 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02001829 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001830 int ret;
1831
1832 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1833 if (IS_ERR(crtc_state))
1834 return PTR_ERR(crtc_state);
1835
1836 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1837 if (IS_ERR(primary_state))
1838 return PTR_ERR(primary_state);
1839
1840 if (!set->mode) {
1841 WARN_ON(set->fb);
1842 WARN_ON(set->num_connectors);
1843
1844 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1845 if (ret != 0)
1846 return ret;
1847
1848 crtc_state->active = false;
1849
1850 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1851 if (ret != 0)
1852 return ret;
1853
1854 drm_atomic_set_fb_for_plane(primary_state, NULL);
1855
1856 goto commit;
1857 }
1858
1859 WARN_ON(!set->fb);
1860 WARN_ON(!set->num_connectors);
1861
1862 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1863 if (ret != 0)
1864 return ret;
1865
1866 crtc_state->active = true;
1867
1868 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1869 if (ret != 0)
1870 return ret;
1871
Ville Syrjälä83926112015-11-16 17:02:34 +02001872 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1873
Rob Clarkbbb1e522015-08-25 15:35:58 -04001874 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1875 primary_state->crtc_x = 0;
1876 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02001877 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001878 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001879 primary_state->src_x = set->x << 16;
1880 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001881 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02001882 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001883 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001884 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02001885 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001886 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001887 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04001888
1889commit:
1890 ret = update_output_state(state, set);
1891 if (ret)
1892 return ret;
1893
1894 return 0;
1895}
1896
Daniel Vetter042652e2014-07-27 13:46:52 +02001897/**
Thierry Reding14942762015-12-02 17:50:04 +01001898 * drm_atomic_helper_disable_all - disable all currently active outputs
1899 * @dev: DRM device
1900 * @ctx: lock acquisition context
1901 *
1902 * Loops through all connectors, finding those that aren't turned off and then
1903 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1904 * that they are connected to.
1905 *
1906 * This is used for example in suspend/resume to disable all currently active
1907 * functions when suspending.
1908 *
1909 * Note that if callers haven't already acquired all modeset locks this might
1910 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1911 *
1912 * Returns:
1913 * 0 on success or a negative error code on failure.
1914 *
1915 * See also:
1916 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1917 */
1918int drm_atomic_helper_disable_all(struct drm_device *dev,
1919 struct drm_modeset_acquire_ctx *ctx)
1920{
1921 struct drm_atomic_state *state;
1922 struct drm_connector *conn;
1923 int err;
1924
1925 state = drm_atomic_state_alloc(dev);
1926 if (!state)
1927 return -ENOMEM;
1928
1929 state->acquire_ctx = ctx;
1930
1931 drm_for_each_connector(conn, dev) {
1932 struct drm_crtc *crtc = conn->state->crtc;
1933 struct drm_crtc_state *crtc_state;
1934
1935 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1936 continue;
1937
1938 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1939 if (IS_ERR(crtc_state)) {
1940 err = PTR_ERR(crtc_state);
1941 goto free;
1942 }
1943
1944 crtc_state->active = false;
1945 }
1946
1947 err = drm_atomic_commit(state);
1948
1949free:
1950 if (err < 0)
1951 drm_atomic_state_free(state);
1952
1953 return err;
1954}
1955EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1956
1957/**
1958 * drm_atomic_helper_suspend - subsystem-level suspend helper
1959 * @dev: DRM device
1960 *
1961 * Duplicates the current atomic state, disables all active outputs and then
1962 * returns a pointer to the original atomic state to the caller. Drivers can
1963 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1964 * restore the output configuration that was active at the time the system
1965 * entered suspend.
1966 *
1967 * Note that it is potentially unsafe to use this. The atomic state object
1968 * returned by this function is assumed to be persistent. Drivers must ensure
1969 * that this holds true. Before calling this function, drivers must make sure
1970 * to suspend fbdev emulation so that nothing can be using the device.
1971 *
1972 * Returns:
1973 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1974 * encoded error code on failure. Drivers should store the returned atomic
1975 * state object and pass it to the drm_atomic_helper_resume() helper upon
1976 * resume.
1977 *
1978 * See also:
1979 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1980 * drm_atomic_helper_resume()
1981 */
1982struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1983{
1984 struct drm_modeset_acquire_ctx ctx;
1985 struct drm_atomic_state *state;
1986 int err;
1987
1988 drm_modeset_acquire_init(&ctx, 0);
1989
1990retry:
1991 err = drm_modeset_lock_all_ctx(dev, &ctx);
1992 if (err < 0) {
1993 state = ERR_PTR(err);
1994 goto unlock;
1995 }
1996
1997 state = drm_atomic_helper_duplicate_state(dev, &ctx);
1998 if (IS_ERR(state))
1999 goto unlock;
2000
2001 err = drm_atomic_helper_disable_all(dev, &ctx);
2002 if (err < 0) {
2003 drm_atomic_state_free(state);
2004 state = ERR_PTR(err);
2005 goto unlock;
2006 }
2007
2008unlock:
2009 if (PTR_ERR(state) == -EDEADLK) {
2010 drm_modeset_backoff(&ctx);
2011 goto retry;
2012 }
2013
2014 drm_modeset_drop_locks(&ctx);
2015 drm_modeset_acquire_fini(&ctx);
2016 return state;
2017}
2018EXPORT_SYMBOL(drm_atomic_helper_suspend);
2019
2020/**
2021 * drm_atomic_helper_resume - subsystem-level resume helper
2022 * @dev: DRM device
2023 * @state: atomic state to resume to
2024 *
2025 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2026 * grabs all modeset locks and commits the atomic state object. This can be
2027 * used in conjunction with the drm_atomic_helper_suspend() helper to
2028 * implement suspend/resume for drivers that support atomic mode-setting.
2029 *
2030 * Returns:
2031 * 0 on success or a negative error code on failure.
2032 *
2033 * See also:
2034 * drm_atomic_helper_suspend()
2035 */
2036int drm_atomic_helper_resume(struct drm_device *dev,
2037 struct drm_atomic_state *state)
2038{
2039 struct drm_mode_config *config = &dev->mode_config;
2040 int err;
2041
2042 drm_mode_config_reset(dev);
2043 drm_modeset_lock_all(dev);
2044 state->acquire_ctx = config->acquire_ctx;
2045 err = drm_atomic_commit(state);
2046 drm_modeset_unlock_all(dev);
2047
2048 return err;
2049}
2050EXPORT_SYMBOL(drm_atomic_helper_resume);
2051
2052/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002053 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002054 * @crtc: DRM crtc
2055 * @property: DRM property
2056 * @val: value of property
2057 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002058 * Provides a default crtc set_property handler using the atomic driver
2059 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002060 *
2061 * RETURNS:
2062 * Zero on success, error code on failure
2063 */
2064int
2065drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2066 struct drm_property *property,
2067 uint64_t val)
2068{
2069 struct drm_atomic_state *state;
2070 struct drm_crtc_state *crtc_state;
2071 int ret = 0;
2072
2073 state = drm_atomic_state_alloc(crtc->dev);
2074 if (!state)
2075 return -ENOMEM;
2076
2077 /* ->set_property is always called with all locks held. */
2078 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2079retry:
2080 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2081 if (IS_ERR(crtc_state)) {
2082 ret = PTR_ERR(crtc_state);
2083 goto fail;
2084 }
2085
Rob Clark40ecc692014-12-18 16:01:46 -05002086 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2087 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002088 if (ret)
2089 goto fail;
2090
2091 ret = drm_atomic_commit(state);
2092 if (ret != 0)
2093 goto fail;
2094
2095 /* Driver takes ownership of state on successful commit. */
2096 return 0;
2097fail:
2098 if (ret == -EDEADLK)
2099 goto backoff;
2100
2101 drm_atomic_state_free(state);
2102
2103 return ret;
2104backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002105 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002106 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002107
2108 goto retry;
2109}
2110EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2111
2112/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002113 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002114 * @plane: DRM plane
2115 * @property: DRM property
2116 * @val: value of property
2117 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002118 * Provides a default plane set_property handler using the atomic driver
2119 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002120 *
2121 * RETURNS:
2122 * Zero on success, error code on failure
2123 */
2124int
2125drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2126 struct drm_property *property,
2127 uint64_t val)
2128{
2129 struct drm_atomic_state *state;
2130 struct drm_plane_state *plane_state;
2131 int ret = 0;
2132
2133 state = drm_atomic_state_alloc(plane->dev);
2134 if (!state)
2135 return -ENOMEM;
2136
2137 /* ->set_property is always called with all locks held. */
2138 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2139retry:
2140 plane_state = drm_atomic_get_plane_state(state, plane);
2141 if (IS_ERR(plane_state)) {
2142 ret = PTR_ERR(plane_state);
2143 goto fail;
2144 }
2145
Rob Clark40ecc692014-12-18 16:01:46 -05002146 ret = drm_atomic_plane_set_property(plane, plane_state,
2147 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002148 if (ret)
2149 goto fail;
2150
2151 ret = drm_atomic_commit(state);
2152 if (ret != 0)
2153 goto fail;
2154
2155 /* Driver takes ownership of state on successful commit. */
2156 return 0;
2157fail:
2158 if (ret == -EDEADLK)
2159 goto backoff;
2160
2161 drm_atomic_state_free(state);
2162
2163 return ret;
2164backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002165 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002166 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002167
2168 goto retry;
2169}
2170EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2171
2172/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002173 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002174 * @connector: DRM connector
2175 * @property: DRM property
2176 * @val: value of property
2177 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002178 * Provides a default connector set_property handler using the atomic driver
2179 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002180 *
2181 * RETURNS:
2182 * Zero on success, error code on failure
2183 */
2184int
2185drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2186 struct drm_property *property,
2187 uint64_t val)
2188{
2189 struct drm_atomic_state *state;
2190 struct drm_connector_state *connector_state;
2191 int ret = 0;
2192
2193 state = drm_atomic_state_alloc(connector->dev);
2194 if (!state)
2195 return -ENOMEM;
2196
2197 /* ->set_property is always called with all locks held. */
2198 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2199retry:
2200 connector_state = drm_atomic_get_connector_state(state, connector);
2201 if (IS_ERR(connector_state)) {
2202 ret = PTR_ERR(connector_state);
2203 goto fail;
2204 }
2205
Rob Clark40ecc692014-12-18 16:01:46 -05002206 ret = drm_atomic_connector_set_property(connector, connector_state,
2207 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002208 if (ret)
2209 goto fail;
2210
2211 ret = drm_atomic_commit(state);
2212 if (ret != 0)
2213 goto fail;
2214
2215 /* Driver takes ownership of state on successful commit. */
2216 return 0;
2217fail:
2218 if (ret == -EDEADLK)
2219 goto backoff;
2220
2221 drm_atomic_state_free(state);
2222
2223 return ret;
2224backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002225 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002226 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002227
2228 goto retry;
2229}
2230EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002231
2232/**
2233 * drm_atomic_helper_page_flip - execute a legacy page flip
2234 * @crtc: DRM crtc
2235 * @fb: DRM framebuffer
2236 * @event: optional DRM event to signal upon completion
2237 * @flags: flip flags for non-vblank sync'ed updates
2238 *
2239 * Provides a default page flip implementation using the atomic driver interface.
2240 *
2241 * Note that for now so called async page flips (i.e. updates which are not
2242 * synchronized to vblank) are not supported, since the atomic interfaces have
2243 * no provisions for this yet.
2244 *
2245 * Returns:
2246 * Returns 0 on success, negative errno numbers on failure.
2247 */
2248int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2249 struct drm_framebuffer *fb,
2250 struct drm_pending_vblank_event *event,
2251 uint32_t flags)
2252{
2253 struct drm_plane *plane = crtc->primary;
2254 struct drm_atomic_state *state;
2255 struct drm_plane_state *plane_state;
2256 struct drm_crtc_state *crtc_state;
2257 int ret = 0;
2258
2259 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2260 return -EINVAL;
2261
2262 state = drm_atomic_state_alloc(plane->dev);
2263 if (!state)
2264 return -ENOMEM;
2265
2266 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2267retry:
2268 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2269 if (IS_ERR(crtc_state)) {
2270 ret = PTR_ERR(crtc_state);
2271 goto fail;
2272 }
2273 crtc_state->event = event;
2274
2275 plane_state = drm_atomic_get_plane_state(state, plane);
2276 if (IS_ERR(plane_state)) {
2277 ret = PTR_ERR(plane_state);
2278 goto fail;
2279 }
2280
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002281 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002282 if (ret != 0)
2283 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002284 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002285
2286 ret = drm_atomic_async_commit(state);
2287 if (ret != 0)
2288 goto fail;
2289
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002290 /* Driver takes ownership of state on successful async commit. */
2291 return 0;
2292fail:
2293 if (ret == -EDEADLK)
2294 goto backoff;
2295
2296 drm_atomic_state_free(state);
2297
2298 return ret;
2299backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002300 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002301 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002302
2303 /*
2304 * Someone might have exchanged the framebuffer while we dropped locks
2305 * in the backoff code. We need to fix up the fb refcount tracking the
2306 * core does for us.
2307 */
2308 plane->old_fb = plane->fb;
2309
2310 goto retry;
2311}
2312EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002313
2314/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002315 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2316 * @connector: affected connector
2317 * @mode: DPMS mode
2318 *
2319 * This is the main helper function provided by the atomic helper framework for
2320 * implementing the legacy DPMS connector interface. It computes the new desired
2321 * ->active state for the corresponding CRTC (if the connector is enabled) and
2322 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002323 *
2324 * Returns:
2325 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002326 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002327int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2328 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002329{
2330 struct drm_mode_config *config = &connector->dev->mode_config;
2331 struct drm_atomic_state *state;
2332 struct drm_crtc_state *crtc_state;
2333 struct drm_crtc *crtc;
2334 struct drm_connector *tmp_connector;
2335 int ret;
2336 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002337 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002338
2339 if (mode != DRM_MODE_DPMS_ON)
2340 mode = DRM_MODE_DPMS_OFF;
2341
2342 connector->dpms = mode;
2343 crtc = connector->state->crtc;
2344
2345 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002346 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002347
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002348 state = drm_atomic_state_alloc(connector->dev);
2349 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002350 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002351
2352 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2353retry:
2354 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002355 if (IS_ERR(crtc_state)) {
2356 ret = PTR_ERR(crtc_state);
2357 goto fail;
2358 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002359
2360 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2361
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002362 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002363 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002364 continue;
2365
John Hunter0388df02015-03-17 15:30:28 +08002366 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002367 active = true;
2368 break;
2369 }
2370 }
2371 crtc_state->active = active;
2372
2373 ret = drm_atomic_commit(state);
2374 if (ret != 0)
2375 goto fail;
2376
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002377 /* Driver takes ownership of state on successful commit. */
2378 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002379fail:
2380 if (ret == -EDEADLK)
2381 goto backoff;
2382
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002383 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002384 drm_atomic_state_free(state);
2385
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002386 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002387backoff:
2388 drm_atomic_state_clear(state);
2389 drm_atomic_legacy_backoff(state);
2390
2391 goto retry;
2392}
2393EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2394
2395/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002396 * DOC: atomic state reset and initialization
2397 *
2398 * Both the drm core and the atomic helpers assume that there is always the full
2399 * and correct atomic software state for all connectors, CRTCs and planes
2400 * available. Which is a bit a problem on driver load and also after system
2401 * suspend. One way to solve this is to have a hardware state read-out
2402 * infrastructure which reconstructs the full software state (e.g. the i915
2403 * driver).
2404 *
2405 * The simpler solution is to just reset the software state to everything off,
2406 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2407 * the atomic helpers provide default reset implementations for all hooks.
2408 */
2409
2410/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002411 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2412 * @crtc: drm CRTC
2413 *
2414 * Resets the atomic state for @crtc by freeing the state pointer (which might
2415 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2416 */
2417void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2418{
Markus Elfring5f911902015-11-06 12:03:46 +01002419 if (crtc->state)
Daniel Stone99cf4a22015-05-25 19:11:51 +01002420 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002421 kfree(crtc->state);
2422 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002423
2424 if (crtc->state)
2425 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002426}
2427EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2428
2429/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002430 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2431 * @crtc: CRTC object
2432 * @state: atomic CRTC state
2433 *
2434 * Copies atomic state from a CRTC's current state and resets inferred values.
2435 * This is useful for drivers that subclass the CRTC state.
2436 */
2437void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2438 struct drm_crtc_state *state)
2439{
2440 memcpy(state, crtc->state, sizeof(*state));
2441
Daniel Stone99cf4a22015-05-25 19:11:51 +01002442 if (state->mode_blob)
2443 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002444 state->mode_changed = false;
2445 state->active_changed = false;
2446 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002447 state->connectors_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002448 state->event = NULL;
2449}
2450EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2451
2452/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002453 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2454 * @crtc: drm CRTC
2455 *
2456 * Default CRTC state duplicate hook for drivers which don't have their own
2457 * subclassed CRTC state structure.
2458 */
2459struct drm_crtc_state *
2460drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2461{
2462 struct drm_crtc_state *state;
2463
2464 if (WARN_ON(!crtc->state))
2465 return NULL;
2466
Thierry Redingf5e78402015-01-28 14:54:32 +01002467 state = kmalloc(sizeof(*state), GFP_KERNEL);
2468 if (state)
2469 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002470
2471 return state;
2472}
2473EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2474
2475/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002476 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2477 * @crtc: CRTC object
2478 * @state: CRTC state object to release
2479 *
2480 * Releases all resources stored in the CRTC state without actually freeing
2481 * the memory of the CRTC state. This is useful for drivers that subclass the
2482 * CRTC state.
2483 */
2484void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2485 struct drm_crtc_state *state)
2486{
Markus Elfring5f911902015-11-06 12:03:46 +01002487 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002488}
2489EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2490
2491/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002492 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2493 * @crtc: drm CRTC
2494 * @state: CRTC state object to release
2495 *
2496 * Default CRTC state destroy hook for drivers which don't have their own
2497 * subclassed CRTC state structure.
2498 */
2499void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2500 struct drm_crtc_state *state)
2501{
Thierry Redingf5e78402015-01-28 14:54:32 +01002502 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002503 kfree(state);
2504}
2505EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2506
2507/**
2508 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2509 * @plane: drm plane
2510 *
2511 * Resets the atomic state for @plane by freeing the state pointer (which might
2512 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2513 */
2514void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2515{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002516 if (plane->state && plane->state->fb)
2517 drm_framebuffer_unreference(plane->state->fb);
2518
Daniel Vetterd4617012014-11-03 15:56:43 +01002519 kfree(plane->state);
2520 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002521
2522 if (plane->state)
2523 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01002524}
2525EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2526
2527/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002528 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2529 * @plane: plane object
2530 * @state: atomic plane state
2531 *
2532 * Copies atomic state from a plane's current state. This is useful for
2533 * drivers that subclass the plane state.
2534 */
2535void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2536 struct drm_plane_state *state)
2537{
2538 memcpy(state, plane->state, sizeof(*state));
2539
2540 if (state->fb)
2541 drm_framebuffer_reference(state->fb);
2542}
2543EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2544
2545/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002546 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2547 * @plane: drm plane
2548 *
2549 * Default plane state duplicate hook for drivers which don't have their own
2550 * subclassed plane state structure.
2551 */
2552struct drm_plane_state *
2553drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2554{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002555 struct drm_plane_state *state;
2556
Daniel Vetterd4617012014-11-03 15:56:43 +01002557 if (WARN_ON(!plane->state))
2558 return NULL;
2559
Thierry Redingf5e78402015-01-28 14:54:32 +01002560 state = kmalloc(sizeof(*state), GFP_KERNEL);
2561 if (state)
2562 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002563
2564 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002565}
2566EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2567
2568/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002569 * __drm_atomic_helper_plane_destroy_state - release plane state
2570 * @plane: plane object
2571 * @state: plane state object to release
2572 *
2573 * Releases all resources stored in the plane state without actually freeing
2574 * the memory of the plane state. This is useful for drivers that subclass the
2575 * plane state.
2576 */
2577void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2578 struct drm_plane_state *state)
2579{
2580 if (state->fb)
2581 drm_framebuffer_unreference(state->fb);
2582}
2583EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2584
2585/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002586 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2587 * @plane: drm plane
2588 * @state: plane state object to release
2589 *
2590 * Default plane state destroy hook for drivers which don't have their own
2591 * subclassed plane state structure.
2592 */
2593void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002594 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002595{
Thierry Redingf5e78402015-01-28 14:54:32 +01002596 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002597 kfree(state);
2598}
2599EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2600
2601/**
2602 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2603 * @connector: drm connector
2604 *
2605 * Resets the atomic state for @connector by freeing the state pointer (which
2606 * might be NULL, e.g. at driver load time) and allocating a new empty state
2607 * object.
2608 */
2609void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2610{
2611 kfree(connector->state);
2612 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002613
2614 if (connector->state)
2615 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002616}
2617EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2618
2619/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002620 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2621 * @connector: connector object
2622 * @state: atomic connector state
2623 *
2624 * Copies atomic state from a connector's current state. This is useful for
2625 * drivers that subclass the connector state.
2626 */
2627void
2628__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2629 struct drm_connector_state *state)
2630{
2631 memcpy(state, connector->state, sizeof(*state));
2632}
2633EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2634
2635/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002636 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2637 * @connector: drm connector
2638 *
2639 * Default connector state duplicate hook for drivers which don't have their own
2640 * subclassed connector state structure.
2641 */
2642struct drm_connector_state *
2643drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2644{
Thierry Redingf5e78402015-01-28 14:54:32 +01002645 struct drm_connector_state *state;
2646
Daniel Vetterd4617012014-11-03 15:56:43 +01002647 if (WARN_ON(!connector->state))
2648 return NULL;
2649
Thierry Redingf5e78402015-01-28 14:54:32 +01002650 state = kmalloc(sizeof(*state), GFP_KERNEL);
2651 if (state)
2652 __drm_atomic_helper_connector_duplicate_state(connector, state);
2653
2654 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002655}
2656EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2657
2658/**
Thierry Reding397fd772015-09-08 15:00:45 +02002659 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2660 * @dev: DRM device
2661 * @ctx: lock acquisition context
2662 *
2663 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01002664 * duplicating their respective states. This is used for example by suspend/
2665 * resume support code to save the state prior to suspend such that it can
2666 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02002667 *
2668 * Note that this treats atomic state as persistent between save and restore.
2669 * Drivers must make sure that this is possible and won't result in confusion
2670 * or erroneous behaviour.
2671 *
2672 * Note that if callers haven't already acquired all modeset locks this might
2673 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2674 *
2675 * Returns:
2676 * A pointer to the copy of the atomic state object on success or an
2677 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01002678 *
2679 * See also:
2680 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02002681 */
2682struct drm_atomic_state *
2683drm_atomic_helper_duplicate_state(struct drm_device *dev,
2684 struct drm_modeset_acquire_ctx *ctx)
2685{
2686 struct drm_atomic_state *state;
2687 struct drm_connector *conn;
2688 struct drm_plane *plane;
2689 struct drm_crtc *crtc;
2690 int err = 0;
2691
2692 state = drm_atomic_state_alloc(dev);
2693 if (!state)
2694 return ERR_PTR(-ENOMEM);
2695
2696 state->acquire_ctx = ctx;
2697
2698 drm_for_each_crtc(crtc, dev) {
2699 struct drm_crtc_state *crtc_state;
2700
2701 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2702 if (IS_ERR(crtc_state)) {
2703 err = PTR_ERR(crtc_state);
2704 goto free;
2705 }
2706 }
2707
2708 drm_for_each_plane(plane, dev) {
2709 struct drm_plane_state *plane_state;
2710
2711 plane_state = drm_atomic_get_plane_state(state, plane);
2712 if (IS_ERR(plane_state)) {
2713 err = PTR_ERR(plane_state);
2714 goto free;
2715 }
2716 }
2717
2718 drm_for_each_connector(conn, dev) {
2719 struct drm_connector_state *conn_state;
2720
2721 conn_state = drm_atomic_get_connector_state(state, conn);
2722 if (IS_ERR(conn_state)) {
2723 err = PTR_ERR(conn_state);
2724 goto free;
2725 }
2726 }
2727
2728 /* clear the acquire context so that it isn't accidentally reused */
2729 state->acquire_ctx = NULL;
2730
2731free:
2732 if (err < 0) {
2733 drm_atomic_state_free(state);
2734 state = ERR_PTR(err);
2735 }
2736
2737 return state;
2738}
2739EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2740
2741/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002742 * __drm_atomic_helper_connector_destroy_state - release connector state
2743 * @connector: connector object
2744 * @state: connector state object to release
2745 *
2746 * Releases all resources stored in the connector state without actually
2747 * freeing the memory of the connector state. This is useful for drivers that
2748 * subclass the connector state.
2749 */
2750void
2751__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2752 struct drm_connector_state *state)
2753{
2754 /*
2755 * This is currently a placeholder so that drivers that subclass the
2756 * state will automatically do the right thing if code is ever added
2757 * to this function.
2758 */
2759}
2760EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2761
2762/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002763 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2764 * @connector: drm connector
2765 * @state: connector state object to release
2766 *
2767 * Default connector state destroy hook for drivers which don't have their own
2768 * subclassed connector state structure.
2769 */
2770void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2771 struct drm_connector_state *state)
2772{
Thierry Redingf5e78402015-01-28 14:54:32 +01002773 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002774 kfree(state);
2775}
2776EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);