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