blob: 63f925b75357df20a0c7e855226f527f8b84c6c2 [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
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200146 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100147 encoder->base.id, encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200148 encoder_crtc->base.id, encoder_crtc->name);
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äb5ceff22015-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 Vetter3b8a684b2015-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) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200249 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100250 connector->base.id,
251 connector->name,
252 new_encoder->base.id,
253 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200254 connector_state->crtc->base.id,
255 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200256
257 return 0;
258 }
259
Daniel Vetter97ac3202015-12-03 10:49:14 +0100260 if (!check_pending_encoder_assignment(state, new_encoder, connector)) {
261 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
262 connector->base.id,
263 connector->name);
264 return -EINVAL;
265 }
266
Daniel Vetter623369e2014-09-16 17:50:47 +0200267 encoder_crtc = get_current_crtc_for_encoder(state->dev,
268 new_encoder);
269
270 if (encoder_crtc) {
271 ret = steal_encoder(state, new_encoder, encoder_crtc);
272 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100273 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
274 connector->base.id,
275 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200276 return ret;
277 }
278 }
279
Daniel Vetter6ea76f3c2015-08-03 17:24:11 +0200280 if (WARN_ON(!connector_state->crtc))
281 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200282
Daniel Vetter623369e2014-09-16 17:50:47 +0200283 connector_state->best_encoder = new_encoder;
284 idx = drm_crtc_index(connector_state->crtc);
285
286 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200287 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200288
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200289 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100290 connector->base.id,
291 connector->name,
292 new_encoder->base.id,
293 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200294 connector_state->crtc->base.id,
295 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200296
297 return 0;
298}
299
300static int
301mode_fixup(struct drm_atomic_state *state)
302{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300303 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200304 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300305 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200306 struct drm_connector_state *conn_state;
307 int i;
308 bool ret;
309
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300310 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200311 if (!crtc_state->mode_changed &&
312 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200313 continue;
314
315 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
316 }
317
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300318 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200319 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200320 struct drm_encoder *encoder;
321
Daniel Vetter623369e2014-09-16 17:50:47 +0200322 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
323
324 if (!conn_state->crtc || !conn_state->best_encoder)
325 continue;
326
327 crtc_state =
328 state->crtc_states[drm_crtc_index(conn_state->crtc)];
329
330 /*
331 * Each encoder has at most one connector (since we always steal
332 * it away), so we won't call ->mode_fixup twice.
333 */
334 encoder = conn_state->best_encoder;
335 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300336 if (!funcs)
337 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200338
Archit Taneja862e6862015-05-21 11:03:16 +0530339 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
340 &crtc_state->adjusted_mode);
341 if (!ret) {
342 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
343 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200344 }
345
Thierry Reding4cd4df82014-12-03 16:44:34 +0100346 if (funcs->atomic_check) {
347 ret = funcs->atomic_check(encoder, crtc_state,
348 conn_state);
349 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100350 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
351 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100352 return ret;
353 }
Inki Dae84524912015-08-11 21:23:49 +0900354 } else if (funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100355 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
356 &crtc_state->adjusted_mode);
357 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100358 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
359 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100360 return -EINVAL;
361 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200362 }
363 }
364
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300365 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200366 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200367
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200368 if (!crtc_state->mode_changed &&
369 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200370 continue;
371
372 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300373 if (!funcs->mode_fixup)
374 continue;
375
Daniel Vetter623369e2014-09-16 17:50:47 +0200376 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
377 &crtc_state->adjusted_mode);
378 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200379 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
380 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200381 return -EINVAL;
382 }
383 }
384
385 return 0;
386}
387
Daniel Vetterd9b13622014-11-26 16:57:41 +0100388/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800389 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100390 * @dev: DRM device
391 * @state: the driver state object
392 *
393 * Check the state object to see if the requested state is physically possible.
394 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200395 * update and adds any additional connectors needed for full modesets and calls
396 * down into ->mode_fixup functions of the driver backend.
397 *
398 * crtc_state->mode_changed is set when the input mode is changed.
399 * crtc_state->connectors_changed is set when a connector is added or
400 * removed from the crtc.
401 * crtc_state->active_changed is set when crtc_state->active changes,
402 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100403 *
404 * IMPORTANT:
405 *
406 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
407 * plane update can't be done without a full modeset) _must_ call this function
408 * afterwards after that change. It is permitted to call this function multiple
409 * times for the same update, e.g. when the ->atomic_check functions depend upon
410 * the adjusted dotclock for fifo space allocation and watermark computation.
411 *
412 * RETURNS
413 * Zero for success or -errno
414 */
415int
Rob Clark934ce1c2014-11-19 16:41:33 -0500416drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200417 struct drm_atomic_state *state)
418{
Daniel Vetter623369e2014-09-16 17:50:47 +0200419 struct drm_crtc *crtc;
420 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300421 struct drm_connector *connector;
422 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200423 int i, ret;
424
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300425 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200426 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200427 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
428 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200429 crtc_state->mode_changed = true;
430 }
431
432 if (crtc->state->enable != crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200433 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
434 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200435
436 /*
437 * For clarity this assignment is done here, but
438 * enable == 0 is only true when there are no
439 * connectors and a NULL mode.
440 *
441 * The other way around is true as well. enable != 0
442 * iff connectors are attached and a mode is set.
443 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200444 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200445 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200446 }
447 }
448
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300449 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200450 /*
451 * This only sets crtc->mode_changed for routing changes,
452 * drivers must set crtc->mode_changed themselves when connector
453 * properties need to be updated.
454 */
455 ret = update_connector_routing(state, i);
456 if (ret)
457 return ret;
458 }
459
460 /*
461 * After all the routing has been prepared we need to add in any
462 * connector which is itself unchanged, but who's crtc changes it's
463 * configuration. This must be done before calling mode_fixup in case a
464 * crtc only changed its mode but has the same set of connectors.
465 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300466 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200467 int num_connectors;
468
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100469 /*
470 * We must set ->active_changed after walking connectors for
471 * otherwise an update that only changes active would result in
472 * a full modeset because update_connector_routing force that.
473 */
474 if (crtc->state->active != crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200475 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
476 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100477 crtc_state->active_changed = true;
478 }
479
Daniel Vetter2465ff62015-06-18 09:58:55 +0200480 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100481 continue;
482
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200483 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
484 crtc->base.id, crtc->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100485 crtc_state->enable ? 'y' : 'n',
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200486 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200487
488 ret = drm_atomic_add_affected_connectors(state, crtc);
489 if (ret != 0)
490 return ret;
491
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200492 ret = drm_atomic_add_affected_planes(state, crtc);
493 if (ret != 0)
494 return ret;
495
Daniel Vetter623369e2014-09-16 17:50:47 +0200496 num_connectors = drm_atomic_connectors_for_crtc(state,
497 crtc);
498
499 if (crtc_state->enable != !!num_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200500 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
501 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200502
503 return -EINVAL;
504 }
505 }
506
507 return mode_fixup(state);
508}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100509EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200510
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100511/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800512 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100513 * @dev: DRM device
514 * @state: the driver state object
515 *
516 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100517 * This does all the plane update related checks using by calling into the
518 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100519 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200520 * It also sets crtc_state->planes_changed to indicate that a crtc has
521 * updated planes.
522 *
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100523 * RETURNS
524 * Zero for success or -errno
525 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100526int
527drm_atomic_helper_check_planes(struct drm_device *dev,
528 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100529{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300530 struct drm_crtc *crtc;
531 struct drm_crtc_state *crtc_state;
532 struct drm_plane *plane;
533 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100534 int i, ret = 0;
535
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300536 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200537 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100538
539 funcs = plane->helper_private;
540
541 drm_atomic_helper_plane_changed(state, plane_state, plane);
542
543 if (!funcs || !funcs->atomic_check)
544 continue;
545
546 ret = funcs->atomic_check(plane, plane_state);
547 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200548 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
549 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100550 return ret;
551 }
552 }
553
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300554 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200555 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100556
557 funcs = crtc->helper_private;
558
559 if (!funcs || !funcs->atomic_check)
560 continue;
561
562 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
563 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200564 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
565 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100566 return ret;
567 }
568 }
569
Daniel Vetterd9b13622014-11-26 16:57:41 +0100570 return ret;
571}
572EXPORT_SYMBOL(drm_atomic_helper_check_planes);
573
574/**
575 * drm_atomic_helper_check - validate state object
576 * @dev: DRM device
577 * @state: the driver state object
578 *
579 * Check the state object to see if the requested state is physically possible.
580 * Only crtcs and planes have check callbacks, so for any additional (global)
581 * checking that a driver needs it can simply wrap that around this function.
582 * Drivers without such needs can directly use this as their ->atomic_check()
583 * callback.
584 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100585 * This just wraps the two parts of the state checking for planes and modeset
586 * state in the default order: First it calls drm_atomic_helper_check_modeset()
587 * and then drm_atomic_helper_check_planes(). The assumption is that the
588 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
589 * e.g. properly compute watermarks.
590 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100591 * RETURNS
592 * Zero for success or -errno
593 */
594int drm_atomic_helper_check(struct drm_device *dev,
595 struct drm_atomic_state *state)
596{
597 int ret;
598
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100599 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100600 if (ret)
601 return ret;
602
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100603 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500604 if (ret)
605 return ret;
606
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100607 return ret;
608}
609EXPORT_SYMBOL(drm_atomic_helper_check);
610
Daniel Vetter623369e2014-09-16 17:50:47 +0200611static void
612disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
613{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300614 struct drm_connector *connector;
615 struct drm_connector_state *old_conn_state;
616 struct drm_crtc *crtc;
617 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200618 int i;
619
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300620 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200621 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200622 struct drm_encoder *encoder;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100623 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200624
Daniel Vetter623369e2014-09-16 17:50:47 +0200625 /* Shut down everything that's in the changeset and currently
626 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300627 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200628 continue;
629
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100630 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
631
Daniel Vetter4218a322015-03-26 22:18:40 +0100632 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200633 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100634 continue;
635
Rob Clark46df9ad2014-11-20 15:40:36 -0500636 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200637
Rob Clark46df9ad2014-11-20 15:40:36 -0500638 /* We shouldn't get this far if we didn't previously have
639 * an encoder.. but WARN_ON() rather than explode.
640 */
641 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200642 continue;
643
644 funcs = encoder->helper_private;
645
Daniel Vetter17a38d92015-02-22 12:24:16 +0100646 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
647 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100648
Daniel Vetter623369e2014-09-16 17:50:47 +0200649 /*
650 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800651 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200652 */
Archit Taneja862e6862015-05-21 11:03:16 +0530653 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200654
655 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100656 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200657 funcs->prepare(encoder);
658 else if (funcs->disable)
659 funcs->disable(encoder);
660 else
661 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
662
Archit Taneja862e6862015-05-21 11:03:16 +0530663 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200664 }
665
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300666 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200667 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200668
669 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200670 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100671 continue;
672
673 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200674 continue;
675
676 funcs = crtc->helper_private;
677
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200678 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
679 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100680
681
Daniel Vetter623369e2014-09-16 17:50:47 +0200682 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100683 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200684 funcs->prepare(crtc);
685 else if (funcs->disable)
686 funcs->disable(crtc);
687 else
688 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
689 }
690}
691
Daniel Vetter4c18d302015-05-12 15:27:37 +0200692/**
693 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
694 * @dev: DRM device
695 * @old_state: atomic state object with old state structures
696 *
697 * This function updates all the various legacy modeset state pointers in
698 * connectors, encoders and crtcs. It also updates the timestamping constants
699 * used for precise vblank timestamps by calling
700 * drm_calc_timestamping_constants().
701 *
702 * Drivers can use this for building their own atomic commit if they don't have
703 * a pure helper-based modeset implementation.
704 */
705void
706drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
707 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200708{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300709 struct drm_connector *connector;
710 struct drm_connector_state *old_conn_state;
711 struct drm_crtc *crtc;
712 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200713 int i;
714
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200715 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300716 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200717 if (connector->encoder) {
718 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200719
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200720 connector->encoder->crtc = NULL;
721 connector->encoder = NULL;
722 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200723
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200724 crtc = connector->state->crtc;
725 if ((!crtc && old_conn_state->crtc) ||
726 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
727 struct drm_property *dpms_prop =
728 dev->mode_config.dpms_property;
729 int mode = DRM_MODE_DPMS_OFF;
730
731 if (crtc && crtc->state->active)
732 mode = DRM_MODE_DPMS_ON;
733
734 connector->dpms = mode;
735 drm_object_property_set_value(&connector->base,
736 dpms_prop, mode);
737 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200738 }
739
740 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300741 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
742 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200743 continue;
744
745 if (WARN_ON(!connector->state->best_encoder))
746 continue;
747
748 connector->encoder = connector->state->best_encoder;
749 connector->encoder->crtc = connector->state->crtc;
750 }
751
752 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300753 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200754 struct drm_plane *primary = crtc->primary;
755
Daniel Vetter623369e2014-09-16 17:50:47 +0200756 crtc->mode = crtc->state->mode;
757 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200758
759 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
760 primary->state->crtc == crtc) {
761 crtc->x = primary->state->src_x >> 16;
762 crtc->y = primary->state->src_y >> 16;
763 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200764
765 if (crtc->state->enable)
766 drm_calc_timestamping_constants(crtc,
767 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200768 }
769}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200770EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200771
772static void
773crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
774{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300775 struct drm_crtc *crtc;
776 struct drm_crtc_state *old_crtc_state;
777 struct drm_connector *connector;
778 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200779 int i;
780
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300781 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200782 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200783
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300784 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200785 continue;
786
787 funcs = crtc->helper_private;
788
Daniel Vetterc982bd92015-02-22 12:24:20 +0100789 if (crtc->state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200790 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
791 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100792
Daniel Vetter623369e2014-09-16 17:50:47 +0200793 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100794 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200795 }
796
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300797 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200798 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200799 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200800 struct drm_encoder *encoder;
801 struct drm_display_mode *mode, *adjusted_mode;
802
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300803 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200804 continue;
805
806 encoder = connector->state->best_encoder;
807 funcs = encoder->helper_private;
808 new_crtc_state = connector->state->crtc->state;
809 mode = &new_crtc_state->mode;
810 adjusted_mode = &new_crtc_state->adjusted_mode;
811
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100812 if (!new_crtc_state->mode_changed)
813 continue;
814
Daniel Vetter17a38d92015-02-22 12:24:16 +0100815 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
816 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100817
Daniel Vetter623369e2014-09-16 17:50:47 +0200818 /*
819 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800820 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200821 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100822 if (funcs->mode_set)
823 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200824
Archit Taneja862e6862015-05-21 11:03:16 +0530825 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200826 }
827}
828
829/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100830 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200831 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100832 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200833 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100834 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200835 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100836 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300837 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100838 * drm_atomic_helper_commit_planes(), which is what the default commit function
839 * does. But drivers with different needs can group the modeset commits together
840 * and do the plane commits at the end. This is useful for drivers doing runtime
841 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200842 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100843void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
844 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200845{
Laurent Pincharta072f802015-02-22 12:24:18 +0100846 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200847
848 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
849
Laurent Pincharta072f802015-02-22 12:24:18 +0100850 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200851}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100852EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200853
854/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100855 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200856 * @dev: DRM device
857 * @old_state: atomic state object with old state structures
858 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100859 * This function enables all the outputs with the new configuration which had to
860 * be turned off for the update.
861 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300862 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100863 * drm_atomic_helper_commit_planes(), which is what the default commit function
864 * does. But drivers with different needs can group the modeset commits together
865 * and do the plane commits at the end. This is useful for drivers doing runtime
866 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200867 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100868void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
869 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200870{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300871 struct drm_crtc *crtc;
872 struct drm_crtc_state *old_crtc_state;
873 struct drm_connector *connector;
874 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200875 int i;
876
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300877 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200878 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200879
880 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200881 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100882 continue;
883
884 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200885 continue;
886
887 funcs = crtc->helper_private;
888
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100889 if (crtc->state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200890 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
891 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100892
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100893 if (funcs->enable)
894 funcs->enable(crtc);
895 else
896 funcs->commit(crtc);
897 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200898 }
899
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300900 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200901 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200902 struct drm_encoder *encoder;
903
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300904 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200905 continue;
906
Daniel Vetter4218a322015-03-26 22:18:40 +0100907 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200908 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100909 continue;
910
Daniel Vetter623369e2014-09-16 17:50:47 +0200911 encoder = connector->state->best_encoder;
912 funcs = encoder->helper_private;
913
Daniel Vetter17a38d92015-02-22 12:24:16 +0100914 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
915 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100916
Daniel Vetter623369e2014-09-16 17:50:47 +0200917 /*
918 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800919 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200920 */
Archit Taneja862e6862015-05-21 11:03:16 +0530921 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200922
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100923 if (funcs->enable)
924 funcs->enable(encoder);
925 else
926 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200927
Archit Taneja862e6862015-05-21 11:03:16 +0530928 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200929 }
930}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100931EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200932
Daniel Vettere2330f02014-10-29 11:34:56 +0100933static void wait_for_fences(struct drm_device *dev,
934 struct drm_atomic_state *state)
935{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300936 struct drm_plane *plane;
937 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +0100938 int i;
939
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300940 for_each_plane_in_state(state, plane, plane_state, i) {
941 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +0100942 continue;
943
944 WARN_ON(!plane->state->fb);
945
946 fence_wait(plane->state->fence, false);
947 fence_put(plane->state->fence);
948 plane->state->fence = NULL;
949 }
950}
951
Daniel Vetterab58e332014-11-24 20:42:42 +0100952static bool framebuffer_changed(struct drm_device *dev,
953 struct drm_atomic_state *old_state,
954 struct drm_crtc *crtc)
955{
956 struct drm_plane *plane;
957 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +0100958 int i;
959
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300960 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +0100961 if (plane->state->crtc != crtc &&
962 old_plane_state->crtc != crtc)
963 continue;
964
965 if (plane->state->fb != old_plane_state->fb)
966 return true;
967 }
968
969 return false;
970}
971
Rob Clark5ee32292014-11-11 19:38:59 -0500972/**
973 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
974 * @dev: DRM device
975 * @old_state: atomic state object with old state structures
976 *
977 * Helper to, after atomic commit, wait for vblanks on all effected
978 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100979 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
980 * framebuffers have actually changed to optimize for the legacy cursor and
981 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500982 */
983void
984drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
985 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200986{
987 struct drm_crtc *crtc;
988 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200989 int i, ret;
990
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300991 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200992 /* No one cares about the old state, so abuse it for tracking
993 * and store whether we hold a vblank reference (and should do a
994 * vblank wait) in the ->enable boolean. */
995 old_crtc_state->enable = false;
996
997 if (!crtc->state->enable)
998 continue;
999
Daniel Vetterf02ad902015-01-22 16:36:23 +01001000 /* Legacy cursor ioctls are completely unsynced, and userspace
1001 * relies on that (by doing tons of cursor updates). */
1002 if (old_state->legacy_cursor_update)
1003 continue;
1004
Daniel Vetterab58e332014-11-24 20:42:42 +01001005 if (!framebuffer_changed(dev, old_state, crtc))
1006 continue;
1007
Daniel Vetter623369e2014-09-16 17:50:47 +02001008 ret = drm_crtc_vblank_get(crtc);
1009 if (ret != 0)
1010 continue;
1011
1012 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +02001013 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001014 }
1015
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001016 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1017 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +02001018 continue;
1019
1020 ret = wait_event_timeout(dev->vblank[i].queue,
1021 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001022 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001023 msecs_to_jiffies(50));
1024
1025 drm_crtc_vblank_put(crtc);
1026 }
1027}
Rob Clark5ee32292014-11-11 19:38:59 -05001028EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001029
1030/**
1031 * drm_atomic_helper_commit - commit validated state object
1032 * @dev: DRM device
1033 * @state: the driver state object
1034 * @async: asynchronous commit
1035 *
1036 * This function commits a with drm_atomic_helper_check() pre-validated state
1037 * object. This can still fail when e.g. the framebuffer reservation fails. For
1038 * now this doesn't implement asynchronous commits.
1039 *
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001040 * Note that right now this function does not support async commits, and hence
1041 * driver writers must implement their own version for now. Also note that the
1042 * default ordering of how the various stages are called is to match the legacy
1043 * modeset helper library closest. One peculiarity of that is that it doesn't
1044 * mesh well with runtime PM at all.
1045 *
1046 * For drivers supporting runtime PM the recommended sequence is
1047 *
1048 * drm_atomic_helper_commit_modeset_disables(dev, state);
1049 *
1050 * drm_atomic_helper_commit_modeset_enables(dev, state);
1051 *
1052 * drm_atomic_helper_commit_planes(dev, state, true);
1053 *
1054 * See the kerneldoc entries for these three functions for more details.
1055 *
Daniel Vetter623369e2014-09-16 17:50:47 +02001056 * RETURNS
1057 * Zero for success or -errno.
1058 */
1059int drm_atomic_helper_commit(struct drm_device *dev,
1060 struct drm_atomic_state *state,
1061 bool async)
1062{
1063 int ret;
1064
1065 if (async)
1066 return -EBUSY;
1067
1068 ret = drm_atomic_helper_prepare_planes(dev, state);
1069 if (ret)
1070 return ret;
1071
1072 /*
1073 * This is the point of no return - everything below never fails except
1074 * when the hw goes bonghits. Which means we can commit the new state on
1075 * the software side now.
1076 */
1077
1078 drm_atomic_helper_swap_state(dev, state);
1079
1080 /*
1081 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001082 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001083 * that the asynchronous work has either been cancelled (if the driver
1084 * supports it, which at least requires that the framebuffers get
1085 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1086 * before the new state gets committed on the software side with
1087 * drm_atomic_helper_swap_state().
1088 *
1089 * This scheme allows new atomic state updates to be prepared and
1090 * checked in parallel to the asynchronous completion of the previous
1091 * update. Which is important since compositors need to figure out the
1092 * composition of the next frame right after having submitted the
1093 * current layout.
1094 */
1095
Daniel Vettere2330f02014-10-29 11:34:56 +01001096 wait_for_fences(dev, state);
1097
Daniel Vetter1af434a2015-02-22 12:24:19 +01001098 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001099
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001100 drm_atomic_helper_commit_planes(dev, state, false);
Daniel Vetter623369e2014-09-16 17:50:47 +02001101
Daniel Vetter1af434a2015-02-22 12:24:19 +01001102 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001103
Rob Clark5ee32292014-11-11 19:38:59 -05001104 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001105
1106 drm_atomic_helper_cleanup_planes(dev, state);
1107
1108 drm_atomic_state_free(state);
1109
1110 return 0;
1111}
1112EXPORT_SYMBOL(drm_atomic_helper_commit);
1113
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001114/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001115 * DOC: implementing async commit
1116 *
1117 * For now the atomic helpers don't support async commit directly. If there is
1118 * real need it could be added though, using the dma-buf fence infrastructure
1119 * for generic synchronization with outstanding rendering.
1120 *
1121 * For now drivers have to implement async commit themselves, with the following
1122 * sequence being the recommended one:
1123 *
1124 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1125 * which commit needs to call which can fail, so we want to run it first and
1126 * synchronously.
1127 *
1128 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1129 * might be affected the new state update. This can be done by either cancelling
1130 * or flushing the work items, depending upon whether the driver can deal with
1131 * cancelled updates. Note that it is important to ensure that the framebuffer
1132 * cleanup is still done when cancelling.
1133 *
1134 * For sufficient parallelism it is recommended to have a work item per crtc
1135 * (for updates which don't touch global state) and a global one. Then we only
1136 * need to synchronize with the crtc work items for changed crtcs and the global
1137 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1138 *
1139 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001140 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001141 * locks means concurrent callers never see inconsistent state. And doing this
1142 * while it's guaranteed that no relevant async worker runs means that async
1143 * workers do not need grab any locks. Actually they must not grab locks, for
1144 * otherwise the work flushing will deadlock.
1145 *
1146 * 4. Schedule a work item to do all subsequent steps, using the split-out
1147 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1148 * then cleaning up the framebuffers after the old framebuffer is no longer
1149 * being displayed.
1150 */
1151
1152/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001153 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001154 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001155 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001156 *
1157 * This function prepares plane state, specifically framebuffers, for the new
1158 * configuration. If any failure is encountered this function will call
1159 * ->cleanup_fb on any already successfully prepared framebuffer.
1160 *
1161 * Returns:
1162 * 0 on success, negative error code on failure.
1163 */
1164int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1165 struct drm_atomic_state *state)
1166{
1167 int nplanes = dev->mode_config.num_total_plane;
1168 int ret, i;
1169
1170 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001171 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001172 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001173 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001174
1175 if (!plane)
1176 continue;
1177
1178 funcs = plane->helper_private;
1179
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001180 if (funcs->prepare_fb) {
1181 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001182 if (ret)
1183 goto fail;
1184 }
1185 }
1186
1187 return 0;
1188
1189fail:
1190 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001191 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001192 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001193 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001194
1195 if (!plane)
1196 continue;
1197
1198 funcs = plane->helper_private;
1199
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001200 if (funcs->cleanup_fb)
1201 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001202
1203 }
1204
1205 return ret;
1206}
1207EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1208
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001209bool plane_crtc_active(struct drm_plane_state *state)
1210{
1211 return state->crtc && state->crtc->state->active;
1212}
1213
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001214/**
1215 * drm_atomic_helper_commit_planes - commit plane state
1216 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001217 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001218 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001219 *
1220 * This function commits the new plane state using the plane and atomic helper
1221 * functions for planes and crtcs. It assumes that the atomic state has already
1222 * been pushed into the relevant object state pointers, since this step can no
1223 * longer fail.
1224 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001225 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001226 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001227 *
1228 * Note that this function does all plane updates across all CRTCs in one step.
1229 * If the hardware can't support this approach look at
1230 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001231 *
1232 * Plane parameters can be updated by applications while the associated CRTC is
1233 * disabled. The DRM/KMS core will store the parameters in the plane state,
1234 * which will be available to the driver when the CRTC is turned on. As a result
1235 * most drivers don't need to be immediately notified of plane updates for a
1236 * disabled CRTC.
1237 *
1238 * Unless otherwise needed, drivers are advised to set the @active_only
1239 * parameters to true in order not to receive plane update notifications related
1240 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1241 * driver code when the driver and/or hardware can't or just don't need to deal
1242 * with updates on disabled CRTCs, for example when supporting runtime PM.
1243 *
1244 * The drm_atomic_helper_commit() default implementation only sets @active_only
1245 * to false to most closely match the behaviour of the legacy helpers. This should
1246 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001247 */
1248void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001249 struct drm_atomic_state *old_state,
1250 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001251{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001252 struct drm_crtc *crtc;
1253 struct drm_crtc_state *old_crtc_state;
1254 struct drm_plane *plane;
1255 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001256 int i;
1257
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001258 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001259 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001260
1261 funcs = crtc->helper_private;
1262
1263 if (!funcs || !funcs->atomic_begin)
1264 continue;
1265
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001266 if (active_only && !crtc->state->active)
1267 continue;
1268
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001269 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001270 }
1271
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001272 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001273 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001274 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001275
1276 funcs = plane->helper_private;
1277
Thierry Reding3cad4b62014-11-25 13:05:12 +01001278 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001279 continue;
1280
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001281 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1282
1283 if (active_only) {
1284 /*
1285 * Skip planes related to inactive CRTCs. If the plane
1286 * is enabled use the state of the current CRTC. If the
1287 * plane is being disabled use the state of the old
1288 * CRTC to avoid skipping planes being disabled on an
1289 * active CRTC.
1290 */
1291 if (!disabling && !plane_crtc_active(plane->state))
1292 continue;
1293 if (disabling && !plane_crtc_active(old_plane_state))
1294 continue;
1295 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001296
Thierry Reding407b8bd2014-11-20 12:05:50 +01001297 /*
1298 * Special-case disabling the plane if drivers support it.
1299 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001300 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001301 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001302 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001303 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001304 }
1305
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001306 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001307 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001308
1309 funcs = crtc->helper_private;
1310
1311 if (!funcs || !funcs->atomic_flush)
1312 continue;
1313
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001314 if (active_only && !crtc->state->active)
1315 continue;
1316
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001317 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001318 }
1319}
1320EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1321
1322/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001323 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1324 * @old_crtc_state: atomic state object with the old crtc state
1325 *
1326 * This function commits the new plane state using the plane and atomic helper
1327 * functions for planes on the specific crtc. It assumes that the atomic state
1328 * has already been pushed into the relevant object state pointers, since this
1329 * step can no longer fail.
1330 *
1331 * This function is useful when plane updates should be done crtc-by-crtc
1332 * instead of one global step like drm_atomic_helper_commit_planes() does.
1333 *
1334 * This function can only be savely used when planes are not allowed to move
1335 * between different CRTCs because this function doesn't handle inter-CRTC
1336 * depencies. Callers need to ensure that either no such depencies exist,
1337 * resolve them through ordering of commit calls or through some other means.
1338 */
1339void
1340drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1341{
1342 const struct drm_crtc_helper_funcs *crtc_funcs;
1343 struct drm_crtc *crtc = old_crtc_state->crtc;
1344 struct drm_atomic_state *old_state = old_crtc_state->state;
1345 struct drm_plane *plane;
1346 unsigned plane_mask;
1347
1348 plane_mask = old_crtc_state->plane_mask;
1349 plane_mask |= crtc->state->plane_mask;
1350
1351 crtc_funcs = crtc->helper_private;
1352 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001353 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001354
1355 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1356 struct drm_plane_state *old_plane_state =
1357 drm_atomic_get_existing_plane_state(old_state, plane);
1358 const struct drm_plane_helper_funcs *plane_funcs;
1359
1360 plane_funcs = plane->helper_private;
1361
1362 if (!old_plane_state || !plane_funcs)
1363 continue;
1364
1365 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1366
1367 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1368 plane_funcs->atomic_disable)
1369 plane_funcs->atomic_disable(plane, old_plane_state);
1370 else if (plane->state->crtc ||
1371 drm_atomic_plane_disabling(plane, old_plane_state))
1372 plane_funcs->atomic_update(plane, old_plane_state);
1373 }
1374
1375 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001376 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001377}
1378EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1379
1380/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001381 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1382 * @crtc: CRTC
1383 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1384 *
1385 * Disables all planes associated with the given CRTC. This can be
1386 * used for instance in the CRTC helper disable callback to disable
1387 * all planes before shutting down the display pipeline.
1388 *
1389 * If the atomic-parameter is set the function calls the CRTC's
1390 * atomic_begin hook before and atomic_flush hook after disabling the
1391 * planes.
1392 *
1393 * It is a bug to call this function without having implemented the
1394 * ->atomic_disable() plane hook.
1395 */
1396void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1397 bool atomic)
1398{
1399 const struct drm_crtc_helper_funcs *crtc_funcs =
1400 crtc->helper_private;
1401 struct drm_plane *plane;
1402
1403 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1404 crtc_funcs->atomic_begin(crtc, NULL);
1405
1406 drm_for_each_plane(plane, crtc->dev) {
1407 const struct drm_plane_helper_funcs *plane_funcs =
1408 plane->helper_private;
1409
1410 if (plane->state->crtc != crtc || !plane_funcs)
1411 continue;
1412
1413 WARN_ON(!plane_funcs->atomic_disable);
1414 if (plane_funcs->atomic_disable)
1415 plane_funcs->atomic_disable(plane, NULL);
1416 }
1417
1418 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1419 crtc_funcs->atomic_flush(crtc, NULL);
1420}
1421EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1422
1423/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001424 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1425 * @dev: DRM device
1426 * @old_state: atomic state object with old state structures
1427 *
1428 * This function cleans up plane state, specifically framebuffers, from the old
1429 * configuration. Hence the old configuration must be perserved in @old_state to
1430 * be able to call this function.
1431 *
1432 * This function must also be called on the new state when the atomic update
1433 * fails at any point after calling drm_atomic_helper_prepare_planes().
1434 */
1435void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1436 struct drm_atomic_state *old_state)
1437{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001438 struct drm_plane *plane;
1439 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001440 int i;
1441
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001442 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001443 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001444
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001445 funcs = plane->helper_private;
1446
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001447 if (funcs->cleanup_fb)
1448 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001449 }
1450}
1451EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1452
1453/**
1454 * drm_atomic_helper_swap_state - store atomic state into current sw state
1455 * @dev: DRM device
1456 * @state: atomic state
1457 *
1458 * This function stores the atomic state into the current state pointers in all
1459 * driver objects. It should be called after all failing steps have been done
1460 * and succeeded, but before the actual hardware state is committed.
1461 *
1462 * For cleanup and error recovery the current state for all changed objects will
1463 * be swaped into @state.
1464 *
1465 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1466 *
1467 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1468 *
1469 * 2. Do any other steps that might fail.
1470 *
1471 * 3. Put the staged state into the current state pointers with this function.
1472 *
1473 * 4. Actually commit the hardware state.
1474 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001475 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001476 * contains the old state. Also do any other cleanup required with that state.
1477 */
1478void drm_atomic_helper_swap_state(struct drm_device *dev,
1479 struct drm_atomic_state *state)
1480{
1481 int i;
1482
1483 for (i = 0; i < dev->mode_config.num_connector; i++) {
1484 struct drm_connector *connector = state->connectors[i];
1485
1486 if (!connector)
1487 continue;
1488
1489 connector->state->state = state;
1490 swap(state->connector_states[i], connector->state);
1491 connector->state->state = NULL;
1492 }
1493
1494 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1495 struct drm_crtc *crtc = state->crtcs[i];
1496
1497 if (!crtc)
1498 continue;
1499
1500 crtc->state->state = state;
1501 swap(state->crtc_states[i], crtc->state);
1502 crtc->state->state = NULL;
1503 }
1504
1505 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1506 struct drm_plane *plane = state->planes[i];
1507
1508 if (!plane)
1509 continue;
1510
1511 plane->state->state = state;
1512 swap(state->plane_states[i], plane->state);
1513 plane->state->state = NULL;
1514 }
1515}
1516EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001517
1518/**
1519 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1520 * @plane: plane object to update
1521 * @crtc: owning CRTC of owning plane
1522 * @fb: framebuffer to flip onto plane
1523 * @crtc_x: x offset of primary plane on crtc
1524 * @crtc_y: y offset of primary plane on crtc
1525 * @crtc_w: width of primary plane rectangle on crtc
1526 * @crtc_h: height of primary plane rectangle on crtc
1527 * @src_x: x offset of @fb for panning
1528 * @src_y: y offset of @fb for panning
1529 * @src_w: width of source rectangle in @fb
1530 * @src_h: height of source rectangle in @fb
1531 *
1532 * Provides a default plane update handler using the atomic driver interface.
1533 *
1534 * RETURNS:
1535 * Zero on success, error code on failure
1536 */
1537int drm_atomic_helper_update_plane(struct drm_plane *plane,
1538 struct drm_crtc *crtc,
1539 struct drm_framebuffer *fb,
1540 int crtc_x, int crtc_y,
1541 unsigned int crtc_w, unsigned int crtc_h,
1542 uint32_t src_x, uint32_t src_y,
1543 uint32_t src_w, uint32_t src_h)
1544{
1545 struct drm_atomic_state *state;
1546 struct drm_plane_state *plane_state;
1547 int ret = 0;
1548
1549 state = drm_atomic_state_alloc(plane->dev);
1550 if (!state)
1551 return -ENOMEM;
1552
1553 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1554retry:
1555 plane_state = drm_atomic_get_plane_state(state, plane);
1556 if (IS_ERR(plane_state)) {
1557 ret = PTR_ERR(plane_state);
1558 goto fail;
1559 }
1560
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001561 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001562 if (ret != 0)
1563 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001564 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001565 plane_state->crtc_x = crtc_x;
1566 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001567 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001568 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001569 plane_state->src_x = src_x;
1570 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001571 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001572 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001573
Daniel Vetter3671c582015-05-04 15:40:52 +02001574 if (plane == crtc->cursor)
1575 state->legacy_cursor_update = true;
1576
Daniel Vetter042652e2014-07-27 13:46:52 +02001577 ret = drm_atomic_commit(state);
1578 if (ret != 0)
1579 goto fail;
1580
1581 /* Driver takes ownership of state on successful commit. */
1582 return 0;
1583fail:
1584 if (ret == -EDEADLK)
1585 goto backoff;
1586
1587 drm_atomic_state_free(state);
1588
1589 return ret;
1590backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001591 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001592 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001593
1594 /*
1595 * Someone might have exchanged the framebuffer while we dropped locks
1596 * in the backoff code. We need to fix up the fb refcount tracking the
1597 * core does for us.
1598 */
1599 plane->old_fb = plane->fb;
1600
1601 goto retry;
1602}
1603EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1604
1605/**
1606 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1607 * @plane: plane to disable
1608 *
1609 * Provides a default plane disable handler using the atomic driver interface.
1610 *
1611 * RETURNS:
1612 * Zero on success, error code on failure
1613 */
1614int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1615{
1616 struct drm_atomic_state *state;
1617 struct drm_plane_state *plane_state;
1618 int ret = 0;
1619
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001620 /*
1621 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1622 * acquire context. The real fix will be to wire the acquire ctx through
1623 * everywhere we need it, but meanwhile prevent chaos by just skipping
1624 * this noop. The critical case is the cursor ioctls which a) only grab
1625 * crtc/cursor-plane locks (so we need the crtc to get at the right
1626 * acquire context) and b) can try to disable the plane multiple times.
1627 */
1628 if (!plane->crtc)
1629 return 0;
1630
Daniel Vetter042652e2014-07-27 13:46:52 +02001631 state = drm_atomic_state_alloc(plane->dev);
1632 if (!state)
1633 return -ENOMEM;
1634
1635 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1636retry:
1637 plane_state = drm_atomic_get_plane_state(state, plane);
1638 if (IS_ERR(plane_state)) {
1639 ret = PTR_ERR(plane_state);
1640 goto fail;
1641 }
1642
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01001643 if (plane_state->crtc && (plane == plane->crtc->cursor))
1644 plane_state->state->legacy_cursor_update = true;
1645
Rob Clarkbbb1e522015-08-25 15:35:58 -04001646 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001647 if (ret != 0)
1648 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01001649
Daniel Vetter042652e2014-07-27 13:46:52 +02001650 ret = drm_atomic_commit(state);
1651 if (ret != 0)
1652 goto fail;
1653
1654 /* Driver takes ownership of state on successful commit. */
1655 return 0;
1656fail:
1657 if (ret == -EDEADLK)
1658 goto backoff;
1659
1660 drm_atomic_state_free(state);
1661
1662 return ret;
1663backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001664 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001665 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001666
1667 /*
1668 * Someone might have exchanged the framebuffer while we dropped locks
1669 * in the backoff code. We need to fix up the fb refcount tracking the
1670 * core does for us.
1671 */
1672 plane->old_fb = plane->fb;
1673
1674 goto retry;
1675}
1676EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1677
Rob Clarkbbb1e522015-08-25 15:35:58 -04001678/* just used from fb-helper and atomic-helper: */
1679int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1680 struct drm_plane_state *plane_state)
1681{
1682 int ret;
1683
1684 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1685 if (ret != 0)
1686 return ret;
1687
1688 drm_atomic_set_fb_for_plane(plane_state, NULL);
1689 plane_state->crtc_x = 0;
1690 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001691 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001692 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001693 plane_state->src_x = 0;
1694 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001695 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001696 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001697
Rob Clarkbbb1e522015-08-25 15:35:58 -04001698 return 0;
1699}
1700
Daniel Vetter042652e2014-07-27 13:46:52 +02001701static int update_output_state(struct drm_atomic_state *state,
1702 struct drm_mode_set *set)
1703{
1704 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001705 struct drm_crtc *crtc;
1706 struct drm_crtc_state *crtc_state;
1707 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001708 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001709 int ret, i, j;
1710
1711 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1712 state->acquire_ctx);
1713 if (ret)
1714 return ret;
1715
1716 /* First grab all affected connector/crtc states. */
1717 for (i = 0; i < set->num_connectors; i++) {
1718 conn_state = drm_atomic_get_connector_state(state,
1719 set->connectors[i]);
1720 if (IS_ERR(conn_state))
1721 return PTR_ERR(conn_state);
1722 }
1723
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001724 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001725 ret = drm_atomic_add_affected_connectors(state, crtc);
1726 if (ret)
1727 return ret;
1728 }
1729
1730 /* Then recompute connector->crtc links and crtc enabling state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001731 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001732 if (conn_state->crtc == set->crtc) {
1733 ret = drm_atomic_set_crtc_for_connector(conn_state,
1734 NULL);
1735 if (ret)
1736 return ret;
1737 }
1738
1739 for (j = 0; j < set->num_connectors; j++) {
1740 if (set->connectors[j] == connector) {
1741 ret = drm_atomic_set_crtc_for_connector(conn_state,
1742 set->crtc);
1743 if (ret)
1744 return ret;
1745 break;
1746 }
1747 }
1748 }
1749
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001750 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001751 /* Don't update ->enable for the CRTC in the set_config request,
1752 * since a mismatch would indicate a bug in the upper layers.
1753 * The actual modeset code later on will catch any
1754 * inconsistencies here. */
1755 if (crtc == set->crtc)
1756 continue;
1757
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001758 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1759 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1760 NULL);
1761 if (ret < 0)
1762 return ret;
1763
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001764 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001765 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001766 }
1767
1768 return 0;
1769}
1770
1771/**
1772 * drm_atomic_helper_set_config - set a new config from userspace
1773 * @set: mode set configuration
1774 *
1775 * Provides a default crtc set_config handler using the atomic driver interface.
1776 *
1777 * Returns:
1778 * Returns 0 on success, negative errno numbers on failure.
1779 */
1780int drm_atomic_helper_set_config(struct drm_mode_set *set)
1781{
1782 struct drm_atomic_state *state;
1783 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02001784 int ret = 0;
1785
1786 state = drm_atomic_state_alloc(crtc->dev);
1787 if (!state)
1788 return -ENOMEM;
1789
1790 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1791retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04001792 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01001793 if (ret != 0)
1794 goto fail;
1795
Daniel Vetter042652e2014-07-27 13:46:52 +02001796 ret = drm_atomic_commit(state);
1797 if (ret != 0)
1798 goto fail;
1799
1800 /* Driver takes ownership of state on successful commit. */
1801 return 0;
1802fail:
1803 if (ret == -EDEADLK)
1804 goto backoff;
1805
1806 drm_atomic_state_free(state);
1807
1808 return ret;
1809backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001810 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001811 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001812
1813 /*
1814 * Someone might have exchanged the framebuffer while we dropped locks
1815 * in the backoff code. We need to fix up the fb refcount tracking the
1816 * core does for us.
1817 */
1818 crtc->primary->old_fb = crtc->primary->fb;
1819
1820 goto retry;
1821}
1822EXPORT_SYMBOL(drm_atomic_helper_set_config);
1823
Rob Clarkbbb1e522015-08-25 15:35:58 -04001824/* just used from fb-helper and atomic-helper: */
1825int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1826 struct drm_atomic_state *state)
1827{
1828 struct drm_crtc_state *crtc_state;
1829 struct drm_plane_state *primary_state;
1830 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02001831 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001832 int ret;
1833
1834 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1835 if (IS_ERR(crtc_state))
1836 return PTR_ERR(crtc_state);
1837
1838 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1839 if (IS_ERR(primary_state))
1840 return PTR_ERR(primary_state);
1841
1842 if (!set->mode) {
1843 WARN_ON(set->fb);
1844 WARN_ON(set->num_connectors);
1845
1846 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1847 if (ret != 0)
1848 return ret;
1849
1850 crtc_state->active = false;
1851
1852 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1853 if (ret != 0)
1854 return ret;
1855
1856 drm_atomic_set_fb_for_plane(primary_state, NULL);
1857
1858 goto commit;
1859 }
1860
1861 WARN_ON(!set->fb);
1862 WARN_ON(!set->num_connectors);
1863
1864 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1865 if (ret != 0)
1866 return ret;
1867
1868 crtc_state->active = true;
1869
1870 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1871 if (ret != 0)
1872 return ret;
1873
Ville Syrjälä83926112015-11-16 17:02:34 +02001874 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1875
Rob Clarkbbb1e522015-08-25 15:35:58 -04001876 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1877 primary_state->crtc_x = 0;
1878 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02001879 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001880 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001881 primary_state->src_x = set->x << 16;
1882 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001883 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02001884 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001885 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001886 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02001887 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001888 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001889 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04001890
1891commit:
1892 ret = update_output_state(state, set);
1893 if (ret)
1894 return ret;
1895
1896 return 0;
1897}
1898
Daniel Vetter042652e2014-07-27 13:46:52 +02001899/**
Thierry Reding14942762015-12-02 17:50:04 +01001900 * drm_atomic_helper_disable_all - disable all currently active outputs
1901 * @dev: DRM device
1902 * @ctx: lock acquisition context
1903 *
1904 * Loops through all connectors, finding those that aren't turned off and then
1905 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1906 * that they are connected to.
1907 *
1908 * This is used for example in suspend/resume to disable all currently active
1909 * functions when suspending.
1910 *
1911 * Note that if callers haven't already acquired all modeset locks this might
1912 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1913 *
1914 * Returns:
1915 * 0 on success or a negative error code on failure.
1916 *
1917 * See also:
1918 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1919 */
1920int drm_atomic_helper_disable_all(struct drm_device *dev,
1921 struct drm_modeset_acquire_ctx *ctx)
1922{
1923 struct drm_atomic_state *state;
1924 struct drm_connector *conn;
1925 int err;
1926
1927 state = drm_atomic_state_alloc(dev);
1928 if (!state)
1929 return -ENOMEM;
1930
1931 state->acquire_ctx = ctx;
1932
1933 drm_for_each_connector(conn, dev) {
1934 struct drm_crtc *crtc = conn->state->crtc;
1935 struct drm_crtc_state *crtc_state;
1936
1937 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1938 continue;
1939
1940 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1941 if (IS_ERR(crtc_state)) {
1942 err = PTR_ERR(crtc_state);
1943 goto free;
1944 }
1945
1946 crtc_state->active = false;
1947 }
1948
1949 err = drm_atomic_commit(state);
1950
1951free:
1952 if (err < 0)
1953 drm_atomic_state_free(state);
1954
1955 return err;
1956}
1957EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1958
1959/**
1960 * drm_atomic_helper_suspend - subsystem-level suspend helper
1961 * @dev: DRM device
1962 *
1963 * Duplicates the current atomic state, disables all active outputs and then
1964 * returns a pointer to the original atomic state to the caller. Drivers can
1965 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1966 * restore the output configuration that was active at the time the system
1967 * entered suspend.
1968 *
1969 * Note that it is potentially unsafe to use this. The atomic state object
1970 * returned by this function is assumed to be persistent. Drivers must ensure
1971 * that this holds true. Before calling this function, drivers must make sure
1972 * to suspend fbdev emulation so that nothing can be using the device.
1973 *
1974 * Returns:
1975 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1976 * encoded error code on failure. Drivers should store the returned atomic
1977 * state object and pass it to the drm_atomic_helper_resume() helper upon
1978 * resume.
1979 *
1980 * See also:
1981 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1982 * drm_atomic_helper_resume()
1983 */
1984struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1985{
1986 struct drm_modeset_acquire_ctx ctx;
1987 struct drm_atomic_state *state;
1988 int err;
1989
1990 drm_modeset_acquire_init(&ctx, 0);
1991
1992retry:
1993 err = drm_modeset_lock_all_ctx(dev, &ctx);
1994 if (err < 0) {
1995 state = ERR_PTR(err);
1996 goto unlock;
1997 }
1998
1999 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2000 if (IS_ERR(state))
2001 goto unlock;
2002
2003 err = drm_atomic_helper_disable_all(dev, &ctx);
2004 if (err < 0) {
2005 drm_atomic_state_free(state);
2006 state = ERR_PTR(err);
2007 goto unlock;
2008 }
2009
2010unlock:
2011 if (PTR_ERR(state) == -EDEADLK) {
2012 drm_modeset_backoff(&ctx);
2013 goto retry;
2014 }
2015
2016 drm_modeset_drop_locks(&ctx);
2017 drm_modeset_acquire_fini(&ctx);
2018 return state;
2019}
2020EXPORT_SYMBOL(drm_atomic_helper_suspend);
2021
2022/**
2023 * drm_atomic_helper_resume - subsystem-level resume helper
2024 * @dev: DRM device
2025 * @state: atomic state to resume to
2026 *
2027 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2028 * grabs all modeset locks and commits the atomic state object. This can be
2029 * used in conjunction with the drm_atomic_helper_suspend() helper to
2030 * implement suspend/resume for drivers that support atomic mode-setting.
2031 *
2032 * Returns:
2033 * 0 on success or a negative error code on failure.
2034 *
2035 * See also:
2036 * drm_atomic_helper_suspend()
2037 */
2038int drm_atomic_helper_resume(struct drm_device *dev,
2039 struct drm_atomic_state *state)
2040{
2041 struct drm_mode_config *config = &dev->mode_config;
2042 int err;
2043
2044 drm_mode_config_reset(dev);
2045 drm_modeset_lock_all(dev);
2046 state->acquire_ctx = config->acquire_ctx;
2047 err = drm_atomic_commit(state);
2048 drm_modeset_unlock_all(dev);
2049
2050 return err;
2051}
2052EXPORT_SYMBOL(drm_atomic_helper_resume);
2053
2054/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002055 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002056 * @crtc: DRM crtc
2057 * @property: DRM property
2058 * @val: value of property
2059 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002060 * Provides a default crtc set_property handler using the atomic driver
2061 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002062 *
2063 * RETURNS:
2064 * Zero on success, error code on failure
2065 */
2066int
2067drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2068 struct drm_property *property,
2069 uint64_t val)
2070{
2071 struct drm_atomic_state *state;
2072 struct drm_crtc_state *crtc_state;
2073 int ret = 0;
2074
2075 state = drm_atomic_state_alloc(crtc->dev);
2076 if (!state)
2077 return -ENOMEM;
2078
2079 /* ->set_property is always called with all locks held. */
2080 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2081retry:
2082 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2083 if (IS_ERR(crtc_state)) {
2084 ret = PTR_ERR(crtc_state);
2085 goto fail;
2086 }
2087
Rob Clark40ecc692014-12-18 16:01:46 -05002088 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2089 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002090 if (ret)
2091 goto fail;
2092
2093 ret = drm_atomic_commit(state);
2094 if (ret != 0)
2095 goto fail;
2096
2097 /* Driver takes ownership of state on successful commit. */
2098 return 0;
2099fail:
2100 if (ret == -EDEADLK)
2101 goto backoff;
2102
2103 drm_atomic_state_free(state);
2104
2105 return ret;
2106backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002107 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002108 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002109
2110 goto retry;
2111}
2112EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2113
2114/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002115 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002116 * @plane: DRM plane
2117 * @property: DRM property
2118 * @val: value of property
2119 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002120 * Provides a default plane set_property handler using the atomic driver
2121 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002122 *
2123 * RETURNS:
2124 * Zero on success, error code on failure
2125 */
2126int
2127drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2128 struct drm_property *property,
2129 uint64_t val)
2130{
2131 struct drm_atomic_state *state;
2132 struct drm_plane_state *plane_state;
2133 int ret = 0;
2134
2135 state = drm_atomic_state_alloc(plane->dev);
2136 if (!state)
2137 return -ENOMEM;
2138
2139 /* ->set_property is always called with all locks held. */
2140 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2141retry:
2142 plane_state = drm_atomic_get_plane_state(state, plane);
2143 if (IS_ERR(plane_state)) {
2144 ret = PTR_ERR(plane_state);
2145 goto fail;
2146 }
2147
Rob Clark40ecc692014-12-18 16:01:46 -05002148 ret = drm_atomic_plane_set_property(plane, plane_state,
2149 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002150 if (ret)
2151 goto fail;
2152
2153 ret = drm_atomic_commit(state);
2154 if (ret != 0)
2155 goto fail;
2156
2157 /* Driver takes ownership of state on successful commit. */
2158 return 0;
2159fail:
2160 if (ret == -EDEADLK)
2161 goto backoff;
2162
2163 drm_atomic_state_free(state);
2164
2165 return ret;
2166backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002167 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002168 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002169
2170 goto retry;
2171}
2172EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2173
2174/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002175 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002176 * @connector: DRM connector
2177 * @property: DRM property
2178 * @val: value of property
2179 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002180 * Provides a default connector set_property handler using the atomic driver
2181 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002182 *
2183 * RETURNS:
2184 * Zero on success, error code on failure
2185 */
2186int
2187drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2188 struct drm_property *property,
2189 uint64_t val)
2190{
2191 struct drm_atomic_state *state;
2192 struct drm_connector_state *connector_state;
2193 int ret = 0;
2194
2195 state = drm_atomic_state_alloc(connector->dev);
2196 if (!state)
2197 return -ENOMEM;
2198
2199 /* ->set_property is always called with all locks held. */
2200 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2201retry:
2202 connector_state = drm_atomic_get_connector_state(state, connector);
2203 if (IS_ERR(connector_state)) {
2204 ret = PTR_ERR(connector_state);
2205 goto fail;
2206 }
2207
Rob Clark40ecc692014-12-18 16:01:46 -05002208 ret = drm_atomic_connector_set_property(connector, connector_state,
2209 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002210 if (ret)
2211 goto fail;
2212
2213 ret = drm_atomic_commit(state);
2214 if (ret != 0)
2215 goto fail;
2216
2217 /* Driver takes ownership of state on successful commit. */
2218 return 0;
2219fail:
2220 if (ret == -EDEADLK)
2221 goto backoff;
2222
2223 drm_atomic_state_free(state);
2224
2225 return ret;
2226backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002227 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002228 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002229
2230 goto retry;
2231}
2232EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002233
2234/**
2235 * drm_atomic_helper_page_flip - execute a legacy page flip
2236 * @crtc: DRM crtc
2237 * @fb: DRM framebuffer
2238 * @event: optional DRM event to signal upon completion
2239 * @flags: flip flags for non-vblank sync'ed updates
2240 *
2241 * Provides a default page flip implementation using the atomic driver interface.
2242 *
2243 * Note that for now so called async page flips (i.e. updates which are not
2244 * synchronized to vblank) are not supported, since the atomic interfaces have
2245 * no provisions for this yet.
2246 *
2247 * Returns:
2248 * Returns 0 on success, negative errno numbers on failure.
2249 */
2250int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2251 struct drm_framebuffer *fb,
2252 struct drm_pending_vblank_event *event,
2253 uint32_t flags)
2254{
2255 struct drm_plane *plane = crtc->primary;
2256 struct drm_atomic_state *state;
2257 struct drm_plane_state *plane_state;
2258 struct drm_crtc_state *crtc_state;
2259 int ret = 0;
2260
2261 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2262 return -EINVAL;
2263
2264 state = drm_atomic_state_alloc(plane->dev);
2265 if (!state)
2266 return -ENOMEM;
2267
2268 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2269retry:
2270 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2271 if (IS_ERR(crtc_state)) {
2272 ret = PTR_ERR(crtc_state);
2273 goto fail;
2274 }
2275 crtc_state->event = event;
2276
2277 plane_state = drm_atomic_get_plane_state(state, plane);
2278 if (IS_ERR(plane_state)) {
2279 ret = PTR_ERR(plane_state);
2280 goto fail;
2281 }
2282
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002283 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002284 if (ret != 0)
2285 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002286 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002287
2288 ret = drm_atomic_async_commit(state);
2289 if (ret != 0)
2290 goto fail;
2291
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002292 /* Driver takes ownership of state on successful async commit. */
2293 return 0;
2294fail:
2295 if (ret == -EDEADLK)
2296 goto backoff;
2297
2298 drm_atomic_state_free(state);
2299
2300 return ret;
2301backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002302 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002303 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002304
2305 /*
2306 * Someone might have exchanged the framebuffer while we dropped locks
2307 * in the backoff code. We need to fix up the fb refcount tracking the
2308 * core does for us.
2309 */
2310 plane->old_fb = plane->fb;
2311
2312 goto retry;
2313}
2314EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002315
2316/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002317 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2318 * @connector: affected connector
2319 * @mode: DPMS mode
2320 *
2321 * This is the main helper function provided by the atomic helper framework for
2322 * implementing the legacy DPMS connector interface. It computes the new desired
2323 * ->active state for the corresponding CRTC (if the connector is enabled) and
2324 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002325 *
2326 * Returns:
2327 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002328 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002329int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2330 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002331{
2332 struct drm_mode_config *config = &connector->dev->mode_config;
2333 struct drm_atomic_state *state;
2334 struct drm_crtc_state *crtc_state;
2335 struct drm_crtc *crtc;
2336 struct drm_connector *tmp_connector;
2337 int ret;
2338 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002339 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002340
2341 if (mode != DRM_MODE_DPMS_ON)
2342 mode = DRM_MODE_DPMS_OFF;
2343
2344 connector->dpms = mode;
2345 crtc = connector->state->crtc;
2346
2347 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002348 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002349
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002350 state = drm_atomic_state_alloc(connector->dev);
2351 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002352 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002353
2354 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2355retry:
2356 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002357 if (IS_ERR(crtc_state)) {
2358 ret = PTR_ERR(crtc_state);
2359 goto fail;
2360 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002361
2362 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2363
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002364 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002365 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002366 continue;
2367
John Hunter0388df02015-03-17 15:30:28 +08002368 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002369 active = true;
2370 break;
2371 }
2372 }
2373 crtc_state->active = active;
2374
2375 ret = drm_atomic_commit(state);
2376 if (ret != 0)
2377 goto fail;
2378
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002379 /* Driver takes ownership of state on successful commit. */
2380 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002381fail:
2382 if (ret == -EDEADLK)
2383 goto backoff;
2384
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002385 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002386 drm_atomic_state_free(state);
2387
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002388 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002389backoff:
2390 drm_atomic_state_clear(state);
2391 drm_atomic_legacy_backoff(state);
2392
2393 goto retry;
2394}
2395EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2396
2397/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002398 * DOC: atomic state reset and initialization
2399 *
2400 * Both the drm core and the atomic helpers assume that there is always the full
2401 * and correct atomic software state for all connectors, CRTCs and planes
2402 * available. Which is a bit a problem on driver load and also after system
2403 * suspend. One way to solve this is to have a hardware state read-out
2404 * infrastructure which reconstructs the full software state (e.g. the i915
2405 * driver).
2406 *
2407 * The simpler solution is to just reset the software state to everything off,
2408 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2409 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01002410 *
2411 * On the upside the precise state tracking of atomic simplifies system suspend
2412 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2413 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2414 * For other drivers the building blocks are split out, see the documentation
2415 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002416 */
2417
2418/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002419 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2420 * @crtc: drm CRTC
2421 *
2422 * Resets the atomic state for @crtc by freeing the state pointer (which might
2423 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2424 */
2425void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2426{
Markus Elfring5f911902015-11-06 12:03:46 +01002427 if (crtc->state)
Daniel Stone99cf4a22015-05-25 19:11:51 +01002428 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002429 kfree(crtc->state);
2430 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002431
2432 if (crtc->state)
2433 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002434}
2435EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2436
2437/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002438 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2439 * @crtc: CRTC object
2440 * @state: atomic CRTC state
2441 *
2442 * Copies atomic state from a CRTC's current state and resets inferred values.
2443 * This is useful for drivers that subclass the CRTC state.
2444 */
2445void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2446 struct drm_crtc_state *state)
2447{
2448 memcpy(state, crtc->state, sizeof(*state));
2449
Daniel Stone99cf4a22015-05-25 19:11:51 +01002450 if (state->mode_blob)
2451 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002452 state->mode_changed = false;
2453 state->active_changed = false;
2454 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002455 state->connectors_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002456 state->event = NULL;
2457}
2458EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2459
2460/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002461 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2462 * @crtc: drm CRTC
2463 *
2464 * Default CRTC state duplicate hook for drivers which don't have their own
2465 * subclassed CRTC state structure.
2466 */
2467struct drm_crtc_state *
2468drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2469{
2470 struct drm_crtc_state *state;
2471
2472 if (WARN_ON(!crtc->state))
2473 return NULL;
2474
Thierry Redingf5e78402015-01-28 14:54:32 +01002475 state = kmalloc(sizeof(*state), GFP_KERNEL);
2476 if (state)
2477 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002478
2479 return state;
2480}
2481EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2482
2483/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002484 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2485 * @crtc: CRTC object
2486 * @state: CRTC state object to release
2487 *
2488 * Releases all resources stored in the CRTC state without actually freeing
2489 * the memory of the CRTC state. This is useful for drivers that subclass the
2490 * CRTC state.
2491 */
2492void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2493 struct drm_crtc_state *state)
2494{
Markus Elfring5f911902015-11-06 12:03:46 +01002495 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002496}
2497EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2498
2499/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002500 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2501 * @crtc: drm CRTC
2502 * @state: CRTC state object to release
2503 *
2504 * Default CRTC state destroy hook for drivers which don't have their own
2505 * subclassed CRTC state structure.
2506 */
2507void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2508 struct drm_crtc_state *state)
2509{
Thierry Redingf5e78402015-01-28 14:54:32 +01002510 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002511 kfree(state);
2512}
2513EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2514
2515/**
2516 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2517 * @plane: drm plane
2518 *
2519 * Resets the atomic state for @plane by freeing the state pointer (which might
2520 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2521 */
2522void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2523{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002524 if (plane->state && plane->state->fb)
2525 drm_framebuffer_unreference(plane->state->fb);
2526
Daniel Vetterd4617012014-11-03 15:56:43 +01002527 kfree(plane->state);
2528 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002529
2530 if (plane->state)
2531 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01002532}
2533EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2534
2535/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002536 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2537 * @plane: plane object
2538 * @state: atomic plane state
2539 *
2540 * Copies atomic state from a plane's current state. This is useful for
2541 * drivers that subclass the plane state.
2542 */
2543void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2544 struct drm_plane_state *state)
2545{
2546 memcpy(state, plane->state, sizeof(*state));
2547
2548 if (state->fb)
2549 drm_framebuffer_reference(state->fb);
2550}
2551EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2552
2553/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002554 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2555 * @plane: drm plane
2556 *
2557 * Default plane state duplicate hook for drivers which don't have their own
2558 * subclassed plane state structure.
2559 */
2560struct drm_plane_state *
2561drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2562{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002563 struct drm_plane_state *state;
2564
Daniel Vetterd4617012014-11-03 15:56:43 +01002565 if (WARN_ON(!plane->state))
2566 return NULL;
2567
Thierry Redingf5e78402015-01-28 14:54:32 +01002568 state = kmalloc(sizeof(*state), GFP_KERNEL);
2569 if (state)
2570 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002571
2572 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002573}
2574EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2575
2576/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002577 * __drm_atomic_helper_plane_destroy_state - release plane state
2578 * @plane: plane object
2579 * @state: plane state object to release
2580 *
2581 * Releases all resources stored in the plane state without actually freeing
2582 * the memory of the plane state. This is useful for drivers that subclass the
2583 * plane state.
2584 */
2585void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2586 struct drm_plane_state *state)
2587{
2588 if (state->fb)
2589 drm_framebuffer_unreference(state->fb);
2590}
2591EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2592
2593/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002594 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2595 * @plane: drm plane
2596 * @state: plane state object to release
2597 *
2598 * Default plane state destroy hook for drivers which don't have their own
2599 * subclassed plane state structure.
2600 */
2601void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002602 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002603{
Thierry Redingf5e78402015-01-28 14:54:32 +01002604 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002605 kfree(state);
2606}
2607EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2608
2609/**
2610 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2611 * @connector: drm connector
2612 *
2613 * Resets the atomic state for @connector by freeing the state pointer (which
2614 * might be NULL, e.g. at driver load time) and allocating a new empty state
2615 * object.
2616 */
2617void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2618{
2619 kfree(connector->state);
2620 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002621
2622 if (connector->state)
2623 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002624}
2625EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2626
2627/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002628 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2629 * @connector: connector object
2630 * @state: atomic connector state
2631 *
2632 * Copies atomic state from a connector's current state. This is useful for
2633 * drivers that subclass the connector state.
2634 */
2635void
2636__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2637 struct drm_connector_state *state)
2638{
2639 memcpy(state, connector->state, sizeof(*state));
2640}
2641EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2642
2643/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002644 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2645 * @connector: drm connector
2646 *
2647 * Default connector state duplicate hook for drivers which don't have their own
2648 * subclassed connector state structure.
2649 */
2650struct drm_connector_state *
2651drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2652{
Thierry Redingf5e78402015-01-28 14:54:32 +01002653 struct drm_connector_state *state;
2654
Daniel Vetterd4617012014-11-03 15:56:43 +01002655 if (WARN_ON(!connector->state))
2656 return NULL;
2657
Thierry Redingf5e78402015-01-28 14:54:32 +01002658 state = kmalloc(sizeof(*state), GFP_KERNEL);
2659 if (state)
2660 __drm_atomic_helper_connector_duplicate_state(connector, state);
2661
2662 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002663}
2664EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2665
2666/**
Thierry Reding397fd772015-09-08 15:00:45 +02002667 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2668 * @dev: DRM device
2669 * @ctx: lock acquisition context
2670 *
2671 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01002672 * duplicating their respective states. This is used for example by suspend/
2673 * resume support code to save the state prior to suspend such that it can
2674 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02002675 *
2676 * Note that this treats atomic state as persistent between save and restore.
2677 * Drivers must make sure that this is possible and won't result in confusion
2678 * or erroneous behaviour.
2679 *
2680 * Note that if callers haven't already acquired all modeset locks this might
2681 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2682 *
2683 * Returns:
2684 * A pointer to the copy of the atomic state object on success or an
2685 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01002686 *
2687 * See also:
2688 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02002689 */
2690struct drm_atomic_state *
2691drm_atomic_helper_duplicate_state(struct drm_device *dev,
2692 struct drm_modeset_acquire_ctx *ctx)
2693{
2694 struct drm_atomic_state *state;
2695 struct drm_connector *conn;
2696 struct drm_plane *plane;
2697 struct drm_crtc *crtc;
2698 int err = 0;
2699
2700 state = drm_atomic_state_alloc(dev);
2701 if (!state)
2702 return ERR_PTR(-ENOMEM);
2703
2704 state->acquire_ctx = ctx;
2705
2706 drm_for_each_crtc(crtc, dev) {
2707 struct drm_crtc_state *crtc_state;
2708
2709 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2710 if (IS_ERR(crtc_state)) {
2711 err = PTR_ERR(crtc_state);
2712 goto free;
2713 }
2714 }
2715
2716 drm_for_each_plane(plane, dev) {
2717 struct drm_plane_state *plane_state;
2718
2719 plane_state = drm_atomic_get_plane_state(state, plane);
2720 if (IS_ERR(plane_state)) {
2721 err = PTR_ERR(plane_state);
2722 goto free;
2723 }
2724 }
2725
2726 drm_for_each_connector(conn, dev) {
2727 struct drm_connector_state *conn_state;
2728
2729 conn_state = drm_atomic_get_connector_state(state, conn);
2730 if (IS_ERR(conn_state)) {
2731 err = PTR_ERR(conn_state);
2732 goto free;
2733 }
2734 }
2735
2736 /* clear the acquire context so that it isn't accidentally reused */
2737 state->acquire_ctx = NULL;
2738
2739free:
2740 if (err < 0) {
2741 drm_atomic_state_free(state);
2742 state = ERR_PTR(err);
2743 }
2744
2745 return state;
2746}
2747EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2748
2749/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002750 * __drm_atomic_helper_connector_destroy_state - release connector state
2751 * @connector: connector object
2752 * @state: connector state object to release
2753 *
2754 * Releases all resources stored in the connector state without actually
2755 * freeing the memory of the connector state. This is useful for drivers that
2756 * subclass the connector state.
2757 */
2758void
2759__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2760 struct drm_connector_state *state)
2761{
2762 /*
2763 * This is currently a placeholder so that drivers that subclass the
2764 * state will automatically do the right thing if code is ever added
2765 * to this function.
2766 */
2767}
2768EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2769
2770/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002771 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2772 * @connector: drm connector
2773 * @state: connector state object to release
2774 *
2775 * Default connector state destroy hook for drivers which don't have their own
2776 * subclassed connector state structure.
2777 */
2778void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2779 struct drm_connector_state *state)
2780{
Thierry Redingf5e78402015-01-28 14:54:32 +01002781 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002782 kfree(state);
2783}
2784EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);