xref: /linux/drivers/gpu/drm/drm_atomic.c (revision e08a1d97d33e2ac05cd368b955f9fdc2823f15fd)
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 
29 #include <drm/drmP.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_mode.h>
32 #include <drm/drm_plane_helper.h>
33 
34 #include "drm_crtc_internal.h"
35 
36 static void crtc_commit_free(struct kref *kref)
37 {
38 	struct drm_crtc_commit *commit =
39 		container_of(kref, struct drm_crtc_commit, ref);
40 
41 	kfree(commit);
42 }
43 
44 void drm_crtc_commit_put(struct drm_crtc_commit *commit)
45 {
46 	kref_put(&commit->ref, crtc_commit_free);
47 }
48 EXPORT_SYMBOL(drm_crtc_commit_put);
49 
50 /**
51  * drm_atomic_state_default_release -
52  * release memory initialized by drm_atomic_state_init
53  * @state: atomic state
54  *
55  * Free all the memory allocated by drm_atomic_state_init.
56  * This is useful for drivers that subclass the atomic state.
57  */
58 void drm_atomic_state_default_release(struct drm_atomic_state *state)
59 {
60 	kfree(state->connectors);
61 	kfree(state->crtcs);
62 	kfree(state->planes);
63 }
64 EXPORT_SYMBOL(drm_atomic_state_default_release);
65 
66 /**
67  * drm_atomic_state_init - init new atomic state
68  * @dev: DRM device
69  * @state: atomic state
70  *
71  * Default implementation for filling in a new atomic state.
72  * This is useful for drivers that subclass the atomic state.
73  */
74 int
75 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
76 {
77 	kref_init(&state->ref);
78 
79 	/* TODO legacy paths should maybe do a better job about
80 	 * setting this appropriately?
81 	 */
82 	state->allow_modeset = true;
83 
84 	state->crtcs = kcalloc(dev->mode_config.num_crtc,
85 			       sizeof(*state->crtcs), GFP_KERNEL);
86 	if (!state->crtcs)
87 		goto fail;
88 	state->planes = kcalloc(dev->mode_config.num_total_plane,
89 				sizeof(*state->planes), GFP_KERNEL);
90 	if (!state->planes)
91 		goto fail;
92 
93 	state->dev = dev;
94 
95 	DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
96 
97 	return 0;
98 fail:
99 	drm_atomic_state_default_release(state);
100 	return -ENOMEM;
101 }
102 EXPORT_SYMBOL(drm_atomic_state_init);
103 
104 /**
105  * drm_atomic_state_alloc - allocate atomic state
106  * @dev: DRM device
107  *
108  * This allocates an empty atomic state to track updates.
109  */
110 struct drm_atomic_state *
111 drm_atomic_state_alloc(struct drm_device *dev)
112 {
113 	struct drm_mode_config *config = &dev->mode_config;
114 	struct drm_atomic_state *state;
115 
116 	if (!config->funcs->atomic_state_alloc) {
117 		state = kzalloc(sizeof(*state), GFP_KERNEL);
118 		if (!state)
119 			return NULL;
120 		if (drm_atomic_state_init(dev, state) < 0) {
121 			kfree(state);
122 			return NULL;
123 		}
124 		return state;
125 	}
126 
127 	return config->funcs->atomic_state_alloc(dev);
128 }
129 EXPORT_SYMBOL(drm_atomic_state_alloc);
130 
131 /**
132  * drm_atomic_state_default_clear - clear base atomic state
133  * @state: atomic state
134  *
135  * Default implementation for clearing atomic state.
136  * This is useful for drivers that subclass the atomic state.
137  */
138 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
139 {
140 	struct drm_device *dev = state->dev;
141 	struct drm_mode_config *config = &dev->mode_config;
142 	int i;
143 
144 	DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
145 
146 	for (i = 0; i < state->num_connector; i++) {
147 		struct drm_connector *connector = state->connectors[i].ptr;
148 
149 		if (!connector)
150 			continue;
151 
152 		connector->funcs->atomic_destroy_state(connector,
153 						       state->connectors[i].state);
154 		state->connectors[i].ptr = NULL;
155 		state->connectors[i].state = NULL;
156 		drm_connector_unreference(connector);
157 	}
158 
159 	for (i = 0; i < config->num_crtc; i++) {
160 		struct drm_crtc *crtc = state->crtcs[i].ptr;
161 
162 		if (!crtc)
163 			continue;
164 
165 		crtc->funcs->atomic_destroy_state(crtc,
166 						  state->crtcs[i].state);
167 
168 		if (state->crtcs[i].commit) {
169 			kfree(state->crtcs[i].commit->event);
170 			state->crtcs[i].commit->event = NULL;
171 			drm_crtc_commit_put(state->crtcs[i].commit);
172 		}
173 
174 		state->crtcs[i].commit = NULL;
175 		state->crtcs[i].ptr = NULL;
176 		state->crtcs[i].state = NULL;
177 	}
178 
179 	for (i = 0; i < config->num_total_plane; i++) {
180 		struct drm_plane *plane = state->planes[i].ptr;
181 
182 		if (!plane)
183 			continue;
184 
185 		plane->funcs->atomic_destroy_state(plane,
186 						   state->planes[i].state);
187 		state->planes[i].ptr = NULL;
188 		state->planes[i].state = NULL;
189 	}
190 }
191 EXPORT_SYMBOL(drm_atomic_state_default_clear);
192 
193 /**
194  * drm_atomic_state_clear - clear state object
195  * @state: atomic state
196  *
197  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
198  * all locks. So someone else could sneak in and change the current modeset
199  * configuration. Which means that all the state assembled in @state is no
200  * longer an atomic update to the current state, but to some arbitrary earlier
201  * state. Which could break assumptions the driver's ->atomic_check likely
202  * relies on.
203  *
204  * Hence we must clear all cached state and completely start over, using this
205  * function.
206  */
207 void drm_atomic_state_clear(struct drm_atomic_state *state)
208 {
209 	struct drm_device *dev = state->dev;
210 	struct drm_mode_config *config = &dev->mode_config;
211 
212 	if (config->funcs->atomic_state_clear)
213 		config->funcs->atomic_state_clear(state);
214 	else
215 		drm_atomic_state_default_clear(state);
216 }
217 EXPORT_SYMBOL(drm_atomic_state_clear);
218 
219 /**
220  * __drm_atomic_state_free - free all memory for an atomic state
221  * @ref: This atomic state to deallocate
222  *
223  * This frees all memory associated with an atomic state, including all the
224  * per-object state for planes, crtcs and connectors.
225  */
226 void __drm_atomic_state_free(struct kref *ref)
227 {
228 	struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
229 	struct drm_mode_config *config = &state->dev->mode_config;
230 
231 	drm_atomic_state_clear(state);
232 
233 	DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
234 
235 	if (config->funcs->atomic_state_free) {
236 		config->funcs->atomic_state_free(state);
237 	} else {
238 		drm_atomic_state_default_release(state);
239 		kfree(state);
240 	}
241 }
242 EXPORT_SYMBOL(__drm_atomic_state_free);
243 
244 /**
245  * drm_atomic_get_crtc_state - get crtc state
246  * @state: global atomic state object
247  * @crtc: crtc to get state object for
248  *
249  * This function returns the crtc state for the given crtc, allocating it if
250  * needed. It will also grab the relevant crtc lock to make sure that the state
251  * is consistent.
252  *
253  * Returns:
254  *
255  * Either the allocated state or the error code encoded into the pointer. When
256  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
257  * entire atomic sequence must be restarted. All other errors are fatal.
258  */
259 struct drm_crtc_state *
260 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
261 			  struct drm_crtc *crtc)
262 {
263 	int ret, index = drm_crtc_index(crtc);
264 	struct drm_crtc_state *crtc_state;
265 
266 	WARN_ON(!state->acquire_ctx);
267 
268 	crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
269 	if (crtc_state)
270 		return crtc_state;
271 
272 	ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
273 	if (ret)
274 		return ERR_PTR(ret);
275 
276 	crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
277 	if (!crtc_state)
278 		return ERR_PTR(-ENOMEM);
279 
280 	state->crtcs[index].state = crtc_state;
281 	state->crtcs[index].ptr = crtc;
282 	crtc_state->state = state;
283 
284 	DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
285 			 crtc->base.id, crtc->name, crtc_state, state);
286 
287 	return crtc_state;
288 }
289 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
290 
291 /**
292  * drm_atomic_set_mode_for_crtc - set mode for CRTC
293  * @state: the CRTC whose incoming state to update
294  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
295  *
296  * Set a mode (originating from the kernel) on the desired CRTC state. Does
297  * not change any other state properties, including enable, active, or
298  * mode_changed.
299  *
300  * RETURNS:
301  * Zero on success, error code on failure. Cannot return -EDEADLK.
302  */
303 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
304 				 struct drm_display_mode *mode)
305 {
306 	struct drm_mode_modeinfo umode;
307 
308 	/* Early return for no change. */
309 	if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
310 		return 0;
311 
312 	drm_property_unreference_blob(state->mode_blob);
313 	state->mode_blob = NULL;
314 
315 	if (mode) {
316 		drm_mode_convert_to_umode(&umode, mode);
317 		state->mode_blob =
318 			drm_property_create_blob(state->crtc->dev,
319 		                                 sizeof(umode),
320 		                                 &umode);
321 		if (IS_ERR(state->mode_blob))
322 			return PTR_ERR(state->mode_blob);
323 
324 		drm_mode_copy(&state->mode, mode);
325 		state->enable = true;
326 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
327 				 mode->name, state);
328 	} else {
329 		memset(&state->mode, 0, sizeof(state->mode));
330 		state->enable = false;
331 		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
332 				 state);
333 	}
334 
335 	return 0;
336 }
337 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
338 
339 /**
340  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
341  * @state: the CRTC whose incoming state to update
342  * @blob: pointer to blob property to use for mode
343  *
344  * Set a mode (originating from a blob property) on the desired CRTC state.
345  * This function will take a reference on the blob property for the CRTC state,
346  * and release the reference held on the state's existing mode property, if any
347  * was set.
348  *
349  * RETURNS:
350  * Zero on success, error code on failure. Cannot return -EDEADLK.
351  */
352 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
353                                       struct drm_property_blob *blob)
354 {
355 	if (blob == state->mode_blob)
356 		return 0;
357 
358 	drm_property_unreference_blob(state->mode_blob);
359 	state->mode_blob = NULL;
360 
361 	memset(&state->mode, 0, sizeof(state->mode));
362 
363 	if (blob) {
364 		if (blob->length != sizeof(struct drm_mode_modeinfo) ||
365 		    drm_mode_convert_umode(&state->mode,
366 		                           (const struct drm_mode_modeinfo *)
367 		                            blob->data))
368 			return -EINVAL;
369 
370 		state->mode_blob = drm_property_reference_blob(blob);
371 		state->enable = true;
372 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
373 				 state->mode.name, state);
374 	} else {
375 		state->enable = false;
376 		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
377 				 state);
378 	}
379 
380 	return 0;
381 }
382 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
383 
384 /**
385  * drm_atomic_replace_property_blob - replace a blob property
386  * @blob: a pointer to the member blob to be replaced
387  * @new_blob: the new blob to replace with
388  * @replaced: whether the blob has been replaced
389  *
390  * RETURNS:
391  * Zero on success, error code on failure
392  */
393 static void
394 drm_atomic_replace_property_blob(struct drm_property_blob **blob,
395 				 struct drm_property_blob *new_blob,
396 				 bool *replaced)
397 {
398 	struct drm_property_blob *old_blob = *blob;
399 
400 	if (old_blob == new_blob)
401 		return;
402 
403 	drm_property_unreference_blob(old_blob);
404 	if (new_blob)
405 		drm_property_reference_blob(new_blob);
406 	*blob = new_blob;
407 	*replaced = true;
408 
409 	return;
410 }
411 
412 static int
413 drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
414 					 struct drm_property_blob **blob,
415 					 uint64_t blob_id,
416 					 ssize_t expected_size,
417 					 bool *replaced)
418 {
419 	struct drm_property_blob *new_blob = NULL;
420 
421 	if (blob_id != 0) {
422 		new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
423 		if (new_blob == NULL)
424 			return -EINVAL;
425 
426 		if (expected_size > 0 && expected_size != new_blob->length) {
427 			drm_property_unreference_blob(new_blob);
428 			return -EINVAL;
429 		}
430 	}
431 
432 	drm_atomic_replace_property_blob(blob, new_blob, replaced);
433 	drm_property_unreference_blob(new_blob);
434 
435 	return 0;
436 }
437 
438 /**
439  * drm_atomic_crtc_set_property - set property on CRTC
440  * @crtc: the drm CRTC to set a property on
441  * @state: the state object to update with the new property value
442  * @property: the property to set
443  * @val: the new property value
444  *
445  * Use this instead of calling crtc->atomic_set_property directly.
446  * This function handles generic/core properties and calls out to
447  * driver's ->atomic_set_property() for driver properties.  To ensure
448  * consistent behavior you must call this function rather than the
449  * driver hook directly.
450  *
451  * RETURNS:
452  * Zero on success, error code on failure
453  */
454 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
455 		struct drm_crtc_state *state, struct drm_property *property,
456 		uint64_t val)
457 {
458 	struct drm_device *dev = crtc->dev;
459 	struct drm_mode_config *config = &dev->mode_config;
460 	bool replaced = false;
461 	int ret;
462 
463 	if (property == config->prop_active)
464 		state->active = val;
465 	else if (property == config->prop_mode_id) {
466 		struct drm_property_blob *mode =
467 			drm_property_lookup_blob(dev, val);
468 		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
469 		drm_property_unreference_blob(mode);
470 		return ret;
471 	} else if (property == config->degamma_lut_property) {
472 		ret = drm_atomic_replace_property_blob_from_id(crtc,
473 					&state->degamma_lut,
474 					val,
475 					-1,
476 					&replaced);
477 		state->color_mgmt_changed |= replaced;
478 		return ret;
479 	} else if (property == config->ctm_property) {
480 		ret = drm_atomic_replace_property_blob_from_id(crtc,
481 					&state->ctm,
482 					val,
483 					sizeof(struct drm_color_ctm),
484 					&replaced);
485 		state->color_mgmt_changed |= replaced;
486 		return ret;
487 	} else if (property == config->gamma_lut_property) {
488 		ret = drm_atomic_replace_property_blob_from_id(crtc,
489 					&state->gamma_lut,
490 					val,
491 					-1,
492 					&replaced);
493 		state->color_mgmt_changed |= replaced;
494 		return ret;
495 	} else if (crtc->funcs->atomic_set_property)
496 		return crtc->funcs->atomic_set_property(crtc, state, property, val);
497 	else
498 		return -EINVAL;
499 
500 	return 0;
501 }
502 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
503 
504 /**
505  * drm_atomic_crtc_get_property - get property value from CRTC state
506  * @crtc: the drm CRTC to set a property on
507  * @state: the state object to get the property value from
508  * @property: the property to set
509  * @val: return location for the property value
510  *
511  * This function handles generic/core properties and calls out to
512  * driver's ->atomic_get_property() for driver properties.  To ensure
513  * consistent behavior you must call this function rather than the
514  * driver hook directly.
515  *
516  * RETURNS:
517  * Zero on success, error code on failure
518  */
519 static int
520 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
521 		const struct drm_crtc_state *state,
522 		struct drm_property *property, uint64_t *val)
523 {
524 	struct drm_device *dev = crtc->dev;
525 	struct drm_mode_config *config = &dev->mode_config;
526 
527 	if (property == config->prop_active)
528 		*val = state->active;
529 	else if (property == config->prop_mode_id)
530 		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
531 	else if (property == config->degamma_lut_property)
532 		*val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
533 	else if (property == config->ctm_property)
534 		*val = (state->ctm) ? state->ctm->base.id : 0;
535 	else if (property == config->gamma_lut_property)
536 		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
537 	else if (crtc->funcs->atomic_get_property)
538 		return crtc->funcs->atomic_get_property(crtc, state, property, val);
539 	else
540 		return -EINVAL;
541 
542 	return 0;
543 }
544 
545 /**
546  * drm_atomic_crtc_check - check crtc state
547  * @crtc: crtc to check
548  * @state: crtc state to check
549  *
550  * Provides core sanity checks for crtc state.
551  *
552  * RETURNS:
553  * Zero on success, error code on failure
554  */
555 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
556 		struct drm_crtc_state *state)
557 {
558 	/* NOTE: we explicitly don't enforce constraints such as primary
559 	 * layer covering entire screen, since that is something we want
560 	 * to allow (on hw that supports it).  For hw that does not, it
561 	 * should be checked in driver's crtc->atomic_check() vfunc.
562 	 *
563 	 * TODO: Add generic modeset state checks once we support those.
564 	 */
565 
566 	if (state->active && !state->enable) {
567 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
568 				 crtc->base.id, crtc->name);
569 		return -EINVAL;
570 	}
571 
572 	/* The state->enable vs. state->mode_blob checks can be WARN_ON,
573 	 * as this is a kernel-internal detail that userspace should never
574 	 * be able to trigger. */
575 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
576 	    WARN_ON(state->enable && !state->mode_blob)) {
577 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
578 				 crtc->base.id, crtc->name);
579 		return -EINVAL;
580 	}
581 
582 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
583 	    WARN_ON(!state->enable && state->mode_blob)) {
584 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
585 				 crtc->base.id, crtc->name);
586 		return -EINVAL;
587 	}
588 
589 	/*
590 	 * Reject event generation for when a CRTC is off and stays off.
591 	 * It wouldn't be hard to implement this, but userspace has a track
592 	 * record of happily burning through 100% cpu (or worse, crash) when the
593 	 * display pipe is suspended. To avoid all that fun just reject updates
594 	 * that ask for events since likely that indicates a bug in the
595 	 * compositor's drawing loop. This is consistent with the vblank IOCTL
596 	 * and legacy page_flip IOCTL which also reject service on a disabled
597 	 * pipe.
598 	 */
599 	if (state->event && !state->active && !crtc->state->active) {
600 		DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
601 				 crtc->base.id);
602 		return -EINVAL;
603 	}
604 
605 	return 0;
606 }
607 
608 /**
609  * drm_atomic_get_plane_state - get plane state
610  * @state: global atomic state object
611  * @plane: plane to get state object for
612  *
613  * This function returns the plane state for the given plane, allocating it if
614  * needed. It will also grab the relevant plane lock to make sure that the state
615  * is consistent.
616  *
617  * Returns:
618  *
619  * Either the allocated state or the error code encoded into the pointer. When
620  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
621  * entire atomic sequence must be restarted. All other errors are fatal.
622  */
623 struct drm_plane_state *
624 drm_atomic_get_plane_state(struct drm_atomic_state *state,
625 			  struct drm_plane *plane)
626 {
627 	int ret, index = drm_plane_index(plane);
628 	struct drm_plane_state *plane_state;
629 
630 	WARN_ON(!state->acquire_ctx);
631 
632 	plane_state = drm_atomic_get_existing_plane_state(state, plane);
633 	if (plane_state)
634 		return plane_state;
635 
636 	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
637 	if (ret)
638 		return ERR_PTR(ret);
639 
640 	plane_state = plane->funcs->atomic_duplicate_state(plane);
641 	if (!plane_state)
642 		return ERR_PTR(-ENOMEM);
643 
644 	state->planes[index].state = plane_state;
645 	state->planes[index].ptr = plane;
646 	plane_state->state = state;
647 
648 	DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
649 			 plane->base.id, plane->name, plane_state, state);
650 
651 	if (plane_state->crtc) {
652 		struct drm_crtc_state *crtc_state;
653 
654 		crtc_state = drm_atomic_get_crtc_state(state,
655 						       plane_state->crtc);
656 		if (IS_ERR(crtc_state))
657 			return ERR_CAST(crtc_state);
658 	}
659 
660 	return plane_state;
661 }
662 EXPORT_SYMBOL(drm_atomic_get_plane_state);
663 
664 /**
665  * drm_atomic_plane_set_property - set property on plane
666  * @plane: the drm plane to set a property on
667  * @state: the state object to update with the new property value
668  * @property: the property to set
669  * @val: the new property value
670  *
671  * Use this instead of calling plane->atomic_set_property directly.
672  * This function handles generic/core properties and calls out to
673  * driver's ->atomic_set_property() for driver properties.  To ensure
674  * consistent behavior you must call this function rather than the
675  * driver hook directly.
676  *
677  * RETURNS:
678  * Zero on success, error code on failure
679  */
680 int drm_atomic_plane_set_property(struct drm_plane *plane,
681 		struct drm_plane_state *state, struct drm_property *property,
682 		uint64_t val)
683 {
684 	struct drm_device *dev = plane->dev;
685 	struct drm_mode_config *config = &dev->mode_config;
686 
687 	if (property == config->prop_fb_id) {
688 		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
689 		drm_atomic_set_fb_for_plane(state, fb);
690 		if (fb)
691 			drm_framebuffer_unreference(fb);
692 	} else if (property == config->prop_crtc_id) {
693 		struct drm_crtc *crtc = drm_crtc_find(dev, val);
694 		return drm_atomic_set_crtc_for_plane(state, crtc);
695 	} else if (property == config->prop_crtc_x) {
696 		state->crtc_x = U642I64(val);
697 	} else if (property == config->prop_crtc_y) {
698 		state->crtc_y = U642I64(val);
699 	} else if (property == config->prop_crtc_w) {
700 		state->crtc_w = val;
701 	} else if (property == config->prop_crtc_h) {
702 		state->crtc_h = val;
703 	} else if (property == config->prop_src_x) {
704 		state->src_x = val;
705 	} else if (property == config->prop_src_y) {
706 		state->src_y = val;
707 	} else if (property == config->prop_src_w) {
708 		state->src_w = val;
709 	} else if (property == config->prop_src_h) {
710 		state->src_h = val;
711 	} else if (property == plane->rotation_property) {
712 		if (!is_power_of_2(val & DRM_ROTATE_MASK))
713 			return -EINVAL;
714 		state->rotation = val;
715 	} else if (property == plane->zpos_property) {
716 		state->zpos = val;
717 	} else if (plane->funcs->atomic_set_property) {
718 		return plane->funcs->atomic_set_property(plane, state,
719 				property, val);
720 	} else {
721 		return -EINVAL;
722 	}
723 
724 	return 0;
725 }
726 EXPORT_SYMBOL(drm_atomic_plane_set_property);
727 
728 /**
729  * drm_atomic_plane_get_property - get property value from plane state
730  * @plane: the drm plane to set a property on
731  * @state: the state object to get the property value from
732  * @property: the property to set
733  * @val: return location for the property value
734  *
735  * This function handles generic/core properties and calls out to
736  * driver's ->atomic_get_property() for driver properties.  To ensure
737  * consistent behavior you must call this function rather than the
738  * driver hook directly.
739  *
740  * RETURNS:
741  * Zero on success, error code on failure
742  */
743 static int
744 drm_atomic_plane_get_property(struct drm_plane *plane,
745 		const struct drm_plane_state *state,
746 		struct drm_property *property, uint64_t *val)
747 {
748 	struct drm_device *dev = plane->dev;
749 	struct drm_mode_config *config = &dev->mode_config;
750 
751 	if (property == config->prop_fb_id) {
752 		*val = (state->fb) ? state->fb->base.id : 0;
753 	} else if (property == config->prop_crtc_id) {
754 		*val = (state->crtc) ? state->crtc->base.id : 0;
755 	} else if (property == config->prop_crtc_x) {
756 		*val = I642U64(state->crtc_x);
757 	} else if (property == config->prop_crtc_y) {
758 		*val = I642U64(state->crtc_y);
759 	} else if (property == config->prop_crtc_w) {
760 		*val = state->crtc_w;
761 	} else if (property == config->prop_crtc_h) {
762 		*val = state->crtc_h;
763 	} else if (property == config->prop_src_x) {
764 		*val = state->src_x;
765 	} else if (property == config->prop_src_y) {
766 		*val = state->src_y;
767 	} else if (property == config->prop_src_w) {
768 		*val = state->src_w;
769 	} else if (property == config->prop_src_h) {
770 		*val = state->src_h;
771 	} else if (property == plane->rotation_property) {
772 		*val = state->rotation;
773 	} else if (property == plane->zpos_property) {
774 		*val = state->zpos;
775 	} else if (plane->funcs->atomic_get_property) {
776 		return plane->funcs->atomic_get_property(plane, state, property, val);
777 	} else {
778 		return -EINVAL;
779 	}
780 
781 	return 0;
782 }
783 
784 static bool
785 plane_switching_crtc(struct drm_atomic_state *state,
786 		     struct drm_plane *plane,
787 		     struct drm_plane_state *plane_state)
788 {
789 	if (!plane->state->crtc || !plane_state->crtc)
790 		return false;
791 
792 	if (plane->state->crtc == plane_state->crtc)
793 		return false;
794 
795 	/* This could be refined, but currently there's no helper or driver code
796 	 * to implement direct switching of active planes nor userspace to take
797 	 * advantage of more direct plane switching without the intermediate
798 	 * full OFF state.
799 	 */
800 	return true;
801 }
802 
803 /**
804  * drm_atomic_plane_check - check plane state
805  * @plane: plane to check
806  * @state: plane state to check
807  *
808  * Provides core sanity checks for plane state.
809  *
810  * RETURNS:
811  * Zero on success, error code on failure
812  */
813 static int drm_atomic_plane_check(struct drm_plane *plane,
814 		struct drm_plane_state *state)
815 {
816 	unsigned int fb_width, fb_height;
817 	int ret;
818 
819 	/* either *both* CRTC and FB must be set, or neither */
820 	if (WARN_ON(state->crtc && !state->fb)) {
821 		DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
822 		return -EINVAL;
823 	} else if (WARN_ON(state->fb && !state->crtc)) {
824 		DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
825 		return -EINVAL;
826 	}
827 
828 	/* if disabled, we don't care about the rest of the state: */
829 	if (!state->crtc)
830 		return 0;
831 
832 	/* Check whether this plane is usable on this CRTC */
833 	if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
834 		DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
835 		return -EINVAL;
836 	}
837 
838 	/* Check whether this plane supports the fb pixel format. */
839 	ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
840 	if (ret) {
841 		char *format_name = drm_get_format_name(state->fb->pixel_format);
842 		DRM_DEBUG_ATOMIC("Invalid pixel format %s\n", format_name);
843 		kfree(format_name);
844 		return ret;
845 	}
846 
847 	/* Give drivers some help against integer overflows */
848 	if (state->crtc_w > INT_MAX ||
849 	    state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
850 	    state->crtc_h > INT_MAX ||
851 	    state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
852 		DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
853 				 state->crtc_w, state->crtc_h,
854 				 state->crtc_x, state->crtc_y);
855 		return -ERANGE;
856 	}
857 
858 	fb_width = state->fb->width << 16;
859 	fb_height = state->fb->height << 16;
860 
861 	/* Make sure source coordinates are inside the fb. */
862 	if (state->src_w > fb_width ||
863 	    state->src_x > fb_width - state->src_w ||
864 	    state->src_h > fb_height ||
865 	    state->src_y > fb_height - state->src_h) {
866 		DRM_DEBUG_ATOMIC("Invalid source coordinates "
867 				 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
868 				 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
869 				 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
870 				 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
871 				 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
872 		return -ENOSPC;
873 	}
874 
875 	if (plane_switching_crtc(state->state, plane, state)) {
876 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
877 				 plane->base.id, plane->name);
878 		return -EINVAL;
879 	}
880 
881 	return 0;
882 }
883 
884 /**
885  * drm_atomic_get_connector_state - get connector state
886  * @state: global atomic state object
887  * @connector: connector to get state object for
888  *
889  * This function returns the connector state for the given connector,
890  * allocating it if needed. It will also grab the relevant connector lock to
891  * make sure that the state is consistent.
892  *
893  * Returns:
894  *
895  * Either the allocated state or the error code encoded into the pointer. When
896  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
897  * entire atomic sequence must be restarted. All other errors are fatal.
898  */
899 struct drm_connector_state *
900 drm_atomic_get_connector_state(struct drm_atomic_state *state,
901 			  struct drm_connector *connector)
902 {
903 	int ret, index;
904 	struct drm_mode_config *config = &connector->dev->mode_config;
905 	struct drm_connector_state *connector_state;
906 
907 	WARN_ON(!state->acquire_ctx);
908 
909 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
910 	if (ret)
911 		return ERR_PTR(ret);
912 
913 	index = drm_connector_index(connector);
914 
915 	if (index >= state->num_connector) {
916 		struct __drm_connnectors_state *c;
917 		int alloc = max(index + 1, config->num_connector);
918 
919 		c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
920 		if (!c)
921 			return ERR_PTR(-ENOMEM);
922 
923 		state->connectors = c;
924 		memset(&state->connectors[state->num_connector], 0,
925 		       sizeof(*state->connectors) * (alloc - state->num_connector));
926 
927 		state->num_connector = alloc;
928 	}
929 
930 	if (state->connectors[index].state)
931 		return state->connectors[index].state;
932 
933 	connector_state = connector->funcs->atomic_duplicate_state(connector);
934 	if (!connector_state)
935 		return ERR_PTR(-ENOMEM);
936 
937 	drm_connector_reference(connector);
938 	state->connectors[index].state = connector_state;
939 	state->connectors[index].ptr = connector;
940 	connector_state->state = state;
941 
942 	DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
943 			 connector->base.id, connector_state, state);
944 
945 	if (connector_state->crtc) {
946 		struct drm_crtc_state *crtc_state;
947 
948 		crtc_state = drm_atomic_get_crtc_state(state,
949 						       connector_state->crtc);
950 		if (IS_ERR(crtc_state))
951 			return ERR_CAST(crtc_state);
952 	}
953 
954 	return connector_state;
955 }
956 EXPORT_SYMBOL(drm_atomic_get_connector_state);
957 
958 /**
959  * drm_atomic_connector_set_property - set property on connector.
960  * @connector: the drm connector to set a property on
961  * @state: the state object to update with the new property value
962  * @property: the property to set
963  * @val: the new property value
964  *
965  * Use this instead of calling connector->atomic_set_property directly.
966  * This function handles generic/core properties and calls out to
967  * driver's ->atomic_set_property() for driver properties.  To ensure
968  * consistent behavior you must call this function rather than the
969  * driver hook directly.
970  *
971  * RETURNS:
972  * Zero on success, error code on failure
973  */
974 int drm_atomic_connector_set_property(struct drm_connector *connector,
975 		struct drm_connector_state *state, struct drm_property *property,
976 		uint64_t val)
977 {
978 	struct drm_device *dev = connector->dev;
979 	struct drm_mode_config *config = &dev->mode_config;
980 
981 	if (property == config->prop_crtc_id) {
982 		struct drm_crtc *crtc = drm_crtc_find(dev, val);
983 		return drm_atomic_set_crtc_for_connector(state, crtc);
984 	} else if (property == config->dpms_property) {
985 		/* setting DPMS property requires special handling, which
986 		 * is done in legacy setprop path for us.  Disallow (for
987 		 * now?) atomic writes to DPMS property:
988 		 */
989 		return -EINVAL;
990 	} else if (connector->funcs->atomic_set_property) {
991 		return connector->funcs->atomic_set_property(connector,
992 				state, property, val);
993 	} else {
994 		return -EINVAL;
995 	}
996 }
997 EXPORT_SYMBOL(drm_atomic_connector_set_property);
998 
999 /**
1000  * drm_atomic_connector_get_property - get property value from connector state
1001  * @connector: the drm connector to set a property on
1002  * @state: the state object to get the property value from
1003  * @property: the property to set
1004  * @val: return location for the property value
1005  *
1006  * This function handles generic/core properties and calls out to
1007  * driver's ->atomic_get_property() for driver properties.  To ensure
1008  * consistent behavior you must call this function rather than the
1009  * driver hook directly.
1010  *
1011  * RETURNS:
1012  * Zero on success, error code on failure
1013  */
1014 static int
1015 drm_atomic_connector_get_property(struct drm_connector *connector,
1016 		const struct drm_connector_state *state,
1017 		struct drm_property *property, uint64_t *val)
1018 {
1019 	struct drm_device *dev = connector->dev;
1020 	struct drm_mode_config *config = &dev->mode_config;
1021 
1022 	if (property == config->prop_crtc_id) {
1023 		*val = (state->crtc) ? state->crtc->base.id : 0;
1024 	} else if (property == config->dpms_property) {
1025 		*val = connector->dpms;
1026 	} else if (connector->funcs->atomic_get_property) {
1027 		return connector->funcs->atomic_get_property(connector,
1028 				state, property, val);
1029 	} else {
1030 		return -EINVAL;
1031 	}
1032 
1033 	return 0;
1034 }
1035 
1036 int drm_atomic_get_property(struct drm_mode_object *obj,
1037 		struct drm_property *property, uint64_t *val)
1038 {
1039 	struct drm_device *dev = property->dev;
1040 	int ret;
1041 
1042 	switch (obj->type) {
1043 	case DRM_MODE_OBJECT_CONNECTOR: {
1044 		struct drm_connector *connector = obj_to_connector(obj);
1045 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1046 		ret = drm_atomic_connector_get_property(connector,
1047 				connector->state, property, val);
1048 		break;
1049 	}
1050 	case DRM_MODE_OBJECT_CRTC: {
1051 		struct drm_crtc *crtc = obj_to_crtc(obj);
1052 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1053 		ret = drm_atomic_crtc_get_property(crtc,
1054 				crtc->state, property, val);
1055 		break;
1056 	}
1057 	case DRM_MODE_OBJECT_PLANE: {
1058 		struct drm_plane *plane = obj_to_plane(obj);
1059 		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1060 		ret = drm_atomic_plane_get_property(plane,
1061 				plane->state, property, val);
1062 		break;
1063 	}
1064 	default:
1065 		ret = -EINVAL;
1066 		break;
1067 	}
1068 
1069 	return ret;
1070 }
1071 
1072 /**
1073  * drm_atomic_set_crtc_for_plane - set crtc for plane
1074  * @plane_state: the plane whose incoming state to update
1075  * @crtc: crtc to use for the plane
1076  *
1077  * Changing the assigned crtc for a plane requires us to grab the lock and state
1078  * for the new crtc, as needed. This function takes care of all these details
1079  * besides updating the pointer in the state object itself.
1080  *
1081  * Returns:
1082  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1083  * then the w/w mutex code has detected a deadlock and the entire atomic
1084  * sequence must be restarted. All other errors are fatal.
1085  */
1086 int
1087 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1088 			      struct drm_crtc *crtc)
1089 {
1090 	struct drm_plane *plane = plane_state->plane;
1091 	struct drm_crtc_state *crtc_state;
1092 
1093 	if (plane_state->crtc) {
1094 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1095 						       plane_state->crtc);
1096 		if (WARN_ON(IS_ERR(crtc_state)))
1097 			return PTR_ERR(crtc_state);
1098 
1099 		crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1100 	}
1101 
1102 	plane_state->crtc = crtc;
1103 
1104 	if (crtc) {
1105 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1106 						       crtc);
1107 		if (IS_ERR(crtc_state))
1108 			return PTR_ERR(crtc_state);
1109 		crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1110 	}
1111 
1112 	if (crtc)
1113 		DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1114 				 plane_state, crtc->base.id, crtc->name);
1115 	else
1116 		DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1117 				 plane_state);
1118 
1119 	return 0;
1120 }
1121 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1122 
1123 /**
1124  * drm_atomic_set_fb_for_plane - set framebuffer for plane
1125  * @plane_state: atomic state object for the plane
1126  * @fb: fb to use for the plane
1127  *
1128  * Changing the assigned framebuffer for a plane requires us to grab a reference
1129  * to the new fb and drop the reference to the old fb, if there is one. This
1130  * function takes care of all these details besides updating the pointer in the
1131  * state object itself.
1132  */
1133 void
1134 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1135 			    struct drm_framebuffer *fb)
1136 {
1137 	if (plane_state->fb)
1138 		drm_framebuffer_unreference(plane_state->fb);
1139 	if (fb)
1140 		drm_framebuffer_reference(fb);
1141 	plane_state->fb = fb;
1142 
1143 	if (fb)
1144 		DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1145 				 fb->base.id, plane_state);
1146 	else
1147 		DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1148 				 plane_state);
1149 }
1150 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1151 
1152 /**
1153  * drm_atomic_set_crtc_for_connector - set crtc for connector
1154  * @conn_state: atomic state object for the connector
1155  * @crtc: crtc to use for the connector
1156  *
1157  * Changing the assigned crtc for a connector requires us to grab the lock and
1158  * state for the new crtc, as needed. This function takes care of all these
1159  * details besides updating the pointer in the state object itself.
1160  *
1161  * Returns:
1162  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1163  * then the w/w mutex code has detected a deadlock and the entire atomic
1164  * sequence must be restarted. All other errors are fatal.
1165  */
1166 int
1167 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1168 				  struct drm_crtc *crtc)
1169 {
1170 	struct drm_crtc_state *crtc_state;
1171 
1172 	if (conn_state->crtc == crtc)
1173 		return 0;
1174 
1175 	if (conn_state->crtc) {
1176 		crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1177 								conn_state->crtc);
1178 
1179 		crtc_state->connector_mask &=
1180 			~(1 << drm_connector_index(conn_state->connector));
1181 
1182 		drm_connector_unreference(conn_state->connector);
1183 		conn_state->crtc = NULL;
1184 	}
1185 
1186 	if (crtc) {
1187 		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1188 		if (IS_ERR(crtc_state))
1189 			return PTR_ERR(crtc_state);
1190 
1191 		crtc_state->connector_mask |=
1192 			1 << drm_connector_index(conn_state->connector);
1193 
1194 		drm_connector_reference(conn_state->connector);
1195 		conn_state->crtc = crtc;
1196 
1197 		DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1198 				 conn_state, crtc->base.id, crtc->name);
1199 	} else {
1200 		DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1201 				 conn_state);
1202 	}
1203 
1204 	return 0;
1205 }
1206 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1207 
1208 /**
1209  * drm_atomic_add_affected_connectors - add connectors for crtc
1210  * @state: atomic state
1211  * @crtc: DRM crtc
1212  *
1213  * This function walks the current configuration and adds all connectors
1214  * currently using @crtc to the atomic configuration @state. Note that this
1215  * function must acquire the connection mutex. This can potentially cause
1216  * unneeded seralization if the update is just for the planes on one crtc. Hence
1217  * drivers and helpers should only call this when really needed (e.g. when a
1218  * full modeset needs to happen due to some change).
1219  *
1220  * Returns:
1221  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1222  * then the w/w mutex code has detected a deadlock and the entire atomic
1223  * sequence must be restarted. All other errors are fatal.
1224  */
1225 int
1226 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1227 				   struct drm_crtc *crtc)
1228 {
1229 	struct drm_mode_config *config = &state->dev->mode_config;
1230 	struct drm_connector *connector;
1231 	struct drm_connector_state *conn_state;
1232 	int ret;
1233 
1234 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1235 	if (ret)
1236 		return ret;
1237 
1238 	DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1239 			 crtc->base.id, crtc->name, state);
1240 
1241 	/*
1242 	 * Changed connectors are already in @state, so only need to look at the
1243 	 * current configuration.
1244 	 */
1245 	drm_for_each_connector(connector, state->dev) {
1246 		if (connector->state->crtc != crtc)
1247 			continue;
1248 
1249 		conn_state = drm_atomic_get_connector_state(state, connector);
1250 		if (IS_ERR(conn_state))
1251 			return PTR_ERR(conn_state);
1252 	}
1253 
1254 	return 0;
1255 }
1256 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1257 
1258 /**
1259  * drm_atomic_add_affected_planes - add planes for crtc
1260  * @state: atomic state
1261  * @crtc: DRM crtc
1262  *
1263  * This function walks the current configuration and adds all planes
1264  * currently used by @crtc to the atomic configuration @state. This is useful
1265  * when an atomic commit also needs to check all currently enabled plane on
1266  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1267  * to avoid special code to force-enable all planes.
1268  *
1269  * Since acquiring a plane state will always also acquire the w/w mutex of the
1270  * current CRTC for that plane (if there is any) adding all the plane states for
1271  * a CRTC will not reduce parallism of atomic updates.
1272  *
1273  * Returns:
1274  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1275  * then the w/w mutex code has detected a deadlock and the entire atomic
1276  * sequence must be restarted. All other errors are fatal.
1277  */
1278 int
1279 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1280 			       struct drm_crtc *crtc)
1281 {
1282 	struct drm_plane *plane;
1283 
1284 	WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1285 
1286 	drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1287 		struct drm_plane_state *plane_state =
1288 			drm_atomic_get_plane_state(state, plane);
1289 
1290 		if (IS_ERR(plane_state))
1291 			return PTR_ERR(plane_state);
1292 	}
1293 	return 0;
1294 }
1295 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1296 
1297 /**
1298  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1299  * @state: atomic state
1300  *
1301  * This function should be used by legacy entry points which don't understand
1302  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1303  * the slowpath completed.
1304  */
1305 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1306 {
1307 	struct drm_device *dev = state->dev;
1308 	unsigned crtc_mask = 0;
1309 	struct drm_crtc *crtc;
1310 	int ret;
1311 	bool global = false;
1312 
1313 	drm_for_each_crtc(crtc, dev) {
1314 		if (crtc->acquire_ctx != state->acquire_ctx)
1315 			continue;
1316 
1317 		crtc_mask |= drm_crtc_mask(crtc);
1318 		crtc->acquire_ctx = NULL;
1319 	}
1320 
1321 	if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1322 		global = true;
1323 
1324 		dev->mode_config.acquire_ctx = NULL;
1325 	}
1326 
1327 retry:
1328 	drm_modeset_backoff(state->acquire_ctx);
1329 
1330 	ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
1331 	if (ret)
1332 		goto retry;
1333 
1334 	drm_for_each_crtc(crtc, dev)
1335 		if (drm_crtc_mask(crtc) & crtc_mask)
1336 			crtc->acquire_ctx = state->acquire_ctx;
1337 
1338 	if (global)
1339 		dev->mode_config.acquire_ctx = state->acquire_ctx;
1340 }
1341 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1342 
1343 /**
1344  * drm_atomic_check_only - check whether a given config would work
1345  * @state: atomic configuration to check
1346  *
1347  * Note that this function can return -EDEADLK if the driver needed to acquire
1348  * more locks but encountered a deadlock. The caller must then do the usual w/w
1349  * backoff dance and restart. All other errors are fatal.
1350  *
1351  * Returns:
1352  * 0 on success, negative error code on failure.
1353  */
1354 int drm_atomic_check_only(struct drm_atomic_state *state)
1355 {
1356 	struct drm_device *dev = state->dev;
1357 	struct drm_mode_config *config = &dev->mode_config;
1358 	struct drm_plane *plane;
1359 	struct drm_plane_state *plane_state;
1360 	struct drm_crtc *crtc;
1361 	struct drm_crtc_state *crtc_state;
1362 	int i, ret = 0;
1363 
1364 	DRM_DEBUG_ATOMIC("checking %p\n", state);
1365 
1366 	for_each_plane_in_state(state, plane, plane_state, i) {
1367 		ret = drm_atomic_plane_check(plane, plane_state);
1368 		if (ret) {
1369 			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1370 					 plane->base.id, plane->name);
1371 			return ret;
1372 		}
1373 	}
1374 
1375 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1376 		ret = drm_atomic_crtc_check(crtc, crtc_state);
1377 		if (ret) {
1378 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1379 					 crtc->base.id, crtc->name);
1380 			return ret;
1381 		}
1382 	}
1383 
1384 	if (config->funcs->atomic_check)
1385 		ret = config->funcs->atomic_check(state->dev, state);
1386 
1387 	if (!state->allow_modeset) {
1388 		for_each_crtc_in_state(state, crtc, crtc_state, i) {
1389 			if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1390 				DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1391 						 crtc->base.id, crtc->name);
1392 				return -EINVAL;
1393 			}
1394 		}
1395 	}
1396 
1397 	return ret;
1398 }
1399 EXPORT_SYMBOL(drm_atomic_check_only);
1400 
1401 /**
1402  * drm_atomic_commit - commit configuration atomically
1403  * @state: atomic configuration to check
1404  *
1405  * Note that this function can return -EDEADLK if the driver needed to acquire
1406  * more locks but encountered a deadlock. The caller must then do the usual w/w
1407  * backoff dance and restart. All other errors are fatal.
1408  *
1409  * Also note that on successful execution ownership of @state is transferred
1410  * from the caller of this function to the function itself. The caller must not
1411  * free or in any other way access @state. If the function fails then the caller
1412  * must clean up @state itself.
1413  *
1414  * Returns:
1415  * 0 on success, negative error code on failure.
1416  */
1417 int drm_atomic_commit(struct drm_atomic_state *state)
1418 {
1419 	struct drm_mode_config *config = &state->dev->mode_config;
1420 	int ret;
1421 
1422 	ret = drm_atomic_check_only(state);
1423 	if (ret)
1424 		return ret;
1425 
1426 	DRM_DEBUG_ATOMIC("commiting %p\n", state);
1427 
1428 	return config->funcs->atomic_commit(state->dev, state, false);
1429 }
1430 EXPORT_SYMBOL(drm_atomic_commit);
1431 
1432 /**
1433  * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
1434  * @state: atomic configuration to check
1435  *
1436  * Note that this function can return -EDEADLK if the driver needed to acquire
1437  * more locks but encountered a deadlock. The caller must then do the usual w/w
1438  * backoff dance and restart. All other errors are fatal.
1439  *
1440  * Also note that on successful execution ownership of @state is transferred
1441  * from the caller of this function to the function itself. The caller must not
1442  * free or in any other way access @state. If the function fails then the caller
1443  * must clean up @state itself.
1444  *
1445  * Returns:
1446  * 0 on success, negative error code on failure.
1447  */
1448 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1449 {
1450 	struct drm_mode_config *config = &state->dev->mode_config;
1451 	int ret;
1452 
1453 	ret = drm_atomic_check_only(state);
1454 	if (ret)
1455 		return ret;
1456 
1457 	DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
1458 
1459 	return config->funcs->atomic_commit(state->dev, state, true);
1460 }
1461 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1462 
1463 /*
1464  * The big monstor ioctl
1465  */
1466 
1467 static struct drm_pending_vblank_event *create_vblank_event(
1468 		struct drm_device *dev, struct drm_file *file_priv,
1469 		struct dma_fence *fence, uint64_t user_data)
1470 {
1471 	struct drm_pending_vblank_event *e = NULL;
1472 	int ret;
1473 
1474 	e = kzalloc(sizeof *e, GFP_KERNEL);
1475 	if (!e)
1476 		return NULL;
1477 
1478 	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1479 	e->event.base.length = sizeof(e->event);
1480 	e->event.user_data = user_data;
1481 
1482 	if (file_priv) {
1483 		ret = drm_event_reserve_init(dev, file_priv, &e->base,
1484 					     &e->event.base);
1485 		if (ret) {
1486 			kfree(e);
1487 			return NULL;
1488 		}
1489 	}
1490 
1491 	e->base.fence = fence;
1492 
1493 	return e;
1494 }
1495 
1496 static int atomic_set_prop(struct drm_atomic_state *state,
1497 		struct drm_mode_object *obj, struct drm_property *prop,
1498 		uint64_t prop_value)
1499 {
1500 	struct drm_mode_object *ref;
1501 	int ret;
1502 
1503 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
1504 		return -EINVAL;
1505 
1506 	switch (obj->type) {
1507 	case DRM_MODE_OBJECT_CONNECTOR: {
1508 		struct drm_connector *connector = obj_to_connector(obj);
1509 		struct drm_connector_state *connector_state;
1510 
1511 		connector_state = drm_atomic_get_connector_state(state, connector);
1512 		if (IS_ERR(connector_state)) {
1513 			ret = PTR_ERR(connector_state);
1514 			break;
1515 		}
1516 
1517 		ret = drm_atomic_connector_set_property(connector,
1518 				connector_state, prop, prop_value);
1519 		break;
1520 	}
1521 	case DRM_MODE_OBJECT_CRTC: {
1522 		struct drm_crtc *crtc = obj_to_crtc(obj);
1523 		struct drm_crtc_state *crtc_state;
1524 
1525 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
1526 		if (IS_ERR(crtc_state)) {
1527 			ret = PTR_ERR(crtc_state);
1528 			break;
1529 		}
1530 
1531 		ret = drm_atomic_crtc_set_property(crtc,
1532 				crtc_state, prop, prop_value);
1533 		break;
1534 	}
1535 	case DRM_MODE_OBJECT_PLANE: {
1536 		struct drm_plane *plane = obj_to_plane(obj);
1537 		struct drm_plane_state *plane_state;
1538 
1539 		plane_state = drm_atomic_get_plane_state(state, plane);
1540 		if (IS_ERR(plane_state)) {
1541 			ret = PTR_ERR(plane_state);
1542 			break;
1543 		}
1544 
1545 		ret = drm_atomic_plane_set_property(plane,
1546 				plane_state, prop, prop_value);
1547 		break;
1548 	}
1549 	default:
1550 		ret = -EINVAL;
1551 		break;
1552 	}
1553 
1554 	drm_property_change_valid_put(prop, ref);
1555 	return ret;
1556 }
1557 
1558 /**
1559  * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1560  *
1561  * @dev: drm device to check.
1562  * @plane_mask: plane mask for planes that were updated.
1563  * @ret: return value, can be -EDEADLK for a retry.
1564  *
1565  * Before doing an update plane->old_fb is set to plane->fb,
1566  * but before dropping the locks old_fb needs to be set to NULL
1567  * and plane->fb updated. This is a common operation for each
1568  * atomic update, so this call is split off as a helper.
1569  */
1570 void drm_atomic_clean_old_fb(struct drm_device *dev,
1571 			     unsigned plane_mask,
1572 			     int ret)
1573 {
1574 	struct drm_plane *plane;
1575 
1576 	/* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1577 	 * locks (ie. while it is still safe to deref plane->state).  We
1578 	 * need to do this here because the driver entry points cannot
1579 	 * distinguish between legacy and atomic ioctls.
1580 	 */
1581 	drm_for_each_plane_mask(plane, dev, plane_mask) {
1582 		if (ret == 0) {
1583 			struct drm_framebuffer *new_fb = plane->state->fb;
1584 			if (new_fb)
1585 				drm_framebuffer_reference(new_fb);
1586 			plane->fb = new_fb;
1587 			plane->crtc = plane->state->crtc;
1588 
1589 			if (plane->old_fb)
1590 				drm_framebuffer_unreference(plane->old_fb);
1591 		}
1592 		plane->old_fb = NULL;
1593 	}
1594 }
1595 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1596 
1597 int drm_mode_atomic_ioctl(struct drm_device *dev,
1598 			  void *data, struct drm_file *file_priv)
1599 {
1600 	struct drm_mode_atomic *arg = data;
1601 	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1602 	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1603 	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1604 	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1605 	unsigned int copied_objs, copied_props;
1606 	struct drm_atomic_state *state;
1607 	struct drm_modeset_acquire_ctx ctx;
1608 	struct drm_plane *plane;
1609 	struct drm_crtc *crtc;
1610 	struct drm_crtc_state *crtc_state;
1611 	unsigned plane_mask;
1612 	int ret = 0;
1613 	unsigned int i, j;
1614 
1615 	/* disallow for drivers not supporting atomic: */
1616 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1617 		return -EINVAL;
1618 
1619 	/* disallow for userspace that has not enabled atomic cap (even
1620 	 * though this may be a bit overkill, since legacy userspace
1621 	 * wouldn't know how to call this ioctl)
1622 	 */
1623 	if (!file_priv->atomic)
1624 		return -EINVAL;
1625 
1626 	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1627 		return -EINVAL;
1628 
1629 	if (arg->reserved)
1630 		return -EINVAL;
1631 
1632 	if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1633 			!dev->mode_config.async_page_flip)
1634 		return -EINVAL;
1635 
1636 	/* can't test and expect an event at the same time. */
1637 	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1638 			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1639 		return -EINVAL;
1640 
1641 	drm_modeset_acquire_init(&ctx, 0);
1642 
1643 	state = drm_atomic_state_alloc(dev);
1644 	if (!state)
1645 		return -ENOMEM;
1646 
1647 	state->acquire_ctx = &ctx;
1648 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1649 
1650 retry:
1651 	plane_mask = 0;
1652 	copied_objs = 0;
1653 	copied_props = 0;
1654 
1655 	for (i = 0; i < arg->count_objs; i++) {
1656 		uint32_t obj_id, count_props;
1657 		struct drm_mode_object *obj;
1658 
1659 		if (get_user(obj_id, objs_ptr + copied_objs)) {
1660 			ret = -EFAULT;
1661 			goto out;
1662 		}
1663 
1664 		obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1665 		if (!obj) {
1666 			ret = -ENOENT;
1667 			goto out;
1668 		}
1669 
1670 		if (!obj->properties) {
1671 			drm_mode_object_unreference(obj);
1672 			ret = -ENOENT;
1673 			goto out;
1674 		}
1675 
1676 		if (get_user(count_props, count_props_ptr + copied_objs)) {
1677 			drm_mode_object_unreference(obj);
1678 			ret = -EFAULT;
1679 			goto out;
1680 		}
1681 
1682 		copied_objs++;
1683 
1684 		for (j = 0; j < count_props; j++) {
1685 			uint32_t prop_id;
1686 			uint64_t prop_value;
1687 			struct drm_property *prop;
1688 
1689 			if (get_user(prop_id, props_ptr + copied_props)) {
1690 				drm_mode_object_unreference(obj);
1691 				ret = -EFAULT;
1692 				goto out;
1693 			}
1694 
1695 			prop = drm_mode_obj_find_prop_id(obj, prop_id);
1696 			if (!prop) {
1697 				drm_mode_object_unreference(obj);
1698 				ret = -ENOENT;
1699 				goto out;
1700 			}
1701 
1702 			if (copy_from_user(&prop_value,
1703 					   prop_values_ptr + copied_props,
1704 					   sizeof(prop_value))) {
1705 				drm_mode_object_unreference(obj);
1706 				ret = -EFAULT;
1707 				goto out;
1708 			}
1709 
1710 			ret = atomic_set_prop(state, obj, prop, prop_value);
1711 			if (ret) {
1712 				drm_mode_object_unreference(obj);
1713 				goto out;
1714 			}
1715 
1716 			copied_props++;
1717 		}
1718 
1719 		if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1720 		    !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
1721 			plane = obj_to_plane(obj);
1722 			plane_mask |= (1 << drm_plane_index(plane));
1723 			plane->old_fb = plane->fb;
1724 		}
1725 		drm_mode_object_unreference(obj);
1726 	}
1727 
1728 	if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1729 		for_each_crtc_in_state(state, crtc, crtc_state, i) {
1730 			struct drm_pending_vblank_event *e;
1731 
1732 			e = create_vblank_event(dev, file_priv, NULL,
1733 						arg->user_data);
1734 			if (!e) {
1735 				ret = -ENOMEM;
1736 				goto out;
1737 			}
1738 
1739 			crtc_state->event = e;
1740 		}
1741 	}
1742 
1743 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1744 		/*
1745 		 * Unlike commit, check_only does not clean up state.
1746 		 * Below we call drm_atomic_state_put for it.
1747 		 */
1748 		ret = drm_atomic_check_only(state);
1749 	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1750 		ret = drm_atomic_nonblocking_commit(state);
1751 	} else {
1752 		ret = drm_atomic_commit(state);
1753 	}
1754 
1755 out:
1756 	drm_atomic_clean_old_fb(dev, plane_mask, ret);
1757 
1758 	if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1759 		/*
1760 		 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1761 		 * if they weren't, this code should be called on success
1762 		 * for TEST_ONLY too.
1763 		 */
1764 
1765 		for_each_crtc_in_state(state, crtc, crtc_state, i) {
1766 			if (!crtc_state->event)
1767 				continue;
1768 
1769 			drm_event_cancel_free(dev, &crtc_state->event->base);
1770 		}
1771 	}
1772 
1773 	if (ret == -EDEADLK) {
1774 		drm_atomic_state_clear(state);
1775 		drm_modeset_backoff(&ctx);
1776 		goto retry;
1777 	}
1778 
1779 	drm_atomic_state_put(state);
1780 
1781 	drm_modeset_drop_locks(&ctx);
1782 	drm_modeset_acquire_fini(&ctx);
1783 
1784 	return ret;
1785 }
1786