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