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