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