1 /*
2  * Copyright (C) 2018 Intel Corp.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors:
23  * Rob Clark <robdclark@gmail.com>
24  * Daniel Vetter <daniel.vetter@ffwll.ch>
25  */
26 
27 #include <drm/drm_atomic.h>
28 #include <drm/drm_atomic_state_helper.h>
29 #include <drm/drm_connector.h>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_device.h>
32 #include <drm/drm_plane.h>
33 #include <drm/drm_print.h>
34 #include <drm/drm_writeback.h>
35 
36 #include <linux/slab.h>
37 #include <linux/dma-fence.h>
38 
39 /**
40  * DOC: atomic state reset and initialization
41  *
42  * Both the drm core and the atomic helpers assume that there is always the full
43  * and correct atomic software state for all connectors, CRTCs and planes
44  * available. Which is a bit a problem on driver load and also after system
45  * suspend. One way to solve this is to have a hardware state read-out
46  * infrastructure which reconstructs the full software state (e.g. the i915
47  * driver).
48  *
49  * The simpler solution is to just reset the software state to everything off,
50  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
51  * the atomic helpers provide default reset implementations for all hooks.
52  *
53  * On the upside the precise state tracking of atomic simplifies system suspend
54  * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
55  * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
56  * For other drivers the building blocks are split out, see the documentation
57  * for these functions.
58  */
59 
60 /**
61  * __drm_atomic_helper_crtc_state_reset - reset the CRTC state
62  * @crtc_state: atomic CRTC state, must not be NULL
63  * @crtc: CRTC object, must not be NULL
64  *
65  * Initializes the newly allocated @crtc_state with default
66  * values. This is useful for drivers that subclass the CRTC state.
67  */
68 void
69 __drm_atomic_helper_crtc_state_reset(struct drm_crtc_state *crtc_state,
70 				     struct drm_crtc *crtc)
71 {
72 	crtc_state->crtc = crtc;
73 }
74 EXPORT_SYMBOL(__drm_atomic_helper_crtc_state_reset);
75 
76 /**
77  * __drm_atomic_helper_crtc_reset - reset state on CRTC
78  * @crtc: drm CRTC
79  * @crtc_state: CRTC state to assign
80  *
81  * Initializes the newly allocated @crtc_state and assigns it to
82  * the &drm_crtc->state pointer of @crtc, usually required when
83  * initializing the drivers or when called from the &drm_crtc_funcs.reset
84  * hook.
85  *
86  * This is useful for drivers that subclass the CRTC state.
87  */
88 void
89 __drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
90 			       struct drm_crtc_state *crtc_state)
91 {
92 	if (crtc_state)
93 		__drm_atomic_helper_crtc_state_reset(crtc_state, crtc);
94 
95 	crtc->state = crtc_state;
96 }
97 EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset);
98 
99 /**
100  * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
101  * @crtc: drm CRTC
102  *
103  * Resets the atomic state for @crtc by freeing the state pointer (which might
104  * be NULL, e.g. at driver load time) and allocating a new empty state object.
105  */
106 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
107 {
108 	struct drm_crtc_state *crtc_state =
109 		kzalloc(sizeof(*crtc->state), GFP_KERNEL);
110 
111 	if (crtc->state)
112 		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
113 
114 	__drm_atomic_helper_crtc_reset(crtc, crtc_state);
115 }
116 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
117 
118 /**
119  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
120  * @crtc: CRTC object
121  * @state: atomic CRTC state
122  *
123  * Copies atomic state from a CRTC's current state and resets inferred values.
124  * This is useful for drivers that subclass the CRTC state.
125  */
126 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
127 					      struct drm_crtc_state *state)
128 {
129 	memcpy(state, crtc->state, sizeof(*state));
130 
131 	if (state->mode_blob)
132 		drm_property_blob_get(state->mode_blob);
133 	if (state->degamma_lut)
134 		drm_property_blob_get(state->degamma_lut);
135 	if (state->ctm)
136 		drm_property_blob_get(state->ctm);
137 	if (state->gamma_lut)
138 		drm_property_blob_get(state->gamma_lut);
139 	state->mode_changed = false;
140 	state->active_changed = false;
141 	state->planes_changed = false;
142 	state->connectors_changed = false;
143 	state->color_mgmt_changed = false;
144 	state->zpos_changed = false;
145 	state->commit = NULL;
146 	state->event = NULL;
147 	state->async_flip = false;
148 
149 	/* Self refresh should be canceled when a new update is available */
150 	state->active = drm_atomic_crtc_effectively_active(state);
151 	state->self_refresh_active = false;
152 }
153 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
154 
155 /**
156  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
157  * @crtc: drm CRTC
158  *
159  * Default CRTC state duplicate hook for drivers which don't have their own
160  * subclassed CRTC state structure.
161  */
162 struct drm_crtc_state *
163 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
164 {
165 	struct drm_crtc_state *state;
166 
167 	if (WARN_ON(!crtc->state))
168 		return NULL;
169 
170 	state = kmalloc(sizeof(*state), GFP_KERNEL);
171 	if (state)
172 		__drm_atomic_helper_crtc_duplicate_state(crtc, state);
173 
174 	return state;
175 }
176 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
177 
178 /**
179  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
180  * @state: CRTC state object to release
181  *
182  * Releases all resources stored in the CRTC state without actually freeing
183  * the memory of the CRTC state. This is useful for drivers that subclass the
184  * CRTC state.
185  */
186 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
187 {
188 	if (state->commit) {
189 		/*
190 		 * In the event that a non-blocking commit returns
191 		 * -ERESTARTSYS before the commit_tail work is queued, we will
192 		 * have an extra reference to the commit object. Release it, if
193 		 * the event has not been consumed by the worker.
194 		 *
195 		 * state->event may be freed, so we can't directly look at
196 		 * state->event->base.completion.
197 		 */
198 		if (state->event && state->commit->abort_completion)
199 			drm_crtc_commit_put(state->commit);
200 
201 		kfree(state->commit->event);
202 		state->commit->event = NULL;
203 
204 		drm_crtc_commit_put(state->commit);
205 	}
206 
207 	drm_property_blob_put(state->mode_blob);
208 	drm_property_blob_put(state->degamma_lut);
209 	drm_property_blob_put(state->ctm);
210 	drm_property_blob_put(state->gamma_lut);
211 }
212 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
213 
214 /**
215  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
216  * @crtc: drm CRTC
217  * @state: CRTC state object to release
218  *
219  * Default CRTC state destroy hook for drivers which don't have their own
220  * subclassed CRTC state structure.
221  */
222 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
223 					  struct drm_crtc_state *state)
224 {
225 	__drm_atomic_helper_crtc_destroy_state(state);
226 	kfree(state);
227 }
228 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
229 
230 /**
231  * __drm_atomic_helper_plane_state_reset - resets plane state to default values
232  * @plane_state: atomic plane state, must not be NULL
233  * @plane: plane object, must not be NULL
234  *
235  * Initializes the newly allocated @plane_state with default
236  * values. This is useful for drivers that subclass the CRTC state.
237  */
238 void __drm_atomic_helper_plane_state_reset(struct drm_plane_state *plane_state,
239 					   struct drm_plane *plane)
240 {
241 	plane_state->plane = plane;
242 	plane_state->rotation = DRM_MODE_ROTATE_0;
243 
244 	plane_state->alpha = DRM_BLEND_ALPHA_OPAQUE;
245 	plane_state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
246 }
247 EXPORT_SYMBOL(__drm_atomic_helper_plane_state_reset);
248 
249 /**
250  * __drm_atomic_helper_plane_reset - reset state on plane
251  * @plane: drm plane
252  * @plane_state: plane state to assign
253  *
254  * Initializes the newly allocated @plane_state and assigns it to
255  * the &drm_crtc->state pointer of @plane, usually required when
256  * initializing the drivers or when called from the &drm_plane_funcs.reset
257  * hook.
258  *
259  * This is useful for drivers that subclass the plane state.
260  */
261 void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
262 				     struct drm_plane_state *plane_state)
263 {
264 	if (plane_state)
265 		__drm_atomic_helper_plane_state_reset(plane_state, plane);
266 
267 	plane->state = plane_state;
268 }
269 EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
270 
271 /**
272  * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
273  * @plane: drm plane
274  *
275  * Resets the atomic state for @plane by freeing the state pointer (which might
276  * be NULL, e.g. at driver load time) and allocating a new empty state object.
277  */
278 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
279 {
280 	if (plane->state)
281 		__drm_atomic_helper_plane_destroy_state(plane->state);
282 
283 	kfree(plane->state);
284 	plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
285 	if (plane->state)
286 		__drm_atomic_helper_plane_reset(plane, plane->state);
287 }
288 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
289 
290 /**
291  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
292  * @plane: plane object
293  * @state: atomic plane state
294  *
295  * Copies atomic state from a plane's current state. This is useful for
296  * drivers that subclass the plane state.
297  */
298 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
299 					       struct drm_plane_state *state)
300 {
301 	memcpy(state, plane->state, sizeof(*state));
302 
303 	if (state->fb)
304 		drm_framebuffer_get(state->fb);
305 
306 	state->fence = NULL;
307 	state->commit = NULL;
308 	state->fb_damage_clips = NULL;
309 }
310 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
311 
312 /**
313  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
314  * @plane: drm plane
315  *
316  * Default plane state duplicate hook for drivers which don't have their own
317  * subclassed plane state structure.
318  */
319 struct drm_plane_state *
320 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
321 {
322 	struct drm_plane_state *state;
323 
324 	if (WARN_ON(!plane->state))
325 		return NULL;
326 
327 	state = kmalloc(sizeof(*state), GFP_KERNEL);
328 	if (state)
329 		__drm_atomic_helper_plane_duplicate_state(plane, state);
330 
331 	return state;
332 }
333 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
334 
335 /**
336  * __drm_atomic_helper_plane_destroy_state - release plane state
337  * @state: plane state object to release
338  *
339  * Releases all resources stored in the plane state without actually freeing
340  * the memory of the plane state. This is useful for drivers that subclass the
341  * plane state.
342  */
343 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
344 {
345 	if (state->fb)
346 		drm_framebuffer_put(state->fb);
347 
348 	if (state->fence)
349 		dma_fence_put(state->fence);
350 
351 	if (state->commit)
352 		drm_crtc_commit_put(state->commit);
353 
354 	drm_property_blob_put(state->fb_damage_clips);
355 }
356 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
357 
358 /**
359  * drm_atomic_helper_plane_destroy_state - default state destroy hook
360  * @plane: drm plane
361  * @state: plane state object to release
362  *
363  * Default plane state destroy hook for drivers which don't have their own
364  * subclassed plane state structure.
365  */
366 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
367 					   struct drm_plane_state *state)
368 {
369 	__drm_atomic_helper_plane_destroy_state(state);
370 	kfree(state);
371 }
372 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
373 
374 /**
375  * __drm_atomic_helper_connector_state_reset - reset the connector state
376  * @conn_state: atomic connector state, must not be NULL
377  * @connector: connectotr object, must not be NULL
378  *
379  * Initializes the newly allocated @conn_state with default
380  * values. This is useful for drivers that subclass the connector state.
381  */
382 void
383 __drm_atomic_helper_connector_state_reset(struct drm_connector_state *conn_state,
384 					  struct drm_connector *connector)
385 {
386 	conn_state->connector = connector;
387 }
388 EXPORT_SYMBOL(__drm_atomic_helper_connector_state_reset);
389 
390 /**
391  * __drm_atomic_helper_connector_reset - reset state on connector
392  * @connector: drm connector
393  * @conn_state: connector state to assign
394  *
395  * Initializes the newly allocated @conn_state and assigns it to
396  * the &drm_connector->state pointer of @connector, usually required when
397  * initializing the drivers or when called from the &drm_connector_funcs.reset
398  * hook.
399  *
400  * This is useful for drivers that subclass the connector state.
401  */
402 void
403 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
404 				    struct drm_connector_state *conn_state)
405 {
406 	if (conn_state)
407 		__drm_atomic_helper_connector_state_reset(conn_state, connector);
408 
409 	connector->state = conn_state;
410 }
411 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
412 
413 /**
414  * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
415  * @connector: drm connector
416  *
417  * Resets the atomic state for @connector by freeing the state pointer (which
418  * might be NULL, e.g. at driver load time) and allocating a new empty state
419  * object.
420  */
421 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
422 {
423 	struct drm_connector_state *conn_state =
424 		kzalloc(sizeof(*conn_state), GFP_KERNEL);
425 
426 	if (connector->state)
427 		__drm_atomic_helper_connector_destroy_state(connector->state);
428 
429 	kfree(connector->state);
430 	__drm_atomic_helper_connector_reset(connector, conn_state);
431 }
432 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
433 
434 /**
435  * drm_atomic_helper_connector_tv_reset - Resets TV connector properties
436  * @connector: DRM connector
437  *
438  * Resets the TV-related properties attached to a connector.
439  */
440 void drm_atomic_helper_connector_tv_reset(struct drm_connector *connector)
441 {
442 	struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
443 	struct drm_connector_state *state = connector->state;
444 
445 	state->tv.margins.left = cmdline->tv_margins.left;
446 	state->tv.margins.right = cmdline->tv_margins.right;
447 	state->tv.margins.top = cmdline->tv_margins.top;
448 	state->tv.margins.bottom = cmdline->tv_margins.bottom;
449 }
450 EXPORT_SYMBOL(drm_atomic_helper_connector_tv_reset);
451 
452 /**
453  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
454  * @connector: connector object
455  * @state: atomic connector state
456  *
457  * Copies atomic state from a connector's current state. This is useful for
458  * drivers that subclass the connector state.
459  */
460 void
461 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
462 					    struct drm_connector_state *state)
463 {
464 	memcpy(state, connector->state, sizeof(*state));
465 	if (state->crtc)
466 		drm_connector_get(connector);
467 	state->commit = NULL;
468 
469 	if (state->hdr_output_metadata)
470 		drm_property_blob_get(state->hdr_output_metadata);
471 
472 	/* Don't copy over a writeback job, they are used only once */
473 	state->writeback_job = NULL;
474 }
475 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
476 
477 /**
478  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
479  * @connector: drm connector
480  *
481  * Default connector state duplicate hook for drivers which don't have their own
482  * subclassed connector state structure.
483  */
484 struct drm_connector_state *
485 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
486 {
487 	struct drm_connector_state *state;
488 
489 	if (WARN_ON(!connector->state))
490 		return NULL;
491 
492 	state = kmalloc(sizeof(*state), GFP_KERNEL);
493 	if (state)
494 		__drm_atomic_helper_connector_duplicate_state(connector, state);
495 
496 	return state;
497 }
498 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
499 
500 /**
501  * __drm_atomic_helper_connector_destroy_state - release connector state
502  * @state: connector state object to release
503  *
504  * Releases all resources stored in the connector state without actually
505  * freeing the memory of the connector state. This is useful for drivers that
506  * subclass the connector state.
507  */
508 void
509 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
510 {
511 	if (state->crtc)
512 		drm_connector_put(state->connector);
513 
514 	if (state->commit)
515 		drm_crtc_commit_put(state->commit);
516 
517 	if (state->writeback_job)
518 		drm_writeback_cleanup_job(state->writeback_job);
519 
520 	drm_property_blob_put(state->hdr_output_metadata);
521 }
522 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
523 
524 /**
525  * drm_atomic_helper_connector_destroy_state - default state destroy hook
526  * @connector: drm connector
527  * @state: connector state object to release
528  *
529  * Default connector state destroy hook for drivers which don't have their own
530  * subclassed connector state structure.
531  */
532 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
533 					  struct drm_connector_state *state)
534 {
535 	__drm_atomic_helper_connector_destroy_state(state);
536 	kfree(state);
537 }
538 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
539 
540 /**
541  * __drm_atomic_helper_private_duplicate_state - copy atomic private state
542  * @obj: CRTC object
543  * @state: new private object state
544  *
545  * Copies atomic state from a private objects's current state and resets inferred values.
546  * This is useful for drivers that subclass the private state.
547  */
548 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
549 						     struct drm_private_state *state)
550 {
551 	memcpy(state, obj->state, sizeof(*state));
552 }
553 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);
554