1/*
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 <linux/dma-fence.h>
29#include <linux/ktime.h>
30
31#include <drm/drm_atomic.h>
32#include <drm/drm_atomic_helper.h>
33#include <drm/drm_atomic_uapi.h>
34#include <drm/drm_blend.h>
35#include <drm/drm_bridge.h>
36#include <drm/drm_damage_helper.h>
37#include <drm/drm_device.h>
38#include <drm/drm_drv.h>
39#include <drm/drm_framebuffer.h>
40#include <drm/drm_gem_atomic_helper.h>
41#include <drm/drm_print.h>
42#include <drm/drm_self_refresh_helper.h>
43#include <drm/drm_vblank.h>
44#include <drm/drm_writeback.h>
45
46#include "drm_crtc_helper_internal.h"
47#include "drm_crtc_internal.h"
48
49/**
50 * DOC: overview
51 *
52 * This helper library provides implementations of check and commit functions on
53 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
54 * also provides convenience implementations for the atomic state handling
55 * callbacks for drivers which don't need to subclass the drm core structures to
56 * add their own additional internal state.
57 *
58 * This library also provides default implementations for the check callback in
59 * drm_atomic_helper_check() and for the commit callback with
60 * drm_atomic_helper_commit(). But the individual stages and callbacks are
61 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
62 * together with a driver private modeset implementation.
63 *
64 * This library also provides implementations for all the legacy driver
65 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
66 * drm_atomic_helper_disable_plane(), and the various functions to implement
67 * set_property callbacks. New drivers must not implement these functions
68 * themselves but must use the provided helpers.
69 *
70 * The atomic helper uses the same function table structures as all other
71 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
72 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
73 * also shares the &struct drm_plane_helper_funcs function table with the plane
74 * helpers.
75 */
76static void
77drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
78				struct drm_plane_state *old_plane_state,
79				struct drm_plane_state *plane_state,
80				struct drm_plane *plane)
81{
82	struct drm_crtc_state *crtc_state;
83
84	if (old_plane_state->crtc) {
85		crtc_state = drm_atomic_get_new_crtc_state(state,
86							   old_plane_state->crtc);
87
88		if (WARN_ON(!crtc_state))
89			return;
90
91		crtc_state->planes_changed = true;
92	}
93
94	if (plane_state->crtc) {
95		crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
96
97		if (WARN_ON(!crtc_state))
98			return;
99
100		crtc_state->planes_changed = true;
101	}
102}
103
104static int handle_conflicting_encoders(struct drm_atomic_state *state,
105				       bool disable_conflicting_encoders)
106{
107	struct drm_connector_state *new_conn_state;
108	struct drm_connector *connector;
109	struct drm_connector_list_iter conn_iter;
110	struct drm_encoder *encoder;
111	unsigned int encoder_mask = 0;
112	int i, ret = 0;
113
114	/*
115	 * First loop, find all newly assigned encoders from the connectors
116	 * part of the state. If the same encoder is assigned to multiple
117	 * connectors bail out.
118	 */
119	for_each_new_connector_in_state(state, connector, new_conn_state, i) {
120		const struct drm_connector_helper_funcs *funcs = connector->helper_private;
121		struct drm_encoder *new_encoder;
122
123		if (!new_conn_state->crtc)
124			continue;
125
126		if (funcs->atomic_best_encoder)
127			new_encoder = funcs->atomic_best_encoder(connector,
128								 state);
129		else if (funcs->best_encoder)
130			new_encoder = funcs->best_encoder(connector);
131		else
132			new_encoder = drm_connector_get_single_encoder(connector);
133
134		if (new_encoder) {
135			if (encoder_mask & drm_encoder_mask(new_encoder)) {
136				drm_dbg_atomic(connector->dev,
137					       "[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
138					       new_encoder->base.id, new_encoder->name,
139					       connector->base.id, connector->name);
140
141				return -EINVAL;
142			}
143
144			encoder_mask |= drm_encoder_mask(new_encoder);
145		}
146	}
147
148	if (!encoder_mask)
149		return 0;
150
151	/*
152	 * Second loop, iterate over all connectors not part of the state.
153	 *
154	 * If a conflicting encoder is found and disable_conflicting_encoders
155	 * is not set, an error is returned. Userspace can provide a solution
156	 * through the atomic ioctl.
157	 *
158	 * If the flag is set conflicting connectors are removed from the CRTC
159	 * and the CRTC is disabled if no encoder is left. This preserves
160	 * compatibility with the legacy set_config behavior.
161	 */
162	drm_connector_list_iter_begin(state->dev, &conn_iter);
163	drm_for_each_connector_iter(connector, &conn_iter) {
164		struct drm_crtc_state *crtc_state;
165
166		if (drm_atomic_get_new_connector_state(state, connector))
167			continue;
168
169		encoder = connector->state->best_encoder;
170		if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
171			continue;
172
173		if (!disable_conflicting_encoders) {
174			drm_dbg_atomic(connector->dev,
175				       "[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
176				       encoder->base.id, encoder->name,
177				       connector->state->crtc->base.id,
178				       connector->state->crtc->name,
179				       connector->base.id, connector->name);
180			ret = -EINVAL;
181			goto out;
182		}
183
184		new_conn_state = drm_atomic_get_connector_state(state, connector);
185		if (IS_ERR(new_conn_state)) {
186			ret = PTR_ERR(new_conn_state);
187			goto out;
188		}
189
190		drm_dbg_atomic(connector->dev,
191			       "[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
192			       encoder->base.id, encoder->name,
193			       new_conn_state->crtc->base.id, new_conn_state->crtc->name,
194			       connector->base.id, connector->name);
195
196		crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
197
198		ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
199		if (ret)
200			goto out;
201
202		if (!crtc_state->connector_mask) {
203			ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
204								NULL);
205			if (ret < 0)
206				goto out;
207
208			crtc_state->active = false;
209		}
210	}
211out:
212	drm_connector_list_iter_end(&conn_iter);
213
214	return ret;
215}
216
217static void
218set_best_encoder(struct drm_atomic_state *state,
219		 struct drm_connector_state *conn_state,
220		 struct drm_encoder *encoder)
221{
222	struct drm_crtc_state *crtc_state;
223	struct drm_crtc *crtc;
224
225	if (conn_state->best_encoder) {
226		/* Unset the encoder_mask in the old crtc state. */
227		crtc = conn_state->connector->state->crtc;
228
229		/* A NULL crtc is an error here because we should have
230		 * duplicated a NULL best_encoder when crtc was NULL.
231		 * As an exception restoring duplicated atomic state
232		 * during resume is allowed, so don't warn when
233		 * best_encoder is equal to encoder we intend to set.
234		 */
235		WARN_ON(!crtc && encoder != conn_state->best_encoder);
236		if (crtc) {
237			crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
238
239			crtc_state->encoder_mask &=
240				~drm_encoder_mask(conn_state->best_encoder);
241		}
242	}
243
244	if (encoder) {
245		crtc = conn_state->crtc;
246		WARN_ON(!crtc);
247		if (crtc) {
248			crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
249
250			crtc_state->encoder_mask |=
251				drm_encoder_mask(encoder);
252		}
253	}
254
255	conn_state->best_encoder = encoder;
256}
257
258static void
259steal_encoder(struct drm_atomic_state *state,
260	      struct drm_encoder *encoder)
261{
262	struct drm_crtc_state *crtc_state;
263	struct drm_connector *connector;
264	struct drm_connector_state *old_connector_state, *new_connector_state;
265	int i;
266
267	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
268		struct drm_crtc *encoder_crtc;
269
270		if (new_connector_state->best_encoder != encoder)
271			continue;
272
273		encoder_crtc = old_connector_state->crtc;
274
275		drm_dbg_atomic(encoder->dev,
276			       "[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
277			       encoder->base.id, encoder->name,
278			       encoder_crtc->base.id, encoder_crtc->name);
279
280		set_best_encoder(state, new_connector_state, NULL);
281
282		crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
283		crtc_state->connectors_changed = true;
284
285		return;
286	}
287}
288
289static int
290update_connector_routing(struct drm_atomic_state *state,
291			 struct drm_connector *connector,
292			 struct drm_connector_state *old_connector_state,
293			 struct drm_connector_state *new_connector_state,
294			 bool added_by_user)
295{
296	const struct drm_connector_helper_funcs *funcs;
297	struct drm_encoder *new_encoder;
298	struct drm_crtc_state *crtc_state;
299
300	drm_dbg_atomic(connector->dev, "Updating routing for [CONNECTOR:%d:%s]\n",
301		       connector->base.id, connector->name);
302
303	if (old_connector_state->crtc != new_connector_state->crtc) {
304		if (old_connector_state->crtc) {
305			crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
306			crtc_state->connectors_changed = true;
307		}
308
309		if (new_connector_state->crtc) {
310			crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
311			crtc_state->connectors_changed = true;
312		}
313	}
314
315	if (!new_connector_state->crtc) {
316		drm_dbg_atomic(connector->dev, "Disabling [CONNECTOR:%d:%s]\n",
317				connector->base.id, connector->name);
318
319		set_best_encoder(state, new_connector_state, NULL);
320
321		return 0;
322	}
323
324	crtc_state = drm_atomic_get_new_crtc_state(state,
325						   new_connector_state->crtc);
326	/*
327	 * For compatibility with legacy users, we want to make sure that
328	 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
329	 * which would result in anything else must be considered invalid, to
330	 * avoid turning on new displays on dead connectors.
331	 *
332	 * Since the connector can be unregistered at any point during an
333	 * atomic check or commit, this is racy. But that's OK: all we care
334	 * about is ensuring that userspace can't do anything but shut off the
335	 * display on a connector that was destroyed after it's been notified,
336	 * not before.
337	 *
338	 * Additionally, we also want to ignore connector registration when
339	 * we're trying to restore an atomic state during system resume since
340	 * there's a chance the connector may have been destroyed during the
341	 * process, but it's better to ignore that then cause
342	 * drm_atomic_helper_resume() to fail.
343	 *
344	 * Last, we want to ignore connector registration when the connector
345	 * was not pulled in the atomic state by user-space (ie, was pulled
346	 * in by the driver, e.g. when updating a DP-MST stream).
347	 */
348	if (!state->duplicated && drm_connector_is_unregistered(connector) &&
349	    added_by_user && crtc_state->active) {
350		drm_dbg_atomic(connector->dev,
351			       "[CONNECTOR:%d:%s] is not registered\n",
352			       connector->base.id, connector->name);
353		return -EINVAL;
354	}
355
356	funcs = connector->helper_private;
357
358	if (funcs->atomic_best_encoder)
359		new_encoder = funcs->atomic_best_encoder(connector, state);
360	else if (funcs->best_encoder)
361		new_encoder = funcs->best_encoder(connector);
362	else
363		new_encoder = drm_connector_get_single_encoder(connector);
364
365	if (!new_encoder) {
366		drm_dbg_atomic(connector->dev,
367			       "No suitable encoder found for [CONNECTOR:%d:%s]\n",
368			       connector->base.id, connector->name);
369		return -EINVAL;
370	}
371
372	if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
373		drm_dbg_atomic(connector->dev,
374			       "[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
375			       new_encoder->base.id,
376			       new_encoder->name,
377			       new_connector_state->crtc->base.id,
378			       new_connector_state->crtc->name);
379		return -EINVAL;
380	}
381
382	if (new_encoder == new_connector_state->best_encoder) {
383		set_best_encoder(state, new_connector_state, new_encoder);
384
385		drm_dbg_atomic(connector->dev,
386			       "[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
387			       connector->base.id,
388			       connector->name,
389			       new_encoder->base.id,
390			       new_encoder->name,
391			       new_connector_state->crtc->base.id,
392			       new_connector_state->crtc->name);
393
394		return 0;
395	}
396
397	steal_encoder(state, new_encoder);
398
399	set_best_encoder(state, new_connector_state, new_encoder);
400
401	crtc_state->connectors_changed = true;
402
403	drm_dbg_atomic(connector->dev,
404		       "[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
405		       connector->base.id,
406		       connector->name,
407		       new_encoder->base.id,
408		       new_encoder->name,
409		       new_connector_state->crtc->base.id,
410		       new_connector_state->crtc->name);
411
412	return 0;
413}
414
415static int
416mode_fixup(struct drm_atomic_state *state)
417{
418	struct drm_crtc *crtc;
419	struct drm_crtc_state *new_crtc_state;
420	struct drm_connector *connector;
421	struct drm_connector_state *new_conn_state;
422	int i;
423	int ret;
424
425	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
426		if (!new_crtc_state->mode_changed &&
427		    !new_crtc_state->connectors_changed)
428			continue;
429
430		drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
431	}
432
433	for_each_new_connector_in_state(state, connector, new_conn_state, i) {
434		const struct drm_encoder_helper_funcs *funcs;
435		struct drm_encoder *encoder;
436		struct drm_bridge *bridge;
437
438		WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
439
440		if (!new_conn_state->crtc || !new_conn_state->best_encoder)
441			continue;
442
443		new_crtc_state =
444			drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
445
446		/*
447		 * Each encoder has at most one connector (since we always steal
448		 * it away), so we won't call ->mode_fixup twice.
449		 */
450		encoder = new_conn_state->best_encoder;
451		funcs = encoder->helper_private;
452
453		bridge = drm_bridge_chain_get_first_bridge(encoder);
454		ret = drm_atomic_bridge_chain_check(bridge,
455						    new_crtc_state,
456						    new_conn_state);
457		if (ret) {
458			drm_dbg_atomic(encoder->dev, "Bridge atomic check failed\n");
459			return ret;
460		}
461
462		if (funcs && funcs->atomic_check) {
463			ret = funcs->atomic_check(encoder, new_crtc_state,
464						  new_conn_state);
465			if (ret) {
466				drm_dbg_atomic(encoder->dev,
467					       "[ENCODER:%d:%s] check failed\n",
468					       encoder->base.id, encoder->name);
469				return ret;
470			}
471		} else if (funcs && funcs->mode_fixup) {
472			ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
473						&new_crtc_state->adjusted_mode);
474			if (!ret) {
475				drm_dbg_atomic(encoder->dev,
476					       "[ENCODER:%d:%s] fixup failed\n",
477					       encoder->base.id, encoder->name);
478				return -EINVAL;
479			}
480		}
481	}
482
483	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
484		const struct drm_crtc_helper_funcs *funcs;
485
486		if (!new_crtc_state->enable)
487			continue;
488
489		if (!new_crtc_state->mode_changed &&
490		    !new_crtc_state->connectors_changed)
491			continue;
492
493		funcs = crtc->helper_private;
494		if (!funcs || !funcs->mode_fixup)
495			continue;
496
497		ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
498					&new_crtc_state->adjusted_mode);
499		if (!ret) {
500			drm_dbg_atomic(crtc->dev, "[CRTC:%d:%s] fixup failed\n",
501				       crtc->base.id, crtc->name);
502			return -EINVAL;
503		}
504	}
505
506	return 0;
507}
508
509static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
510					    struct drm_encoder *encoder,
511					    struct drm_crtc *crtc,
512					    const struct drm_display_mode *mode)
513{
514	struct drm_bridge *bridge;
515	enum drm_mode_status ret;
516
517	ret = drm_encoder_mode_valid(encoder, mode);
518	if (ret != MODE_OK) {
519		drm_dbg_atomic(encoder->dev,
520			       "[ENCODER:%d:%s] mode_valid() failed\n",
521			       encoder->base.id, encoder->name);
522		return ret;
523	}
524
525	bridge = drm_bridge_chain_get_first_bridge(encoder);
526	ret = drm_bridge_chain_mode_valid(bridge, &connector->display_info,
527					  mode);
528	if (ret != MODE_OK) {
529		drm_dbg_atomic(encoder->dev, "[BRIDGE] mode_valid() failed\n");
530		return ret;
531	}
532
533	ret = drm_crtc_mode_valid(crtc, mode);
534	if (ret != MODE_OK) {
535		drm_dbg_atomic(encoder->dev, "[CRTC:%d:%s] mode_valid() failed\n",
536			       crtc->base.id, crtc->name);
537		return ret;
538	}
539
540	return ret;
541}
542
543static int
544mode_valid(struct drm_atomic_state *state)
545{
546	struct drm_connector_state *conn_state;
547	struct drm_connector *connector;
548	int i;
549
550	for_each_new_connector_in_state(state, connector, conn_state, i) {
551		struct drm_encoder *encoder = conn_state->best_encoder;
552		struct drm_crtc *crtc = conn_state->crtc;
553		struct drm_crtc_state *crtc_state;
554		enum drm_mode_status mode_status;
555		const struct drm_display_mode *mode;
556
557		if (!crtc || !encoder)
558			continue;
559
560		crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
561		if (!crtc_state)
562			continue;
563		if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
564			continue;
565
566		mode = &crtc_state->mode;
567
568		mode_status = mode_valid_path(connector, encoder, crtc, mode);
569		if (mode_status != MODE_OK)
570			return -EINVAL;
571	}
572
573	return 0;
574}
575
576/**
577 * drm_atomic_helper_check_modeset - validate state object for modeset changes
578 * @dev: DRM device
579 * @state: the driver state object
580 *
581 * Check the state object to see if the requested state is physically possible.
582 * This does all the CRTC and connector related computations for an atomic
583 * update and adds any additional connectors needed for full modesets. It calls
584 * the various per-object callbacks in the follow order:
585 *
586 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
587 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
588 * 3. If it's determined a modeset is needed then all connectors on the affected
589 *    CRTC are added and &drm_connector_helper_funcs.atomic_check is run on them.
590 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
591 *    &drm_crtc_helper_funcs.mode_valid are called on the affected components.
592 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
593 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
594 *    This function is only called when the encoder will be part of a configured CRTC,
595 *    it must not be used for implementing connector property validation.
596 *    If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
597 *    instead.
598 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with CRTC constraints.
599 *
600 * &drm_crtc_state.mode_changed is set when the input mode is changed.
601 * &drm_crtc_state.connectors_changed is set when a connector is added or
602 * removed from the CRTC.  &drm_crtc_state.active_changed is set when
603 * &drm_crtc_state.active changes, which is used for DPMS.
604 * &drm_crtc_state.no_vblank is set from the result of drm_dev_has_vblank().
605 * See also: drm_atomic_crtc_needs_modeset()
606 *
607 * IMPORTANT:
608 *
609 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
610 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
611 * without a full modeset) _must_ call this function after that change. It is
612 * permitted to call this function multiple times for the same update, e.g.
613 * when the &drm_crtc_helper_funcs.atomic_check functions depend upon the
614 * adjusted dotclock for fifo space allocation and watermark computation.
615 *
616 * RETURNS:
617 * Zero for success or -errno
618 */
619int
620drm_atomic_helper_check_modeset(struct drm_device *dev,
621				struct drm_atomic_state *state)
622{
623	struct drm_crtc *crtc;
624	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
625	struct drm_connector *connector;
626	struct drm_connector_state *old_connector_state, *new_connector_state;
627	int i, ret;
628	unsigned int connectors_mask = 0, user_connectors_mask = 0;
629
630	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i)
631		user_connectors_mask |= BIT(i);
632
633	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
634		bool has_connectors =
635			!!new_crtc_state->connector_mask;
636
637		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
638
639		if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
640			drm_dbg_atomic(dev, "[CRTC:%d:%s] mode changed\n",
641				       crtc->base.id, crtc->name);
642			new_crtc_state->mode_changed = true;
643		}
644
645		if (old_crtc_state->enable != new_crtc_state->enable) {
646			drm_dbg_atomic(dev, "[CRTC:%d:%s] enable changed\n",
647				       crtc->base.id, crtc->name);
648
649			/*
650			 * For clarity this assignment is done here, but
651			 * enable == 0 is only true when there are no
652			 * connectors and a NULL mode.
653			 *
654			 * The other way around is true as well. enable != 0
655			 * implies that connectors are attached and a mode is set.
656			 */
657			new_crtc_state->mode_changed = true;
658			new_crtc_state->connectors_changed = true;
659		}
660
661		if (old_crtc_state->active != new_crtc_state->active) {
662			drm_dbg_atomic(dev, "[CRTC:%d:%s] active changed\n",
663				       crtc->base.id, crtc->name);
664			new_crtc_state->active_changed = true;
665		}
666
667		if (new_crtc_state->enable != has_connectors) {
668			drm_dbg_atomic(dev, "[CRTC:%d:%s] enabled/connectors mismatch\n",
669				       crtc->base.id, crtc->name);
670
671			return -EINVAL;
672		}
673
674		if (drm_dev_has_vblank(dev))
675			new_crtc_state->no_vblank = false;
676		else
677			new_crtc_state->no_vblank = true;
678	}
679
680	ret = handle_conflicting_encoders(state, false);
681	if (ret)
682		return ret;
683
684	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
685		const struct drm_connector_helper_funcs *funcs = connector->helper_private;
686
687		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
688
689		/*
690		 * This only sets crtc->connectors_changed for routing changes,
691		 * drivers must set crtc->connectors_changed themselves when
692		 * connector properties need to be updated.
693		 */
694		ret = update_connector_routing(state, connector,
695					       old_connector_state,
696					       new_connector_state,
697					       BIT(i) & user_connectors_mask);
698		if (ret)
699			return ret;
700		if (old_connector_state->crtc) {
701			new_crtc_state = drm_atomic_get_new_crtc_state(state,
702								       old_connector_state->crtc);
703			if (old_connector_state->link_status !=
704			    new_connector_state->link_status)
705				new_crtc_state->connectors_changed = true;
706
707			if (old_connector_state->max_requested_bpc !=
708			    new_connector_state->max_requested_bpc)
709				new_crtc_state->connectors_changed = true;
710		}
711
712		if (funcs->atomic_check)
713			ret = funcs->atomic_check(connector, state);
714		if (ret) {
715			drm_dbg_atomic(dev,
716				       "[CONNECTOR:%d:%s] driver check failed\n",
717				       connector->base.id, connector->name);
718			return ret;
719		}
720
721		connectors_mask |= BIT(i);
722	}
723
724	/*
725	 * After all the routing has been prepared we need to add in any
726	 * connector which is itself unchanged, but whose CRTC changes its
727	 * configuration. This must be done before calling mode_fixup in case a
728	 * crtc only changed its mode but has the same set of connectors.
729	 */
730	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
731		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
732			continue;
733
734		drm_dbg_atomic(dev,
735			       "[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
736			       crtc->base.id, crtc->name,
737			       new_crtc_state->enable ? 'y' : 'n',
738			       new_crtc_state->active ? 'y' : 'n');
739
740		ret = drm_atomic_add_affected_connectors(state, crtc);
741		if (ret != 0)
742			return ret;
743
744		ret = drm_atomic_add_affected_planes(state, crtc);
745		if (ret != 0)
746			return ret;
747	}
748
749	/*
750	 * Iterate over all connectors again, to make sure atomic_check()
751	 * has been called on them when a modeset is forced.
752	 */
753	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
754		const struct drm_connector_helper_funcs *funcs = connector->helper_private;
755
756		if (connectors_mask & BIT(i))
757			continue;
758
759		if (funcs->atomic_check)
760			ret = funcs->atomic_check(connector, state);
761		if (ret) {
762			drm_dbg_atomic(dev,
763				       "[CONNECTOR:%d:%s] driver check failed\n",
764				       connector->base.id, connector->name);
765			return ret;
766		}
767	}
768
769	/*
770	 * Iterate over all connectors again, and add all affected bridges to
771	 * the state.
772	 */
773	for_each_oldnew_connector_in_state(state, connector,
774					   old_connector_state,
775					   new_connector_state, i) {
776		struct drm_encoder *encoder;
777
778		encoder = old_connector_state->best_encoder;
779		ret = drm_atomic_add_encoder_bridges(state, encoder);
780		if (ret)
781			return ret;
782
783		encoder = new_connector_state->best_encoder;
784		ret = drm_atomic_add_encoder_bridges(state, encoder);
785		if (ret)
786			return ret;
787	}
788
789	ret = mode_valid(state);
790	if (ret)
791		return ret;
792
793	return mode_fixup(state);
794}
795EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
796
797/**
798 * drm_atomic_helper_check_wb_encoder_state() - Check writeback encoder state
799 * @encoder: encoder state to check
800 * @conn_state: connector state to check
801 *
802 * Checks if the writeback connector state is valid, and returns an error if it
803 * isn't.
804 *
805 * RETURNS:
806 * Zero for success or -errno
807 */
808int
809drm_atomic_helper_check_wb_encoder_state(struct drm_encoder *encoder,
810					 struct drm_connector_state *conn_state)
811{
812	struct drm_writeback_job *wb_job = conn_state->writeback_job;
813	struct drm_property_blob *pixel_format_blob;
814	struct drm_framebuffer *fb;
815	size_t i, nformats;
816	u32 *formats;
817
818	if (!wb_job || !wb_job->fb)
819		return 0;
820
821	pixel_format_blob = wb_job->connector->pixel_formats_blob_ptr;
822	nformats = pixel_format_blob->length / sizeof(u32);
823	formats = pixel_format_blob->data;
824	fb = wb_job->fb;
825
826	for (i = 0; i < nformats; i++)
827		if (fb->format->format == formats[i])
828			return 0;
829
830	drm_dbg_kms(encoder->dev, "Invalid pixel format %p4cc\n", &fb->format->format);
831
832	return -EINVAL;
833}
834EXPORT_SYMBOL(drm_atomic_helper_check_wb_encoder_state);
835
836/**
837 * drm_atomic_helper_check_plane_state() - Check plane state for validity
838 * @plane_state: plane state to check
839 * @crtc_state: CRTC state to check
840 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
841 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
842 * @can_position: is it legal to position the plane such that it
843 *                doesn't cover the entire CRTC?  This will generally
844 *                only be false for primary planes.
845 * @can_update_disabled: can the plane be updated while the CRTC
846 *                       is disabled?
847 *
848 * Checks that a desired plane update is valid, and updates various
849 * bits of derived state (clipped coordinates etc.). Drivers that provide
850 * their own plane handling rather than helper-provided implementations may
851 * still wish to call this function to avoid duplication of error checking
852 * code.
853 *
854 * RETURNS:
855 * Zero if update appears valid, error code on failure
856 */
857int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
858					const struct drm_crtc_state *crtc_state,
859					int min_scale,
860					int max_scale,
861					bool can_position,
862					bool can_update_disabled)
863{
864	struct drm_framebuffer *fb = plane_state->fb;
865	struct drm_rect *src = &plane_state->src;
866	struct drm_rect *dst = &plane_state->dst;
867	unsigned int rotation = plane_state->rotation;
868	struct drm_rect clip = {};
869	int hscale, vscale;
870
871	WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
872
873	*src = drm_plane_state_src(plane_state);
874	*dst = drm_plane_state_dest(plane_state);
875
876	if (!fb) {
877		plane_state->visible = false;
878		return 0;
879	}
880
881	/* crtc should only be NULL when disabling (i.e., !fb) */
882	if (WARN_ON(!plane_state->crtc)) {
883		plane_state->visible = false;
884		return 0;
885	}
886
887	if (!crtc_state->enable && !can_update_disabled) {
888		drm_dbg_kms(plane_state->plane->dev,
889			    "Cannot update plane of a disabled CRTC.\n");
890		return -EINVAL;
891	}
892
893	drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
894
895	/* Check scaling */
896	hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
897	vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
898	if (hscale < 0 || vscale < 0) {
899		drm_dbg_kms(plane_state->plane->dev,
900			    "Invalid scaling of plane\n");
901		drm_rect_debug_print("src: ", &plane_state->src, true);
902		drm_rect_debug_print("dst: ", &plane_state->dst, false);
903		return -ERANGE;
904	}
905
906	if (crtc_state->enable)
907		drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
908
909	plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
910
911	drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
912
913	if (!plane_state->visible)
914		/*
915		 * Plane isn't visible; some drivers can handle this
916		 * so we just return success here.  Drivers that can't
917		 * (including those that use the primary plane helper's
918		 * update function) will return an error from their
919		 * update_plane handler.
920		 */
921		return 0;
922
923	if (!can_position && !drm_rect_equals(dst, &clip)) {
924		drm_dbg_kms(plane_state->plane->dev,
925			    "Plane must cover entire CRTC\n");
926		drm_rect_debug_print("dst: ", dst, false);
927		drm_rect_debug_print("clip: ", &clip, false);
928		return -EINVAL;
929	}
930
931	return 0;
932}
933EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
934
935/**
936 * drm_atomic_helper_check_crtc_primary_plane() - Check CRTC state for primary plane
937 * @crtc_state: CRTC state to check
938 *
939 * Checks that a CRTC has at least one primary plane attached to it, which is
940 * a requirement on some hardware. Note that this only involves the CRTC side
941 * of the test. To test if the primary plane is visible or if it can be updated
942 * without the CRTC being enabled, use drm_atomic_helper_check_plane_state() in
943 * the plane's atomic check.
944 *
945 * RETURNS:
946 * 0 if a primary plane is attached to the CRTC, or an error code otherwise
947 */
948int drm_atomic_helper_check_crtc_primary_plane(struct drm_crtc_state *crtc_state)
949{
950	struct drm_crtc *crtc = crtc_state->crtc;
951	struct drm_device *dev = crtc->dev;
952	struct drm_plane *plane;
953
954	/* needs at least one primary plane to be enabled */
955	drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
956		if (plane->type == DRM_PLANE_TYPE_PRIMARY)
957			return 0;
958	}
959
960	drm_dbg_atomic(dev, "[CRTC:%d:%s] primary plane missing\n", crtc->base.id, crtc->name);
961
962	return -EINVAL;
963}
964EXPORT_SYMBOL(drm_atomic_helper_check_crtc_primary_plane);
965
966/**
967 * drm_atomic_helper_check_planes - validate state object for planes changes
968 * @dev: DRM device
969 * @state: the driver state object
970 *
971 * Check the state object to see if the requested state is physically possible.
972 * This does all the plane update related checks using by calling into the
973 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
974 * hooks provided by the driver.
975 *
976 * It also sets &drm_crtc_state.planes_changed to indicate that a CRTC has
977 * updated planes.
978 *
979 * RETURNS:
980 * Zero for success or -errno
981 */
982int
983drm_atomic_helper_check_planes(struct drm_device *dev,
984			       struct drm_atomic_state *state)
985{
986	struct drm_crtc *crtc;
987	struct drm_crtc_state *new_crtc_state;
988	struct drm_plane *plane;
989	struct drm_plane_state *new_plane_state, *old_plane_state;
990	int i, ret = 0;
991
992	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
993		const struct drm_plane_helper_funcs *funcs;
994
995		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
996
997		funcs = plane->helper_private;
998
999		drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
1000
1001		drm_atomic_helper_check_plane_damage(state, new_plane_state);
1002
1003		if (!funcs || !funcs->atomic_check)
1004			continue;
1005
1006		ret = funcs->atomic_check(plane, state);
1007		if (ret) {
1008			drm_dbg_atomic(plane->dev,
1009				       "[PLANE:%d:%s] atomic driver check failed\n",
1010				       plane->base.id, plane->name);
1011			return ret;
1012		}
1013	}
1014
1015	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1016		const struct drm_crtc_helper_funcs *funcs;
1017
1018		funcs = crtc->helper_private;
1019
1020		if (!funcs || !funcs->atomic_check)
1021			continue;
1022
1023		ret = funcs->atomic_check(crtc, state);
1024		if (ret) {
1025			drm_dbg_atomic(crtc->dev,
1026				       "[CRTC:%d:%s] atomic driver check failed\n",
1027				       crtc->base.id, crtc->name);
1028			return ret;
1029		}
1030	}
1031
1032	return ret;
1033}
1034EXPORT_SYMBOL(drm_atomic_helper_check_planes);
1035
1036/**
1037 * drm_atomic_helper_check - validate state object
1038 * @dev: DRM device
1039 * @state: the driver state object
1040 *
1041 * Check the state object to see if the requested state is physically possible.
1042 * Only CRTCs and planes have check callbacks, so for any additional (global)
1043 * checking that a driver needs it can simply wrap that around this function.
1044 * Drivers without such needs can directly use this as their
1045 * &drm_mode_config_funcs.atomic_check callback.
1046 *
1047 * This just wraps the two parts of the state checking for planes and modeset
1048 * state in the default order: First it calls drm_atomic_helper_check_modeset()
1049 * and then drm_atomic_helper_check_planes(). The assumption is that the
1050 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
1051 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
1052 * watermarks.
1053 *
1054 * Note that zpos normalization will add all enable planes to the state which
1055 * might not desired for some drivers.
1056 * For example enable/disable of a cursor plane which have fixed zpos value
1057 * would trigger all other enabled planes to be forced to the state change.
1058 *
1059 * RETURNS:
1060 * Zero for success or -errno
1061 */
1062int drm_atomic_helper_check(struct drm_device *dev,
1063			    struct drm_atomic_state *state)
1064{
1065	int ret;
1066
1067	ret = drm_atomic_helper_check_modeset(dev, state);
1068	if (ret)
1069		return ret;
1070
1071	if (dev->mode_config.normalize_zpos) {
1072		ret = drm_atomic_normalize_zpos(dev, state);
1073		if (ret)
1074			return ret;
1075	}
1076
1077	ret = drm_atomic_helper_check_planes(dev, state);
1078	if (ret)
1079		return ret;
1080
1081	if (state->legacy_cursor_update)
1082		state->async_update = !drm_atomic_helper_async_check(dev, state);
1083
1084	drm_self_refresh_helper_alter_state(state);
1085
1086	return ret;
1087}
1088EXPORT_SYMBOL(drm_atomic_helper_check);
1089
1090static bool
1091crtc_needs_disable(struct drm_crtc_state *old_state,
1092		   struct drm_crtc_state *new_state)
1093{
1094	/*
1095	 * No new_state means the CRTC is off, so the only criteria is whether
1096	 * it's currently active or in self refresh mode.
1097	 */
1098	if (!new_state)
1099		return drm_atomic_crtc_effectively_active(old_state);
1100
1101	/*
1102	 * We need to disable bridge(s) and CRTC if we're transitioning out of
1103	 * self-refresh and changing CRTCs at the same time, because the
1104	 * bridge tracks self-refresh status via CRTC state.
1105	 */
1106	if (old_state->self_refresh_active &&
1107	    old_state->crtc != new_state->crtc)
1108		return true;
1109
1110	/*
1111	 * We also need to run through the crtc_funcs->disable() function if
1112	 * the CRTC is currently on, if it's transitioning to self refresh
1113	 * mode, or if it's in self refresh mode and needs to be fully
1114	 * disabled.
1115	 */
1116	return old_state->active ||
1117	       (old_state->self_refresh_active && !new_state->active) ||
1118	       new_state->self_refresh_active;
1119}
1120
1121static void
1122disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
1123{
1124	struct drm_connector *connector;
1125	struct drm_connector_state *old_conn_state, *new_conn_state;
1126	struct drm_crtc *crtc;
1127	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1128	int i;
1129
1130	for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1131		const struct drm_encoder_helper_funcs *funcs;
1132		struct drm_encoder *encoder;
1133		struct drm_bridge *bridge;
1134
1135		/*
1136		 * Shut down everything that's in the changeset and currently
1137		 * still on. So need to check the old, saved state.
1138		 */
1139		if (!old_conn_state->crtc)
1140			continue;
1141
1142		old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
1143
1144		if (new_conn_state->crtc)
1145			new_crtc_state = drm_atomic_get_new_crtc_state(
1146						old_state,
1147						new_conn_state->crtc);
1148		else
1149			new_crtc_state = NULL;
1150
1151		if (!crtc_needs_disable(old_crtc_state, new_crtc_state) ||
1152		    !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
1153			continue;
1154
1155		encoder = old_conn_state->best_encoder;
1156
1157		/* We shouldn't get this far if we didn't previously have
1158		 * an encoder.. but WARN_ON() rather than explode.
1159		 */
1160		if (WARN_ON(!encoder))
1161			continue;
1162
1163		funcs = encoder->helper_private;
1164
1165		drm_dbg_atomic(dev, "disabling [ENCODER:%d:%s]\n",
1166			       encoder->base.id, encoder->name);
1167
1168		/*
1169		 * Each encoder has at most one connector (since we always steal
1170		 * it away), so we won't call disable hooks twice.
1171		 */
1172		bridge = drm_bridge_chain_get_first_bridge(encoder);
1173		drm_atomic_bridge_chain_disable(bridge, old_state);
1174
1175		/* Right function depends upon target state. */
1176		if (funcs) {
1177			if (funcs->atomic_disable)
1178				funcs->atomic_disable(encoder, old_state);
1179			else if (new_conn_state->crtc && funcs->prepare)
1180				funcs->prepare(encoder);
1181			else if (funcs->disable)
1182				funcs->disable(encoder);
1183			else if (funcs->dpms)
1184				funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1185		}
1186
1187		drm_atomic_bridge_chain_post_disable(bridge, old_state);
1188	}
1189
1190	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1191		const struct drm_crtc_helper_funcs *funcs;
1192		int ret;
1193
1194		/* Shut down everything that needs a full modeset. */
1195		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1196			continue;
1197
1198		if (!crtc_needs_disable(old_crtc_state, new_crtc_state))
1199			continue;
1200
1201		funcs = crtc->helper_private;
1202
1203		drm_dbg_atomic(dev, "disabling [CRTC:%d:%s]\n",
1204			       crtc->base.id, crtc->name);
1205
1206
1207		/* Right function depends upon target state. */
1208		if (new_crtc_state->enable && funcs->prepare)
1209			funcs->prepare(crtc);
1210		else if (funcs->atomic_disable)
1211			funcs->atomic_disable(crtc, old_state);
1212		else if (funcs->disable)
1213			funcs->disable(crtc);
1214		else if (funcs->dpms)
1215			funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1216
1217		if (!drm_dev_has_vblank(dev))
1218			continue;
1219
1220		ret = drm_crtc_vblank_get(crtc);
1221		/*
1222		 * Self-refresh is not a true "disable"; ensure vblank remains
1223		 * enabled.
1224		 */
1225		if (new_crtc_state->self_refresh_active)
1226			WARN_ONCE(ret != 0,
1227				  "driver disabled vblank in self-refresh\n");
1228		else
1229			WARN_ONCE(ret != -EINVAL,
1230				  "driver forgot to call drm_crtc_vblank_off()\n");
1231		if (ret == 0)
1232			drm_crtc_vblank_put(crtc);
1233	}
1234}
1235
1236/**
1237 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1238 * @dev: DRM device
1239 * @old_state: atomic state object with old state structures
1240 *
1241 * This function updates all the various legacy modeset state pointers in
1242 * connectors, encoders and CRTCs.
1243 *
1244 * Drivers can use this for building their own atomic commit if they don't have
1245 * a pure helper-based modeset implementation.
1246 *
1247 * Since these updates are not synchronized with lockings, only code paths
1248 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1249 * legacy state filled out by this helper. Defacto this means this helper and
1250 * the legacy state pointers are only really useful for transitioning an
1251 * existing driver to the atomic world.
1252 */
1253void
1254drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1255					      struct drm_atomic_state *old_state)
1256{
1257	struct drm_connector *connector;
1258	struct drm_connector_state *old_conn_state, *new_conn_state;
1259	struct drm_crtc *crtc;
1260	struct drm_crtc_state *new_crtc_state;
1261	int i;
1262
1263	/* clear out existing links and update dpms */
1264	for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1265		if (connector->encoder) {
1266			WARN_ON(!connector->encoder->crtc);
1267
1268			connector->encoder->crtc = NULL;
1269			connector->encoder = NULL;
1270		}
1271
1272		crtc = new_conn_state->crtc;
1273		if ((!crtc && old_conn_state->crtc) ||
1274		    (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
1275			int mode = DRM_MODE_DPMS_OFF;
1276
1277			if (crtc && crtc->state->active)
1278				mode = DRM_MODE_DPMS_ON;
1279
1280			connector->dpms = mode;
1281		}
1282	}
1283
1284	/* set new links */
1285	for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1286		if (!new_conn_state->crtc)
1287			continue;
1288
1289		if (WARN_ON(!new_conn_state->best_encoder))
1290			continue;
1291
1292		connector->encoder = new_conn_state->best_encoder;
1293		connector->encoder->crtc = new_conn_state->crtc;
1294	}
1295
1296	/* set legacy state in the crtc structure */
1297	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1298		struct drm_plane *primary = crtc->primary;
1299		struct drm_plane_state *new_plane_state;
1300
1301		crtc->mode = new_crtc_state->mode;
1302		crtc->enabled = new_crtc_state->enable;
1303
1304		new_plane_state =
1305			drm_atomic_get_new_plane_state(old_state, primary);
1306
1307		if (new_plane_state && new_plane_state->crtc == crtc) {
1308			crtc->x = new_plane_state->src_x >> 16;
1309			crtc->y = new_plane_state->src_y >> 16;
1310		}
1311	}
1312}
1313EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
1314
1315/**
1316 * drm_atomic_helper_calc_timestamping_constants - update vblank timestamping constants
1317 * @state: atomic state object
1318 *
1319 * Updates the timestamping constants used for precise vblank timestamps
1320 * by calling drm_calc_timestamping_constants() for all enabled crtcs in @state.
1321 */
1322void drm_atomic_helper_calc_timestamping_constants(struct drm_atomic_state *state)
1323{
1324	struct drm_crtc_state *new_crtc_state;
1325	struct drm_crtc *crtc;
1326	int i;
1327
1328	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1329		if (new_crtc_state->enable)
1330			drm_calc_timestamping_constants(crtc,
1331							&new_crtc_state->adjusted_mode);
1332	}
1333}
1334EXPORT_SYMBOL(drm_atomic_helper_calc_timestamping_constants);
1335
1336static void
1337crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1338{
1339	struct drm_crtc *crtc;
1340	struct drm_crtc_state *new_crtc_state;
1341	struct drm_connector *connector;
1342	struct drm_connector_state *new_conn_state;
1343	int i;
1344
1345	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1346		const struct drm_crtc_helper_funcs *funcs;
1347
1348		if (!new_crtc_state->mode_changed)
1349			continue;
1350
1351		funcs = crtc->helper_private;
1352
1353		if (new_crtc_state->enable && funcs->mode_set_nofb) {
1354			drm_dbg_atomic(dev, "modeset on [CRTC:%d:%s]\n",
1355				       crtc->base.id, crtc->name);
1356
1357			funcs->mode_set_nofb(crtc);
1358		}
1359	}
1360
1361	for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1362		const struct drm_encoder_helper_funcs *funcs;
1363		struct drm_encoder *encoder;
1364		struct drm_display_mode *mode, *adjusted_mode;
1365		struct drm_bridge *bridge;
1366
1367		if (!new_conn_state->best_encoder)
1368			continue;
1369
1370		encoder = new_conn_state->best_encoder;
1371		funcs = encoder->helper_private;
1372		new_crtc_state = new_conn_state->crtc->state;
1373		mode = &new_crtc_state->mode;
1374		adjusted_mode = &new_crtc_state->adjusted_mode;
1375
1376		if (!new_crtc_state->mode_changed)
1377			continue;
1378
1379		drm_dbg_atomic(dev, "modeset on [ENCODER:%d:%s]\n",
1380			       encoder->base.id, encoder->name);
1381
1382		/*
1383		 * Each encoder has at most one connector (since we always steal
1384		 * it away), so we won't call mode_set hooks twice.
1385		 */
1386		if (funcs && funcs->atomic_mode_set) {
1387			funcs->atomic_mode_set(encoder, new_crtc_state,
1388					       new_conn_state);
1389		} else if (funcs && funcs->mode_set) {
1390			funcs->mode_set(encoder, mode, adjusted_mode);
1391		}
1392
1393		bridge = drm_bridge_chain_get_first_bridge(encoder);
1394		drm_bridge_chain_mode_set(bridge, mode, adjusted_mode);
1395	}
1396}
1397
1398/**
1399 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
1400 * @dev: DRM device
1401 * @old_state: atomic state object with old state structures
1402 *
1403 * This function shuts down all the outputs that need to be shut down and
1404 * prepares them (if required) with the new mode.
1405 *
1406 * For compatibility with legacy CRTC helpers this should be called before
1407 * drm_atomic_helper_commit_planes(), which is what the default commit function
1408 * does. But drivers with different needs can group the modeset commits together
1409 * and do the plane commits at the end. This is useful for drivers doing runtime
1410 * PM since planes updates then only happen when the CRTC is actually enabled.
1411 */
1412void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1413					       struct drm_atomic_state *old_state)
1414{
1415	disable_outputs(dev, old_state);
1416
1417	drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1418	drm_atomic_helper_calc_timestamping_constants(old_state);
1419
1420	crtc_set_mode(dev, old_state);
1421}
1422EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1423
1424static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1425						struct drm_atomic_state *old_state)
1426{
1427	struct drm_connector *connector;
1428	struct drm_connector_state *new_conn_state;
1429	int i;
1430
1431	for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1432		const struct drm_connector_helper_funcs *funcs;
1433
1434		funcs = connector->helper_private;
1435		if (!funcs->atomic_commit)
1436			continue;
1437
1438		if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1439			WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1440			funcs->atomic_commit(connector, old_state);
1441		}
1442	}
1443}
1444
1445/**
1446 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
1447 * @dev: DRM device
1448 * @old_state: atomic state object with old state structures
1449 *
1450 * This function enables all the outputs with the new configuration which had to
1451 * be turned off for the update.
1452 *
1453 * For compatibility with legacy CRTC helpers this should be called after
1454 * drm_atomic_helper_commit_planes(), which is what the default commit function
1455 * does. But drivers with different needs can group the modeset commits together
1456 * and do the plane commits at the end. This is useful for drivers doing runtime
1457 * PM since planes updates then only happen when the CRTC is actually enabled.
1458 */
1459void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1460					      struct drm_atomic_state *old_state)
1461{
1462	struct drm_crtc *crtc;
1463	struct drm_crtc_state *old_crtc_state;
1464	struct drm_crtc_state *new_crtc_state;
1465	struct drm_connector *connector;
1466	struct drm_connector_state *new_conn_state;
1467	int i;
1468
1469	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1470		const struct drm_crtc_helper_funcs *funcs;
1471
1472		/* Need to filter out CRTCs where only planes change. */
1473		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1474			continue;
1475
1476		if (!new_crtc_state->active)
1477			continue;
1478
1479		funcs = crtc->helper_private;
1480
1481		if (new_crtc_state->enable) {
1482			drm_dbg_atomic(dev, "enabling [CRTC:%d:%s]\n",
1483				       crtc->base.id, crtc->name);
1484			if (funcs->atomic_enable)
1485				funcs->atomic_enable(crtc, old_state);
1486			else if (funcs->commit)
1487				funcs->commit(crtc);
1488		}
1489	}
1490
1491	for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1492		const struct drm_encoder_helper_funcs *funcs;
1493		struct drm_encoder *encoder;
1494		struct drm_bridge *bridge;
1495
1496		if (!new_conn_state->best_encoder)
1497			continue;
1498
1499		if (!new_conn_state->crtc->state->active ||
1500		    !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1501			continue;
1502
1503		encoder = new_conn_state->best_encoder;
1504		funcs = encoder->helper_private;
1505
1506		drm_dbg_atomic(dev, "enabling [ENCODER:%d:%s]\n",
1507			       encoder->base.id, encoder->name);
1508
1509		/*
1510		 * Each encoder has at most one connector (since we always steal
1511		 * it away), so we won't call enable hooks twice.
1512		 */
1513		bridge = drm_bridge_chain_get_first_bridge(encoder);
1514		drm_atomic_bridge_chain_pre_enable(bridge, old_state);
1515
1516		if (funcs) {
1517			if (funcs->atomic_enable)
1518				funcs->atomic_enable(encoder, old_state);
1519			else if (funcs->enable)
1520				funcs->enable(encoder);
1521			else if (funcs->commit)
1522				funcs->commit(encoder);
1523		}
1524
1525		drm_atomic_bridge_chain_enable(bridge, old_state);
1526	}
1527
1528	drm_atomic_helper_commit_writebacks(dev, old_state);
1529}
1530EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1531
1532/*
1533 * For atomic updates which touch just a single CRTC, calculate the time of the
1534 * next vblank, and inform all the fences of the deadline.
1535 */
1536static void set_fence_deadline(struct drm_device *dev,
1537			       struct drm_atomic_state *state)
1538{
1539	struct drm_crtc *crtc;
1540	struct drm_crtc_state *new_crtc_state;
1541	struct drm_plane *plane;
1542	struct drm_plane_state *new_plane_state;
1543	ktime_t vbltime = 0;
1544	int i;
1545
1546	for_each_new_crtc_in_state (state, crtc, new_crtc_state, i) {
1547		ktime_t v;
1548
1549		if (drm_atomic_crtc_needs_modeset(new_crtc_state))
1550			continue;
1551
1552		if (!new_crtc_state->active)
1553			continue;
1554
1555		if (drm_crtc_next_vblank_start(crtc, &v))
1556			continue;
1557
1558		if (!vbltime || ktime_before(v, vbltime))
1559			vbltime = v;
1560	}
1561
1562	/* If no CRTCs updated, then nothing to do: */
1563	if (!vbltime)
1564		return;
1565
1566	for_each_new_plane_in_state (state, plane, new_plane_state, i) {
1567		if (!new_plane_state->fence)
1568			continue;
1569		dma_fence_set_deadline(new_plane_state->fence, vbltime);
1570	}
1571}
1572
1573/**
1574 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1575 * @dev: DRM device
1576 * @state: atomic state object with old state structures
1577 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1578 *	Otherwise @state is the old state.
1579 *
1580 * For implicit sync, driver should fish the exclusive fence out from the
1581 * incoming fb's and stash it in the drm_plane_state.  This is called after
1582 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1583 * just uses the atomic state to find the changed planes)
1584 *
1585 * Note that @pre_swap is needed since the point where we block for fences moves
1586 * around depending upon whether an atomic commit is blocking or
1587 * non-blocking. For non-blocking commit all waiting needs to happen after
1588 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1589 * to wait **before** we do anything that can't be easily rolled back. That is
1590 * before we call drm_atomic_helper_swap_state().
1591 *
1592 * Returns zero if success or < 0 if dma_fence_wait() fails.
1593 */
1594int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1595				      struct drm_atomic_state *state,
1596				      bool pre_swap)
1597{
1598	struct drm_plane *plane;
1599	struct drm_plane_state *new_plane_state;
1600	int i, ret;
1601
1602	set_fence_deadline(dev, state);
1603
1604	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1605		if (!new_plane_state->fence)
1606			continue;
1607
1608		WARN_ON(!new_plane_state->fb);
1609
1610		/*
1611		 * If waiting for fences pre-swap (ie: nonblock), userspace can
1612		 * still interrupt the operation. Instead of blocking until the
1613		 * timer expires, make the wait interruptible.
1614		 */
1615		ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1616		if (ret)
1617			return ret;
1618
1619		dma_fence_put(new_plane_state->fence);
1620		new_plane_state->fence = NULL;
1621	}
1622
1623	return 0;
1624}
1625EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1626
1627/**
1628 * drm_atomic_helper_wait_for_vblanks - wait for vblank on CRTCs
1629 * @dev: DRM device
1630 * @old_state: atomic state object with old state structures
1631 *
1632 * Helper to, after atomic commit, wait for vblanks on all affected
1633 * CRTCs (ie. before cleaning up old framebuffers using
1634 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
1635 * framebuffers have actually changed to optimize for the legacy cursor and
1636 * plane update use-case.
1637 *
1638 * Drivers using the nonblocking commit tracking support initialized by calling
1639 * drm_atomic_helper_setup_commit() should look at
1640 * drm_atomic_helper_wait_for_flip_done() as an alternative.
1641 */
1642void
1643drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1644		struct drm_atomic_state *old_state)
1645{
1646	struct drm_crtc *crtc;
1647	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1648	int i, ret;
1649	unsigned int crtc_mask = 0;
1650
1651	 /*
1652	  * Legacy cursor ioctls are completely unsynced, and userspace
1653	  * relies on that (by doing tons of cursor updates).
1654	  */
1655	if (old_state->legacy_cursor_update)
1656		return;
1657
1658	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1659		if (!new_crtc_state->active)
1660			continue;
1661
1662		ret = drm_crtc_vblank_get(crtc);
1663		if (ret != 0)
1664			continue;
1665
1666		crtc_mask |= drm_crtc_mask(crtc);
1667		old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1668	}
1669
1670	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1671		if (!(crtc_mask & drm_crtc_mask(crtc)))
1672			continue;
1673
1674		ret = wait_event_timeout(dev->vblank[i].queue,
1675				old_state->crtcs[i].last_vblank_count !=
1676					drm_crtc_vblank_count(crtc),
1677				msecs_to_jiffies(100));
1678
1679		WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1680		     crtc->base.id, crtc->name);
1681
1682		drm_crtc_vblank_put(crtc);
1683	}
1684}
1685EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1686
1687/**
1688 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1689 * @dev: DRM device
1690 * @old_state: atomic state object with old state structures
1691 *
1692 * Helper to, after atomic commit, wait for page flips on all affected
1693 * crtcs (ie. before cleaning up old framebuffers using
1694 * drm_atomic_helper_cleanup_planes()). Compared to
1695 * drm_atomic_helper_wait_for_vblanks() this waits for the completion on all
1696 * CRTCs, assuming that cursors-only updates are signalling their completion
1697 * immediately (or using a different path).
1698 *
1699 * This requires that drivers use the nonblocking commit tracking support
1700 * initialized using drm_atomic_helper_setup_commit().
1701 */
1702void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1703					  struct drm_atomic_state *old_state)
1704{
1705	struct drm_crtc *crtc;
1706	int i;
1707
1708	for (i = 0; i < dev->mode_config.num_crtc; i++) {
1709		struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
1710		int ret;
1711
1712		crtc = old_state->crtcs[i].ptr;
1713
1714		if (!crtc || !commit)
1715			continue;
1716
1717		ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1718		if (ret == 0)
1719			drm_err(dev, "[CRTC:%d:%s] flip_done timed out\n",
1720				crtc->base.id, crtc->name);
1721	}
1722
1723	if (old_state->fake_commit)
1724		complete_all(&old_state->fake_commit->flip_done);
1725}
1726EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1727
1728/**
1729 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1730 * @old_state: atomic state object with old state structures
1731 *
1732 * This is the default implementation for the
1733 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1734 * that do not support runtime_pm or do not need the CRTC to be
1735 * enabled to perform a commit. Otherwise, see
1736 * drm_atomic_helper_commit_tail_rpm().
1737 *
1738 * Note that the default ordering of how the various stages are called is to
1739 * match the legacy modeset helper library closest.
1740 */
1741void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1742{
1743	struct drm_device *dev = old_state->dev;
1744
1745	drm_atomic_helper_commit_modeset_disables(dev, old_state);
1746
1747	drm_atomic_helper_commit_planes(dev, old_state, 0);
1748
1749	drm_atomic_helper_commit_modeset_enables(dev, old_state);
1750
1751	drm_atomic_helper_fake_vblank(old_state);
1752
1753	drm_atomic_helper_commit_hw_done(old_state);
1754
1755	drm_atomic_helper_wait_for_vblanks(dev, old_state);
1756
1757	drm_atomic_helper_cleanup_planes(dev, old_state);
1758}
1759EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1760
1761/**
1762 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1763 * @old_state: new modeset state to be committed
1764 *
1765 * This is an alternative implementation for the
1766 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1767 * that support runtime_pm or need the CRTC to be enabled to perform a
1768 * commit. Otherwise, one should use the default implementation
1769 * drm_atomic_helper_commit_tail().
1770 */
1771void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1772{
1773	struct drm_device *dev = old_state->dev;
1774
1775	drm_atomic_helper_commit_modeset_disables(dev, old_state);
1776
1777	drm_atomic_helper_commit_modeset_enables(dev, old_state);
1778
1779	drm_atomic_helper_commit_planes(dev, old_state,
1780					DRM_PLANE_COMMIT_ACTIVE_ONLY);
1781
1782	drm_atomic_helper_fake_vblank(old_state);
1783
1784	drm_atomic_helper_commit_hw_done(old_state);
1785
1786	drm_atomic_helper_wait_for_vblanks(dev, old_state);
1787
1788	drm_atomic_helper_cleanup_planes(dev, old_state);
1789}
1790EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1791
1792static void commit_tail(struct drm_atomic_state *old_state)
1793{
1794	struct drm_device *dev = old_state->dev;
1795	const struct drm_mode_config_helper_funcs *funcs;
1796	struct drm_crtc_state *new_crtc_state;
1797	struct drm_crtc *crtc;
1798	ktime_t start;
1799	s64 commit_time_ms;
1800	unsigned int i, new_self_refresh_mask = 0;
1801
1802	funcs = dev->mode_config.helper_private;
1803
1804	/*
1805	 * We're measuring the _entire_ commit, so the time will vary depending
1806	 * on how many fences and objects are involved. For the purposes of self
1807	 * refresh, this is desirable since it'll give us an idea of how
1808	 * congested things are. This will inform our decision on how often we
1809	 * should enter self refresh after idle.
1810	 *
1811	 * These times will be averaged out in the self refresh helpers to avoid
1812	 * overreacting over one outlier frame
1813	 */
1814	start = ktime_get();
1815
1816	drm_atomic_helper_wait_for_fences(dev, old_state, false);
1817
1818	drm_atomic_helper_wait_for_dependencies(old_state);
1819
1820	/*
1821	 * We cannot safely access new_crtc_state after
1822	 * drm_atomic_helper_commit_hw_done() so figure out which crtc's have
1823	 * self-refresh active beforehand:
1824	 */
1825	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i)
1826		if (new_crtc_state->self_refresh_active)
1827			new_self_refresh_mask |= BIT(i);
1828
1829	if (funcs && funcs->atomic_commit_tail)
1830		funcs->atomic_commit_tail(old_state);
1831	else
1832		drm_atomic_helper_commit_tail(old_state);
1833
1834	commit_time_ms = ktime_ms_delta(ktime_get(), start);
1835	if (commit_time_ms > 0)
1836		drm_self_refresh_helper_update_avg_times(old_state,
1837						 (unsigned long)commit_time_ms,
1838						 new_self_refresh_mask);
1839
1840	drm_atomic_helper_commit_cleanup_done(old_state);
1841
1842	drm_atomic_state_put(old_state);
1843}
1844
1845static void commit_work(struct work_struct *work)
1846{
1847	struct drm_atomic_state *state = container_of(work,
1848						      struct drm_atomic_state,
1849						      commit_work);
1850	commit_tail(state);
1851}
1852
1853/**
1854 * drm_atomic_helper_async_check - check if state can be committed asynchronously
1855 * @dev: DRM device
1856 * @state: the driver state object
1857 *
1858 * This helper will check if it is possible to commit the state asynchronously.
1859 * Async commits are not supposed to swap the states like normal sync commits
1860 * but just do in-place changes on the current state.
1861 *
1862 * It will return 0 if the commit can happen in an asynchronous fashion or error
1863 * if not. Note that error just mean it can't be committed asynchronously, if it
1864 * fails the commit should be treated like a normal synchronous commit.
1865 */
1866int drm_atomic_helper_async_check(struct drm_device *dev,
1867				   struct drm_atomic_state *state)
1868{
1869	struct drm_crtc *crtc;
1870	struct drm_crtc_state *crtc_state;
1871	struct drm_plane *plane = NULL;
1872	struct drm_plane_state *old_plane_state = NULL;
1873	struct drm_plane_state *new_plane_state = NULL;
1874	const struct drm_plane_helper_funcs *funcs;
1875	int i, ret, n_planes = 0;
1876
1877	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1878		if (drm_atomic_crtc_needs_modeset(crtc_state))
1879			return -EINVAL;
1880	}
1881
1882	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
1883		n_planes++;
1884
1885	/* FIXME: we support only single plane updates for now */
1886	if (n_planes != 1) {
1887		drm_dbg_atomic(dev,
1888			       "only single plane async updates are supported\n");
1889		return -EINVAL;
1890	}
1891
1892	if (!new_plane_state->crtc ||
1893	    old_plane_state->crtc != new_plane_state->crtc) {
1894		drm_dbg_atomic(dev,
1895			       "[PLANE:%d:%s] async update cannot change CRTC\n",
1896			       plane->base.id, plane->name);
1897		return -EINVAL;
1898	}
1899
1900	funcs = plane->helper_private;
1901	if (!funcs->atomic_async_update) {
1902		drm_dbg_atomic(dev,
1903			       "[PLANE:%d:%s] driver does not support async updates\n",
1904			       plane->base.id, plane->name);
1905		return -EINVAL;
1906	}
1907
1908	if (new_plane_state->fence) {
1909		drm_dbg_atomic(dev,
1910			       "[PLANE:%d:%s] missing fence for async update\n",
1911			       plane->base.id, plane->name);
1912		return -EINVAL;
1913	}
1914
1915	/*
1916	 * Don't do an async update if there is an outstanding commit modifying
1917	 * the plane.  This prevents our async update's changes from getting
1918	 * overridden by a previous synchronous update's state.
1919	 */
1920	if (old_plane_state->commit &&
1921	    !try_wait_for_completion(&old_plane_state->commit->hw_done)) {
1922		drm_dbg_atomic(dev,
1923			       "[PLANE:%d:%s] inflight previous commit preventing async commit\n",
1924			       plane->base.id, plane->name);
1925		return -EBUSY;
1926	}
1927
1928	ret = funcs->atomic_async_check(plane, state);
1929	if (ret != 0)
1930		drm_dbg_atomic(dev,
1931			       "[PLANE:%d:%s] driver async check failed\n",
1932			       plane->base.id, plane->name);
1933	return ret;
1934}
1935EXPORT_SYMBOL(drm_atomic_helper_async_check);
1936
1937/**
1938 * drm_atomic_helper_async_commit - commit state asynchronously
1939 * @dev: DRM device
1940 * @state: the driver state object
1941 *
1942 * This function commits a state asynchronously, i.e., not vblank
1943 * synchronized. It should be used on a state only when
1944 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1945 * the states like normal sync commits, but just do in-place changes on the
1946 * current state.
1947 *
1948 * TODO: Implement full swap instead of doing in-place changes.
1949 */
1950void drm_atomic_helper_async_commit(struct drm_device *dev,
1951				    struct drm_atomic_state *state)
1952{
1953	struct drm_plane *plane;
1954	struct drm_plane_state *plane_state;
1955	const struct drm_plane_helper_funcs *funcs;
1956	int i;
1957
1958	for_each_new_plane_in_state(state, plane, plane_state, i) {
1959		struct drm_framebuffer *new_fb = plane_state->fb;
1960		struct drm_framebuffer *old_fb = plane->state->fb;
1961
1962		funcs = plane->helper_private;
1963		funcs->atomic_async_update(plane, state);
1964
1965		/*
1966		 * ->atomic_async_update() is supposed to update the
1967		 * plane->state in-place, make sure at least common
1968		 * properties have been properly updated.
1969		 */
1970		WARN_ON_ONCE(plane->state->fb != new_fb);
1971		WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1972		WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1973		WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1974		WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1975
1976		/*
1977		 * Make sure the FBs have been swapped so that cleanups in the
1978		 * new_state performs a cleanup in the old FB.
1979		 */
1980		WARN_ON_ONCE(plane_state->fb != old_fb);
1981	}
1982}
1983EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1984
1985/**
1986 * drm_atomic_helper_commit - commit validated state object
1987 * @dev: DRM device
1988 * @state: the driver state object
1989 * @nonblock: whether nonblocking behavior is requested.
1990 *
1991 * This function commits a with drm_atomic_helper_check() pre-validated state
1992 * object. This can still fail when e.g. the framebuffer reservation fails. This
1993 * function implements nonblocking commits, using
1994 * drm_atomic_helper_setup_commit() and related functions.
1995 *
1996 * Committing the actual hardware state is done through the
1997 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or its default
1998 * implementation drm_atomic_helper_commit_tail().
1999 *
2000 * RETURNS:
2001 * Zero for success or -errno.
2002 */
2003int drm_atomic_helper_commit(struct drm_device *dev,
2004			     struct drm_atomic_state *state,
2005			     bool nonblock)
2006{
2007	int ret;
2008
2009	if (state->async_update) {
2010		ret = drm_atomic_helper_prepare_planes(dev, state);
2011		if (ret)
2012			return ret;
2013
2014		drm_atomic_helper_async_commit(dev, state);
2015		drm_atomic_helper_unprepare_planes(dev, state);
2016
2017		return 0;
2018	}
2019
2020	ret = drm_atomic_helper_setup_commit(state, nonblock);
2021	if (ret)
2022		return ret;
2023
2024	INIT_WORK(&state->commit_work, commit_work);
2025
2026	ret = drm_atomic_helper_prepare_planes(dev, state);
2027	if (ret)
2028		return ret;
2029
2030	if (!nonblock) {
2031		ret = drm_atomic_helper_wait_for_fences(dev, state, true);
2032		if (ret)
2033			goto err;
2034	}
2035
2036	/*
2037	 * This is the point of no return - everything below never fails except
2038	 * when the hw goes bonghits. Which means we can commit the new state on
2039	 * the software side now.
2040	 */
2041
2042	ret = drm_atomic_helper_swap_state(state, true);
2043	if (ret)
2044		goto err;
2045
2046	/*
2047	 * Everything below can be run asynchronously without the need to grab
2048	 * any modeset locks at all under one condition: It must be guaranteed
2049	 * that the asynchronous work has either been cancelled (if the driver
2050	 * supports it, which at least requires that the framebuffers get
2051	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
2052	 * before the new state gets committed on the software side with
2053	 * drm_atomic_helper_swap_state().
2054	 *
2055	 * This scheme allows new atomic state updates to be prepared and
2056	 * checked in parallel to the asynchronous completion of the previous
2057	 * update. Which is important since compositors need to figure out the
2058	 * composition of the next frame right after having submitted the
2059	 * current layout.
2060	 *
2061	 * NOTE: Commit work has multiple phases, first hardware commit, then
2062	 * cleanup. We want them to overlap, hence need system_unbound_wq to
2063	 * make sure work items don't artificially stall on each another.
2064	 */
2065
2066	drm_atomic_state_get(state);
2067	if (nonblock)
2068		queue_work(system_unbound_wq, &state->commit_work);
2069	else
2070		commit_tail(state);
2071
2072	return 0;
2073
2074err:
2075	drm_atomic_helper_unprepare_planes(dev, state);
2076	return ret;
2077}
2078EXPORT_SYMBOL(drm_atomic_helper_commit);
2079
2080/**
2081 * DOC: implementing nonblocking commit
2082 *
2083 * Nonblocking atomic commits should use struct &drm_crtc_commit to sequence
2084 * different operations against each another. Locks, especially struct
2085 * &drm_modeset_lock, should not be held in worker threads or any other
2086 * asynchronous context used to commit the hardware state.
2087 *
2088 * drm_atomic_helper_commit() implements the recommended sequence for
2089 * nonblocking commits, using drm_atomic_helper_setup_commit() internally:
2090 *
2091 * 1. Run drm_atomic_helper_prepare_planes(). Since this can fail and we
2092 * need to propagate out of memory/VRAM errors to userspace, it must be called
2093 * synchronously.
2094 *
2095 * 2. Synchronize with any outstanding nonblocking commit worker threads which
2096 * might be affected by the new state update. This is handled by
2097 * drm_atomic_helper_setup_commit().
2098 *
2099 * Asynchronous workers need to have sufficient parallelism to be able to run
2100 * different atomic commits on different CRTCs in parallel. The simplest way to
2101 * achieve this is by running them on the &system_unbound_wq work queue. Note
2102 * that drivers are not required to split up atomic commits and run an
2103 * individual commit in parallel - userspace is supposed to do that if it cares.
2104 * But it might be beneficial to do that for modesets, since those necessarily
2105 * must be done as one global operation, and enabling or disabling a CRTC can
2106 * take a long time. But even that is not required.
2107 *
2108 * IMPORTANT: A &drm_atomic_state update for multiple CRTCs is sequenced
2109 * against all CRTCs therein. Therefore for atomic state updates which only flip
2110 * planes the driver must not get the struct &drm_crtc_state of unrelated CRTCs
2111 * in its atomic check code: This would prevent committing of atomic updates to
2112 * multiple CRTCs in parallel. In general, adding additional state structures
2113 * should be avoided as much as possible, because this reduces parallelism in
2114 * (nonblocking) commits, both due to locking and due to commit sequencing
2115 * requirements.
2116 *
2117 * 3. The software state is updated synchronously with
2118 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
2119 * locks means concurrent callers never see inconsistent state. Note that commit
2120 * workers do not hold any locks; their access is only coordinated through
2121 * ordering. If workers would access state only through the pointers in the
2122 * free-standing state objects (currently not the case for any driver) then even
2123 * multiple pending commits could be in-flight at the same time.
2124 *
2125 * 4. Schedule a work item to do all subsequent steps, using the split-out
2126 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
2127 * then cleaning up the framebuffers after the old framebuffer is no longer
2128 * being displayed. The scheduled work should synchronize against other workers
2129 * using the &drm_crtc_commit infrastructure as needed. See
2130 * drm_atomic_helper_setup_commit() for more details.
2131 */
2132
2133static int stall_checks(struct drm_crtc *crtc, bool nonblock)
2134{
2135	struct drm_crtc_commit *commit, *stall_commit = NULL;
2136	bool completed = true;
2137	int i;
2138	long ret = 0;
2139
2140	spin_lock(&crtc->commit_lock);
2141	i = 0;
2142	list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
2143		if (i == 0) {
2144			completed = try_wait_for_completion(&commit->flip_done);
2145			/*
2146			 * Userspace is not allowed to get ahead of the previous
2147			 * commit with nonblocking ones.
2148			 */
2149			if (!completed && nonblock) {
2150				spin_unlock(&crtc->commit_lock);
2151				drm_dbg_atomic(crtc->dev,
2152					       "[CRTC:%d:%s] busy with a previous commit\n",
2153					       crtc->base.id, crtc->name);
2154
2155				return -EBUSY;
2156			}
2157		} else if (i == 1) {
2158			stall_commit = drm_crtc_commit_get(commit);
2159			break;
2160		}
2161
2162		i++;
2163	}
2164	spin_unlock(&crtc->commit_lock);
2165
2166	if (!stall_commit)
2167		return 0;
2168
2169	/* We don't want to let commits get ahead of cleanup work too much,
2170	 * stalling on 2nd previous commit means triple-buffer won't ever stall.
2171	 */
2172	ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
2173							10*HZ);
2174	if (ret == 0)
2175		drm_err(crtc->dev, "[CRTC:%d:%s] cleanup_done timed out\n",
2176			crtc->base.id, crtc->name);
2177
2178	drm_crtc_commit_put(stall_commit);
2179
2180	return ret < 0 ? ret : 0;
2181}
2182
2183static void release_crtc_commit(struct completion *completion)
2184{
2185	struct drm_crtc_commit *commit = container_of(completion,
2186						      typeof(*commit),
2187						      flip_done);
2188
2189	drm_crtc_commit_put(commit);
2190}
2191
2192static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
2193{
2194	init_completion(&commit->flip_done);
2195	init_completion(&commit->hw_done);
2196	init_completion(&commit->cleanup_done);
2197	INIT_LIST_HEAD(&commit->commit_entry);
2198	kref_init(&commit->ref);
2199	commit->crtc = crtc;
2200}
2201
2202static struct drm_crtc_commit *
2203crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
2204{
2205	if (crtc) {
2206		struct drm_crtc_state *new_crtc_state;
2207
2208		new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
2209
2210		return new_crtc_state->commit;
2211	}
2212
2213	if (!state->fake_commit) {
2214		state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
2215		if (!state->fake_commit)
2216			return NULL;
2217
2218		init_commit(state->fake_commit, NULL);
2219	}
2220
2221	return state->fake_commit;
2222}
2223
2224/**
2225 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
2226 * @state: new modeset state to be committed
2227 * @nonblock: whether nonblocking behavior is requested.
2228 *
2229 * This function prepares @state to be used by the atomic helper's support for
2230 * nonblocking commits. Drivers using the nonblocking commit infrastructure
2231 * should always call this function from their
2232 * &drm_mode_config_funcs.atomic_commit hook.
2233 *
2234 * Drivers that need to extend the commit setup to private objects can use the
2235 * &drm_mode_config_helper_funcs.atomic_commit_setup hook.
2236 *
2237 * To be able to use this support drivers need to use a few more helper
2238 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
2239 * actually committing the hardware state, and for nonblocking commits this call
2240 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
2241 * and its stall parameter, for when a driver's commit hooks look at the
2242 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
2243 *
2244 * Completion of the hardware commit step must be signalled using
2245 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
2246 * to read or change any permanent software or hardware modeset state. The only
2247 * exception is state protected by other means than &drm_modeset_lock locks.
2248 * Only the free standing @state with pointers to the old state structures can
2249 * be inspected, e.g. to clean up old buffers using
2250 * drm_atomic_helper_cleanup_planes().
2251 *
2252 * At the very end, before cleaning up @state drivers must call
2253 * drm_atomic_helper_commit_cleanup_done().
2254 *
2255 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
2256 * complete and easy-to-use default implementation of the atomic_commit() hook.
2257 *
2258 * The tracking of asynchronously executed and still pending commits is done
2259 * using the core structure &drm_crtc_commit.
2260 *
2261 * By default there's no need to clean up resources allocated by this function
2262 * explicitly: drm_atomic_state_default_clear() will take care of that
2263 * automatically.
2264 *
2265 * Returns:
2266 *
2267 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
2268 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
2269 */
2270int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
2271				   bool nonblock)
2272{
2273	struct drm_crtc *crtc;
2274	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2275	struct drm_connector *conn;
2276	struct drm_connector_state *old_conn_state, *new_conn_state;
2277	struct drm_plane *plane;
2278	struct drm_plane_state *old_plane_state, *new_plane_state;
2279	struct drm_crtc_commit *commit;
2280	const struct drm_mode_config_helper_funcs *funcs;
2281	int i, ret;
2282
2283	funcs = state->dev->mode_config.helper_private;
2284
2285	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2286		commit = kzalloc(sizeof(*commit), GFP_KERNEL);
2287		if (!commit)
2288			return -ENOMEM;
2289
2290		init_commit(commit, crtc);
2291
2292		new_crtc_state->commit = commit;
2293
2294		ret = stall_checks(crtc, nonblock);
2295		if (ret)
2296			return ret;
2297
2298		/*
2299		 * Drivers only send out events when at least either current or
2300		 * new CRTC state is active. Complete right away if everything
2301		 * stays off.
2302		 */
2303		if (!old_crtc_state->active && !new_crtc_state->active) {
2304			complete_all(&commit->flip_done);
2305			continue;
2306		}
2307
2308		/* Legacy cursor updates are fully unsynced. */
2309		if (state->legacy_cursor_update) {
2310			complete_all(&commit->flip_done);
2311			continue;
2312		}
2313
2314		if (!new_crtc_state->event) {
2315			commit->event = kzalloc(sizeof(*commit->event),
2316						GFP_KERNEL);
2317			if (!commit->event)
2318				return -ENOMEM;
2319
2320			new_crtc_state->event = commit->event;
2321		}
2322
2323		new_crtc_state->event->base.completion = &commit->flip_done;
2324		new_crtc_state->event->base.completion_release = release_crtc_commit;
2325		drm_crtc_commit_get(commit);
2326
2327		commit->abort_completion = true;
2328
2329		state->crtcs[i].commit = commit;
2330		drm_crtc_commit_get(commit);
2331	}
2332
2333	for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
2334		/*
2335		 * Userspace is not allowed to get ahead of the previous
2336		 * commit with nonblocking ones.
2337		 */
2338		if (nonblock && old_conn_state->commit &&
2339		    !try_wait_for_completion(&old_conn_state->commit->flip_done)) {
2340			drm_dbg_atomic(conn->dev,
2341				       "[CONNECTOR:%d:%s] busy with a previous commit\n",
2342				       conn->base.id, conn->name);
2343
2344			return -EBUSY;
2345		}
2346
2347		/* Always track connectors explicitly for e.g. link retraining. */
2348		commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
2349		if (!commit)
2350			return -ENOMEM;
2351
2352		new_conn_state->commit = drm_crtc_commit_get(commit);
2353	}
2354
2355	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2356		/*
2357		 * Userspace is not allowed to get ahead of the previous
2358		 * commit with nonblocking ones.
2359		 */
2360		if (nonblock && old_plane_state->commit &&
2361		    !try_wait_for_completion(&old_plane_state->commit->flip_done)) {
2362			drm_dbg_atomic(plane->dev,
2363				       "[PLANE:%d:%s] busy with a previous commit\n",
2364				       plane->base.id, plane->name);
2365
2366			return -EBUSY;
2367		}
2368
2369		/* Always track planes explicitly for async pageflip support. */
2370		commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
2371		if (!commit)
2372			return -ENOMEM;
2373
2374		new_plane_state->commit = drm_crtc_commit_get(commit);
2375	}
2376
2377	if (funcs && funcs->atomic_commit_setup)
2378		return funcs->atomic_commit_setup(state);
2379
2380	return 0;
2381}
2382EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2383
2384/**
2385 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
2386 * @old_state: atomic state object with old state structures
2387 *
2388 * This function waits for all preceeding commits that touch the same CRTC as
2389 * @old_state to both be committed to the hardware (as signalled by
2390 * drm_atomic_helper_commit_hw_done()) and executed by the hardware (as signalled
2391 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
2392 *
2393 * This is part of the atomic helper support for nonblocking commits, see
2394 * drm_atomic_helper_setup_commit() for an overview.
2395 */
2396void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
2397{
2398	struct drm_crtc *crtc;
2399	struct drm_crtc_state *old_crtc_state;
2400	struct drm_plane *plane;
2401	struct drm_plane_state *old_plane_state;
2402	struct drm_connector *conn;
2403	struct drm_connector_state *old_conn_state;
2404	int i;
2405	long ret;
2406
2407	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2408		ret = drm_crtc_commit_wait(old_crtc_state->commit);
2409		if (ret)
2410			drm_err(crtc->dev,
2411				"[CRTC:%d:%s] commit wait timed out\n",
2412				crtc->base.id, crtc->name);
2413	}
2414
2415	for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2416		ret = drm_crtc_commit_wait(old_conn_state->commit);
2417		if (ret)
2418			drm_err(conn->dev,
2419				"[CONNECTOR:%d:%s] commit wait timed out\n",
2420				conn->base.id, conn->name);
2421	}
2422
2423	for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2424		ret = drm_crtc_commit_wait(old_plane_state->commit);
2425		if (ret)
2426			drm_err(plane->dev,
2427				"[PLANE:%d:%s] commit wait timed out\n",
2428				plane->base.id, plane->name);
2429	}
2430}
2431EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2432
2433/**
2434 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2435 * @old_state: atomic state object with old state structures
2436 *
2437 * This function walks all CRTCs and fakes VBLANK events on those with
2438 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2439 * The primary use of this function is writeback connectors working in oneshot
2440 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2441 * when a job is queued, and any change to the pipeline that does not touch the
2442 * connector is leading to timeouts when calling
2443 * drm_atomic_helper_wait_for_vblanks() or
2444 * drm_atomic_helper_wait_for_flip_done(). In addition to writeback
2445 * connectors, this function can also fake VBLANK events for CRTCs without
2446 * VBLANK interrupt.
2447 *
2448 * This is part of the atomic helper support for nonblocking commits, see
2449 * drm_atomic_helper_setup_commit() for an overview.
2450 */
2451void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2452{
2453	struct drm_crtc_state *new_crtc_state;
2454	struct drm_crtc *crtc;
2455	int i;
2456
2457	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2458		unsigned long flags;
2459
2460		if (!new_crtc_state->no_vblank)
2461			continue;
2462
2463		spin_lock_irqsave(&old_state->dev->event_lock, flags);
2464		if (new_crtc_state->event) {
2465			drm_crtc_send_vblank_event(crtc,
2466						   new_crtc_state->event);
2467			new_crtc_state->event = NULL;
2468		}
2469		spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2470	}
2471}
2472EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2473
2474/**
2475 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
2476 * @old_state: atomic state object with old state structures
2477 *
2478 * This function is used to signal completion of the hardware commit step. After
2479 * this step the driver is not allowed to read or change any permanent software
2480 * or hardware modeset state. The only exception is state protected by other
2481 * means than &drm_modeset_lock locks.
2482 *
2483 * Drivers should try to postpone any expensive or delayed cleanup work after
2484 * this function is called.
2485 *
2486 * This is part of the atomic helper support for nonblocking commits, see
2487 * drm_atomic_helper_setup_commit() for an overview.
2488 */
2489void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2490{
2491	struct drm_crtc *crtc;
2492	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2493	struct drm_crtc_commit *commit;
2494	int i;
2495
2496	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2497		commit = new_crtc_state->commit;
2498		if (!commit)
2499			continue;
2500
2501		/*
2502		 * copy new_crtc_state->commit to old_crtc_state->commit,
2503		 * it's unsafe to touch new_crtc_state after hw_done,
2504		 * but we still need to do so in cleanup_done().
2505		 */
2506		if (old_crtc_state->commit)
2507			drm_crtc_commit_put(old_crtc_state->commit);
2508
2509		old_crtc_state->commit = drm_crtc_commit_get(commit);
2510
2511		/* backend must have consumed any event by now */
2512		WARN_ON(new_crtc_state->event);
2513		complete_all(&commit->hw_done);
2514	}
2515
2516	if (old_state->fake_commit) {
2517		complete_all(&old_state->fake_commit->hw_done);
2518		complete_all(&old_state->fake_commit->flip_done);
2519	}
2520}
2521EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2522
2523/**
2524 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
2525 * @old_state: atomic state object with old state structures
2526 *
2527 * This signals completion of the atomic update @old_state, including any
2528 * cleanup work. If used, it must be called right before calling
2529 * drm_atomic_state_put().
2530 *
2531 * This is part of the atomic helper support for nonblocking commits, see
2532 * drm_atomic_helper_setup_commit() for an overview.
2533 */
2534void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2535{
2536	struct drm_crtc *crtc;
2537	struct drm_crtc_state *old_crtc_state;
2538	struct drm_crtc_commit *commit;
2539	int i;
2540
2541	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2542		commit = old_crtc_state->commit;
2543		if (WARN_ON(!commit))
2544			continue;
2545
2546		complete_all(&commit->cleanup_done);
2547		WARN_ON(!try_wait_for_completion(&commit->hw_done));
2548
2549		spin_lock(&crtc->commit_lock);
2550		list_del(&commit->commit_entry);
2551		spin_unlock(&crtc->commit_lock);
2552	}
2553
2554	if (old_state->fake_commit) {
2555		complete_all(&old_state->fake_commit->cleanup_done);
2556		WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2557	}
2558}
2559EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2560
2561/**
2562 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
2563 * @dev: DRM device
2564 * @state: atomic state object with new state structures
2565 *
2566 * This function prepares plane state, specifically framebuffers, for the new
2567 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2568 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2569 * any already successfully prepared framebuffer.
2570 *
2571 * Returns:
2572 * 0 on success, negative error code on failure.
2573 */
2574int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2575				     struct drm_atomic_state *state)
2576{
2577	struct drm_connector *connector;
2578	struct drm_connector_state *new_conn_state;
2579	struct drm_plane *plane;
2580	struct drm_plane_state *new_plane_state;
2581	int ret, i, j;
2582
2583	for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2584		if (!new_conn_state->writeback_job)
2585			continue;
2586
2587		ret = drm_writeback_prepare_job(new_conn_state->writeback_job);
2588		if (ret < 0)
2589			return ret;
2590	}
2591
2592	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2593		const struct drm_plane_helper_funcs *funcs;
2594
2595		funcs = plane->helper_private;
2596
2597		if (funcs->prepare_fb) {
2598			ret = funcs->prepare_fb(plane, new_plane_state);
2599			if (ret)
2600				goto fail_prepare_fb;
2601		} else {
2602			WARN_ON_ONCE(funcs->cleanup_fb);
2603
2604			if (!drm_core_check_feature(dev, DRIVER_GEM))
2605				continue;
2606
2607			ret = drm_gem_plane_helper_prepare_fb(plane, new_plane_state);
2608			if (ret)
2609				goto fail_prepare_fb;
2610		}
2611	}
2612
2613	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2614		const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2615
2616		if (funcs->begin_fb_access) {
2617			ret = funcs->begin_fb_access(plane, new_plane_state);
2618			if (ret)
2619				goto fail_begin_fb_access;
2620		}
2621	}
2622
2623	return 0;
2624
2625fail_begin_fb_access:
2626	for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2627		const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2628
2629		if (j >= i)
2630			continue;
2631
2632		if (funcs->end_fb_access)
2633			funcs->end_fb_access(plane, new_plane_state);
2634	}
2635	i = j; /* set i to upper limit to cleanup all planes */
2636fail_prepare_fb:
2637	for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2638		const struct drm_plane_helper_funcs *funcs;
2639
2640		if (j >= i)
2641			continue;
2642
2643		funcs = plane->helper_private;
2644
2645		if (funcs->cleanup_fb)
2646			funcs->cleanup_fb(plane, new_plane_state);
2647	}
2648
2649	return ret;
2650}
2651EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2652
2653/**
2654 * drm_atomic_helper_unprepare_planes - release plane resources on aborts
2655 * @dev: DRM device
2656 * @state: atomic state object with old state structures
2657 *
2658 * This function cleans up plane state, specifically framebuffers, from the
2659 * atomic state. It undoes the effects of drm_atomic_helper_prepare_planes()
2660 * when aborting an atomic commit. For cleaning up after a successful commit
2661 * use drm_atomic_helper_cleanup_planes().
2662 */
2663void drm_atomic_helper_unprepare_planes(struct drm_device *dev,
2664					struct drm_atomic_state *state)
2665{
2666	struct drm_plane *plane;
2667	struct drm_plane_state *new_plane_state;
2668	int i;
2669
2670	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2671		const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2672
2673		if (funcs->end_fb_access)
2674			funcs->end_fb_access(plane, new_plane_state);
2675	}
2676
2677	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2678		const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2679
2680		if (funcs->cleanup_fb)
2681			funcs->cleanup_fb(plane, new_plane_state);
2682	}
2683}
2684EXPORT_SYMBOL(drm_atomic_helper_unprepare_planes);
2685
2686static bool plane_crtc_active(const struct drm_plane_state *state)
2687{
2688	return state->crtc && state->crtc->state->active;
2689}
2690
2691/**
2692 * drm_atomic_helper_commit_planes - commit plane state
2693 * @dev: DRM device
2694 * @old_state: atomic state object with old state structures
2695 * @flags: flags for committing plane state
2696 *
2697 * This function commits the new plane state using the plane and atomic helper
2698 * functions for planes and CRTCs. It assumes that the atomic state has already
2699 * been pushed into the relevant object state pointers, since this step can no
2700 * longer fail.
2701 *
2702 * It still requires the global state object @old_state to know which planes and
2703 * crtcs need to be updated though.
2704 *
2705 * Note that this function does all plane updates across all CRTCs in one step.
2706 * If the hardware can't support this approach look at
2707 * drm_atomic_helper_commit_planes_on_crtc() instead.
2708 *
2709 * Plane parameters can be updated by applications while the associated CRTC is
2710 * disabled. The DRM/KMS core will store the parameters in the plane state,
2711 * which will be available to the driver when the CRTC is turned on. As a result
2712 * most drivers don't need to be immediately notified of plane updates for a
2713 * disabled CRTC.
2714 *
2715 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2716 * @flags in order not to receive plane update notifications related to a
2717 * disabled CRTC. This avoids the need to manually ignore plane updates in
2718 * driver code when the driver and/or hardware can't or just don't need to deal
2719 * with updates on disabled CRTCs, for example when supporting runtime PM.
2720 *
2721 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2722 * display controllers require to disable a CRTC's planes when the CRTC is
2723 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2724 * call for a plane if the CRTC of the old plane state needs a modesetting
2725 * operation. Of course, the drivers need to disable the planes in their CRTC
2726 * disable callbacks since no one else would do that.
2727 *
2728 * The drm_atomic_helper_commit() default implementation doesn't set the
2729 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2730 * This should not be copied blindly by drivers.
2731 */
2732void drm_atomic_helper_commit_planes(struct drm_device *dev,
2733				     struct drm_atomic_state *old_state,
2734				     uint32_t flags)
2735{
2736	struct drm_crtc *crtc;
2737	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2738	struct drm_plane *plane;
2739	struct drm_plane_state *old_plane_state, *new_plane_state;
2740	int i;
2741	bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2742	bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2743
2744	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2745		const struct drm_crtc_helper_funcs *funcs;
2746
2747		funcs = crtc->helper_private;
2748
2749		if (!funcs || !funcs->atomic_begin)
2750			continue;
2751
2752		if (active_only && !new_crtc_state->active)
2753			continue;
2754
2755		funcs->atomic_begin(crtc, old_state);
2756	}
2757
2758	for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2759		const struct drm_plane_helper_funcs *funcs;
2760		bool disabling;
2761
2762		funcs = plane->helper_private;
2763
2764		if (!funcs)
2765			continue;
2766
2767		disabling = drm_atomic_plane_disabling(old_plane_state,
2768						       new_plane_state);
2769
2770		if (active_only) {
2771			/*
2772			 * Skip planes related to inactive CRTCs. If the plane
2773			 * is enabled use the state of the current CRTC. If the
2774			 * plane is being disabled use the state of the old
2775			 * CRTC to avoid skipping planes being disabled on an
2776			 * active CRTC.
2777			 */
2778			if (!disabling && !plane_crtc_active(new_plane_state))
2779				continue;
2780			if (disabling && !plane_crtc_active(old_plane_state))
2781				continue;
2782		}
2783
2784		/*
2785		 * Special-case disabling the plane if drivers support it.
2786		 */
2787		if (disabling && funcs->atomic_disable) {
2788			struct drm_crtc_state *crtc_state;
2789
2790			crtc_state = old_plane_state->crtc->state;
2791
2792			if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2793			    no_disable)
2794				continue;
2795
2796			funcs->atomic_disable(plane, old_state);
2797		} else if (new_plane_state->crtc || disabling) {
2798			funcs->atomic_update(plane, old_state);
2799
2800			if (!disabling && funcs->atomic_enable) {
2801				if (drm_atomic_plane_enabling(old_plane_state, new_plane_state))
2802					funcs->atomic_enable(plane, old_state);
2803			}
2804		}
2805	}
2806
2807	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2808		const struct drm_crtc_helper_funcs *funcs;
2809
2810		funcs = crtc->helper_private;
2811
2812		if (!funcs || !funcs->atomic_flush)
2813			continue;
2814
2815		if (active_only && !new_crtc_state->active)
2816			continue;
2817
2818		funcs->atomic_flush(crtc, old_state);
2819	}
2820
2821	/*
2822	 * Signal end of framebuffer access here before hw_done. After hw_done,
2823	 * a later commit might have already released the plane state.
2824	 */
2825	for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2826		const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2827
2828		if (funcs->end_fb_access)
2829			funcs->end_fb_access(plane, old_plane_state);
2830	}
2831}
2832EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2833
2834/**
2835 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a CRTC
2836 * @old_crtc_state: atomic state object with the old CRTC state
2837 *
2838 * This function commits the new plane state using the plane and atomic helper
2839 * functions for planes on the specific CRTC. It assumes that the atomic state
2840 * has already been pushed into the relevant object state pointers, since this
2841 * step can no longer fail.
2842 *
2843 * This function is useful when plane updates should be done CRTC-by-CRTC
2844 * instead of one global step like drm_atomic_helper_commit_planes() does.
2845 *
2846 * This function can only be savely used when planes are not allowed to move
2847 * between different CRTCs because this function doesn't handle inter-CRTC
2848 * dependencies. Callers need to ensure that either no such dependencies exist,
2849 * resolve them through ordering of commit calls or through some other means.
2850 */
2851void
2852drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2853{
2854	const struct drm_crtc_helper_funcs *crtc_funcs;
2855	struct drm_crtc *crtc = old_crtc_state->crtc;
2856	struct drm_atomic_state *old_state = old_crtc_state->state;
2857	struct drm_crtc_state *new_crtc_state =
2858		drm_atomic_get_new_crtc_state(old_state, crtc);
2859	struct drm_plane *plane;
2860	unsigned int plane_mask;
2861
2862	plane_mask = old_crtc_state->plane_mask;
2863	plane_mask |= new_crtc_state->plane_mask;
2864
2865	crtc_funcs = crtc->helper_private;
2866	if (crtc_funcs && crtc_funcs->atomic_begin)
2867		crtc_funcs->atomic_begin(crtc, old_state);
2868
2869	drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2870		struct drm_plane_state *old_plane_state =
2871			drm_atomic_get_old_plane_state(old_state, plane);
2872		struct drm_plane_state *new_plane_state =
2873			drm_atomic_get_new_plane_state(old_state, plane);
2874		const struct drm_plane_helper_funcs *plane_funcs;
2875		bool disabling;
2876
2877		plane_funcs = plane->helper_private;
2878
2879		if (!old_plane_state || !plane_funcs)
2880			continue;
2881
2882		WARN_ON(new_plane_state->crtc &&
2883			new_plane_state->crtc != crtc);
2884
2885		disabling = drm_atomic_plane_disabling(old_plane_state, new_plane_state);
2886
2887		if (disabling && plane_funcs->atomic_disable) {
2888			plane_funcs->atomic_disable(plane, old_state);
2889		} else if (new_plane_state->crtc || disabling) {
2890			plane_funcs->atomic_update(plane, old_state);
2891
2892			if (!disabling && plane_funcs->atomic_enable) {
2893				if (drm_atomic_plane_enabling(old_plane_state, new_plane_state))
2894					plane_funcs->atomic_enable(plane, old_state);
2895			}
2896		}
2897	}
2898
2899	if (crtc_funcs && crtc_funcs->atomic_flush)
2900		crtc_funcs->atomic_flush(crtc, old_state);
2901}
2902EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2903
2904/**
2905 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2906 * @old_crtc_state: atomic state object with the old CRTC state
2907 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2908 *
2909 * Disables all planes associated with the given CRTC. This can be
2910 * used for instance in the CRTC helper atomic_disable callback to disable
2911 * all planes.
2912 *
2913 * If the atomic-parameter is set the function calls the CRTC's
2914 * atomic_begin hook before and atomic_flush hook after disabling the
2915 * planes.
2916 *
2917 * It is a bug to call this function without having implemented the
2918 * &drm_plane_helper_funcs.atomic_disable plane hook.
2919 */
2920void
2921drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2922					 bool atomic)
2923{
2924	struct drm_crtc *crtc = old_crtc_state->crtc;
2925	const struct drm_crtc_helper_funcs *crtc_funcs =
2926		crtc->helper_private;
2927	struct drm_plane *plane;
2928
2929	if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2930		crtc_funcs->atomic_begin(crtc, NULL);
2931
2932	drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2933		const struct drm_plane_helper_funcs *plane_funcs =
2934			plane->helper_private;
2935
2936		if (!plane_funcs)
2937			continue;
2938
2939		WARN_ON(!plane_funcs->atomic_disable);
2940		if (plane_funcs->atomic_disable)
2941			plane_funcs->atomic_disable(plane, NULL);
2942	}
2943
2944	if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2945		crtc_funcs->atomic_flush(crtc, NULL);
2946}
2947EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2948
2949/**
2950 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2951 * @dev: DRM device
2952 * @old_state: atomic state object with old state structures
2953 *
2954 * This function cleans up plane state, specifically framebuffers, from the old
2955 * configuration. Hence the old configuration must be perserved in @old_state to
2956 * be able to call this function.
2957 *
2958 * This function may not be called on the new state when the atomic update
2959 * fails at any point after calling drm_atomic_helper_prepare_planes(). Use
2960 * drm_atomic_helper_unprepare_planes() in this case.
2961 */
2962void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2963				      struct drm_atomic_state *old_state)
2964{
2965	struct drm_plane *plane;
2966	struct drm_plane_state *old_plane_state;
2967	int i;
2968
2969	for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2970		const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2971
2972		if (funcs->cleanup_fb)
2973			funcs->cleanup_fb(plane, old_plane_state);
2974	}
2975}
2976EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2977
2978/**
2979 * drm_atomic_helper_swap_state - store atomic state into current sw state
2980 * @state: atomic state
2981 * @stall: stall for preceding commits
2982 *
2983 * This function stores the atomic state into the current state pointers in all
2984 * driver objects. It should be called after all failing steps have been done
2985 * and succeeded, but before the actual hardware state is committed.
2986 *
2987 * For cleanup and error recovery the current state for all changed objects will
2988 * be swapped into @state.
2989 *
2990 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2991 *
2992 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2993 *
2994 * 2. Do any other steps that might fail.
2995 *
2996 * 3. Put the staged state into the current state pointers with this function.
2997 *
2998 * 4. Actually commit the hardware state.
2999 *
3000 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
3001 * contains the old state. Also do any other cleanup required with that state.
3002 *
3003 * @stall must be set when nonblocking commits for this driver directly access
3004 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
3005 * the current atomic helpers this is almost always the case, since the helpers
3006 * don't pass the right state structures to the callbacks.
3007 *
3008 * Returns:
3009 *
3010 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
3011 * waiting for the previous commits has been interrupted.
3012 */
3013int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
3014				  bool stall)
3015{
3016	int i, ret;
3017	struct drm_connector *connector;
3018	struct drm_connector_state *old_conn_state, *new_conn_state;
3019	struct drm_crtc *crtc;
3020	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
3021	struct drm_plane *plane;
3022	struct drm_plane_state *old_plane_state, *new_plane_state;
3023	struct drm_crtc_commit *commit;
3024	struct drm_private_obj *obj;
3025	struct drm_private_state *old_obj_state, *new_obj_state;
3026
3027	if (stall) {
3028		/*
3029		 * We have to stall for hw_done here before
3030		 * drm_atomic_helper_wait_for_dependencies() because flip
3031		 * depth > 1 is not yet supported by all drivers. As long as
3032		 * obj->state is directly dereferenced anywhere in the drivers
3033		 * atomic_commit_tail function, then it's unsafe to swap state
3034		 * before drm_atomic_helper_commit_hw_done() is called.
3035		 */
3036
3037		for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
3038			commit = old_crtc_state->commit;
3039
3040			if (!commit)
3041				continue;
3042
3043			ret = wait_for_completion_interruptible(&commit->hw_done);
3044			if (ret)
3045				return ret;
3046		}
3047
3048		for_each_old_connector_in_state(state, connector, old_conn_state, i) {
3049			commit = old_conn_state->commit;
3050
3051			if (!commit)
3052				continue;
3053
3054			ret = wait_for_completion_interruptible(&commit->hw_done);
3055			if (ret)
3056				return ret;
3057		}
3058
3059		for_each_old_plane_in_state(state, plane, old_plane_state, i) {
3060			commit = old_plane_state->commit;
3061
3062			if (!commit)
3063				continue;
3064
3065			ret = wait_for_completion_interruptible(&commit->hw_done);
3066			if (ret)
3067				return ret;
3068		}
3069	}
3070
3071	for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
3072		WARN_ON(connector->state != old_conn_state);
3073
3074		old_conn_state->state = state;
3075		new_conn_state->state = NULL;
3076
3077		state->connectors[i].state = old_conn_state;
3078		connector->state = new_conn_state;
3079	}
3080
3081	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
3082		WARN_ON(crtc->state != old_crtc_state);
3083
3084		old_crtc_state->state = state;
3085		new_crtc_state->state = NULL;
3086
3087		state->crtcs[i].state = old_crtc_state;
3088		crtc->state = new_crtc_state;
3089
3090		if (new_crtc_state->commit) {
3091			spin_lock(&crtc->commit_lock);
3092			list_add(&new_crtc_state->commit->commit_entry,
3093				 &crtc->commit_list);
3094			spin_unlock(&crtc->commit_lock);
3095
3096			new_crtc_state->commit->event = NULL;
3097		}
3098	}
3099
3100	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
3101		WARN_ON(plane->state != old_plane_state);
3102
3103		old_plane_state->state = state;
3104		new_plane_state->state = NULL;
3105
3106		state->planes[i].state = old_plane_state;
3107		plane->state = new_plane_state;
3108	}
3109
3110	for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
3111		WARN_ON(obj->state != old_obj_state);
3112
3113		old_obj_state->state = state;
3114		new_obj_state->state = NULL;
3115
3116		state->private_objs[i].state = old_obj_state;
3117		obj->state = new_obj_state;
3118	}
3119
3120	return 0;
3121}
3122EXPORT_SYMBOL(drm_atomic_helper_swap_state);
3123
3124/**
3125 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
3126 * @plane: plane object to update
3127 * @crtc: owning CRTC of owning plane
3128 * @fb: framebuffer to flip onto plane
3129 * @crtc_x: x offset of primary plane on @crtc
3130 * @crtc_y: y offset of primary plane on @crtc
3131 * @crtc_w: width of primary plane rectangle on @crtc
3132 * @crtc_h: height of primary plane rectangle on @crtc
3133 * @src_x: x offset of @fb for panning
3134 * @src_y: y offset of @fb for panning
3135 * @src_w: width of source rectangle in @fb
3136 * @src_h: height of source rectangle in @fb
3137 * @ctx: lock acquire context
3138 *
3139 * Provides a default plane update handler using the atomic driver interface.
3140 *
3141 * RETURNS:
3142 * Zero on success, error code on failure
3143 */
3144int drm_atomic_helper_update_plane(struct drm_plane *plane,
3145				   struct drm_crtc *crtc,
3146				   struct drm_framebuffer *fb,
3147				   int crtc_x, int crtc_y,
3148				   unsigned int crtc_w, unsigned int crtc_h,
3149				   uint32_t src_x, uint32_t src_y,
3150				   uint32_t src_w, uint32_t src_h,
3151				   struct drm_modeset_acquire_ctx *ctx)
3152{
3153	struct drm_atomic_state *state;
3154	struct drm_plane_state *plane_state;
3155	int ret = 0;
3156
3157	state = drm_atomic_state_alloc(plane->dev);
3158	if (!state)
3159		return -ENOMEM;
3160
3161	state->acquire_ctx = ctx;
3162	plane_state = drm_atomic_get_plane_state(state, plane);
3163	if (IS_ERR(plane_state)) {
3164		ret = PTR_ERR(plane_state);
3165		goto fail;
3166	}
3167
3168	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3169	if (ret != 0)
3170		goto fail;
3171	drm_atomic_set_fb_for_plane(plane_state, fb);
3172	plane_state->crtc_x = crtc_x;
3173	plane_state->crtc_y = crtc_y;
3174	plane_state->crtc_w = crtc_w;
3175	plane_state->crtc_h = crtc_h;
3176	plane_state->src_x = src_x;
3177	plane_state->src_y = src_y;
3178	plane_state->src_w = src_w;
3179	plane_state->src_h = src_h;
3180
3181	if (plane == crtc->cursor)
3182		state->legacy_cursor_update = true;
3183
3184	ret = drm_atomic_commit(state);
3185fail:
3186	drm_atomic_state_put(state);
3187	return ret;
3188}
3189EXPORT_SYMBOL(drm_atomic_helper_update_plane);
3190
3191/**
3192 * drm_atomic_helper_disable_plane - Helper for primary plane disable using atomic
3193 * @plane: plane to disable
3194 * @ctx: lock acquire context
3195 *
3196 * Provides a default plane disable handler using the atomic driver interface.
3197 *
3198 * RETURNS:
3199 * Zero on success, error code on failure
3200 */
3201int drm_atomic_helper_disable_plane(struct drm_plane *plane,
3202				    struct drm_modeset_acquire_ctx *ctx)
3203{
3204	struct drm_atomic_state *state;
3205	struct drm_plane_state *plane_state;
3206	int ret = 0;
3207
3208	state = drm_atomic_state_alloc(plane->dev);
3209	if (!state)
3210		return -ENOMEM;
3211
3212	state->acquire_ctx = ctx;
3213	plane_state = drm_atomic_get_plane_state(state, plane);
3214	if (IS_ERR(plane_state)) {
3215		ret = PTR_ERR(plane_state);
3216		goto fail;
3217	}
3218
3219	if (plane_state->crtc && plane_state->crtc->cursor == plane)
3220		plane_state->state->legacy_cursor_update = true;
3221
3222	ret = __drm_atomic_helper_disable_plane(plane, plane_state);
3223	if (ret != 0)
3224		goto fail;
3225
3226	ret = drm_atomic_commit(state);
3227fail:
3228	drm_atomic_state_put(state);
3229	return ret;
3230}
3231EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
3232
3233/**
3234 * drm_atomic_helper_set_config - set a new config from userspace
3235 * @set: mode set configuration
3236 * @ctx: lock acquisition context
3237 *
3238 * Provides a default CRTC set_config handler using the atomic driver interface.
3239 *
3240 * NOTE: For backwards compatibility with old userspace this automatically
3241 * resets the "link-status" property to GOOD, to force any link
3242 * re-training. The SETCRTC ioctl does not define whether an update does
3243 * need a full modeset or just a plane update, hence we're allowed to do
3244 * that. See also drm_connector_set_link_status_property().
3245 *
3246 * Returns:
3247 * Returns 0 on success, negative errno numbers on failure.
3248 */
3249int drm_atomic_helper_set_config(struct drm_mode_set *set,
3250				 struct drm_modeset_acquire_ctx *ctx)
3251{
3252	struct drm_atomic_state *state;
3253	struct drm_crtc *crtc = set->crtc;
3254	int ret = 0;
3255
3256	state = drm_atomic_state_alloc(crtc->dev);
3257	if (!state)
3258		return -ENOMEM;
3259
3260	state->acquire_ctx = ctx;
3261	ret = __drm_atomic_helper_set_config(set, state);
3262	if (ret != 0)
3263		goto fail;
3264
3265	ret = handle_conflicting_encoders(state, true);
3266	if (ret)
3267		goto fail;
3268
3269	ret = drm_atomic_commit(state);
3270
3271fail:
3272	drm_atomic_state_put(state);
3273	return ret;
3274}
3275EXPORT_SYMBOL(drm_atomic_helper_set_config);
3276
3277/**
3278 * drm_atomic_helper_disable_all - disable all currently active outputs
3279 * @dev: DRM device
3280 * @ctx: lock acquisition context
3281 *
3282 * Loops through all connectors, finding those that aren't turned off and then
3283 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3284 * that they are connected to.
3285 *
3286 * This is used for example in suspend/resume to disable all currently active
3287 * functions when suspending. If you just want to shut down everything at e.g.
3288 * driver unload, look at drm_atomic_helper_shutdown().
3289 *
3290 * Note that if callers haven't already acquired all modeset locks this might
3291 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3292 *
3293 * Returns:
3294 * 0 on success or a negative error code on failure.
3295 *
3296 * See also:
3297 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3298 * drm_atomic_helper_shutdown().
3299 */
3300int drm_atomic_helper_disable_all(struct drm_device *dev,
3301				  struct drm_modeset_acquire_ctx *ctx)
3302{
3303	struct drm_atomic_state *state;
3304	struct drm_connector_state *conn_state;
3305	struct drm_connector *conn;
3306	struct drm_plane_state *plane_state;
3307	struct drm_plane *plane;
3308	struct drm_crtc_state *crtc_state;
3309	struct drm_crtc *crtc;
3310	int ret, i;
3311
3312	state = drm_atomic_state_alloc(dev);
3313	if (!state)
3314		return -ENOMEM;
3315
3316	state->acquire_ctx = ctx;
3317
3318	drm_for_each_crtc(crtc, dev) {
3319		crtc_state = drm_atomic_get_crtc_state(state, crtc);
3320		if (IS_ERR(crtc_state)) {
3321			ret = PTR_ERR(crtc_state);
3322			goto free;
3323		}
3324
3325		crtc_state->active = false;
3326
3327		ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3328		if (ret < 0)
3329			goto free;
3330
3331		ret = drm_atomic_add_affected_planes(state, crtc);
3332		if (ret < 0)
3333			goto free;
3334
3335		ret = drm_atomic_add_affected_connectors(state, crtc);
3336		if (ret < 0)
3337			goto free;
3338	}
3339
3340	for_each_new_connector_in_state(state, conn, conn_state, i) {
3341		ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3342		if (ret < 0)
3343			goto free;
3344	}
3345
3346	for_each_new_plane_in_state(state, plane, plane_state, i) {
3347		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3348		if (ret < 0)
3349			goto free;
3350
3351		drm_atomic_set_fb_for_plane(plane_state, NULL);
3352	}
3353
3354	ret = drm_atomic_commit(state);
3355free:
3356	drm_atomic_state_put(state);
3357	return ret;
3358}
3359EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3360
3361/**
3362 * drm_atomic_helper_shutdown - shutdown all CRTC
3363 * @dev: DRM device
3364 *
3365 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3366 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3367 * that also takes a snapshot of the modeset state to be restored on resume.
3368 *
3369 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3370 * and it is the atomic version of drm_helper_force_disable_all().
3371 */
3372void drm_atomic_helper_shutdown(struct drm_device *dev)
3373{
3374	struct drm_modeset_acquire_ctx ctx;
3375	int ret;
3376
3377	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
3378
3379	ret = drm_atomic_helper_disable_all(dev, &ctx);
3380	if (ret)
3381		drm_err(dev,
3382			"Disabling all crtc's during unload failed with %i\n",
3383			ret);
3384
3385	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
3386}
3387EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3388
3389/**
3390 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3391 * @dev: DRM device
3392 * @ctx: lock acquisition context
3393 *
3394 * Makes a copy of the current atomic state by looping over all objects and
3395 * duplicating their respective states. This is used for example by suspend/
3396 * resume support code to save the state prior to suspend such that it can
3397 * be restored upon resume.
3398 *
3399 * Note that this treats atomic state as persistent between save and restore.
3400 * Drivers must make sure that this is possible and won't result in confusion
3401 * or erroneous behaviour.
3402 *
3403 * Note that if callers haven't already acquired all modeset locks this might
3404 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3405 *
3406 * Returns:
3407 * A pointer to the copy of the atomic state object on success or an
3408 * ERR_PTR()-encoded error code on failure.
3409 *
3410 * See also:
3411 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3412 */
3413struct drm_atomic_state *
3414drm_atomic_helper_duplicate_state(struct drm_device *dev,
3415				  struct drm_modeset_acquire_ctx *ctx)
3416{
3417	struct drm_atomic_state *state;
3418	struct drm_connector *conn;
3419	struct drm_connector_list_iter conn_iter;
3420	struct drm_plane *plane;
3421	struct drm_crtc *crtc;
3422	int err = 0;
3423
3424	state = drm_atomic_state_alloc(dev);
3425	if (!state)
3426		return ERR_PTR(-ENOMEM);
3427
3428	state->acquire_ctx = ctx;
3429	state->duplicated = true;
3430
3431	drm_for_each_crtc(crtc, dev) {
3432		struct drm_crtc_state *crtc_state;
3433
3434		crtc_state = drm_atomic_get_crtc_state(state, crtc);
3435		if (IS_ERR(crtc_state)) {
3436			err = PTR_ERR(crtc_state);
3437			goto free;
3438		}
3439	}
3440
3441	drm_for_each_plane(plane, dev) {
3442		struct drm_plane_state *plane_state;
3443
3444		plane_state = drm_atomic_get_plane_state(state, plane);
3445		if (IS_ERR(plane_state)) {
3446			err = PTR_ERR(plane_state);
3447			goto free;
3448		}
3449	}
3450
3451	drm_connector_list_iter_begin(dev, &conn_iter);
3452	drm_for_each_connector_iter(conn, &conn_iter) {
3453		struct drm_connector_state *conn_state;
3454
3455		conn_state = drm_atomic_get_connector_state(state, conn);
3456		if (IS_ERR(conn_state)) {
3457			err = PTR_ERR(conn_state);
3458			drm_connector_list_iter_end(&conn_iter);
3459			goto free;
3460		}
3461	}
3462	drm_connector_list_iter_end(&conn_iter);
3463
3464	/* clear the acquire context so that it isn't accidentally reused */
3465	state->acquire_ctx = NULL;
3466
3467free:
3468	if (err < 0) {
3469		drm_atomic_state_put(state);
3470		state = ERR_PTR(err);
3471	}
3472
3473	return state;
3474}
3475EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3476
3477/**
3478 * drm_atomic_helper_suspend - subsystem-level suspend helper
3479 * @dev: DRM device
3480 *
3481 * Duplicates the current atomic state, disables all active outputs and then
3482 * returns a pointer to the original atomic state to the caller. Drivers can
3483 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3484 * restore the output configuration that was active at the time the system
3485 * entered suspend.
3486 *
3487 * Note that it is potentially unsafe to use this. The atomic state object
3488 * returned by this function is assumed to be persistent. Drivers must ensure
3489 * that this holds true. Before calling this function, drivers must make sure
3490 * to suspend fbdev emulation so that nothing can be using the device.
3491 *
3492 * Returns:
3493 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3494 * encoded error code on failure. Drivers should store the returned atomic
3495 * state object and pass it to the drm_atomic_helper_resume() helper upon
3496 * resume.
3497 *
3498 * See also:
3499 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
3500 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
3501 */
3502struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3503{
3504	struct drm_modeset_acquire_ctx ctx;
3505	struct drm_atomic_state *state;
3506	int err;
3507
3508	/* This can never be returned, but it makes the compiler happy */
3509	state = ERR_PTR(-EINVAL);
3510
3511	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3512
3513	state = drm_atomic_helper_duplicate_state(dev, &ctx);
3514	if (IS_ERR(state))
3515		goto unlock;
3516
3517	err = drm_atomic_helper_disable_all(dev, &ctx);
3518	if (err < 0) {
3519		drm_atomic_state_put(state);
3520		state = ERR_PTR(err);
3521		goto unlock;
3522	}
3523
3524unlock:
3525	DRM_MODESET_LOCK_ALL_END(dev, ctx, err);
3526	if (err)
3527		return ERR_PTR(err);
3528
3529	return state;
3530}
3531EXPORT_SYMBOL(drm_atomic_helper_suspend);
3532
3533/**
3534 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3535 * @state: duplicated atomic state to commit
3536 * @ctx: pointer to acquire_ctx to use for commit.
3537 *
3538 * The state returned by drm_atomic_helper_duplicate_state() and
3539 * drm_atomic_helper_suspend() is partially invalid, and needs to
3540 * be fixed up before commit.
3541 *
3542 * Returns:
3543 * 0 on success or a negative error code on failure.
3544 *
3545 * See also:
3546 * drm_atomic_helper_suspend()
3547 */
3548int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3549					      struct drm_modeset_acquire_ctx *ctx)
3550{
3551	int i, ret;
3552	struct drm_plane *plane;
3553	struct drm_plane_state *new_plane_state;
3554	struct drm_connector *connector;
3555	struct drm_connector_state *new_conn_state;
3556	struct drm_crtc *crtc;
3557	struct drm_crtc_state *new_crtc_state;
3558
3559	state->acquire_ctx = ctx;
3560
3561	for_each_new_plane_in_state(state, plane, new_plane_state, i)
3562		state->planes[i].old_state = plane->state;
3563
3564	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3565		state->crtcs[i].old_state = crtc->state;
3566
3567	for_each_new_connector_in_state(state, connector, new_conn_state, i)
3568		state->connectors[i].old_state = connector->state;
3569
3570	ret = drm_atomic_commit(state);
3571
3572	state->acquire_ctx = NULL;
3573
3574	return ret;
3575}
3576EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3577
3578/**
3579 * drm_atomic_helper_resume - subsystem-level resume helper
3580 * @dev: DRM device
3581 * @state: atomic state to resume to
3582 *
3583 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3584 * grabs all modeset locks and commits the atomic state object. This can be
3585 * used in conjunction with the drm_atomic_helper_suspend() helper to
3586 * implement suspend/resume for drivers that support atomic mode-setting.
3587 *
3588 * Returns:
3589 * 0 on success or a negative error code on failure.
3590 *
3591 * See also:
3592 * drm_atomic_helper_suspend()
3593 */
3594int drm_atomic_helper_resume(struct drm_device *dev,
3595			     struct drm_atomic_state *state)
3596{
3597	struct drm_modeset_acquire_ctx ctx;
3598	int err;
3599
3600	drm_mode_config_reset(dev);
3601
3602	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3603
3604	err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3605
3606	DRM_MODESET_LOCK_ALL_END(dev, ctx, err);
3607	drm_atomic_state_put(state);
3608
3609	return err;
3610}
3611EXPORT_SYMBOL(drm_atomic_helper_resume);
3612
3613static int page_flip_common(struct drm_atomic_state *state,
3614			    struct drm_crtc *crtc,
3615			    struct drm_framebuffer *fb,
3616			    struct drm_pending_vblank_event *event,
3617			    uint32_t flags)
3618{
3619	struct drm_plane *plane = crtc->primary;
3620	struct drm_plane_state *plane_state;
3621	struct drm_crtc_state *crtc_state;
3622	int ret = 0;
3623
3624	crtc_state = drm_atomic_get_crtc_state(state, crtc);
3625	if (IS_ERR(crtc_state))
3626		return PTR_ERR(crtc_state);
3627
3628	crtc_state->event = event;
3629	crtc_state->async_flip = flags & DRM_MODE_PAGE_FLIP_ASYNC;
3630
3631	plane_state = drm_atomic_get_plane_state(state, plane);
3632	if (IS_ERR(plane_state))
3633		return PTR_ERR(plane_state);
3634
3635	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3636	if (ret != 0)
3637		return ret;
3638	drm_atomic_set_fb_for_plane(plane_state, fb);
3639
3640	/* Make sure we don't accidentally do a full modeset. */
3641	state->allow_modeset = false;
3642	if (!crtc_state->active) {
3643		drm_dbg_atomic(crtc->dev,
3644			       "[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3645			       crtc->base.id, crtc->name);
3646		return -EINVAL;
3647	}
3648
3649	return ret;
3650}
3651
3652/**
3653 * drm_atomic_helper_page_flip - execute a legacy page flip
3654 * @crtc: DRM CRTC
3655 * @fb: DRM framebuffer
3656 * @event: optional DRM event to signal upon completion
3657 * @flags: flip flags for non-vblank sync'ed updates
3658 * @ctx: lock acquisition context
3659 *
3660 * Provides a default &drm_crtc_funcs.page_flip implementation
3661 * using the atomic driver interface.
3662 *
3663 * Returns:
3664 * Returns 0 on success, negative errno numbers on failure.
3665 *
3666 * See also:
3667 * drm_atomic_helper_page_flip_target()
3668 */
3669int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3670				struct drm_framebuffer *fb,
3671				struct drm_pending_vblank_event *event,
3672				uint32_t flags,
3673				struct drm_modeset_acquire_ctx *ctx)
3674{
3675	struct drm_plane *plane = crtc->primary;
3676	struct drm_atomic_state *state;
3677	int ret = 0;
3678
3679	state = drm_atomic_state_alloc(plane->dev);
3680	if (!state)
3681		return -ENOMEM;
3682
3683	state->acquire_ctx = ctx;
3684
3685	ret = page_flip_common(state, crtc, fb, event, flags);
3686	if (ret != 0)
3687		goto fail;
3688
3689	ret = drm_atomic_nonblocking_commit(state);
3690fail:
3691	drm_atomic_state_put(state);
3692	return ret;
3693}
3694EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3695
3696/**
3697 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3698 * @crtc: DRM CRTC
3699 * @fb: DRM framebuffer
3700 * @event: optional DRM event to signal upon completion
3701 * @flags: flip flags for non-vblank sync'ed updates
3702 * @target: specifying the target vblank period when the flip to take effect
3703 * @ctx: lock acquisition context
3704 *
3705 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3706 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3707 * target vblank period to flip.
3708 *
3709 * Returns:
3710 * Returns 0 on success, negative errno numbers on failure.
3711 */
3712int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3713				       struct drm_framebuffer *fb,
3714				       struct drm_pending_vblank_event *event,
3715				       uint32_t flags,
3716				       uint32_t target,
3717				       struct drm_modeset_acquire_ctx *ctx)
3718{
3719	struct drm_plane *plane = crtc->primary;
3720	struct drm_atomic_state *state;
3721	struct drm_crtc_state *crtc_state;
3722	int ret = 0;
3723
3724	state = drm_atomic_state_alloc(plane->dev);
3725	if (!state)
3726		return -ENOMEM;
3727
3728	state->acquire_ctx = ctx;
3729
3730	ret = page_flip_common(state, crtc, fb, event, flags);
3731	if (ret != 0)
3732		goto fail;
3733
3734	crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3735	if (WARN_ON(!crtc_state)) {
3736		ret = -EINVAL;
3737		goto fail;
3738	}
3739	crtc_state->target_vblank = target;
3740
3741	ret = drm_atomic_nonblocking_commit(state);
3742fail:
3743	drm_atomic_state_put(state);
3744	return ret;
3745}
3746EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3747
3748/**
3749 * drm_atomic_helper_bridge_propagate_bus_fmt() - Propagate output format to
3750 *						  the input end of a bridge
3751 * @bridge: bridge control structure
3752 * @bridge_state: new bridge state
3753 * @crtc_state: new CRTC state
3754 * @conn_state: new connector state
3755 * @output_fmt: tested output bus format
3756 * @num_input_fmts: will contain the size of the returned array
3757 *
3758 * This helper is a pluggable implementation of the
3759 * &drm_bridge_funcs.atomic_get_input_bus_fmts operation for bridges that don't
3760 * modify the bus configuration between their input and their output. It
3761 * returns an array of input formats with a single element set to @output_fmt.
3762 *
3763 * RETURNS:
3764 * a valid format array of size @num_input_fmts, or NULL if the allocation
3765 * failed
3766 */
3767u32 *
3768drm_atomic_helper_bridge_propagate_bus_fmt(struct drm_bridge *bridge,
3769					struct drm_bridge_state *bridge_state,
3770					struct drm_crtc_state *crtc_state,
3771					struct drm_connector_state *conn_state,
3772					u32 output_fmt,
3773					unsigned int *num_input_fmts)
3774{
3775	u32 *input_fmts;
3776
3777	input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
3778	if (!input_fmts) {
3779		*num_input_fmts = 0;
3780		return NULL;
3781	}
3782
3783	*num_input_fmts = 1;
3784	input_fmts[0] = output_fmt;
3785	return input_fmts;
3786}
3787EXPORT_SYMBOL(drm_atomic_helper_bridge_propagate_bus_fmt);
3788