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 #ifndef DRM_ATOMIC_HELPER_H_
29 #define DRM_ATOMIC_HELPER_H_
30 
31 #include <drm/drm_crtc.h>
32 
33 struct drm_atomic_state;
34 
35 int drm_atomic_helper_check_modeset(struct drm_device *dev,
36 				struct drm_atomic_state *state);
37 int drm_atomic_helper_check_planes(struct drm_device *dev,
38 			       struct drm_atomic_state *state);
39 int drm_atomic_helper_check(struct drm_device *dev,
40 			    struct drm_atomic_state *state);
41 void drm_atomic_helper_commit_tail(struct drm_atomic_state *state);
42 int drm_atomic_helper_commit(struct drm_device *dev,
43 			     struct drm_atomic_state *state,
44 			     bool nonblock);
45 
46 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
47 					struct drm_atomic_state *state,
48 					bool pre_swap);
49 bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
50 					   struct drm_atomic_state *old_state,
51 					   struct drm_crtc *crtc);
52 
53 void drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
54 					struct drm_atomic_state *old_state);
55 
56 void
57 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
58 					      struct drm_atomic_state *old_state);
59 
60 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
61 					       struct drm_atomic_state *state);
62 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
63 					  struct drm_atomic_state *old_state);
64 
65 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
66 				     struct drm_atomic_state *state);
67 
68 #define DRM_PLANE_COMMIT_ACTIVE_ONLY			BIT(0)
69 #define DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET	BIT(1)
70 
71 void drm_atomic_helper_commit_planes(struct drm_device *dev,
72 				     struct drm_atomic_state *state,
73 				     uint32_t flags);
74 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
75 				      struct drm_atomic_state *old_state);
76 void drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state);
77 void
78 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
79 					 bool atomic);
80 
81 void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
82 				  bool stall);
83 
84 /* nonblocking commit helpers */
85 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
86 				   bool nonblock);
87 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *state);
88 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *state);
89 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *state);
90 
91 /* implementations for legacy interfaces */
92 int drm_atomic_helper_update_plane(struct drm_plane *plane,
93 				   struct drm_crtc *crtc,
94 				   struct drm_framebuffer *fb,
95 				   int crtc_x, int crtc_y,
96 				   unsigned int crtc_w, unsigned int crtc_h,
97 				   uint32_t src_x, uint32_t src_y,
98 				   uint32_t src_w, uint32_t src_h);
99 int drm_atomic_helper_disable_plane(struct drm_plane *plane);
100 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
101 		struct drm_plane_state *plane_state);
102 int drm_atomic_helper_set_config(struct drm_mode_set *set);
103 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
104 		struct drm_atomic_state *state);
105 
106 int drm_atomic_helper_disable_all(struct drm_device *dev,
107 				  struct drm_modeset_acquire_ctx *ctx);
108 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev);
109 int drm_atomic_helper_resume(struct drm_device *dev,
110 			     struct drm_atomic_state *state);
111 
112 int drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
113 					struct drm_property *property,
114 					uint64_t val);
115 int drm_atomic_helper_plane_set_property(struct drm_plane *plane,
116 					struct drm_property *property,
117 					uint64_t val);
118 int drm_atomic_helper_connector_set_property(struct drm_connector *connector,
119 					struct drm_property *property,
120 					uint64_t val);
121 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
122 				struct drm_framebuffer *fb,
123 				struct drm_pending_vblank_event *event,
124 				uint32_t flags);
125 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
126 				     int mode);
127 struct drm_encoder *
128 drm_atomic_helper_best_encoder(struct drm_connector *connector);
129 
130 /* default implementations for state handling */
131 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc);
132 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
133 					      struct drm_crtc_state *state);
134 struct drm_crtc_state *
135 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc);
136 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state);
137 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
138 					  struct drm_crtc_state *state);
139 
140 void drm_atomic_helper_plane_reset(struct drm_plane *plane);
141 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
142 					       struct drm_plane_state *state);
143 struct drm_plane_state *
144 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane);
145 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state);
146 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
147 					  struct drm_plane_state *state);
148 
149 void __drm_atomic_helper_connector_reset(struct drm_connector *connector,
150 					 struct drm_connector_state *conn_state);
151 void drm_atomic_helper_connector_reset(struct drm_connector *connector);
152 void
153 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
154 					   struct drm_connector_state *state);
155 struct drm_connector_state *
156 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector);
157 struct drm_atomic_state *
158 drm_atomic_helper_duplicate_state(struct drm_device *dev,
159 				  struct drm_modeset_acquire_ctx *ctx);
160 void
161 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state);
162 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
163 					  struct drm_connector_state *state);
164 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
165 				       u16 *red, u16 *green, u16 *blue,
166 				       uint32_t size);
167 
168 /**
169  * drm_atomic_crtc_for_each_plane - iterate over planes currently attached to CRTC
170  * @plane: the loop cursor
171  * @crtc:  the crtc whose planes are iterated
172  *
173  * This iterates over the current state, useful (for example) when applying
174  * atomic state after it has been checked and swapped.  To iterate over the
175  * planes which *will* be attached (for ->atomic_check()) see
176  * drm_atomic_crtc_state_for_each_plane().
177  */
178 #define drm_atomic_crtc_for_each_plane(plane, crtc) \
179 	drm_for_each_plane_mask(plane, (crtc)->dev, (crtc)->state->plane_mask)
180 
181 /**
182  * drm_crtc_atomic_state_for_each_plane - iterate over attached planes in new state
183  * @plane: the loop cursor
184  * @crtc_state: the incoming crtc-state
185  *
186  * Similar to drm_crtc_for_each_plane(), but iterates the planes that will be
187  * attached if the specified state is applied.  Useful during (for example)
188  * ->atomic_check() operations, to validate the incoming state.
189  */
190 #define drm_atomic_crtc_state_for_each_plane(plane, crtc_state) \
191 	drm_for_each_plane_mask(plane, (crtc_state)->state->dev, (crtc_state)->plane_mask)
192 
193 /**
194  * drm_crtc_atomic_state_for_each_plane_state - iterate over attached planes in new state
195  * @plane: the loop cursor
196  * @plane_state: loop cursor for the plane's state, must be const
197  * @crtc_state: the incoming crtc-state
198  *
199  * Similar to drm_crtc_for_each_plane(), but iterates the planes that will be
200  * attached if the specified state is applied.  Useful during (for example)
201  * ->atomic_check() operations, to validate the incoming state.
202  *
203  * Compared to just drm_atomic_crtc_state_for_each_plane() this also fills in a
204  * const plane_state. This is useful when a driver just wants to peek at other
205  * active planes on this crtc, but does not need to change it.
206  */
207 #define drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) \
208 	drm_for_each_plane_mask(plane, (crtc_state)->state->dev, (crtc_state)->plane_mask) \
209 		for_each_if ((plane_state = \
210 			      __drm_atomic_get_current_plane_state((crtc_state)->state, \
211 								   plane)))
212 
213 /*
214  * drm_atomic_plane_disabling - check whether a plane is being disabled
215  * @plane: plane object
216  * @old_state: previous atomic state
217  *
218  * Checks the atomic state of a plane to determine whether it's being disabled
219  * or not. This also WARNs if it detects an invalid state (both CRTC and FB
220  * need to either both be NULL or both be non-NULL).
221  *
222  * RETURNS:
223  * True if the plane is being disabled, false otherwise.
224  */
225 static inline bool
226 drm_atomic_plane_disabling(struct drm_plane *plane,
227 			   struct drm_plane_state *old_state)
228 {
229 	/*
230 	 * When disabling a plane, CRTC and FB should always be NULL together.
231 	 * Anything else should be considered a bug in the atomic core, so we
232 	 * gently warn about it.
233 	 */
234 	WARN_ON((plane->state->crtc == NULL && plane->state->fb != NULL) ||
235 		(plane->state->crtc != NULL && plane->state->fb == NULL));
236 
237 	/*
238 	 * When using the transitional helpers, old_state may be NULL. If so,
239 	 * we know nothing about the current state and have to assume that it
240 	 * might be enabled.
241 	 *
242 	 * When using the atomic helpers, old_state won't be NULL. Therefore
243 	 * this check assumes that either the driver will have reconstructed
244 	 * the correct state in ->reset() or that the driver will have taken
245 	 * appropriate measures to disable all planes.
246 	 */
247 	return (!old_state || old_state->crtc) && !plane->state->crtc;
248 }
249 
250 #endif /* DRM_ATOMIC_HELPER_H_ */
251