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 #include <drm/drm_modeset_helper_vtables.h>
33 #include <drm/drm_modeset_helper.h>
34 #include <drm/drm_atomic_state_helper.h>
35 #include <drm/drm_util.h>
36 
37 /*
38  * Drivers that don't allow primary plane scaling may pass this macro in place
39  * of the min/max scale parameters of the plane-state checker function.
40  *
41  * Due to src being in 16.16 fixed point and dest being in integer pixels,
42  * 1<<16 represents no scaling.
43  */
44 #define DRM_PLANE_NO_SCALING (1<<16)
45 
46 struct drm_atomic_state;
47 struct drm_private_obj;
48 struct drm_private_state;
49 
50 int drm_atomic_helper_check_modeset(struct drm_device *dev,
51 				struct drm_atomic_state *state);
52 int
53 drm_atomic_helper_check_wb_encoder_state(struct drm_encoder *encoder,
54 					 struct drm_connector_state *conn_state);
55 int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
56 					const struct drm_crtc_state *crtc_state,
57 					int min_scale,
58 					int max_scale,
59 					bool can_position,
60 					bool can_update_disabled);
61 int drm_atomic_helper_check_planes(struct drm_device *dev,
62 			       struct drm_atomic_state *state);
63 int drm_atomic_helper_check_crtc_primary_plane(struct drm_crtc_state *crtc_state);
64 int drm_atomic_helper_check(struct drm_device *dev,
65 			    struct drm_atomic_state *state);
66 void drm_atomic_helper_commit_tail(struct drm_atomic_state *state);
67 void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *state);
68 int drm_atomic_helper_commit(struct drm_device *dev,
69 			     struct drm_atomic_state *state,
70 			     bool nonblock);
71 int drm_atomic_helper_async_check(struct drm_device *dev,
72 				  struct drm_atomic_state *state);
73 void drm_atomic_helper_async_commit(struct drm_device *dev,
74 				    struct drm_atomic_state *state);
75 
76 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
77 					struct drm_atomic_state *state,
78 					bool pre_swap);
79 
80 void drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
81 					struct drm_atomic_state *old_state);
82 
83 void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
84 					  struct drm_atomic_state *old_state);
85 
86 void
87 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
88 					      struct drm_atomic_state *old_state);
89 
90 void
91 drm_atomic_helper_calc_timestamping_constants(struct drm_atomic_state *state);
92 
93 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
94 					       struct drm_atomic_state *state);
95 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
96 					  struct drm_atomic_state *old_state);
97 
98 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
99 				     struct drm_atomic_state *state);
100 
101 #define DRM_PLANE_COMMIT_ACTIVE_ONLY			BIT(0)
102 #define DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET	BIT(1)
103 
104 void drm_atomic_helper_commit_planes(struct drm_device *dev,
105 				     struct drm_atomic_state *state,
106 				     uint32_t flags);
107 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
108 				      struct drm_atomic_state *old_state);
109 void drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state);
110 void
111 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
112 					 bool atomic);
113 
114 int __must_check drm_atomic_helper_swap_state(struct drm_atomic_state *state,
115 					      bool stall);
116 
117 /* nonblocking commit helpers */
118 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
119 				   bool nonblock);
120 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *state);
121 void drm_atomic_helper_fake_vblank(struct drm_atomic_state *state);
122 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *state);
123 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *state);
124 
125 /* implementations for legacy interfaces */
126 int drm_atomic_helper_update_plane(struct drm_plane *plane,
127 				   struct drm_crtc *crtc,
128 				   struct drm_framebuffer *fb,
129 				   int crtc_x, int crtc_y,
130 				   unsigned int crtc_w, unsigned int crtc_h,
131 				   uint32_t src_x, uint32_t src_y,
132 				   uint32_t src_w, uint32_t src_h,
133 				   struct drm_modeset_acquire_ctx *ctx);
134 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
135 				    struct drm_modeset_acquire_ctx *ctx);
136 int drm_atomic_helper_set_config(struct drm_mode_set *set,
137 				 struct drm_modeset_acquire_ctx *ctx);
138 
139 int drm_atomic_helper_disable_all(struct drm_device *dev,
140 				  struct drm_modeset_acquire_ctx *ctx);
141 void drm_atomic_helper_shutdown(struct drm_device *dev);
142 struct drm_atomic_state *
143 drm_atomic_helper_duplicate_state(struct drm_device *dev,
144 				  struct drm_modeset_acquire_ctx *ctx);
145 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev);
146 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
147 					      struct drm_modeset_acquire_ctx *ctx);
148 int drm_atomic_helper_resume(struct drm_device *dev,
149 			     struct drm_atomic_state *state);
150 
151 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
152 				struct drm_framebuffer *fb,
153 				struct drm_pending_vblank_event *event,
154 				uint32_t flags,
155 				struct drm_modeset_acquire_ctx *ctx);
156 int drm_atomic_helper_page_flip_target(
157 				struct drm_crtc *crtc,
158 				struct drm_framebuffer *fb,
159 				struct drm_pending_vblank_event *event,
160 				uint32_t flags,
161 				uint32_t target,
162 				struct drm_modeset_acquire_ctx *ctx);
163 
164 /**
165  * drm_atomic_crtc_for_each_plane - iterate over planes currently attached to CRTC
166  * @plane: the loop cursor
167  * @crtc:  the CRTC whose planes are iterated
168  *
169  * This iterates over the current state, useful (for example) when applying
170  * atomic state after it has been checked and swapped.  To iterate over the
171  * planes which *will* be attached (more useful in code called from
172  * &drm_mode_config_funcs.atomic_check) see
173  * drm_atomic_crtc_state_for_each_plane().
174  */
175 #define drm_atomic_crtc_for_each_plane(plane, crtc) \
176 	drm_for_each_plane_mask(plane, (crtc)->dev, (crtc)->state->plane_mask)
177 
178 /**
179  * drm_atomic_crtc_state_for_each_plane - iterate over attached planes in new state
180  * @plane: the loop cursor
181  * @crtc_state: the incoming CRTC state
182  *
183  * Similar to drm_crtc_for_each_plane(), but iterates the planes that will be
184  * attached if the specified state is applied.  Useful during for example
185  * in code called from &drm_mode_config_funcs.atomic_check operations, to
186  * validate the incoming state.
187  */
188 #define drm_atomic_crtc_state_for_each_plane(plane, crtc_state) \
189 	drm_for_each_plane_mask(plane, (crtc_state)->state->dev, (crtc_state)->plane_mask)
190 
191 /**
192  * drm_atomic_crtc_state_for_each_plane_state - iterate over attached planes in new state
193  * @plane: the loop cursor
194  * @plane_state: loop cursor for the plane's state, must be const
195  * @crtc_state: the incoming CRTC state
196  *
197  * Similar to drm_crtc_for_each_plane(), but iterates the planes that will be
198  * attached if the specified state is applied.  Useful during for example
199  * in code called from &drm_mode_config_funcs.atomic_check operations, to
200  * validate the incoming state.
201  *
202  * Compared to just drm_atomic_crtc_state_for_each_plane() this also fills in a
203  * const plane_state. This is useful when a driver just wants to peek at other
204  * active planes on this CRTC, but does not need to change it.
205  */
206 #define drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) \
207 	drm_for_each_plane_mask(plane, (crtc_state)->state->dev, (crtc_state)->plane_mask) \
208 		for_each_if ((plane_state = \
209 			      __drm_atomic_get_current_plane_state((crtc_state)->state, \
210 								   plane)))
211 
212 /**
213  * drm_atomic_plane_enabling - check whether a plane is being enabled
214  * @old_plane_state: old atomic plane state
215  * @new_plane_state: new atomic plane state
216  *
217  * Checks the atomic state of a plane to determine whether it's being enabled
218  * or not. This also WARNs if it detects an invalid state (both CRTC and FB
219  * need to either both be NULL or both be non-NULL).
220  *
221  * RETURNS:
222  * True if the plane is being enabled, false otherwise.
223  */
224 static inline bool drm_atomic_plane_enabling(struct drm_plane_state *old_plane_state,
225 					     struct drm_plane_state *new_plane_state)
226 {
227 	/*
228 	 * When enabling a plane, CRTC and FB should always be set together.
229 	 * Anything else should be considered a bug in the atomic core, so we
230 	 * gently warn about it.
231 	 */
232 	WARN_ON((!new_plane_state->crtc && new_plane_state->fb) ||
233 		(new_plane_state->crtc && !new_plane_state->fb));
234 
235 	return !old_plane_state->crtc && new_plane_state->crtc;
236 }
237 
238 /**
239  * drm_atomic_plane_disabling - check whether a plane is being disabled
240  * @old_plane_state: old atomic plane state
241  * @new_plane_state: new atomic plane state
242  *
243  * Checks the atomic state of a plane to determine whether it's being disabled
244  * or not. This also WARNs if it detects an invalid state (both CRTC and FB
245  * need to either both be NULL or both be non-NULL).
246  *
247  * RETURNS:
248  * True if the plane is being disabled, false otherwise.
249  */
250 static inline bool
251 drm_atomic_plane_disabling(struct drm_plane_state *old_plane_state,
252 			   struct drm_plane_state *new_plane_state)
253 {
254 	/*
255 	 * When disabling a plane, CRTC and FB should always be NULL together.
256 	 * Anything else should be considered a bug in the atomic core, so we
257 	 * gently warn about it.
258 	 */
259 	WARN_ON((new_plane_state->crtc == NULL && new_plane_state->fb != NULL) ||
260 		(new_plane_state->crtc != NULL && new_plane_state->fb == NULL));
261 
262 	return old_plane_state->crtc && !new_plane_state->crtc;
263 }
264 
265 u32 *
266 drm_atomic_helper_bridge_propagate_bus_fmt(struct drm_bridge *bridge,
267 					struct drm_bridge_state *bridge_state,
268 					struct drm_crtc_state *crtc_state,
269 					struct drm_connector_state *conn_state,
270 					u32 output_fmt,
271 					unsigned int *num_input_fmts);
272 
273 #endif /* DRM_ATOMIC_HELPER_H_ */
274