1 /*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 * Copyright (C) 2018 Intel Corp.
5 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robdclark@gmail.com>
27 * Daniel Vetter <daniel.vetter@ffwll.ch>
28 */
29
30 #include <drm/drm_atomic_uapi.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_print.h>
33 #include <drm/drm_drv.h>
34 #include <drm/drm_writeback.h>
35 #include <drm/drm_vblank.h>
36
37 #include <linux/dma-fence.h>
38 #include <linux/uaccess.h>
39 #include <linux/sync_file.h>
40 #include <linux/file.h>
41
42 #include "drm_crtc_internal.h"
43
44 /**
45 * DOC: overview
46 *
47 * This file contains the marshalling and demarshalling glue for the atomic UAPI
48 * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and
49 * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and
50 * drivers which have special needs to construct their own atomic updates, e.g.
51 * for load detect or similar.
52 */
53
54 /**
55 * drm_atomic_set_mode_for_crtc - set mode for CRTC
56 * @state: the CRTC whose incoming state to update
57 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
58 *
59 * Set a mode (originating from the kernel) on the desired CRTC state and update
60 * the enable property.
61 *
62 * RETURNS:
63 * Zero on success, error code on failure. Cannot return -EDEADLK.
64 */
drm_atomic_set_mode_for_crtc(struct drm_crtc_state * state,const struct drm_display_mode * mode)65 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
66 const struct drm_display_mode *mode)
67 {
68 struct drm_crtc *crtc = state->crtc;
69 struct drm_mode_modeinfo umode;
70
71 /* Early return for no change. */
72 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
73 return 0;
74
75 drm_property_blob_put(state->mode_blob);
76 state->mode_blob = NULL;
77
78 if (mode) {
79 struct drm_property_blob *blob;
80
81 drm_mode_convert_to_umode(&umode, mode);
82 blob = drm_property_create_blob(crtc->dev,
83 sizeof(umode), &umode);
84 if (IS_ERR(blob))
85 return PTR_ERR(blob);
86
87 drm_mode_copy(&state->mode, mode);
88
89 state->mode_blob = blob;
90 state->enable = true;
91 drm_dbg_atomic(crtc->dev,
92 "Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
93 mode->name, crtc->base.id, crtc->name, state);
94 } else {
95 memset(&state->mode, 0, sizeof(state->mode));
96 state->enable = false;
97 drm_dbg_atomic(crtc->dev,
98 "Set [NOMODE] for [CRTC:%d:%s] state %p\n",
99 crtc->base.id, crtc->name, state);
100 }
101
102 return 0;
103 }
104 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
105
106 /**
107 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
108 * @state: the CRTC whose incoming state to update
109 * @blob: pointer to blob property to use for mode
110 *
111 * Set a mode (originating from a blob property) on the desired CRTC state.
112 * This function will take a reference on the blob property for the CRTC state,
113 * and release the reference held on the state's existing mode property, if any
114 * was set.
115 *
116 * RETURNS:
117 * Zero on success, error code on failure. Cannot return -EDEADLK.
118 */
drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state * state,struct drm_property_blob * blob)119 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
120 struct drm_property_blob *blob)
121 {
122 struct drm_crtc *crtc = state->crtc;
123
124 if (blob == state->mode_blob)
125 return 0;
126
127 drm_property_blob_put(state->mode_blob);
128 state->mode_blob = NULL;
129
130 memset(&state->mode, 0, sizeof(state->mode));
131
132 if (blob) {
133 int ret;
134
135 if (blob->length != sizeof(struct drm_mode_modeinfo)) {
136 drm_dbg_atomic(crtc->dev,
137 "[CRTC:%d:%s] bad mode blob length: %zu\n",
138 crtc->base.id, crtc->name,
139 blob->length);
140 return -EINVAL;
141 }
142
143 ret = drm_mode_convert_umode(crtc->dev,
144 &state->mode, blob->data);
145 if (ret) {
146 drm_dbg_atomic(crtc->dev,
147 "[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
148 crtc->base.id, crtc->name,
149 ret, drm_get_mode_status_name(state->mode.status));
150 drm_mode_debug_printmodeline(&state->mode);
151 return -EINVAL;
152 }
153
154 state->mode_blob = drm_property_blob_get(blob);
155 state->enable = true;
156 drm_dbg_atomic(crtc->dev,
157 "Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
158 state->mode.name, crtc->base.id, crtc->name,
159 state);
160 } else {
161 state->enable = false;
162 drm_dbg_atomic(crtc->dev,
163 "Set [NOMODE] for [CRTC:%d:%s] state %p\n",
164 crtc->base.id, crtc->name, state);
165 }
166
167 return 0;
168 }
169 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
170
171 /**
172 * drm_atomic_set_crtc_for_plane - set CRTC for plane
173 * @plane_state: the plane whose incoming state to update
174 * @crtc: CRTC to use for the plane
175 *
176 * Changing the assigned CRTC for a plane requires us to grab the lock and state
177 * for the new CRTC, as needed. This function takes care of all these details
178 * besides updating the pointer in the state object itself.
179 *
180 * Returns:
181 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
182 * then the w/w mutex code has detected a deadlock and the entire atomic
183 * sequence must be restarted. All other errors are fatal.
184 */
185 int
drm_atomic_set_crtc_for_plane(struct drm_plane_state * plane_state,struct drm_crtc * crtc)186 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
187 struct drm_crtc *crtc)
188 {
189 struct drm_plane *plane = plane_state->plane;
190 struct drm_crtc_state *crtc_state;
191 /* Nothing to do for same crtc*/
192 if (plane_state->crtc == crtc)
193 return 0;
194 if (plane_state->crtc) {
195 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
196 plane_state->crtc);
197 if (WARN_ON(IS_ERR(crtc_state)))
198 return PTR_ERR(crtc_state);
199
200 crtc_state->plane_mask &= ~drm_plane_mask(plane);
201 }
202
203 plane_state->crtc = crtc;
204
205 if (crtc) {
206 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
207 crtc);
208 if (IS_ERR(crtc_state))
209 return PTR_ERR(crtc_state);
210 crtc_state->plane_mask |= drm_plane_mask(plane);
211 }
212
213 if (crtc)
214 drm_dbg_atomic(plane->dev,
215 "Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
216 plane->base.id, plane->name, plane_state,
217 crtc->base.id, crtc->name);
218 else
219 drm_dbg_atomic(plane->dev,
220 "Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
221 plane->base.id, plane->name, plane_state);
222
223 return 0;
224 }
225 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
226
227 /**
228 * drm_atomic_set_fb_for_plane - set framebuffer for plane
229 * @plane_state: atomic state object for the plane
230 * @fb: fb to use for the plane
231 *
232 * Changing the assigned framebuffer for a plane requires us to grab a reference
233 * to the new fb and drop the reference to the old fb, if there is one. This
234 * function takes care of all these details besides updating the pointer in the
235 * state object itself.
236 */
237 void
drm_atomic_set_fb_for_plane(struct drm_plane_state * plane_state,struct drm_framebuffer * fb)238 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
239 struct drm_framebuffer *fb)
240 {
241 struct drm_plane *plane = plane_state->plane;
242
243 if (fb)
244 drm_dbg_atomic(plane->dev,
245 "Set [FB:%d] for [PLANE:%d:%s] state %p\n",
246 fb->base.id, plane->base.id, plane->name,
247 plane_state);
248 else
249 drm_dbg_atomic(plane->dev,
250 "Set [NOFB] for [PLANE:%d:%s] state %p\n",
251 plane->base.id, plane->name, plane_state);
252
253 drm_framebuffer_assign(&plane_state->fb, fb);
254 }
255 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
256
257 /**
258 * drm_atomic_set_fence_for_plane - set fence for plane
259 * @plane_state: atomic state object for the plane
260 * @fence: dma_fence to use for the plane
261 *
262 * Helper to setup the plane_state fence in case it is not set yet.
263 * By using this drivers doesn't need to worry if the user choose
264 * implicit or explicit fencing.
265 *
266 * This function will not set the fence to the state if it was set
267 * via explicit fencing interfaces on the atomic ioctl. In that case it will
268 * drop the reference to the fence as we are not storing it anywhere.
269 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
270 * with the received implicit fence. In both cases this function consumes a
271 * reference for @fence.
272 *
273 * This way explicit fencing can be used to overrule implicit fencing, which is
274 * important to make explicit fencing use-cases work: One example is using one
275 * buffer for 2 screens with different refresh rates. Implicit fencing will
276 * clamp rendering to the refresh rate of the slower screen, whereas explicit
277 * fence allows 2 independent render and display loops on a single buffer. If a
278 * driver allows obeys both implicit and explicit fences for plane updates, then
279 * it will break all the benefits of explicit fencing.
280 */
281 void
drm_atomic_set_fence_for_plane(struct drm_plane_state * plane_state,struct dma_fence * fence)282 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
283 struct dma_fence *fence)
284 {
285 if (plane_state->fence) {
286 dma_fence_put(fence);
287 return;
288 }
289
290 plane_state->fence = fence;
291 }
292 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
293
294 /**
295 * drm_atomic_set_crtc_for_connector - set CRTC for connector
296 * @conn_state: atomic state object for the connector
297 * @crtc: CRTC to use for the connector
298 *
299 * Changing the assigned CRTC for a connector requires us to grab the lock and
300 * state for the new CRTC, as needed. This function takes care of all these
301 * details besides updating the pointer in the state object itself.
302 *
303 * Returns:
304 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
305 * then the w/w mutex code has detected a deadlock and the entire atomic
306 * sequence must be restarted. All other errors are fatal.
307 */
308 int
drm_atomic_set_crtc_for_connector(struct drm_connector_state * conn_state,struct drm_crtc * crtc)309 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
310 struct drm_crtc *crtc)
311 {
312 struct drm_connector *connector = conn_state->connector;
313 struct drm_crtc_state *crtc_state;
314
315 if (conn_state->crtc == crtc)
316 return 0;
317
318 if (conn_state->crtc) {
319 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
320 conn_state->crtc);
321
322 crtc_state->connector_mask &=
323 ~drm_connector_mask(conn_state->connector);
324
325 drm_connector_put(conn_state->connector);
326 conn_state->crtc = NULL;
327 }
328
329 if (crtc) {
330 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
331 if (IS_ERR(crtc_state))
332 return PTR_ERR(crtc_state);
333
334 crtc_state->connector_mask |=
335 drm_connector_mask(conn_state->connector);
336
337 drm_connector_get(conn_state->connector);
338 conn_state->crtc = crtc;
339
340 drm_dbg_atomic(connector->dev,
341 "Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
342 connector->base.id, connector->name,
343 conn_state, crtc->base.id, crtc->name);
344 } else {
345 drm_dbg_atomic(connector->dev,
346 "Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
347 connector->base.id, connector->name,
348 conn_state);
349 }
350
351 return 0;
352 }
353 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
354
set_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc,s32 __user * fence_ptr)355 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
356 struct drm_crtc *crtc, s32 __user *fence_ptr)
357 {
358 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
359 }
360
get_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc)361 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
362 struct drm_crtc *crtc)
363 {
364 s32 __user *fence_ptr;
365
366 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
367 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
368
369 return fence_ptr;
370 }
371
set_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector,s32 __user * fence_ptr)372 static int set_out_fence_for_connector(struct drm_atomic_state *state,
373 struct drm_connector *connector,
374 s32 __user *fence_ptr)
375 {
376 unsigned int index = drm_connector_index(connector);
377
378 if (!fence_ptr)
379 return 0;
380
381 if (put_user(-1, fence_ptr))
382 return -EFAULT;
383
384 state->connectors[index].out_fence_ptr = fence_ptr;
385
386 return 0;
387 }
388
get_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector)389 static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
390 struct drm_connector *connector)
391 {
392 unsigned int index = drm_connector_index(connector);
393 s32 __user *fence_ptr;
394
395 fence_ptr = state->connectors[index].out_fence_ptr;
396 state->connectors[index].out_fence_ptr = NULL;
397
398 return fence_ptr;
399 }
400
401 static int
drm_atomic_replace_property_blob_from_id(struct drm_device * dev,struct drm_property_blob ** blob,uint64_t blob_id,ssize_t expected_size,ssize_t expected_elem_size,bool * replaced)402 drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
403 struct drm_property_blob **blob,
404 uint64_t blob_id,
405 ssize_t expected_size,
406 ssize_t expected_elem_size,
407 bool *replaced)
408 {
409 struct drm_property_blob *new_blob = NULL;
410
411 if (blob_id != 0) {
412 new_blob = drm_property_lookup_blob(dev, blob_id);
413 if (new_blob == NULL)
414 return -EINVAL;
415
416 if (expected_size > 0 &&
417 new_blob->length != expected_size) {
418 drm_property_blob_put(new_blob);
419 return -EINVAL;
420 }
421 if (expected_elem_size > 0 &&
422 new_blob->length % expected_elem_size != 0) {
423 drm_property_blob_put(new_blob);
424 return -EINVAL;
425 }
426 }
427
428 *replaced |= drm_property_replace_blob(blob, new_blob);
429 drm_property_blob_put(new_blob);
430
431 return 0;
432 }
433
drm_atomic_crtc_set_property(struct drm_crtc * crtc,struct drm_crtc_state * state,struct drm_property * property,uint64_t val)434 static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
435 struct drm_crtc_state *state, struct drm_property *property,
436 uint64_t val)
437 {
438 struct drm_device *dev = crtc->dev;
439 struct drm_mode_config *config = &dev->mode_config;
440 bool replaced = false;
441 int ret;
442
443 if (property == config->prop_active)
444 state->active = val;
445 else if (property == config->prop_mode_id) {
446 struct drm_property_blob *mode =
447 drm_property_lookup_blob(dev, val);
448 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
449 drm_property_blob_put(mode);
450 return ret;
451 } else if (property == config->prop_vrr_enabled) {
452 state->vrr_enabled = val;
453 } else if (property == config->degamma_lut_property) {
454 ret = drm_atomic_replace_property_blob_from_id(dev,
455 &state->degamma_lut,
456 val,
457 -1, sizeof(struct drm_color_lut),
458 &replaced);
459 state->color_mgmt_changed |= replaced;
460 return ret;
461 } else if (property == config->ctm_property) {
462 ret = drm_atomic_replace_property_blob_from_id(dev,
463 &state->ctm,
464 val,
465 sizeof(struct drm_color_ctm), -1,
466 &replaced);
467 state->color_mgmt_changed |= replaced;
468 return ret;
469 } else if (property == config->gamma_lut_property) {
470 ret = drm_atomic_replace_property_blob_from_id(dev,
471 &state->gamma_lut,
472 val,
473 -1, sizeof(struct drm_color_lut),
474 &replaced);
475 state->color_mgmt_changed |= replaced;
476 return ret;
477 } else if (property == config->prop_out_fence_ptr) {
478 s32 __user *fence_ptr = u64_to_user_ptr(val);
479
480 if (!fence_ptr)
481 return 0;
482
483 if (put_user(-1, fence_ptr))
484 return -EFAULT;
485
486 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
487 } else if (property == crtc->scaling_filter_property) {
488 state->scaling_filter = val;
489 } else if (crtc->funcs->atomic_set_property) {
490 return crtc->funcs->atomic_set_property(crtc, state, property, val);
491 } else {
492 drm_dbg_atomic(crtc->dev,
493 "[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
494 crtc->base.id, crtc->name,
495 property->base.id, property->name);
496 return -EINVAL;
497 }
498
499 return 0;
500 }
501
502 static int
drm_atomic_crtc_get_property(struct drm_crtc * crtc,const struct drm_crtc_state * state,struct drm_property * property,uint64_t * val)503 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
504 const struct drm_crtc_state *state,
505 struct drm_property *property, uint64_t *val)
506 {
507 struct drm_device *dev = crtc->dev;
508 struct drm_mode_config *config = &dev->mode_config;
509
510 if (property == config->prop_active)
511 *val = drm_atomic_crtc_effectively_active(state);
512 else if (property == config->prop_mode_id)
513 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
514 else if (property == config->prop_vrr_enabled)
515 *val = state->vrr_enabled;
516 else if (property == config->degamma_lut_property)
517 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
518 else if (property == config->ctm_property)
519 *val = (state->ctm) ? state->ctm->base.id : 0;
520 else if (property == config->gamma_lut_property)
521 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
522 else if (property == config->prop_out_fence_ptr)
523 *val = 0;
524 else if (property == crtc->scaling_filter_property)
525 *val = state->scaling_filter;
526 else if (crtc->funcs->atomic_get_property)
527 return crtc->funcs->atomic_get_property(crtc, state, property, val);
528 else
529 return -EINVAL;
530
531 return 0;
532 }
533
drm_atomic_plane_set_property(struct drm_plane * plane,struct drm_plane_state * state,struct drm_file * file_priv,struct drm_property * property,uint64_t val)534 static int drm_atomic_plane_set_property(struct drm_plane *plane,
535 struct drm_plane_state *state, struct drm_file *file_priv,
536 struct drm_property *property, uint64_t val)
537 {
538 struct drm_device *dev = plane->dev;
539 struct drm_mode_config *config = &dev->mode_config;
540 bool replaced = false;
541 int ret;
542
543 if (property == config->prop_fb_id) {
544 struct drm_framebuffer *fb;
545
546 fb = drm_framebuffer_lookup(dev, file_priv, val);
547 drm_atomic_set_fb_for_plane(state, fb);
548 if (fb)
549 drm_framebuffer_put(fb);
550 } else if (property == config->prop_in_fence_fd) {
551 if (state->fence)
552 return -EINVAL;
553
554 if (U642I64(val) == -1)
555 return 0;
556
557 state->fence = sync_file_get_fence(val);
558 if (!state->fence)
559 return -EINVAL;
560
561 } else if (property == config->prop_crtc_id) {
562 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
563
564 if (val && !crtc)
565 return -EACCES;
566 return drm_atomic_set_crtc_for_plane(state, crtc);
567 } else if (property == config->prop_crtc_x) {
568 state->crtc_x = U642I64(val);
569 } else if (property == config->prop_crtc_y) {
570 state->crtc_y = U642I64(val);
571 } else if (property == config->prop_crtc_w) {
572 state->crtc_w = val;
573 } else if (property == config->prop_crtc_h) {
574 state->crtc_h = val;
575 } else if (property == config->prop_src_x) {
576 state->src_x = val;
577 } else if (property == config->prop_src_y) {
578 state->src_y = val;
579 } else if (property == config->prop_src_w) {
580 state->src_w = val;
581 } else if (property == config->prop_src_h) {
582 state->src_h = val;
583 } else if (property == plane->alpha_property) {
584 state->alpha = val;
585 } else if (property == plane->blend_mode_property) {
586 state->pixel_blend_mode = val;
587 } else if (property == plane->rotation_property) {
588 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
589 drm_dbg_atomic(plane->dev,
590 "[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
591 plane->base.id, plane->name, val);
592 return -EINVAL;
593 }
594 state->rotation = val;
595 } else if (property == plane->zpos_property) {
596 state->zpos = val;
597 } else if (property == plane->color_encoding_property) {
598 state->color_encoding = val;
599 } else if (property == plane->color_range_property) {
600 state->color_range = val;
601 } else if (property == config->prop_fb_damage_clips) {
602 ret = drm_atomic_replace_property_blob_from_id(dev,
603 &state->fb_damage_clips,
604 val,
605 -1,
606 sizeof(struct drm_rect),
607 &replaced);
608 return ret;
609 } else if (property == plane->scaling_filter_property) {
610 state->scaling_filter = val;
611 } else if (plane->funcs->atomic_set_property) {
612 return plane->funcs->atomic_set_property(plane, state,
613 property, val);
614 } else {
615 drm_dbg_atomic(plane->dev,
616 "[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
617 plane->base.id, plane->name,
618 property->base.id, property->name);
619 return -EINVAL;
620 }
621
622 return 0;
623 }
624
625 static int
drm_atomic_plane_get_property(struct drm_plane * plane,const struct drm_plane_state * state,struct drm_property * property,uint64_t * val)626 drm_atomic_plane_get_property(struct drm_plane *plane,
627 const struct drm_plane_state *state,
628 struct drm_property *property, uint64_t *val)
629 {
630 struct drm_device *dev = plane->dev;
631 struct drm_mode_config *config = &dev->mode_config;
632
633 if (property == config->prop_fb_id) {
634 *val = (state->fb) ? state->fb->base.id : 0;
635 } else if (property == config->prop_in_fence_fd) {
636 *val = -1;
637 } else if (property == config->prop_crtc_id) {
638 *val = (state->crtc) ? state->crtc->base.id : 0;
639 } else if (property == config->prop_crtc_x) {
640 *val = I642U64(state->crtc_x);
641 } else if (property == config->prop_crtc_y) {
642 *val = I642U64(state->crtc_y);
643 } else if (property == config->prop_crtc_w) {
644 *val = state->crtc_w;
645 } else if (property == config->prop_crtc_h) {
646 *val = state->crtc_h;
647 } else if (property == config->prop_src_x) {
648 *val = state->src_x;
649 } else if (property == config->prop_src_y) {
650 *val = state->src_y;
651 } else if (property == config->prop_src_w) {
652 *val = state->src_w;
653 } else if (property == config->prop_src_h) {
654 *val = state->src_h;
655 } else if (property == plane->alpha_property) {
656 *val = state->alpha;
657 } else if (property == plane->blend_mode_property) {
658 *val = state->pixel_blend_mode;
659 } else if (property == plane->rotation_property) {
660 *val = state->rotation;
661 } else if (property == plane->zpos_property) {
662 *val = state->zpos;
663 } else if (property == plane->color_encoding_property) {
664 *val = state->color_encoding;
665 } else if (property == plane->color_range_property) {
666 *val = state->color_range;
667 } else if (property == config->prop_fb_damage_clips) {
668 *val = (state->fb_damage_clips) ?
669 state->fb_damage_clips->base.id : 0;
670 } else if (property == plane->scaling_filter_property) {
671 *val = state->scaling_filter;
672 } else if (plane->funcs->atomic_get_property) {
673 return plane->funcs->atomic_get_property(plane, state, property, val);
674 } else {
675 return -EINVAL;
676 }
677
678 return 0;
679 }
680
drm_atomic_set_writeback_fb_for_connector(struct drm_connector_state * conn_state,struct drm_framebuffer * fb)681 static int drm_atomic_set_writeback_fb_for_connector(
682 struct drm_connector_state *conn_state,
683 struct drm_framebuffer *fb)
684 {
685 int ret;
686 struct drm_connector *conn = conn_state->connector;
687
688 ret = drm_writeback_set_fb(conn_state, fb);
689 if (ret < 0)
690 return ret;
691
692 if (fb)
693 drm_dbg_atomic(conn->dev,
694 "Set [FB:%d] for connector state %p\n",
695 fb->base.id, conn_state);
696 else
697 drm_dbg_atomic(conn->dev,
698 "Set [NOFB] for connector state %p\n",
699 conn_state);
700
701 return 0;
702 }
703
drm_atomic_connector_set_property(struct drm_connector * connector,struct drm_connector_state * state,struct drm_file * file_priv,struct drm_property * property,uint64_t val)704 static int drm_atomic_connector_set_property(struct drm_connector *connector,
705 struct drm_connector_state *state, struct drm_file *file_priv,
706 struct drm_property *property, uint64_t val)
707 {
708 struct drm_device *dev = connector->dev;
709 struct drm_mode_config *config = &dev->mode_config;
710 bool replaced = false;
711 int ret;
712
713 if (property == config->prop_crtc_id) {
714 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
715
716 if (val && !crtc)
717 return -EACCES;
718 return drm_atomic_set_crtc_for_connector(state, crtc);
719 } else if (property == config->dpms_property) {
720 /* setting DPMS property requires special handling, which
721 * is done in legacy setprop path for us. Disallow (for
722 * now?) atomic writes to DPMS property:
723 */
724 return -EINVAL;
725 } else if (property == config->tv_select_subconnector_property) {
726 state->tv.subconnector = val;
727 } else if (property == config->tv_left_margin_property) {
728 state->tv.margins.left = val;
729 } else if (property == config->tv_right_margin_property) {
730 state->tv.margins.right = val;
731 } else if (property == config->tv_top_margin_property) {
732 state->tv.margins.top = val;
733 } else if (property == config->tv_bottom_margin_property) {
734 state->tv.margins.bottom = val;
735 } else if (property == config->tv_mode_property) {
736 state->tv.mode = val;
737 } else if (property == config->tv_brightness_property) {
738 state->tv.brightness = val;
739 } else if (property == config->tv_contrast_property) {
740 state->tv.contrast = val;
741 } else if (property == config->tv_flicker_reduction_property) {
742 state->tv.flicker_reduction = val;
743 } else if (property == config->tv_overscan_property) {
744 state->tv.overscan = val;
745 } else if (property == config->tv_saturation_property) {
746 state->tv.saturation = val;
747 } else if (property == config->tv_hue_property) {
748 state->tv.hue = val;
749 } else if (property == config->link_status_property) {
750 /* Never downgrade from GOOD to BAD on userspace's request here,
751 * only hw issues can do that.
752 *
753 * For an atomic property the userspace doesn't need to be able
754 * to understand all the properties, but needs to be able to
755 * restore the state it wants on VT switch. So if the userspace
756 * tries to change the link_status from GOOD to BAD, driver
757 * silently rejects it and returns a 0. This prevents userspace
758 * from accidentally breaking the display when it restores the
759 * state.
760 */
761 if (state->link_status != DRM_LINK_STATUS_GOOD)
762 state->link_status = val;
763 } else if (property == config->hdr_output_metadata_property) {
764 ret = drm_atomic_replace_property_blob_from_id(dev,
765 &state->hdr_output_metadata,
766 val,
767 sizeof(struct hdr_output_metadata), -1,
768 &replaced);
769 return ret;
770 } else if (property == config->aspect_ratio_property) {
771 state->picture_aspect_ratio = val;
772 } else if (property == config->content_type_property) {
773 state->content_type = val;
774 } else if (property == connector->scaling_mode_property) {
775 state->scaling_mode = val;
776 } else if (property == config->content_protection_property) {
777 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
778 DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
779 return -EINVAL;
780 }
781 state->content_protection = val;
782 } else if (property == config->hdcp_content_type_property) {
783 state->hdcp_content_type = val;
784 } else if (property == connector->colorspace_property) {
785 state->colorspace = val;
786 } else if (property == config->writeback_fb_id_property) {
787 struct drm_framebuffer *fb;
788 int ret;
789
790 fb = drm_framebuffer_lookup(dev, file_priv, val);
791 ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
792 if (fb)
793 drm_framebuffer_put(fb);
794 return ret;
795 } else if (property == config->writeback_out_fence_ptr_property) {
796 s32 __user *fence_ptr = u64_to_user_ptr(val);
797
798 return set_out_fence_for_connector(state->state, connector,
799 fence_ptr);
800 } else if (property == connector->max_bpc_property) {
801 state->max_requested_bpc = val;
802 } else if (connector->funcs->atomic_set_property) {
803 return connector->funcs->atomic_set_property(connector,
804 state, property, val);
805 } else {
806 drm_dbg_atomic(connector->dev,
807 "[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
808 connector->base.id, connector->name,
809 property->base.id, property->name);
810 return -EINVAL;
811 }
812
813 return 0;
814 }
815
816 static int
drm_atomic_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)817 drm_atomic_connector_get_property(struct drm_connector *connector,
818 const struct drm_connector_state *state,
819 struct drm_property *property, uint64_t *val)
820 {
821 struct drm_device *dev = connector->dev;
822 struct drm_mode_config *config = &dev->mode_config;
823
824 if (property == config->prop_crtc_id) {
825 *val = (state->crtc) ? state->crtc->base.id : 0;
826 } else if (property == config->dpms_property) {
827 if (state->crtc && state->crtc->state->self_refresh_active)
828 *val = DRM_MODE_DPMS_ON;
829 else
830 *val = connector->dpms;
831 } else if (property == config->tv_select_subconnector_property) {
832 *val = state->tv.subconnector;
833 } else if (property == config->tv_left_margin_property) {
834 *val = state->tv.margins.left;
835 } else if (property == config->tv_right_margin_property) {
836 *val = state->tv.margins.right;
837 } else if (property == config->tv_top_margin_property) {
838 *val = state->tv.margins.top;
839 } else if (property == config->tv_bottom_margin_property) {
840 *val = state->tv.margins.bottom;
841 } else if (property == config->tv_mode_property) {
842 *val = state->tv.mode;
843 } else if (property == config->tv_brightness_property) {
844 *val = state->tv.brightness;
845 } else if (property == config->tv_contrast_property) {
846 *val = state->tv.contrast;
847 } else if (property == config->tv_flicker_reduction_property) {
848 *val = state->tv.flicker_reduction;
849 } else if (property == config->tv_overscan_property) {
850 *val = state->tv.overscan;
851 } else if (property == config->tv_saturation_property) {
852 *val = state->tv.saturation;
853 } else if (property == config->tv_hue_property) {
854 *val = state->tv.hue;
855 } else if (property == config->link_status_property) {
856 *val = state->link_status;
857 } else if (property == config->aspect_ratio_property) {
858 *val = state->picture_aspect_ratio;
859 } else if (property == config->content_type_property) {
860 *val = state->content_type;
861 } else if (property == connector->colorspace_property) {
862 *val = state->colorspace;
863 } else if (property == connector->scaling_mode_property) {
864 *val = state->scaling_mode;
865 } else if (property == config->hdr_output_metadata_property) {
866 *val = state->hdr_output_metadata ?
867 state->hdr_output_metadata->base.id : 0;
868 } else if (property == config->content_protection_property) {
869 *val = state->content_protection;
870 } else if (property == config->hdcp_content_type_property) {
871 *val = state->hdcp_content_type;
872 } else if (property == config->writeback_fb_id_property) {
873 /* Writeback framebuffer is one-shot, write and forget */
874 *val = 0;
875 } else if (property == config->writeback_out_fence_ptr_property) {
876 *val = 0;
877 } else if (property == connector->max_bpc_property) {
878 *val = state->max_requested_bpc;
879 } else if (connector->funcs->atomic_get_property) {
880 return connector->funcs->atomic_get_property(connector,
881 state, property, val);
882 } else {
883 return -EINVAL;
884 }
885
886 return 0;
887 }
888
drm_atomic_get_property(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)889 int drm_atomic_get_property(struct drm_mode_object *obj,
890 struct drm_property *property, uint64_t *val)
891 {
892 struct drm_device *dev = property->dev;
893 int ret;
894
895 switch (obj->type) {
896 case DRM_MODE_OBJECT_CONNECTOR: {
897 struct drm_connector *connector = obj_to_connector(obj);
898
899 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
900 ret = drm_atomic_connector_get_property(connector,
901 connector->state, property, val);
902 break;
903 }
904 case DRM_MODE_OBJECT_CRTC: {
905 struct drm_crtc *crtc = obj_to_crtc(obj);
906
907 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
908 ret = drm_atomic_crtc_get_property(crtc,
909 crtc->state, property, val);
910 break;
911 }
912 case DRM_MODE_OBJECT_PLANE: {
913 struct drm_plane *plane = obj_to_plane(obj);
914
915 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
916 ret = drm_atomic_plane_get_property(plane,
917 plane->state, property, val);
918 break;
919 }
920 default:
921 ret = -EINVAL;
922 break;
923 }
924
925 return ret;
926 }
927
928 /*
929 * The big monster ioctl
930 */
931
create_vblank_event(struct drm_crtc * crtc,uint64_t user_data)932 static struct drm_pending_vblank_event *create_vblank_event(
933 struct drm_crtc *crtc, uint64_t user_data)
934 {
935 struct drm_pending_vblank_event *e = NULL;
936
937 e = kzalloc(sizeof *e, GFP_KERNEL);
938 if (!e)
939 return NULL;
940
941 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
942 e->event.base.length = sizeof(e->event);
943 e->event.vbl.crtc_id = crtc->base.id;
944 e->event.vbl.user_data = user_data;
945
946 return e;
947 }
948
drm_atomic_connector_commit_dpms(struct drm_atomic_state * state,struct drm_connector * connector,int mode)949 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
950 struct drm_connector *connector,
951 int mode)
952 {
953 struct drm_connector *tmp_connector;
954 struct drm_connector_state *new_conn_state;
955 struct drm_crtc *crtc;
956 struct drm_crtc_state *crtc_state;
957 int i, ret, old_mode = connector->dpms;
958 bool active = false;
959
960 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
961 state->acquire_ctx);
962 if (ret)
963 return ret;
964
965 if (mode != DRM_MODE_DPMS_ON)
966 mode = DRM_MODE_DPMS_OFF;
967 connector->dpms = mode;
968
969 crtc = connector->state->crtc;
970 if (!crtc)
971 goto out;
972 ret = drm_atomic_add_affected_connectors(state, crtc);
973 if (ret)
974 goto out;
975
976 crtc_state = drm_atomic_get_crtc_state(state, crtc);
977 if (IS_ERR(crtc_state)) {
978 ret = PTR_ERR(crtc_state);
979 goto out;
980 }
981
982 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
983 if (new_conn_state->crtc != crtc)
984 continue;
985 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
986 active = true;
987 break;
988 }
989 }
990
991 crtc_state->active = active;
992 ret = drm_atomic_commit(state);
993 out:
994 if (ret != 0)
995 connector->dpms = old_mode;
996 return ret;
997 }
998
drm_atomic_set_property(struct drm_atomic_state * state,struct drm_file * file_priv,struct drm_mode_object * obj,struct drm_property * prop,uint64_t prop_value)999 int drm_atomic_set_property(struct drm_atomic_state *state,
1000 struct drm_file *file_priv,
1001 struct drm_mode_object *obj,
1002 struct drm_property *prop,
1003 uint64_t prop_value)
1004 {
1005 struct drm_mode_object *ref;
1006 int ret;
1007
1008 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1009 return -EINVAL;
1010
1011 switch (obj->type) {
1012 case DRM_MODE_OBJECT_CONNECTOR: {
1013 struct drm_connector *connector = obj_to_connector(obj);
1014 struct drm_connector_state *connector_state;
1015
1016 connector_state = drm_atomic_get_connector_state(state, connector);
1017 if (IS_ERR(connector_state)) {
1018 ret = PTR_ERR(connector_state);
1019 break;
1020 }
1021
1022 ret = drm_atomic_connector_set_property(connector,
1023 connector_state, file_priv,
1024 prop, prop_value);
1025 break;
1026 }
1027 case DRM_MODE_OBJECT_CRTC: {
1028 struct drm_crtc *crtc = obj_to_crtc(obj);
1029 struct drm_crtc_state *crtc_state;
1030
1031 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1032 if (IS_ERR(crtc_state)) {
1033 ret = PTR_ERR(crtc_state);
1034 break;
1035 }
1036
1037 ret = drm_atomic_crtc_set_property(crtc,
1038 crtc_state, prop, prop_value);
1039 break;
1040 }
1041 case DRM_MODE_OBJECT_PLANE: {
1042 struct drm_plane *plane = obj_to_plane(obj);
1043 struct drm_plane_state *plane_state;
1044
1045 plane_state = drm_atomic_get_plane_state(state, plane);
1046 if (IS_ERR(plane_state)) {
1047 ret = PTR_ERR(plane_state);
1048 break;
1049 }
1050
1051 ret = drm_atomic_plane_set_property(plane,
1052 plane_state, file_priv,
1053 prop, prop_value);
1054 break;
1055 }
1056 default:
1057 ret = -EINVAL;
1058 break;
1059 }
1060
1061 drm_property_change_valid_put(prop, ref);
1062 return ret;
1063 }
1064
1065 /**
1066 * DOC: explicit fencing properties
1067 *
1068 * Explicit fencing allows userspace to control the buffer synchronization
1069 * between devices. A Fence or a group of fences are transferred to/from
1070 * userspace using Sync File fds and there are two DRM properties for that.
1071 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1072 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1073 *
1074 * As a contrast, with implicit fencing the kernel keeps track of any
1075 * ongoing rendering, and automatically ensures that the atomic update waits
1076 * for any pending rendering to complete. For shared buffers represented with
1077 * a &struct dma_buf this is tracked in &struct dma_resv.
1078 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1079 * whereas explicit fencing is what Android wants.
1080 *
1081 * "IN_FENCE_FD”:
1082 * Use this property to pass a fence that DRM should wait on before
1083 * proceeding with the Atomic Commit request and show the framebuffer for
1084 * the plane on the screen. The fence can be either a normal fence or a
1085 * merged one, the sync_file framework will handle both cases and use a
1086 * fence_array if a merged fence is received. Passing -1 here means no
1087 * fences to wait on.
1088 *
1089 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1090 * it will only check if the Sync File is a valid one.
1091 *
1092 * On the driver side the fence is stored on the @fence parameter of
1093 * &struct drm_plane_state. Drivers which also support implicit fencing
1094 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1095 * to make sure there's consistent behaviour between drivers in precedence
1096 * of implicit vs. explicit fencing.
1097 *
1098 * "OUT_FENCE_PTR”:
1099 * Use this property to pass a file descriptor pointer to DRM. Once the
1100 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1101 * the file descriptor number of a Sync File. This Sync File contains the
1102 * CRTC fence that will be signaled when all framebuffers present on the
1103 * Atomic Commit * request for that given CRTC are scanned out on the
1104 * screen.
1105 *
1106 * The Atomic Commit request fails if a invalid pointer is passed. If the
1107 * Atomic Commit request fails for any other reason the out fence fd
1108 * returned will be -1. On a Atomic Commit with the
1109 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1110 *
1111 * Note that out-fences don't have a special interface to drivers and are
1112 * internally represented by a &struct drm_pending_vblank_event in struct
1113 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1114 * helpers and for the DRM event handling for existing userspace.
1115 */
1116
1117 struct drm_out_fence_state {
1118 s32 __user *out_fence_ptr;
1119 struct sync_file *sync_file;
1120 int fd;
1121 };
1122
setup_out_fence(struct drm_out_fence_state * fence_state,struct dma_fence * fence)1123 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1124 struct dma_fence *fence)
1125 {
1126 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1127 if (fence_state->fd < 0)
1128 return fence_state->fd;
1129
1130 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1131 return -EFAULT;
1132
1133 fence_state->sync_file = sync_file_create(fence);
1134 if (!fence_state->sync_file)
1135 return -ENOMEM;
1136
1137 return 0;
1138 }
1139
prepare_signaling(struct drm_device * dev,struct drm_atomic_state * state,struct drm_mode_atomic * arg,struct drm_file * file_priv,struct drm_out_fence_state ** fence_state,unsigned int * num_fences)1140 static int prepare_signaling(struct drm_device *dev,
1141 struct drm_atomic_state *state,
1142 struct drm_mode_atomic *arg,
1143 struct drm_file *file_priv,
1144 struct drm_out_fence_state **fence_state,
1145 unsigned int *num_fences)
1146 {
1147 struct drm_crtc *crtc;
1148 struct drm_crtc_state *crtc_state;
1149 struct drm_connector *conn;
1150 struct drm_connector_state *conn_state;
1151 int i, c = 0, ret;
1152
1153 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1154 return 0;
1155
1156 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1157 s32 __user *fence_ptr;
1158
1159 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1160
1161 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1162 struct drm_pending_vblank_event *e;
1163
1164 e = create_vblank_event(crtc, arg->user_data);
1165 if (!e)
1166 return -ENOMEM;
1167
1168 crtc_state->event = e;
1169 }
1170
1171 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1172 struct drm_pending_vblank_event *e = crtc_state->event;
1173
1174 if (!file_priv)
1175 continue;
1176
1177 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1178 &e->event.base);
1179 if (ret) {
1180 kfree(e);
1181 crtc_state->event = NULL;
1182 return ret;
1183 }
1184 }
1185
1186 if (fence_ptr) {
1187 struct dma_fence *fence;
1188 struct drm_out_fence_state *f;
1189
1190 f = krealloc(*fence_state, sizeof(**fence_state) *
1191 (*num_fences + 1), GFP_KERNEL);
1192 if (!f)
1193 return -ENOMEM;
1194
1195 memset(&f[*num_fences], 0, sizeof(*f));
1196
1197 f[*num_fences].out_fence_ptr = fence_ptr;
1198 *fence_state = f;
1199
1200 fence = drm_crtc_create_fence(crtc);
1201 if (!fence)
1202 return -ENOMEM;
1203
1204 ret = setup_out_fence(&f[(*num_fences)++], fence);
1205 if (ret) {
1206 dma_fence_put(fence);
1207 return ret;
1208 }
1209
1210 crtc_state->event->base.fence = fence;
1211 }
1212
1213 c++;
1214 }
1215
1216 for_each_new_connector_in_state(state, conn, conn_state, i) {
1217 struct drm_writeback_connector *wb_conn;
1218 struct drm_out_fence_state *f;
1219 struct dma_fence *fence;
1220 s32 __user *fence_ptr;
1221
1222 if (!conn_state->writeback_job)
1223 continue;
1224
1225 fence_ptr = get_out_fence_for_connector(state, conn);
1226 if (!fence_ptr)
1227 continue;
1228
1229 f = krealloc(*fence_state, sizeof(**fence_state) *
1230 (*num_fences + 1), GFP_KERNEL);
1231 if (!f)
1232 return -ENOMEM;
1233
1234 memset(&f[*num_fences], 0, sizeof(*f));
1235
1236 f[*num_fences].out_fence_ptr = fence_ptr;
1237 *fence_state = f;
1238
1239 wb_conn = drm_connector_to_writeback(conn);
1240 fence = drm_writeback_get_out_fence(wb_conn);
1241 if (!fence)
1242 return -ENOMEM;
1243
1244 ret = setup_out_fence(&f[(*num_fences)++], fence);
1245 if (ret) {
1246 dma_fence_put(fence);
1247 return ret;
1248 }
1249
1250 conn_state->writeback_job->out_fence = fence;
1251 }
1252
1253 /*
1254 * Having this flag means user mode pends on event which will never
1255 * reach due to lack of at least one CRTC for signaling
1256 */
1257 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1258 return -EINVAL;
1259
1260 return 0;
1261 }
1262
complete_signaling(struct drm_device * dev,struct drm_atomic_state * state,struct drm_out_fence_state * fence_state,unsigned int num_fences,bool install_fds)1263 static void complete_signaling(struct drm_device *dev,
1264 struct drm_atomic_state *state,
1265 struct drm_out_fence_state *fence_state,
1266 unsigned int num_fences,
1267 bool install_fds)
1268 {
1269 struct drm_crtc *crtc;
1270 struct drm_crtc_state *crtc_state;
1271 int i;
1272
1273 if (install_fds) {
1274 for (i = 0; i < num_fences; i++)
1275 fd_install(fence_state[i].fd,
1276 fence_state[i].sync_file->file);
1277
1278 kfree(fence_state);
1279 return;
1280 }
1281
1282 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1283 struct drm_pending_vblank_event *event = crtc_state->event;
1284 /*
1285 * Free the allocated event. drm_atomic_helper_setup_commit
1286 * can allocate an event too, so only free it if it's ours
1287 * to prevent a double free in drm_atomic_state_clear.
1288 */
1289 if (event && (event->base.fence || event->base.file_priv)) {
1290 drm_event_cancel_free(dev, &event->base);
1291 crtc_state->event = NULL;
1292 }
1293 }
1294
1295 if (!fence_state)
1296 return;
1297
1298 for (i = 0; i < num_fences; i++) {
1299 if (fence_state[i].sync_file)
1300 fput(fence_state[i].sync_file->file);
1301 if (fence_state[i].fd >= 0)
1302 put_unused_fd(fence_state[i].fd);
1303
1304 /* If this fails log error to the user */
1305 if (fence_state[i].out_fence_ptr &&
1306 put_user(-1, fence_state[i].out_fence_ptr))
1307 drm_dbg_atomic(dev, "Couldn't clear out_fence_ptr\n");
1308 }
1309
1310 kfree(fence_state);
1311 }
1312
drm_mode_atomic_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1313 int drm_mode_atomic_ioctl(struct drm_device *dev,
1314 void *data, struct drm_file *file_priv)
1315 {
1316 struct drm_mode_atomic *arg = data;
1317 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1318 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1319 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1320 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1321 unsigned int copied_objs, copied_props;
1322 struct drm_atomic_state *state;
1323 struct drm_modeset_acquire_ctx ctx;
1324 struct drm_out_fence_state *fence_state;
1325 int ret = 0;
1326 unsigned int i, j, num_fences;
1327 struct drm_printer p = drm_info_printer(dev->dev);
1328
1329 /* disallow for drivers not supporting atomic: */
1330 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1331 return -EOPNOTSUPP;
1332
1333 /* disallow for userspace that has not enabled atomic cap (even
1334 * though this may be a bit overkill, since legacy userspace
1335 * wouldn't know how to call this ioctl)
1336 */
1337 if (!file_priv->atomic) {
1338 drm_dbg_atomic(dev,
1339 "commit failed: atomic cap not enabled\n");
1340 return -EINVAL;
1341 }
1342
1343 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) {
1344 drm_dbg_atomic(dev, "commit failed: invalid flag\n");
1345 return -EINVAL;
1346 }
1347
1348 if (arg->reserved) {
1349 drm_dbg_atomic(dev, "commit failed: reserved field set\n");
1350 return -EINVAL;
1351 }
1352
1353 if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) {
1354 drm_dbg_atomic(dev,
1355 "commit failed: invalid flag DRM_MODE_PAGE_FLIP_ASYNC\n");
1356 return -EINVAL;
1357 }
1358
1359 /* can't test and expect an event at the same time. */
1360 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1361 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
1362 drm_dbg_atomic(dev,
1363 "commit failed: page-flip event requested with test-only commit\n");
1364 return -EINVAL;
1365 }
1366
1367 state = drm_atomic_state_alloc(dev);
1368 if (!state)
1369 return -ENOMEM;
1370
1371 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1372 state->acquire_ctx = &ctx;
1373 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1374
1375 retry:
1376 copied_objs = 0;
1377 copied_props = 0;
1378 fence_state = NULL;
1379 num_fences = 0;
1380
1381 for (i = 0; i < arg->count_objs; i++) {
1382 uint32_t obj_id, count_props;
1383 struct drm_mode_object *obj;
1384
1385 if (get_user(obj_id, objs_ptr + copied_objs)) {
1386 ret = -EFAULT;
1387 goto out;
1388 }
1389
1390 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1391 if (!obj) {
1392 ret = -ENOENT;
1393 goto out;
1394 }
1395
1396 if (!obj->properties) {
1397 drm_mode_object_put(obj);
1398 ret = -ENOENT;
1399 goto out;
1400 }
1401
1402 if (get_user(count_props, count_props_ptr + copied_objs)) {
1403 drm_mode_object_put(obj);
1404 ret = -EFAULT;
1405 goto out;
1406 }
1407
1408 copied_objs++;
1409
1410 for (j = 0; j < count_props; j++) {
1411 uint32_t prop_id;
1412 uint64_t prop_value;
1413 struct drm_property *prop;
1414
1415 if (get_user(prop_id, props_ptr + copied_props)) {
1416 drm_mode_object_put(obj);
1417 ret = -EFAULT;
1418 goto out;
1419 }
1420
1421 prop = drm_mode_obj_find_prop_id(obj, prop_id);
1422 if (!prop) {
1423 drm_mode_object_put(obj);
1424 ret = -ENOENT;
1425 goto out;
1426 }
1427
1428 if (copy_from_user(&prop_value,
1429 prop_values_ptr + copied_props,
1430 sizeof(prop_value))) {
1431 drm_mode_object_put(obj);
1432 ret = -EFAULT;
1433 goto out;
1434 }
1435
1436 ret = drm_atomic_set_property(state, file_priv,
1437 obj, prop, prop_value);
1438 if (ret) {
1439 drm_mode_object_put(obj);
1440 goto out;
1441 }
1442
1443 copied_props++;
1444 }
1445
1446 drm_mode_object_put(obj);
1447 }
1448
1449 ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1450 &num_fences);
1451 if (ret)
1452 goto out;
1453
1454 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1455 ret = drm_atomic_check_only(state);
1456 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1457 ret = drm_atomic_nonblocking_commit(state);
1458 } else {
1459 if (drm_debug_enabled(DRM_UT_STATE))
1460 drm_atomic_print_new_state(state, &p);
1461
1462 ret = drm_atomic_commit(state);
1463 }
1464
1465 out:
1466 complete_signaling(dev, state, fence_state, num_fences, !ret);
1467
1468 if (ret == -EDEADLK) {
1469 drm_atomic_state_clear(state);
1470 ret = drm_modeset_backoff(&ctx);
1471 if (!ret)
1472 goto retry;
1473 }
1474
1475 drm_atomic_state_put(state);
1476
1477 drm_modeset_drop_locks(&ctx);
1478 drm_modeset_acquire_fini(&ctx);
1479
1480 return ret;
1481 }
1482