blob: af5f539ed1474d9735a9a6f53bb1695734271d6b [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 Vetterd9b13622014-11-26 16:57:41 +0100333/**
334 * drm_atomic_helper_check - validate state object for modeset changes
335 * @dev: DRM device
336 * @state: the driver state object
337 *
338 * Check the state object to see if the requested state is physically possible.
339 * This does all the crtc and connector related computations for an atomic
340 * update. It computes and updates crtc_state->mode_changed, adds any additional
341 * connectors needed for full modesets and calls down into ->mode_fixup
342 * functions of the driver backend.
343 *
344 * IMPORTANT:
345 *
346 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
347 * plane update can't be done without a full modeset) _must_ call this function
348 * afterwards after that change. It is permitted to call this function multiple
349 * times for the same update, e.g. when the ->atomic_check functions depend upon
350 * the adjusted dotclock for fifo space allocation and watermark computation.
351 *
352 * RETURNS
353 * Zero for success or -errno
354 */
355int
Rob Clark934ce1c2014-11-19 16:41:33 -0500356drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200357 struct drm_atomic_state *state)
358{
359 int ncrtcs = dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200360 struct drm_crtc *crtc;
361 struct drm_crtc_state *crtc_state;
362 int i, ret;
363
364 for (i = 0; i < ncrtcs; i++) {
365 crtc = state->crtcs[i];
366 crtc_state = state->crtc_states[i];
367
368 if (!crtc)
369 continue;
370
371 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
372 DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
373 crtc->base.id);
374 crtc_state->mode_changed = true;
375 }
376
377 if (crtc->state->enable != crtc_state->enable) {
378 DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
379 crtc->base.id);
380 crtc_state->mode_changed = true;
381 }
382 }
383
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100384 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200385 /*
386 * This only sets crtc->mode_changed for routing changes,
387 * drivers must set crtc->mode_changed themselves when connector
388 * properties need to be updated.
389 */
390 ret = update_connector_routing(state, i);
391 if (ret)
392 return ret;
393 }
394
395 /*
396 * After all the routing has been prepared we need to add in any
397 * connector which is itself unchanged, but who's crtc changes it's
398 * configuration. This must be done before calling mode_fixup in case a
399 * crtc only changed its mode but has the same set of connectors.
400 */
401 for (i = 0; i < ncrtcs; i++) {
402 int num_connectors;
403
404 crtc = state->crtcs[i];
405 crtc_state = state->crtc_states[i];
406
407 if (!crtc || !crtc_state->mode_changed)
408 continue;
409
410 DRM_DEBUG_KMS("[CRTC:%d] needs full modeset, enable: %c\n",
411 crtc->base.id,
412 crtc_state->enable ? 'y' : 'n');
413
414 ret = drm_atomic_add_affected_connectors(state, crtc);
415 if (ret != 0)
416 return ret;
417
418 num_connectors = drm_atomic_connectors_for_crtc(state,
419 crtc);
420
421 if (crtc_state->enable != !!num_connectors) {
422 DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
423 crtc->base.id);
424
425 return -EINVAL;
426 }
427 }
428
429 return mode_fixup(state);
430}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100431EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200432
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100433/**
Daniel Vetterd9b13622014-11-26 16:57:41 +0100434 * drm_atomic_helper_check - validate state object for modeset changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100435 * @dev: DRM device
436 * @state: the driver state object
437 *
438 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100439 * This does all the plane update related checks using by calling into the
440 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100441 *
442 * RETURNS
443 * Zero for success or -errno
444 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100445int
446drm_atomic_helper_check_planes(struct drm_device *dev,
447 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100448{
449 int nplanes = dev->mode_config.num_total_plane;
450 int ncrtcs = dev->mode_config.num_crtc;
451 int i, ret = 0;
452
453 for (i = 0; i < nplanes; i++) {
454 struct drm_plane_helper_funcs *funcs;
455 struct drm_plane *plane = state->planes[i];
456 struct drm_plane_state *plane_state = state->plane_states[i];
457
458 if (!plane)
459 continue;
460
461 funcs = plane->helper_private;
462
463 drm_atomic_helper_plane_changed(state, plane_state, plane);
464
465 if (!funcs || !funcs->atomic_check)
466 continue;
467
468 ret = funcs->atomic_check(plane, plane_state);
469 if (ret) {
Rob Clark5e743732014-12-18 16:01:51 -0500470 DRM_DEBUG_KMS("[PLANE:%d] atomic driver check failed\n",
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100471 plane->base.id);
472 return ret;
473 }
474 }
475
476 for (i = 0; i < ncrtcs; i++) {
477 struct drm_crtc_helper_funcs *funcs;
478 struct drm_crtc *crtc = state->crtcs[i];
479
480 if (!crtc)
481 continue;
482
483 funcs = crtc->helper_private;
484
485 if (!funcs || !funcs->atomic_check)
486 continue;
487
488 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
489 if (ret) {
Rob Clark5e743732014-12-18 16:01:51 -0500490 DRM_DEBUG_KMS("[CRTC:%d] atomic driver check failed\n",
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100491 crtc->base.id);
492 return ret;
493 }
494 }
495
Daniel Vetterd9b13622014-11-26 16:57:41 +0100496 return ret;
497}
498EXPORT_SYMBOL(drm_atomic_helper_check_planes);
499
500/**
501 * drm_atomic_helper_check - validate state object
502 * @dev: DRM device
503 * @state: the driver state object
504 *
505 * Check the state object to see if the requested state is physically possible.
506 * Only crtcs and planes have check callbacks, so for any additional (global)
507 * checking that a driver needs it can simply wrap that around this function.
508 * Drivers without such needs can directly use this as their ->atomic_check()
509 * callback.
510 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100511 * This just wraps the two parts of the state checking for planes and modeset
512 * state in the default order: First it calls drm_atomic_helper_check_modeset()
513 * and then drm_atomic_helper_check_planes(). The assumption is that the
514 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
515 * e.g. properly compute watermarks.
516 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100517 * RETURNS
518 * Zero for success or -errno
519 */
520int drm_atomic_helper_check(struct drm_device *dev,
521 struct drm_atomic_state *state)
522{
523 int ret;
524
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100525 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100526 if (ret)
527 return ret;
528
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100529 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500530 if (ret)
531 return ret;
532
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100533 return ret;
534}
535EXPORT_SYMBOL(drm_atomic_helper_check);
536
Daniel Vetter623369e2014-09-16 17:50:47 +0200537static void
538disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
539{
540 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200541 int i;
542
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100543 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200544 struct drm_connector_state *old_conn_state;
545 struct drm_connector *connector;
546 struct drm_encoder_helper_funcs *funcs;
547 struct drm_encoder *encoder;
548
549 old_conn_state = old_state->connector_states[i];
550 connector = old_state->connectors[i];
551
552 /* Shut down everything that's in the changeset and currently
553 * still on. So need to check the old, saved state. */
554 if (!old_conn_state || !old_conn_state->crtc)
555 continue;
556
Rob Clark46df9ad2014-11-20 15:40:36 -0500557 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200558
Rob Clark46df9ad2014-11-20 15:40:36 -0500559 /* We shouldn't get this far if we didn't previously have
560 * an encoder.. but WARN_ON() rather than explode.
561 */
562 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200563 continue;
564
565 funcs = encoder->helper_private;
566
567 /*
568 * Each encoder has at most one connector (since we always steal
569 * it away), so we won't call call disable hooks twice.
570 */
571 if (encoder->bridge)
572 encoder->bridge->funcs->disable(encoder->bridge);
573
574 /* Right function depends upon target state. */
575 if (connector->state->crtc)
576 funcs->prepare(encoder);
577 else if (funcs->disable)
578 funcs->disable(encoder);
579 else
580 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
581
582 if (encoder->bridge)
583 encoder->bridge->funcs->post_disable(encoder->bridge);
584 }
585
586 for (i = 0; i < ncrtcs; i++) {
587 struct drm_crtc_helper_funcs *funcs;
588 struct drm_crtc *crtc;
589
590 crtc = old_state->crtcs[i];
591
592 /* Shut down everything that needs a full modeset. */
593 if (!crtc || !crtc->state->mode_changed)
594 continue;
595
596 funcs = crtc->helper_private;
597
598 /* Right function depends upon target state. */
599 if (crtc->state->enable)
600 funcs->prepare(crtc);
601 else if (funcs->disable)
602 funcs->disable(crtc);
603 else
604 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
605 }
606}
607
608static void
609set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
610{
Daniel Vetter623369e2014-09-16 17:50:47 +0200611 int ncrtcs = old_state->dev->mode_config.num_crtc;
612 int i;
613
614 /* clear out existing links */
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100615 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200616 struct drm_connector *connector;
617
618 connector = old_state->connectors[i];
619
620 if (!connector || !connector->encoder)
621 continue;
622
623 WARN_ON(!connector->encoder->crtc);
624
625 connector->encoder->crtc = NULL;
626 connector->encoder = NULL;
627 }
628
629 /* set new links */
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100630 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200631 struct drm_connector *connector;
632
633 connector = old_state->connectors[i];
634
635 if (!connector || !connector->state->crtc)
636 continue;
637
638 if (WARN_ON(!connector->state->best_encoder))
639 continue;
640
641 connector->encoder = connector->state->best_encoder;
642 connector->encoder->crtc = connector->state->crtc;
643 }
644
645 /* set legacy state in the crtc structure */
646 for (i = 0; i < ncrtcs; i++) {
647 struct drm_crtc *crtc;
648
649 crtc = old_state->crtcs[i];
650
651 if (!crtc)
652 continue;
653
654 crtc->mode = crtc->state->mode;
655 crtc->enabled = crtc->state->enable;
656 crtc->x = crtc->primary->state->src_x >> 16;
657 crtc->y = crtc->primary->state->src_y >> 16;
658 }
659}
660
661static void
662crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
663{
664 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200665 int i;
666
667 for (i = 0; i < ncrtcs; i++) {
668 struct drm_crtc_helper_funcs *funcs;
669 struct drm_crtc *crtc;
670
671 crtc = old_state->crtcs[i];
672
673 if (!crtc || !crtc->state->mode_changed)
674 continue;
675
676 funcs = crtc->helper_private;
677
678 if (crtc->state->enable)
679 funcs->mode_set_nofb(crtc);
680 }
681
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100682 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200683 struct drm_connector *connector;
684 struct drm_crtc_state *new_crtc_state;
685 struct drm_encoder_helper_funcs *funcs;
686 struct drm_encoder *encoder;
687 struct drm_display_mode *mode, *adjusted_mode;
688
689 connector = old_state->connectors[i];
690
691 if (!connector || !connector->state->best_encoder)
692 continue;
693
694 encoder = connector->state->best_encoder;
695 funcs = encoder->helper_private;
696 new_crtc_state = connector->state->crtc->state;
697 mode = &new_crtc_state->mode;
698 adjusted_mode = &new_crtc_state->adjusted_mode;
699
700 /*
701 * Each encoder has at most one connector (since we always steal
702 * it away), so we won't call call mode_set hooks twice.
703 */
704 funcs->mode_set(encoder, mode, adjusted_mode);
705
706 if (encoder->bridge && encoder->bridge->funcs->mode_set)
707 encoder->bridge->funcs->mode_set(encoder->bridge,
708 mode, adjusted_mode);
709 }
710}
711
712/**
713 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
714 * @dev: DRM device
715 * @state: atomic state
716 *
717 * This function commits the modeset changes that need to be committed before
718 * updating planes. It shuts down all the outputs that need to be shut down and
719 * prepares them (if required) with the new mode.
720 */
721void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
722 struct drm_atomic_state *state)
723{
724 disable_outputs(dev, state);
725 set_routing_links(dev, state);
726 crtc_set_mode(dev, state);
727}
728EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
729
730/**
731 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
732 * @dev: DRM device
733 * @old_state: atomic state object with old state structures
734 *
735 * This function commits the modeset changes that need to be committed after
736 * updating planes: It enables all the outputs with the new configuration which
737 * had to be turned off for the update.
738 */
739void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
740 struct drm_atomic_state *old_state)
741{
742 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200743 int i;
744
745 for (i = 0; i < ncrtcs; i++) {
746 struct drm_crtc_helper_funcs *funcs;
747 struct drm_crtc *crtc;
748
749 crtc = old_state->crtcs[i];
750
751 /* Need to filter out CRTCs where only planes change. */
752 if (!crtc || !crtc->state->mode_changed)
753 continue;
754
755 funcs = crtc->helper_private;
756
757 if (crtc->state->enable)
758 funcs->commit(crtc);
759 }
760
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100761 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200762 struct drm_connector *connector;
763 struct drm_encoder_helper_funcs *funcs;
764 struct drm_encoder *encoder;
765
766 connector = old_state->connectors[i];
767
768 if (!connector || !connector->state->best_encoder)
769 continue;
770
771 encoder = connector->state->best_encoder;
772 funcs = encoder->helper_private;
773
774 /*
775 * Each encoder has at most one connector (since we always steal
776 * it away), so we won't call call enable hooks twice.
777 */
778 if (encoder->bridge)
779 encoder->bridge->funcs->pre_enable(encoder->bridge);
780
781 funcs->commit(encoder);
782
783 if (encoder->bridge)
784 encoder->bridge->funcs->enable(encoder->bridge);
785 }
786}
787EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
788
Daniel Vettere2330f02014-10-29 11:34:56 +0100789static void wait_for_fences(struct drm_device *dev,
790 struct drm_atomic_state *state)
791{
792 int nplanes = dev->mode_config.num_total_plane;
793 int i;
794
795 for (i = 0; i < nplanes; i++) {
796 struct drm_plane *plane = state->planes[i];
797
798 if (!plane || !plane->state->fence)
799 continue;
800
801 WARN_ON(!plane->state->fb);
802
803 fence_wait(plane->state->fence, false);
804 fence_put(plane->state->fence);
805 plane->state->fence = NULL;
806 }
807}
808
Daniel Vetterab58e332014-11-24 20:42:42 +0100809static bool framebuffer_changed(struct drm_device *dev,
810 struct drm_atomic_state *old_state,
811 struct drm_crtc *crtc)
812{
813 struct drm_plane *plane;
814 struct drm_plane_state *old_plane_state;
815 int nplanes = old_state->dev->mode_config.num_total_plane;
816 int i;
817
818 for (i = 0; i < nplanes; i++) {
819 plane = old_state->planes[i];
820 old_plane_state = old_state->plane_states[i];
821
822 if (!plane)
823 continue;
824
825 if (plane->state->crtc != crtc &&
826 old_plane_state->crtc != crtc)
827 continue;
828
829 if (plane->state->fb != old_plane_state->fb)
830 return true;
831 }
832
833 return false;
834}
835
Rob Clark5ee32292014-11-11 19:38:59 -0500836/**
837 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
838 * @dev: DRM device
839 * @old_state: atomic state object with old state structures
840 *
841 * Helper to, after atomic commit, wait for vblanks on all effected
842 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100843 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
844 * framebuffers have actually changed to optimize for the legacy cursor and
845 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500846 */
847void
848drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
849 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200850{
851 struct drm_crtc *crtc;
852 struct drm_crtc_state *old_crtc_state;
853 int ncrtcs = old_state->dev->mode_config.num_crtc;
854 int i, ret;
855
856 for (i = 0; i < ncrtcs; i++) {
857 crtc = old_state->crtcs[i];
858 old_crtc_state = old_state->crtc_states[i];
859
860 if (!crtc)
861 continue;
862
863 /* No one cares about the old state, so abuse it for tracking
864 * and store whether we hold a vblank reference (and should do a
865 * vblank wait) in the ->enable boolean. */
866 old_crtc_state->enable = false;
867
868 if (!crtc->state->enable)
869 continue;
870
Daniel Vetterab58e332014-11-24 20:42:42 +0100871 if (!framebuffer_changed(dev, old_state, crtc))
872 continue;
873
Daniel Vetter623369e2014-09-16 17:50:47 +0200874 ret = drm_crtc_vblank_get(crtc);
875 if (ret != 0)
876 continue;
877
878 old_crtc_state->enable = true;
879 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
880 }
881
882 for (i = 0; i < ncrtcs; i++) {
883 crtc = old_state->crtcs[i];
884 old_crtc_state = old_state->crtc_states[i];
885
886 if (!crtc || !old_crtc_state->enable)
887 continue;
888
889 ret = wait_event_timeout(dev->vblank[i].queue,
890 old_crtc_state->last_vblank_count !=
891 drm_vblank_count(dev, i),
892 msecs_to_jiffies(50));
893
894 drm_crtc_vblank_put(crtc);
895 }
896}
Rob Clark5ee32292014-11-11 19:38:59 -0500897EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +0200898
899/**
900 * drm_atomic_helper_commit - commit validated state object
901 * @dev: DRM device
902 * @state: the driver state object
903 * @async: asynchronous commit
904 *
905 * This function commits a with drm_atomic_helper_check() pre-validated state
906 * object. This can still fail when e.g. the framebuffer reservation fails. For
907 * now this doesn't implement asynchronous commits.
908 *
909 * RETURNS
910 * Zero for success or -errno.
911 */
912int drm_atomic_helper_commit(struct drm_device *dev,
913 struct drm_atomic_state *state,
914 bool async)
915{
916 int ret;
917
918 if (async)
919 return -EBUSY;
920
921 ret = drm_atomic_helper_prepare_planes(dev, state);
922 if (ret)
923 return ret;
924
925 /*
926 * This is the point of no return - everything below never fails except
927 * when the hw goes bonghits. Which means we can commit the new state on
928 * the software side now.
929 */
930
931 drm_atomic_helper_swap_state(dev, state);
932
933 /*
934 * Everything below can be run asynchronously without the need to grab
935 * any modeset locks at all under one conditions: It must be guaranteed
936 * that the asynchronous work has either been cancelled (if the driver
937 * supports it, which at least requires that the framebuffers get
938 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
939 * before the new state gets committed on the software side with
940 * drm_atomic_helper_swap_state().
941 *
942 * This scheme allows new atomic state updates to be prepared and
943 * checked in parallel to the asynchronous completion of the previous
944 * update. Which is important since compositors need to figure out the
945 * composition of the next frame right after having submitted the
946 * current layout.
947 */
948
Daniel Vettere2330f02014-10-29 11:34:56 +0100949 wait_for_fences(dev, state);
950
Daniel Vetter623369e2014-09-16 17:50:47 +0200951 drm_atomic_helper_commit_pre_planes(dev, state);
952
953 drm_atomic_helper_commit_planes(dev, state);
954
955 drm_atomic_helper_commit_post_planes(dev, state);
956
Rob Clark5ee32292014-11-11 19:38:59 -0500957 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200958
959 drm_atomic_helper_cleanup_planes(dev, state);
960
961 drm_atomic_state_free(state);
962
963 return 0;
964}
965EXPORT_SYMBOL(drm_atomic_helper_commit);
966
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100967/**
Daniel Vettere8c833a2014-07-27 18:30:19 +0200968 * DOC: implementing async commit
969 *
970 * For now the atomic helpers don't support async commit directly. If there is
971 * real need it could be added though, using the dma-buf fence infrastructure
972 * for generic synchronization with outstanding rendering.
973 *
974 * For now drivers have to implement async commit themselves, with the following
975 * sequence being the recommended one:
976 *
977 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
978 * which commit needs to call which can fail, so we want to run it first and
979 * synchronously.
980 *
981 * 2. Synchronize with any outstanding asynchronous commit worker threads which
982 * might be affected the new state update. This can be done by either cancelling
983 * or flushing the work items, depending upon whether the driver can deal with
984 * cancelled updates. Note that it is important to ensure that the framebuffer
985 * cleanup is still done when cancelling.
986 *
987 * For sufficient parallelism it is recommended to have a work item per crtc
988 * (for updates which don't touch global state) and a global one. Then we only
989 * need to synchronize with the crtc work items for changed crtcs and the global
990 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
991 *
992 * 3. The software state is updated synchronously with
993 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
994 * locks means concurrent callers never see inconsistent state. And doing this
995 * while it's guaranteed that no relevant async worker runs means that async
996 * workers do not need grab any locks. Actually they must not grab locks, for
997 * otherwise the work flushing will deadlock.
998 *
999 * 4. Schedule a work item to do all subsequent steps, using the split-out
1000 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1001 * then cleaning up the framebuffers after the old framebuffer is no longer
1002 * being displayed.
1003 */
1004
1005/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001006 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
1007 * @dev: DRM device
1008 * @state: atomic state object with old state structures
1009 *
1010 * This function prepares plane state, specifically framebuffers, for the new
1011 * configuration. If any failure is encountered this function will call
1012 * ->cleanup_fb on any already successfully prepared framebuffer.
1013 *
1014 * Returns:
1015 * 0 on success, negative error code on failure.
1016 */
1017int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1018 struct drm_atomic_state *state)
1019{
1020 int nplanes = dev->mode_config.num_total_plane;
1021 int ret, i;
1022
1023 for (i = 0; i < nplanes; i++) {
1024 struct drm_plane_helper_funcs *funcs;
1025 struct drm_plane *plane = state->planes[i];
1026 struct drm_framebuffer *fb;
1027
1028 if (!plane)
1029 continue;
1030
1031 funcs = plane->helper_private;
1032
1033 fb = state->plane_states[i]->fb;
1034
1035 if (fb && funcs->prepare_fb) {
1036 ret = funcs->prepare_fb(plane, fb);
1037 if (ret)
1038 goto fail;
1039 }
1040 }
1041
1042 return 0;
1043
1044fail:
1045 for (i--; i >= 0; i--) {
1046 struct drm_plane_helper_funcs *funcs;
1047 struct drm_plane *plane = state->planes[i];
1048 struct drm_framebuffer *fb;
1049
1050 if (!plane)
1051 continue;
1052
1053 funcs = plane->helper_private;
1054
1055 fb = state->plane_states[i]->fb;
1056
1057 if (fb && funcs->cleanup_fb)
1058 funcs->cleanup_fb(plane, fb);
1059
1060 }
1061
1062 return ret;
1063}
1064EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1065
1066/**
1067 * drm_atomic_helper_commit_planes - commit plane state
1068 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001069 * @old_state: atomic state object with old state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001070 *
1071 * This function commits the new plane state using the plane and atomic helper
1072 * functions for planes and crtcs. It assumes that the atomic state has already
1073 * been pushed into the relevant object state pointers, since this step can no
1074 * longer fail.
1075 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001076 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001077 * crtcs need to be updated though.
1078 */
1079void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001080 struct drm_atomic_state *old_state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001081{
1082 int nplanes = dev->mode_config.num_total_plane;
1083 int ncrtcs = dev->mode_config.num_crtc;
1084 int i;
1085
1086 for (i = 0; i < ncrtcs; i++) {
1087 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001088 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001089
1090 if (!crtc)
1091 continue;
1092
1093 funcs = crtc->helper_private;
1094
1095 if (!funcs || !funcs->atomic_begin)
1096 continue;
1097
1098 funcs->atomic_begin(crtc);
1099 }
1100
1101 for (i = 0; i < nplanes; i++) {
1102 struct drm_plane_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001103 struct drm_plane *plane = old_state->planes[i];
Thierry Redingf1c37e12014-11-25 12:09:44 +01001104 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001105
1106 if (!plane)
1107 continue;
1108
1109 funcs = plane->helper_private;
1110
Thierry Reding3cad4b62014-11-25 13:05:12 +01001111 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001112 continue;
1113
Thierry Redingf1c37e12014-11-25 12:09:44 +01001114 old_plane_state = old_state->plane_states[i];
1115
Thierry Reding407b8bd2014-11-20 12:05:50 +01001116 /*
1117 * Special-case disabling the plane if drivers support it.
1118 */
1119 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1120 funcs->atomic_disable)
1121 funcs->atomic_disable(plane, old_plane_state);
1122 else
1123 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001124 }
1125
1126 for (i = 0; i < ncrtcs; i++) {
1127 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001128 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001129
1130 if (!crtc)
1131 continue;
1132
1133 funcs = crtc->helper_private;
1134
1135 if (!funcs || !funcs->atomic_flush)
1136 continue;
1137
1138 funcs->atomic_flush(crtc);
1139 }
1140}
1141EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1142
1143/**
1144 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1145 * @dev: DRM device
1146 * @old_state: atomic state object with old state structures
1147 *
1148 * This function cleans up plane state, specifically framebuffers, from the old
1149 * configuration. Hence the old configuration must be perserved in @old_state to
1150 * be able to call this function.
1151 *
1152 * This function must also be called on the new state when the atomic update
1153 * fails at any point after calling drm_atomic_helper_prepare_planes().
1154 */
1155void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1156 struct drm_atomic_state *old_state)
1157{
1158 int nplanes = dev->mode_config.num_total_plane;
1159 int i;
1160
1161 for (i = 0; i < nplanes; i++) {
1162 struct drm_plane_helper_funcs *funcs;
1163 struct drm_plane *plane = old_state->planes[i];
1164 struct drm_framebuffer *old_fb;
1165
1166 if (!plane)
1167 continue;
1168
1169 funcs = plane->helper_private;
1170
1171 old_fb = old_state->plane_states[i]->fb;
1172
1173 if (old_fb && funcs->cleanup_fb)
1174 funcs->cleanup_fb(plane, old_fb);
1175 }
1176}
1177EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1178
1179/**
1180 * drm_atomic_helper_swap_state - store atomic state into current sw state
1181 * @dev: DRM device
1182 * @state: atomic state
1183 *
1184 * This function stores the atomic state into the current state pointers in all
1185 * driver objects. It should be called after all failing steps have been done
1186 * and succeeded, but before the actual hardware state is committed.
1187 *
1188 * For cleanup and error recovery the current state for all changed objects will
1189 * be swaped into @state.
1190 *
1191 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1192 *
1193 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1194 *
1195 * 2. Do any other steps that might fail.
1196 *
1197 * 3. Put the staged state into the current state pointers with this function.
1198 *
1199 * 4. Actually commit the hardware state.
1200 *
1201 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1202 * contains the old state. Also do any other cleanup required with that state.
1203 */
1204void drm_atomic_helper_swap_state(struct drm_device *dev,
1205 struct drm_atomic_state *state)
1206{
1207 int i;
1208
1209 for (i = 0; i < dev->mode_config.num_connector; i++) {
1210 struct drm_connector *connector = state->connectors[i];
1211
1212 if (!connector)
1213 continue;
1214
1215 connector->state->state = state;
1216 swap(state->connector_states[i], connector->state);
1217 connector->state->state = NULL;
1218 }
1219
1220 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1221 struct drm_crtc *crtc = state->crtcs[i];
1222
1223 if (!crtc)
1224 continue;
1225
1226 crtc->state->state = state;
1227 swap(state->crtc_states[i], crtc->state);
1228 crtc->state->state = NULL;
1229 }
1230
1231 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1232 struct drm_plane *plane = state->planes[i];
1233
1234 if (!plane)
1235 continue;
1236
1237 plane->state->state = state;
1238 swap(state->plane_states[i], plane->state);
1239 plane->state->state = NULL;
1240 }
1241}
1242EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001243
1244/**
1245 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1246 * @plane: plane object to update
1247 * @crtc: owning CRTC of owning plane
1248 * @fb: framebuffer to flip onto plane
1249 * @crtc_x: x offset of primary plane on crtc
1250 * @crtc_y: y offset of primary plane on crtc
1251 * @crtc_w: width of primary plane rectangle on crtc
1252 * @crtc_h: height of primary plane rectangle on crtc
1253 * @src_x: x offset of @fb for panning
1254 * @src_y: y offset of @fb for panning
1255 * @src_w: width of source rectangle in @fb
1256 * @src_h: height of source rectangle in @fb
1257 *
1258 * Provides a default plane update handler using the atomic driver interface.
1259 *
1260 * RETURNS:
1261 * Zero on success, error code on failure
1262 */
1263int drm_atomic_helper_update_plane(struct drm_plane *plane,
1264 struct drm_crtc *crtc,
1265 struct drm_framebuffer *fb,
1266 int crtc_x, int crtc_y,
1267 unsigned int crtc_w, unsigned int crtc_h,
1268 uint32_t src_x, uint32_t src_y,
1269 uint32_t src_w, uint32_t src_h)
1270{
1271 struct drm_atomic_state *state;
1272 struct drm_plane_state *plane_state;
1273 int ret = 0;
1274
1275 state = drm_atomic_state_alloc(plane->dev);
1276 if (!state)
1277 return -ENOMEM;
1278
1279 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1280retry:
1281 plane_state = drm_atomic_get_plane_state(state, plane);
1282 if (IS_ERR(plane_state)) {
1283 ret = PTR_ERR(plane_state);
1284 goto fail;
1285 }
1286
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001287 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001288 if (ret != 0)
1289 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001290 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001291 plane_state->crtc_x = crtc_x;
1292 plane_state->crtc_y = crtc_y;
1293 plane_state->crtc_h = crtc_h;
1294 plane_state->crtc_w = crtc_w;
1295 plane_state->src_x = src_x;
1296 plane_state->src_y = src_y;
1297 plane_state->src_h = src_h;
1298 plane_state->src_w = src_w;
1299
1300 ret = drm_atomic_commit(state);
1301 if (ret != 0)
1302 goto fail;
1303
1304 /* Driver takes ownership of state on successful commit. */
1305 return 0;
1306fail:
1307 if (ret == -EDEADLK)
1308 goto backoff;
1309
1310 drm_atomic_state_free(state);
1311
1312 return ret;
1313backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001314 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001315 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001316
1317 /*
1318 * Someone might have exchanged the framebuffer while we dropped locks
1319 * in the backoff code. We need to fix up the fb refcount tracking the
1320 * core does for us.
1321 */
1322 plane->old_fb = plane->fb;
1323
1324 goto retry;
1325}
1326EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1327
1328/**
1329 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1330 * @plane: plane to disable
1331 *
1332 * Provides a default plane disable handler using the atomic driver interface.
1333 *
1334 * RETURNS:
1335 * Zero on success, error code on failure
1336 */
1337int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1338{
1339 struct drm_atomic_state *state;
1340 struct drm_plane_state *plane_state;
1341 int ret = 0;
1342
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001343 /*
1344 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1345 * acquire context. The real fix will be to wire the acquire ctx through
1346 * everywhere we need it, but meanwhile prevent chaos by just skipping
1347 * this noop. The critical case is the cursor ioctls which a) only grab
1348 * crtc/cursor-plane locks (so we need the crtc to get at the right
1349 * acquire context) and b) can try to disable the plane multiple times.
1350 */
1351 if (!plane->crtc)
1352 return 0;
1353
Daniel Vetter042652e2014-07-27 13:46:52 +02001354 state = drm_atomic_state_alloc(plane->dev);
1355 if (!state)
1356 return -ENOMEM;
1357
1358 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1359retry:
1360 plane_state = drm_atomic_get_plane_state(state, plane);
1361 if (IS_ERR(plane_state)) {
1362 ret = PTR_ERR(plane_state);
1363 goto fail;
1364 }
1365
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001366 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001367 if (ret != 0)
1368 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001369 drm_atomic_set_fb_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001370 plane_state->crtc_x = 0;
1371 plane_state->crtc_y = 0;
1372 plane_state->crtc_h = 0;
1373 plane_state->crtc_w = 0;
1374 plane_state->src_x = 0;
1375 plane_state->src_y = 0;
1376 plane_state->src_h = 0;
1377 plane_state->src_w = 0;
1378
1379 ret = drm_atomic_commit(state);
1380 if (ret != 0)
1381 goto fail;
1382
1383 /* Driver takes ownership of state on successful commit. */
1384 return 0;
1385fail:
1386 if (ret == -EDEADLK)
1387 goto backoff;
1388
1389 drm_atomic_state_free(state);
1390
1391 return ret;
1392backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001393 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001394 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001395
1396 /*
1397 * Someone might have exchanged the framebuffer while we dropped locks
1398 * in the backoff code. We need to fix up the fb refcount tracking the
1399 * core does for us.
1400 */
1401 plane->old_fb = plane->fb;
1402
1403 goto retry;
1404}
1405EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1406
1407static int update_output_state(struct drm_atomic_state *state,
1408 struct drm_mode_set *set)
1409{
1410 struct drm_device *dev = set->crtc->dev;
1411 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001412 int ncrtcs = state->dev->mode_config.num_crtc;
1413 int ret, i, j;
1414
1415 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1416 state->acquire_ctx);
1417 if (ret)
1418 return ret;
1419
1420 /* First grab all affected connector/crtc states. */
1421 for (i = 0; i < set->num_connectors; i++) {
1422 conn_state = drm_atomic_get_connector_state(state,
1423 set->connectors[i]);
1424 if (IS_ERR(conn_state))
1425 return PTR_ERR(conn_state);
1426 }
1427
1428 for (i = 0; i < ncrtcs; i++) {
1429 struct drm_crtc *crtc = state->crtcs[i];
1430
1431 if (!crtc)
1432 continue;
1433
1434 ret = drm_atomic_add_affected_connectors(state, crtc);
1435 if (ret)
1436 return ret;
1437 }
1438
1439 /* Then recompute connector->crtc links and crtc enabling state. */
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001440 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001441 struct drm_connector *connector;
1442
1443 connector = state->connectors[i];
1444 conn_state = state->connector_states[i];
1445
1446 if (!connector)
1447 continue;
1448
1449 if (conn_state->crtc == set->crtc) {
1450 ret = drm_atomic_set_crtc_for_connector(conn_state,
1451 NULL);
1452 if (ret)
1453 return ret;
1454 }
1455
1456 for (j = 0; j < set->num_connectors; j++) {
1457 if (set->connectors[j] == connector) {
1458 ret = drm_atomic_set_crtc_for_connector(conn_state,
1459 set->crtc);
1460 if (ret)
1461 return ret;
1462 break;
1463 }
1464 }
1465 }
1466
1467 for (i = 0; i < ncrtcs; i++) {
1468 struct drm_crtc *crtc = state->crtcs[i];
1469 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1470
1471 if (!crtc)
1472 continue;
1473
1474 /* Don't update ->enable for the CRTC in the set_config request,
1475 * since a mismatch would indicate a bug in the upper layers.
1476 * The actual modeset code later on will catch any
1477 * inconsistencies here. */
1478 if (crtc == set->crtc)
1479 continue;
1480
1481 crtc_state->enable =
1482 drm_atomic_connectors_for_crtc(state, crtc);
1483 }
1484
1485 return 0;
1486}
1487
1488/**
1489 * drm_atomic_helper_set_config - set a new config from userspace
1490 * @set: mode set configuration
1491 *
1492 * Provides a default crtc set_config handler using the atomic driver interface.
1493 *
1494 * Returns:
1495 * Returns 0 on success, negative errno numbers on failure.
1496 */
1497int drm_atomic_helper_set_config(struct drm_mode_set *set)
1498{
1499 struct drm_atomic_state *state;
1500 struct drm_crtc *crtc = set->crtc;
1501 struct drm_crtc_state *crtc_state;
1502 struct drm_plane_state *primary_state;
1503 int ret = 0;
1504
1505 state = drm_atomic_state_alloc(crtc->dev);
1506 if (!state)
1507 return -ENOMEM;
1508
1509 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1510retry:
1511 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1512 if (IS_ERR(crtc_state)) {
1513 ret = PTR_ERR(crtc_state);
1514 goto fail;
1515 }
1516
Rob Clarke5b53412014-11-26 18:58:04 -05001517 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1518 if (IS_ERR(primary_state)) {
1519 ret = PTR_ERR(primary_state);
1520 goto fail;
1521 }
1522
Daniel Vetter042652e2014-07-27 13:46:52 +02001523 if (!set->mode) {
1524 WARN_ON(set->fb);
1525 WARN_ON(set->num_connectors);
1526
1527 crtc_state->enable = false;
Rob Clarke5b53412014-11-26 18:58:04 -05001528
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001529 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
Rob Clarke5b53412014-11-26 18:58:04 -05001530 if (ret != 0)
1531 goto fail;
1532
1533 drm_atomic_set_fb_for_plane(primary_state, NULL);
1534
Daniel Vetter042652e2014-07-27 13:46:52 +02001535 goto commit;
1536 }
1537
1538 WARN_ON(!set->fb);
1539 WARN_ON(!set->num_connectors);
1540
1541 crtc_state->enable = true;
1542 drm_mode_copy(&crtc_state->mode, set->mode);
1543
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001544 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001545 if (ret != 0)
1546 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001547 drm_atomic_set_fb_for_plane(primary_state, set->fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001548 primary_state->crtc_x = 0;
1549 primary_state->crtc_y = 0;
1550 primary_state->crtc_h = set->mode->vdisplay;
1551 primary_state->crtc_w = set->mode->hdisplay;
1552 primary_state->src_x = set->x << 16;
1553 primary_state->src_y = set->y << 16;
1554 primary_state->src_h = set->mode->vdisplay << 16;
1555 primary_state->src_w = set->mode->hdisplay << 16;
1556
1557commit:
1558 ret = update_output_state(state, set);
1559 if (ret)
1560 goto fail;
1561
1562 ret = drm_atomic_commit(state);
1563 if (ret != 0)
1564 goto fail;
1565
1566 /* Driver takes ownership of state on successful commit. */
1567 return 0;
1568fail:
1569 if (ret == -EDEADLK)
1570 goto backoff;
1571
1572 drm_atomic_state_free(state);
1573
1574 return ret;
1575backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001576 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001577 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001578
1579 /*
1580 * Someone might have exchanged the framebuffer while we dropped locks
1581 * in the backoff code. We need to fix up the fb refcount tracking the
1582 * core does for us.
1583 */
1584 crtc->primary->old_fb = crtc->primary->fb;
1585
1586 goto retry;
1587}
1588EXPORT_SYMBOL(drm_atomic_helper_set_config);
1589
1590/**
1591 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1592 * @crtc: DRM crtc
1593 * @property: DRM property
1594 * @val: value of property
1595 *
1596 * Provides a default plane disablle handler using the atomic driver interface.
1597 *
1598 * RETURNS:
1599 * Zero on success, error code on failure
1600 */
1601int
1602drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1603 struct drm_property *property,
1604 uint64_t val)
1605{
1606 struct drm_atomic_state *state;
1607 struct drm_crtc_state *crtc_state;
1608 int ret = 0;
1609
1610 state = drm_atomic_state_alloc(crtc->dev);
1611 if (!state)
1612 return -ENOMEM;
1613
1614 /* ->set_property is always called with all locks held. */
1615 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1616retry:
1617 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1618 if (IS_ERR(crtc_state)) {
1619 ret = PTR_ERR(crtc_state);
1620 goto fail;
1621 }
1622
Rob Clark40ecc692014-12-18 16:01:46 -05001623 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1624 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001625 if (ret)
1626 goto fail;
1627
1628 ret = drm_atomic_commit(state);
1629 if (ret != 0)
1630 goto fail;
1631
1632 /* Driver takes ownership of state on successful commit. */
1633 return 0;
1634fail:
1635 if (ret == -EDEADLK)
1636 goto backoff;
1637
1638 drm_atomic_state_free(state);
1639
1640 return ret;
1641backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001642 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001643 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001644
1645 goto retry;
1646}
1647EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1648
1649/**
1650 * drm_atomic_helper_plane_set_property - helper for plane prorties
1651 * @plane: DRM plane
1652 * @property: DRM property
1653 * @val: value of property
1654 *
1655 * Provides a default plane disable handler using the atomic driver interface.
1656 *
1657 * RETURNS:
1658 * Zero on success, error code on failure
1659 */
1660int
1661drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1662 struct drm_property *property,
1663 uint64_t val)
1664{
1665 struct drm_atomic_state *state;
1666 struct drm_plane_state *plane_state;
1667 int ret = 0;
1668
1669 state = drm_atomic_state_alloc(plane->dev);
1670 if (!state)
1671 return -ENOMEM;
1672
1673 /* ->set_property is always called with all locks held. */
1674 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1675retry:
1676 plane_state = drm_atomic_get_plane_state(state, plane);
1677 if (IS_ERR(plane_state)) {
1678 ret = PTR_ERR(plane_state);
1679 goto fail;
1680 }
1681
Rob Clark40ecc692014-12-18 16:01:46 -05001682 ret = drm_atomic_plane_set_property(plane, plane_state,
1683 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001684 if (ret)
1685 goto fail;
1686
1687 ret = drm_atomic_commit(state);
1688 if (ret != 0)
1689 goto fail;
1690
1691 /* Driver takes ownership of state on successful commit. */
1692 return 0;
1693fail:
1694 if (ret == -EDEADLK)
1695 goto backoff;
1696
1697 drm_atomic_state_free(state);
1698
1699 return ret;
1700backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001701 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001702 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001703
1704 goto retry;
1705}
1706EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1707
1708/**
1709 * drm_atomic_helper_connector_set_property - helper for connector prorties
1710 * @connector: DRM connector
1711 * @property: DRM property
1712 * @val: value of property
1713 *
1714 * Provides a default plane disablle handler using the atomic driver interface.
1715 *
1716 * RETURNS:
1717 * Zero on success, error code on failure
1718 */
1719int
1720drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1721 struct drm_property *property,
1722 uint64_t val)
1723{
1724 struct drm_atomic_state *state;
1725 struct drm_connector_state *connector_state;
1726 int ret = 0;
1727
1728 state = drm_atomic_state_alloc(connector->dev);
1729 if (!state)
1730 return -ENOMEM;
1731
1732 /* ->set_property is always called with all locks held. */
1733 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1734retry:
1735 connector_state = drm_atomic_get_connector_state(state, connector);
1736 if (IS_ERR(connector_state)) {
1737 ret = PTR_ERR(connector_state);
1738 goto fail;
1739 }
1740
Rob Clark40ecc692014-12-18 16:01:46 -05001741 ret = drm_atomic_connector_set_property(connector, connector_state,
1742 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001743 if (ret)
1744 goto fail;
1745
1746 ret = drm_atomic_commit(state);
1747 if (ret != 0)
1748 goto fail;
1749
1750 /* Driver takes ownership of state on successful commit. */
1751 return 0;
1752fail:
1753 if (ret == -EDEADLK)
1754 goto backoff;
1755
1756 drm_atomic_state_free(state);
1757
1758 return ret;
1759backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001760 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001761 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001762
1763 goto retry;
1764}
1765EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001766
1767/**
1768 * drm_atomic_helper_page_flip - execute a legacy page flip
1769 * @crtc: DRM crtc
1770 * @fb: DRM framebuffer
1771 * @event: optional DRM event to signal upon completion
1772 * @flags: flip flags for non-vblank sync'ed updates
1773 *
1774 * Provides a default page flip implementation using the atomic driver interface.
1775 *
1776 * Note that for now so called async page flips (i.e. updates which are not
1777 * synchronized to vblank) are not supported, since the atomic interfaces have
1778 * no provisions for this yet.
1779 *
1780 * Returns:
1781 * Returns 0 on success, negative errno numbers on failure.
1782 */
1783int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1784 struct drm_framebuffer *fb,
1785 struct drm_pending_vblank_event *event,
1786 uint32_t flags)
1787{
1788 struct drm_plane *plane = crtc->primary;
1789 struct drm_atomic_state *state;
1790 struct drm_plane_state *plane_state;
1791 struct drm_crtc_state *crtc_state;
1792 int ret = 0;
1793
1794 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1795 return -EINVAL;
1796
1797 state = drm_atomic_state_alloc(plane->dev);
1798 if (!state)
1799 return -ENOMEM;
1800
1801 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1802retry:
1803 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1804 if (IS_ERR(crtc_state)) {
1805 ret = PTR_ERR(crtc_state);
1806 goto fail;
1807 }
1808 crtc_state->event = event;
1809
1810 plane_state = drm_atomic_get_plane_state(state, plane);
1811 if (IS_ERR(plane_state)) {
1812 ret = PTR_ERR(plane_state);
1813 goto fail;
1814 }
1815
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001816 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001817 if (ret != 0)
1818 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001819 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001820
1821 ret = drm_atomic_async_commit(state);
1822 if (ret != 0)
1823 goto fail;
1824
1825 /* TODO: ->page_flip is the only driver callback where the core
1826 * doesn't update plane->fb. For now patch it up here. */
1827 plane->fb = plane->state->fb;
1828
1829 /* Driver takes ownership of state on successful async commit. */
1830 return 0;
1831fail:
1832 if (ret == -EDEADLK)
1833 goto backoff;
1834
1835 drm_atomic_state_free(state);
1836
1837 return ret;
1838backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001839 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001840 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001841
1842 /*
1843 * Someone might have exchanged the framebuffer while we dropped locks
1844 * in the backoff code. We need to fix up the fb refcount tracking the
1845 * core does for us.
1846 */
1847 plane->old_fb = plane->fb;
1848
1849 goto retry;
1850}
1851EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01001852
1853/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01001854 * DOC: atomic state reset and initialization
1855 *
1856 * Both the drm core and the atomic helpers assume that there is always the full
1857 * and correct atomic software state for all connectors, CRTCs and planes
1858 * available. Which is a bit a problem on driver load and also after system
1859 * suspend. One way to solve this is to have a hardware state read-out
1860 * infrastructure which reconstructs the full software state (e.g. the i915
1861 * driver).
1862 *
1863 * The simpler solution is to just reset the software state to everything off,
1864 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
1865 * the atomic helpers provide default reset implementations for all hooks.
1866 */
1867
1868/**
Daniel Vetterd4617012014-11-03 15:56:43 +01001869 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
1870 * @crtc: drm CRTC
1871 *
1872 * Resets the atomic state for @crtc by freeing the state pointer (which might
1873 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1874 */
1875void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
1876{
1877 kfree(crtc->state);
1878 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001879
1880 if (crtc->state)
1881 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01001882}
1883EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
1884
1885/**
1886 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
1887 * @crtc: drm CRTC
1888 *
1889 * Default CRTC state duplicate hook for drivers which don't have their own
1890 * subclassed CRTC state structure.
1891 */
1892struct drm_crtc_state *
1893drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
1894{
1895 struct drm_crtc_state *state;
1896
1897 if (WARN_ON(!crtc->state))
1898 return NULL;
1899
1900 state = kmemdup(crtc->state, sizeof(*crtc->state), GFP_KERNEL);
1901
1902 if (state) {
1903 state->mode_changed = false;
1904 state->planes_changed = false;
1905 state->event = NULL;
1906 }
1907
1908 return state;
1909}
1910EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
1911
1912/**
1913 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
1914 * @crtc: drm CRTC
1915 * @state: CRTC state object to release
1916 *
1917 * Default CRTC state destroy hook for drivers which don't have their own
1918 * subclassed CRTC state structure.
1919 */
1920void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
1921 struct drm_crtc_state *state)
1922{
1923 kfree(state);
1924}
1925EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
1926
1927/**
1928 * drm_atomic_helper_plane_reset - default ->reset hook for planes
1929 * @plane: drm plane
1930 *
1931 * Resets the atomic state for @plane by freeing the state pointer (which might
1932 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1933 */
1934void drm_atomic_helper_plane_reset(struct drm_plane *plane)
1935{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001936 if (plane->state && plane->state->fb)
1937 drm_framebuffer_unreference(plane->state->fb);
1938
Daniel Vetterd4617012014-11-03 15:56:43 +01001939 kfree(plane->state);
1940 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001941
1942 if (plane->state)
1943 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01001944}
1945EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
1946
1947/**
1948 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
1949 * @plane: drm plane
1950 *
1951 * Default plane state duplicate hook for drivers which don't have their own
1952 * subclassed plane state structure.
1953 */
1954struct drm_plane_state *
1955drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
1956{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001957 struct drm_plane_state *state;
1958
Daniel Vetterd4617012014-11-03 15:56:43 +01001959 if (WARN_ON(!plane->state))
1960 return NULL;
1961
Daniel Vetter321ebf02014-11-04 22:57:27 +01001962 state = kmemdup(plane->state, sizeof(*plane->state), GFP_KERNEL);
1963
1964 if (state && state->fb)
1965 drm_framebuffer_reference(state->fb);
1966
1967 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01001968}
1969EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
1970
1971/**
1972 * drm_atomic_helper_plane_destroy_state - default state destroy hook
1973 * @plane: drm plane
1974 * @state: plane state object to release
1975 *
1976 * Default plane state destroy hook for drivers which don't have their own
1977 * subclassed plane state structure.
1978 */
1979void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01001980 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01001981{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001982 if (state->fb)
1983 drm_framebuffer_unreference(state->fb);
1984
Daniel Vetterd4617012014-11-03 15:56:43 +01001985 kfree(state);
1986}
1987EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
1988
1989/**
1990 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
1991 * @connector: drm connector
1992 *
1993 * Resets the atomic state for @connector by freeing the state pointer (which
1994 * might be NULL, e.g. at driver load time) and allocating a new empty state
1995 * object.
1996 */
1997void drm_atomic_helper_connector_reset(struct drm_connector *connector)
1998{
1999 kfree(connector->state);
2000 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002001
2002 if (connector->state)
2003 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002004}
2005EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2006
2007/**
2008 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2009 * @connector: drm connector
2010 *
2011 * Default connector state duplicate hook for drivers which don't have their own
2012 * subclassed connector state structure.
2013 */
2014struct drm_connector_state *
2015drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2016{
2017 if (WARN_ON(!connector->state))
2018 return NULL;
2019
2020 return kmemdup(connector->state, sizeof(*connector->state), GFP_KERNEL);
2021}
2022EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2023
2024/**
2025 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2026 * @connector: drm connector
2027 * @state: connector state object to release
2028 *
2029 * Default connector state destroy hook for drivers which don't have their own
2030 * subclassed connector state structure.
2031 */
2032void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2033 struct drm_connector_state *state)
2034{
2035 kfree(state);
2036}
2037EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);