1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23 #include <drm/drmP.h>
24 #include <drm/drm_plane.h>
25
26 #include "drm_crtc_internal.h"
27
28 /**
29 * DOC: overview
30 *
31 * A plane represents an image source that can be blended with or overlayed on
32 * top of a CRTC during the scanout process. Planes take their input data from a
33 * &drm_framebuffer object. The plane itself specifies the cropping and scaling
34 * of that image, and where it is placed on the visible are of a display
35 * pipeline, represented by &drm_crtc. A plane can also have additional
36 * properties that specify how the pixels are positioned and blended, like
37 * rotation or Z-position. All these properties are stored in &drm_plane_state.
38 *
39 * To create a plane, a KMS drivers allocates and zeroes an instances of
40 * &struct drm_plane (possibly as part of a larger structure) and registers it
41 * with a call to drm_universal_plane_init().
42 *
43 * Cursor and overlay planes are optional. All drivers should provide one
44 * primary plane per CRTC to avoid surprising userspace too much. See enum
45 * drm_plane_type for a more in-depth discussion of these special uapi-relevant
46 * plane types. Special planes are associated with their CRTC by calling
47 * drm_crtc_init_with_planes().
48 *
49 * The type of a plane is exposed in the immutable "type" enumeration property,
50 * which has one of the following values: "Overlay", "Primary", "Cursor".
51 */
52
drm_num_planes(struct drm_device * dev)53 static unsigned int drm_num_planes(struct drm_device *dev)
54 {
55 unsigned int num = 0;
56 struct drm_plane *tmp;
57
58 drm_for_each_plane(tmp, dev) {
59 num++;
60 }
61
62 return num;
63 }
64
65 static inline u32 *
formats_ptr(struct drm_format_modifier_blob * blob)66 formats_ptr(struct drm_format_modifier_blob *blob)
67 {
68 return (u32 *)(((char *)blob) + blob->formats_offset);
69 }
70
71 static inline struct drm_format_modifier *
modifiers_ptr(struct drm_format_modifier_blob * blob)72 modifiers_ptr(struct drm_format_modifier_blob *blob)
73 {
74 return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset);
75 }
76
create_in_format_blob(struct drm_device * dev,struct drm_plane * plane)77 static int create_in_format_blob(struct drm_device *dev, struct drm_plane *plane)
78 {
79 const struct drm_mode_config *config = &dev->mode_config;
80 struct drm_property_blob *blob;
81 struct drm_format_modifier *mod;
82 size_t blob_size, formats_size, modifiers_size;
83 struct drm_format_modifier_blob *blob_data;
84 unsigned int i, j;
85
86 formats_size = sizeof(__u32) * plane->format_count;
87 if (WARN_ON(!formats_size)) {
88 /* 0 formats are never expected */
89 return 0;
90 }
91
92 modifiers_size =
93 sizeof(struct drm_format_modifier) * plane->modifier_count;
94
95 blob_size = sizeof(struct drm_format_modifier_blob);
96 /* Modifiers offset is a pointer to a struct with a 64 bit field so it
97 * should be naturally aligned to 8B.
98 */
99 BUILD_BUG_ON(sizeof(struct drm_format_modifier_blob) % 8);
100 blob_size += ALIGN(formats_size, 8);
101 blob_size += modifiers_size;
102
103 blob = drm_property_create_blob(dev, blob_size, NULL);
104 if (IS_ERR(blob))
105 return -1;
106
107 blob_data = blob->data;
108 blob_data->version = FORMAT_BLOB_CURRENT;
109 blob_data->count_formats = plane->format_count;
110 blob_data->formats_offset = sizeof(struct drm_format_modifier_blob);
111 blob_data->count_modifiers = plane->modifier_count;
112
113 blob_data->modifiers_offset =
114 ALIGN(blob_data->formats_offset + formats_size, 8);
115
116 memcpy(formats_ptr(blob_data), plane->format_types, formats_size);
117
118 /* If we can't determine support, just bail */
119 if (!plane->funcs->format_mod_supported)
120 goto done;
121
122 mod = modifiers_ptr(blob_data);
123 for (i = 0; i < plane->modifier_count; i++) {
124 for (j = 0; j < plane->format_count; j++) {
125 if (plane->funcs->format_mod_supported(plane,
126 plane->format_types[j],
127 plane->modifiers[i])) {
128
129 mod->formats |= 1ULL << j;
130 }
131 }
132
133 mod->modifier = plane->modifiers[i];
134 mod->offset = 0;
135 mod->pad = 0;
136 mod++;
137 }
138
139 done:
140 drm_object_attach_property(&plane->base, config->modifiers_property,
141 blob->base.id);
142
143 return 0;
144 }
145
146 /**
147 * drm_universal_plane_init - Initialize a new universal plane object
148 * @dev: DRM device
149 * @plane: plane object to init
150 * @possible_crtcs: bitmask of possible CRTCs
151 * @funcs: callbacks for the new plane
152 * @formats: array of supported formats (DRM_FORMAT\_\*)
153 * @format_count: number of elements in @formats
154 * @format_modifiers: array of struct drm_format modifiers terminated by
155 * DRM_FORMAT_MOD_INVALID
156 * @type: type of plane (overlay, primary, cursor)
157 * @name: printf style format string for the plane name, or NULL for default name
158 *
159 * Initializes a plane object of type @type.
160 *
161 * Returns:
162 * Zero on success, error code on failure.
163 */
drm_universal_plane_init(struct drm_device * dev,struct drm_plane * plane,uint32_t possible_crtcs,const struct drm_plane_funcs * funcs,const uint32_t * formats,unsigned int format_count,const uint64_t * format_modifiers,enum drm_plane_type type,const char * name,...)164 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
165 uint32_t possible_crtcs,
166 const struct drm_plane_funcs *funcs,
167 const uint32_t *formats, unsigned int format_count,
168 const uint64_t *format_modifiers,
169 enum drm_plane_type type,
170 const char *name, ...)
171 {
172 struct drm_mode_config *config = &dev->mode_config;
173 unsigned int format_modifier_count = 0;
174 int ret;
175
176 /* plane index is used with 32bit bitmasks */
177 if (WARN_ON(config->num_total_plane >= 32))
178 return -EINVAL;
179
180 WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
181 (!funcs->atomic_destroy_state ||
182 !funcs->atomic_duplicate_state));
183
184 ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
185 if (ret)
186 return ret;
187
188 drm_modeset_lock_init(&plane->mutex);
189
190 plane->base.properties = &plane->properties;
191 plane->dev = dev;
192 plane->funcs = funcs;
193 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
194 GFP_KERNEL);
195 if (!plane->format_types) {
196 DRM_DEBUG_KMS("out of memory when allocating plane\n");
197 drm_mode_object_unregister(dev, &plane->base);
198 return -ENOMEM;
199 }
200
201 /*
202 * First driver to need more than 64 formats needs to fix this. Each
203 * format is encoded as a bit and the current code only supports a u64.
204 */
205 if (WARN_ON(format_count > 64))
206 return -EINVAL;
207
208 if (format_modifiers) {
209 const uint64_t *temp_modifiers = format_modifiers;
210 while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
211 format_modifier_count++;
212 }
213
214 if (format_modifier_count)
215 config->allow_fb_modifiers = true;
216
217 plane->modifier_count = format_modifier_count;
218 plane->modifiers = kmalloc_array(format_modifier_count,
219 sizeof(format_modifiers[0]),
220 GFP_KERNEL);
221
222 if (format_modifier_count && !plane->modifiers) {
223 DRM_DEBUG_KMS("out of memory when allocating plane\n");
224 kfree(plane->format_types);
225 drm_mode_object_unregister(dev, &plane->base);
226 return -ENOMEM;
227 }
228
229 if (name) {
230 va_list ap;
231
232 va_start(ap, name);
233 plane->name = kvasprintf(GFP_KERNEL, name, ap);
234 va_end(ap);
235 } else {
236 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
237 drm_num_planes(dev));
238 }
239 if (!plane->name) {
240 kfree(plane->format_types);
241 kfree(plane->modifiers);
242 drm_mode_object_unregister(dev, &plane->base);
243 return -ENOMEM;
244 }
245
246 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
247 plane->format_count = format_count;
248 memcpy(plane->modifiers, format_modifiers,
249 format_modifier_count * sizeof(format_modifiers[0]));
250 plane->possible_crtcs = possible_crtcs;
251 plane->type = type;
252
253 list_add_tail(&plane->head, &config->plane_list);
254 plane->index = config->num_total_plane++;
255
256 drm_object_attach_property(&plane->base,
257 config->plane_type_property,
258 plane->type);
259
260 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
261 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
262 drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
263 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
264 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
265 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
266 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
267 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
268 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
269 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
270 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
271 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
272 }
273
274 if (config->allow_fb_modifiers)
275 create_in_format_blob(dev, plane);
276
277 return 0;
278 }
279 EXPORT_SYMBOL(drm_universal_plane_init);
280
drm_plane_register_all(struct drm_device * dev)281 int drm_plane_register_all(struct drm_device *dev)
282 {
283 struct drm_plane *plane;
284 int ret = 0;
285
286 drm_for_each_plane(plane, dev) {
287 if (plane->funcs->late_register)
288 ret = plane->funcs->late_register(plane);
289 if (ret)
290 return ret;
291 }
292
293 return 0;
294 }
295
drm_plane_unregister_all(struct drm_device * dev)296 void drm_plane_unregister_all(struct drm_device *dev)
297 {
298 struct drm_plane *plane;
299
300 drm_for_each_plane(plane, dev) {
301 if (plane->funcs->early_unregister)
302 plane->funcs->early_unregister(plane);
303 }
304 }
305
306 /**
307 * drm_plane_init - Initialize a legacy plane
308 * @dev: DRM device
309 * @plane: plane object to init
310 * @possible_crtcs: bitmask of possible CRTCs
311 * @funcs: callbacks for the new plane
312 * @formats: array of supported formats (DRM_FORMAT\_\*)
313 * @format_count: number of elements in @formats
314 * @is_primary: plane type (primary vs overlay)
315 *
316 * Legacy API to initialize a DRM plane.
317 *
318 * New drivers should call drm_universal_plane_init() instead.
319 *
320 * Returns:
321 * Zero on success, error code on failure.
322 */
drm_plane_init(struct drm_device * dev,struct drm_plane * plane,uint32_t possible_crtcs,const struct drm_plane_funcs * funcs,const uint32_t * formats,unsigned int format_count,bool is_primary)323 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
324 uint32_t possible_crtcs,
325 const struct drm_plane_funcs *funcs,
326 const uint32_t *formats, unsigned int format_count,
327 bool is_primary)
328 {
329 enum drm_plane_type type;
330
331 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
332 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
333 formats, format_count,
334 NULL, type, NULL);
335 }
336 EXPORT_SYMBOL(drm_plane_init);
337
338 /**
339 * drm_plane_cleanup - Clean up the core plane usage
340 * @plane: plane to cleanup
341 *
342 * This function cleans up @plane and removes it from the DRM mode setting
343 * core. Note that the function does *not* free the plane structure itself,
344 * this is the responsibility of the caller.
345 */
drm_plane_cleanup(struct drm_plane * plane)346 void drm_plane_cleanup(struct drm_plane *plane)
347 {
348 struct drm_device *dev = plane->dev;
349
350 drm_modeset_lock_fini(&plane->mutex);
351
352 kfree(plane->format_types);
353 kfree(plane->modifiers);
354 drm_mode_object_unregister(dev, &plane->base);
355
356 BUG_ON(list_empty(&plane->head));
357
358 /* Note that the plane_list is considered to be static; should we
359 * remove the drm_plane at runtime we would have to decrement all
360 * the indices on the drm_plane after us in the plane_list.
361 */
362
363 list_del(&plane->head);
364 dev->mode_config.num_total_plane--;
365
366 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
367 if (plane->state && plane->funcs->atomic_destroy_state)
368 plane->funcs->atomic_destroy_state(plane, plane->state);
369
370 kfree(plane->name);
371
372 memset(plane, 0, sizeof(*plane));
373 }
374 EXPORT_SYMBOL(drm_plane_cleanup);
375
376 /**
377 * drm_plane_from_index - find the registered plane at an index
378 * @dev: DRM device
379 * @idx: index of registered plane to find for
380 *
381 * Given a plane index, return the registered plane from DRM device's
382 * list of planes with matching index. This is the inverse of drm_plane_index().
383 */
384 struct drm_plane *
drm_plane_from_index(struct drm_device * dev,int idx)385 drm_plane_from_index(struct drm_device *dev, int idx)
386 {
387 struct drm_plane *plane;
388
389 drm_for_each_plane(plane, dev)
390 if (idx == plane->index)
391 return plane;
392
393 return NULL;
394 }
395 EXPORT_SYMBOL(drm_plane_from_index);
396
397 /**
398 * drm_plane_force_disable - Forcibly disable a plane
399 * @plane: plane to disable
400 *
401 * Forces the plane to be disabled.
402 *
403 * Used when the plane's current framebuffer is destroyed,
404 * and when restoring fbdev mode.
405 *
406 * Note that this function is not suitable for atomic drivers, since it doesn't
407 * wire through the lock acquisition context properly and hence can't handle
408 * retries or driver private locks. You probably want to use
409 * drm_atomic_helper_disable_plane() or
410 * drm_atomic_helper_disable_planes_on_crtc() instead.
411 */
drm_plane_force_disable(struct drm_plane * plane)412 void drm_plane_force_disable(struct drm_plane *plane)
413 {
414 int ret;
415
416 if (!plane->fb)
417 return;
418
419 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
420
421 plane->old_fb = plane->fb;
422 ret = plane->funcs->disable_plane(plane, NULL);
423 if (ret) {
424 DRM_ERROR("failed to disable plane with busy fb\n");
425 plane->old_fb = NULL;
426 return;
427 }
428 /* disconnect the plane from the fb and crtc: */
429 drm_framebuffer_put(plane->old_fb);
430 plane->old_fb = NULL;
431 plane->fb = NULL;
432 plane->crtc = NULL;
433 }
434 EXPORT_SYMBOL(drm_plane_force_disable);
435
436 /**
437 * drm_mode_plane_set_obj_prop - set the value of a property
438 * @plane: drm plane object to set property value for
439 * @property: property to set
440 * @value: value the property should be set to
441 *
442 * This functions sets a given property on a given plane object. This function
443 * calls the driver's ->set_property callback and changes the software state of
444 * the property if the callback succeeds.
445 *
446 * Returns:
447 * Zero on success, error code on failure.
448 */
drm_mode_plane_set_obj_prop(struct drm_plane * plane,struct drm_property * property,uint64_t value)449 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
450 struct drm_property *property,
451 uint64_t value)
452 {
453 int ret = -EINVAL;
454 struct drm_mode_object *obj = &plane->base;
455
456 if (plane->funcs->set_property)
457 ret = plane->funcs->set_property(plane, property, value);
458 if (!ret)
459 drm_object_property_set_value(obj, property, value);
460
461 return ret;
462 }
463 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
464
drm_mode_getplane_res(struct drm_device * dev,void * data,struct drm_file * file_priv)465 int drm_mode_getplane_res(struct drm_device *dev, void *data,
466 struct drm_file *file_priv)
467 {
468 struct drm_mode_get_plane_res *plane_resp = data;
469 struct drm_mode_config *config;
470 struct drm_plane *plane;
471 uint32_t __user *plane_ptr;
472 int count = 0;
473
474 if (!drm_core_check_feature(dev, DRIVER_MODESET))
475 return -EINVAL;
476
477 config = &dev->mode_config;
478 plane_ptr = u64_to_user_ptr(plane_resp->plane_id_ptr);
479
480 /*
481 * This ioctl is called twice, once to determine how much space is
482 * needed, and the 2nd time to fill it.
483 */
484 drm_for_each_plane(plane, dev) {
485 /*
486 * Unless userspace set the 'universal planes'
487 * capability bit, only advertise overlays.
488 */
489 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
490 !file_priv->universal_planes)
491 continue;
492
493 if (drm_lease_held(file_priv, plane->base.id)) {
494 if (count < plane_resp->count_planes &&
495 put_user(plane->base.id, plane_ptr + count))
496 return -EFAULT;
497 count++;
498 }
499 }
500 plane_resp->count_planes = count;
501
502 return 0;
503 }
504
drm_mode_getplane(struct drm_device * dev,void * data,struct drm_file * file_priv)505 int drm_mode_getplane(struct drm_device *dev, void *data,
506 struct drm_file *file_priv)
507 {
508 struct drm_mode_get_plane *plane_resp = data;
509 struct drm_plane *plane;
510 uint32_t __user *format_ptr;
511
512 if (!drm_core_check_feature(dev, DRIVER_MODESET))
513 return -EINVAL;
514
515 plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
516 if (!plane)
517 return -ENOENT;
518
519 drm_modeset_lock(&plane->mutex, NULL);
520 if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id))
521 plane_resp->crtc_id = plane->state->crtc->base.id;
522 else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id))
523 plane_resp->crtc_id = plane->crtc->base.id;
524 else
525 plane_resp->crtc_id = 0;
526
527 if (plane->state && plane->state->fb)
528 plane_resp->fb_id = plane->state->fb->base.id;
529 else if (!plane->state && plane->fb)
530 plane_resp->fb_id = plane->fb->base.id;
531 else
532 plane_resp->fb_id = 0;
533 drm_modeset_unlock(&plane->mutex);
534
535 plane_resp->plane_id = plane->base.id;
536 plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
537 plane->possible_crtcs);
538
539 plane_resp->gamma_size = 0;
540
541 /*
542 * This ioctl is called twice, once to determine how much space is
543 * needed, and the 2nd time to fill it.
544 */
545 if (plane->format_count &&
546 (plane_resp->count_format_types >= plane->format_count)) {
547 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
548 if (copy_to_user(format_ptr,
549 plane->format_types,
550 sizeof(uint32_t) * plane->format_count)) {
551 return -EFAULT;
552 }
553 }
554 plane_resp->count_format_types = plane->format_count;
555
556 return 0;
557 }
558
drm_plane_check_pixel_format(struct drm_plane * plane,u32 format,u64 modifier)559 int drm_plane_check_pixel_format(struct drm_plane *plane,
560 u32 format, u64 modifier)
561 {
562 unsigned int i;
563
564 for (i = 0; i < plane->format_count; i++) {
565 if (format == plane->format_types[i])
566 break;
567 }
568 if (i == plane->format_count)
569 return -EINVAL;
570
571 if (plane->funcs->format_mod_supported) {
572 if (!plane->funcs->format_mod_supported(plane, format, modifier))
573 return -EINVAL;
574 } else {
575 if (!plane->modifier_count)
576 return 0;
577
578 for (i = 0; i < plane->modifier_count; i++) {
579 if (modifier == plane->modifiers[i])
580 break;
581 }
582 if (i == plane->modifier_count)
583 return -EINVAL;
584 }
585
586 return 0;
587 }
588
__setplane_check(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int32_t crtc_x,int32_t crtc_y,uint32_t crtc_w,uint32_t crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h)589 static int __setplane_check(struct drm_plane *plane,
590 struct drm_crtc *crtc,
591 struct drm_framebuffer *fb,
592 int32_t crtc_x, int32_t crtc_y,
593 uint32_t crtc_w, uint32_t crtc_h,
594 uint32_t src_x, uint32_t src_y,
595 uint32_t src_w, uint32_t src_h)
596 {
597 int ret;
598
599 /* Check whether this plane is usable on this CRTC */
600 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
601 DRM_DEBUG_KMS("Invalid crtc for plane\n");
602 return -EINVAL;
603 }
604
605 /* Check whether this plane supports the fb pixel format. */
606 ret = drm_plane_check_pixel_format(plane, fb->format->format,
607 fb->modifier);
608 if (ret) {
609 struct drm_format_name_buf format_name;
610
611 DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
612 drm_get_format_name(fb->format->format,
613 &format_name),
614 fb->modifier);
615 return ret;
616 }
617
618 /* Give drivers some help against integer overflows */
619 if (crtc_w > INT_MAX ||
620 crtc_x > INT_MAX - (int32_t) crtc_w ||
621 crtc_h > INT_MAX ||
622 crtc_y > INT_MAX - (int32_t) crtc_h) {
623 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
624 crtc_w, crtc_h, crtc_x, crtc_y);
625 return -ERANGE;
626 }
627
628 ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
629 if (ret)
630 return ret;
631
632 return 0;
633 }
634
635 /*
636 * __setplane_internal - setplane handler for internal callers
637 *
638 * This function will take a reference on the new fb for the plane
639 * on success.
640 *
641 * src_{x,y,w,h} are provided in 16.16 fixed point format
642 */
__setplane_internal(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int32_t crtc_x,int32_t crtc_y,uint32_t crtc_w,uint32_t crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h,struct drm_modeset_acquire_ctx * ctx)643 static int __setplane_internal(struct drm_plane *plane,
644 struct drm_crtc *crtc,
645 struct drm_framebuffer *fb,
646 int32_t crtc_x, int32_t crtc_y,
647 uint32_t crtc_w, uint32_t crtc_h,
648 /* src_{x,y,w,h} values are 16.16 fixed point */
649 uint32_t src_x, uint32_t src_y,
650 uint32_t src_w, uint32_t src_h,
651 struct drm_modeset_acquire_ctx *ctx)
652 {
653 int ret = 0;
654
655 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
656
657 /* No fb means shut it down */
658 if (!fb) {
659 plane->old_fb = plane->fb;
660 ret = plane->funcs->disable_plane(plane, ctx);
661 if (!ret) {
662 plane->crtc = NULL;
663 plane->fb = NULL;
664 } else {
665 plane->old_fb = NULL;
666 }
667 goto out;
668 }
669
670 ret = __setplane_check(plane, crtc, fb,
671 crtc_x, crtc_y, crtc_w, crtc_h,
672 src_x, src_y, src_w, src_h);
673 if (ret)
674 goto out;
675
676 plane->old_fb = plane->fb;
677 ret = plane->funcs->update_plane(plane, crtc, fb,
678 crtc_x, crtc_y, crtc_w, crtc_h,
679 src_x, src_y, src_w, src_h, ctx);
680 if (!ret) {
681 plane->crtc = crtc;
682 plane->fb = fb;
683 drm_framebuffer_get(plane->fb);
684 } else {
685 plane->old_fb = NULL;
686 }
687
688 out:
689 if (plane->old_fb)
690 drm_framebuffer_put(plane->old_fb);
691 plane->old_fb = NULL;
692
693 return ret;
694 }
695
__setplane_atomic(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int32_t crtc_x,int32_t crtc_y,uint32_t crtc_w,uint32_t crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h,struct drm_modeset_acquire_ctx * ctx)696 static int __setplane_atomic(struct drm_plane *plane,
697 struct drm_crtc *crtc,
698 struct drm_framebuffer *fb,
699 int32_t crtc_x, int32_t crtc_y,
700 uint32_t crtc_w, uint32_t crtc_h,
701 uint32_t src_x, uint32_t src_y,
702 uint32_t src_w, uint32_t src_h,
703 struct drm_modeset_acquire_ctx *ctx)
704 {
705 int ret;
706
707 WARN_ON(!drm_drv_uses_atomic_modeset(plane->dev));
708
709 /* No fb means shut it down */
710 if (!fb)
711 return plane->funcs->disable_plane(plane, ctx);
712
713 /*
714 * FIXME: This is redundant with drm_atomic_plane_check(),
715 * but the legacy cursor/"async" .update_plane() tricks
716 * don't call that so we still need this here. Should remove
717 * this when all .update_plane() implementations have been
718 * fixed to call drm_atomic_plane_check().
719 */
720 ret = __setplane_check(plane, crtc, fb,
721 crtc_x, crtc_y, crtc_w, crtc_h,
722 src_x, src_y, src_w, src_h);
723 if (ret)
724 return ret;
725
726 return plane->funcs->update_plane(plane, crtc, fb,
727 crtc_x, crtc_y, crtc_w, crtc_h,
728 src_x, src_y, src_w, src_h, ctx);
729 }
730
setplane_internal(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int32_t crtc_x,int32_t crtc_y,uint32_t crtc_w,uint32_t crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h)731 static int setplane_internal(struct drm_plane *plane,
732 struct drm_crtc *crtc,
733 struct drm_framebuffer *fb,
734 int32_t crtc_x, int32_t crtc_y,
735 uint32_t crtc_w, uint32_t crtc_h,
736 /* src_{x,y,w,h} values are 16.16 fixed point */
737 uint32_t src_x, uint32_t src_y,
738 uint32_t src_w, uint32_t src_h)
739 {
740 struct drm_modeset_acquire_ctx ctx;
741 int ret;
742
743 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
744 retry:
745 ret = drm_modeset_lock_all_ctx(plane->dev, &ctx);
746 if (ret)
747 goto fail;
748
749 if (drm_drv_uses_atomic_modeset(plane->dev))
750 ret = __setplane_atomic(plane, crtc, fb,
751 crtc_x, crtc_y, crtc_w, crtc_h,
752 src_x, src_y, src_w, src_h, &ctx);
753 else
754 ret = __setplane_internal(plane, crtc, fb,
755 crtc_x, crtc_y, crtc_w, crtc_h,
756 src_x, src_y, src_w, src_h, &ctx);
757
758 fail:
759 if (ret == -EDEADLK) {
760 ret = drm_modeset_backoff(&ctx);
761 if (!ret)
762 goto retry;
763 }
764 drm_modeset_drop_locks(&ctx);
765 drm_modeset_acquire_fini(&ctx);
766
767 return ret;
768 }
769
drm_mode_setplane(struct drm_device * dev,void * data,struct drm_file * file_priv)770 int drm_mode_setplane(struct drm_device *dev, void *data,
771 struct drm_file *file_priv)
772 {
773 struct drm_mode_set_plane *plane_req = data;
774 struct drm_plane *plane;
775 struct drm_crtc *crtc = NULL;
776 struct drm_framebuffer *fb = NULL;
777 int ret;
778
779 if (!drm_core_check_feature(dev, DRIVER_MODESET))
780 return -EINVAL;
781
782 /*
783 * First, find the plane, crtc, and fb objects. If not available,
784 * we don't bother to call the driver.
785 */
786 plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
787 if (!plane) {
788 DRM_DEBUG_KMS("Unknown plane ID %d\n",
789 plane_req->plane_id);
790 return -ENOENT;
791 }
792
793 if (plane_req->fb_id) {
794 fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
795 if (!fb) {
796 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
797 plane_req->fb_id);
798 return -ENOENT;
799 }
800
801 crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
802 if (!crtc) {
803 drm_framebuffer_put(fb);
804 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
805 plane_req->crtc_id);
806 return -ENOENT;
807 }
808 }
809
810 ret = setplane_internal(plane, crtc, fb,
811 plane_req->crtc_x, plane_req->crtc_y,
812 plane_req->crtc_w, plane_req->crtc_h,
813 plane_req->src_x, plane_req->src_y,
814 plane_req->src_w, plane_req->src_h);
815
816 if (fb)
817 drm_framebuffer_put(fb);
818
819 return ret;
820 }
821
drm_mode_cursor_universal(struct drm_crtc * crtc,struct drm_mode_cursor2 * req,struct drm_file * file_priv,struct drm_modeset_acquire_ctx * ctx)822 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
823 struct drm_mode_cursor2 *req,
824 struct drm_file *file_priv,
825 struct drm_modeset_acquire_ctx *ctx)
826 {
827 struct drm_device *dev = crtc->dev;
828 struct drm_plane *plane = crtc->cursor;
829 struct drm_framebuffer *fb = NULL;
830 struct drm_mode_fb_cmd2 fbreq = {
831 .width = req->width,
832 .height = req->height,
833 .pixel_format = DRM_FORMAT_ARGB8888,
834 .pitches = { req->width * 4 },
835 .handles = { req->handle },
836 };
837 int32_t crtc_x, crtc_y;
838 uint32_t crtc_w = 0, crtc_h = 0;
839 uint32_t src_w = 0, src_h = 0;
840 int ret = 0;
841
842 BUG_ON(!plane);
843 WARN_ON(plane->crtc != crtc && plane->crtc != NULL);
844
845 /*
846 * Obtain fb we'll be using (either new or existing) and take an extra
847 * reference to it if fb != null. setplane will take care of dropping
848 * the reference if the plane update fails.
849 */
850 if (req->flags & DRM_MODE_CURSOR_BO) {
851 if (req->handle) {
852 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
853 if (IS_ERR(fb)) {
854 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
855 return PTR_ERR(fb);
856 }
857
858 fb->hot_x = req->hot_x;
859 fb->hot_y = req->hot_y;
860 } else {
861 fb = NULL;
862 }
863 } else {
864 if (plane->state)
865 fb = plane->state->fb;
866 else
867 fb = plane->fb;
868
869 if (fb)
870 drm_framebuffer_get(fb);
871 }
872
873 if (req->flags & DRM_MODE_CURSOR_MOVE) {
874 crtc_x = req->x;
875 crtc_y = req->y;
876 } else {
877 crtc_x = crtc->cursor_x;
878 crtc_y = crtc->cursor_y;
879 }
880
881 if (fb) {
882 crtc_w = fb->width;
883 crtc_h = fb->height;
884 src_w = fb->width << 16;
885 src_h = fb->height << 16;
886 }
887
888 if (drm_drv_uses_atomic_modeset(dev))
889 ret = __setplane_atomic(plane, crtc, fb,
890 crtc_x, crtc_y, crtc_w, crtc_h,
891 0, 0, src_w, src_h, ctx);
892 else
893 ret = __setplane_internal(plane, crtc, fb,
894 crtc_x, crtc_y, crtc_w, crtc_h,
895 0, 0, src_w, src_h, ctx);
896
897 if (fb)
898 drm_framebuffer_put(fb);
899
900 /* Update successful; save new cursor position, if necessary */
901 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
902 crtc->cursor_x = req->x;
903 crtc->cursor_y = req->y;
904 }
905
906 return ret;
907 }
908
drm_mode_cursor_common(struct drm_device * dev,struct drm_mode_cursor2 * req,struct drm_file * file_priv)909 static int drm_mode_cursor_common(struct drm_device *dev,
910 struct drm_mode_cursor2 *req,
911 struct drm_file *file_priv)
912 {
913 struct drm_crtc *crtc;
914 struct drm_modeset_acquire_ctx ctx;
915 int ret = 0;
916
917 if (!drm_core_check_feature(dev, DRIVER_MODESET))
918 return -EINVAL;
919
920 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
921 return -EINVAL;
922
923 crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
924 if (!crtc) {
925 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
926 return -ENOENT;
927 }
928
929 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
930 retry:
931 ret = drm_modeset_lock(&crtc->mutex, &ctx);
932 if (ret)
933 goto out;
934 /*
935 * If this crtc has a universal cursor plane, call that plane's update
936 * handler rather than using legacy cursor handlers.
937 */
938 if (crtc->cursor) {
939 ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
940 if (ret)
941 goto out;
942
943 if (!drm_lease_held(file_priv, crtc->cursor->base.id)) {
944 ret = -EACCES;
945 goto out;
946 }
947
948 ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
949 goto out;
950 }
951
952 if (req->flags & DRM_MODE_CURSOR_BO) {
953 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
954 ret = -ENXIO;
955 goto out;
956 }
957 /* Turns off the cursor if handle is 0 */
958 if (crtc->funcs->cursor_set2)
959 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
960 req->width, req->height, req->hot_x, req->hot_y);
961 else
962 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
963 req->width, req->height);
964 }
965
966 if (req->flags & DRM_MODE_CURSOR_MOVE) {
967 if (crtc->funcs->cursor_move) {
968 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
969 } else {
970 ret = -EFAULT;
971 goto out;
972 }
973 }
974 out:
975 if (ret == -EDEADLK) {
976 ret = drm_modeset_backoff(&ctx);
977 if (!ret)
978 goto retry;
979 }
980
981 drm_modeset_drop_locks(&ctx);
982 drm_modeset_acquire_fini(&ctx);
983
984 return ret;
985
986 }
987
988
drm_mode_cursor_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)989 int drm_mode_cursor_ioctl(struct drm_device *dev,
990 void *data, struct drm_file *file_priv)
991 {
992 struct drm_mode_cursor *req = data;
993 struct drm_mode_cursor2 new_req;
994
995 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
996 new_req.hot_x = new_req.hot_y = 0;
997
998 return drm_mode_cursor_common(dev, &new_req, file_priv);
999 }
1000
1001 /*
1002 * Set the cursor configuration based on user request. This implements the 2nd
1003 * version of the cursor ioctl, which allows userspace to additionally specify
1004 * the hotspot of the pointer.
1005 */
drm_mode_cursor2_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1006 int drm_mode_cursor2_ioctl(struct drm_device *dev,
1007 void *data, struct drm_file *file_priv)
1008 {
1009 struct drm_mode_cursor2 *req = data;
1010
1011 return drm_mode_cursor_common(dev, req, file_priv);
1012 }
1013
drm_mode_page_flip_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1014 int drm_mode_page_flip_ioctl(struct drm_device *dev,
1015 void *data, struct drm_file *file_priv)
1016 {
1017 struct drm_mode_crtc_page_flip_target *page_flip = data;
1018 struct drm_crtc *crtc;
1019 struct drm_plane *plane;
1020 struct drm_framebuffer *fb = NULL, *old_fb;
1021 struct drm_pending_vblank_event *e = NULL;
1022 u32 target_vblank = page_flip->sequence;
1023 struct drm_modeset_acquire_ctx ctx;
1024 int ret = -EINVAL;
1025
1026 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1027 return -EINVAL;
1028
1029 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
1030 return -EINVAL;
1031
1032 if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
1033 return -EINVAL;
1034
1035 /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
1036 * can be specified
1037 */
1038 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
1039 return -EINVAL;
1040
1041 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
1042 return -EINVAL;
1043
1044 crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id);
1045 if (!crtc)
1046 return -ENOENT;
1047
1048 plane = crtc->primary;
1049
1050 if (!drm_lease_held(file_priv, plane->base.id))
1051 return -EACCES;
1052
1053 if (crtc->funcs->page_flip_target) {
1054 u32 current_vblank;
1055 int r;
1056
1057 r = drm_crtc_vblank_get(crtc);
1058 if (r)
1059 return r;
1060
1061 current_vblank = (u32)drm_crtc_vblank_count(crtc);
1062
1063 switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
1064 case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
1065 if ((int)(target_vblank - current_vblank) > 1) {
1066 DRM_DEBUG("Invalid absolute flip target %u, "
1067 "must be <= %u\n", target_vblank,
1068 current_vblank + 1);
1069 drm_crtc_vblank_put(crtc);
1070 return -EINVAL;
1071 }
1072 break;
1073 case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
1074 if (target_vblank != 0 && target_vblank != 1) {
1075 DRM_DEBUG("Invalid relative flip target %u, "
1076 "must be 0 or 1\n", target_vblank);
1077 drm_crtc_vblank_put(crtc);
1078 return -EINVAL;
1079 }
1080 target_vblank += current_vblank;
1081 break;
1082 default:
1083 target_vblank = current_vblank +
1084 !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
1085 break;
1086 }
1087 } else if (crtc->funcs->page_flip == NULL ||
1088 (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
1089 return -EINVAL;
1090 }
1091
1092 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1093 retry:
1094 ret = drm_modeset_lock(&crtc->mutex, &ctx);
1095 if (ret)
1096 goto out;
1097 ret = drm_modeset_lock(&plane->mutex, &ctx);
1098 if (ret)
1099 goto out;
1100
1101 if (plane->state)
1102 old_fb = plane->state->fb;
1103 else
1104 old_fb = plane->fb;
1105
1106 if (old_fb == NULL) {
1107 /* The framebuffer is currently unbound, presumably
1108 * due to a hotplug event, that userspace has not
1109 * yet discovered.
1110 */
1111 ret = -EBUSY;
1112 goto out;
1113 }
1114
1115 fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id);
1116 if (!fb) {
1117 ret = -ENOENT;
1118 goto out;
1119 }
1120
1121 if (plane->state) {
1122 const struct drm_plane_state *state = plane->state;
1123
1124 ret = drm_framebuffer_check_src_coords(state->src_x,
1125 state->src_y,
1126 state->src_w,
1127 state->src_h,
1128 fb);
1129 } else {
1130 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y,
1131 &crtc->mode, fb);
1132 }
1133 if (ret)
1134 goto out;
1135
1136 if (old_fb->format != fb->format) {
1137 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
1138 ret = -EINVAL;
1139 goto out;
1140 }
1141
1142 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1143 e = kzalloc(sizeof *e, GFP_KERNEL);
1144 if (!e) {
1145 ret = -ENOMEM;
1146 goto out;
1147 }
1148
1149 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1150 e->event.base.length = sizeof(e->event);
1151 e->event.vbl.user_data = page_flip->user_data;
1152 e->event.vbl.crtc_id = crtc->base.id;
1153
1154 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
1155 if (ret) {
1156 kfree(e);
1157 e = NULL;
1158 goto out;
1159 }
1160 }
1161
1162 plane->old_fb = plane->fb;
1163 if (crtc->funcs->page_flip_target)
1164 ret = crtc->funcs->page_flip_target(crtc, fb, e,
1165 page_flip->flags,
1166 target_vblank,
1167 &ctx);
1168 else
1169 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
1170 &ctx);
1171 if (ret) {
1172 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
1173 drm_event_cancel_free(dev, &e->base);
1174 /* Keep the old fb, don't unref it. */
1175 plane->old_fb = NULL;
1176 } else {
1177 if (!plane->state) {
1178 plane->fb = fb;
1179 drm_framebuffer_get(fb);
1180 }
1181 }
1182
1183 out:
1184 if (fb)
1185 drm_framebuffer_put(fb);
1186 if (plane->old_fb)
1187 drm_framebuffer_put(plane->old_fb);
1188 plane->old_fb = NULL;
1189
1190 if (ret == -EDEADLK) {
1191 ret = drm_modeset_backoff(&ctx);
1192 if (!ret)
1193 goto retry;
1194 }
1195
1196 drm_modeset_drop_locks(&ctx);
1197 drm_modeset_acquire_fini(&ctx);
1198
1199 if (ret && crtc->funcs->page_flip_target)
1200 drm_crtc_vblank_put(crtc);
1201
1202 return ret;
1203 }
1204