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