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.h>
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_atomic_uapi.h>
33 #include <drm/drm_framebuffer.h>
34 #include <drm/drm_print.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_writeback.h>
37 #include <drm/drm_vblank.h>
38 #include <drm/drm_colorop.h>
39
40 #include <linux/export.h>
41 #include <linux/dma-fence.h>
42 #include <linux/uaccess.h>
43 #include <linux/sync_file.h>
44 #include <linux/file.h>
45
46 #include "drm_crtc_internal.h"
47
48 /**
49 * DOC: overview
50 *
51 * This file contains the marshalling and demarshalling glue for the atomic UAPI
52 * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and
53 * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and
54 * drivers which have special needs to construct their own atomic updates, e.g.
55 * for load detect or similar.
56 */
57
58 /**
59 * drm_atomic_set_mode_for_crtc - set mode for CRTC
60 * @state: the CRTC whose incoming state to update
61 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
62 *
63 * Set a mode (originating from the kernel) on the desired CRTC state and update
64 * the enable property.
65 *
66 * RETURNS:
67 * Zero on success, error code on failure. Cannot return -EDEADLK.
68 */
drm_atomic_set_mode_for_crtc(struct drm_crtc_state * state,const struct drm_display_mode * mode)69 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
70 const struct drm_display_mode *mode)
71 {
72 struct drm_crtc *crtc = state->crtc;
73 struct drm_mode_modeinfo umode;
74
75 /* Early return for no change. */
76 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
77 return 0;
78
79 drm_property_blob_put(state->mode_blob);
80 state->mode_blob = NULL;
81
82 if (mode) {
83 struct drm_property_blob *blob;
84
85 drm_mode_convert_to_umode(&umode, mode);
86 blob = drm_property_create_blob(crtc->dev,
87 sizeof(umode), &umode);
88 if (IS_ERR(blob))
89 return PTR_ERR(blob);
90
91 drm_mode_copy(&state->mode, mode);
92
93 state->mode_blob = blob;
94 state->enable = true;
95 drm_dbg_atomic(crtc->dev,
96 "Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
97 mode->name, crtc->base.id, crtc->name, state);
98 } else {
99 memset(&state->mode, 0, sizeof(state->mode));
100 state->enable = false;
101 drm_dbg_atomic(crtc->dev,
102 "Set [NOMODE] for [CRTC:%d:%s] state %p\n",
103 crtc->base.id, crtc->name, state);
104 }
105
106 return 0;
107 }
108 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
109
110 /**
111 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
112 * @state: the CRTC whose incoming state to update
113 * @blob: pointer to blob property to use for mode
114 *
115 * Set a mode (originating from a blob property) on the desired CRTC state.
116 * This function will take a reference on the blob property for the CRTC state,
117 * and release the reference held on the state's existing mode property, if any
118 * was set.
119 *
120 * RETURNS:
121 * Zero on success, error code on failure. Cannot return -EDEADLK.
122 */
drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state * state,struct drm_property_blob * blob)123 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
124 struct drm_property_blob *blob)
125 {
126 struct drm_crtc *crtc = state->crtc;
127
128 if (blob == state->mode_blob)
129 return 0;
130
131 drm_property_blob_put(state->mode_blob);
132 state->mode_blob = NULL;
133
134 memset(&state->mode, 0, sizeof(state->mode));
135
136 if (blob) {
137 int ret;
138
139 if (blob->length != sizeof(struct drm_mode_modeinfo)) {
140 drm_dbg_atomic(crtc->dev,
141 "[CRTC:%d:%s] bad mode blob length: %zu\n",
142 crtc->base.id, crtc->name,
143 blob->length);
144 return -EINVAL;
145 }
146
147 ret = drm_mode_convert_umode(crtc->dev,
148 &state->mode, blob->data);
149 if (ret) {
150 drm_dbg_atomic(crtc->dev,
151 "[CRTC:%d:%s] invalid mode (%s, %pe): " DRM_MODE_FMT "\n",
152 crtc->base.id, crtc->name,
153 drm_get_mode_status_name(state->mode.status),
154 ERR_PTR(ret), DRM_MODE_ARG(&state->mode));
155 return -EINVAL;
156 }
157
158 state->mode_blob = drm_property_blob_get(blob);
159 state->enable = true;
160 drm_dbg_atomic(crtc->dev,
161 "Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
162 state->mode.name, crtc->base.id, crtc->name,
163 state);
164 } else {
165 state->enable = false;
166 drm_dbg_atomic(crtc->dev,
167 "Set [NOMODE] for [CRTC:%d:%s] state %p\n",
168 crtc->base.id, crtc->name, state);
169 }
170
171 return 0;
172 }
173 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
174
175 /**
176 * drm_atomic_set_crtc_for_plane - set CRTC for plane
177 * @plane_state: the plane whose incoming state to update
178 * @crtc: CRTC to use for the plane
179 *
180 * Changing the assigned CRTC for a plane requires us to grab the lock and state
181 * for the new CRTC, as needed. This function takes care of all these details
182 * besides updating the pointer in the state object itself.
183 *
184 * Returns:
185 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
186 * then the w/w mutex code has detected a deadlock and the entire atomic
187 * sequence must be restarted. All other errors are fatal.
188 */
189 int
drm_atomic_set_crtc_for_plane(struct drm_plane_state * plane_state,struct drm_crtc * crtc)190 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
191 struct drm_crtc *crtc)
192 {
193 struct drm_plane *plane = plane_state->plane;
194 struct drm_crtc_state *crtc_state;
195 /* Nothing to do for same crtc*/
196 if (plane_state->crtc == crtc)
197 return 0;
198 if (plane_state->crtc) {
199 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
200 plane_state->crtc);
201 if (WARN_ON(IS_ERR(crtc_state)))
202 return PTR_ERR(crtc_state);
203
204 crtc_state->plane_mask &= ~drm_plane_mask(plane);
205 }
206
207 plane_state->crtc = crtc;
208
209 if (crtc) {
210 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
211 crtc);
212 if (IS_ERR(crtc_state))
213 return PTR_ERR(crtc_state);
214 crtc_state->plane_mask |= drm_plane_mask(plane);
215 }
216
217 if (crtc)
218 drm_dbg_atomic(plane->dev,
219 "Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
220 plane->base.id, plane->name, plane_state,
221 crtc->base.id, crtc->name);
222 else
223 drm_dbg_atomic(plane->dev,
224 "Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
225 plane->base.id, plane->name, plane_state);
226
227 return 0;
228 }
229 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
230
231 /**
232 * drm_atomic_set_fb_for_plane - set framebuffer for plane
233 * @plane_state: atomic state object for the plane
234 * @fb: fb to use for the plane
235 *
236 * Changing the assigned framebuffer for a plane requires us to grab a reference
237 * to the new fb and drop the reference to the old fb, if there is one. This
238 * function takes care of all these details besides updating the pointer in the
239 * state object itself.
240 */
241 void
drm_atomic_set_fb_for_plane(struct drm_plane_state * plane_state,struct drm_framebuffer * fb)242 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
243 struct drm_framebuffer *fb)
244 {
245 struct drm_plane *plane = plane_state->plane;
246
247 if (fb)
248 drm_dbg_atomic(plane->dev,
249 "Set [FB:%d] for [PLANE:%d:%s] state %p\n",
250 fb->base.id, plane->base.id, plane->name,
251 plane_state);
252 else
253 drm_dbg_atomic(plane->dev,
254 "Set [NOFB] for [PLANE:%d:%s] state %p\n",
255 plane->base.id, plane->name, plane_state);
256
257 drm_framebuffer_assign(&plane_state->fb, fb);
258 }
259 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
260
261 /**
262 * drm_atomic_set_colorop_for_plane - set colorop for plane
263 * @plane_state: atomic state object for the plane
264 * @colorop: colorop to use for the plane
265 *
266 * Helper function to select the color pipeline on a plane by setting
267 * it to the first drm_colorop element of the pipeline.
268 */
269 void
drm_atomic_set_colorop_for_plane(struct drm_plane_state * plane_state,struct drm_colorop * colorop)270 drm_atomic_set_colorop_for_plane(struct drm_plane_state *plane_state,
271 struct drm_colorop *colorop)
272 {
273 struct drm_plane *plane = plane_state->plane;
274
275 if (colorop)
276 drm_dbg_atomic(plane->dev,
277 "Set [COLOROP:%d] for [PLANE:%d:%s] state %p\n",
278 colorop->base.id, plane->base.id, plane->name,
279 plane_state);
280 else
281 drm_dbg_atomic(plane->dev,
282 "Set [NOCOLOROP] for [PLANE:%d:%s] state %p\n",
283 plane->base.id, plane->name, plane_state);
284
285 plane_state->color_pipeline = colorop;
286 }
287 EXPORT_SYMBOL(drm_atomic_set_colorop_for_plane);
288
289 /**
290 * drm_atomic_set_crtc_for_connector - set CRTC for connector
291 * @conn_state: atomic state object for the connector
292 * @crtc: CRTC to use for the connector
293 *
294 * Changing the assigned CRTC for a connector requires us to grab the lock and
295 * state for the new CRTC, as needed. This function takes care of all these
296 * details besides updating the pointer in the state object itself.
297 *
298 * Returns:
299 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
300 * then the w/w mutex code has detected a deadlock and the entire atomic
301 * sequence must be restarted. All other errors are fatal.
302 */
303 int
drm_atomic_set_crtc_for_connector(struct drm_connector_state * conn_state,struct drm_crtc * crtc)304 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
305 struct drm_crtc *crtc)
306 {
307 struct drm_connector *connector = conn_state->connector;
308 struct drm_crtc_state *crtc_state;
309
310 if (conn_state->crtc == crtc)
311 return 0;
312
313 if (conn_state->crtc) {
314 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
315 conn_state->crtc);
316
317 crtc_state->connector_mask &=
318 ~drm_connector_mask(conn_state->connector);
319
320 drm_connector_put(conn_state->connector);
321 conn_state->crtc = NULL;
322 }
323
324 if (crtc) {
325 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
326 if (IS_ERR(crtc_state))
327 return PTR_ERR(crtc_state);
328
329 crtc_state->connector_mask |=
330 drm_connector_mask(conn_state->connector);
331
332 drm_connector_get(conn_state->connector);
333 conn_state->crtc = crtc;
334
335 drm_dbg_atomic(connector->dev,
336 "Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
337 connector->base.id, connector->name,
338 conn_state, crtc->base.id, crtc->name);
339 } else {
340 drm_dbg_atomic(connector->dev,
341 "Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
342 connector->base.id, connector->name,
343 conn_state);
344 }
345
346 return 0;
347 }
348 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
349
set_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc,s32 __user * fence_ptr)350 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
351 struct drm_crtc *crtc, s32 __user *fence_ptr)
352 {
353 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
354 }
355
get_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc)356 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
357 struct drm_crtc *crtc)
358 {
359 s32 __user *fence_ptr;
360
361 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
362 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
363
364 return fence_ptr;
365 }
366
set_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector,s32 __user * fence_ptr)367 static int set_out_fence_for_connector(struct drm_atomic_state *state,
368 struct drm_connector *connector,
369 s32 __user *fence_ptr)
370 {
371 unsigned int index = drm_connector_index(connector);
372
373 if (!fence_ptr)
374 return 0;
375
376 if (put_user(-1, fence_ptr))
377 return -EFAULT;
378
379 state->connectors[index].out_fence_ptr = fence_ptr;
380
381 return 0;
382 }
383
get_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector)384 static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
385 struct drm_connector *connector)
386 {
387 unsigned int index = drm_connector_index(connector);
388 s32 __user *fence_ptr;
389
390 fence_ptr = state->connectors[index].out_fence_ptr;
391 state->connectors[index].out_fence_ptr = NULL;
392
393 return fence_ptr;
394 }
395
drm_atomic_crtc_set_property(struct drm_crtc * crtc,struct drm_crtc_state * state,struct drm_property * property,uint64_t val)396 static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
397 struct drm_crtc_state *state, struct drm_property *property,
398 uint64_t val)
399 {
400 struct drm_device *dev = crtc->dev;
401 struct drm_mode_config *config = &dev->mode_config;
402 bool replaced = false;
403 int ret;
404
405 if (property == config->prop_active)
406 state->active = val;
407 else if (property == config->prop_mode_id) {
408 struct drm_property_blob *mode =
409 drm_property_lookup_blob(dev, val);
410 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
411 drm_property_blob_put(mode);
412 return ret;
413 } else if (property == config->prop_vrr_enabled) {
414 state->vrr_enabled = val;
415 } else if (property == config->degamma_lut_property) {
416 const size_t elem_size = sizeof(struct drm_color_lut);
417 u64 lut_size;
418
419 ret = drm_object_immutable_property_get_value(&crtc->base,
420 config->degamma_lut_size_property,
421 &lut_size);
422 if (ret)
423 return ret;
424
425 ret = drm_property_replace_blob_from_id(dev,
426 &state->degamma_lut,
427 val,
428 elem_size * lut_size, -1, elem_size,
429 &replaced);
430 state->color_mgmt_changed |= replaced;
431 return ret;
432 } else if (property == config->ctm_property) {
433 ret = drm_property_replace_blob_from_id(dev,
434 &state->ctm,
435 val,
436 -1, sizeof(struct drm_color_ctm), -1,
437 &replaced);
438 state->color_mgmt_changed |= replaced;
439 return ret;
440 } else if (property == config->gamma_lut_property) {
441 const size_t elem_size = sizeof(struct drm_color_lut);
442 u64 lut_size;
443
444 ret = drm_object_immutable_property_get_value(&crtc->base,
445 config->gamma_lut_size_property,
446 &lut_size);
447 if (ret)
448 return ret;
449
450 ret = drm_property_replace_blob_from_id(dev,
451 &state->gamma_lut,
452 val,
453 elem_size * lut_size, -1, elem_size,
454 &replaced);
455 state->color_mgmt_changed |= replaced;
456 return ret;
457 } else if (property == config->prop_out_fence_ptr) {
458 s32 __user *fence_ptr = u64_to_user_ptr(val);
459
460 if (!fence_ptr)
461 return 0;
462
463 if (put_user(-1, fence_ptr))
464 return -EFAULT;
465
466 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
467 } else if (property == crtc->scaling_filter_property) {
468 state->scaling_filter = val;
469 } else if (property == crtc->sharpness_strength_property) {
470 state->sharpness_strength = val;
471 } else if (crtc->funcs->atomic_set_property) {
472 return crtc->funcs->atomic_set_property(crtc, state, property, val);
473 } else {
474 drm_dbg_atomic(crtc->dev,
475 "[CRTC:%d:%s] unknown property [PROP:%d:%s]\n",
476 crtc->base.id, crtc->name,
477 property->base.id, property->name);
478 return -EINVAL;
479 }
480
481 return 0;
482 }
483
484 static int
drm_atomic_crtc_get_property(struct drm_crtc * crtc,const struct drm_crtc_state * state,struct drm_property * property,uint64_t * val)485 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
486 const struct drm_crtc_state *state,
487 struct drm_property *property, uint64_t *val)
488 {
489 struct drm_device *dev = crtc->dev;
490 struct drm_mode_config *config = &dev->mode_config;
491
492 if (property == config->prop_active)
493 *val = drm_atomic_crtc_effectively_active(state);
494 else if (property == config->prop_mode_id)
495 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
496 else if (property == config->prop_vrr_enabled)
497 *val = state->vrr_enabled;
498 else if (property == config->degamma_lut_property)
499 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
500 else if (property == config->ctm_property)
501 *val = (state->ctm) ? state->ctm->base.id : 0;
502 else if (property == config->gamma_lut_property)
503 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
504 else if (property == config->prop_out_fence_ptr)
505 *val = 0;
506 else if (property == crtc->scaling_filter_property)
507 *val = state->scaling_filter;
508 else if (property == crtc->sharpness_strength_property)
509 *val = state->sharpness_strength;
510 else if (crtc->funcs->atomic_get_property)
511 return crtc->funcs->atomic_get_property(crtc, state, property, val);
512 else {
513 drm_dbg_atomic(dev,
514 "[CRTC:%d:%s] unknown property [PROP:%d:%s]\n",
515 crtc->base.id, crtc->name,
516 property->base.id, property->name);
517 return -EINVAL;
518 }
519
520 return 0;
521 }
522
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)523 static int drm_atomic_plane_set_property(struct drm_plane *plane,
524 struct drm_plane_state *state, struct drm_file *file_priv,
525 struct drm_property *property, uint64_t val)
526 {
527 struct drm_device *dev = plane->dev;
528 struct drm_mode_config *config = &dev->mode_config;
529 bool replaced = false;
530 int ret;
531
532 if (property == config->prop_fb_id) {
533 struct drm_framebuffer *fb;
534
535 fb = drm_framebuffer_lookup(dev, file_priv, val);
536 drm_atomic_set_fb_for_plane(state, fb);
537 if (fb)
538 drm_framebuffer_put(fb);
539 } else if (property == config->prop_in_fence_fd) {
540 if (state->fence)
541 return -EINVAL;
542
543 if (U642I64(val) == -1)
544 return 0;
545
546 state->fence = sync_file_get_fence(val);
547 if (!state->fence)
548 return -EINVAL;
549
550 } else if (property == config->prop_crtc_id) {
551 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
552
553 if (val && !crtc) {
554 drm_dbg_atomic(dev,
555 "[PROP:%d:%s] cannot find CRTC with ID %llu\n",
556 property->base.id, property->name, val);
557 return -EACCES;
558 }
559 return drm_atomic_set_crtc_for_plane(state, crtc);
560 } else if (property == config->prop_crtc_x) {
561 state->crtc_x = U642I64(val);
562 } else if (property == config->prop_crtc_y) {
563 state->crtc_y = U642I64(val);
564 } else if (property == config->prop_crtc_w) {
565 state->crtc_w = val;
566 } else if (property == config->prop_crtc_h) {
567 state->crtc_h = val;
568 } else if (property == config->prop_src_x) {
569 state->src_x = val;
570 } else if (property == config->prop_src_y) {
571 state->src_y = val;
572 } else if (property == config->prop_src_w) {
573 state->src_w = val;
574 } else if (property == config->prop_src_h) {
575 state->src_h = val;
576 } else if (property == plane->alpha_property) {
577 state->alpha = val;
578 } else if (property == plane->blend_mode_property) {
579 state->pixel_blend_mode = val;
580 } else if (property == plane->rotation_property) {
581 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
582 drm_dbg_atomic(plane->dev,
583 "[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
584 plane->base.id, plane->name, val);
585 return -EINVAL;
586 }
587 state->rotation = val;
588 } else if (property == plane->zpos_property) {
589 state->zpos = val;
590 } else if (property == plane->color_encoding_property) {
591 state->color_encoding = val;
592 } else if (property == plane->color_range_property) {
593 state->color_range = val;
594 } else if (property == plane->color_pipeline_property) {
595 /* find DRM colorop object */
596 struct drm_colorop *colorop = NULL;
597
598 colorop = drm_colorop_find(dev, file_priv, val);
599
600 if (val && !colorop)
601 return -EACCES;
602
603 drm_atomic_set_colorop_for_plane(state, colorop);
604 } else if (property == config->prop_fb_damage_clips) {
605 ret = drm_property_replace_blob_from_id(dev,
606 &state->fb_damage_clips,
607 val,
608 -1, -1, sizeof(struct drm_mode_rect),
609 &replaced);
610 return ret;
611 } else if (property == plane->scaling_filter_property) {
612 state->scaling_filter = val;
613 } else if (plane->funcs->atomic_set_property) {
614 return plane->funcs->atomic_set_property(plane, state,
615 property, val);
616 } else if (property == plane->hotspot_x_property) {
617 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
618 drm_dbg_atomic(plane->dev,
619 "[PLANE:%d:%s] is not a cursor plane: 0x%llx\n",
620 plane->base.id, plane->name, val);
621 return -EINVAL;
622 }
623 state->hotspot_x = val;
624 } else if (property == plane->hotspot_y_property) {
625 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
626 drm_dbg_atomic(plane->dev,
627 "[PLANE:%d:%s] is not a cursor plane: 0x%llx\n",
628 plane->base.id, plane->name, val);
629 return -EINVAL;
630 }
631 state->hotspot_y = val;
632 } else {
633 drm_dbg_atomic(plane->dev,
634 "[PLANE:%d:%s] unknown property [PROP:%d:%s]\n",
635 plane->base.id, plane->name,
636 property->base.id, property->name);
637 return -EINVAL;
638 }
639
640 return 0;
641 }
642
643 static int
drm_atomic_plane_get_property(struct drm_plane * plane,const struct drm_plane_state * state,struct drm_property * property,uint64_t * val)644 drm_atomic_plane_get_property(struct drm_plane *plane,
645 const struct drm_plane_state *state,
646 struct drm_property *property, uint64_t *val)
647 {
648 struct drm_device *dev = plane->dev;
649 struct drm_mode_config *config = &dev->mode_config;
650
651 if (property == config->prop_fb_id) {
652 *val = (state->fb) ? state->fb->base.id : 0;
653 } else if (property == config->prop_in_fence_fd) {
654 *val = -1;
655 } else if (property == config->prop_crtc_id) {
656 *val = (state->crtc) ? state->crtc->base.id : 0;
657 } else if (property == config->prop_crtc_x) {
658 *val = I642U64(state->crtc_x);
659 } else if (property == config->prop_crtc_y) {
660 *val = I642U64(state->crtc_y);
661 } else if (property == config->prop_crtc_w) {
662 *val = state->crtc_w;
663 } else if (property == config->prop_crtc_h) {
664 *val = state->crtc_h;
665 } else if (property == config->prop_src_x) {
666 *val = state->src_x;
667 } else if (property == config->prop_src_y) {
668 *val = state->src_y;
669 } else if (property == config->prop_src_w) {
670 *val = state->src_w;
671 } else if (property == config->prop_src_h) {
672 *val = state->src_h;
673 } else if (property == plane->alpha_property) {
674 *val = state->alpha;
675 } else if (property == plane->blend_mode_property) {
676 *val = state->pixel_blend_mode;
677 } else if (property == plane->rotation_property) {
678 *val = state->rotation;
679 } else if (property == plane->zpos_property) {
680 *val = state->zpos;
681 } else if (property == plane->color_encoding_property) {
682 *val = state->color_encoding;
683 } else if (property == plane->color_range_property) {
684 *val = state->color_range;
685 } else if (property == plane->color_pipeline_property) {
686 *val = (state->color_pipeline) ? state->color_pipeline->base.id : 0;
687 } else if (property == config->prop_fb_damage_clips) {
688 *val = (state->fb_damage_clips) ?
689 state->fb_damage_clips->base.id : 0;
690 } else if (property == plane->scaling_filter_property) {
691 *val = state->scaling_filter;
692 } else if (plane->funcs->atomic_get_property) {
693 return plane->funcs->atomic_get_property(plane, state, property, val);
694 } else if (property == plane->hotspot_x_property) {
695 *val = state->hotspot_x;
696 } else if (property == plane->hotspot_y_property) {
697 *val = state->hotspot_y;
698 } else {
699 drm_dbg_atomic(dev,
700 "[PLANE:%d:%s] unknown property [PROP:%d:%s]\n",
701 plane->base.id, plane->name,
702 property->base.id, property->name);
703 return -EINVAL;
704 }
705
706 return 0;
707 }
708
drm_atomic_color_set_data_property(struct drm_colorop * colorop,struct drm_colorop_state * state,struct drm_property * property,uint64_t val)709 static int drm_atomic_color_set_data_property(struct drm_colorop *colorop,
710 struct drm_colorop_state *state,
711 struct drm_property *property,
712 uint64_t val)
713 {
714 ssize_t elem_size = -1;
715 ssize_t size = -1;
716 bool replaced = false;
717
718 switch (colorop->type) {
719 case DRM_COLOROP_1D_LUT:
720 size = colorop->size * sizeof(struct drm_color_lut32);
721 break;
722 case DRM_COLOROP_CTM_3X4:
723 size = sizeof(struct drm_color_ctm_3x4);
724 break;
725 case DRM_COLOROP_3D_LUT:
726 size = colorop->size * colorop->size * colorop->size *
727 sizeof(struct drm_color_lut32);
728 break;
729 default:
730 /* should never get here */
731 return -EINVAL;
732 }
733
734 return drm_property_replace_blob_from_id(colorop->dev,
735 &state->data,
736 val,
737 -1, size, elem_size,
738 &replaced);
739 }
740
drm_atomic_colorop_set_property(struct drm_colorop * colorop,struct drm_colorop_state * state,struct drm_file * file_priv,struct drm_property * property,uint64_t val)741 static int drm_atomic_colorop_set_property(struct drm_colorop *colorop,
742 struct drm_colorop_state *state,
743 struct drm_file *file_priv,
744 struct drm_property *property,
745 uint64_t val)
746 {
747 if (property == colorop->bypass_property) {
748 state->bypass = val;
749 } else if (property == colorop->lut1d_interpolation_property) {
750 colorop->lut1d_interpolation = val;
751 } else if (property == colorop->curve_1d_type_property) {
752 state->curve_1d_type = val;
753 } else if (property == colorop->multiplier_property) {
754 state->multiplier = val;
755 } else if (property == colorop->lut3d_interpolation_property) {
756 colorop->lut3d_interpolation = val;
757 } else if (property == colorop->data_property) {
758 return drm_atomic_color_set_data_property(colorop, state,
759 property, val);
760 } else {
761 drm_dbg_atomic(colorop->dev,
762 "[COLOROP:%d:%d] unknown property [PROP:%d:%s]\n",
763 colorop->base.id, colorop->type,
764 property->base.id, property->name);
765 return -EINVAL;
766 }
767
768 return 0;
769 }
770
771 static int
drm_atomic_colorop_get_property(struct drm_colorop * colorop,const struct drm_colorop_state * state,struct drm_property * property,uint64_t * val)772 drm_atomic_colorop_get_property(struct drm_colorop *colorop,
773 const struct drm_colorop_state *state,
774 struct drm_property *property, uint64_t *val)
775 {
776 if (property == colorop->type_property)
777 *val = colorop->type;
778 else if (property == colorop->bypass_property)
779 *val = state->bypass;
780 else if (property == colorop->lut1d_interpolation_property)
781 *val = colorop->lut1d_interpolation;
782 else if (property == colorop->curve_1d_type_property)
783 *val = state->curve_1d_type;
784 else if (property == colorop->multiplier_property)
785 *val = state->multiplier;
786 else if (property == colorop->size_property)
787 *val = colorop->size;
788 else if (property == colorop->lut3d_interpolation_property)
789 *val = colorop->lut3d_interpolation;
790 else if (property == colorop->data_property)
791 *val = (state->data) ? state->data->base.id : 0;
792 else
793 return -EINVAL;
794
795 return 0;
796 }
797
drm_atomic_set_writeback_fb_for_connector(struct drm_connector_state * conn_state,struct drm_framebuffer * fb)798 static int drm_atomic_set_writeback_fb_for_connector(
799 struct drm_connector_state *conn_state,
800 struct drm_framebuffer *fb)
801 {
802 int ret;
803 struct drm_connector *conn = conn_state->connector;
804
805 ret = drm_writeback_set_fb(conn_state, fb);
806 if (ret < 0)
807 return ret;
808
809 if (fb)
810 drm_dbg_atomic(conn->dev,
811 "Set [FB:%d] for connector state %p\n",
812 fb->base.id, conn_state);
813 else
814 drm_dbg_atomic(conn->dev,
815 "Set [NOFB] for connector state %p\n",
816 conn_state);
817
818 return 0;
819 }
820
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)821 static int drm_atomic_connector_set_property(struct drm_connector *connector,
822 struct drm_connector_state *state, struct drm_file *file_priv,
823 struct drm_property *property, uint64_t val)
824 {
825 struct drm_device *dev = connector->dev;
826 struct drm_mode_config *config = &dev->mode_config;
827 bool replaced = false;
828 int ret;
829
830 if (property == config->prop_crtc_id) {
831 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
832
833 if (val && !crtc) {
834 drm_dbg_atomic(dev,
835 "[PROP:%d:%s] cannot find CRTC with ID %llu\n",
836 property->base.id, property->name, val);
837 return -EACCES;
838 }
839 return drm_atomic_set_crtc_for_connector(state, crtc);
840 } else if (property == config->dpms_property) {
841 /* setting DPMS property requires special handling, which
842 * is done in legacy setprop path for us. Disallow (for
843 * now?) atomic writes to DPMS property:
844 */
845 drm_dbg_atomic(dev,
846 "legacy [PROP:%d:%s] can only be set via legacy uAPI\n",
847 property->base.id, property->name);
848 return -EINVAL;
849 } else if (property == config->tv_select_subconnector_property) {
850 state->tv.select_subconnector = val;
851 } else if (property == config->tv_subconnector_property) {
852 state->tv.subconnector = val;
853 } else if (property == config->tv_left_margin_property) {
854 state->tv.margins.left = val;
855 } else if (property == config->tv_right_margin_property) {
856 state->tv.margins.right = val;
857 } else if (property == config->tv_top_margin_property) {
858 state->tv.margins.top = val;
859 } else if (property == config->tv_bottom_margin_property) {
860 state->tv.margins.bottom = val;
861 } else if (property == config->legacy_tv_mode_property) {
862 state->tv.legacy_mode = val;
863 } else if (property == config->tv_mode_property) {
864 state->tv.mode = val;
865 } else if (property == config->tv_brightness_property) {
866 state->tv.brightness = val;
867 } else if (property == config->tv_contrast_property) {
868 state->tv.contrast = val;
869 } else if (property == config->tv_flicker_reduction_property) {
870 state->tv.flicker_reduction = val;
871 } else if (property == config->tv_overscan_property) {
872 state->tv.overscan = val;
873 } else if (property == config->tv_saturation_property) {
874 state->tv.saturation = val;
875 } else if (property == config->tv_hue_property) {
876 state->tv.hue = val;
877 } else if (property == config->link_status_property) {
878 /* Never downgrade from GOOD to BAD on userspace's request here,
879 * only hw issues can do that.
880 *
881 * For an atomic property the userspace doesn't need to be able
882 * to understand all the properties, but needs to be able to
883 * restore the state it wants on VT switch. So if the userspace
884 * tries to change the link_status from GOOD to BAD, driver
885 * silently rejects it and returns a 0. This prevents userspace
886 * from accidentally breaking the display when it restores the
887 * state.
888 */
889 if (state->link_status != DRM_LINK_STATUS_GOOD)
890 state->link_status = val;
891 } else if (property == config->hdr_output_metadata_property) {
892 ret = drm_property_replace_blob_from_id(dev,
893 &state->hdr_output_metadata,
894 val,
895 -1, sizeof(struct hdr_output_metadata), -1,
896 &replaced);
897 return ret;
898 } else if (property == config->aspect_ratio_property) {
899 state->picture_aspect_ratio = val;
900 } else if (property == config->content_type_property) {
901 state->content_type = val;
902 } else if (property == connector->scaling_mode_property) {
903 state->scaling_mode = val;
904 } else if (property == config->content_protection_property) {
905 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
906 drm_dbg_kms(dev, "only drivers can set CP Enabled\n");
907 return -EINVAL;
908 }
909 state->content_protection = val;
910 } else if (property == config->hdcp_content_type_property) {
911 state->hdcp_content_type = val;
912 } else if (property == connector->colorspace_property) {
913 state->colorspace = val;
914 } else if (property == config->writeback_fb_id_property) {
915 struct drm_framebuffer *fb;
916 int ret;
917
918 fb = drm_framebuffer_lookup(dev, file_priv, val);
919 ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
920 if (fb)
921 drm_framebuffer_put(fb);
922 return ret;
923 } else if (property == config->writeback_out_fence_ptr_property) {
924 s32 __user *fence_ptr = u64_to_user_ptr(val);
925
926 return set_out_fence_for_connector(state->state, connector,
927 fence_ptr);
928 } else if (property == connector->max_bpc_property) {
929 state->max_requested_bpc = val;
930 } else if (property == connector->privacy_screen_sw_state_property) {
931 state->privacy_screen_sw_state = val;
932 } else if (property == connector->broadcast_rgb_property) {
933 state->hdmi.broadcast_rgb = val;
934 } else if (connector->funcs->atomic_set_property) {
935 return connector->funcs->atomic_set_property(connector,
936 state, property, val);
937 } else {
938 drm_dbg_atomic(connector->dev,
939 "[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]\n",
940 connector->base.id, connector->name,
941 property->base.id, property->name);
942 return -EINVAL;
943 }
944
945 return 0;
946 }
947
948 static int
drm_atomic_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)949 drm_atomic_connector_get_property(struct drm_connector *connector,
950 const struct drm_connector_state *state,
951 struct drm_property *property, uint64_t *val)
952 {
953 struct drm_device *dev = connector->dev;
954 struct drm_mode_config *config = &dev->mode_config;
955
956 if (property == config->prop_crtc_id) {
957 *val = (state->crtc) ? state->crtc->base.id : 0;
958 } else if (property == config->dpms_property) {
959 if (state->crtc && state->crtc->state->self_refresh_active)
960 *val = DRM_MODE_DPMS_ON;
961 else
962 *val = connector->dpms;
963 } else if (property == config->tv_select_subconnector_property) {
964 *val = state->tv.select_subconnector;
965 } else if (property == config->tv_subconnector_property) {
966 *val = state->tv.subconnector;
967 } else if (property == config->tv_left_margin_property) {
968 *val = state->tv.margins.left;
969 } else if (property == config->tv_right_margin_property) {
970 *val = state->tv.margins.right;
971 } else if (property == config->tv_top_margin_property) {
972 *val = state->tv.margins.top;
973 } else if (property == config->tv_bottom_margin_property) {
974 *val = state->tv.margins.bottom;
975 } else if (property == config->legacy_tv_mode_property) {
976 *val = state->tv.legacy_mode;
977 } else if (property == config->tv_mode_property) {
978 *val = state->tv.mode;
979 } else if (property == config->tv_brightness_property) {
980 *val = state->tv.brightness;
981 } else if (property == config->tv_contrast_property) {
982 *val = state->tv.contrast;
983 } else if (property == config->tv_flicker_reduction_property) {
984 *val = state->tv.flicker_reduction;
985 } else if (property == config->tv_overscan_property) {
986 *val = state->tv.overscan;
987 } else if (property == config->tv_saturation_property) {
988 *val = state->tv.saturation;
989 } else if (property == config->tv_hue_property) {
990 *val = state->tv.hue;
991 } else if (property == config->link_status_property) {
992 *val = state->link_status;
993 } else if (property == config->aspect_ratio_property) {
994 *val = state->picture_aspect_ratio;
995 } else if (property == config->content_type_property) {
996 *val = state->content_type;
997 } else if (property == connector->colorspace_property) {
998 *val = state->colorspace;
999 } else if (property == connector->scaling_mode_property) {
1000 *val = state->scaling_mode;
1001 } else if (property == config->hdr_output_metadata_property) {
1002 *val = state->hdr_output_metadata ?
1003 state->hdr_output_metadata->base.id : 0;
1004 } else if (property == config->content_protection_property) {
1005 *val = state->content_protection;
1006 } else if (property == config->hdcp_content_type_property) {
1007 *val = state->hdcp_content_type;
1008 } else if (property == config->writeback_fb_id_property) {
1009 /* Writeback framebuffer is one-shot, write and forget */
1010 *val = 0;
1011 } else if (property == config->writeback_out_fence_ptr_property) {
1012 *val = 0;
1013 } else if (property == connector->max_bpc_property) {
1014 *val = state->max_requested_bpc;
1015 } else if (property == connector->privacy_screen_sw_state_property) {
1016 *val = state->privacy_screen_sw_state;
1017 } else if (property == connector->broadcast_rgb_property) {
1018 *val = state->hdmi.broadcast_rgb;
1019 } else if (connector->funcs->atomic_get_property) {
1020 return connector->funcs->atomic_get_property(connector,
1021 state, property, val);
1022 } else {
1023 drm_dbg_atomic(dev,
1024 "[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]\n",
1025 connector->base.id, connector->name,
1026 property->base.id, property->name);
1027 return -EINVAL;
1028 }
1029
1030 return 0;
1031 }
1032
drm_atomic_get_property(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)1033 int drm_atomic_get_property(struct drm_mode_object *obj,
1034 struct drm_property *property, uint64_t *val)
1035 {
1036 struct drm_device *dev = property->dev;
1037 int ret;
1038
1039 switch (obj->type) {
1040 case DRM_MODE_OBJECT_CONNECTOR: {
1041 struct drm_connector *connector = obj_to_connector(obj);
1042
1043 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1044 ret = drm_atomic_connector_get_property(connector,
1045 connector->state, property, val);
1046 break;
1047 }
1048 case DRM_MODE_OBJECT_CRTC: {
1049 struct drm_crtc *crtc = obj_to_crtc(obj);
1050
1051 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1052 ret = drm_atomic_crtc_get_property(crtc,
1053 crtc->state, property, val);
1054 break;
1055 }
1056 case DRM_MODE_OBJECT_PLANE: {
1057 struct drm_plane *plane = obj_to_plane(obj);
1058
1059 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1060 ret = drm_atomic_plane_get_property(plane,
1061 plane->state, property, val);
1062 break;
1063 }
1064 case DRM_MODE_OBJECT_COLOROP: {
1065 struct drm_colorop *colorop = obj_to_colorop(obj);
1066
1067 if (colorop->plane)
1068 WARN_ON(!drm_modeset_is_locked(&colorop->plane->mutex));
1069
1070 ret = drm_atomic_colorop_get_property(colorop, colorop->state, property, val);
1071 break;
1072 }
1073 default:
1074 drm_dbg_atomic(dev, "[OBJECT:%d] has no properties\n", obj->id);
1075 ret = -EINVAL;
1076 break;
1077 }
1078
1079 return ret;
1080 }
1081
1082 /*
1083 * The big monster ioctl
1084 */
1085
create_vblank_event(struct drm_crtc * crtc,uint64_t user_data)1086 static struct drm_pending_vblank_event *create_vblank_event(
1087 struct drm_crtc *crtc, uint64_t user_data)
1088 {
1089 struct drm_pending_vblank_event *e = NULL;
1090
1091 e = kzalloc_obj(*e);
1092 if (!e)
1093 return NULL;
1094
1095 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1096 e->event.base.length = sizeof(e->event);
1097 e->event.vbl.crtc_id = crtc->base.id;
1098 e->event.vbl.user_data = user_data;
1099
1100 return e;
1101 }
1102
drm_atomic_connector_commit_dpms(struct drm_atomic_state * state,struct drm_connector * connector,int mode)1103 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
1104 struct drm_connector *connector,
1105 int mode)
1106 {
1107 struct drm_connector *tmp_connector;
1108 struct drm_connector_state *new_conn_state;
1109 struct drm_crtc *crtc;
1110 struct drm_crtc_state *crtc_state;
1111 int i, ret, old_mode = connector->dpms;
1112 bool active = false;
1113
1114 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1115 state->acquire_ctx);
1116 if (ret)
1117 return ret;
1118
1119 if (mode != DRM_MODE_DPMS_ON)
1120 mode = DRM_MODE_DPMS_OFF;
1121
1122 if (connector->dpms == mode)
1123 goto out;
1124
1125 connector->dpms = mode;
1126
1127 crtc = connector->state->crtc;
1128 if (!crtc)
1129 goto out;
1130 ret = drm_atomic_add_affected_connectors(state, crtc);
1131 if (ret)
1132 goto out;
1133
1134 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1135 if (IS_ERR(crtc_state)) {
1136 ret = PTR_ERR(crtc_state);
1137 goto out;
1138 }
1139
1140 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
1141 if (new_conn_state->crtc != crtc)
1142 continue;
1143 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
1144 active = true;
1145 break;
1146 }
1147 }
1148
1149 crtc_state->active = active;
1150 ret = drm_atomic_commit(state);
1151 out:
1152 if (ret != 0)
1153 connector->dpms = old_mode;
1154 return ret;
1155 }
1156
drm_atomic_check_prop_changes(int ret,uint64_t old_val,uint64_t prop_value,struct drm_property * prop)1157 static int drm_atomic_check_prop_changes(int ret, uint64_t old_val, uint64_t prop_value,
1158 struct drm_property *prop)
1159 {
1160 if (ret != 0 || old_val != prop_value) {
1161 drm_dbg_atomic(prop->dev,
1162 "[PROP:%d:%s] No prop can be changed during async flip\n",
1163 prop->base.id, prop->name);
1164 return -EINVAL;
1165 }
1166
1167 return 0;
1168 }
1169
drm_atomic_set_property(struct drm_atomic_state * state,struct drm_file * file_priv,struct drm_mode_object * obj,struct drm_property * prop,u64 prop_value,bool async_flip)1170 int drm_atomic_set_property(struct drm_atomic_state *state,
1171 struct drm_file *file_priv,
1172 struct drm_mode_object *obj,
1173 struct drm_property *prop,
1174 u64 prop_value,
1175 bool async_flip)
1176 {
1177 struct drm_mode_object *ref;
1178 u64 old_val;
1179 int ret;
1180
1181 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1182 return -EINVAL;
1183
1184 switch (obj->type) {
1185 case DRM_MODE_OBJECT_CONNECTOR: {
1186 struct drm_connector *connector = obj_to_connector(obj);
1187 struct drm_connector_state *connector_state;
1188
1189 connector_state = drm_atomic_get_connector_state(state, connector);
1190 if (IS_ERR(connector_state)) {
1191 ret = PTR_ERR(connector_state);
1192 break;
1193 }
1194
1195 if (async_flip) {
1196 ret = drm_atomic_connector_get_property(connector, connector_state,
1197 prop, &old_val);
1198 ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1199 break;
1200 }
1201
1202 ret = drm_atomic_connector_set_property(connector,
1203 connector_state, file_priv,
1204 prop, prop_value);
1205 break;
1206 }
1207 case DRM_MODE_OBJECT_CRTC: {
1208 struct drm_crtc *crtc = obj_to_crtc(obj);
1209 struct drm_crtc_state *crtc_state;
1210
1211 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1212 if (IS_ERR(crtc_state)) {
1213 ret = PTR_ERR(crtc_state);
1214 break;
1215 }
1216
1217 if (async_flip) {
1218 ret = drm_atomic_crtc_get_property(crtc, crtc_state,
1219 prop, &old_val);
1220 ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1221 break;
1222 }
1223
1224 ret = drm_atomic_crtc_set_property(crtc,
1225 crtc_state, prop, prop_value);
1226 break;
1227 }
1228 case DRM_MODE_OBJECT_PLANE: {
1229 struct drm_plane *plane = obj_to_plane(obj);
1230 struct drm_plane_state *plane_state;
1231 struct drm_mode_config *config = &plane->dev->mode_config;
1232 const struct drm_plane_helper_funcs *plane_funcs = plane->helper_private;
1233
1234 plane_state = drm_atomic_get_plane_state(state, plane);
1235 if (IS_ERR(plane_state)) {
1236 ret = PTR_ERR(plane_state);
1237 break;
1238 }
1239
1240 if (async_flip) {
1241 /* no-op changes are always allowed */
1242 ret = drm_atomic_plane_get_property(plane, plane_state,
1243 prop, &old_val);
1244 ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1245
1246 /* fail everything that isn't no-op or a pure flip */
1247 if (ret && prop != config->prop_fb_id &&
1248 prop != config->prop_in_fence_fd &&
1249 prop != config->prop_fb_damage_clips) {
1250 break;
1251 }
1252
1253 if (ret && plane->type != DRM_PLANE_TYPE_PRIMARY) {
1254 /* ask the driver if this non-primary plane is supported */
1255 if (plane_funcs && plane_funcs->atomic_async_check)
1256 ret = plane_funcs->atomic_async_check(plane, state, true);
1257
1258 if (ret) {
1259 drm_dbg_atomic(prop->dev,
1260 "[PLANE:%d:%s] does not support async flips\n",
1261 obj->id, plane->name);
1262 break;
1263 }
1264 }
1265 }
1266
1267 ret = drm_atomic_plane_set_property(plane,
1268 plane_state, file_priv,
1269 prop, prop_value);
1270
1271 break;
1272 }
1273 case DRM_MODE_OBJECT_COLOROP: {
1274 struct drm_colorop *colorop = obj_to_colorop(obj);
1275 struct drm_colorop_state *colorop_state;
1276
1277 colorop_state = drm_atomic_get_colorop_state(state, colorop);
1278 if (IS_ERR(colorop_state)) {
1279 ret = PTR_ERR(colorop_state);
1280 break;
1281 }
1282
1283 ret = drm_atomic_colorop_set_property(colorop, colorop_state,
1284 file_priv, prop, prop_value);
1285 break;
1286 }
1287 default:
1288 drm_dbg_atomic(prop->dev, "[OBJECT:%d] has no properties\n", obj->id);
1289 ret = -EINVAL;
1290 break;
1291 }
1292
1293 drm_property_change_valid_put(prop, ref);
1294 return ret;
1295 }
1296
1297 /**
1298 * DOC: explicit fencing properties
1299 *
1300 * Explicit fencing allows userspace to control the buffer synchronization
1301 * between devices. A Fence or a group of fences are transferred to/from
1302 * userspace using Sync File fds and there are two DRM properties for that.
1303 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1304 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1305 *
1306 * As a contrast, with implicit fencing the kernel keeps track of any
1307 * ongoing rendering, and automatically ensures that the atomic update waits
1308 * for any pending rendering to complete. This is usually tracked in &struct
1309 * dma_resv which can also contain mandatory kernel fences. Implicit syncing
1310 * is how Linux traditionally worked (e.g. DRI2/3 on X.org), whereas explicit
1311 * fencing is what Android wants.
1312 *
1313 * "IN_FENCE_FD”:
1314 * Use this property to pass a fence that DRM should wait on before
1315 * proceeding with the Atomic Commit request and show the framebuffer for
1316 * the plane on the screen. The fence can be either a normal fence or a
1317 * merged one, the sync_file framework will handle both cases and use a
1318 * fence_array if a merged fence is received. Passing -1 here means no
1319 * fences to wait on.
1320 *
1321 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1322 * it will only check if the Sync File is a valid one.
1323 *
1324 * On the driver side the fence is stored on the @fence parameter of
1325 * &struct drm_plane_state. Drivers which also support implicit fencing
1326 * should extract the implicit fence using drm_gem_plane_helper_prepare_fb(),
1327 * to make sure there's consistent behaviour between drivers in precedence
1328 * of implicit vs. explicit fencing.
1329 *
1330 * "OUT_FENCE_PTR”:
1331 * Use this property to pass a file descriptor pointer to DRM. Once the
1332 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1333 * the file descriptor number of a Sync File. This Sync File contains the
1334 * CRTC fence that will be signaled when all framebuffers present on the
1335 * Atomic Commit * request for that given CRTC are scanned out on the
1336 * screen.
1337 *
1338 * The Atomic Commit request fails if a invalid pointer is passed. If the
1339 * Atomic Commit request fails for any other reason the out fence fd
1340 * returned will be -1. On a Atomic Commit with the
1341 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1342 *
1343 * Note that out-fences don't have a special interface to drivers and are
1344 * internally represented by a &struct drm_pending_vblank_event in struct
1345 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1346 * helpers and for the DRM event handling for existing userspace.
1347 */
1348
1349 struct drm_out_fence_state {
1350 s32 __user *out_fence_ptr;
1351 struct sync_file *sync_file;
1352 int fd;
1353 };
1354
setup_out_fence(struct drm_out_fence_state * fence_state,struct dma_fence * fence)1355 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1356 struct dma_fence *fence)
1357 {
1358 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1359 if (fence_state->fd < 0)
1360 return fence_state->fd;
1361
1362 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1363 return -EFAULT;
1364
1365 fence_state->sync_file = sync_file_create(fence);
1366 if (!fence_state->sync_file)
1367 return -ENOMEM;
1368
1369 return 0;
1370 }
1371
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)1372 static int prepare_signaling(struct drm_device *dev,
1373 struct drm_atomic_state *state,
1374 struct drm_mode_atomic *arg,
1375 struct drm_file *file_priv,
1376 struct drm_out_fence_state **fence_state,
1377 unsigned int *num_fences)
1378 {
1379 struct drm_crtc *crtc;
1380 struct drm_crtc_state *crtc_state;
1381 struct drm_connector *conn;
1382 struct drm_connector_state *conn_state;
1383 int i, c = 0, ret;
1384
1385 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1386 return 0;
1387
1388 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1389 s32 __user *fence_ptr;
1390
1391 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1392
1393 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1394 struct drm_pending_vblank_event *e;
1395
1396 e = create_vblank_event(crtc, arg->user_data);
1397 if (!e)
1398 return -ENOMEM;
1399
1400 crtc_state->event = e;
1401 }
1402
1403 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1404 struct drm_pending_vblank_event *e = crtc_state->event;
1405
1406 if (!file_priv)
1407 continue;
1408
1409 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1410 &e->event.base);
1411 if (ret) {
1412 kfree(e);
1413 crtc_state->event = NULL;
1414 return ret;
1415 }
1416 }
1417
1418 if (fence_ptr) {
1419 struct dma_fence *fence;
1420 struct drm_out_fence_state *f;
1421
1422 f = krealloc(*fence_state, sizeof(**fence_state) *
1423 (*num_fences + 1), GFP_KERNEL);
1424 if (!f)
1425 return -ENOMEM;
1426
1427 memset(&f[*num_fences], 0, sizeof(*f));
1428
1429 f[*num_fences].out_fence_ptr = fence_ptr;
1430 *fence_state = f;
1431
1432 fence = drm_crtc_create_fence(crtc);
1433 if (!fence)
1434 return -ENOMEM;
1435
1436 ret = setup_out_fence(&f[(*num_fences)++], fence);
1437 if (ret) {
1438 dma_fence_put(fence);
1439 return ret;
1440 }
1441
1442 crtc_state->event->base.fence = fence;
1443 }
1444
1445 c++;
1446 }
1447
1448 for_each_new_connector_in_state(state, conn, conn_state, i) {
1449 struct drm_writeback_connector *wb_conn;
1450 struct drm_out_fence_state *f;
1451 struct dma_fence *fence;
1452 s32 __user *fence_ptr;
1453
1454 if (!conn_state->writeback_job)
1455 continue;
1456
1457 fence_ptr = get_out_fence_for_connector(state, conn);
1458 if (!fence_ptr)
1459 continue;
1460
1461 f = krealloc(*fence_state, sizeof(**fence_state) *
1462 (*num_fences + 1), GFP_KERNEL);
1463 if (!f)
1464 return -ENOMEM;
1465
1466 memset(&f[*num_fences], 0, sizeof(*f));
1467
1468 f[*num_fences].out_fence_ptr = fence_ptr;
1469 *fence_state = f;
1470
1471 wb_conn = drm_connector_to_writeback(conn);
1472 fence = drm_writeback_get_out_fence(wb_conn);
1473 if (!fence)
1474 return -ENOMEM;
1475
1476 ret = setup_out_fence(&f[(*num_fences)++], fence);
1477 if (ret) {
1478 dma_fence_put(fence);
1479 return ret;
1480 }
1481
1482 conn_state->writeback_job->out_fence = fence;
1483 }
1484
1485 /*
1486 * Having this flag means user mode pends on event which will never
1487 * reach due to lack of at least one CRTC for signaling
1488 */
1489 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
1490 drm_dbg_atomic(dev, "need at least one CRTC for DRM_MODE_PAGE_FLIP_EVENT");
1491 return -EINVAL;
1492 }
1493
1494 return 0;
1495 }
1496
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)1497 static void complete_signaling(struct drm_device *dev,
1498 struct drm_atomic_state *state,
1499 struct drm_out_fence_state *fence_state,
1500 unsigned int num_fences,
1501 bool install_fds)
1502 {
1503 struct drm_crtc *crtc;
1504 struct drm_crtc_state *crtc_state;
1505 int i;
1506
1507 if (install_fds) {
1508 for (i = 0; i < num_fences; i++)
1509 fd_install(fence_state[i].fd,
1510 fence_state[i].sync_file->file);
1511
1512 kfree(fence_state);
1513 return;
1514 }
1515
1516 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1517 struct drm_pending_vblank_event *event = crtc_state->event;
1518 /*
1519 * Free the allocated event. drm_atomic_helper_setup_commit
1520 * can allocate an event too, so only free it if it's ours
1521 * to prevent a double free in drm_atomic_state_clear.
1522 */
1523 if (event && (event->base.fence || event->base.file_priv)) {
1524 drm_event_cancel_free(dev, &event->base);
1525 crtc_state->event = NULL;
1526 }
1527 }
1528
1529 if (!fence_state)
1530 return;
1531
1532 for (i = 0; i < num_fences; i++) {
1533 if (fence_state[i].sync_file)
1534 fput(fence_state[i].sync_file->file);
1535 if (fence_state[i].fd >= 0)
1536 put_unused_fd(fence_state[i].fd);
1537
1538 /* If this fails log error to the user */
1539 if (fence_state[i].out_fence_ptr &&
1540 put_user(-1, fence_state[i].out_fence_ptr))
1541 drm_dbg_atomic(dev, "Couldn't clear out_fence_ptr\n");
1542 }
1543
1544 kfree(fence_state);
1545 }
1546
1547 static void
set_async_flip(struct drm_atomic_state * state)1548 set_async_flip(struct drm_atomic_state *state)
1549 {
1550 struct drm_crtc *crtc;
1551 struct drm_crtc_state *crtc_state;
1552 int i;
1553
1554 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1555 crtc_state->async_flip = true;
1556 }
1557 }
1558
drm_mode_atomic_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1559 int drm_mode_atomic_ioctl(struct drm_device *dev,
1560 void *data, struct drm_file *file_priv)
1561 {
1562 struct drm_mode_atomic *arg = data;
1563 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1564 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1565 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1566 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1567 unsigned int copied_objs, copied_props;
1568 struct drm_atomic_state *state;
1569 struct drm_modeset_acquire_ctx ctx;
1570 struct drm_out_fence_state *fence_state;
1571 int ret = 0;
1572 unsigned int i, j, num_fences;
1573 bool async_flip = false;
1574
1575 /* disallow for drivers not supporting atomic: */
1576 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1577 return -EOPNOTSUPP;
1578
1579 /* disallow for userspace that has not enabled atomic cap (even
1580 * though this may be a bit overkill, since legacy userspace
1581 * wouldn't know how to call this ioctl)
1582 */
1583 if (!file_priv->atomic) {
1584 drm_dbg_atomic(dev,
1585 "commit failed: atomic cap not enabled\n");
1586 return -EINVAL;
1587 }
1588
1589 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) {
1590 drm_dbg_atomic(dev, "commit failed: invalid flag\n");
1591 return -EINVAL;
1592 }
1593
1594 if (arg->reserved) {
1595 drm_dbg_atomic(dev, "commit failed: reserved field set\n");
1596 return -EINVAL;
1597 }
1598
1599 if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) {
1600 if (!dev->mode_config.async_page_flip) {
1601 drm_dbg_atomic(dev,
1602 "commit failed: DRM_MODE_PAGE_FLIP_ASYNC not supported\n");
1603 return -EINVAL;
1604 }
1605
1606 async_flip = true;
1607 }
1608
1609 /* can't test and expect an event at the same time. */
1610 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1611 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
1612 drm_dbg_atomic(dev,
1613 "commit failed: page-flip event requested with test-only commit\n");
1614 return -EINVAL;
1615 }
1616
1617 state = drm_atomic_state_alloc(dev);
1618 if (!state)
1619 return -ENOMEM;
1620
1621 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1622 state->acquire_ctx = &ctx;
1623 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1624 state->plane_color_pipeline = file_priv->plane_color_pipeline;
1625
1626 retry:
1627 copied_objs = 0;
1628 copied_props = 0;
1629 fence_state = NULL;
1630 num_fences = 0;
1631
1632 for (i = 0; i < arg->count_objs; i++) {
1633 uint32_t obj_id, count_props;
1634 struct drm_mode_object *obj;
1635
1636 if (get_user(obj_id, objs_ptr + copied_objs)) {
1637 ret = -EFAULT;
1638 goto out;
1639 }
1640
1641 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1642 if (!obj) {
1643 drm_dbg_atomic(dev, "cannot find object ID %d", obj_id);
1644 ret = -ENOENT;
1645 goto out;
1646 }
1647
1648 if (!obj->properties) {
1649 drm_dbg_atomic(dev, "[OBJECT:%d] has no properties", obj_id);
1650 drm_mode_object_put(obj);
1651 ret = -ENOENT;
1652 goto out;
1653 }
1654
1655 if (get_user(count_props, count_props_ptr + copied_objs)) {
1656 drm_mode_object_put(obj);
1657 ret = -EFAULT;
1658 goto out;
1659 }
1660
1661 copied_objs++;
1662
1663 for (j = 0; j < count_props; j++) {
1664 uint32_t prop_id;
1665 uint64_t prop_value;
1666 struct drm_property *prop;
1667
1668 if (get_user(prop_id, props_ptr + copied_props)) {
1669 drm_mode_object_put(obj);
1670 ret = -EFAULT;
1671 goto out;
1672 }
1673
1674 prop = drm_mode_obj_find_prop_id(obj, prop_id);
1675 if (!prop) {
1676 drm_dbg_atomic(dev,
1677 "[OBJECT:%d] cannot find property ID %d",
1678 obj_id, prop_id);
1679 drm_mode_object_put(obj);
1680 ret = -ENOENT;
1681 goto out;
1682 }
1683
1684 if (copy_from_user(&prop_value,
1685 prop_values_ptr + copied_props,
1686 sizeof(prop_value))) {
1687 drm_mode_object_put(obj);
1688 ret = -EFAULT;
1689 goto out;
1690 }
1691
1692 ret = drm_atomic_set_property(state, file_priv, obj,
1693 prop, prop_value, async_flip);
1694 if (ret) {
1695 drm_mode_object_put(obj);
1696 goto out;
1697 }
1698
1699 copied_props++;
1700 }
1701
1702 drm_mode_object_put(obj);
1703 }
1704
1705 ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1706 &num_fences);
1707 if (ret)
1708 goto out;
1709
1710 if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
1711 set_async_flip(state);
1712
1713 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1714 ret = drm_atomic_check_only(state);
1715 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1716 ret = drm_atomic_nonblocking_commit(state);
1717 } else {
1718 ret = drm_atomic_commit(state);
1719 }
1720
1721 out:
1722 complete_signaling(dev, state, fence_state, num_fences, !ret);
1723
1724 if (ret == -EDEADLK) {
1725 drm_atomic_state_clear(state);
1726 ret = drm_modeset_backoff(&ctx);
1727 if (!ret)
1728 goto retry;
1729 }
1730
1731 drm_atomic_state_put(state);
1732
1733 drm_modeset_drop_locks(&ctx);
1734 drm_modeset_acquire_fini(&ctx);
1735
1736 return ret;
1737 }
1738