• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_mode.h>
32 #include <drm/drm_print.h>
33 #include <drm/drm_writeback.h>
34 #include <linux/sync_file.h>
35 
36 #include "drm_crtc_internal.h"
37 #include "drm_internal.h"
38 
__drm_crtc_commit_free(struct kref * kref)39 void __drm_crtc_commit_free(struct kref *kref)
40 {
41 	struct drm_crtc_commit *commit =
42 		container_of(kref, struct drm_crtc_commit, ref);
43 
44 	kfree(commit);
45 }
46 EXPORT_SYMBOL(__drm_crtc_commit_free);
47 
48 /**
49  * drm_atomic_state_default_release -
50  * release memory initialized by drm_atomic_state_init
51  * @state: atomic state
52  *
53  * Free all the memory allocated by drm_atomic_state_init.
54  * This should only be used by drivers which are still subclassing
55  * &drm_atomic_state and haven't switched to &drm_private_state yet.
56  */
drm_atomic_state_default_release(struct drm_atomic_state * state)57 void drm_atomic_state_default_release(struct drm_atomic_state *state)
58 {
59 	kfree(state->connectors);
60 	kfree(state->crtcs);
61 	kfree(state->planes);
62 	kfree(state->private_objs);
63 }
64 EXPORT_SYMBOL(drm_atomic_state_default_release);
65 
66 /**
67  * drm_atomic_state_init - init new atomic state
68  * @dev: DRM device
69  * @state: atomic state
70  *
71  * Default implementation for filling in a new atomic state.
72  * This should only be used by drivers which are still subclassing
73  * &drm_atomic_state and haven't switched to &drm_private_state yet.
74  */
75 int
drm_atomic_state_init(struct drm_device * dev,struct drm_atomic_state * state)76 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
77 {
78 	kref_init(&state->ref);
79 
80 	/* TODO legacy paths should maybe do a better job about
81 	 * setting this appropriately?
82 	 */
83 	state->allow_modeset = true;
84 
85 	state->crtcs = kcalloc(dev->mode_config.num_crtc,
86 			       sizeof(*state->crtcs), GFP_KERNEL);
87 	if (!state->crtcs)
88 		goto fail;
89 	state->planes = kcalloc(dev->mode_config.num_total_plane,
90 				sizeof(*state->planes), GFP_KERNEL);
91 	if (!state->planes)
92 		goto fail;
93 
94 	state->dev = dev;
95 
96 	DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
97 
98 	return 0;
99 fail:
100 	drm_atomic_state_default_release(state);
101 	return -ENOMEM;
102 }
103 EXPORT_SYMBOL(drm_atomic_state_init);
104 
105 /**
106  * drm_atomic_state_alloc - allocate atomic state
107  * @dev: DRM device
108  *
109  * This allocates an empty atomic state to track updates.
110  */
111 struct drm_atomic_state *
drm_atomic_state_alloc(struct drm_device * dev)112 drm_atomic_state_alloc(struct drm_device *dev)
113 {
114 	struct drm_mode_config *config = &dev->mode_config;
115 
116 	if (!config->funcs->atomic_state_alloc) {
117 		struct drm_atomic_state *state;
118 
119 		state = kzalloc(sizeof(*state), GFP_KERNEL);
120 		if (!state)
121 			return NULL;
122 		if (drm_atomic_state_init(dev, state) < 0) {
123 			kfree(state);
124 			return NULL;
125 		}
126 		return state;
127 	}
128 
129 	return config->funcs->atomic_state_alloc(dev);
130 }
131 EXPORT_SYMBOL(drm_atomic_state_alloc);
132 
133 /**
134  * drm_atomic_state_default_clear - clear base atomic state
135  * @state: atomic state
136  *
137  * Default implementation for clearing atomic state.
138  * This should only be used by drivers which are still subclassing
139  * &drm_atomic_state and haven't switched to &drm_private_state yet.
140  */
drm_atomic_state_default_clear(struct drm_atomic_state * state)141 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
142 {
143 	struct drm_device *dev = state->dev;
144 	struct drm_mode_config *config = &dev->mode_config;
145 	int i;
146 
147 	DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
148 
149 	for (i = 0; i < state->num_connector; i++) {
150 		struct drm_connector *connector = state->connectors[i].ptr;
151 
152 		if (!connector)
153 			continue;
154 
155 		connector->funcs->atomic_destroy_state(connector,
156 						       state->connectors[i].state);
157 		state->connectors[i].ptr = NULL;
158 		state->connectors[i].state = NULL;
159 		state->connectors[i].old_state = NULL;
160 		state->connectors[i].new_state = NULL;
161 		drm_connector_put(connector);
162 	}
163 
164 	for (i = 0; i < config->num_crtc; i++) {
165 		struct drm_crtc *crtc = state->crtcs[i].ptr;
166 
167 		if (!crtc)
168 			continue;
169 
170 		crtc->funcs->atomic_destroy_state(crtc,
171 						  state->crtcs[i].state);
172 
173 		state->crtcs[i].ptr = NULL;
174 		state->crtcs[i].state = NULL;
175 		state->crtcs[i].old_state = NULL;
176 		state->crtcs[i].new_state = NULL;
177 
178 		if (state->crtcs[i].commit) {
179 			drm_crtc_commit_put(state->crtcs[i].commit);
180 			state->crtcs[i].commit = NULL;
181 		}
182 	}
183 
184 	for (i = 0; i < config->num_total_plane; i++) {
185 		struct drm_plane *plane = state->planes[i].ptr;
186 
187 		if (!plane)
188 			continue;
189 
190 		plane->funcs->atomic_destroy_state(plane,
191 						   state->planes[i].state);
192 		state->planes[i].ptr = NULL;
193 		state->planes[i].state = NULL;
194 		state->planes[i].old_state = NULL;
195 		state->planes[i].new_state = NULL;
196 	}
197 
198 	for (i = 0; i < state->num_private_objs; i++) {
199 		struct drm_private_obj *obj = state->private_objs[i].ptr;
200 
201 		obj->funcs->atomic_destroy_state(obj,
202 						 state->private_objs[i].state);
203 		state->private_objs[i].ptr = NULL;
204 		state->private_objs[i].state = NULL;
205 		state->private_objs[i].old_state = NULL;
206 		state->private_objs[i].new_state = NULL;
207 	}
208 	state->num_private_objs = 0;
209 
210 	if (state->fake_commit) {
211 		drm_crtc_commit_put(state->fake_commit);
212 		state->fake_commit = NULL;
213 	}
214 }
215 EXPORT_SYMBOL(drm_atomic_state_default_clear);
216 
217 /**
218  * drm_atomic_state_clear - clear state object
219  * @state: atomic state
220  *
221  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
222  * all locks. So someone else could sneak in and change the current modeset
223  * configuration. Which means that all the state assembled in @state is no
224  * longer an atomic update to the current state, but to some arbitrary earlier
225  * state. Which could break assumptions the driver's
226  * &drm_mode_config_funcs.atomic_check likely relies on.
227  *
228  * Hence we must clear all cached state and completely start over, using this
229  * function.
230  */
drm_atomic_state_clear(struct drm_atomic_state * state)231 void drm_atomic_state_clear(struct drm_atomic_state *state)
232 {
233 	struct drm_device *dev = state->dev;
234 	struct drm_mode_config *config = &dev->mode_config;
235 
236 	if (config->funcs->atomic_state_clear)
237 		config->funcs->atomic_state_clear(state);
238 	else
239 		drm_atomic_state_default_clear(state);
240 }
241 EXPORT_SYMBOL(drm_atomic_state_clear);
242 
243 /**
244  * __drm_atomic_state_free - free all memory for an atomic state
245  * @ref: This atomic state to deallocate
246  *
247  * This frees all memory associated with an atomic state, including all the
248  * per-object state for planes, crtcs and connectors.
249  */
__drm_atomic_state_free(struct kref * ref)250 void __drm_atomic_state_free(struct kref *ref)
251 {
252 	struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
253 	struct drm_mode_config *config = &state->dev->mode_config;
254 
255 	drm_atomic_state_clear(state);
256 
257 	DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
258 
259 	if (config->funcs->atomic_state_free) {
260 		config->funcs->atomic_state_free(state);
261 	} else {
262 		drm_atomic_state_default_release(state);
263 		kfree(state);
264 	}
265 }
266 EXPORT_SYMBOL(__drm_atomic_state_free);
267 
268 /**
269  * drm_atomic_get_crtc_state - get crtc state
270  * @state: global atomic state object
271  * @crtc: crtc to get state object for
272  *
273  * This function returns the crtc state for the given crtc, allocating it if
274  * needed. It will also grab the relevant crtc lock to make sure that the state
275  * is consistent.
276  *
277  * Returns:
278  *
279  * Either the allocated state or the error code encoded into the pointer. When
280  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
281  * entire atomic sequence must be restarted. All other errors are fatal.
282  */
283 struct drm_crtc_state *
drm_atomic_get_crtc_state(struct drm_atomic_state * state,struct drm_crtc * crtc)284 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
285 			  struct drm_crtc *crtc)
286 {
287 	int ret, index = drm_crtc_index(crtc);
288 	struct drm_crtc_state *crtc_state;
289 
290 	WARN_ON(!state->acquire_ctx);
291 
292 	crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
293 	if (crtc_state)
294 		return crtc_state;
295 
296 	ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
297 	if (ret)
298 		return ERR_PTR(ret);
299 
300 	crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
301 	if (!crtc_state)
302 		return ERR_PTR(-ENOMEM);
303 
304 	state->crtcs[index].state = crtc_state;
305 	state->crtcs[index].old_state = crtc->state;
306 	state->crtcs[index].new_state = crtc_state;
307 	state->crtcs[index].ptr = crtc;
308 	crtc_state->state = state;
309 
310 	DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
311 			 crtc->base.id, crtc->name, crtc_state, state);
312 
313 	return crtc_state;
314 }
315 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
316 
set_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc,s32 __user * fence_ptr)317 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
318 				   struct drm_crtc *crtc, s32 __user *fence_ptr)
319 {
320 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
321 }
322 
get_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc)323 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
324 					  struct drm_crtc *crtc)
325 {
326 	s32 __user *fence_ptr;
327 
328 	fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
329 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
330 
331 	return fence_ptr;
332 }
333 
set_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector,s32 __user * fence_ptr)334 static int set_out_fence_for_connector(struct drm_atomic_state *state,
335 					struct drm_connector *connector,
336 					s32 __user *fence_ptr)
337 {
338 	unsigned int index = drm_connector_index(connector);
339 
340 	if (!fence_ptr)
341 		return 0;
342 
343 	if (put_user(-1, fence_ptr))
344 		return -EFAULT;
345 
346 	state->connectors[index].out_fence_ptr = fence_ptr;
347 
348 	return 0;
349 }
350 
get_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector)351 static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
352 					       struct drm_connector *connector)
353 {
354 	unsigned int index = drm_connector_index(connector);
355 	s32 __user *fence_ptr;
356 
357 	fence_ptr = state->connectors[index].out_fence_ptr;
358 	state->connectors[index].out_fence_ptr = NULL;
359 
360 	return fence_ptr;
361 }
362 
363 /**
364  * drm_atomic_set_mode_for_crtc - set mode for CRTC
365  * @state: the CRTC whose incoming state to update
366  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
367  *
368  * Set a mode (originating from the kernel) on the desired CRTC state and update
369  * the enable property.
370  *
371  * RETURNS:
372  * Zero on success, error code on failure. Cannot return -EDEADLK.
373  */
drm_atomic_set_mode_for_crtc(struct drm_crtc_state * state,const struct drm_display_mode * mode)374 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
375 				 const struct drm_display_mode *mode)
376 {
377 	struct drm_crtc *crtc = state->crtc;
378 	struct drm_mode_modeinfo umode;
379 
380 	/* Early return for no change. */
381 	if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
382 		return 0;
383 
384 	drm_property_blob_put(state->mode_blob);
385 	state->mode_blob = NULL;
386 
387 	if (mode) {
388 		drm_mode_convert_to_umode(&umode, mode);
389 		state->mode_blob =
390 			drm_property_create_blob(state->crtc->dev,
391 		                                 sizeof(umode),
392 		                                 &umode);
393 		if (IS_ERR(state->mode_blob))
394 			return PTR_ERR(state->mode_blob);
395 
396 		drm_mode_copy(&state->mode, mode);
397 		state->enable = true;
398 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
399 				 mode->name, crtc->base.id, crtc->name, state);
400 	} else {
401 		memset(&state->mode, 0, sizeof(state->mode));
402 		state->enable = false;
403 		DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
404 				 crtc->base.id, crtc->name, state);
405 	}
406 
407 	return 0;
408 }
409 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
410 
411 /**
412  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
413  * @state: the CRTC whose incoming state to update
414  * @blob: pointer to blob property to use for mode
415  *
416  * Set a mode (originating from a blob property) on the desired CRTC state.
417  * This function will take a reference on the blob property for the CRTC state,
418  * and release the reference held on the state's existing mode property, if any
419  * was set.
420  *
421  * RETURNS:
422  * Zero on success, error code on failure. Cannot return -EDEADLK.
423  */
drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state * state,struct drm_property_blob * blob)424 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
425                                       struct drm_property_blob *blob)
426 {
427 	struct drm_crtc *crtc = state->crtc;
428 
429 	if (blob == state->mode_blob)
430 		return 0;
431 
432 	drm_property_blob_put(state->mode_blob);
433 	state->mode_blob = NULL;
434 
435 	memset(&state->mode, 0, sizeof(state->mode));
436 
437 	if (blob) {
438 		int ret;
439 
440 		if (blob->length != sizeof(struct drm_mode_modeinfo)) {
441 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
442 					 crtc->base.id, crtc->name,
443 					 blob->length);
444 			return -EINVAL;
445 		}
446 
447 		ret = drm_mode_convert_umode(crtc->dev,
448 					     &state->mode, blob->data);
449 		if (ret) {
450 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
451 					 crtc->base.id, crtc->name,
452 					 ret, drm_get_mode_status_name(state->mode.status));
453 			drm_mode_debug_printmodeline(&state->mode);
454 			return -EINVAL;
455 		}
456 
457 		state->mode_blob = drm_property_blob_get(blob);
458 		state->enable = true;
459 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
460 				 state->mode.name, crtc->base.id, crtc->name,
461 				 state);
462 	} else {
463 		state->enable = false;
464 		DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
465 				 crtc->base.id, crtc->name, state);
466 	}
467 
468 	return 0;
469 }
470 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
471 
472 /**
473  * drm_atomic_replace_property_blob_from_id - lookup the new blob and replace the old one with it
474  * @dev: DRM device
475  * @blob: a pointer to the member blob to be replaced
476  * @blob_id: ID of the new blob
477  * @expected_size: total expected size of the blob data (in bytes)
478  * @expected_elem_size: expected element size of the blob data (in bytes)
479  * @replaced: did the blob get replaced?
480  *
481  * Replace @blob with another blob with the ID @blob_id. If @blob_id is zero
482  * @blob becomes NULL.
483  *
484  * If @expected_size is positive the new blob length is expected to be equal
485  * to @expected_size bytes. If @expected_elem_size is positive the new blob
486  * length is expected to be a multiple of @expected_elem_size bytes. Otherwise
487  * an error is returned.
488  *
489  * @replaced will indicate to the caller whether the blob was replaced or not.
490  * If the old and new blobs were in fact the same blob @replaced will be false
491  * otherwise it will be true.
492  *
493  * RETURNS:
494  * Zero on success, error code on failure.
495  */
496 static int
drm_atomic_replace_property_blob_from_id(struct drm_device * dev,struct drm_property_blob ** blob,uint64_t blob_id,ssize_t expected_size,ssize_t expected_elem_size,bool * replaced)497 drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
498 					 struct drm_property_blob **blob,
499 					 uint64_t blob_id,
500 					 ssize_t expected_size,
501 					 ssize_t expected_elem_size,
502 					 bool *replaced)
503 {
504 	struct drm_property_blob *new_blob = NULL;
505 
506 	if (blob_id != 0) {
507 		new_blob = drm_property_lookup_blob(dev, blob_id);
508 		if (new_blob == NULL)
509 			return -EINVAL;
510 
511 		if (expected_size > 0 &&
512 		    new_blob->length != expected_size) {
513 			drm_property_blob_put(new_blob);
514 			return -EINVAL;
515 		}
516 		if (expected_elem_size > 0 &&
517 		    new_blob->length % expected_elem_size != 0) {
518 			drm_property_blob_put(new_blob);
519 			return -EINVAL;
520 		}
521 	}
522 
523 	*replaced |= drm_property_replace_blob(blob, new_blob);
524 	drm_property_blob_put(new_blob);
525 
526 	return 0;
527 }
528 
529 /**
530  * drm_atomic_crtc_set_property - set property on CRTC
531  * @crtc: the drm CRTC to set a property on
532  * @state: the state object to update with the new property value
533  * @property: the property to set
534  * @val: the new property value
535  *
536  * This function handles generic/core properties and calls out to driver's
537  * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
538  * consistent behavior you must call this function rather than the driver hook
539  * directly.
540  *
541  * RETURNS:
542  * Zero on success, error code on failure
543  */
drm_atomic_crtc_set_property(struct drm_crtc * crtc,struct drm_crtc_state * state,struct drm_property * property,uint64_t val)544 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
545 		struct drm_crtc_state *state, struct drm_property *property,
546 		uint64_t val)
547 {
548 	struct drm_device *dev = crtc->dev;
549 	struct drm_mode_config *config = &dev->mode_config;
550 	bool replaced = false;
551 	int ret;
552 
553 	if (property == config->prop_active)
554 		state->active = val;
555 	else if (property == config->prop_mode_id) {
556 		struct drm_property_blob *mode =
557 			drm_property_lookup_blob(dev, val);
558 		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
559 		drm_property_blob_put(mode);
560 		return ret;
561 	} else if (property == config->degamma_lut_property) {
562 		ret = drm_atomic_replace_property_blob_from_id(dev,
563 					&state->degamma_lut,
564 					val,
565 					-1, sizeof(struct drm_color_lut),
566 					&replaced);
567 		state->color_mgmt_changed |= replaced;
568 		return ret;
569 	} else if (property == config->ctm_property) {
570 		ret = drm_atomic_replace_property_blob_from_id(dev,
571 					&state->ctm,
572 					val,
573 					sizeof(struct drm_color_ctm), -1,
574 					&replaced);
575 		state->color_mgmt_changed |= replaced;
576 		return ret;
577 	} else if (property == config->gamma_lut_property) {
578 		ret = drm_atomic_replace_property_blob_from_id(dev,
579 					&state->gamma_lut,
580 					val,
581 					-1, sizeof(struct drm_color_lut),
582 					&replaced);
583 		state->color_mgmt_changed |= replaced;
584 		return ret;
585 	} else if (property == config->prop_out_fence_ptr) {
586 		s32 __user *fence_ptr = u64_to_user_ptr(val);
587 
588 		if (!fence_ptr)
589 			return 0;
590 
591 		if (put_user(-1, fence_ptr))
592 			return -EFAULT;
593 
594 		set_out_fence_for_crtc(state->state, crtc, fence_ptr);
595 	} else if (crtc->funcs->atomic_set_property) {
596 		return crtc->funcs->atomic_set_property(crtc, state, property, val);
597 	} else {
598 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
599 				 crtc->base.id, crtc->name,
600 				 property->base.id, property->name);
601 		return -EINVAL;
602 	}
603 
604 	return 0;
605 }
606 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
607 
608 /**
609  * drm_atomic_crtc_get_property - get property value from CRTC state
610  * @crtc: the drm CRTC to set a property on
611  * @state: the state object to get the property value from
612  * @property: the property to set
613  * @val: return location for the property value
614  *
615  * This function handles generic/core properties and calls out to driver's
616  * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
617  * consistent behavior you must call this function rather than the driver hook
618  * directly.
619  *
620  * RETURNS:
621  * Zero on success, error code on failure
622  */
623 static int
drm_atomic_crtc_get_property(struct drm_crtc * crtc,const struct drm_crtc_state * state,struct drm_property * property,uint64_t * val)624 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
625 		const struct drm_crtc_state *state,
626 		struct drm_property *property, uint64_t *val)
627 {
628 	struct drm_device *dev = crtc->dev;
629 	struct drm_mode_config *config = &dev->mode_config;
630 
631 	if (property == config->prop_active)
632 		*val = state->active;
633 	else if (property == config->prop_mode_id)
634 		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
635 	else if (property == config->degamma_lut_property)
636 		*val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
637 	else if (property == config->ctm_property)
638 		*val = (state->ctm) ? state->ctm->base.id : 0;
639 	else if (property == config->gamma_lut_property)
640 		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
641 	else if (property == config->prop_out_fence_ptr)
642 		*val = 0;
643 	else if (crtc->funcs->atomic_get_property)
644 		return crtc->funcs->atomic_get_property(crtc, state, property, val);
645 	else
646 		return -EINVAL;
647 
648 	return 0;
649 }
650 
651 /**
652  * drm_atomic_crtc_check - check crtc state
653  * @crtc: crtc to check
654  * @state: crtc state to check
655  *
656  * Provides core sanity checks for crtc state.
657  *
658  * RETURNS:
659  * Zero on success, error code on failure
660  */
drm_atomic_crtc_check(struct drm_crtc * crtc,struct drm_crtc_state * state)661 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
662 		struct drm_crtc_state *state)
663 {
664 	/* NOTE: we explicitly don't enforce constraints such as primary
665 	 * layer covering entire screen, since that is something we want
666 	 * to allow (on hw that supports it).  For hw that does not, it
667 	 * should be checked in driver's crtc->atomic_check() vfunc.
668 	 *
669 	 * TODO: Add generic modeset state checks once we support those.
670 	 */
671 
672 	if (state->active && !state->enable) {
673 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
674 				 crtc->base.id, crtc->name);
675 		return -EINVAL;
676 	}
677 
678 	/* The state->enable vs. state->mode_blob checks can be WARN_ON,
679 	 * as this is a kernel-internal detail that userspace should never
680 	 * be able to trigger. */
681 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
682 	    WARN_ON(state->enable && !state->mode_blob)) {
683 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
684 				 crtc->base.id, crtc->name);
685 		return -EINVAL;
686 	}
687 
688 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
689 	    WARN_ON(!state->enable && state->mode_blob)) {
690 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
691 				 crtc->base.id, crtc->name);
692 		return -EINVAL;
693 	}
694 
695 	/*
696 	 * Reject event generation for when a CRTC is off and stays off.
697 	 * It wouldn't be hard to implement this, but userspace has a track
698 	 * record of happily burning through 100% cpu (or worse, crash) when the
699 	 * display pipe is suspended. To avoid all that fun just reject updates
700 	 * that ask for events since likely that indicates a bug in the
701 	 * compositor's drawing loop. This is consistent with the vblank IOCTL
702 	 * and legacy page_flip IOCTL which also reject service on a disabled
703 	 * pipe.
704 	 */
705 	if (state->event && !state->active && !crtc->state->active) {
706 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
707 				 crtc->base.id, crtc->name);
708 		return -EINVAL;
709 	}
710 
711 	return 0;
712 }
713 
drm_atomic_crtc_print_state(struct drm_printer * p,const struct drm_crtc_state * state)714 static void drm_atomic_crtc_print_state(struct drm_printer *p,
715 		const struct drm_crtc_state *state)
716 {
717 	struct drm_crtc *crtc = state->crtc;
718 
719 	drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
720 	drm_printf(p, "\tenable=%d\n", state->enable);
721 	drm_printf(p, "\tactive=%d\n", state->active);
722 	drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
723 	drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
724 	drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
725 	drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
726 	drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
727 	drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
728 	drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
729 	drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
730 	drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
731 
732 	if (crtc->funcs->atomic_print_state)
733 		crtc->funcs->atomic_print_state(p, state);
734 }
735 
736 /**
737  * drm_atomic_connector_check - check connector state
738  * @connector: connector to check
739  * @state: connector state to check
740  *
741  * Provides core sanity checks for connector state.
742  *
743  * RETURNS:
744  * Zero on success, error code on failure
745  */
drm_atomic_connector_check(struct drm_connector * connector,struct drm_connector_state * state)746 static int drm_atomic_connector_check(struct drm_connector *connector,
747 		struct drm_connector_state *state)
748 {
749 	struct drm_crtc_state *crtc_state;
750 	struct drm_writeback_job *writeback_job = state->writeback_job;
751 
752 	if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
753 		return 0;
754 
755 	if (writeback_job->fb && !state->crtc) {
756 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
757 				 connector->base.id, connector->name);
758 		return -EINVAL;
759 	}
760 
761 	if (state->crtc)
762 		crtc_state = drm_atomic_get_existing_crtc_state(state->state,
763 								state->crtc);
764 
765 	if (writeback_job->fb && !crtc_state->active) {
766 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
767 				 connector->base.id, connector->name,
768 				 state->crtc->base.id);
769 		return -EINVAL;
770 	}
771 
772 	if (writeback_job->out_fence && !writeback_job->fb) {
773 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
774 				 connector->base.id, connector->name);
775 		return -EINVAL;
776 	}
777 
778 	return 0;
779 }
780 
781 /**
782  * drm_atomic_get_plane_state - get plane state
783  * @state: global atomic state object
784  * @plane: plane to get state object for
785  *
786  * This function returns the plane state for the given plane, allocating it if
787  * needed. It will also grab the relevant plane lock to make sure that the state
788  * is consistent.
789  *
790  * Returns:
791  *
792  * Either the allocated state or the error code encoded into the pointer. When
793  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
794  * entire atomic sequence must be restarted. All other errors are fatal.
795  */
796 struct drm_plane_state *
drm_atomic_get_plane_state(struct drm_atomic_state * state,struct drm_plane * plane)797 drm_atomic_get_plane_state(struct drm_atomic_state *state,
798 			  struct drm_plane *plane)
799 {
800 	int ret, index = drm_plane_index(plane);
801 	struct drm_plane_state *plane_state;
802 
803 	WARN_ON(!state->acquire_ctx);
804 
805 	/* the legacy pointers should never be set */
806 	WARN_ON(plane->fb);
807 	WARN_ON(plane->old_fb);
808 	WARN_ON(plane->crtc);
809 
810 	plane_state = drm_atomic_get_existing_plane_state(state, plane);
811 	if (plane_state)
812 		return plane_state;
813 
814 	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
815 	if (ret)
816 		return ERR_PTR(ret);
817 
818 	plane_state = plane->funcs->atomic_duplicate_state(plane);
819 	if (!plane_state)
820 		return ERR_PTR(-ENOMEM);
821 
822 	state->planes[index].state = plane_state;
823 	state->planes[index].ptr = plane;
824 	state->planes[index].old_state = plane->state;
825 	state->planes[index].new_state = plane_state;
826 	plane_state->state = state;
827 
828 	DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
829 			 plane->base.id, plane->name, plane_state, state);
830 
831 	if (plane_state->crtc) {
832 		struct drm_crtc_state *crtc_state;
833 
834 		crtc_state = drm_atomic_get_crtc_state(state,
835 						       plane_state->crtc);
836 		if (IS_ERR(crtc_state))
837 			return ERR_CAST(crtc_state);
838 	}
839 
840 	return plane_state;
841 }
842 EXPORT_SYMBOL(drm_atomic_get_plane_state);
843 
844 /**
845  * drm_atomic_plane_set_property - set property on plane
846  * @plane: the drm plane to set a property on
847  * @state: the state object to update with the new property value
848  * @property: the property to set
849  * @val: the new property value
850  *
851  * This function handles generic/core properties and calls out to driver's
852  * &drm_plane_funcs.atomic_set_property for driver properties.  To ensure
853  * consistent behavior you must call this function rather than the driver hook
854  * directly.
855  *
856  * RETURNS:
857  * Zero on success, error code on failure
858  */
drm_atomic_plane_set_property(struct drm_plane * plane,struct drm_plane_state * state,struct drm_property * property,uint64_t val)859 static int drm_atomic_plane_set_property(struct drm_plane *plane,
860 		struct drm_plane_state *state, struct drm_property *property,
861 		uint64_t val)
862 {
863 	struct drm_device *dev = plane->dev;
864 	struct drm_mode_config *config = &dev->mode_config;
865 
866 	if (property == config->prop_fb_id) {
867 		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
868 		drm_atomic_set_fb_for_plane(state, fb);
869 		if (fb)
870 			drm_framebuffer_put(fb);
871 	} else if (property == config->prop_in_fence_fd) {
872 		if (state->fence)
873 			return -EINVAL;
874 
875 		if (U642I64(val) == -1)
876 			return 0;
877 
878 		state->fence = sync_file_get_fence(val);
879 		if (!state->fence)
880 			return -EINVAL;
881 
882 	} else if (property == config->prop_crtc_id) {
883 		struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
884 		return drm_atomic_set_crtc_for_plane(state, crtc);
885 	} else if (property == config->prop_crtc_x) {
886 		state->crtc_x = U642I64(val);
887 	} else if (property == config->prop_crtc_y) {
888 		state->crtc_y = U642I64(val);
889 	} else if (property == config->prop_crtc_w) {
890 		state->crtc_w = val;
891 	} else if (property == config->prop_crtc_h) {
892 		state->crtc_h = val;
893 	} else if (property == config->prop_src_x) {
894 		state->src_x = val;
895 	} else if (property == config->prop_src_y) {
896 		state->src_y = val;
897 	} else if (property == config->prop_src_w) {
898 		state->src_w = val;
899 	} else if (property == config->prop_src_h) {
900 		state->src_h = val;
901 	} else if (property == plane->alpha_property) {
902 		state->alpha = val;
903 	} else if (property == plane->rotation_property) {
904 		if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
905 			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
906 					 plane->base.id, plane->name, val);
907 			return -EINVAL;
908 		}
909 		state->rotation = val;
910 	} else if (property == plane->zpos_property) {
911 		state->zpos = val;
912 	} else if (property == plane->color_encoding_property) {
913 		state->color_encoding = val;
914 	} else if (property == plane->color_range_property) {
915 		state->color_range = val;
916 	} else if (plane->funcs->atomic_set_property) {
917 		return plane->funcs->atomic_set_property(plane, state,
918 				property, val);
919 	} else {
920 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
921 				 plane->base.id, plane->name,
922 				 property->base.id, property->name);
923 		return -EINVAL;
924 	}
925 
926 	return 0;
927 }
928 
929 /**
930  * drm_atomic_plane_get_property - get property value from plane state
931  * @plane: the drm plane to set a property on
932  * @state: the state object to get the property value from
933  * @property: the property to set
934  * @val: return location for the property value
935  *
936  * This function handles generic/core properties and calls out to driver's
937  * &drm_plane_funcs.atomic_get_property for driver properties.  To ensure
938  * consistent behavior you must call this function rather than the driver hook
939  * directly.
940  *
941  * RETURNS:
942  * Zero on success, error code on failure
943  */
944 static int
drm_atomic_plane_get_property(struct drm_plane * plane,const struct drm_plane_state * state,struct drm_property * property,uint64_t * val)945 drm_atomic_plane_get_property(struct drm_plane *plane,
946 		const struct drm_plane_state *state,
947 		struct drm_property *property, uint64_t *val)
948 {
949 	struct drm_device *dev = plane->dev;
950 	struct drm_mode_config *config = &dev->mode_config;
951 
952 	if (property == config->prop_fb_id) {
953 		*val = (state->fb) ? state->fb->base.id : 0;
954 	} else if (property == config->prop_in_fence_fd) {
955 		*val = -1;
956 	} else if (property == config->prop_crtc_id) {
957 		*val = (state->crtc) ? state->crtc->base.id : 0;
958 	} else if (property == config->prop_crtc_x) {
959 		*val = I642U64(state->crtc_x);
960 	} else if (property == config->prop_crtc_y) {
961 		*val = I642U64(state->crtc_y);
962 	} else if (property == config->prop_crtc_w) {
963 		*val = state->crtc_w;
964 	} else if (property == config->prop_crtc_h) {
965 		*val = state->crtc_h;
966 	} else if (property == config->prop_src_x) {
967 		*val = state->src_x;
968 	} else if (property == config->prop_src_y) {
969 		*val = state->src_y;
970 	} else if (property == config->prop_src_w) {
971 		*val = state->src_w;
972 	} else if (property == config->prop_src_h) {
973 		*val = state->src_h;
974 	} else if (property == plane->alpha_property) {
975 		*val = state->alpha;
976 	} else if (property == plane->rotation_property) {
977 		*val = state->rotation;
978 	} else if (property == plane->zpos_property) {
979 		*val = state->zpos;
980 	} else if (property == plane->color_encoding_property) {
981 		*val = state->color_encoding;
982 	} else if (property == plane->color_range_property) {
983 		*val = state->color_range;
984 	} else if (plane->funcs->atomic_get_property) {
985 		return plane->funcs->atomic_get_property(plane, state, property, val);
986 	} else {
987 		return -EINVAL;
988 	}
989 
990 	return 0;
991 }
992 
993 static bool
plane_switching_crtc(struct drm_atomic_state * state,struct drm_plane * plane,struct drm_plane_state * plane_state)994 plane_switching_crtc(struct drm_atomic_state *state,
995 		     struct drm_plane *plane,
996 		     struct drm_plane_state *plane_state)
997 {
998 	if (!plane->state->crtc || !plane_state->crtc)
999 		return false;
1000 
1001 	if (plane->state->crtc == plane_state->crtc)
1002 		return false;
1003 
1004 	/* This could be refined, but currently there's no helper or driver code
1005 	 * to implement direct switching of active planes nor userspace to take
1006 	 * advantage of more direct plane switching without the intermediate
1007 	 * full OFF state.
1008 	 */
1009 	return true;
1010 }
1011 
1012 /**
1013  * drm_atomic_plane_check - check plane state
1014  * @plane: plane to check
1015  * @state: plane state to check
1016  *
1017  * Provides core sanity checks for plane state.
1018  *
1019  * RETURNS:
1020  * Zero on success, error code on failure
1021  */
drm_atomic_plane_check(struct drm_plane * plane,struct drm_plane_state * state)1022 static int drm_atomic_plane_check(struct drm_plane *plane,
1023 		struct drm_plane_state *state)
1024 {
1025 	unsigned int fb_width, fb_height;
1026 	int ret;
1027 
1028 	/* either *both* CRTC and FB must be set, or neither */
1029 	if (state->crtc && !state->fb) {
1030 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
1031 				 plane->base.id, plane->name);
1032 		return -EINVAL;
1033 	} else if (state->fb && !state->crtc) {
1034 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
1035 				 plane->base.id, plane->name);
1036 		return -EINVAL;
1037 	}
1038 
1039 	/* if disabled, we don't care about the rest of the state: */
1040 	if (!state->crtc)
1041 		return 0;
1042 
1043 	/* Check whether this plane is usable on this CRTC */
1044 	if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
1045 		DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
1046 				 state->crtc->base.id, state->crtc->name,
1047 				 plane->base.id, plane->name);
1048 		return -EINVAL;
1049 	}
1050 
1051 	/* Check whether this plane supports the fb pixel format. */
1052 	ret = drm_plane_check_pixel_format(plane, state->fb->format->format,
1053 					   state->fb->modifier);
1054 	if (ret) {
1055 		struct drm_format_name_buf format_name;
1056 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n",
1057 				 plane->base.id, plane->name,
1058 				 drm_get_format_name(state->fb->format->format,
1059 						     &format_name),
1060 				 state->fb->modifier);
1061 		return ret;
1062 	}
1063 
1064 	/* Give drivers some help against integer overflows */
1065 	if (state->crtc_w > INT_MAX ||
1066 	    state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
1067 	    state->crtc_h > INT_MAX ||
1068 	    state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
1069 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
1070 				 plane->base.id, plane->name,
1071 				 state->crtc_w, state->crtc_h,
1072 				 state->crtc_x, state->crtc_y);
1073 		return -ERANGE;
1074 	}
1075 
1076 	fb_width = state->fb->width << 16;
1077 	fb_height = state->fb->height << 16;
1078 
1079 	/* Make sure source coordinates are inside the fb. */
1080 	if (state->src_w > fb_width ||
1081 	    state->src_x > fb_width - state->src_w ||
1082 	    state->src_h > fb_height ||
1083 	    state->src_y > fb_height - state->src_h) {
1084 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
1085 				 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
1086 				 plane->base.id, plane->name,
1087 				 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
1088 				 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
1089 				 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
1090 				 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10,
1091 				 state->fb->width, state->fb->height);
1092 		return -ENOSPC;
1093 	}
1094 
1095 	if (plane_switching_crtc(state->state, plane, state)) {
1096 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
1097 				 plane->base.id, plane->name);
1098 		return -EINVAL;
1099 	}
1100 
1101 	return 0;
1102 }
1103 
drm_atomic_plane_print_state(struct drm_printer * p,const struct drm_plane_state * state)1104 static void drm_atomic_plane_print_state(struct drm_printer *p,
1105 		const struct drm_plane_state *state)
1106 {
1107 	struct drm_plane *plane = state->plane;
1108 	struct drm_rect src  = drm_plane_state_src(state);
1109 	struct drm_rect dest = drm_plane_state_dest(state);
1110 
1111 	drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
1112 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1113 	drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
1114 	if (state->fb)
1115 		drm_framebuffer_print_info(p, 2, state->fb);
1116 	drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
1117 	drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
1118 	drm_printf(p, "\trotation=%x\n", state->rotation);
1119 	drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
1120 	drm_printf(p, "\tcolor-encoding=%s\n",
1121 		   drm_get_color_encoding_name(state->color_encoding));
1122 	drm_printf(p, "\tcolor-range=%s\n",
1123 		   drm_get_color_range_name(state->color_range));
1124 
1125 	if (plane->funcs->atomic_print_state)
1126 		plane->funcs->atomic_print_state(p, state);
1127 }
1128 
1129 /**
1130  * DOC: handling driver private state
1131  *
1132  * Very often the DRM objects exposed to userspace in the atomic modeset api
1133  * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
1134  * underlying hardware. Especially for any kind of shared resources (e.g. shared
1135  * clocks, scaler units, bandwidth and fifo limits shared among a group of
1136  * planes or CRTCs, and so on) it makes sense to model these as independent
1137  * objects. Drivers then need to do similar state tracking and commit ordering for
1138  * such private (since not exposed to userpace) objects as the atomic core and
1139  * helpers already provide for connectors, planes and CRTCs.
1140  *
1141  * To make this easier on drivers the atomic core provides some support to track
1142  * driver private state objects using struct &drm_private_obj, with the
1143  * associated state struct &drm_private_state.
1144  *
1145  * Similar to userspace-exposed objects, private state structures can be
1146  * acquired by calling drm_atomic_get_private_obj_state(). Since this function
1147  * does not take care of locking, drivers should wrap it for each type of
1148  * private state object they have with the required call to drm_modeset_lock()
1149  * for the corresponding &drm_modeset_lock.
1150  *
1151  * All private state structures contained in a &drm_atomic_state update can be
1152  * iterated using for_each_oldnew_private_obj_in_state(),
1153  * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
1154  * Drivers are recommended to wrap these for each type of driver private state
1155  * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
1156  * least if they want to iterate over all objects of a given type.
1157  *
1158  * An earlier way to handle driver private state was by subclassing struct
1159  * &drm_atomic_state. But since that encourages non-standard ways to implement
1160  * the check/commit split atomic requires (by using e.g. "check and rollback or
1161  * commit instead" of "duplicate state, check, then either commit or release
1162  * duplicated state) it is deprecated in favour of using &drm_private_state.
1163  */
1164 
1165 /**
1166  * drm_atomic_private_obj_init - initialize private object
1167  * @obj: private object
1168  * @state: initial private object state
1169  * @funcs: pointer to the struct of function pointers that identify the object
1170  * type
1171  *
1172  * Initialize the private object, which can be embedded into any
1173  * driver private object that needs its own atomic state.
1174  */
1175 void
drm_atomic_private_obj_init(struct drm_private_obj * obj,struct drm_private_state * state,const struct drm_private_state_funcs * funcs)1176 drm_atomic_private_obj_init(struct drm_private_obj *obj,
1177 			    struct drm_private_state *state,
1178 			    const struct drm_private_state_funcs *funcs)
1179 {
1180 	memset(obj, 0, sizeof(*obj));
1181 
1182 	obj->state = state;
1183 	obj->funcs = funcs;
1184 }
1185 EXPORT_SYMBOL(drm_atomic_private_obj_init);
1186 
1187 /**
1188  * drm_atomic_private_obj_fini - finalize private object
1189  * @obj: private object
1190  *
1191  * Finalize the private object.
1192  */
1193 void
drm_atomic_private_obj_fini(struct drm_private_obj * obj)1194 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
1195 {
1196 	obj->funcs->atomic_destroy_state(obj, obj->state);
1197 }
1198 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
1199 
1200 /**
1201  * drm_atomic_get_private_obj_state - get private object state
1202  * @state: global atomic state
1203  * @obj: private object to get the state for
1204  *
1205  * This function returns the private object state for the given private object,
1206  * allocating the state if needed. It does not grab any locks as the caller is
1207  * expected to care of any required locking.
1208  *
1209  * RETURNS:
1210  *
1211  * Either the allocated state or the error code encoded into a pointer.
1212  */
1213 struct drm_private_state *
drm_atomic_get_private_obj_state(struct drm_atomic_state * state,struct drm_private_obj * obj)1214 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
1215 				 struct drm_private_obj *obj)
1216 {
1217 	int index, num_objs, i;
1218 	size_t size;
1219 	struct __drm_private_objs_state *arr;
1220 	struct drm_private_state *obj_state;
1221 
1222 	for (i = 0; i < state->num_private_objs; i++)
1223 		if (obj == state->private_objs[i].ptr)
1224 			return state->private_objs[i].state;
1225 
1226 	num_objs = state->num_private_objs + 1;
1227 	size = sizeof(*state->private_objs) * num_objs;
1228 	arr = krealloc(state->private_objs, size, GFP_KERNEL);
1229 	if (!arr)
1230 		return ERR_PTR(-ENOMEM);
1231 
1232 	state->private_objs = arr;
1233 	index = state->num_private_objs;
1234 	memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1235 
1236 	obj_state = obj->funcs->atomic_duplicate_state(obj);
1237 	if (!obj_state)
1238 		return ERR_PTR(-ENOMEM);
1239 
1240 	state->private_objs[index].state = obj_state;
1241 	state->private_objs[index].old_state = obj->state;
1242 	state->private_objs[index].new_state = obj_state;
1243 	state->private_objs[index].ptr = obj;
1244 	obj_state->state = state;
1245 
1246 	state->num_private_objs = num_objs;
1247 
1248 	DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
1249 			 obj, obj_state, state);
1250 
1251 	return obj_state;
1252 }
1253 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1254 
1255 /**
1256  * drm_atomic_get_connector_state - get connector state
1257  * @state: global atomic state object
1258  * @connector: connector to get state object for
1259  *
1260  * This function returns the connector state for the given connector,
1261  * allocating it if needed. It will also grab the relevant connector lock to
1262  * make sure that the state is consistent.
1263  *
1264  * Returns:
1265  *
1266  * Either the allocated state or the error code encoded into the pointer. When
1267  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1268  * entire atomic sequence must be restarted. All other errors are fatal.
1269  */
1270 struct drm_connector_state *
drm_atomic_get_connector_state(struct drm_atomic_state * state,struct drm_connector * connector)1271 drm_atomic_get_connector_state(struct drm_atomic_state *state,
1272 			  struct drm_connector *connector)
1273 {
1274 	int ret, index;
1275 	struct drm_mode_config *config = &connector->dev->mode_config;
1276 	struct drm_connector_state *connector_state;
1277 
1278 	WARN_ON(!state->acquire_ctx);
1279 
1280 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1281 	if (ret)
1282 		return ERR_PTR(ret);
1283 
1284 	index = drm_connector_index(connector);
1285 
1286 	if (index >= state->num_connector) {
1287 		struct __drm_connnectors_state *c;
1288 		int alloc = max(index + 1, config->num_connector);
1289 
1290 		c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1291 		if (!c)
1292 			return ERR_PTR(-ENOMEM);
1293 
1294 		state->connectors = c;
1295 		memset(&state->connectors[state->num_connector], 0,
1296 		       sizeof(*state->connectors) * (alloc - state->num_connector));
1297 
1298 		state->num_connector = alloc;
1299 	}
1300 
1301 	if (state->connectors[index].state)
1302 		return state->connectors[index].state;
1303 
1304 	connector_state = connector->funcs->atomic_duplicate_state(connector);
1305 	if (!connector_state)
1306 		return ERR_PTR(-ENOMEM);
1307 
1308 	drm_connector_get(connector);
1309 	state->connectors[index].state = connector_state;
1310 	state->connectors[index].old_state = connector->state;
1311 	state->connectors[index].new_state = connector_state;
1312 	state->connectors[index].ptr = connector;
1313 	connector_state->state = state;
1314 
1315 	DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1316 			 connector->base.id, connector->name,
1317 			 connector_state, state);
1318 
1319 	if (connector_state->crtc) {
1320 		struct drm_crtc_state *crtc_state;
1321 
1322 		crtc_state = drm_atomic_get_crtc_state(state,
1323 						       connector_state->crtc);
1324 		if (IS_ERR(crtc_state))
1325 			return ERR_CAST(crtc_state);
1326 	}
1327 
1328 	return connector_state;
1329 }
1330 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1331 
1332 /**
1333  * drm_atomic_connector_set_property - set property on connector.
1334  * @connector: the drm connector to set a property on
1335  * @state: the state object to update with the new property value
1336  * @property: the property to set
1337  * @val: the new property value
1338  *
1339  * This function handles generic/core properties and calls out to driver's
1340  * &drm_connector_funcs.atomic_set_property for driver properties.  To ensure
1341  * consistent behavior you must call this function rather than the driver hook
1342  * directly.
1343  *
1344  * RETURNS:
1345  * Zero on success, error code on failure
1346  */
drm_atomic_connector_set_property(struct drm_connector * connector,struct drm_connector_state * state,struct drm_property * property,uint64_t val)1347 static int drm_atomic_connector_set_property(struct drm_connector *connector,
1348 		struct drm_connector_state *state, struct drm_property *property,
1349 		uint64_t val)
1350 {
1351 	struct drm_device *dev = connector->dev;
1352 	struct drm_mode_config *config = &dev->mode_config;
1353 
1354 	if (property == config->prop_crtc_id) {
1355 		struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
1356 		return drm_atomic_set_crtc_for_connector(state, crtc);
1357 	} else if (property == config->dpms_property) {
1358 		/* setting DPMS property requires special handling, which
1359 		 * is done in legacy setprop path for us.  Disallow (for
1360 		 * now?) atomic writes to DPMS property:
1361 		 */
1362 		return -EINVAL;
1363 	} else if (property == config->tv_select_subconnector_property) {
1364 		state->tv.subconnector = val;
1365 	} else if (property == config->tv_left_margin_property) {
1366 		state->tv.margins.left = val;
1367 	} else if (property == config->tv_right_margin_property) {
1368 		state->tv.margins.right = val;
1369 	} else if (property == config->tv_top_margin_property) {
1370 		state->tv.margins.top = val;
1371 	} else if (property == config->tv_bottom_margin_property) {
1372 		state->tv.margins.bottom = val;
1373 	} else if (property == config->tv_mode_property) {
1374 		state->tv.mode = val;
1375 	} else if (property == config->tv_brightness_property) {
1376 		state->tv.brightness = val;
1377 	} else if (property == config->tv_contrast_property) {
1378 		state->tv.contrast = val;
1379 	} else if (property == config->tv_flicker_reduction_property) {
1380 		state->tv.flicker_reduction = val;
1381 	} else if (property == config->tv_overscan_property) {
1382 		state->tv.overscan = val;
1383 	} else if (property == config->tv_saturation_property) {
1384 		state->tv.saturation = val;
1385 	} else if (property == config->tv_hue_property) {
1386 		state->tv.hue = val;
1387 	} else if (property == config->link_status_property) {
1388 		/* Never downgrade from GOOD to BAD on userspace's request here,
1389 		 * only hw issues can do that.
1390 		 *
1391 		 * For an atomic property the userspace doesn't need to be able
1392 		 * to understand all the properties, but needs to be able to
1393 		 * restore the state it wants on VT switch. So if the userspace
1394 		 * tries to change the link_status from GOOD to BAD, driver
1395 		 * silently rejects it and returns a 0. This prevents userspace
1396 		 * from accidently breaking  the display when it restores the
1397 		 * state.
1398 		 */
1399 		if (state->link_status != DRM_LINK_STATUS_GOOD)
1400 			state->link_status = val;
1401 	} else if (property == config->aspect_ratio_property) {
1402 		state->picture_aspect_ratio = val;
1403 	} else if (property == config->content_type_property) {
1404 		state->content_type = val;
1405 	} else if (property == connector->scaling_mode_property) {
1406 		state->scaling_mode = val;
1407 	} else if (property == connector->content_protection_property) {
1408 		if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
1409 			DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
1410 			return -EINVAL;
1411 		}
1412 		state->content_protection = val;
1413 	} else if (property == config->writeback_fb_id_property) {
1414 		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
1415 		int ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
1416 		if (fb)
1417 			drm_framebuffer_put(fb);
1418 		return ret;
1419 	} else if (property == config->writeback_out_fence_ptr_property) {
1420 		s32 __user *fence_ptr = u64_to_user_ptr(val);
1421 
1422 		return set_out_fence_for_connector(state->state, connector,
1423 						   fence_ptr);
1424 	} else if (connector->funcs->atomic_set_property) {
1425 		return connector->funcs->atomic_set_property(connector,
1426 				state, property, val);
1427 	} else {
1428 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
1429 				 connector->base.id, connector->name,
1430 				 property->base.id, property->name);
1431 		return -EINVAL;
1432 	}
1433 
1434 	return 0;
1435 }
1436 
drm_atomic_connector_print_state(struct drm_printer * p,const struct drm_connector_state * state)1437 static void drm_atomic_connector_print_state(struct drm_printer *p,
1438 		const struct drm_connector_state *state)
1439 {
1440 	struct drm_connector *connector = state->connector;
1441 
1442 	drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1443 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1444 
1445 	if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1446 		if (state->writeback_job && state->writeback_job->fb)
1447 			drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
1448 
1449 	if (connector->funcs->atomic_print_state)
1450 		connector->funcs->atomic_print_state(p, state);
1451 }
1452 
1453 /**
1454  * drm_atomic_connector_get_property - get property value from connector state
1455  * @connector: the drm connector to set a property on
1456  * @state: the state object to get the property value from
1457  * @property: the property to set
1458  * @val: return location for the property value
1459  *
1460  * This function handles generic/core properties and calls out to driver's
1461  * &drm_connector_funcs.atomic_get_property for driver properties.  To ensure
1462  * consistent behavior you must call this function rather than the driver hook
1463  * directly.
1464  *
1465  * RETURNS:
1466  * Zero on success, error code on failure
1467  */
1468 static int
drm_atomic_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)1469 drm_atomic_connector_get_property(struct drm_connector *connector,
1470 		const struct drm_connector_state *state,
1471 		struct drm_property *property, uint64_t *val)
1472 {
1473 	struct drm_device *dev = connector->dev;
1474 	struct drm_mode_config *config = &dev->mode_config;
1475 
1476 	if (property == config->prop_crtc_id) {
1477 		*val = (state->crtc) ? state->crtc->base.id : 0;
1478 	} else if (property == config->dpms_property) {
1479 		*val = connector->dpms;
1480 	} else if (property == config->tv_select_subconnector_property) {
1481 		*val = state->tv.subconnector;
1482 	} else if (property == config->tv_left_margin_property) {
1483 		*val = state->tv.margins.left;
1484 	} else if (property == config->tv_right_margin_property) {
1485 		*val = state->tv.margins.right;
1486 	} else if (property == config->tv_top_margin_property) {
1487 		*val = state->tv.margins.top;
1488 	} else if (property == config->tv_bottom_margin_property) {
1489 		*val = state->tv.margins.bottom;
1490 	} else if (property == config->tv_mode_property) {
1491 		*val = state->tv.mode;
1492 	} else if (property == config->tv_brightness_property) {
1493 		*val = state->tv.brightness;
1494 	} else if (property == config->tv_contrast_property) {
1495 		*val = state->tv.contrast;
1496 	} else if (property == config->tv_flicker_reduction_property) {
1497 		*val = state->tv.flicker_reduction;
1498 	} else if (property == config->tv_overscan_property) {
1499 		*val = state->tv.overscan;
1500 	} else if (property == config->tv_saturation_property) {
1501 		*val = state->tv.saturation;
1502 	} else if (property == config->tv_hue_property) {
1503 		*val = state->tv.hue;
1504 	} else if (property == config->link_status_property) {
1505 		*val = state->link_status;
1506 	} else if (property == config->aspect_ratio_property) {
1507 		*val = state->picture_aspect_ratio;
1508 	} else if (property == config->content_type_property) {
1509 		*val = state->content_type;
1510 	} else if (property == connector->scaling_mode_property) {
1511 		*val = state->scaling_mode;
1512 	} else if (property == connector->content_protection_property) {
1513 		*val = state->content_protection;
1514 	} else if (property == config->writeback_fb_id_property) {
1515 		/* Writeback framebuffer is one-shot, write and forget */
1516 		*val = 0;
1517 	} else if (property == config->writeback_out_fence_ptr_property) {
1518 		*val = 0;
1519 	} else if (connector->funcs->atomic_get_property) {
1520 		return connector->funcs->atomic_get_property(connector,
1521 				state, property, val);
1522 	} else {
1523 		return -EINVAL;
1524 	}
1525 
1526 	return 0;
1527 }
1528 
drm_atomic_get_property(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)1529 int drm_atomic_get_property(struct drm_mode_object *obj,
1530 		struct drm_property *property, uint64_t *val)
1531 {
1532 	struct drm_device *dev = property->dev;
1533 	int ret;
1534 
1535 	switch (obj->type) {
1536 	case DRM_MODE_OBJECT_CONNECTOR: {
1537 		struct drm_connector *connector = obj_to_connector(obj);
1538 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1539 		ret = drm_atomic_connector_get_property(connector,
1540 				connector->state, property, val);
1541 		break;
1542 	}
1543 	case DRM_MODE_OBJECT_CRTC: {
1544 		struct drm_crtc *crtc = obj_to_crtc(obj);
1545 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1546 		ret = drm_atomic_crtc_get_property(crtc,
1547 				crtc->state, property, val);
1548 		break;
1549 	}
1550 	case DRM_MODE_OBJECT_PLANE: {
1551 		struct drm_plane *plane = obj_to_plane(obj);
1552 		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1553 		ret = drm_atomic_plane_get_property(plane,
1554 				plane->state, property, val);
1555 		break;
1556 	}
1557 	default:
1558 		ret = -EINVAL;
1559 		break;
1560 	}
1561 
1562 	return ret;
1563 }
1564 
1565 /**
1566  * drm_atomic_set_crtc_for_plane - set crtc for plane
1567  * @plane_state: the plane whose incoming state to update
1568  * @crtc: crtc to use for the plane
1569  *
1570  * Changing the assigned crtc for a plane requires us to grab the lock and state
1571  * for the new crtc, as needed. This function takes care of all these details
1572  * besides updating the pointer in the state object itself.
1573  *
1574  * Returns:
1575  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1576  * then the w/w mutex code has detected a deadlock and the entire atomic
1577  * sequence must be restarted. All other errors are fatal.
1578  */
1579 int
drm_atomic_set_crtc_for_plane(struct drm_plane_state * plane_state,struct drm_crtc * crtc)1580 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1581 			      struct drm_crtc *crtc)
1582 {
1583 	struct drm_plane *plane = plane_state->plane;
1584 	struct drm_crtc_state *crtc_state;
1585 	/* Nothing to do for same crtc*/
1586 	if (plane_state->crtc == crtc)
1587 		return 0;
1588 	if (plane_state->crtc) {
1589 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1590 						       plane_state->crtc);
1591 		if (WARN_ON(IS_ERR(crtc_state)))
1592 			return PTR_ERR(crtc_state);
1593 
1594 		crtc_state->plane_mask &= ~drm_plane_mask(plane);
1595 	}
1596 
1597 	plane_state->crtc = crtc;
1598 
1599 	if (crtc) {
1600 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1601 						       crtc);
1602 		if (IS_ERR(crtc_state))
1603 			return PTR_ERR(crtc_state);
1604 		crtc_state->plane_mask |= drm_plane_mask(plane);
1605 	}
1606 
1607 	if (crtc)
1608 		DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
1609 				 plane->base.id, plane->name, plane_state,
1610 				 crtc->base.id, crtc->name);
1611 	else
1612 		DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
1613 				 plane->base.id, plane->name, plane_state);
1614 
1615 	return 0;
1616 }
1617 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1618 
1619 /**
1620  * drm_atomic_set_fb_for_plane - set framebuffer for plane
1621  * @plane_state: atomic state object for the plane
1622  * @fb: fb to use for the plane
1623  *
1624  * Changing the assigned framebuffer for a plane requires us to grab a reference
1625  * to the new fb and drop the reference to the old fb, if there is one. This
1626  * function takes care of all these details besides updating the pointer in the
1627  * state object itself.
1628  */
1629 void
drm_atomic_set_fb_for_plane(struct drm_plane_state * plane_state,struct drm_framebuffer * fb)1630 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1631 			    struct drm_framebuffer *fb)
1632 {
1633 	struct drm_plane *plane = plane_state->plane;
1634 
1635 	if (fb)
1636 		DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
1637 				 fb->base.id, plane->base.id, plane->name,
1638 				 plane_state);
1639 	else
1640 		DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
1641 				 plane->base.id, plane->name, plane_state);
1642 
1643 	drm_framebuffer_assign(&plane_state->fb, fb);
1644 }
1645 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1646 
1647 /**
1648  * drm_atomic_set_fence_for_plane - set fence for plane
1649  * @plane_state: atomic state object for the plane
1650  * @fence: dma_fence to use for the plane
1651  *
1652  * Helper to setup the plane_state fence in case it is not set yet.
1653  * By using this drivers doesn't need to worry if the user choose
1654  * implicit or explicit fencing.
1655  *
1656  * This function will not set the fence to the state if it was set
1657  * via explicit fencing interfaces on the atomic ioctl. In that case it will
1658  * drop the reference to the fence as we are not storing it anywhere.
1659  * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1660  * with the received implicit fence. In both cases this function consumes a
1661  * reference for @fence.
1662  *
1663  * This way explicit fencing can be used to overrule implicit fencing, which is
1664  * important to make explicit fencing use-cases work: One example is using one
1665  * buffer for 2 screens with different refresh rates. Implicit fencing will
1666  * clamp rendering to the refresh rate of the slower screen, whereas explicit
1667  * fence allows 2 independent render and display loops on a single buffer. If a
1668  * driver allows obeys both implicit and explicit fences for plane updates, then
1669  * it will break all the benefits of explicit fencing.
1670  */
1671 void
drm_atomic_set_fence_for_plane(struct drm_plane_state * plane_state,struct dma_fence * fence)1672 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1673 			       struct dma_fence *fence)
1674 {
1675 	if (plane_state->fence) {
1676 		dma_fence_put(fence);
1677 		return;
1678 	}
1679 
1680 	plane_state->fence = fence;
1681 }
1682 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1683 
1684 /**
1685  * drm_atomic_set_crtc_for_connector - set crtc for connector
1686  * @conn_state: atomic state object for the connector
1687  * @crtc: crtc to use for the connector
1688  *
1689  * Changing the assigned crtc for a connector requires us to grab the lock and
1690  * state for the new crtc, as needed. This function takes care of all these
1691  * details besides updating the pointer in the state object itself.
1692  *
1693  * Returns:
1694  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1695  * then the w/w mutex code has detected a deadlock and the entire atomic
1696  * sequence must be restarted. All other errors are fatal.
1697  */
1698 int
drm_atomic_set_crtc_for_connector(struct drm_connector_state * conn_state,struct drm_crtc * crtc)1699 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1700 				  struct drm_crtc *crtc)
1701 {
1702 	struct drm_connector *connector = conn_state->connector;
1703 	struct drm_crtc_state *crtc_state;
1704 
1705 	/*
1706 	 * For compatibility with legacy users, we want to make sure that
1707 	 * we allow DPMS On<->Off modesets on unregistered connectors, since
1708 	 * legacy modesetting users will not be expecting these to fail. We do
1709 	 * not however, want to allow legacy users to assign a connector
1710 	 * that's been unregistered from sysfs to another CRTC, since doing
1711 	 * this with a now non-existent connector could potentially leave us
1712 	 * in an invalid state.
1713 	 *
1714 	 * Since the connector can be unregistered at any point during an
1715 	 * atomic check or commit, this is racy. But that's OK: all we care
1716 	 * about is ensuring that userspace can't use this connector for new
1717 	 * configurations after it's been notified that the connector is no
1718 	 * longer present.
1719 	 */
1720 	if (!READ_ONCE(connector->registered) && crtc) {
1721 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
1722 				 connector->base.id, connector->name);
1723 		return -EINVAL;
1724 	}
1725 
1726 	if (conn_state->crtc == crtc)
1727 		return 0;
1728 
1729 	if (conn_state->crtc) {
1730 		crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1731 							   conn_state->crtc);
1732 
1733 		crtc_state->connector_mask &=
1734 			~drm_connector_mask(conn_state->connector);
1735 
1736 		drm_connector_put(conn_state->connector);
1737 		conn_state->crtc = NULL;
1738 	}
1739 
1740 	if (crtc) {
1741 		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1742 		if (IS_ERR(crtc_state))
1743 			return PTR_ERR(crtc_state);
1744 
1745 		crtc_state->connector_mask |=
1746 			drm_connector_mask(conn_state->connector);
1747 
1748 		drm_connector_get(conn_state->connector);
1749 		conn_state->crtc = crtc;
1750 
1751 		DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
1752 				 connector->base.id, connector->name,
1753 				 conn_state, crtc->base.id, crtc->name);
1754 	} else {
1755 		DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
1756 				 connector->base.id, connector->name,
1757 				 conn_state);
1758 	}
1759 
1760 	return 0;
1761 }
1762 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1763 
1764 /*
1765  * drm_atomic_get_writeback_job - return or allocate a writeback job
1766  * @conn_state: Connector state to get the job for
1767  *
1768  * Writeback jobs have a different lifetime to the atomic state they are
1769  * associated with. This convenience function takes care of allocating a job
1770  * if there isn't yet one associated with the connector state, otherwise
1771  * it just returns the existing job.
1772  *
1773  * Returns: The writeback job for the given connector state
1774  */
1775 static struct drm_writeback_job *
drm_atomic_get_writeback_job(struct drm_connector_state * conn_state)1776 drm_atomic_get_writeback_job(struct drm_connector_state *conn_state)
1777 {
1778 	WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1779 
1780 	if (!conn_state->writeback_job)
1781 		conn_state->writeback_job =
1782 			kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL);
1783 
1784 	return conn_state->writeback_job;
1785 }
1786 
1787 /**
1788  * drm_atomic_set_writeback_fb_for_connector - set writeback framebuffer
1789  * @conn_state: atomic state object for the connector
1790  * @fb: fb to use for the connector
1791  *
1792  * This is used to set the framebuffer for a writeback connector, which outputs
1793  * to a buffer instead of an actual physical connector.
1794  * Changing the assigned framebuffer requires us to grab a reference to the new
1795  * fb and drop the reference to the old fb, if there is one. This function
1796  * takes care of all these details besides updating the pointer in the
1797  * state object itself.
1798  *
1799  * Note: The only way conn_state can already have an fb set is if the commit
1800  * sets the property more than once.
1801  *
1802  * See also: drm_writeback_connector_init()
1803  *
1804  * Returns: 0 on success
1805  */
drm_atomic_set_writeback_fb_for_connector(struct drm_connector_state * conn_state,struct drm_framebuffer * fb)1806 int drm_atomic_set_writeback_fb_for_connector(
1807 		struct drm_connector_state *conn_state,
1808 		struct drm_framebuffer *fb)
1809 {
1810 	struct drm_writeback_job *job =
1811 		drm_atomic_get_writeback_job(conn_state);
1812 	if (!job)
1813 		return -ENOMEM;
1814 
1815 	drm_framebuffer_assign(&job->fb, fb);
1816 
1817 	if (fb)
1818 		DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
1819 				 fb->base.id, conn_state);
1820 	else
1821 		DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
1822 				 conn_state);
1823 
1824 	return 0;
1825 }
1826 EXPORT_SYMBOL(drm_atomic_set_writeback_fb_for_connector);
1827 
1828 /**
1829  * drm_atomic_add_affected_connectors - add connectors for crtc
1830  * @state: atomic state
1831  * @crtc: DRM crtc
1832  *
1833  * This function walks the current configuration and adds all connectors
1834  * currently using @crtc to the atomic configuration @state. Note that this
1835  * function must acquire the connection mutex. This can potentially cause
1836  * unneeded seralization if the update is just for the planes on one crtc. Hence
1837  * drivers and helpers should only call this when really needed (e.g. when a
1838  * full modeset needs to happen due to some change).
1839  *
1840  * Returns:
1841  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1842  * then the w/w mutex code has detected a deadlock and the entire atomic
1843  * sequence must be restarted. All other errors are fatal.
1844  */
1845 int
drm_atomic_add_affected_connectors(struct drm_atomic_state * state,struct drm_crtc * crtc)1846 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1847 				   struct drm_crtc *crtc)
1848 {
1849 	struct drm_mode_config *config = &state->dev->mode_config;
1850 	struct drm_connector *connector;
1851 	struct drm_connector_state *conn_state;
1852 	struct drm_connector_list_iter conn_iter;
1853 	struct drm_crtc_state *crtc_state;
1854 	int ret;
1855 
1856 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1857 	if (IS_ERR(crtc_state))
1858 		return PTR_ERR(crtc_state);
1859 
1860 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1861 	if (ret)
1862 		return ret;
1863 
1864 	DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1865 			 crtc->base.id, crtc->name, state);
1866 
1867 	/*
1868 	 * Changed connectors are already in @state, so only need to look
1869 	 * at the connector_mask in crtc_state.
1870 	 */
1871 	drm_connector_list_iter_begin(state->dev, &conn_iter);
1872 	drm_for_each_connector_iter(connector, &conn_iter) {
1873 		if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
1874 			continue;
1875 
1876 		conn_state = drm_atomic_get_connector_state(state, connector);
1877 		if (IS_ERR(conn_state)) {
1878 			drm_connector_list_iter_end(&conn_iter);
1879 			return PTR_ERR(conn_state);
1880 		}
1881 	}
1882 	drm_connector_list_iter_end(&conn_iter);
1883 
1884 	return 0;
1885 }
1886 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1887 
1888 /**
1889  * drm_atomic_add_affected_planes - add planes for crtc
1890  * @state: atomic state
1891  * @crtc: DRM crtc
1892  *
1893  * This function walks the current configuration and adds all planes
1894  * currently used by @crtc to the atomic configuration @state. This is useful
1895  * when an atomic commit also needs to check all currently enabled plane on
1896  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1897  * to avoid special code to force-enable all planes.
1898  *
1899  * Since acquiring a plane state will always also acquire the w/w mutex of the
1900  * current CRTC for that plane (if there is any) adding all the plane states for
1901  * a CRTC will not reduce parallism of atomic updates.
1902  *
1903  * Returns:
1904  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1905  * then the w/w mutex code has detected a deadlock and the entire atomic
1906  * sequence must be restarted. All other errors are fatal.
1907  */
1908 int
drm_atomic_add_affected_planes(struct drm_atomic_state * state,struct drm_crtc * crtc)1909 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1910 			       struct drm_crtc *crtc)
1911 {
1912 	struct drm_plane *plane;
1913 
1914 	WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1915 
1916 	DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
1917 			 crtc->base.id, crtc->name, state);
1918 
1919 	drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1920 		struct drm_plane_state *plane_state =
1921 			drm_atomic_get_plane_state(state, plane);
1922 
1923 		if (IS_ERR(plane_state))
1924 			return PTR_ERR(plane_state);
1925 	}
1926 	return 0;
1927 }
1928 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1929 
1930 /**
1931  * drm_atomic_check_only - check whether a given config would work
1932  * @state: atomic configuration to check
1933  *
1934  * Note that this function can return -EDEADLK if the driver needed to acquire
1935  * more locks but encountered a deadlock. The caller must then do the usual w/w
1936  * backoff dance and restart. All other errors are fatal.
1937  *
1938  * Returns:
1939  * 0 on success, negative error code on failure.
1940  */
drm_atomic_check_only(struct drm_atomic_state * state)1941 int drm_atomic_check_only(struct drm_atomic_state *state)
1942 {
1943 	struct drm_device *dev = state->dev;
1944 	struct drm_mode_config *config = &dev->mode_config;
1945 	struct drm_plane *plane;
1946 	struct drm_plane_state *plane_state;
1947 	struct drm_crtc *crtc;
1948 	struct drm_crtc_state *crtc_state;
1949 	struct drm_connector *conn;
1950 	struct drm_connector_state *conn_state;
1951 	int i, ret = 0;
1952 
1953 	DRM_DEBUG_ATOMIC("checking %p\n", state);
1954 
1955 	for_each_new_plane_in_state(state, plane, plane_state, i) {
1956 		ret = drm_atomic_plane_check(plane, plane_state);
1957 		if (ret) {
1958 			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1959 					 plane->base.id, plane->name);
1960 			return ret;
1961 		}
1962 	}
1963 
1964 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1965 		ret = drm_atomic_crtc_check(crtc, crtc_state);
1966 		if (ret) {
1967 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1968 					 crtc->base.id, crtc->name);
1969 			return ret;
1970 		}
1971 	}
1972 
1973 	for_each_new_connector_in_state(state, conn, conn_state, i) {
1974 		ret = drm_atomic_connector_check(conn, conn_state);
1975 		if (ret) {
1976 			DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
1977 					 conn->base.id, conn->name);
1978 			return ret;
1979 		}
1980 	}
1981 
1982 	if (config->funcs->atomic_check) {
1983 		ret = config->funcs->atomic_check(state->dev, state);
1984 
1985 		if (ret) {
1986 			DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
1987 					 state, ret);
1988 			return ret;
1989 		}
1990 	}
1991 
1992 	if (!state->allow_modeset) {
1993 		for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1994 			if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1995 				DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1996 						 crtc->base.id, crtc->name);
1997 				return -EINVAL;
1998 			}
1999 		}
2000 	}
2001 
2002 	return 0;
2003 }
2004 EXPORT_SYMBOL(drm_atomic_check_only);
2005 
2006 /**
2007  * drm_atomic_commit - commit configuration atomically
2008  * @state: atomic configuration to check
2009  *
2010  * Note that this function can return -EDEADLK if the driver needed to acquire
2011  * more locks but encountered a deadlock. The caller must then do the usual w/w
2012  * backoff dance and restart. All other errors are fatal.
2013  *
2014  * This function will take its own reference on @state.
2015  * Callers should always release their reference with drm_atomic_state_put().
2016  *
2017  * Returns:
2018  * 0 on success, negative error code on failure.
2019  */
drm_atomic_commit(struct drm_atomic_state * state)2020 int drm_atomic_commit(struct drm_atomic_state *state)
2021 {
2022 	struct drm_mode_config *config = &state->dev->mode_config;
2023 	int ret;
2024 
2025 	ret = drm_atomic_check_only(state);
2026 	if (ret)
2027 		return ret;
2028 
2029 	DRM_DEBUG_ATOMIC("committing %p\n", state);
2030 
2031 	return config->funcs->atomic_commit(state->dev, state, false);
2032 }
2033 EXPORT_SYMBOL(drm_atomic_commit);
2034 
2035 /**
2036  * drm_atomic_nonblocking_commit - atomic nonblocking commit
2037  * @state: atomic configuration to check
2038  *
2039  * Note that this function can return -EDEADLK if the driver needed to acquire
2040  * more locks but encountered a deadlock. The caller must then do the usual w/w
2041  * backoff dance and restart. All other errors are fatal.
2042  *
2043  * This function will take its own reference on @state.
2044  * Callers should always release their reference with drm_atomic_state_put().
2045  *
2046  * Returns:
2047  * 0 on success, negative error code on failure.
2048  */
drm_atomic_nonblocking_commit(struct drm_atomic_state * state)2049 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
2050 {
2051 	struct drm_mode_config *config = &state->dev->mode_config;
2052 	int ret;
2053 
2054 	ret = drm_atomic_check_only(state);
2055 	if (ret)
2056 		return ret;
2057 
2058 	DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
2059 
2060 	return config->funcs->atomic_commit(state->dev, state, true);
2061 }
2062 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
2063 
drm_atomic_print_state(const struct drm_atomic_state * state)2064 static void drm_atomic_print_state(const struct drm_atomic_state *state)
2065 {
2066 	struct drm_printer p = drm_info_printer(state->dev->dev);
2067 	struct drm_plane *plane;
2068 	struct drm_plane_state *plane_state;
2069 	struct drm_crtc *crtc;
2070 	struct drm_crtc_state *crtc_state;
2071 	struct drm_connector *connector;
2072 	struct drm_connector_state *connector_state;
2073 	int i;
2074 
2075 	DRM_DEBUG_ATOMIC("checking %p\n", state);
2076 
2077 	for_each_new_plane_in_state(state, plane, plane_state, i)
2078 		drm_atomic_plane_print_state(&p, plane_state);
2079 
2080 	for_each_new_crtc_in_state(state, crtc, crtc_state, i)
2081 		drm_atomic_crtc_print_state(&p, crtc_state);
2082 
2083 	for_each_new_connector_in_state(state, connector, connector_state, i)
2084 		drm_atomic_connector_print_state(&p, connector_state);
2085 }
2086 
__drm_state_dump(struct drm_device * dev,struct drm_printer * p,bool take_locks)2087 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
2088 			     bool take_locks)
2089 {
2090 	struct drm_mode_config *config = &dev->mode_config;
2091 	struct drm_plane *plane;
2092 	struct drm_crtc *crtc;
2093 	struct drm_connector *connector;
2094 	struct drm_connector_list_iter conn_iter;
2095 
2096 	if (!drm_drv_uses_atomic_modeset(dev))
2097 		return;
2098 
2099 	list_for_each_entry(plane, &config->plane_list, head) {
2100 		if (take_locks)
2101 			drm_modeset_lock(&plane->mutex, NULL);
2102 		drm_atomic_plane_print_state(p, plane->state);
2103 		if (take_locks)
2104 			drm_modeset_unlock(&plane->mutex);
2105 	}
2106 
2107 	list_for_each_entry(crtc, &config->crtc_list, head) {
2108 		if (take_locks)
2109 			drm_modeset_lock(&crtc->mutex, NULL);
2110 		drm_atomic_crtc_print_state(p, crtc->state);
2111 		if (take_locks)
2112 			drm_modeset_unlock(&crtc->mutex);
2113 	}
2114 
2115 	drm_connector_list_iter_begin(dev, &conn_iter);
2116 	if (take_locks)
2117 		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2118 	drm_for_each_connector_iter(connector, &conn_iter)
2119 		drm_atomic_connector_print_state(p, connector->state);
2120 	if (take_locks)
2121 		drm_modeset_unlock(&dev->mode_config.connection_mutex);
2122 	drm_connector_list_iter_end(&conn_iter);
2123 }
2124 
2125 /**
2126  * drm_state_dump - dump entire device atomic state
2127  * @dev: the drm device
2128  * @p: where to print the state to
2129  *
2130  * Just for debugging.  Drivers might want an option to dump state
2131  * to dmesg in case of error irq's.  (Hint, you probably want to
2132  * ratelimit this!)
2133  *
2134  * The caller must drm_modeset_lock_all(), or if this is called
2135  * from error irq handler, it should not be enabled by default.
2136  * (Ie. if you are debugging errors you might not care that this
2137  * is racey.  But calling this without all modeset locks held is
2138  * not inherently safe.)
2139  */
drm_state_dump(struct drm_device * dev,struct drm_printer * p)2140 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
2141 {
2142 	__drm_state_dump(dev, p, false);
2143 }
2144 EXPORT_SYMBOL(drm_state_dump);
2145 
2146 #ifdef CONFIG_DEBUG_FS
drm_state_info(struct seq_file * m,void * data)2147 static int drm_state_info(struct seq_file *m, void *data)
2148 {
2149 	struct drm_info_node *node = (struct drm_info_node *) m->private;
2150 	struct drm_device *dev = node->minor->dev;
2151 	struct drm_printer p = drm_seq_file_printer(m);
2152 
2153 	__drm_state_dump(dev, &p, true);
2154 
2155 	return 0;
2156 }
2157 
2158 /* any use in debugfs files to dump individual planes/crtc/etc? */
2159 static const struct drm_info_list drm_atomic_debugfs_list[] = {
2160 	{"state", drm_state_info, 0},
2161 };
2162 
drm_atomic_debugfs_init(struct drm_minor * minor)2163 int drm_atomic_debugfs_init(struct drm_minor *minor)
2164 {
2165 	return drm_debugfs_create_files(drm_atomic_debugfs_list,
2166 			ARRAY_SIZE(drm_atomic_debugfs_list),
2167 			minor->debugfs_root, minor);
2168 }
2169 #endif
2170 
2171 /*
2172  * The big monster ioctl
2173  */
2174 
create_vblank_event(struct drm_crtc * crtc,uint64_t user_data)2175 static struct drm_pending_vblank_event *create_vblank_event(
2176 		struct drm_crtc *crtc, uint64_t user_data)
2177 {
2178 	struct drm_pending_vblank_event *e = NULL;
2179 
2180 	e = kzalloc(sizeof *e, GFP_KERNEL);
2181 	if (!e)
2182 		return NULL;
2183 
2184 	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2185 	e->event.base.length = sizeof(e->event);
2186 	e->event.vbl.crtc_id = crtc->base.id;
2187 	e->event.vbl.user_data = user_data;
2188 
2189 	return e;
2190 }
2191 
drm_atomic_connector_commit_dpms(struct drm_atomic_state * state,struct drm_connector * connector,int mode)2192 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
2193 				     struct drm_connector *connector,
2194 				     int mode)
2195 {
2196 	struct drm_connector *tmp_connector;
2197 	struct drm_connector_state *new_conn_state;
2198 	struct drm_crtc *crtc;
2199 	struct drm_crtc_state *crtc_state;
2200 	int i, ret, old_mode = connector->dpms;
2201 	bool active = false;
2202 
2203 	ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
2204 			       state->acquire_ctx);
2205 	if (ret)
2206 		return ret;
2207 
2208 	if (mode != DRM_MODE_DPMS_ON)
2209 		mode = DRM_MODE_DPMS_OFF;
2210 	connector->dpms = mode;
2211 
2212 	crtc = connector->state->crtc;
2213 	if (!crtc)
2214 		goto out;
2215 	ret = drm_atomic_add_affected_connectors(state, crtc);
2216 	if (ret)
2217 		goto out;
2218 
2219 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2220 	if (IS_ERR(crtc_state)) {
2221 		ret = PTR_ERR(crtc_state);
2222 		goto out;
2223 	}
2224 
2225 	for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
2226 		if (new_conn_state->crtc != crtc)
2227 			continue;
2228 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2229 			active = true;
2230 			break;
2231 		}
2232 	}
2233 
2234 	crtc_state->active = active;
2235 	ret = drm_atomic_commit(state);
2236 out:
2237 	if (ret != 0)
2238 		connector->dpms = old_mode;
2239 	return ret;
2240 }
2241 
drm_atomic_set_property(struct drm_atomic_state * state,struct drm_mode_object * obj,struct drm_property * prop,uint64_t prop_value)2242 int drm_atomic_set_property(struct drm_atomic_state *state,
2243 			    struct drm_mode_object *obj,
2244 			    struct drm_property *prop,
2245 			    uint64_t prop_value)
2246 {
2247 	struct drm_mode_object *ref;
2248 	int ret;
2249 
2250 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
2251 		return -EINVAL;
2252 
2253 	switch (obj->type) {
2254 	case DRM_MODE_OBJECT_CONNECTOR: {
2255 		struct drm_connector *connector = obj_to_connector(obj);
2256 		struct drm_connector_state *connector_state;
2257 
2258 		connector_state = drm_atomic_get_connector_state(state, connector);
2259 		if (IS_ERR(connector_state)) {
2260 			ret = PTR_ERR(connector_state);
2261 			break;
2262 		}
2263 
2264 		ret = drm_atomic_connector_set_property(connector,
2265 				connector_state, prop, prop_value);
2266 		break;
2267 	}
2268 	case DRM_MODE_OBJECT_CRTC: {
2269 		struct drm_crtc *crtc = obj_to_crtc(obj);
2270 		struct drm_crtc_state *crtc_state;
2271 
2272 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
2273 		if (IS_ERR(crtc_state)) {
2274 			ret = PTR_ERR(crtc_state);
2275 			break;
2276 		}
2277 
2278 		ret = drm_atomic_crtc_set_property(crtc,
2279 				crtc_state, prop, prop_value);
2280 		break;
2281 	}
2282 	case DRM_MODE_OBJECT_PLANE: {
2283 		struct drm_plane *plane = obj_to_plane(obj);
2284 		struct drm_plane_state *plane_state;
2285 
2286 		plane_state = drm_atomic_get_plane_state(state, plane);
2287 		if (IS_ERR(plane_state)) {
2288 			ret = PTR_ERR(plane_state);
2289 			break;
2290 		}
2291 
2292 		ret = drm_atomic_plane_set_property(plane,
2293 				plane_state, prop, prop_value);
2294 		break;
2295 	}
2296 	default:
2297 		ret = -EINVAL;
2298 		break;
2299 	}
2300 
2301 	drm_property_change_valid_put(prop, ref);
2302 	return ret;
2303 }
2304 
2305 /**
2306  * DOC: explicit fencing properties
2307  *
2308  * Explicit fencing allows userspace to control the buffer synchronization
2309  * between devices. A Fence or a group of fences are transfered to/from
2310  * userspace using Sync File fds and there are two DRM properties for that.
2311  * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
2312  * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
2313  *
2314  * As a contrast, with implicit fencing the kernel keeps track of any
2315  * ongoing rendering, and automatically ensures that the atomic update waits
2316  * for any pending rendering to complete. For shared buffers represented with
2317  * a &struct dma_buf this is tracked in &struct reservation_object.
2318  * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
2319  * whereas explicit fencing is what Android wants.
2320  *
2321  * "IN_FENCE_FD”:
2322  *	Use this property to pass a fence that DRM should wait on before
2323  *	proceeding with the Atomic Commit request and show the framebuffer for
2324  *	the plane on the screen. The fence can be either a normal fence or a
2325  *	merged one, the sync_file framework will handle both cases and use a
2326  *	fence_array if a merged fence is received. Passing -1 here means no
2327  *	fences to wait on.
2328  *
2329  *	If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
2330  *	it will only check if the Sync File is a valid one.
2331  *
2332  *	On the driver side the fence is stored on the @fence parameter of
2333  *	&struct drm_plane_state. Drivers which also support implicit fencing
2334  *	should set the implicit fence using drm_atomic_set_fence_for_plane(),
2335  *	to make sure there's consistent behaviour between drivers in precedence
2336  *	of implicit vs. explicit fencing.
2337  *
2338  * "OUT_FENCE_PTR”:
2339  *	Use this property to pass a file descriptor pointer to DRM. Once the
2340  *	Atomic Commit request call returns OUT_FENCE_PTR will be filled with
2341  *	the file descriptor number of a Sync File. This Sync File contains the
2342  *	CRTC fence that will be signaled when all framebuffers present on the
2343  *	Atomic Commit * request for that given CRTC are scanned out on the
2344  *	screen.
2345  *
2346  *	The Atomic Commit request fails if a invalid pointer is passed. If the
2347  *	Atomic Commit request fails for any other reason the out fence fd
2348  *	returned will be -1. On a Atomic Commit with the
2349  *	DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2350  *
2351  *	Note that out-fences don't have a special interface to drivers and are
2352  *	internally represented by a &struct drm_pending_vblank_event in struct
2353  *	&drm_crtc_state, which is also used by the nonblocking atomic commit
2354  *	helpers and for the DRM event handling for existing userspace.
2355  */
2356 
2357 struct drm_out_fence_state {
2358 	s32 __user *out_fence_ptr;
2359 	struct sync_file *sync_file;
2360 	int fd;
2361 };
2362 
setup_out_fence(struct drm_out_fence_state * fence_state,struct dma_fence * fence)2363 static int setup_out_fence(struct drm_out_fence_state *fence_state,
2364 			   struct dma_fence *fence)
2365 {
2366 	fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
2367 	if (fence_state->fd < 0)
2368 		return fence_state->fd;
2369 
2370 	if (put_user(fence_state->fd, fence_state->out_fence_ptr))
2371 		return -EFAULT;
2372 
2373 	fence_state->sync_file = sync_file_create(fence);
2374 	if (!fence_state->sync_file)
2375 		return -ENOMEM;
2376 
2377 	return 0;
2378 }
2379 
prepare_signaling(struct drm_device * dev,struct drm_atomic_state * state,struct drm_mode_atomic * arg,struct drm_file * file_priv,struct drm_out_fence_state ** fence_state,unsigned int * num_fences)2380 static int prepare_signaling(struct drm_device *dev,
2381 				  struct drm_atomic_state *state,
2382 				  struct drm_mode_atomic *arg,
2383 				  struct drm_file *file_priv,
2384 				  struct drm_out_fence_state **fence_state,
2385 				  unsigned int *num_fences)
2386 {
2387 	struct drm_crtc *crtc;
2388 	struct drm_crtc_state *crtc_state;
2389 	struct drm_connector *conn;
2390 	struct drm_connector_state *conn_state;
2391 	int i, c = 0, ret;
2392 
2393 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
2394 		return 0;
2395 
2396 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2397 		s32 __user *fence_ptr;
2398 
2399 		fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
2400 
2401 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
2402 			struct drm_pending_vblank_event *e;
2403 
2404 			e = create_vblank_event(crtc, arg->user_data);
2405 			if (!e)
2406 				return -ENOMEM;
2407 
2408 			crtc_state->event = e;
2409 		}
2410 
2411 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2412 			struct drm_pending_vblank_event *e = crtc_state->event;
2413 
2414 			if (!file_priv)
2415 				continue;
2416 
2417 			ret = drm_event_reserve_init(dev, file_priv, &e->base,
2418 						     &e->event.base);
2419 			if (ret) {
2420 				kfree(e);
2421 				crtc_state->event = NULL;
2422 				return ret;
2423 			}
2424 		}
2425 
2426 		if (fence_ptr) {
2427 			struct dma_fence *fence;
2428 			struct drm_out_fence_state *f;
2429 
2430 			f = krealloc(*fence_state, sizeof(**fence_state) *
2431 				     (*num_fences + 1), GFP_KERNEL);
2432 			if (!f)
2433 				return -ENOMEM;
2434 
2435 			memset(&f[*num_fences], 0, sizeof(*f));
2436 
2437 			f[*num_fences].out_fence_ptr = fence_ptr;
2438 			*fence_state = f;
2439 
2440 			fence = drm_crtc_create_fence(crtc);
2441 			if (!fence)
2442 				return -ENOMEM;
2443 
2444 			ret = setup_out_fence(&f[(*num_fences)++], fence);
2445 			if (ret) {
2446 				dma_fence_put(fence);
2447 				return ret;
2448 			}
2449 
2450 			crtc_state->event->base.fence = fence;
2451 		}
2452 
2453 		c++;
2454 	}
2455 
2456 	for_each_new_connector_in_state(state, conn, conn_state, i) {
2457 		struct drm_writeback_connector *wb_conn;
2458 		struct drm_writeback_job *job;
2459 		struct drm_out_fence_state *f;
2460 		struct dma_fence *fence;
2461 		s32 __user *fence_ptr;
2462 
2463 		fence_ptr = get_out_fence_for_connector(state, conn);
2464 		if (!fence_ptr)
2465 			continue;
2466 
2467 		job = drm_atomic_get_writeback_job(conn_state);
2468 		if (!job)
2469 			return -ENOMEM;
2470 
2471 		f = krealloc(*fence_state, sizeof(**fence_state) *
2472 			     (*num_fences + 1), GFP_KERNEL);
2473 		if (!f)
2474 			return -ENOMEM;
2475 
2476 		memset(&f[*num_fences], 0, sizeof(*f));
2477 
2478 		f[*num_fences].out_fence_ptr = fence_ptr;
2479 		*fence_state = f;
2480 
2481 		wb_conn = drm_connector_to_writeback(conn);
2482 		fence = drm_writeback_get_out_fence(wb_conn);
2483 		if (!fence)
2484 			return -ENOMEM;
2485 
2486 		ret = setup_out_fence(&f[(*num_fences)++], fence);
2487 		if (ret) {
2488 			dma_fence_put(fence);
2489 			return ret;
2490 		}
2491 
2492 		job->out_fence = fence;
2493 	}
2494 
2495 	/*
2496 	 * Having this flag means user mode pends on event which will never
2497 	 * reach due to lack of at least one CRTC for signaling
2498 	 */
2499 	if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2500 		return -EINVAL;
2501 
2502 	return 0;
2503 }
2504 
complete_signaling(struct drm_device * dev,struct drm_atomic_state * state,struct drm_out_fence_state * fence_state,unsigned int num_fences,bool install_fds)2505 static void complete_signaling(struct drm_device *dev,
2506 			       struct drm_atomic_state *state,
2507 			       struct drm_out_fence_state *fence_state,
2508 			       unsigned int num_fences,
2509 			       bool install_fds)
2510 {
2511 	struct drm_crtc *crtc;
2512 	struct drm_crtc_state *crtc_state;
2513 	int i;
2514 
2515 	if (install_fds) {
2516 		for (i = 0; i < num_fences; i++)
2517 			fd_install(fence_state[i].fd,
2518 				   fence_state[i].sync_file->file);
2519 
2520 		kfree(fence_state);
2521 		return;
2522 	}
2523 
2524 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2525 		struct drm_pending_vblank_event *event = crtc_state->event;
2526 		/*
2527 		 * Free the allocated event. drm_atomic_helper_setup_commit
2528 		 * can allocate an event too, so only free it if it's ours
2529 		 * to prevent a double free in drm_atomic_state_clear.
2530 		 */
2531 		if (event && (event->base.fence || event->base.file_priv)) {
2532 			drm_event_cancel_free(dev, &event->base);
2533 			crtc_state->event = NULL;
2534 		}
2535 	}
2536 
2537 	if (!fence_state)
2538 		return;
2539 
2540 	for (i = 0; i < num_fences; i++) {
2541 		if (fence_state[i].sync_file)
2542 			fput(fence_state[i].sync_file->file);
2543 		if (fence_state[i].fd >= 0)
2544 			put_unused_fd(fence_state[i].fd);
2545 
2546 		/* If this fails log error to the user */
2547 		if (fence_state[i].out_fence_ptr &&
2548 		    put_user(-1, fence_state[i].out_fence_ptr))
2549 			DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2550 	}
2551 
2552 	kfree(fence_state);
2553 }
2554 
drm_mode_atomic_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)2555 int drm_mode_atomic_ioctl(struct drm_device *dev,
2556 			  void *data, struct drm_file *file_priv)
2557 {
2558 	struct drm_mode_atomic *arg = data;
2559 	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2560 	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2561 	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2562 	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2563 	unsigned int copied_objs, copied_props;
2564 	struct drm_atomic_state *state;
2565 	struct drm_modeset_acquire_ctx ctx;
2566 	struct drm_out_fence_state *fence_state;
2567 	int ret = 0;
2568 	unsigned int i, j, num_fences;
2569 
2570 	/* disallow for drivers not supporting atomic: */
2571 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2572 		return -EINVAL;
2573 
2574 	/* disallow for userspace that has not enabled atomic cap (even
2575 	 * though this may be a bit overkill, since legacy userspace
2576 	 * wouldn't know how to call this ioctl)
2577 	 */
2578 	if (!file_priv->atomic)
2579 		return -EINVAL;
2580 
2581 	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2582 		return -EINVAL;
2583 
2584 	if (arg->reserved)
2585 		return -EINVAL;
2586 
2587 	if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2588 			!dev->mode_config.async_page_flip)
2589 		return -EINVAL;
2590 
2591 	/* can't test and expect an event at the same time. */
2592 	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2593 			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2594 		return -EINVAL;
2595 
2596 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
2597 
2598 	state = drm_atomic_state_alloc(dev);
2599 	if (!state)
2600 		return -ENOMEM;
2601 
2602 	state->acquire_ctx = &ctx;
2603 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2604 
2605 retry:
2606 	copied_objs = 0;
2607 	copied_props = 0;
2608 	fence_state = NULL;
2609 	num_fences = 0;
2610 
2611 	for (i = 0; i < arg->count_objs; i++) {
2612 		uint32_t obj_id, count_props;
2613 		struct drm_mode_object *obj;
2614 
2615 		if (get_user(obj_id, objs_ptr + copied_objs)) {
2616 			ret = -EFAULT;
2617 			goto out;
2618 		}
2619 
2620 		obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
2621 		if (!obj) {
2622 			ret = -ENOENT;
2623 			goto out;
2624 		}
2625 
2626 		if (!obj->properties) {
2627 			drm_mode_object_put(obj);
2628 			ret = -ENOENT;
2629 			goto out;
2630 		}
2631 
2632 		if (get_user(count_props, count_props_ptr + copied_objs)) {
2633 			drm_mode_object_put(obj);
2634 			ret = -EFAULT;
2635 			goto out;
2636 		}
2637 
2638 		copied_objs++;
2639 
2640 		for (j = 0; j < count_props; j++) {
2641 			uint32_t prop_id;
2642 			uint64_t prop_value;
2643 			struct drm_property *prop;
2644 
2645 			if (get_user(prop_id, props_ptr + copied_props)) {
2646 				drm_mode_object_put(obj);
2647 				ret = -EFAULT;
2648 				goto out;
2649 			}
2650 
2651 			prop = drm_mode_obj_find_prop_id(obj, prop_id);
2652 			if (!prop) {
2653 				drm_mode_object_put(obj);
2654 				ret = -ENOENT;
2655 				goto out;
2656 			}
2657 
2658 			if (copy_from_user(&prop_value,
2659 					   prop_values_ptr + copied_props,
2660 					   sizeof(prop_value))) {
2661 				drm_mode_object_put(obj);
2662 				ret = -EFAULT;
2663 				goto out;
2664 			}
2665 
2666 			ret = drm_atomic_set_property(state, obj, prop,
2667 						      prop_value);
2668 			if (ret) {
2669 				drm_mode_object_put(obj);
2670 				goto out;
2671 			}
2672 
2673 			copied_props++;
2674 		}
2675 
2676 		drm_mode_object_put(obj);
2677 	}
2678 
2679 	ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
2680 				&num_fences);
2681 	if (ret)
2682 		goto out;
2683 
2684 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2685 		ret = drm_atomic_check_only(state);
2686 	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
2687 		ret = drm_atomic_nonblocking_commit(state);
2688 	} else {
2689 		if (unlikely(drm_debug & DRM_UT_STATE))
2690 			drm_atomic_print_state(state);
2691 
2692 		ret = drm_atomic_commit(state);
2693 	}
2694 
2695 out:
2696 	complete_signaling(dev, state, fence_state, num_fences, !ret);
2697 
2698 	if (ret == -EDEADLK) {
2699 		drm_atomic_state_clear(state);
2700 		ret = drm_modeset_backoff(&ctx);
2701 		if (!ret)
2702 			goto retry;
2703 	}
2704 
2705 	drm_atomic_state_put(state);
2706 
2707 	drm_modeset_drop_locks(&ctx);
2708 	drm_modeset_acquire_fini(&ctx);
2709 
2710 	return ret;
2711 }
2712