xref: /dragonfly/sys/dev/drm/drm_atomic.c (revision f2a91d31)
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
444 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
445 		const struct drm_crtc_state *state,
446 		struct drm_property *property, uint64_t *val)
447 {
448 	struct drm_device *dev = crtc->dev;
449 	struct drm_mode_config *config = &dev->mode_config;
450 
451 	if (property == config->prop_active)
452 		*val = state->active;
453 	else if (property == config->prop_mode_id)
454 		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
455 	else if (crtc->funcs->atomic_get_property)
456 		return crtc->funcs->atomic_get_property(crtc, state, property, val);
457 	else
458 		return -EINVAL;
459 
460 	return 0;
461 }
462 
463 /**
464  * drm_atomic_crtc_check - check crtc state
465  * @crtc: crtc to check
466  * @state: crtc state to check
467  *
468  * Provides core sanity checks for crtc state.
469  *
470  * RETURNS:
471  * Zero on success, error code on failure
472  */
473 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
474 		struct drm_crtc_state *state)
475 {
476 	/* NOTE: we explicitly don't enforce constraints such as primary
477 	 * layer covering entire screen, since that is something we want
478 	 * to allow (on hw that supports it).  For hw that does not, it
479 	 * should be checked in driver's crtc->atomic_check() vfunc.
480 	 *
481 	 * TODO: Add generic modeset state checks once we support those.
482 	 */
483 
484 	if (state->active && !state->enable) {
485 		DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
486 				 crtc->base.id);
487 		return -EINVAL;
488 	}
489 
490 	/* The state->enable vs. state->mode_blob checks can be WARN_ON,
491 	 * as this is a kernel-internal detail that userspace should never
492 	 * be able to trigger. */
493 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
494 	    WARN_ON(state->enable && !state->mode_blob)) {
495 		DRM_DEBUG_ATOMIC("[CRTC:%d] enabled without mode blob\n",
496 				 crtc->base.id);
497 		return -EINVAL;
498 	}
499 
500 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
501 	    WARN_ON(!state->enable && state->mode_blob)) {
502 		DRM_DEBUG_ATOMIC("[CRTC:%d] disabled with mode blob\n",
503 				 crtc->base.id);
504 		return -EINVAL;
505 	}
506 
507 	return 0;
508 }
509 
510 /**
511  * drm_atomic_get_plane_state - get plane state
512  * @state: global atomic state object
513  * @plane: plane to get state object for
514  *
515  * This function returns the plane state for the given plane, allocating it if
516  * needed. It will also grab the relevant plane lock to make sure that the state
517  * is consistent.
518  *
519  * Returns:
520  *
521  * Either the allocated state or the error code encoded into the pointer. When
522  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
523  * entire atomic sequence must be restarted. All other errors are fatal.
524  */
525 struct drm_plane_state *
526 drm_atomic_get_plane_state(struct drm_atomic_state *state,
527 			  struct drm_plane *plane)
528 {
529 	int ret, index = drm_plane_index(plane);
530 	struct drm_plane_state *plane_state;
531 
532 	plane_state = drm_atomic_get_existing_plane_state(state, plane);
533 	if (plane_state)
534 		return plane_state;
535 
536 	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
537 	if (ret)
538 		return ERR_PTR(ret);
539 
540 	plane_state = plane->funcs->atomic_duplicate_state(plane);
541 	if (!plane_state)
542 		return ERR_PTR(-ENOMEM);
543 
544 	state->plane_states[index] = plane_state;
545 	state->planes[index] = plane;
546 	plane_state->state = state;
547 
548 	DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
549 			 plane->base.id, plane_state, state);
550 
551 	if (plane_state->crtc) {
552 		struct drm_crtc_state *crtc_state;
553 
554 		crtc_state = drm_atomic_get_crtc_state(state,
555 						       plane_state->crtc);
556 		if (IS_ERR(crtc_state))
557 			return ERR_CAST(crtc_state);
558 	}
559 
560 	return plane_state;
561 }
562 EXPORT_SYMBOL(drm_atomic_get_plane_state);
563 
564 /**
565  * drm_atomic_plane_set_property - set property on plane
566  * @plane: the drm plane to set a property on
567  * @state: the state object to update with the new property value
568  * @property: the property to set
569  * @val: the new property value
570  *
571  * Use this instead of calling plane->atomic_set_property directly.
572  * This function handles generic/core properties and calls out to
573  * driver's ->atomic_set_property() for driver properties.  To ensure
574  * consistent behavior you must call this function rather than the
575  * driver hook directly.
576  *
577  * RETURNS:
578  * Zero on success, error code on failure
579  */
580 int drm_atomic_plane_set_property(struct drm_plane *plane,
581 		struct drm_plane_state *state, struct drm_property *property,
582 		uint64_t val)
583 {
584 	struct drm_device *dev = plane->dev;
585 	struct drm_mode_config *config = &dev->mode_config;
586 
587 	if (property == config->prop_fb_id) {
588 		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
589 		drm_atomic_set_fb_for_plane(state, fb);
590 		if (fb)
591 			drm_framebuffer_unreference(fb);
592 	} else if (property == config->prop_crtc_id) {
593 		struct drm_crtc *crtc = drm_crtc_find(dev, val);
594 		return drm_atomic_set_crtc_for_plane(state, crtc);
595 	} else if (property == config->prop_crtc_x) {
596 		state->crtc_x = U642I64(val);
597 	} else if (property == config->prop_crtc_y) {
598 		state->crtc_y = U642I64(val);
599 	} else if (property == config->prop_crtc_w) {
600 		state->crtc_w = val;
601 	} else if (property == config->prop_crtc_h) {
602 		state->crtc_h = val;
603 	} else if (property == config->prop_src_x) {
604 		state->src_x = val;
605 	} else if (property == config->prop_src_y) {
606 		state->src_y = val;
607 	} else if (property == config->prop_src_w) {
608 		state->src_w = val;
609 	} else if (property == config->prop_src_h) {
610 		state->src_h = val;
611 	} else if (property == config->rotation_property) {
612 		state->rotation = val;
613 	} else if (plane->funcs->atomic_set_property) {
614 		return plane->funcs->atomic_set_property(plane, state,
615 				property, val);
616 	} else {
617 		return -EINVAL;
618 	}
619 
620 	return 0;
621 }
622 EXPORT_SYMBOL(drm_atomic_plane_set_property);
623 
624 /*
625  * This function handles generic/core properties and calls out to
626  * driver's ->atomic_get_property() for driver properties.  To ensure
627  * consistent behavior you must call this function rather than the
628  * driver hook directly.
629  */
630 static int
631 drm_atomic_plane_get_property(struct drm_plane *plane,
632 		const struct drm_plane_state *state,
633 		struct drm_property *property, uint64_t *val)
634 {
635 	struct drm_device *dev = plane->dev;
636 	struct drm_mode_config *config = &dev->mode_config;
637 
638 	if (property == config->prop_fb_id) {
639 		*val = (state->fb) ? state->fb->base.id : 0;
640 	} else if (property == config->prop_crtc_id) {
641 		*val = (state->crtc) ? state->crtc->base.id : 0;
642 	} else if (property == config->prop_crtc_x) {
643 		*val = I642U64(state->crtc_x);
644 	} else if (property == config->prop_crtc_y) {
645 		*val = I642U64(state->crtc_y);
646 	} else if (property == config->prop_crtc_w) {
647 		*val = state->crtc_w;
648 	} else if (property == config->prop_crtc_h) {
649 		*val = state->crtc_h;
650 	} else if (property == config->prop_src_x) {
651 		*val = state->src_x;
652 	} else if (property == config->prop_src_y) {
653 		*val = state->src_y;
654 	} else if (property == config->prop_src_w) {
655 		*val = state->src_w;
656 	} else if (property == config->prop_src_h) {
657 		*val = state->src_h;
658 	} else if (property == config->rotation_property) {
659 		*val = state->rotation;
660 	} else if (plane->funcs->atomic_get_property) {
661 		return plane->funcs->atomic_get_property(plane, state, property, val);
662 	} else {
663 		return -EINVAL;
664 	}
665 
666 	return 0;
667 }
668 
669 static bool
670 plane_switching_crtc(struct drm_atomic_state *state,
671 		     struct drm_plane *plane,
672 		     struct drm_plane_state *plane_state)
673 {
674 	if (!plane->state->crtc || !plane_state->crtc)
675 		return false;
676 
677 	if (plane->state->crtc == plane_state->crtc)
678 		return false;
679 
680 	/* This could be refined, but currently there's no helper or driver code
681 	 * to implement direct switching of active planes nor userspace to take
682 	 * advantage of more direct plane switching without the intermediate
683 	 * full OFF state.
684 	 */
685 	return true;
686 }
687 
688 /**
689  * drm_atomic_plane_check - check plane state
690  * @plane: plane to check
691  * @state: plane state to check
692  *
693  * Provides core sanity checks for plane state.
694  *
695  * RETURNS:
696  * Zero on success, error code on failure
697  */
698 static int drm_atomic_plane_check(struct drm_plane *plane,
699 		struct drm_plane_state *state)
700 {
701 	unsigned int fb_width, fb_height;
702 	int ret;
703 
704 	/* either *both* CRTC and FB must be set, or neither */
705 	if (WARN_ON(state->crtc && !state->fb)) {
706 		DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
707 		return -EINVAL;
708 	} else if (WARN_ON(state->fb && !state->crtc)) {
709 		DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
710 		return -EINVAL;
711 	}
712 
713 	/* if disabled, we don't care about the rest of the state: */
714 	if (!state->crtc)
715 		return 0;
716 
717 	/* Check whether this plane is usable on this CRTC */
718 	if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
719 		DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
720 		return -EINVAL;
721 	}
722 
723 	/* Check whether this plane supports the fb pixel format. */
724 	ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
725 	if (ret) {
726 		DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
727 				 drm_get_format_name(state->fb->pixel_format));
728 		return ret;
729 	}
730 
731 	/* Give drivers some help against integer overflows */
732 	if (state->crtc_w > INT_MAX ||
733 	    state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
734 	    state->crtc_h > INT_MAX ||
735 	    state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
736 		DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
737 				 state->crtc_w, state->crtc_h,
738 				 state->crtc_x, state->crtc_y);
739 		return -ERANGE;
740 	}
741 
742 	fb_width = state->fb->width << 16;
743 	fb_height = state->fb->height << 16;
744 
745 	/* Make sure source coordinates are inside the fb. */
746 	if (state->src_w > fb_width ||
747 	    state->src_x > fb_width - state->src_w ||
748 	    state->src_h > fb_height ||
749 	    state->src_y > fb_height - state->src_h) {
750 		DRM_DEBUG_ATOMIC("Invalid source coordinates "
751 				 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
752 				 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
753 				 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
754 				 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
755 				 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
756 		return -ENOSPC;
757 	}
758 
759 	if (plane_switching_crtc(state->state, plane, state)) {
760 		DRM_DEBUG_ATOMIC("[PLANE:%d] switching CRTC directly\n",
761 				 plane->base.id);
762 		return -EINVAL;
763 	}
764 
765 	return 0;
766 }
767 
768 /**
769  * drm_atomic_get_connector_state - get connector state
770  * @state: global atomic state object
771  * @connector: connector to get state object for
772  *
773  * This function returns the connector state for the given connector,
774  * allocating it if needed. It will also grab the relevant connector lock to
775  * make sure that the state is consistent.
776  *
777  * Returns:
778  *
779  * Either the allocated state or the error code encoded into the pointer. When
780  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
781  * entire atomic sequence must be restarted. All other errors are fatal.
782  */
783 struct drm_connector_state *
784 drm_atomic_get_connector_state(struct drm_atomic_state *state,
785 			  struct drm_connector *connector)
786 {
787 	int ret, index;
788 	struct drm_mode_config *config = &connector->dev->mode_config;
789 	struct drm_connector_state *connector_state;
790 
791 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
792 	if (ret)
793 		return ERR_PTR(ret);
794 
795 	index = drm_connector_index(connector);
796 
797 	/*
798 	 * Construction of atomic state updates can race with a connector
799 	 * hot-add which might overflow. In this case flip the table and just
800 	 * restart the entire ioctl - no one is fast enough to livelock a cpu
801 	 * with physical hotplug events anyway.
802 	 *
803 	 * Note that we only grab the indexes once we have the right lock to
804 	 * prevent hotplug/unplugging of connectors. So removal is no problem,
805 	 * at most the array is a bit too large.
806 	 */
807 	if (index >= state->num_connector) {
808 		DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
809 		return ERR_PTR(-EAGAIN);
810 	}
811 
812 	if (state->connector_states[index])
813 		return state->connector_states[index];
814 
815 	connector_state = connector->funcs->atomic_duplicate_state(connector);
816 	if (!connector_state)
817 		return ERR_PTR(-ENOMEM);
818 
819 	state->connector_states[index] = connector_state;
820 	state->connectors[index] = connector;
821 	connector_state->state = state;
822 
823 	DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
824 			 connector->base.id, connector_state, state);
825 
826 	if (connector_state->crtc) {
827 		struct drm_crtc_state *crtc_state;
828 
829 		crtc_state = drm_atomic_get_crtc_state(state,
830 						       connector_state->crtc);
831 		if (IS_ERR(crtc_state))
832 			return ERR_CAST(crtc_state);
833 	}
834 
835 	return connector_state;
836 }
837 EXPORT_SYMBOL(drm_atomic_get_connector_state);
838 
839 /**
840  * drm_atomic_connector_set_property - set property on connector.
841  * @connector: the drm connector to set a property on
842  * @state: the state object to update with the new property value
843  * @property: the property to set
844  * @val: the new property value
845  *
846  * Use this instead of calling connector->atomic_set_property directly.
847  * This function handles generic/core properties and calls out to
848  * driver's ->atomic_set_property() for driver properties.  To ensure
849  * consistent behavior you must call this function rather than the
850  * driver hook directly.
851  *
852  * RETURNS:
853  * Zero on success, error code on failure
854  */
855 int drm_atomic_connector_set_property(struct drm_connector *connector,
856 		struct drm_connector_state *state, struct drm_property *property,
857 		uint64_t val)
858 {
859 	struct drm_device *dev = connector->dev;
860 	struct drm_mode_config *config = &dev->mode_config;
861 
862 	if (property == config->prop_crtc_id) {
863 		struct drm_crtc *crtc = drm_crtc_find(dev, val);
864 		return drm_atomic_set_crtc_for_connector(state, crtc);
865 	} else if (property == config->dpms_property) {
866 		/* setting DPMS property requires special handling, which
867 		 * is done in legacy setprop path for us.  Disallow (for
868 		 * now?) atomic writes to DPMS property:
869 		 */
870 		return -EINVAL;
871 	} else if (connector->funcs->atomic_set_property) {
872 		return connector->funcs->atomic_set_property(connector,
873 				state, property, val);
874 	} else {
875 		return -EINVAL;
876 	}
877 }
878 EXPORT_SYMBOL(drm_atomic_connector_set_property);
879 
880 /*
881  * This function handles generic/core properties and calls out to
882  * driver's ->atomic_get_property() for driver properties.  To ensure
883  * consistent behavior you must call this function rather than the
884  * driver hook directly.
885  */
886 static int
887 drm_atomic_connector_get_property(struct drm_connector *connector,
888 		const struct drm_connector_state *state,
889 		struct drm_property *property, uint64_t *val)
890 {
891 	struct drm_device *dev = connector->dev;
892 	struct drm_mode_config *config = &dev->mode_config;
893 
894 	if (property == config->prop_crtc_id) {
895 		*val = (state->crtc) ? state->crtc->base.id : 0;
896 	} else if (property == config->dpms_property) {
897 		*val = connector->dpms;
898 	} else if (connector->funcs->atomic_get_property) {
899 		return connector->funcs->atomic_get_property(connector,
900 				state, property, val);
901 	} else {
902 		return -EINVAL;
903 	}
904 
905 	return 0;
906 }
907 
908 int drm_atomic_get_property(struct drm_mode_object *obj,
909 		struct drm_property *property, uint64_t *val)
910 {
911 	struct drm_device *dev = property->dev;
912 	int ret;
913 
914 	switch (obj->type) {
915 	case DRM_MODE_OBJECT_CONNECTOR: {
916 		struct drm_connector *connector = obj_to_connector(obj);
917 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
918 		ret = drm_atomic_connector_get_property(connector,
919 				connector->state, property, val);
920 		break;
921 	}
922 	case DRM_MODE_OBJECT_CRTC: {
923 		struct drm_crtc *crtc = obj_to_crtc(obj);
924 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
925 		ret = drm_atomic_crtc_get_property(crtc,
926 				crtc->state, property, val);
927 		break;
928 	}
929 	case DRM_MODE_OBJECT_PLANE: {
930 		struct drm_plane *plane = obj_to_plane(obj);
931 		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
932 		ret = drm_atomic_plane_get_property(plane,
933 				plane->state, property, val);
934 		break;
935 	}
936 	default:
937 		ret = -EINVAL;
938 		break;
939 	}
940 
941 	return ret;
942 }
943 
944 /**
945  * drm_atomic_set_crtc_for_plane - set crtc for plane
946  * @plane_state: the plane whose incoming state to update
947  * @crtc: crtc to use for the plane
948  *
949  * Changing the assigned crtc for a plane requires us to grab the lock and state
950  * for the new crtc, as needed. This function takes care of all these details
951  * besides updating the pointer in the state object itself.
952  *
953  * Returns:
954  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
955  * then the w/w mutex code has detected a deadlock and the entire atomic
956  * sequence must be restarted. All other errors are fatal.
957  */
958 int
959 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
960 			      struct drm_crtc *crtc)
961 {
962 	struct drm_plane *plane = plane_state->plane;
963 	struct drm_crtc_state *crtc_state;
964 
965 	if (plane_state->crtc) {
966 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
967 						       plane_state->crtc);
968 		if (WARN_ON(IS_ERR(crtc_state)))
969 			return PTR_ERR(crtc_state);
970 
971 		crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
972 	}
973 
974 	plane_state->crtc = crtc;
975 
976 	if (crtc) {
977 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
978 						       crtc);
979 		if (IS_ERR(crtc_state))
980 			return PTR_ERR(crtc_state);
981 		crtc_state->plane_mask |= (1 << drm_plane_index(plane));
982 	}
983 
984 	if (crtc)
985 		DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
986 				 plane_state, crtc->base.id);
987 	else
988 		DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
989 				 plane_state);
990 
991 	return 0;
992 }
993 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
994 
995 /**
996  * drm_atomic_set_fb_for_plane - set framebuffer for plane
997  * @plane_state: atomic state object for the plane
998  * @fb: fb to use for the plane
999  *
1000  * Changing the assigned framebuffer for a plane requires us to grab a reference
1001  * to the new fb and drop the reference to the old fb, if there is one. This
1002  * function takes care of all these details besides updating the pointer in the
1003  * state object itself.
1004  */
1005 void
1006 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1007 			    struct drm_framebuffer *fb)
1008 {
1009 	if (plane_state->fb)
1010 		drm_framebuffer_unreference(plane_state->fb);
1011 	if (fb)
1012 		drm_framebuffer_reference(fb);
1013 	plane_state->fb = fb;
1014 
1015 	if (fb)
1016 		DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1017 				 fb->base.id, plane_state);
1018 	else
1019 		DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1020 				 plane_state);
1021 }
1022 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1023 
1024 /**
1025  * drm_atomic_set_crtc_for_connector - set crtc for connector
1026  * @conn_state: atomic state object for the connector
1027  * @crtc: crtc to use for the connector
1028  *
1029  * Changing the assigned crtc for a connector requires us to grab the lock and
1030  * state for the new crtc, as needed. This function takes care of all these
1031  * details besides updating the pointer in the state object itself.
1032  *
1033  * Returns:
1034  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1035  * then the w/w mutex code has detected a deadlock and the entire atomic
1036  * sequence must be restarted. All other errors are fatal.
1037  */
1038 int
1039 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1040 				  struct drm_crtc *crtc)
1041 {
1042 	struct drm_crtc_state *crtc_state;
1043 
1044 	if (crtc) {
1045 		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1046 		if (IS_ERR(crtc_state))
1047 			return PTR_ERR(crtc_state);
1048 	}
1049 
1050 	conn_state->crtc = crtc;
1051 
1052 	if (crtc)
1053 		DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
1054 				 conn_state, crtc->base.id);
1055 	else
1056 		DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1057 				 conn_state);
1058 
1059 	return 0;
1060 }
1061 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1062 
1063 /**
1064  * drm_atomic_add_affected_connectors - add connectors for crtc
1065  * @state: atomic state
1066  * @crtc: DRM crtc
1067  *
1068  * This function walks the current configuration and adds all connectors
1069  * currently using @crtc to the atomic configuration @state. Note that this
1070  * function must acquire the connection mutex. This can potentially cause
1071  * unneeded seralization if the update is just for the planes on one crtc. Hence
1072  * drivers and helpers should only call this when really needed (e.g. when a
1073  * full modeset needs to happen due to some change).
1074  *
1075  * Returns:
1076  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1077  * then the w/w mutex code has detected a deadlock and the entire atomic
1078  * sequence must be restarted. All other errors are fatal.
1079  */
1080 int
1081 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1082 				   struct drm_crtc *crtc)
1083 {
1084 	struct drm_mode_config *config = &state->dev->mode_config;
1085 	struct drm_connector *connector;
1086 	struct drm_connector_state *conn_state;
1087 	int ret;
1088 
1089 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1090 	if (ret)
1091 		return ret;
1092 
1093 	DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
1094 			 crtc->base.id, state);
1095 
1096 	/*
1097 	 * Changed connectors are already in @state, so only need to look at the
1098 	 * current configuration.
1099 	 */
1100 	drm_for_each_connector(connector, state->dev) {
1101 		if (connector->state->crtc != crtc)
1102 			continue;
1103 
1104 		conn_state = drm_atomic_get_connector_state(state, connector);
1105 		if (IS_ERR(conn_state))
1106 			return PTR_ERR(conn_state);
1107 	}
1108 
1109 	return 0;
1110 }
1111 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1112 
1113 /**
1114  * drm_atomic_add_affected_planes - add planes for crtc
1115  * @state: atomic state
1116  * @crtc: DRM crtc
1117  *
1118  * This function walks the current configuration and adds all planes
1119  * currently used by @crtc to the atomic configuration @state. This is useful
1120  * when an atomic commit also needs to check all currently enabled plane on
1121  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1122  * to avoid special code to force-enable all planes.
1123  *
1124  * Since acquiring a plane state will always also acquire the w/w mutex of the
1125  * current CRTC for that plane (if there is any) adding all the plane states for
1126  * a CRTC will not reduce parallism of atomic updates.
1127  *
1128  * Returns:
1129  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1130  * then the w/w mutex code has detected a deadlock and the entire atomic
1131  * sequence must be restarted. All other errors are fatal.
1132  */
1133 int
1134 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1135 			       struct drm_crtc *crtc)
1136 {
1137 	struct drm_plane *plane;
1138 
1139 	WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1140 
1141 	drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1142 		struct drm_plane_state *plane_state =
1143 			drm_atomic_get_plane_state(state, plane);
1144 
1145 		if (IS_ERR(plane_state))
1146 			return PTR_ERR(plane_state);
1147 	}
1148 	return 0;
1149 }
1150 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1151 
1152 /**
1153  * drm_atomic_connectors_for_crtc - count number of connected outputs
1154  * @state: atomic state
1155  * @crtc: DRM crtc
1156  *
1157  * This function counts all connectors which will be connected to @crtc
1158  * according to @state. Useful to recompute the enable state for @crtc.
1159  */
1160 int
1161 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
1162 			       struct drm_crtc *crtc)
1163 {
1164 	struct drm_connector *connector;
1165 	struct drm_connector_state *conn_state;
1166 
1167 	int i, num_connected_connectors = 0;
1168 
1169 	for_each_connector_in_state(state, connector, conn_state, i) {
1170 		if (conn_state->crtc == crtc)
1171 			num_connected_connectors++;
1172 	}
1173 
1174 	DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
1175 			 state, num_connected_connectors, crtc->base.id);
1176 
1177 	return num_connected_connectors;
1178 }
1179 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
1180 
1181 /**
1182  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1183  * @state: atomic state
1184  *
1185  * This function should be used by legacy entry points which don't understand
1186  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1187  * the slowpath completed.
1188  */
1189 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1190 {
1191 	int ret;
1192 
1193 retry:
1194 	drm_modeset_backoff(state->acquire_ctx);
1195 
1196 	ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1197 			       state->acquire_ctx);
1198 	if (ret)
1199 		goto retry;
1200 	ret = drm_modeset_lock_all_crtcs(state->dev,
1201 					 state->acquire_ctx);
1202 	if (ret)
1203 		goto retry;
1204 }
1205 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1206 
1207 /**
1208  * drm_atomic_check_only - check whether a given config would work
1209  * @state: atomic configuration to check
1210  *
1211  * Note that this function can return -EDEADLK if the driver needed to acquire
1212  * more locks but encountered a deadlock. The caller must then do the usual w/w
1213  * backoff dance and restart. All other errors are fatal.
1214  *
1215  * Returns:
1216  * 0 on success, negative error code on failure.
1217  */
1218 int drm_atomic_check_only(struct drm_atomic_state *state)
1219 {
1220 	struct drm_device *dev = state->dev;
1221 	struct drm_mode_config *config = &dev->mode_config;
1222 	struct drm_plane *plane;
1223 	struct drm_plane_state *plane_state;
1224 	struct drm_crtc *crtc;
1225 	struct drm_crtc_state *crtc_state;
1226 	int i, ret = 0;
1227 
1228 	DRM_DEBUG_ATOMIC("checking %p\n", state);
1229 
1230 	for_each_plane_in_state(state, plane, plane_state, i) {
1231 		ret = drm_atomic_plane_check(plane, plane_state);
1232 		if (ret) {
1233 			DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
1234 					 plane->base.id);
1235 			return ret;
1236 		}
1237 	}
1238 
1239 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1240 		ret = drm_atomic_crtc_check(crtc, crtc_state);
1241 		if (ret) {
1242 			DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1243 					 crtc->base.id);
1244 			return ret;
1245 		}
1246 	}
1247 
1248 	if (config->funcs->atomic_check)
1249 		ret = config->funcs->atomic_check(state->dev, state);
1250 
1251 	if (!state->allow_modeset) {
1252 		for_each_crtc_in_state(state, crtc, crtc_state, i) {
1253 			if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1254 				DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1255 						 crtc->base.id);
1256 				return -EINVAL;
1257 			}
1258 		}
1259 	}
1260 
1261 	return ret;
1262 }
1263 EXPORT_SYMBOL(drm_atomic_check_only);
1264 
1265 /**
1266  * drm_atomic_commit - commit configuration atomically
1267  * @state: atomic configuration to check
1268  *
1269  * Note that this function can return -EDEADLK if the driver needed to acquire
1270  * more locks but encountered a deadlock. The caller must then do the usual w/w
1271  * backoff dance and restart. All other errors are fatal.
1272  *
1273  * Also note that on successful execution ownership of @state is transferred
1274  * from the caller of this function to the function itself. The caller must not
1275  * free or in any other way access @state. If the function fails then the caller
1276  * must clean up @state itself.
1277  *
1278  * Returns:
1279  * 0 on success, negative error code on failure.
1280  */
1281 int drm_atomic_commit(struct drm_atomic_state *state)
1282 {
1283 	struct drm_mode_config *config = &state->dev->mode_config;
1284 	int ret;
1285 
1286 	ret = drm_atomic_check_only(state);
1287 	if (ret)
1288 		return ret;
1289 
1290 	DRM_DEBUG_ATOMIC("commiting %p\n", state);
1291 
1292 	return config->funcs->atomic_commit(state->dev, state, false);
1293 }
1294 EXPORT_SYMBOL(drm_atomic_commit);
1295 
1296 /**
1297  * drm_atomic_async_commit - atomic&async configuration commit
1298  * @state: atomic configuration to check
1299  *
1300  * Note that this function can return -EDEADLK if the driver needed to acquire
1301  * more locks but encountered a deadlock. The caller must then do the usual w/w
1302  * backoff dance and restart. All other errors are fatal.
1303  *
1304  * Also note that on successful execution ownership of @state is transferred
1305  * from the caller of this function to the function itself. The caller must not
1306  * free or in any other way access @state. If the function fails then the caller
1307  * must clean up @state itself.
1308  *
1309  * Returns:
1310  * 0 on success, negative error code on failure.
1311  */
1312 int drm_atomic_async_commit(struct drm_atomic_state *state)
1313 {
1314 	struct drm_mode_config *config = &state->dev->mode_config;
1315 	int ret;
1316 
1317 	ret = drm_atomic_check_only(state);
1318 	if (ret)
1319 		return ret;
1320 
1321 	DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1322 
1323 	return config->funcs->atomic_commit(state->dev, state, true);
1324 }
1325 EXPORT_SYMBOL(drm_atomic_async_commit);
1326 
1327 #ifdef __DragonFly__
1328 /*
1329  * The Linux layer version of kfree() is a macro and can't be called
1330  * directly via a function pointer
1331  */
1332 static void
1333 drm_atomic_event_destroy(struct drm_pending_event *e)
1334 {
1335 	kfree(e);
1336 }
1337 #endif
1338 
1339 /*
1340  * The big monstor ioctl
1341  */
1342 
1343 static struct drm_pending_vblank_event *create_vblank_event(
1344 		struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1345 {
1346 	struct drm_pending_vblank_event *e = NULL;
1347 	unsigned long flags;
1348 
1349 	spin_lock_irqsave(&dev->event_lock, flags);
1350 	if (file_priv->event_space < sizeof e->event) {
1351 		spin_unlock_irqrestore(&dev->event_lock, flags);
1352 		goto out;
1353 	}
1354 	file_priv->event_space -= sizeof e->event;
1355 	spin_unlock_irqrestore(&dev->event_lock, flags);
1356 
1357 	e = kzalloc(sizeof *e, GFP_KERNEL);
1358 	if (e == NULL) {
1359 		spin_lock_irqsave(&dev->event_lock, flags);
1360 		file_priv->event_space += sizeof e->event;
1361 		spin_unlock_irqrestore(&dev->event_lock, flags);
1362 		goto out;
1363 	}
1364 
1365 	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1366 	e->event.base.length = sizeof e->event;
1367 	e->event.user_data = user_data;
1368 	e->base.event = &e->event.base;
1369 	e->base.file_priv = file_priv;
1370 #ifdef __DragonFly__
1371 	e->base.destroy = drm_atomic_event_destroy;
1372 #else
1373 	e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1374 #endif
1375 
1376 out:
1377 	return e;
1378 }
1379 
1380 static void destroy_vblank_event(struct drm_device *dev,
1381 		struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1382 {
1383 	unsigned long flags;
1384 
1385 	spin_lock_irqsave(&dev->event_lock, flags);
1386 	file_priv->event_space += sizeof e->event;
1387 	spin_unlock_irqrestore(&dev->event_lock, flags);
1388 	kfree(e);
1389 }
1390 
1391 static int atomic_set_prop(struct drm_atomic_state *state,
1392 		struct drm_mode_object *obj, struct drm_property *prop,
1393 		uint64_t prop_value)
1394 {
1395 	struct drm_mode_object *ref;
1396 	int ret;
1397 
1398 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
1399 		return -EINVAL;
1400 
1401 	switch (obj->type) {
1402 	case DRM_MODE_OBJECT_CONNECTOR: {
1403 		struct drm_connector *connector = obj_to_connector(obj);
1404 		struct drm_connector_state *connector_state;
1405 
1406 		connector_state = drm_atomic_get_connector_state(state, connector);
1407 		if (IS_ERR(connector_state)) {
1408 			ret = PTR_ERR(connector_state);
1409 			break;
1410 		}
1411 
1412 		ret = drm_atomic_connector_set_property(connector,
1413 				connector_state, prop, prop_value);
1414 		break;
1415 	}
1416 	case DRM_MODE_OBJECT_CRTC: {
1417 		struct drm_crtc *crtc = obj_to_crtc(obj);
1418 		struct drm_crtc_state *crtc_state;
1419 
1420 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
1421 		if (IS_ERR(crtc_state)) {
1422 			ret = PTR_ERR(crtc_state);
1423 			break;
1424 		}
1425 
1426 		ret = drm_atomic_crtc_set_property(crtc,
1427 				crtc_state, prop, prop_value);
1428 		break;
1429 	}
1430 	case DRM_MODE_OBJECT_PLANE: {
1431 		struct drm_plane *plane = obj_to_plane(obj);
1432 		struct drm_plane_state *plane_state;
1433 
1434 		plane_state = drm_atomic_get_plane_state(state, plane);
1435 		if (IS_ERR(plane_state)) {
1436 			ret = PTR_ERR(plane_state);
1437 			break;
1438 		}
1439 
1440 		ret = drm_atomic_plane_set_property(plane,
1441 				plane_state, prop, prop_value);
1442 		break;
1443 	}
1444 	default:
1445 		ret = -EINVAL;
1446 		break;
1447 	}
1448 
1449 	drm_property_change_valid_put(prop, ref);
1450 	return ret;
1451 }
1452 
1453 /**
1454  * drm_atomic_update_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1455  *
1456  * @dev: drm device to check.
1457  * @plane_mask: plane mask for planes that were updated.
1458  * @ret: return value, can be -EDEADLK for a retry.
1459  *
1460  * Before doing an update plane->old_fb is set to plane->fb,
1461  * but before dropping the locks old_fb needs to be set to NULL
1462  * and plane->fb updated. This is a common operation for each
1463  * atomic update, so this call is split off as a helper.
1464  */
1465 void drm_atomic_clean_old_fb(struct drm_device *dev,
1466 			     unsigned plane_mask,
1467 			     int ret)
1468 {
1469 	struct drm_plane *plane;
1470 
1471 	/* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1472 	 * locks (ie. while it is still safe to deref plane->state).  We
1473 	 * need to do this here because the driver entry points cannot
1474 	 * distinguish between legacy and atomic ioctls.
1475 	 */
1476 	drm_for_each_plane_mask(plane, dev, plane_mask) {
1477 		if (ret == 0) {
1478 			struct drm_framebuffer *new_fb = plane->state->fb;
1479 			if (new_fb)
1480 				drm_framebuffer_reference(new_fb);
1481 			plane->fb = new_fb;
1482 			plane->crtc = plane->state->crtc;
1483 
1484 			if (plane->old_fb)
1485 				drm_framebuffer_unreference(plane->old_fb);
1486 		}
1487 		plane->old_fb = NULL;
1488 	}
1489 }
1490 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1491 
1492 int drm_mode_atomic_ioctl(struct drm_device *dev,
1493 			  void *data, struct drm_file *file_priv)
1494 {
1495 	struct drm_mode_atomic *arg = data;
1496 	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1497 	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1498 	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1499 	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1500 	unsigned int copied_objs, copied_props;
1501 	struct drm_atomic_state *state;
1502 	struct drm_modeset_acquire_ctx ctx;
1503 	struct drm_plane *plane;
1504 	struct drm_crtc *crtc;
1505 	struct drm_crtc_state *crtc_state;
1506 	unsigned plane_mask;
1507 	int ret = 0;
1508 	unsigned int i, j;
1509 
1510 	/* disallow for drivers not supporting atomic: */
1511 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1512 		return -EINVAL;
1513 
1514 	/* disallow for userspace that has not enabled atomic cap (even
1515 	 * though this may be a bit overkill, since legacy userspace
1516 	 * wouldn't know how to call this ioctl)
1517 	 */
1518 	if (!file_priv->atomic)
1519 		return -EINVAL;
1520 
1521 	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1522 		return -EINVAL;
1523 
1524 	if (arg->reserved)
1525 		return -EINVAL;
1526 
1527 	if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1528 			!dev->mode_config.async_page_flip)
1529 		return -EINVAL;
1530 
1531 	/* can't test and expect an event at the same time. */
1532 	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1533 			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1534 		return -EINVAL;
1535 
1536 	drm_modeset_acquire_init(&ctx, 0);
1537 
1538 	state = drm_atomic_state_alloc(dev);
1539 	if (!state)
1540 		return -ENOMEM;
1541 
1542 	state->acquire_ctx = &ctx;
1543 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1544 
1545 retry:
1546 	plane_mask = 0;
1547 	copied_objs = 0;
1548 	copied_props = 0;
1549 
1550 	for (i = 0; i < arg->count_objs; i++) {
1551 		uint32_t obj_id, count_props;
1552 		struct drm_mode_object *obj;
1553 
1554 		if (get_user(obj_id, objs_ptr + copied_objs)) {
1555 			ret = -EFAULT;
1556 			goto out;
1557 		}
1558 
1559 		obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1560 		if (!obj || !obj->properties) {
1561 			ret = -ENOENT;
1562 			goto out;
1563 		}
1564 
1565 		if (get_user(count_props, count_props_ptr + copied_objs)) {
1566 			ret = -EFAULT;
1567 			goto out;
1568 		}
1569 
1570 		copied_objs++;
1571 
1572 		for (j = 0; j < count_props; j++) {
1573 			uint32_t prop_id;
1574 			uint64_t prop_value;
1575 			struct drm_property *prop;
1576 
1577 			if (get_user(prop_id, props_ptr + copied_props)) {
1578 				ret = -EFAULT;
1579 				goto out;
1580 			}
1581 
1582 			prop = drm_property_find(dev, prop_id);
1583 			if (!prop) {
1584 				ret = -ENOENT;
1585 				goto out;
1586 			}
1587 
1588 			if (copy_from_user(&prop_value,
1589 					   prop_values_ptr + copied_props,
1590 					   sizeof(prop_value))) {
1591 				ret = -EFAULT;
1592 				goto out;
1593 			}
1594 
1595 			ret = atomic_set_prop(state, obj, prop, prop_value);
1596 			if (ret)
1597 				goto out;
1598 
1599 			copied_props++;
1600 		}
1601 
1602 		if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1603 		    !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
1604 			plane = obj_to_plane(obj);
1605 			plane_mask |= (1 << drm_plane_index(plane));
1606 			plane->old_fb = plane->fb;
1607 		}
1608 	}
1609 
1610 	if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1611 		for_each_crtc_in_state(state, crtc, crtc_state, i) {
1612 			struct drm_pending_vblank_event *e;
1613 
1614 			e = create_vblank_event(dev, file_priv, arg->user_data);
1615 			if (!e) {
1616 				ret = -ENOMEM;
1617 				goto out;
1618 			}
1619 
1620 			crtc_state->event = e;
1621 		}
1622 	}
1623 
1624 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1625 		/*
1626 		 * Unlike commit, check_only does not clean up state.
1627 		 * Below we call drm_atomic_state_free for it.
1628 		 */
1629 		ret = drm_atomic_check_only(state);
1630 	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1631 		ret = drm_atomic_async_commit(state);
1632 	} else {
1633 		ret = drm_atomic_commit(state);
1634 	}
1635 
1636 out:
1637 	drm_atomic_clean_old_fb(dev, plane_mask, ret);
1638 
1639 	if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1640 		/*
1641 		 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1642 		 * if they weren't, this code should be called on success
1643 		 * for TEST_ONLY too.
1644 		 */
1645 
1646 		for_each_crtc_in_state(state, crtc, crtc_state, i) {
1647 			if (!crtc_state->event)
1648 				continue;
1649 
1650 			destroy_vblank_event(dev, file_priv,
1651 					     crtc_state->event);
1652 		}
1653 	}
1654 
1655 	if (ret == -EDEADLK) {
1656 		drm_atomic_state_clear(state);
1657 		drm_modeset_backoff(&ctx);
1658 		goto retry;
1659 	}
1660 
1661 	if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1662 		drm_atomic_state_free(state);
1663 
1664 	drm_modeset_drop_locks(&ctx);
1665 	drm_modeset_acquire_fini(&ctx);
1666 
1667 	return ret;
1668 }
1669