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