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