blob: 379b6c833bb2c06bd369acfe3f81011d8efdab19 [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
45 * drm_atomic_helper_check and for the commit callback with
46 * drm_atomic_helper_commit. But the individual stages and callbacks are expose
47 * to allow drivers to mix and match and e.g. use the plane helpers only
48 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
51 * 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
53 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
55 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010056static void
57drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
58 struct drm_plane_state *plane_state,
59 struct drm_plane *plane)
60{
61 struct drm_crtc_state *crtc_state;
62
63 if (plane->state->crtc) {
Rob Clark4b08eae2014-12-08 10:45:43 -050064 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
Daniel Vetterc2fcd272014-11-05 00:14:14 +010065
66 if (WARN_ON(!crtc_state))
67 return;
68
69 crtc_state->planes_changed = true;
70 }
71
72 if (plane_state->crtc) {
73 crtc_state =
74 state->crtc_states[drm_crtc_index(plane_state->crtc)];
75
76 if (WARN_ON(!crtc_state))
77 return;
78
79 crtc_state->planes_changed = true;
80 }
81}
82
Daniel Vetter623369e2014-09-16 17:50:47 +020083static struct drm_crtc *
84get_current_crtc_for_encoder(struct drm_device *dev,
85 struct drm_encoder *encoder)
86{
87 struct drm_mode_config *config = &dev->mode_config;
88 struct drm_connector *connector;
89
90 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
91
92 list_for_each_entry(connector, &config->connector_list, head) {
93 if (connector->state->best_encoder != encoder)
94 continue;
95
96 return connector->state->crtc;
97 }
98
99 return NULL;
100}
101
102static int
103steal_encoder(struct drm_atomic_state *state,
104 struct drm_encoder *encoder,
105 struct drm_crtc *encoder_crtc)
106{
107 struct drm_mode_config *config = &state->dev->mode_config;
108 struct drm_crtc_state *crtc_state;
109 struct drm_connector *connector;
110 struct drm_connector_state *connector_state;
Daniel Vetter042652e2014-07-27 13:46:52 +0200111 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200112
113 /*
114 * We can only steal an encoder coming from a connector, which means we
115 * must already hold the connection_mutex.
116 */
117 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118
119 DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
120 encoder->base.id, encoder->name,
121 encoder_crtc->base.id);
122
123 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
124 if (IS_ERR(crtc_state))
125 return PTR_ERR(crtc_state);
126
127 crtc_state->mode_changed = true;
128
129 list_for_each_entry(connector, &config->connector_list, head) {
130 if (connector->state->best_encoder != encoder)
131 continue;
132
133 DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
134 connector->base.id,
135 connector->name);
136
137 connector_state = drm_atomic_get_connector_state(state,
138 connector);
139 if (IS_ERR(connector_state))
140 return PTR_ERR(connector_state);
141
Daniel Vetter042652e2014-07-27 13:46:52 +0200142 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
143 if (ret)
144 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200145 connector_state->best_encoder = NULL;
146 }
147
148 return 0;
149}
150
151static int
152update_connector_routing(struct drm_atomic_state *state, int conn_idx)
153{
154 struct drm_connector_helper_funcs *funcs;
155 struct drm_encoder *new_encoder;
156 struct drm_crtc *encoder_crtc;
157 struct drm_connector *connector;
158 struct drm_connector_state *connector_state;
159 struct drm_crtc_state *crtc_state;
160 int idx, ret;
161
162 connector = state->connectors[conn_idx];
163 connector_state = state->connector_states[conn_idx];
164
165 if (!connector)
166 return 0;
167
168 DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
169 connector->base.id,
170 connector->name);
171
172 if (connector->state->crtc != connector_state->crtc) {
173 if (connector->state->crtc) {
174 idx = drm_crtc_index(connector->state->crtc);
175
176 crtc_state = state->crtc_states[idx];
177 crtc_state->mode_changed = true;
178 }
179
180 if (connector_state->crtc) {
181 idx = drm_crtc_index(connector_state->crtc);
182
183 crtc_state = state->crtc_states[idx];
184 crtc_state->mode_changed = true;
185 }
186 }
187
188 if (!connector_state->crtc) {
189 DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
190 connector->base.id,
191 connector->name);
192
193 connector_state->best_encoder = NULL;
194
195 return 0;
196 }
197
198 funcs = connector->helper_private;
199 new_encoder = funcs->best_encoder(connector);
200
201 if (!new_encoder) {
202 DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
203 connector->base.id,
204 connector->name);
205 return -EINVAL;
206 }
207
208 if (new_encoder == connector_state->best_encoder) {
209 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
210 connector->base.id,
211 connector->name,
212 new_encoder->base.id,
213 new_encoder->name,
214 connector_state->crtc->base.id);
215
216 return 0;
217 }
218
219 encoder_crtc = get_current_crtc_for_encoder(state->dev,
220 new_encoder);
221
222 if (encoder_crtc) {
223 ret = steal_encoder(state, new_encoder, encoder_crtc);
224 if (ret) {
225 DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
226 connector->base.id,
227 connector->name);
228 return ret;
229 }
230 }
231
232 connector_state->best_encoder = new_encoder;
233 idx = drm_crtc_index(connector_state->crtc);
234
235 crtc_state = state->crtc_states[idx];
236 crtc_state->mode_changed = true;
237
238 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
239 connector->base.id,
240 connector->name,
241 new_encoder->base.id,
242 new_encoder->name,
243 connector_state->crtc->base.id);
244
245 return 0;
246}
247
248static int
249mode_fixup(struct drm_atomic_state *state)
250{
251 int ncrtcs = state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200252 struct drm_crtc_state *crtc_state;
253 struct drm_connector_state *conn_state;
254 int i;
255 bool ret;
256
257 for (i = 0; i < ncrtcs; i++) {
258 crtc_state = state->crtc_states[i];
259
260 if (!crtc_state || !crtc_state->mode_changed)
261 continue;
262
263 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
264 }
265
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100266 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200267 struct drm_encoder_helper_funcs *funcs;
268 struct drm_encoder *encoder;
269
270 conn_state = state->connector_states[i];
271
272 if (!conn_state)
273 continue;
274
275 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
276
277 if (!conn_state->crtc || !conn_state->best_encoder)
278 continue;
279
280 crtc_state =
281 state->crtc_states[drm_crtc_index(conn_state->crtc)];
282
283 /*
284 * Each encoder has at most one connector (since we always steal
285 * it away), so we won't call ->mode_fixup twice.
286 */
287 encoder = conn_state->best_encoder;
288 funcs = encoder->helper_private;
289
290 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
291 ret = encoder->bridge->funcs->mode_fixup(
292 encoder->bridge, &crtc_state->mode,
293 &crtc_state->adjusted_mode);
294 if (!ret) {
295 DRM_DEBUG_KMS("Bridge fixup failed\n");
296 return -EINVAL;
297 }
298 }
299
300
301 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
302 &crtc_state->adjusted_mode);
303 if (!ret) {
304 DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
305 encoder->base.id, encoder->name);
306 return -EINVAL;
307 }
308 }
309
310 for (i = 0; i < ncrtcs; i++) {
311 struct drm_crtc_helper_funcs *funcs;
312 struct drm_crtc *crtc;
313
314 crtc_state = state->crtc_states[i];
315 crtc = state->crtcs[i];
316
317 if (!crtc_state || !crtc_state->mode_changed)
318 continue;
319
320 funcs = crtc->helper_private;
321 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
322 &crtc_state->adjusted_mode);
323 if (!ret) {
324 DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
325 crtc->base.id);
326 return -EINVAL;
327 }
328 }
329
330 return 0;
331}
332
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100333static bool
334needs_modeset(struct drm_crtc_state *state)
335{
336 return state->mode_changed || state->active_changed;
337}
338
Daniel Vetterd9b13622014-11-26 16:57:41 +0100339/**
340 * drm_atomic_helper_check - validate state object for modeset changes
341 * @dev: DRM device
342 * @state: the driver state object
343 *
344 * Check the state object to see if the requested state is physically possible.
345 * This does all the crtc and connector related computations for an atomic
346 * update. It computes and updates crtc_state->mode_changed, adds any additional
347 * connectors needed for full modesets and calls down into ->mode_fixup
348 * functions of the driver backend.
349 *
350 * IMPORTANT:
351 *
352 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
353 * plane update can't be done without a full modeset) _must_ call this function
354 * afterwards after that change. It is permitted to call this function multiple
355 * times for the same update, e.g. when the ->atomic_check functions depend upon
356 * the adjusted dotclock for fifo space allocation and watermark computation.
357 *
358 * RETURNS
359 * Zero for success or -errno
360 */
361int
Rob Clark934ce1c2014-11-19 16:41:33 -0500362drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200363 struct drm_atomic_state *state)
364{
365 int ncrtcs = dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200366 struct drm_crtc *crtc;
367 struct drm_crtc_state *crtc_state;
368 int i, ret;
369
370 for (i = 0; i < ncrtcs; i++) {
371 crtc = state->crtcs[i];
372 crtc_state = state->crtc_states[i];
373
374 if (!crtc)
375 continue;
376
377 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
378 DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
379 crtc->base.id);
380 crtc_state->mode_changed = true;
381 }
382
383 if (crtc->state->enable != crtc_state->enable) {
384 DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
385 crtc->base.id);
386 crtc_state->mode_changed = true;
387 }
388 }
389
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100390 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200391 /*
392 * This only sets crtc->mode_changed for routing changes,
393 * drivers must set crtc->mode_changed themselves when connector
394 * properties need to be updated.
395 */
396 ret = update_connector_routing(state, i);
397 if (ret)
398 return ret;
399 }
400
401 /*
402 * After all the routing has been prepared we need to add in any
403 * connector which is itself unchanged, but who's crtc changes it's
404 * configuration. This must be done before calling mode_fixup in case a
405 * crtc only changed its mode but has the same set of connectors.
406 */
407 for (i = 0; i < ncrtcs; i++) {
408 int num_connectors;
409
410 crtc = state->crtcs[i];
411 crtc_state = state->crtc_states[i];
412
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100413 if (!crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200414 continue;
415
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100416 /*
417 * We must set ->active_changed after walking connectors for
418 * otherwise an update that only changes active would result in
419 * a full modeset because update_connector_routing force that.
420 */
421 if (crtc->state->active != crtc_state->active) {
422 DRM_DEBUG_KMS("[CRTC:%d] active changed\n",
423 crtc->base.id);
424 crtc_state->active_changed = true;
425 }
426
427 if (!needs_modeset(crtc_state))
428 continue;
429
430 DRM_DEBUG_KMS("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200431 crtc->base.id,
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100432 crtc_state->enable ? 'y' : 'n',
433 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200434
435 ret = drm_atomic_add_affected_connectors(state, crtc);
436 if (ret != 0)
437 return ret;
438
439 num_connectors = drm_atomic_connectors_for_crtc(state,
440 crtc);
441
442 if (crtc_state->enable != !!num_connectors) {
443 DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
444 crtc->base.id);
445
446 return -EINVAL;
447 }
448 }
449
450 return mode_fixup(state);
451}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100452EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200453
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100454/**
Daniel Vetterd9b13622014-11-26 16:57:41 +0100455 * drm_atomic_helper_check - validate state object for modeset changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100456 * @dev: DRM device
457 * @state: the driver state object
458 *
459 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100460 * This does all the plane update related checks using by calling into the
461 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100462 *
463 * RETURNS
464 * Zero for success or -errno
465 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100466int
467drm_atomic_helper_check_planes(struct drm_device *dev,
468 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100469{
470 int nplanes = dev->mode_config.num_total_plane;
471 int ncrtcs = dev->mode_config.num_crtc;
472 int i, ret = 0;
473
474 for (i = 0; i < nplanes; i++) {
475 struct drm_plane_helper_funcs *funcs;
476 struct drm_plane *plane = state->planes[i];
477 struct drm_plane_state *plane_state = state->plane_states[i];
478
479 if (!plane)
480 continue;
481
482 funcs = plane->helper_private;
483
484 drm_atomic_helper_plane_changed(state, plane_state, plane);
485
486 if (!funcs || !funcs->atomic_check)
487 continue;
488
489 ret = funcs->atomic_check(plane, plane_state);
490 if (ret) {
Rob Clark5e743732014-12-18 16:01:51 -0500491 DRM_DEBUG_KMS("[PLANE:%d] atomic driver check failed\n",
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100492 plane->base.id);
493 return ret;
494 }
495 }
496
497 for (i = 0; i < ncrtcs; i++) {
498 struct drm_crtc_helper_funcs *funcs;
499 struct drm_crtc *crtc = state->crtcs[i];
500
501 if (!crtc)
502 continue;
503
504 funcs = crtc->helper_private;
505
506 if (!funcs || !funcs->atomic_check)
507 continue;
508
509 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
510 if (ret) {
Rob Clark5e743732014-12-18 16:01:51 -0500511 DRM_DEBUG_KMS("[CRTC:%d] atomic driver check failed\n",
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100512 crtc->base.id);
513 return ret;
514 }
515 }
516
Daniel Vetterd9b13622014-11-26 16:57:41 +0100517 return ret;
518}
519EXPORT_SYMBOL(drm_atomic_helper_check_planes);
520
521/**
522 * drm_atomic_helper_check - validate state object
523 * @dev: DRM device
524 * @state: the driver state object
525 *
526 * Check the state object to see if the requested state is physically possible.
527 * Only crtcs and planes have check callbacks, so for any additional (global)
528 * checking that a driver needs it can simply wrap that around this function.
529 * Drivers without such needs can directly use this as their ->atomic_check()
530 * callback.
531 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100532 * This just wraps the two parts of the state checking for planes and modeset
533 * state in the default order: First it calls drm_atomic_helper_check_modeset()
534 * and then drm_atomic_helper_check_planes(). The assumption is that the
535 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
536 * e.g. properly compute watermarks.
537 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100538 * RETURNS
539 * Zero for success or -errno
540 */
541int drm_atomic_helper_check(struct drm_device *dev,
542 struct drm_atomic_state *state)
543{
544 int ret;
545
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100546 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100547 if (ret)
548 return ret;
549
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100550 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500551 if (ret)
552 return ret;
553
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100554 return ret;
555}
556EXPORT_SYMBOL(drm_atomic_helper_check);
557
Daniel Vetter623369e2014-09-16 17:50:47 +0200558static void
559disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
560{
561 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200562 int i;
563
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100564 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200565 struct drm_connector_state *old_conn_state;
566 struct drm_connector *connector;
567 struct drm_encoder_helper_funcs *funcs;
568 struct drm_encoder *encoder;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100569 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200570
571 old_conn_state = old_state->connector_states[i];
572 connector = old_state->connectors[i];
573
574 /* Shut down everything that's in the changeset and currently
575 * still on. So need to check the old, saved state. */
576 if (!old_conn_state || !old_conn_state->crtc)
577 continue;
578
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100579 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
580
581 if (!old_crtc_state->active)
582 continue;
583
Rob Clark46df9ad2014-11-20 15:40:36 -0500584 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200585
Rob Clark46df9ad2014-11-20 15:40:36 -0500586 /* We shouldn't get this far if we didn't previously have
587 * an encoder.. but WARN_ON() rather than explode.
588 */
589 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200590 continue;
591
592 funcs = encoder->helper_private;
593
594 /*
595 * Each encoder has at most one connector (since we always steal
596 * it away), so we won't call call disable hooks twice.
597 */
598 if (encoder->bridge)
599 encoder->bridge->funcs->disable(encoder->bridge);
600
601 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100602 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200603 funcs->prepare(encoder);
604 else if (funcs->disable)
605 funcs->disable(encoder);
606 else
607 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
608
609 if (encoder->bridge)
610 encoder->bridge->funcs->post_disable(encoder->bridge);
611 }
612
613 for (i = 0; i < ncrtcs; i++) {
614 struct drm_crtc_helper_funcs *funcs;
615 struct drm_crtc *crtc;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100616 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200617
618 crtc = old_state->crtcs[i];
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100619 old_crtc_state = old_state->crtc_states[i];
Daniel Vetter623369e2014-09-16 17:50:47 +0200620
621 /* Shut down everything that needs a full modeset. */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100622 if (!crtc || !needs_modeset(crtc->state))
623 continue;
624
625 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200626 continue;
627
628 funcs = crtc->helper_private;
629
630 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100631 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200632 funcs->prepare(crtc);
633 else if (funcs->disable)
634 funcs->disable(crtc);
635 else
636 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
637 }
638}
639
640static void
641set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
642{
Daniel Vetter623369e2014-09-16 17:50:47 +0200643 int ncrtcs = old_state->dev->mode_config.num_crtc;
644 int i;
645
646 /* clear out existing links */
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100647 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200648 struct drm_connector *connector;
649
650 connector = old_state->connectors[i];
651
652 if (!connector || !connector->encoder)
653 continue;
654
655 WARN_ON(!connector->encoder->crtc);
656
657 connector->encoder->crtc = NULL;
658 connector->encoder = NULL;
659 }
660
661 /* set new links */
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100662 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200663 struct drm_connector *connector;
664
665 connector = old_state->connectors[i];
666
667 if (!connector || !connector->state->crtc)
668 continue;
669
670 if (WARN_ON(!connector->state->best_encoder))
671 continue;
672
673 connector->encoder = connector->state->best_encoder;
674 connector->encoder->crtc = connector->state->crtc;
675 }
676
677 /* set legacy state in the crtc structure */
678 for (i = 0; i < ncrtcs; i++) {
679 struct drm_crtc *crtc;
680
681 crtc = old_state->crtcs[i];
682
683 if (!crtc)
684 continue;
685
686 crtc->mode = crtc->state->mode;
687 crtc->enabled = crtc->state->enable;
688 crtc->x = crtc->primary->state->src_x >> 16;
689 crtc->y = crtc->primary->state->src_y >> 16;
690 }
691}
692
693static void
694crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
695{
696 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200697 int i;
698
699 for (i = 0; i < ncrtcs; i++) {
700 struct drm_crtc_helper_funcs *funcs;
701 struct drm_crtc *crtc;
702
703 crtc = old_state->crtcs[i];
704
705 if (!crtc || !crtc->state->mode_changed)
706 continue;
707
708 funcs = crtc->helper_private;
709
710 if (crtc->state->enable)
711 funcs->mode_set_nofb(crtc);
712 }
713
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100714 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200715 struct drm_connector *connector;
716 struct drm_crtc_state *new_crtc_state;
717 struct drm_encoder_helper_funcs *funcs;
718 struct drm_encoder *encoder;
719 struct drm_display_mode *mode, *adjusted_mode;
720
721 connector = old_state->connectors[i];
722
723 if (!connector || !connector->state->best_encoder)
724 continue;
725
726 encoder = connector->state->best_encoder;
727 funcs = encoder->helper_private;
728 new_crtc_state = connector->state->crtc->state;
729 mode = &new_crtc_state->mode;
730 adjusted_mode = &new_crtc_state->adjusted_mode;
731
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100732 if (!new_crtc_state->mode_changed)
733 continue;
734
Daniel Vetter623369e2014-09-16 17:50:47 +0200735 /*
736 * Each encoder has at most one connector (since we always steal
737 * it away), so we won't call call mode_set hooks twice.
738 */
739 funcs->mode_set(encoder, mode, adjusted_mode);
740
741 if (encoder->bridge && encoder->bridge->funcs->mode_set)
742 encoder->bridge->funcs->mode_set(encoder->bridge,
743 mode, adjusted_mode);
744 }
745}
746
747/**
748 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
749 * @dev: DRM device
750 * @state: atomic state
751 *
752 * This function commits the modeset changes that need to be committed before
753 * updating planes. It shuts down all the outputs that need to be shut down and
754 * prepares them (if required) with the new mode.
755 */
756void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
757 struct drm_atomic_state *state)
758{
759 disable_outputs(dev, state);
760 set_routing_links(dev, state);
761 crtc_set_mode(dev, state);
762}
763EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
764
765/**
766 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
767 * @dev: DRM device
768 * @old_state: atomic state object with old state structures
769 *
770 * This function commits the modeset changes that need to be committed after
771 * updating planes: It enables all the outputs with the new configuration which
772 * had to be turned off for the update.
773 */
774void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
775 struct drm_atomic_state *old_state)
776{
777 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200778 int i;
779
780 for (i = 0; i < ncrtcs; i++) {
781 struct drm_crtc_helper_funcs *funcs;
782 struct drm_crtc *crtc;
783
784 crtc = old_state->crtcs[i];
785
786 /* Need to filter out CRTCs where only planes change. */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100787 if (!crtc || !needs_modeset(crtc->state))
788 continue;
789
790 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200791 continue;
792
793 funcs = crtc->helper_private;
794
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100795 if (crtc->state->enable) {
796 if (funcs->enable)
797 funcs->enable(crtc);
798 else
799 funcs->commit(crtc);
800 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200801 }
802
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100803 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200804 struct drm_connector *connector;
805 struct drm_encoder_helper_funcs *funcs;
806 struct drm_encoder *encoder;
807
808 connector = old_state->connectors[i];
809
810 if (!connector || !connector->state->best_encoder)
811 continue;
812
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100813 if (!connector->state->crtc->state->active)
814 continue;
815
Daniel Vetter623369e2014-09-16 17:50:47 +0200816 encoder = connector->state->best_encoder;
817 funcs = encoder->helper_private;
818
819 /*
820 * Each encoder has at most one connector (since we always steal
821 * it away), so we won't call call enable hooks twice.
822 */
823 if (encoder->bridge)
824 encoder->bridge->funcs->pre_enable(encoder->bridge);
825
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100826 if (funcs->enable)
827 funcs->enable(encoder);
828 else
829 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200830
831 if (encoder->bridge)
832 encoder->bridge->funcs->enable(encoder->bridge);
833 }
834}
835EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
836
Daniel Vettere2330f02014-10-29 11:34:56 +0100837static void wait_for_fences(struct drm_device *dev,
838 struct drm_atomic_state *state)
839{
840 int nplanes = dev->mode_config.num_total_plane;
841 int i;
842
843 for (i = 0; i < nplanes; i++) {
844 struct drm_plane *plane = state->planes[i];
845
846 if (!plane || !plane->state->fence)
847 continue;
848
849 WARN_ON(!plane->state->fb);
850
851 fence_wait(plane->state->fence, false);
852 fence_put(plane->state->fence);
853 plane->state->fence = NULL;
854 }
855}
856
Daniel Vetterab58e332014-11-24 20:42:42 +0100857static bool framebuffer_changed(struct drm_device *dev,
858 struct drm_atomic_state *old_state,
859 struct drm_crtc *crtc)
860{
861 struct drm_plane *plane;
862 struct drm_plane_state *old_plane_state;
863 int nplanes = old_state->dev->mode_config.num_total_plane;
864 int i;
865
866 for (i = 0; i < nplanes; i++) {
867 plane = old_state->planes[i];
868 old_plane_state = old_state->plane_states[i];
869
870 if (!plane)
871 continue;
872
873 if (plane->state->crtc != crtc &&
874 old_plane_state->crtc != crtc)
875 continue;
876
877 if (plane->state->fb != old_plane_state->fb)
878 return true;
879 }
880
881 return false;
882}
883
Rob Clark5ee32292014-11-11 19:38:59 -0500884/**
885 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
886 * @dev: DRM device
887 * @old_state: atomic state object with old state structures
888 *
889 * Helper to, after atomic commit, wait for vblanks on all effected
890 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100891 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
892 * framebuffers have actually changed to optimize for the legacy cursor and
893 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500894 */
895void
896drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
897 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200898{
899 struct drm_crtc *crtc;
900 struct drm_crtc_state *old_crtc_state;
901 int ncrtcs = old_state->dev->mode_config.num_crtc;
902 int i, ret;
903
904 for (i = 0; i < ncrtcs; i++) {
905 crtc = old_state->crtcs[i];
906 old_crtc_state = old_state->crtc_states[i];
907
908 if (!crtc)
909 continue;
910
911 /* No one cares about the old state, so abuse it for tracking
912 * and store whether we hold a vblank reference (and should do a
913 * vblank wait) in the ->enable boolean. */
914 old_crtc_state->enable = false;
915
916 if (!crtc->state->enable)
917 continue;
918
Daniel Vetterf02ad902015-01-22 16:36:23 +0100919 /* Legacy cursor ioctls are completely unsynced, and userspace
920 * relies on that (by doing tons of cursor updates). */
921 if (old_state->legacy_cursor_update)
922 continue;
923
Daniel Vetterab58e332014-11-24 20:42:42 +0100924 if (!framebuffer_changed(dev, old_state, crtc))
925 continue;
926
Daniel Vetter623369e2014-09-16 17:50:47 +0200927 ret = drm_crtc_vblank_get(crtc);
928 if (ret != 0)
929 continue;
930
931 old_crtc_state->enable = true;
932 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
933 }
934
935 for (i = 0; i < ncrtcs; i++) {
936 crtc = old_state->crtcs[i];
937 old_crtc_state = old_state->crtc_states[i];
938
939 if (!crtc || !old_crtc_state->enable)
940 continue;
941
942 ret = wait_event_timeout(dev->vblank[i].queue,
943 old_crtc_state->last_vblank_count !=
944 drm_vblank_count(dev, i),
945 msecs_to_jiffies(50));
946
947 drm_crtc_vblank_put(crtc);
948 }
949}
Rob Clark5ee32292014-11-11 19:38:59 -0500950EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +0200951
952/**
953 * drm_atomic_helper_commit - commit validated state object
954 * @dev: DRM device
955 * @state: the driver state object
956 * @async: asynchronous commit
957 *
958 * This function commits a with drm_atomic_helper_check() pre-validated state
959 * object. This can still fail when e.g. the framebuffer reservation fails. For
960 * now this doesn't implement asynchronous commits.
961 *
962 * RETURNS
963 * Zero for success or -errno.
964 */
965int drm_atomic_helper_commit(struct drm_device *dev,
966 struct drm_atomic_state *state,
967 bool async)
968{
969 int ret;
970
971 if (async)
972 return -EBUSY;
973
974 ret = drm_atomic_helper_prepare_planes(dev, state);
975 if (ret)
976 return ret;
977
978 /*
979 * This is the point of no return - everything below never fails except
980 * when the hw goes bonghits. Which means we can commit the new state on
981 * the software side now.
982 */
983
984 drm_atomic_helper_swap_state(dev, state);
985
986 /*
987 * Everything below can be run asynchronously without the need to grab
988 * any modeset locks at all under one conditions: It must be guaranteed
989 * that the asynchronous work has either been cancelled (if the driver
990 * supports it, which at least requires that the framebuffers get
991 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
992 * before the new state gets committed on the software side with
993 * drm_atomic_helper_swap_state().
994 *
995 * This scheme allows new atomic state updates to be prepared and
996 * checked in parallel to the asynchronous completion of the previous
997 * update. Which is important since compositors need to figure out the
998 * composition of the next frame right after having submitted the
999 * current layout.
1000 */
1001
Daniel Vettere2330f02014-10-29 11:34:56 +01001002 wait_for_fences(dev, state);
1003
Daniel Vetter623369e2014-09-16 17:50:47 +02001004 drm_atomic_helper_commit_pre_planes(dev, state);
1005
1006 drm_atomic_helper_commit_planes(dev, state);
1007
1008 drm_atomic_helper_commit_post_planes(dev, state);
1009
Rob Clark5ee32292014-11-11 19:38:59 -05001010 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001011
1012 drm_atomic_helper_cleanup_planes(dev, state);
1013
1014 drm_atomic_state_free(state);
1015
1016 return 0;
1017}
1018EXPORT_SYMBOL(drm_atomic_helper_commit);
1019
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001020/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001021 * DOC: implementing async commit
1022 *
1023 * For now the atomic helpers don't support async commit directly. If there is
1024 * real need it could be added though, using the dma-buf fence infrastructure
1025 * for generic synchronization with outstanding rendering.
1026 *
1027 * For now drivers have to implement async commit themselves, with the following
1028 * sequence being the recommended one:
1029 *
1030 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1031 * which commit needs to call which can fail, so we want to run it first and
1032 * synchronously.
1033 *
1034 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1035 * might be affected the new state update. This can be done by either cancelling
1036 * or flushing the work items, depending upon whether the driver can deal with
1037 * cancelled updates. Note that it is important to ensure that the framebuffer
1038 * cleanup is still done when cancelling.
1039 *
1040 * For sufficient parallelism it is recommended to have a work item per crtc
1041 * (for updates which don't touch global state) and a global one. Then we only
1042 * need to synchronize with the crtc work items for changed crtcs and the global
1043 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1044 *
1045 * 3. The software state is updated synchronously with
1046 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
1047 * locks means concurrent callers never see inconsistent state. And doing this
1048 * while it's guaranteed that no relevant async worker runs means that async
1049 * workers do not need grab any locks. Actually they must not grab locks, for
1050 * otherwise the work flushing will deadlock.
1051 *
1052 * 4. Schedule a work item to do all subsequent steps, using the split-out
1053 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1054 * then cleaning up the framebuffers after the old framebuffer is no longer
1055 * being displayed.
1056 */
1057
1058/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001059 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
1060 * @dev: DRM device
1061 * @state: atomic state object with old state structures
1062 *
1063 * This function prepares plane state, specifically framebuffers, for the new
1064 * configuration. If any failure is encountered this function will call
1065 * ->cleanup_fb on any already successfully prepared framebuffer.
1066 *
1067 * Returns:
1068 * 0 on success, negative error code on failure.
1069 */
1070int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1071 struct drm_atomic_state *state)
1072{
1073 int nplanes = dev->mode_config.num_total_plane;
1074 int ret, i;
1075
1076 for (i = 0; i < nplanes; i++) {
1077 struct drm_plane_helper_funcs *funcs;
1078 struct drm_plane *plane = state->planes[i];
1079 struct drm_framebuffer *fb;
1080
1081 if (!plane)
1082 continue;
1083
1084 funcs = plane->helper_private;
1085
1086 fb = state->plane_states[i]->fb;
1087
1088 if (fb && funcs->prepare_fb) {
1089 ret = funcs->prepare_fb(plane, fb);
1090 if (ret)
1091 goto fail;
1092 }
1093 }
1094
1095 return 0;
1096
1097fail:
1098 for (i--; i >= 0; i--) {
1099 struct drm_plane_helper_funcs *funcs;
1100 struct drm_plane *plane = state->planes[i];
1101 struct drm_framebuffer *fb;
1102
1103 if (!plane)
1104 continue;
1105
1106 funcs = plane->helper_private;
1107
1108 fb = state->plane_states[i]->fb;
1109
1110 if (fb && funcs->cleanup_fb)
1111 funcs->cleanup_fb(plane, fb);
1112
1113 }
1114
1115 return ret;
1116}
1117EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1118
1119/**
1120 * drm_atomic_helper_commit_planes - commit plane state
1121 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001122 * @old_state: atomic state object with old state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001123 *
1124 * This function commits the new plane state using the plane and atomic helper
1125 * functions for planes and crtcs. It assumes that the atomic state has already
1126 * been pushed into the relevant object state pointers, since this step can no
1127 * longer fail.
1128 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001129 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001130 * crtcs need to be updated though.
1131 */
1132void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001133 struct drm_atomic_state *old_state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001134{
1135 int nplanes = dev->mode_config.num_total_plane;
1136 int ncrtcs = dev->mode_config.num_crtc;
1137 int i;
1138
1139 for (i = 0; i < ncrtcs; i++) {
1140 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001141 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001142
1143 if (!crtc)
1144 continue;
1145
1146 funcs = crtc->helper_private;
1147
1148 if (!funcs || !funcs->atomic_begin)
1149 continue;
1150
1151 funcs->atomic_begin(crtc);
1152 }
1153
1154 for (i = 0; i < nplanes; i++) {
1155 struct drm_plane_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001156 struct drm_plane *plane = old_state->planes[i];
Thierry Redingf1c37e12014-11-25 12:09:44 +01001157 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001158
1159 if (!plane)
1160 continue;
1161
1162 funcs = plane->helper_private;
1163
1164 if (!funcs || !funcs->atomic_update)
1165 continue;
1166
Thierry Redingf1c37e12014-11-25 12:09:44 +01001167 old_plane_state = old_state->plane_states[i];
1168
1169 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001170 }
1171
1172 for (i = 0; i < ncrtcs; i++) {
1173 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001174 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001175
1176 if (!crtc)
1177 continue;
1178
1179 funcs = crtc->helper_private;
1180
1181 if (!funcs || !funcs->atomic_flush)
1182 continue;
1183
1184 funcs->atomic_flush(crtc);
1185 }
1186}
1187EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1188
1189/**
1190 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1191 * @dev: DRM device
1192 * @old_state: atomic state object with old state structures
1193 *
1194 * This function cleans up plane state, specifically framebuffers, from the old
1195 * configuration. Hence the old configuration must be perserved in @old_state to
1196 * be able to call this function.
1197 *
1198 * This function must also be called on the new state when the atomic update
1199 * fails at any point after calling drm_atomic_helper_prepare_planes().
1200 */
1201void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1202 struct drm_atomic_state *old_state)
1203{
1204 int nplanes = dev->mode_config.num_total_plane;
1205 int i;
1206
1207 for (i = 0; i < nplanes; i++) {
1208 struct drm_plane_helper_funcs *funcs;
1209 struct drm_plane *plane = old_state->planes[i];
1210 struct drm_framebuffer *old_fb;
1211
1212 if (!plane)
1213 continue;
1214
1215 funcs = plane->helper_private;
1216
1217 old_fb = old_state->plane_states[i]->fb;
1218
1219 if (old_fb && funcs->cleanup_fb)
1220 funcs->cleanup_fb(plane, old_fb);
1221 }
1222}
1223EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1224
1225/**
1226 * drm_atomic_helper_swap_state - store atomic state into current sw state
1227 * @dev: DRM device
1228 * @state: atomic state
1229 *
1230 * This function stores the atomic state into the current state pointers in all
1231 * driver objects. It should be called after all failing steps have been done
1232 * and succeeded, but before the actual hardware state is committed.
1233 *
1234 * For cleanup and error recovery the current state for all changed objects will
1235 * be swaped into @state.
1236 *
1237 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1238 *
1239 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1240 *
1241 * 2. Do any other steps that might fail.
1242 *
1243 * 3. Put the staged state into the current state pointers with this function.
1244 *
1245 * 4. Actually commit the hardware state.
1246 *
1247 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1248 * contains the old state. Also do any other cleanup required with that state.
1249 */
1250void drm_atomic_helper_swap_state(struct drm_device *dev,
1251 struct drm_atomic_state *state)
1252{
1253 int i;
1254
1255 for (i = 0; i < dev->mode_config.num_connector; i++) {
1256 struct drm_connector *connector = state->connectors[i];
1257
1258 if (!connector)
1259 continue;
1260
1261 connector->state->state = state;
1262 swap(state->connector_states[i], connector->state);
1263 connector->state->state = NULL;
1264 }
1265
1266 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1267 struct drm_crtc *crtc = state->crtcs[i];
1268
1269 if (!crtc)
1270 continue;
1271
1272 crtc->state->state = state;
1273 swap(state->crtc_states[i], crtc->state);
1274 crtc->state->state = NULL;
1275 }
1276
1277 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1278 struct drm_plane *plane = state->planes[i];
1279
1280 if (!plane)
1281 continue;
1282
1283 plane->state->state = state;
1284 swap(state->plane_states[i], plane->state);
1285 plane->state->state = NULL;
1286 }
1287}
1288EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001289
1290/**
1291 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1292 * @plane: plane object to update
1293 * @crtc: owning CRTC of owning plane
1294 * @fb: framebuffer to flip onto plane
1295 * @crtc_x: x offset of primary plane on crtc
1296 * @crtc_y: y offset of primary plane on crtc
1297 * @crtc_w: width of primary plane rectangle on crtc
1298 * @crtc_h: height of primary plane rectangle on crtc
1299 * @src_x: x offset of @fb for panning
1300 * @src_y: y offset of @fb for panning
1301 * @src_w: width of source rectangle in @fb
1302 * @src_h: height of source rectangle in @fb
1303 *
1304 * Provides a default plane update handler using the atomic driver interface.
1305 *
1306 * RETURNS:
1307 * Zero on success, error code on failure
1308 */
1309int drm_atomic_helper_update_plane(struct drm_plane *plane,
1310 struct drm_crtc *crtc,
1311 struct drm_framebuffer *fb,
1312 int crtc_x, int crtc_y,
1313 unsigned int crtc_w, unsigned int crtc_h,
1314 uint32_t src_x, uint32_t src_y,
1315 uint32_t src_w, uint32_t src_h)
1316{
1317 struct drm_atomic_state *state;
1318 struct drm_plane_state *plane_state;
1319 int ret = 0;
1320
1321 state = drm_atomic_state_alloc(plane->dev);
1322 if (!state)
1323 return -ENOMEM;
1324
1325 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1326retry:
1327 plane_state = drm_atomic_get_plane_state(state, plane);
1328 if (IS_ERR(plane_state)) {
1329 ret = PTR_ERR(plane_state);
1330 goto fail;
1331 }
1332
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001333 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001334 if (ret != 0)
1335 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001336 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001337 plane_state->crtc_x = crtc_x;
1338 plane_state->crtc_y = crtc_y;
1339 plane_state->crtc_h = crtc_h;
1340 plane_state->crtc_w = crtc_w;
1341 plane_state->src_x = src_x;
1342 plane_state->src_y = src_y;
1343 plane_state->src_h = src_h;
1344 plane_state->src_w = src_w;
1345
1346 ret = drm_atomic_commit(state);
1347 if (ret != 0)
1348 goto fail;
1349
Daniel Vetterf02ad902015-01-22 16:36:23 +01001350 if (plane == crtc->cursor)
1351 state->legacy_cursor_update = true;
1352
Daniel Vetter042652e2014-07-27 13:46:52 +02001353 /* Driver takes ownership of state on successful commit. */
1354 return 0;
1355fail:
1356 if (ret == -EDEADLK)
1357 goto backoff;
1358
1359 drm_atomic_state_free(state);
1360
1361 return ret;
1362backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001363 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001364 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001365
1366 /*
1367 * Someone might have exchanged the framebuffer while we dropped locks
1368 * in the backoff code. We need to fix up the fb refcount tracking the
1369 * core does for us.
1370 */
1371 plane->old_fb = plane->fb;
1372
1373 goto retry;
1374}
1375EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1376
1377/**
1378 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1379 * @plane: plane to disable
1380 *
1381 * Provides a default plane disable handler using the atomic driver interface.
1382 *
1383 * RETURNS:
1384 * Zero on success, error code on failure
1385 */
1386int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1387{
1388 struct drm_atomic_state *state;
1389 struct drm_plane_state *plane_state;
1390 int ret = 0;
1391
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001392 /*
1393 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1394 * acquire context. The real fix will be to wire the acquire ctx through
1395 * everywhere we need it, but meanwhile prevent chaos by just skipping
1396 * this noop. The critical case is the cursor ioctls which a) only grab
1397 * crtc/cursor-plane locks (so we need the crtc to get at the right
1398 * acquire context) and b) can try to disable the plane multiple times.
1399 */
1400 if (!plane->crtc)
1401 return 0;
1402
Daniel Vetter042652e2014-07-27 13:46:52 +02001403 state = drm_atomic_state_alloc(plane->dev);
1404 if (!state)
1405 return -ENOMEM;
1406
1407 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1408retry:
1409 plane_state = drm_atomic_get_plane_state(state, plane);
1410 if (IS_ERR(plane_state)) {
1411 ret = PTR_ERR(plane_state);
1412 goto fail;
1413 }
1414
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001415 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001416 if (ret != 0)
1417 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001418 drm_atomic_set_fb_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001419 plane_state->crtc_x = 0;
1420 plane_state->crtc_y = 0;
1421 plane_state->crtc_h = 0;
1422 plane_state->crtc_w = 0;
1423 plane_state->src_x = 0;
1424 plane_state->src_y = 0;
1425 plane_state->src_h = 0;
1426 plane_state->src_w = 0;
1427
Daniel Vetterf02ad902015-01-22 16:36:23 +01001428 if (plane == plane->crtc->cursor)
1429 state->legacy_cursor_update = true;
1430
Daniel Vetter042652e2014-07-27 13:46:52 +02001431 ret = drm_atomic_commit(state);
1432 if (ret != 0)
1433 goto fail;
1434
1435 /* Driver takes ownership of state on successful commit. */
1436 return 0;
1437fail:
1438 if (ret == -EDEADLK)
1439 goto backoff;
1440
1441 drm_atomic_state_free(state);
1442
1443 return ret;
1444backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001445 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001446 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001447
1448 /*
1449 * Someone might have exchanged the framebuffer while we dropped locks
1450 * in the backoff code. We need to fix up the fb refcount tracking the
1451 * core does for us.
1452 */
1453 plane->old_fb = plane->fb;
1454
1455 goto retry;
1456}
1457EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1458
1459static int update_output_state(struct drm_atomic_state *state,
1460 struct drm_mode_set *set)
1461{
1462 struct drm_device *dev = set->crtc->dev;
1463 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001464 int ncrtcs = state->dev->mode_config.num_crtc;
1465 int ret, i, j;
1466
1467 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1468 state->acquire_ctx);
1469 if (ret)
1470 return ret;
1471
1472 /* First grab all affected connector/crtc states. */
1473 for (i = 0; i < set->num_connectors; i++) {
1474 conn_state = drm_atomic_get_connector_state(state,
1475 set->connectors[i]);
1476 if (IS_ERR(conn_state))
1477 return PTR_ERR(conn_state);
1478 }
1479
1480 for (i = 0; i < ncrtcs; i++) {
1481 struct drm_crtc *crtc = state->crtcs[i];
1482
1483 if (!crtc)
1484 continue;
1485
1486 ret = drm_atomic_add_affected_connectors(state, crtc);
1487 if (ret)
1488 return ret;
1489 }
1490
1491 /* Then recompute connector->crtc links and crtc enabling state. */
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001492 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001493 struct drm_connector *connector;
1494
1495 connector = state->connectors[i];
1496 conn_state = state->connector_states[i];
1497
1498 if (!connector)
1499 continue;
1500
1501 if (conn_state->crtc == set->crtc) {
1502 ret = drm_atomic_set_crtc_for_connector(conn_state,
1503 NULL);
1504 if (ret)
1505 return ret;
1506 }
1507
1508 for (j = 0; j < set->num_connectors; j++) {
1509 if (set->connectors[j] == connector) {
1510 ret = drm_atomic_set_crtc_for_connector(conn_state,
1511 set->crtc);
1512 if (ret)
1513 return ret;
1514 break;
1515 }
1516 }
1517 }
1518
1519 for (i = 0; i < ncrtcs; i++) {
1520 struct drm_crtc *crtc = state->crtcs[i];
1521 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1522
1523 if (!crtc)
1524 continue;
1525
1526 /* Don't update ->enable for the CRTC in the set_config request,
1527 * since a mismatch would indicate a bug in the upper layers.
1528 * The actual modeset code later on will catch any
1529 * inconsistencies here. */
1530 if (crtc == set->crtc)
1531 continue;
1532
1533 crtc_state->enable =
1534 drm_atomic_connectors_for_crtc(state, crtc);
1535 }
1536
1537 return 0;
1538}
1539
1540/**
1541 * drm_atomic_helper_set_config - set a new config from userspace
1542 * @set: mode set configuration
1543 *
1544 * Provides a default crtc set_config handler using the atomic driver interface.
1545 *
1546 * Returns:
1547 * Returns 0 on success, negative errno numbers on failure.
1548 */
1549int drm_atomic_helper_set_config(struct drm_mode_set *set)
1550{
1551 struct drm_atomic_state *state;
1552 struct drm_crtc *crtc = set->crtc;
1553 struct drm_crtc_state *crtc_state;
1554 struct drm_plane_state *primary_state;
1555 int ret = 0;
1556
1557 state = drm_atomic_state_alloc(crtc->dev);
1558 if (!state)
1559 return -ENOMEM;
1560
1561 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1562retry:
1563 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1564 if (IS_ERR(crtc_state)) {
1565 ret = PTR_ERR(crtc_state);
1566 goto fail;
1567 }
1568
Rob Clarke5b53412014-11-26 18:58:04 -05001569 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1570 if (IS_ERR(primary_state)) {
1571 ret = PTR_ERR(primary_state);
1572 goto fail;
1573 }
1574
Daniel Vetter042652e2014-07-27 13:46:52 +02001575 if (!set->mode) {
1576 WARN_ON(set->fb);
1577 WARN_ON(set->num_connectors);
1578
1579 crtc_state->enable = false;
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001580 crtc_state->active = false;
Rob Clarke5b53412014-11-26 18:58:04 -05001581
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001582 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
Rob Clarke5b53412014-11-26 18:58:04 -05001583 if (ret != 0)
1584 goto fail;
1585
1586 drm_atomic_set_fb_for_plane(primary_state, NULL);
1587
Daniel Vetter042652e2014-07-27 13:46:52 +02001588 goto commit;
1589 }
1590
1591 WARN_ON(!set->fb);
1592 WARN_ON(!set->num_connectors);
1593
1594 crtc_state->enable = true;
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001595 crtc_state->active = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02001596 drm_mode_copy(&crtc_state->mode, set->mode);
1597
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001598 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001599 if (ret != 0)
1600 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001601 drm_atomic_set_fb_for_plane(primary_state, set->fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001602 primary_state->crtc_x = 0;
1603 primary_state->crtc_y = 0;
1604 primary_state->crtc_h = set->mode->vdisplay;
1605 primary_state->crtc_w = set->mode->hdisplay;
1606 primary_state->src_x = set->x << 16;
1607 primary_state->src_y = set->y << 16;
1608 primary_state->src_h = set->mode->vdisplay << 16;
1609 primary_state->src_w = set->mode->hdisplay << 16;
1610
1611commit:
1612 ret = update_output_state(state, set);
1613 if (ret)
1614 goto fail;
1615
1616 ret = drm_atomic_commit(state);
1617 if (ret != 0)
1618 goto fail;
1619
1620 /* Driver takes ownership of state on successful commit. */
1621 return 0;
1622fail:
1623 if (ret == -EDEADLK)
1624 goto backoff;
1625
1626 drm_atomic_state_free(state);
1627
1628 return ret;
1629backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001630 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001631 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001632
1633 /*
1634 * Someone might have exchanged the framebuffer while we dropped locks
1635 * in the backoff code. We need to fix up the fb refcount tracking the
1636 * core does for us.
1637 */
1638 crtc->primary->old_fb = crtc->primary->fb;
1639
1640 goto retry;
1641}
1642EXPORT_SYMBOL(drm_atomic_helper_set_config);
1643
1644/**
1645 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1646 * @crtc: DRM crtc
1647 * @property: DRM property
1648 * @val: value of property
1649 *
1650 * Provides a default plane disablle handler using the atomic driver interface.
1651 *
1652 * RETURNS:
1653 * Zero on success, error code on failure
1654 */
1655int
1656drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1657 struct drm_property *property,
1658 uint64_t val)
1659{
1660 struct drm_atomic_state *state;
1661 struct drm_crtc_state *crtc_state;
1662 int ret = 0;
1663
1664 state = drm_atomic_state_alloc(crtc->dev);
1665 if (!state)
1666 return -ENOMEM;
1667
1668 /* ->set_property is always called with all locks held. */
1669 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1670retry:
1671 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1672 if (IS_ERR(crtc_state)) {
1673 ret = PTR_ERR(crtc_state);
1674 goto fail;
1675 }
1676
Rob Clark40ecc692014-12-18 16:01:46 -05001677 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1678 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001679 if (ret)
1680 goto fail;
1681
1682 ret = drm_atomic_commit(state);
1683 if (ret != 0)
1684 goto fail;
1685
1686 /* Driver takes ownership of state on successful commit. */
1687 return 0;
1688fail:
1689 if (ret == -EDEADLK)
1690 goto backoff;
1691
1692 drm_atomic_state_free(state);
1693
1694 return ret;
1695backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001696 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001697 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001698
1699 goto retry;
1700}
1701EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1702
1703/**
1704 * drm_atomic_helper_plane_set_property - helper for plane prorties
1705 * @plane: DRM plane
1706 * @property: DRM property
1707 * @val: value of property
1708 *
1709 * Provides a default plane disable handler using the atomic driver interface.
1710 *
1711 * RETURNS:
1712 * Zero on success, error code on failure
1713 */
1714int
1715drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1716 struct drm_property *property,
1717 uint64_t val)
1718{
1719 struct drm_atomic_state *state;
1720 struct drm_plane_state *plane_state;
1721 int ret = 0;
1722
1723 state = drm_atomic_state_alloc(plane->dev);
1724 if (!state)
1725 return -ENOMEM;
1726
1727 /* ->set_property is always called with all locks held. */
1728 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1729retry:
1730 plane_state = drm_atomic_get_plane_state(state, plane);
1731 if (IS_ERR(plane_state)) {
1732 ret = PTR_ERR(plane_state);
1733 goto fail;
1734 }
1735
Rob Clark40ecc692014-12-18 16:01:46 -05001736 ret = drm_atomic_plane_set_property(plane, plane_state,
1737 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001738 if (ret)
1739 goto fail;
1740
1741 ret = drm_atomic_commit(state);
1742 if (ret != 0)
1743 goto fail;
1744
1745 /* Driver takes ownership of state on successful commit. */
1746 return 0;
1747fail:
1748 if (ret == -EDEADLK)
1749 goto backoff;
1750
1751 drm_atomic_state_free(state);
1752
1753 return ret;
1754backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001755 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001756 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001757
1758 goto retry;
1759}
1760EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1761
1762/**
1763 * drm_atomic_helper_connector_set_property - helper for connector prorties
1764 * @connector: DRM connector
1765 * @property: DRM property
1766 * @val: value of property
1767 *
1768 * Provides a default plane disablle handler using the atomic driver interface.
1769 *
1770 * RETURNS:
1771 * Zero on success, error code on failure
1772 */
1773int
1774drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1775 struct drm_property *property,
1776 uint64_t val)
1777{
1778 struct drm_atomic_state *state;
1779 struct drm_connector_state *connector_state;
1780 int ret = 0;
1781
1782 state = drm_atomic_state_alloc(connector->dev);
1783 if (!state)
1784 return -ENOMEM;
1785
1786 /* ->set_property is always called with all locks held. */
1787 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1788retry:
1789 connector_state = drm_atomic_get_connector_state(state, connector);
1790 if (IS_ERR(connector_state)) {
1791 ret = PTR_ERR(connector_state);
1792 goto fail;
1793 }
1794
Rob Clark40ecc692014-12-18 16:01:46 -05001795 ret = drm_atomic_connector_set_property(connector, connector_state,
1796 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001797 if (ret)
1798 goto fail;
1799
1800 ret = drm_atomic_commit(state);
1801 if (ret != 0)
1802 goto fail;
1803
1804 /* Driver takes ownership of state on successful commit. */
1805 return 0;
1806fail:
1807 if (ret == -EDEADLK)
1808 goto backoff;
1809
1810 drm_atomic_state_free(state);
1811
1812 return ret;
1813backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001814 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001815 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001816
1817 goto retry;
1818}
1819EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001820
1821/**
1822 * drm_atomic_helper_page_flip - execute a legacy page flip
1823 * @crtc: DRM crtc
1824 * @fb: DRM framebuffer
1825 * @event: optional DRM event to signal upon completion
1826 * @flags: flip flags for non-vblank sync'ed updates
1827 *
1828 * Provides a default page flip implementation using the atomic driver interface.
1829 *
1830 * Note that for now so called async page flips (i.e. updates which are not
1831 * synchronized to vblank) are not supported, since the atomic interfaces have
1832 * no provisions for this yet.
1833 *
1834 * Returns:
1835 * Returns 0 on success, negative errno numbers on failure.
1836 */
1837int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1838 struct drm_framebuffer *fb,
1839 struct drm_pending_vblank_event *event,
1840 uint32_t flags)
1841{
1842 struct drm_plane *plane = crtc->primary;
1843 struct drm_atomic_state *state;
1844 struct drm_plane_state *plane_state;
1845 struct drm_crtc_state *crtc_state;
1846 int ret = 0;
1847
1848 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1849 return -EINVAL;
1850
1851 state = drm_atomic_state_alloc(plane->dev);
1852 if (!state)
1853 return -ENOMEM;
1854
1855 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1856retry:
1857 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1858 if (IS_ERR(crtc_state)) {
1859 ret = PTR_ERR(crtc_state);
1860 goto fail;
1861 }
1862 crtc_state->event = event;
1863
1864 plane_state = drm_atomic_get_plane_state(state, plane);
1865 if (IS_ERR(plane_state)) {
1866 ret = PTR_ERR(plane_state);
1867 goto fail;
1868 }
1869
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001870 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001871 if (ret != 0)
1872 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001873 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001874
1875 ret = drm_atomic_async_commit(state);
1876 if (ret != 0)
1877 goto fail;
1878
1879 /* TODO: ->page_flip is the only driver callback where the core
1880 * doesn't update plane->fb. For now patch it up here. */
1881 plane->fb = plane->state->fb;
1882
1883 /* Driver takes ownership of state on successful async commit. */
1884 return 0;
1885fail:
1886 if (ret == -EDEADLK)
1887 goto backoff;
1888
1889 drm_atomic_state_free(state);
1890
1891 return ret;
1892backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001893 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001894 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001895
1896 /*
1897 * Someone might have exchanged the framebuffer while we dropped locks
1898 * in the backoff code. We need to fix up the fb refcount tracking the
1899 * core does for us.
1900 */
1901 plane->old_fb = plane->fb;
1902
1903 goto retry;
1904}
1905EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01001906
1907/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01001908 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
1909 * @connector: affected connector
1910 * @mode: DPMS mode
1911 *
1912 * This is the main helper function provided by the atomic helper framework for
1913 * implementing the legacy DPMS connector interface. It computes the new desired
1914 * ->active state for the corresponding CRTC (if the connector is enabled) and
1915 * updates it.
1916 */
1917void drm_atomic_helper_connector_dpms(struct drm_connector *connector,
1918 int mode)
1919{
1920 struct drm_mode_config *config = &connector->dev->mode_config;
1921 struct drm_atomic_state *state;
1922 struct drm_crtc_state *crtc_state;
1923 struct drm_crtc *crtc;
1924 struct drm_connector *tmp_connector;
1925 int ret;
1926 bool active = false;
1927
1928 if (mode != DRM_MODE_DPMS_ON)
1929 mode = DRM_MODE_DPMS_OFF;
1930
1931 connector->dpms = mode;
1932 crtc = connector->state->crtc;
1933
1934 if (!crtc)
1935 return;
1936
1937 /* FIXME: ->dpms has no return value so can't forward the -ENOMEM. */
1938 state = drm_atomic_state_alloc(connector->dev);
1939 if (!state)
1940 return;
1941
1942 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1943retry:
1944 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1945 if (IS_ERR(crtc_state))
1946 return;
1947
1948 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
1949
1950 list_for_each_entry(tmp_connector, &config->connector_list, head) {
1951 if (connector->state->crtc != crtc)
1952 continue;
1953
1954 if (connector->dpms == DRM_MODE_DPMS_ON) {
1955 active = true;
1956 break;
1957 }
1958 }
1959 crtc_state->active = active;
1960
1961 ret = drm_atomic_commit(state);
1962 if (ret != 0)
1963 goto fail;
1964
1965 /* Driver takes ownership of state on successful async commit. */
1966 return;
1967fail:
1968 if (ret == -EDEADLK)
1969 goto backoff;
1970
1971 drm_atomic_state_free(state);
1972
1973 WARN(1, "Driver bug: Changing ->active failed with ret=%i\n", ret);
1974
1975 return;
1976backoff:
1977 drm_atomic_state_clear(state);
1978 drm_atomic_legacy_backoff(state);
1979
1980 goto retry;
1981}
1982EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
1983
1984/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01001985 * DOC: atomic state reset and initialization
1986 *
1987 * Both the drm core and the atomic helpers assume that there is always the full
1988 * and correct atomic software state for all connectors, CRTCs and planes
1989 * available. Which is a bit a problem on driver load and also after system
1990 * suspend. One way to solve this is to have a hardware state read-out
1991 * infrastructure which reconstructs the full software state (e.g. the i915
1992 * driver).
1993 *
1994 * The simpler solution is to just reset the software state to everything off,
1995 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
1996 * the atomic helpers provide default reset implementations for all hooks.
1997 */
1998
1999/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002000 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2001 * @crtc: drm CRTC
2002 *
2003 * Resets the atomic state for @crtc by freeing the state pointer (which might
2004 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2005 */
2006void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2007{
2008 kfree(crtc->state);
2009 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002010
2011 if (crtc->state)
2012 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002013}
2014EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2015
2016/**
2017 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2018 * @crtc: drm CRTC
2019 *
2020 * Default CRTC state duplicate hook for drivers which don't have their own
2021 * subclassed CRTC state structure.
2022 */
2023struct drm_crtc_state *
2024drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2025{
2026 struct drm_crtc_state *state;
2027
2028 if (WARN_ON(!crtc->state))
2029 return NULL;
2030
2031 state = kmemdup(crtc->state, sizeof(*crtc->state), GFP_KERNEL);
2032
2033 if (state) {
2034 state->mode_changed = false;
Daniel Vettereab3bbe2015-01-22 16:36:21 +01002035 state->active_changed = false;
Daniel Vetterd4617012014-11-03 15:56:43 +01002036 state->planes_changed = false;
2037 state->event = NULL;
2038 }
2039
2040 return state;
2041}
2042EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2043
2044/**
2045 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2046 * @crtc: drm CRTC
2047 * @state: CRTC state object to release
2048 *
2049 * Default CRTC state destroy hook for drivers which don't have their own
2050 * subclassed CRTC state structure.
2051 */
2052void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2053 struct drm_crtc_state *state)
2054{
2055 kfree(state);
2056}
2057EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2058
2059/**
2060 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2061 * @plane: drm plane
2062 *
2063 * Resets the atomic state for @plane by freeing the state pointer (which might
2064 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2065 */
2066void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2067{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002068 if (plane->state && plane->state->fb)
2069 drm_framebuffer_unreference(plane->state->fb);
2070
Daniel Vetterd4617012014-11-03 15:56:43 +01002071 kfree(plane->state);
2072 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002073
2074 if (plane->state)
2075 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01002076}
2077EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2078
2079/**
2080 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2081 * @plane: drm plane
2082 *
2083 * Default plane state duplicate hook for drivers which don't have their own
2084 * subclassed plane state structure.
2085 */
2086struct drm_plane_state *
2087drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2088{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002089 struct drm_plane_state *state;
2090
Daniel Vetterd4617012014-11-03 15:56:43 +01002091 if (WARN_ON(!plane->state))
2092 return NULL;
2093
Daniel Vetter321ebf02014-11-04 22:57:27 +01002094 state = kmemdup(plane->state, sizeof(*plane->state), GFP_KERNEL);
2095
2096 if (state && state->fb)
2097 drm_framebuffer_reference(state->fb);
2098
2099 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002100}
2101EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2102
2103/**
2104 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2105 * @plane: drm plane
2106 * @state: plane state object to release
2107 *
2108 * Default plane state destroy hook for drivers which don't have their own
2109 * subclassed plane state structure.
2110 */
2111void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002112 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002113{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002114 if (state->fb)
2115 drm_framebuffer_unreference(state->fb);
2116
Daniel Vetterd4617012014-11-03 15:56:43 +01002117 kfree(state);
2118}
2119EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2120
2121/**
2122 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2123 * @connector: drm connector
2124 *
2125 * Resets the atomic state for @connector by freeing the state pointer (which
2126 * might be NULL, e.g. at driver load time) and allocating a new empty state
2127 * object.
2128 */
2129void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2130{
2131 kfree(connector->state);
2132 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002133
2134 if (connector->state)
2135 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002136}
2137EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2138
2139/**
2140 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2141 * @connector: drm connector
2142 *
2143 * Default connector state duplicate hook for drivers which don't have their own
2144 * subclassed connector state structure.
2145 */
2146struct drm_connector_state *
2147drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2148{
2149 if (WARN_ON(!connector->state))
2150 return NULL;
2151
2152 return kmemdup(connector->state, sizeof(*connector->state), GFP_KERNEL);
2153}
2154EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2155
2156/**
2157 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2158 * @connector: drm connector
2159 * @state: connector state object to release
2160 *
2161 * Default connector state destroy hook for drivers which don't have their own
2162 * subclassed connector state structure.
2163 */
2164void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2165 struct drm_connector_state *state)
2166{
2167 kfree(state);
2168}
2169EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);