blob: 3f17933b1d2c8ada48d711dd890088161ad46627 [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. */
602 if (connector->state->crtc)
603 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. */
631 if (crtc->state->enable)
632 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
795 if (crtc->state->enable)
796 funcs->commit(crtc);
797 }
798
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100799 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200800 struct drm_connector *connector;
801 struct drm_encoder_helper_funcs *funcs;
802 struct drm_encoder *encoder;
803
804 connector = old_state->connectors[i];
805
806 if (!connector || !connector->state->best_encoder)
807 continue;
808
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100809 if (!connector->state->crtc->state->active)
810 continue;
811
Daniel Vetter623369e2014-09-16 17:50:47 +0200812 encoder = connector->state->best_encoder;
813 funcs = encoder->helper_private;
814
815 /*
816 * Each encoder has at most one connector (since we always steal
817 * it away), so we won't call call enable hooks twice.
818 */
819 if (encoder->bridge)
820 encoder->bridge->funcs->pre_enable(encoder->bridge);
821
822 funcs->commit(encoder);
823
824 if (encoder->bridge)
825 encoder->bridge->funcs->enable(encoder->bridge);
826 }
827}
828EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
829
Daniel Vettere2330f02014-10-29 11:34:56 +0100830static void wait_for_fences(struct drm_device *dev,
831 struct drm_atomic_state *state)
832{
833 int nplanes = dev->mode_config.num_total_plane;
834 int i;
835
836 for (i = 0; i < nplanes; i++) {
837 struct drm_plane *plane = state->planes[i];
838
839 if (!plane || !plane->state->fence)
840 continue;
841
842 WARN_ON(!plane->state->fb);
843
844 fence_wait(plane->state->fence, false);
845 fence_put(plane->state->fence);
846 plane->state->fence = NULL;
847 }
848}
849
Daniel Vetterab58e332014-11-24 20:42:42 +0100850static bool framebuffer_changed(struct drm_device *dev,
851 struct drm_atomic_state *old_state,
852 struct drm_crtc *crtc)
853{
854 struct drm_plane *plane;
855 struct drm_plane_state *old_plane_state;
856 int nplanes = old_state->dev->mode_config.num_total_plane;
857 int i;
858
859 for (i = 0; i < nplanes; i++) {
860 plane = old_state->planes[i];
861 old_plane_state = old_state->plane_states[i];
862
863 if (!plane)
864 continue;
865
866 if (plane->state->crtc != crtc &&
867 old_plane_state->crtc != crtc)
868 continue;
869
870 if (plane->state->fb != old_plane_state->fb)
871 return true;
872 }
873
874 return false;
875}
876
Rob Clark5ee32292014-11-11 19:38:59 -0500877/**
878 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
879 * @dev: DRM device
880 * @old_state: atomic state object with old state structures
881 *
882 * Helper to, after atomic commit, wait for vblanks on all effected
883 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100884 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
885 * framebuffers have actually changed to optimize for the legacy cursor and
886 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500887 */
888void
889drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
890 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200891{
892 struct drm_crtc *crtc;
893 struct drm_crtc_state *old_crtc_state;
894 int ncrtcs = old_state->dev->mode_config.num_crtc;
895 int i, ret;
896
897 for (i = 0; i < ncrtcs; i++) {
898 crtc = old_state->crtcs[i];
899 old_crtc_state = old_state->crtc_states[i];
900
901 if (!crtc)
902 continue;
903
904 /* No one cares about the old state, so abuse it for tracking
905 * and store whether we hold a vblank reference (and should do a
906 * vblank wait) in the ->enable boolean. */
907 old_crtc_state->enable = false;
908
909 if (!crtc->state->enable)
910 continue;
911
Daniel Vetterab58e332014-11-24 20:42:42 +0100912 if (!framebuffer_changed(dev, old_state, crtc))
913 continue;
914
Daniel Vetter623369e2014-09-16 17:50:47 +0200915 ret = drm_crtc_vblank_get(crtc);
916 if (ret != 0)
917 continue;
918
919 old_crtc_state->enable = true;
920 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
921 }
922
923 for (i = 0; i < ncrtcs; i++) {
924 crtc = old_state->crtcs[i];
925 old_crtc_state = old_state->crtc_states[i];
926
927 if (!crtc || !old_crtc_state->enable)
928 continue;
929
930 ret = wait_event_timeout(dev->vblank[i].queue,
931 old_crtc_state->last_vblank_count !=
932 drm_vblank_count(dev, i),
933 msecs_to_jiffies(50));
934
935 drm_crtc_vblank_put(crtc);
936 }
937}
Rob Clark5ee32292014-11-11 19:38:59 -0500938EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +0200939
940/**
941 * drm_atomic_helper_commit - commit validated state object
942 * @dev: DRM device
943 * @state: the driver state object
944 * @async: asynchronous commit
945 *
946 * This function commits a with drm_atomic_helper_check() pre-validated state
947 * object. This can still fail when e.g. the framebuffer reservation fails. For
948 * now this doesn't implement asynchronous commits.
949 *
950 * RETURNS
951 * Zero for success or -errno.
952 */
953int drm_atomic_helper_commit(struct drm_device *dev,
954 struct drm_atomic_state *state,
955 bool async)
956{
957 int ret;
958
959 if (async)
960 return -EBUSY;
961
962 ret = drm_atomic_helper_prepare_planes(dev, state);
963 if (ret)
964 return ret;
965
966 /*
967 * This is the point of no return - everything below never fails except
968 * when the hw goes bonghits. Which means we can commit the new state on
969 * the software side now.
970 */
971
972 drm_atomic_helper_swap_state(dev, state);
973
974 /*
975 * Everything below can be run asynchronously without the need to grab
976 * any modeset locks at all under one conditions: It must be guaranteed
977 * that the asynchronous work has either been cancelled (if the driver
978 * supports it, which at least requires that the framebuffers get
979 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
980 * before the new state gets committed on the software side with
981 * drm_atomic_helper_swap_state().
982 *
983 * This scheme allows new atomic state updates to be prepared and
984 * checked in parallel to the asynchronous completion of the previous
985 * update. Which is important since compositors need to figure out the
986 * composition of the next frame right after having submitted the
987 * current layout.
988 */
989
Daniel Vettere2330f02014-10-29 11:34:56 +0100990 wait_for_fences(dev, state);
991
Daniel Vetter623369e2014-09-16 17:50:47 +0200992 drm_atomic_helper_commit_pre_planes(dev, state);
993
994 drm_atomic_helper_commit_planes(dev, state);
995
996 drm_atomic_helper_commit_post_planes(dev, state);
997
Rob Clark5ee32292014-11-11 19:38:59 -0500998 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200999
1000 drm_atomic_helper_cleanup_planes(dev, state);
1001
1002 drm_atomic_state_free(state);
1003
1004 return 0;
1005}
1006EXPORT_SYMBOL(drm_atomic_helper_commit);
1007
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001008/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001009 * DOC: implementing async commit
1010 *
1011 * For now the atomic helpers don't support async commit directly. If there is
1012 * real need it could be added though, using the dma-buf fence infrastructure
1013 * for generic synchronization with outstanding rendering.
1014 *
1015 * For now drivers have to implement async commit themselves, with the following
1016 * sequence being the recommended one:
1017 *
1018 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1019 * which commit needs to call which can fail, so we want to run it first and
1020 * synchronously.
1021 *
1022 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1023 * might be affected the new state update. This can be done by either cancelling
1024 * or flushing the work items, depending upon whether the driver can deal with
1025 * cancelled updates. Note that it is important to ensure that the framebuffer
1026 * cleanup is still done when cancelling.
1027 *
1028 * For sufficient parallelism it is recommended to have a work item per crtc
1029 * (for updates which don't touch global state) and a global one. Then we only
1030 * need to synchronize with the crtc work items for changed crtcs and the global
1031 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1032 *
1033 * 3. The software state is updated synchronously with
1034 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
1035 * locks means concurrent callers never see inconsistent state. And doing this
1036 * while it's guaranteed that no relevant async worker runs means that async
1037 * workers do not need grab any locks. Actually they must not grab locks, for
1038 * otherwise the work flushing will deadlock.
1039 *
1040 * 4. Schedule a work item to do all subsequent steps, using the split-out
1041 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1042 * then cleaning up the framebuffers after the old framebuffer is no longer
1043 * being displayed.
1044 */
1045
1046/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001047 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
1048 * @dev: DRM device
1049 * @state: atomic state object with old state structures
1050 *
1051 * This function prepares plane state, specifically framebuffers, for the new
1052 * configuration. If any failure is encountered this function will call
1053 * ->cleanup_fb on any already successfully prepared framebuffer.
1054 *
1055 * Returns:
1056 * 0 on success, negative error code on failure.
1057 */
1058int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1059 struct drm_atomic_state *state)
1060{
1061 int nplanes = dev->mode_config.num_total_plane;
1062 int ret, i;
1063
1064 for (i = 0; i < nplanes; i++) {
1065 struct drm_plane_helper_funcs *funcs;
1066 struct drm_plane *plane = state->planes[i];
1067 struct drm_framebuffer *fb;
1068
1069 if (!plane)
1070 continue;
1071
1072 funcs = plane->helper_private;
1073
1074 fb = state->plane_states[i]->fb;
1075
1076 if (fb && funcs->prepare_fb) {
1077 ret = funcs->prepare_fb(plane, fb);
1078 if (ret)
1079 goto fail;
1080 }
1081 }
1082
1083 return 0;
1084
1085fail:
1086 for (i--; i >= 0; i--) {
1087 struct drm_plane_helper_funcs *funcs;
1088 struct drm_plane *plane = state->planes[i];
1089 struct drm_framebuffer *fb;
1090
1091 if (!plane)
1092 continue;
1093
1094 funcs = plane->helper_private;
1095
1096 fb = state->plane_states[i]->fb;
1097
1098 if (fb && funcs->cleanup_fb)
1099 funcs->cleanup_fb(plane, fb);
1100
1101 }
1102
1103 return ret;
1104}
1105EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1106
1107/**
1108 * drm_atomic_helper_commit_planes - commit plane state
1109 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001110 * @old_state: atomic state object with old state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001111 *
1112 * This function commits the new plane state using the plane and atomic helper
1113 * functions for planes and crtcs. It assumes that the atomic state has already
1114 * been pushed into the relevant object state pointers, since this step can no
1115 * longer fail.
1116 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001117 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001118 * crtcs need to be updated though.
1119 */
1120void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001121 struct drm_atomic_state *old_state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001122{
1123 int nplanes = dev->mode_config.num_total_plane;
1124 int ncrtcs = dev->mode_config.num_crtc;
1125 int i;
1126
1127 for (i = 0; i < ncrtcs; i++) {
1128 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001129 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001130
1131 if (!crtc)
1132 continue;
1133
1134 funcs = crtc->helper_private;
1135
1136 if (!funcs || !funcs->atomic_begin)
1137 continue;
1138
1139 funcs->atomic_begin(crtc);
1140 }
1141
1142 for (i = 0; i < nplanes; i++) {
1143 struct drm_plane_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001144 struct drm_plane *plane = old_state->planes[i];
Thierry Redingf1c37e12014-11-25 12:09:44 +01001145 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001146
1147 if (!plane)
1148 continue;
1149
1150 funcs = plane->helper_private;
1151
1152 if (!funcs || !funcs->atomic_update)
1153 continue;
1154
Thierry Redingf1c37e12014-11-25 12:09:44 +01001155 old_plane_state = old_state->plane_states[i];
1156
1157 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001158 }
1159
1160 for (i = 0; i < ncrtcs; i++) {
1161 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001162 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001163
1164 if (!crtc)
1165 continue;
1166
1167 funcs = crtc->helper_private;
1168
1169 if (!funcs || !funcs->atomic_flush)
1170 continue;
1171
1172 funcs->atomic_flush(crtc);
1173 }
1174}
1175EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1176
1177/**
1178 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1179 * @dev: DRM device
1180 * @old_state: atomic state object with old state structures
1181 *
1182 * This function cleans up plane state, specifically framebuffers, from the old
1183 * configuration. Hence the old configuration must be perserved in @old_state to
1184 * be able to call this function.
1185 *
1186 * This function must also be called on the new state when the atomic update
1187 * fails at any point after calling drm_atomic_helper_prepare_planes().
1188 */
1189void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1190 struct drm_atomic_state *old_state)
1191{
1192 int nplanes = dev->mode_config.num_total_plane;
1193 int i;
1194
1195 for (i = 0; i < nplanes; i++) {
1196 struct drm_plane_helper_funcs *funcs;
1197 struct drm_plane *plane = old_state->planes[i];
1198 struct drm_framebuffer *old_fb;
1199
1200 if (!plane)
1201 continue;
1202
1203 funcs = plane->helper_private;
1204
1205 old_fb = old_state->plane_states[i]->fb;
1206
1207 if (old_fb && funcs->cleanup_fb)
1208 funcs->cleanup_fb(plane, old_fb);
1209 }
1210}
1211EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1212
1213/**
1214 * drm_atomic_helper_swap_state - store atomic state into current sw state
1215 * @dev: DRM device
1216 * @state: atomic state
1217 *
1218 * This function stores the atomic state into the current state pointers in all
1219 * driver objects. It should be called after all failing steps have been done
1220 * and succeeded, but before the actual hardware state is committed.
1221 *
1222 * For cleanup and error recovery the current state for all changed objects will
1223 * be swaped into @state.
1224 *
1225 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1226 *
1227 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1228 *
1229 * 2. Do any other steps that might fail.
1230 *
1231 * 3. Put the staged state into the current state pointers with this function.
1232 *
1233 * 4. Actually commit the hardware state.
1234 *
1235 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1236 * contains the old state. Also do any other cleanup required with that state.
1237 */
1238void drm_atomic_helper_swap_state(struct drm_device *dev,
1239 struct drm_atomic_state *state)
1240{
1241 int i;
1242
1243 for (i = 0; i < dev->mode_config.num_connector; i++) {
1244 struct drm_connector *connector = state->connectors[i];
1245
1246 if (!connector)
1247 continue;
1248
1249 connector->state->state = state;
1250 swap(state->connector_states[i], connector->state);
1251 connector->state->state = NULL;
1252 }
1253
1254 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1255 struct drm_crtc *crtc = state->crtcs[i];
1256
1257 if (!crtc)
1258 continue;
1259
1260 crtc->state->state = state;
1261 swap(state->crtc_states[i], crtc->state);
1262 crtc->state->state = NULL;
1263 }
1264
1265 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1266 struct drm_plane *plane = state->planes[i];
1267
1268 if (!plane)
1269 continue;
1270
1271 plane->state->state = state;
1272 swap(state->plane_states[i], plane->state);
1273 plane->state->state = NULL;
1274 }
1275}
1276EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001277
1278/**
1279 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1280 * @plane: plane object to update
1281 * @crtc: owning CRTC of owning plane
1282 * @fb: framebuffer to flip onto plane
1283 * @crtc_x: x offset of primary plane on crtc
1284 * @crtc_y: y offset of primary plane on crtc
1285 * @crtc_w: width of primary plane rectangle on crtc
1286 * @crtc_h: height of primary plane rectangle on crtc
1287 * @src_x: x offset of @fb for panning
1288 * @src_y: y offset of @fb for panning
1289 * @src_w: width of source rectangle in @fb
1290 * @src_h: height of source rectangle in @fb
1291 *
1292 * Provides a default plane update handler using the atomic driver interface.
1293 *
1294 * RETURNS:
1295 * Zero on success, error code on failure
1296 */
1297int drm_atomic_helper_update_plane(struct drm_plane *plane,
1298 struct drm_crtc *crtc,
1299 struct drm_framebuffer *fb,
1300 int crtc_x, int crtc_y,
1301 unsigned int crtc_w, unsigned int crtc_h,
1302 uint32_t src_x, uint32_t src_y,
1303 uint32_t src_w, uint32_t src_h)
1304{
1305 struct drm_atomic_state *state;
1306 struct drm_plane_state *plane_state;
1307 int ret = 0;
1308
1309 state = drm_atomic_state_alloc(plane->dev);
1310 if (!state)
1311 return -ENOMEM;
1312
1313 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1314retry:
1315 plane_state = drm_atomic_get_plane_state(state, plane);
1316 if (IS_ERR(plane_state)) {
1317 ret = PTR_ERR(plane_state);
1318 goto fail;
1319 }
1320
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001321 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001322 if (ret != 0)
1323 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001324 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001325 plane_state->crtc_x = crtc_x;
1326 plane_state->crtc_y = crtc_y;
1327 plane_state->crtc_h = crtc_h;
1328 plane_state->crtc_w = crtc_w;
1329 plane_state->src_x = src_x;
1330 plane_state->src_y = src_y;
1331 plane_state->src_h = src_h;
1332 plane_state->src_w = src_w;
1333
1334 ret = drm_atomic_commit(state);
1335 if (ret != 0)
1336 goto fail;
1337
1338 /* Driver takes ownership of state on successful commit. */
1339 return 0;
1340fail:
1341 if (ret == -EDEADLK)
1342 goto backoff;
1343
1344 drm_atomic_state_free(state);
1345
1346 return ret;
1347backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001348 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001349 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001350
1351 /*
1352 * Someone might have exchanged the framebuffer while we dropped locks
1353 * in the backoff code. We need to fix up the fb refcount tracking the
1354 * core does for us.
1355 */
1356 plane->old_fb = plane->fb;
1357
1358 goto retry;
1359}
1360EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1361
1362/**
1363 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1364 * @plane: plane to disable
1365 *
1366 * Provides a default plane disable handler using the atomic driver interface.
1367 *
1368 * RETURNS:
1369 * Zero on success, error code on failure
1370 */
1371int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1372{
1373 struct drm_atomic_state *state;
1374 struct drm_plane_state *plane_state;
1375 int ret = 0;
1376
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001377 /*
1378 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1379 * acquire context. The real fix will be to wire the acquire ctx through
1380 * everywhere we need it, but meanwhile prevent chaos by just skipping
1381 * this noop. The critical case is the cursor ioctls which a) only grab
1382 * crtc/cursor-plane locks (so we need the crtc to get at the right
1383 * acquire context) and b) can try to disable the plane multiple times.
1384 */
1385 if (!plane->crtc)
1386 return 0;
1387
Daniel Vetter042652e2014-07-27 13:46:52 +02001388 state = drm_atomic_state_alloc(plane->dev);
1389 if (!state)
1390 return -ENOMEM;
1391
1392 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1393retry:
1394 plane_state = drm_atomic_get_plane_state(state, plane);
1395 if (IS_ERR(plane_state)) {
1396 ret = PTR_ERR(plane_state);
1397 goto fail;
1398 }
1399
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001400 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001401 if (ret != 0)
1402 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001403 drm_atomic_set_fb_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001404 plane_state->crtc_x = 0;
1405 plane_state->crtc_y = 0;
1406 plane_state->crtc_h = 0;
1407 plane_state->crtc_w = 0;
1408 plane_state->src_x = 0;
1409 plane_state->src_y = 0;
1410 plane_state->src_h = 0;
1411 plane_state->src_w = 0;
1412
1413 ret = drm_atomic_commit(state);
1414 if (ret != 0)
1415 goto fail;
1416
1417 /* Driver takes ownership of state on successful commit. */
1418 return 0;
1419fail:
1420 if (ret == -EDEADLK)
1421 goto backoff;
1422
1423 drm_atomic_state_free(state);
1424
1425 return ret;
1426backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001427 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001428 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001429
1430 /*
1431 * Someone might have exchanged the framebuffer while we dropped locks
1432 * in the backoff code. We need to fix up the fb refcount tracking the
1433 * core does for us.
1434 */
1435 plane->old_fb = plane->fb;
1436
1437 goto retry;
1438}
1439EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1440
1441static int update_output_state(struct drm_atomic_state *state,
1442 struct drm_mode_set *set)
1443{
1444 struct drm_device *dev = set->crtc->dev;
1445 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001446 int ncrtcs = state->dev->mode_config.num_crtc;
1447 int ret, i, j;
1448
1449 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1450 state->acquire_ctx);
1451 if (ret)
1452 return ret;
1453
1454 /* First grab all affected connector/crtc states. */
1455 for (i = 0; i < set->num_connectors; i++) {
1456 conn_state = drm_atomic_get_connector_state(state,
1457 set->connectors[i]);
1458 if (IS_ERR(conn_state))
1459 return PTR_ERR(conn_state);
1460 }
1461
1462 for (i = 0; i < ncrtcs; i++) {
1463 struct drm_crtc *crtc = state->crtcs[i];
1464
1465 if (!crtc)
1466 continue;
1467
1468 ret = drm_atomic_add_affected_connectors(state, crtc);
1469 if (ret)
1470 return ret;
1471 }
1472
1473 /* Then recompute connector->crtc links and crtc enabling state. */
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001474 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001475 struct drm_connector *connector;
1476
1477 connector = state->connectors[i];
1478 conn_state = state->connector_states[i];
1479
1480 if (!connector)
1481 continue;
1482
1483 if (conn_state->crtc == set->crtc) {
1484 ret = drm_atomic_set_crtc_for_connector(conn_state,
1485 NULL);
1486 if (ret)
1487 return ret;
1488 }
1489
1490 for (j = 0; j < set->num_connectors; j++) {
1491 if (set->connectors[j] == connector) {
1492 ret = drm_atomic_set_crtc_for_connector(conn_state,
1493 set->crtc);
1494 if (ret)
1495 return ret;
1496 break;
1497 }
1498 }
1499 }
1500
1501 for (i = 0; i < ncrtcs; i++) {
1502 struct drm_crtc *crtc = state->crtcs[i];
1503 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1504
1505 if (!crtc)
1506 continue;
1507
1508 /* Don't update ->enable for the CRTC in the set_config request,
1509 * since a mismatch would indicate a bug in the upper layers.
1510 * The actual modeset code later on will catch any
1511 * inconsistencies here. */
1512 if (crtc == set->crtc)
1513 continue;
1514
1515 crtc_state->enable =
1516 drm_atomic_connectors_for_crtc(state, crtc);
1517 }
1518
1519 return 0;
1520}
1521
1522/**
1523 * drm_atomic_helper_set_config - set a new config from userspace
1524 * @set: mode set configuration
1525 *
1526 * Provides a default crtc set_config handler using the atomic driver interface.
1527 *
1528 * Returns:
1529 * Returns 0 on success, negative errno numbers on failure.
1530 */
1531int drm_atomic_helper_set_config(struct drm_mode_set *set)
1532{
1533 struct drm_atomic_state *state;
1534 struct drm_crtc *crtc = set->crtc;
1535 struct drm_crtc_state *crtc_state;
1536 struct drm_plane_state *primary_state;
1537 int ret = 0;
1538
1539 state = drm_atomic_state_alloc(crtc->dev);
1540 if (!state)
1541 return -ENOMEM;
1542
1543 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1544retry:
1545 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1546 if (IS_ERR(crtc_state)) {
1547 ret = PTR_ERR(crtc_state);
1548 goto fail;
1549 }
1550
Rob Clarke5b53412014-11-26 18:58:04 -05001551 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1552 if (IS_ERR(primary_state)) {
1553 ret = PTR_ERR(primary_state);
1554 goto fail;
1555 }
1556
Daniel Vetter042652e2014-07-27 13:46:52 +02001557 if (!set->mode) {
1558 WARN_ON(set->fb);
1559 WARN_ON(set->num_connectors);
1560
1561 crtc_state->enable = false;
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001562 crtc_state->active = false;
Rob Clarke5b53412014-11-26 18:58:04 -05001563
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001564 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
Rob Clarke5b53412014-11-26 18:58:04 -05001565 if (ret != 0)
1566 goto fail;
1567
1568 drm_atomic_set_fb_for_plane(primary_state, NULL);
1569
Daniel Vetter042652e2014-07-27 13:46:52 +02001570 goto commit;
1571 }
1572
1573 WARN_ON(!set->fb);
1574 WARN_ON(!set->num_connectors);
1575
1576 crtc_state->enable = true;
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001577 crtc_state->active = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02001578 drm_mode_copy(&crtc_state->mode, set->mode);
1579
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001580 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001581 if (ret != 0)
1582 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001583 drm_atomic_set_fb_for_plane(primary_state, set->fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001584 primary_state->crtc_x = 0;
1585 primary_state->crtc_y = 0;
1586 primary_state->crtc_h = set->mode->vdisplay;
1587 primary_state->crtc_w = set->mode->hdisplay;
1588 primary_state->src_x = set->x << 16;
1589 primary_state->src_y = set->y << 16;
1590 primary_state->src_h = set->mode->vdisplay << 16;
1591 primary_state->src_w = set->mode->hdisplay << 16;
1592
1593commit:
1594 ret = update_output_state(state, set);
1595 if (ret)
1596 goto fail;
1597
1598 ret = drm_atomic_commit(state);
1599 if (ret != 0)
1600 goto fail;
1601
1602 /* Driver takes ownership of state on successful commit. */
1603 return 0;
1604fail:
1605 if (ret == -EDEADLK)
1606 goto backoff;
1607
1608 drm_atomic_state_free(state);
1609
1610 return ret;
1611backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001612 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001613 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001614
1615 /*
1616 * Someone might have exchanged the framebuffer while we dropped locks
1617 * in the backoff code. We need to fix up the fb refcount tracking the
1618 * core does for us.
1619 */
1620 crtc->primary->old_fb = crtc->primary->fb;
1621
1622 goto retry;
1623}
1624EXPORT_SYMBOL(drm_atomic_helper_set_config);
1625
1626/**
1627 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1628 * @crtc: DRM crtc
1629 * @property: DRM property
1630 * @val: value of property
1631 *
1632 * Provides a default plane disablle handler using the atomic driver interface.
1633 *
1634 * RETURNS:
1635 * Zero on success, error code on failure
1636 */
1637int
1638drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1639 struct drm_property *property,
1640 uint64_t val)
1641{
1642 struct drm_atomic_state *state;
1643 struct drm_crtc_state *crtc_state;
1644 int ret = 0;
1645
1646 state = drm_atomic_state_alloc(crtc->dev);
1647 if (!state)
1648 return -ENOMEM;
1649
1650 /* ->set_property is always called with all locks held. */
1651 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1652retry:
1653 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1654 if (IS_ERR(crtc_state)) {
1655 ret = PTR_ERR(crtc_state);
1656 goto fail;
1657 }
1658
Rob Clark40ecc692014-12-18 16:01:46 -05001659 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1660 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001661 if (ret)
1662 goto fail;
1663
1664 ret = drm_atomic_commit(state);
1665 if (ret != 0)
1666 goto fail;
1667
1668 /* Driver takes ownership of state on successful commit. */
1669 return 0;
1670fail:
1671 if (ret == -EDEADLK)
1672 goto backoff;
1673
1674 drm_atomic_state_free(state);
1675
1676 return ret;
1677backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001678 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001679 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001680
1681 goto retry;
1682}
1683EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1684
1685/**
1686 * drm_atomic_helper_plane_set_property - helper for plane prorties
1687 * @plane: DRM plane
1688 * @property: DRM property
1689 * @val: value of property
1690 *
1691 * Provides a default plane disable handler using the atomic driver interface.
1692 *
1693 * RETURNS:
1694 * Zero on success, error code on failure
1695 */
1696int
1697drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1698 struct drm_property *property,
1699 uint64_t val)
1700{
1701 struct drm_atomic_state *state;
1702 struct drm_plane_state *plane_state;
1703 int ret = 0;
1704
1705 state = drm_atomic_state_alloc(plane->dev);
1706 if (!state)
1707 return -ENOMEM;
1708
1709 /* ->set_property is always called with all locks held. */
1710 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1711retry:
1712 plane_state = drm_atomic_get_plane_state(state, plane);
1713 if (IS_ERR(plane_state)) {
1714 ret = PTR_ERR(plane_state);
1715 goto fail;
1716 }
1717
Rob Clark40ecc692014-12-18 16:01:46 -05001718 ret = drm_atomic_plane_set_property(plane, plane_state,
1719 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001720 if (ret)
1721 goto fail;
1722
1723 ret = drm_atomic_commit(state);
1724 if (ret != 0)
1725 goto fail;
1726
1727 /* Driver takes ownership of state on successful commit. */
1728 return 0;
1729fail:
1730 if (ret == -EDEADLK)
1731 goto backoff;
1732
1733 drm_atomic_state_free(state);
1734
1735 return ret;
1736backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001737 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001738 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001739
1740 goto retry;
1741}
1742EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1743
1744/**
1745 * drm_atomic_helper_connector_set_property - helper for connector prorties
1746 * @connector: DRM connector
1747 * @property: DRM property
1748 * @val: value of property
1749 *
1750 * Provides a default plane disablle handler using the atomic driver interface.
1751 *
1752 * RETURNS:
1753 * Zero on success, error code on failure
1754 */
1755int
1756drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1757 struct drm_property *property,
1758 uint64_t val)
1759{
1760 struct drm_atomic_state *state;
1761 struct drm_connector_state *connector_state;
1762 int ret = 0;
1763
1764 state = drm_atomic_state_alloc(connector->dev);
1765 if (!state)
1766 return -ENOMEM;
1767
1768 /* ->set_property is always called with all locks held. */
1769 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1770retry:
1771 connector_state = drm_atomic_get_connector_state(state, connector);
1772 if (IS_ERR(connector_state)) {
1773 ret = PTR_ERR(connector_state);
1774 goto fail;
1775 }
1776
Rob Clark40ecc692014-12-18 16:01:46 -05001777 ret = drm_atomic_connector_set_property(connector, connector_state,
1778 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001779 if (ret)
1780 goto fail;
1781
1782 ret = drm_atomic_commit(state);
1783 if (ret != 0)
1784 goto fail;
1785
1786 /* Driver takes ownership of state on successful commit. */
1787 return 0;
1788fail:
1789 if (ret == -EDEADLK)
1790 goto backoff;
1791
1792 drm_atomic_state_free(state);
1793
1794 return ret;
1795backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001796 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001797 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001798
1799 goto retry;
1800}
1801EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001802
1803/**
1804 * drm_atomic_helper_page_flip - execute a legacy page flip
1805 * @crtc: DRM crtc
1806 * @fb: DRM framebuffer
1807 * @event: optional DRM event to signal upon completion
1808 * @flags: flip flags for non-vblank sync'ed updates
1809 *
1810 * Provides a default page flip implementation using the atomic driver interface.
1811 *
1812 * Note that for now so called async page flips (i.e. updates which are not
1813 * synchronized to vblank) are not supported, since the atomic interfaces have
1814 * no provisions for this yet.
1815 *
1816 * Returns:
1817 * Returns 0 on success, negative errno numbers on failure.
1818 */
1819int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1820 struct drm_framebuffer *fb,
1821 struct drm_pending_vblank_event *event,
1822 uint32_t flags)
1823{
1824 struct drm_plane *plane = crtc->primary;
1825 struct drm_atomic_state *state;
1826 struct drm_plane_state *plane_state;
1827 struct drm_crtc_state *crtc_state;
1828 int ret = 0;
1829
1830 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1831 return -EINVAL;
1832
1833 state = drm_atomic_state_alloc(plane->dev);
1834 if (!state)
1835 return -ENOMEM;
1836
1837 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1838retry:
1839 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1840 if (IS_ERR(crtc_state)) {
1841 ret = PTR_ERR(crtc_state);
1842 goto fail;
1843 }
1844 crtc_state->event = event;
1845
1846 plane_state = drm_atomic_get_plane_state(state, plane);
1847 if (IS_ERR(plane_state)) {
1848 ret = PTR_ERR(plane_state);
1849 goto fail;
1850 }
1851
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001852 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001853 if (ret != 0)
1854 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001855 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001856
1857 ret = drm_atomic_async_commit(state);
1858 if (ret != 0)
1859 goto fail;
1860
1861 /* TODO: ->page_flip is the only driver callback where the core
1862 * doesn't update plane->fb. For now patch it up here. */
1863 plane->fb = plane->state->fb;
1864
1865 /* Driver takes ownership of state on successful async commit. */
1866 return 0;
1867fail:
1868 if (ret == -EDEADLK)
1869 goto backoff;
1870
1871 drm_atomic_state_free(state);
1872
1873 return ret;
1874backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001875 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001876 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001877
1878 /*
1879 * Someone might have exchanged the framebuffer while we dropped locks
1880 * in the backoff code. We need to fix up the fb refcount tracking the
1881 * core does for us.
1882 */
1883 plane->old_fb = plane->fb;
1884
1885 goto retry;
1886}
1887EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01001888
1889/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01001890 * DOC: atomic state reset and initialization
1891 *
1892 * Both the drm core and the atomic helpers assume that there is always the full
1893 * and correct atomic software state for all connectors, CRTCs and planes
1894 * available. Which is a bit a problem on driver load and also after system
1895 * suspend. One way to solve this is to have a hardware state read-out
1896 * infrastructure which reconstructs the full software state (e.g. the i915
1897 * driver).
1898 *
1899 * The simpler solution is to just reset the software state to everything off,
1900 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
1901 * the atomic helpers provide default reset implementations for all hooks.
1902 */
1903
1904/**
Daniel Vetterd4617012014-11-03 15:56:43 +01001905 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
1906 * @crtc: drm CRTC
1907 *
1908 * Resets the atomic state for @crtc by freeing the state pointer (which might
1909 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1910 */
1911void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
1912{
1913 kfree(crtc->state);
1914 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001915
1916 if (crtc->state)
1917 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01001918}
1919EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
1920
1921/**
1922 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
1923 * @crtc: drm CRTC
1924 *
1925 * Default CRTC state duplicate hook for drivers which don't have their own
1926 * subclassed CRTC state structure.
1927 */
1928struct drm_crtc_state *
1929drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
1930{
1931 struct drm_crtc_state *state;
1932
1933 if (WARN_ON(!crtc->state))
1934 return NULL;
1935
1936 state = kmemdup(crtc->state, sizeof(*crtc->state), GFP_KERNEL);
1937
1938 if (state) {
1939 state->mode_changed = false;
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001940 state->active_changed = false;
Daniel Vetterd4617012014-11-03 15:56:43 +01001941 state->planes_changed = false;
1942 state->event = NULL;
1943 }
1944
1945 return state;
1946}
1947EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
1948
1949/**
1950 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
1951 * @crtc: drm CRTC
1952 * @state: CRTC state object to release
1953 *
1954 * Default CRTC state destroy hook for drivers which don't have their own
1955 * subclassed CRTC state structure.
1956 */
1957void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
1958 struct drm_crtc_state *state)
1959{
1960 kfree(state);
1961}
1962EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
1963
1964/**
1965 * drm_atomic_helper_plane_reset - default ->reset hook for planes
1966 * @plane: drm plane
1967 *
1968 * Resets the atomic state for @plane by freeing the state pointer (which might
1969 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1970 */
1971void drm_atomic_helper_plane_reset(struct drm_plane *plane)
1972{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001973 if (plane->state && plane->state->fb)
1974 drm_framebuffer_unreference(plane->state->fb);
1975
Daniel Vetterd4617012014-11-03 15:56:43 +01001976 kfree(plane->state);
1977 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001978
1979 if (plane->state)
1980 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01001981}
1982EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
1983
1984/**
1985 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
1986 * @plane: drm plane
1987 *
1988 * Default plane state duplicate hook for drivers which don't have their own
1989 * subclassed plane state structure.
1990 */
1991struct drm_plane_state *
1992drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
1993{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001994 struct drm_plane_state *state;
1995
Daniel Vetterd4617012014-11-03 15:56:43 +01001996 if (WARN_ON(!plane->state))
1997 return NULL;
1998
Daniel Vetter321ebf02014-11-04 22:57:27 +01001999 state = kmemdup(plane->state, sizeof(*plane->state), GFP_KERNEL);
2000
2001 if (state && state->fb)
2002 drm_framebuffer_reference(state->fb);
2003
2004 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002005}
2006EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2007
2008/**
2009 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2010 * @plane: drm plane
2011 * @state: plane state object to release
2012 *
2013 * Default plane state destroy hook for drivers which don't have their own
2014 * subclassed plane state structure.
2015 */
2016void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002017 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002018{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002019 if (state->fb)
2020 drm_framebuffer_unreference(state->fb);
2021
Daniel Vetterd4617012014-11-03 15:56:43 +01002022 kfree(state);
2023}
2024EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2025
2026/**
2027 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2028 * @connector: drm connector
2029 *
2030 * Resets the atomic state for @connector by freeing the state pointer (which
2031 * might be NULL, e.g. at driver load time) and allocating a new empty state
2032 * object.
2033 */
2034void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2035{
2036 kfree(connector->state);
2037 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002038
2039 if (connector->state)
2040 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002041}
2042EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2043
2044/**
2045 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2046 * @connector: drm connector
2047 *
2048 * Default connector state duplicate hook for drivers which don't have their own
2049 * subclassed connector state structure.
2050 */
2051struct drm_connector_state *
2052drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2053{
2054 if (WARN_ON(!connector->state))
2055 return NULL;
2056
2057 return kmemdup(connector->state, sizeof(*connector->state), GFP_KERNEL);
2058}
2059EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2060
2061/**
2062 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2063 * @connector: drm connector
2064 * @state: connector state object to release
2065 *
2066 * Default connector state destroy hook for drivers which don't have their own
2067 * subclassed connector state structure.
2068 */
2069void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2070 struct drm_connector_state *state)
2071{
2072 kfree(state);
2073}
2074EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);