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 <linux/export.h> 24 #include <linux/slab.h> 25 #include <linux/uaccess.h> 26 27 #include <drm/drm_plane.h> 28 #include <drm/drm_drv.h> 29 #include <drm/drm_print.h> 30 #include <drm/drm_framebuffer.h> 31 #include <drm/drm_file.h> 32 #include <drm/drm_crtc.h> 33 #include <drm/drm_fourcc.h> 34 #include <drm/drm_managed.h> 35 #include <drm/drm_vblank.h> 36 37 #include "drm_crtc_internal.h" 38 39 /** 40 * DOC: overview 41 * 42 * A plane represents an image source that can be blended with or overlaid on 43 * top of a CRTC during the scanout process. Planes take their input data from a 44 * &drm_framebuffer object. The plane itself specifies the cropping and scaling 45 * of that image, and where it is placed on the visible area of a display 46 * pipeline, represented by &drm_crtc. A plane can also have additional 47 * properties that specify how the pixels are positioned and blended, like 48 * rotation or Z-position. All these properties are stored in &drm_plane_state. 49 * 50 * Unless explicitly specified (via CRTC property or otherwise), the active area 51 * of a CRTC will be black by default. This means portions of the active area 52 * which are not covered by a plane will be black, and alpha blending of any 53 * planes with the CRTC background will blend with black at the lowest zpos. 54 * 55 * To create a plane, a KMS drivers allocates and zeroes an instances of 56 * &struct drm_plane (possibly as part of a larger structure) and registers it 57 * with a call to drm_universal_plane_init(). 58 * 59 * Each plane has a type, see enum drm_plane_type. A plane can be compatible 60 * with multiple CRTCs, see &drm_plane.possible_crtcs. 61 * 62 * Each CRTC must have a unique primary plane userspace can attach to enable 63 * the CRTC. In other words, userspace must be able to attach a different 64 * primary plane to each CRTC at the same time. Primary planes can still be 65 * compatible with multiple CRTCs. There must be exactly as many primary planes 66 * as there are CRTCs. 67 * 68 * Legacy uAPI doesn't expose the primary and cursor planes directly. DRM core 69 * relies on the driver to set the primary and optionally the cursor plane used 70 * for legacy IOCTLs. This is done by calling drm_crtc_init_with_planes(). All 71 * drivers must provide one primary plane per CRTC to avoid surprising legacy 72 * userspace too much. 73 */ 74 75 /** 76 * DOC: standard plane properties 77 * 78 * DRM planes have a few standardized properties: 79 * 80 * type: 81 * Immutable property describing the type of the plane. 82 * 83 * For user-space which has enabled the &DRM_CLIENT_CAP_ATOMIC capability, 84 * the plane type is just a hint and is mostly superseded by atomic 85 * test-only commits. The type hint can still be used to come up more 86 * easily with a plane configuration accepted by the driver. 87 * 88 * The value of this property can be one of the following: 89 * 90 * "Primary": 91 * To light up a CRTC, attaching a primary plane is the most likely to 92 * work if it covers the whole CRTC and doesn't have scaling or 93 * cropping set up. 94 * 95 * Drivers may support more features for the primary plane, user-space 96 * can find out with test-only atomic commits. 97 * 98 * Some primary planes are implicitly used by the kernel in the legacy 99 * IOCTLs &DRM_IOCTL_MODE_SETCRTC and &DRM_IOCTL_MODE_PAGE_FLIP. 100 * Therefore user-space must not mix explicit usage of any primary 101 * plane (e.g. through an atomic commit) with these legacy IOCTLs. 102 * 103 * "Cursor": 104 * To enable this plane, using a framebuffer configured without scaling 105 * or cropping and with the following properties is the most likely to 106 * work: 107 * 108 * - If the driver provides the capabilities &DRM_CAP_CURSOR_WIDTH and 109 * &DRM_CAP_CURSOR_HEIGHT, create the framebuffer with this size. 110 * Otherwise, create a framebuffer with the size 64x64. 111 * - If the driver doesn't support modifiers, create a framebuffer with 112 * a linear layout. Otherwise, use the IN_FORMATS plane property. 113 * 114 * Drivers may support more features for the cursor plane, user-space 115 * can find out with test-only atomic commits. 116 * 117 * Some cursor planes are implicitly used by the kernel in the legacy 118 * IOCTLs &DRM_IOCTL_MODE_CURSOR and &DRM_IOCTL_MODE_CURSOR2. 119 * Therefore user-space must not mix explicit usage of any cursor 120 * plane (e.g. through an atomic commit) with these legacy IOCTLs. 121 * 122 * Some drivers may support cursors even if no cursor plane is exposed. 123 * In this case, the legacy cursor IOCTLs can be used to configure the 124 * cursor. 125 * 126 * "Overlay": 127 * Neither primary nor cursor. 128 * 129 * Overlay planes are the only planes exposed when the 130 * &DRM_CLIENT_CAP_UNIVERSAL_PLANES capability is disabled. 131 * 132 * IN_FORMATS: 133 * Blob property which contains the set of buffer format and modifier 134 * pairs supported by this plane. The blob is a struct 135 * drm_format_modifier_blob. Without this property the plane doesn't 136 * support buffers with modifiers. Userspace cannot change this property. 137 * 138 * Note that userspace can check the &DRM_CAP_ADDFB2_MODIFIERS driver 139 * capability for general modifier support. If this flag is set then every 140 * plane will have the IN_FORMATS property, even when it only supports 141 * DRM_FORMAT_MOD_LINEAR. Before linux kernel release v5.1 there have been 142 * various bugs in this area with inconsistencies between the capability 143 * flag and per-plane properties. 144 * 145 * IN_FORMATS_ASYNC: 146 * Blob property which contains the set of buffer format and modifier 147 * pairs supported by this plane for asynchronous flips. The blob is a struct 148 * drm_format_modifier_blob. Userspace cannot change this property. This is an 149 * optional property and if not present then user should expect a failure in 150 * atomic ioctl when the modifier/format is not supported by that plane under 151 * asynchronous flip. 152 * 153 * SIZE_HINTS: 154 * Blob property which contains the set of recommended plane size 155 * which can used for simple "cursor like" use cases (eg. no scaling). 156 * Using these hints frees userspace from extensive probing of 157 * supported plane sizes through atomic/setcursor ioctls. 158 * 159 * The blob contains an array of struct drm_plane_size_hint, in 160 * order of preference. For optimal usage userspace should pick 161 * the first size that satisfies its own requirements. 162 * 163 * Drivers should only attach this property to planes that 164 * support a very limited set of sizes. 165 * 166 * Note that property value 0 (ie. no blob) is reserved for potential 167 * future use. Current userspace is expected to ignore the property 168 * if the value is 0, and fall back to some other means (eg. 169 * &DRM_CAP_CURSOR_WIDTH and &DRM_CAP_CURSOR_HEIGHT) to determine 170 * the appropriate plane size to use. 171 */ 172 173 static unsigned int drm_num_planes(struct drm_device *dev) 174 { 175 unsigned int num = 0; 176 struct drm_plane *tmp; 177 178 drm_for_each_plane(tmp, dev) { 179 num++; 180 } 181 182 return num; 183 } 184 185 static inline u32 * 186 formats_ptr(struct drm_format_modifier_blob *blob) 187 { 188 return (u32 *)(((char *)blob) + blob->formats_offset); 189 } 190 191 static inline struct drm_format_modifier * 192 modifiers_ptr(struct drm_format_modifier_blob *blob) 193 { 194 return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset); 195 } 196 197 static struct drm_property_blob *create_in_format_blob(struct drm_device *dev, 198 struct drm_plane *plane, 199 bool (*format_mod_supported) 200 (struct drm_plane *plane, 201 u32 format, 202 u64 modifier)) 203 { 204 struct drm_property_blob *blob; 205 struct drm_format_modifier *mod; 206 size_t blob_size, formats_size, modifiers_size; 207 struct drm_format_modifier_blob *blob_data; 208 unsigned int i, j; 209 210 formats_size = sizeof(__u32) * plane->format_count; 211 if (WARN_ON(!formats_size)) { 212 /* 0 formats are never expected */ 213 return ERR_PTR(-EINVAL); 214 } 215 216 modifiers_size = 217 sizeof(struct drm_format_modifier) * plane->modifier_count; 218 219 blob_size = sizeof(struct drm_format_modifier_blob); 220 /* Modifiers offset is a pointer to a struct with a 64 bit field so it 221 * should be naturally aligned to 8B. 222 */ 223 BUILD_BUG_ON(sizeof(struct drm_format_modifier_blob) % 8); 224 blob_size += ALIGN(formats_size, 8); 225 blob_size += modifiers_size; 226 227 blob = drm_property_create_blob(dev, blob_size, NULL); 228 if (IS_ERR(blob)) 229 return blob; 230 231 blob_data = blob->data; 232 blob_data->version = FORMAT_BLOB_CURRENT; 233 blob_data->count_formats = plane->format_count; 234 blob_data->formats_offset = sizeof(struct drm_format_modifier_blob); 235 blob_data->count_modifiers = plane->modifier_count; 236 237 blob_data->modifiers_offset = 238 ALIGN(blob_data->formats_offset + formats_size, 8); 239 240 memcpy(formats_ptr(blob_data), plane->format_types, formats_size); 241 242 mod = modifiers_ptr(blob_data); 243 for (i = 0; i < plane->modifier_count; i++) { 244 for (j = 0; j < plane->format_count; j++) { 245 if (!format_mod_supported || 246 format_mod_supported(plane, 247 plane->format_types[j], 248 plane->modifiers[i])) { 249 mod->formats |= 1ULL << j; 250 } 251 } 252 253 mod->modifier = plane->modifiers[i]; 254 mod->offset = 0; 255 mod->pad = 0; 256 mod++; 257 } 258 259 return blob; 260 } 261 262 /** 263 * DOC: hotspot properties 264 * 265 * HOTSPOT_X: property to set mouse hotspot x offset. 266 * HOTSPOT_Y: property to set mouse hotspot y offset. 267 * 268 * When the plane is being used as a cursor image to display a mouse pointer, 269 * the "hotspot" is the offset within the cursor image where mouse events 270 * are expected to go. 271 * 272 * Positive values move the hotspot from the top-left corner of the cursor 273 * plane towards the right and bottom. 274 * 275 * Most display drivers do not need this information because the 276 * hotspot is not actually connected to anything visible on screen. 277 * However, this is necessary for display drivers like the para-virtualized 278 * drivers (eg qxl, vbox, virtio, vmwgfx), that are attached to a user console 279 * with a mouse pointer. Since these consoles are often being remoted over a 280 * network, they would otherwise have to wait to display the pointer movement to 281 * the user until a full network round-trip has occurred. New mouse events have 282 * to be sent from the user's console, over the network to the virtual input 283 * devices, forwarded to the desktop for processing, and then the cursor plane's 284 * position can be updated and sent back to the user's console over the network. 285 * Instead, with the hotspot information, the console can anticipate the new 286 * location, and draw the mouse cursor there before the confirmation comes in. 287 * To do that correctly, the user's console must be able predict how the 288 * desktop will process mouse events, which normally requires the desktop's 289 * mouse topology information, ie where each CRTC sits in the mouse coordinate 290 * space. This is typically sent to the para-virtualized drivers using some 291 * driver-specific method, and the driver then forwards it to the console by 292 * way of the virtual display device or hypervisor. 293 * 294 * The assumption is generally made that there is only one cursor plane being 295 * used this way at a time, and that the desktop is feeding all mouse devices 296 * into the same global pointer. Para-virtualized drivers that require this 297 * should only be exposing a single cursor plane, or find some other way 298 * to coordinate with a userspace desktop that supports multiple pointers. 299 * If the hotspot properties are set, the cursor plane is therefore assumed to be 300 * used only for displaying a mouse cursor image, and the position of the combined 301 * cursor plane + offset can therefore be used for coordinating with input from a 302 * mouse device. 303 * 304 * The cursor will then be drawn either at the location of the plane in the CRTC 305 * console, or as a free-floating cursor plane on the user's console 306 * corresponding to their desktop mouse position. 307 * 308 * DRM clients which would like to work correctly on drivers which expose 309 * hotspot properties should advertise DRM_CLIENT_CAP_CURSOR_PLANE_HOTSPOT. 310 * Setting this property on drivers which do not special case 311 * cursor planes will return EOPNOTSUPP, which can be used by userspace to 312 * gauge requirements of the hardware/drivers they're running on. Advertising 313 * DRM_CLIENT_CAP_CURSOR_PLANE_HOTSPOT implies that the userspace client will be 314 * correctly setting the hotspot properties. 315 */ 316 317 /** 318 * drm_plane_create_hotspot_properties - creates the mouse hotspot 319 * properties and attaches them to the given cursor plane 320 * 321 * @plane: drm cursor plane 322 * 323 * This function enables the mouse hotspot property on a given 324 * cursor plane. Look at the documentation for hotspot properties 325 * to get a better understanding for what they're used for. 326 * 327 * RETURNS: 328 * Zero for success or -errno 329 */ 330 static int drm_plane_create_hotspot_properties(struct drm_plane *plane) 331 { 332 struct drm_property *prop_x; 333 struct drm_property *prop_y; 334 335 drm_WARN_ON(plane->dev, 336 !drm_core_check_feature(plane->dev, 337 DRIVER_CURSOR_HOTSPOT)); 338 339 prop_x = drm_property_create_signed_range(plane->dev, 0, "HOTSPOT_X", 340 INT_MIN, INT_MAX); 341 if (!prop_x) 342 return -ENOMEM; 343 344 prop_y = drm_property_create_signed_range(plane->dev, 0, "HOTSPOT_Y", 345 INT_MIN, INT_MAX); 346 if (!prop_y) { 347 drm_property_destroy(plane->dev, prop_x); 348 return -ENOMEM; 349 } 350 351 drm_object_attach_property(&plane->base, prop_x, 0); 352 drm_object_attach_property(&plane->base, prop_y, 0); 353 plane->hotspot_x_property = prop_x; 354 plane->hotspot_y_property = prop_y; 355 356 return 0; 357 } 358 359 __printf(9, 0) 360 static int __drm_universal_plane_init(struct drm_device *dev, 361 struct drm_plane *plane, 362 uint32_t possible_crtcs, 363 const struct drm_plane_funcs *funcs, 364 const uint32_t *formats, 365 unsigned int format_count, 366 const uint64_t *format_modifiers, 367 enum drm_plane_type type, 368 const char *name, va_list ap) 369 { 370 struct drm_mode_config *config = &dev->mode_config; 371 struct drm_property_blob *blob; 372 static const uint64_t default_modifiers[] = { 373 DRM_FORMAT_MOD_LINEAR, 374 }; 375 unsigned int format_modifier_count = 0; 376 int ret; 377 378 /* plane index is used with 32bit bitmasks */ 379 if (WARN_ON(config->num_total_plane >= 32)) 380 return -EINVAL; 381 382 /* 383 * First driver to need more than 64 formats needs to fix this. Each 384 * format is encoded as a bit and the current code only supports a u64. 385 */ 386 if (WARN_ON(format_count > 64)) 387 return -EINVAL; 388 389 WARN_ON(drm_drv_uses_atomic_modeset(dev) && 390 (!funcs->atomic_destroy_state || 391 !funcs->atomic_duplicate_state)); 392 393 ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE); 394 if (ret) 395 return ret; 396 397 drm_modeset_lock_init(&plane->mutex); 398 399 plane->base.properties = &plane->properties; 400 plane->dev = dev; 401 plane->funcs = funcs; 402 plane->format_types = kmalloc_objs(uint32_t, format_count); 403 if (!plane->format_types) { 404 DRM_DEBUG_KMS("out of memory when allocating plane\n"); 405 drm_mode_object_unregister(dev, &plane->base); 406 return -ENOMEM; 407 } 408 409 if (format_modifiers) { 410 const uint64_t *temp_modifiers = format_modifiers; 411 412 while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID) 413 format_modifier_count++; 414 } else { 415 if (!dev->mode_config.fb_modifiers_not_supported) { 416 format_modifiers = default_modifiers; 417 format_modifier_count = ARRAY_SIZE(default_modifiers); 418 } 419 } 420 421 /* autoset the cap and check for consistency across all planes */ 422 drm_WARN_ON(dev, config->fb_modifiers_not_supported && 423 format_modifier_count); 424 425 plane->modifier_count = format_modifier_count; 426 plane->modifiers = kmalloc_objs(*plane->modifiers, 427 format_modifier_count); 428 429 if (format_modifier_count && !plane->modifiers) { 430 DRM_DEBUG_KMS("out of memory when allocating plane\n"); 431 kfree(plane->format_types); 432 drm_mode_object_unregister(dev, &plane->base); 433 return -ENOMEM; 434 } 435 436 if (name) { 437 plane->name = kvasprintf(GFP_KERNEL, name, ap); 438 } else { 439 plane->name = kasprintf(GFP_KERNEL, "plane-%d", 440 drm_num_planes(dev)); 441 } 442 if (!plane->name) { 443 kfree(plane->format_types); 444 kfree(plane->modifiers); 445 drm_mode_object_unregister(dev, &plane->base); 446 return -ENOMEM; 447 } 448 449 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t)); 450 plane->format_count = format_count; 451 memcpy(plane->modifiers, format_modifiers, 452 format_modifier_count * sizeof(format_modifiers[0])); 453 plane->possible_crtcs = possible_crtcs; 454 plane->type = type; 455 456 list_add_tail(&plane->head, &config->plane_list); 457 plane->index = config->num_total_plane++; 458 459 drm_object_attach_property(&plane->base, 460 config->plane_type_property, 461 plane->type); 462 463 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) { 464 drm_object_attach_property(&plane->base, config->prop_fb_id, 0); 465 drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1); 466 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0); 467 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0); 468 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0); 469 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0); 470 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0); 471 drm_object_attach_property(&plane->base, config->prop_src_x, 0); 472 drm_object_attach_property(&plane->base, config->prop_src_y, 0); 473 drm_object_attach_property(&plane->base, config->prop_src_w, 0); 474 drm_object_attach_property(&plane->base, config->prop_src_h, 0); 475 } 476 if (drm_core_check_feature(dev, DRIVER_CURSOR_HOTSPOT) && 477 type == DRM_PLANE_TYPE_CURSOR) { 478 drm_plane_create_hotspot_properties(plane); 479 } 480 481 if (format_modifier_count) { 482 blob = create_in_format_blob(dev, plane, 483 plane->funcs->format_mod_supported); 484 if (!IS_ERR(blob)) 485 drm_object_attach_property(&plane->base, 486 config->modifiers_property, 487 blob->base.id); 488 } 489 490 if (plane->funcs->format_mod_supported_async) { 491 blob = create_in_format_blob(dev, plane, 492 plane->funcs->format_mod_supported_async); 493 if (!IS_ERR(blob)) 494 drm_object_attach_property(&plane->base, 495 config->async_modifiers_property, 496 blob->base.id); 497 } 498 499 500 return 0; 501 } 502 503 /** 504 * drm_universal_plane_init - Initialize a new universal plane object 505 * @dev: DRM device 506 * @plane: plane object to init 507 * @possible_crtcs: bitmask of possible CRTCs 508 * @funcs: callbacks for the new plane 509 * @formats: array of supported formats (DRM_FORMAT\_\*) 510 * @format_count: number of elements in @formats 511 * @format_modifiers: array of struct drm_format modifiers terminated by 512 * DRM_FORMAT_MOD_INVALID 513 * @type: type of plane (overlay, primary, cursor) 514 * @name: printf style format string for the plane name, or NULL for default name 515 * 516 * Initializes a plane object of type @type. The &drm_plane_funcs.destroy hook 517 * should call drm_plane_cleanup() and kfree() the plane structure. The plane 518 * structure should not be allocated with devm_kzalloc(). 519 * 520 * Note: consider using drmm_universal_plane_alloc() instead of 521 * drm_universal_plane_init() to let the DRM managed resource infrastructure 522 * take care of cleanup and deallocation. 523 * 524 * Drivers that only support the DRM_FORMAT_MOD_LINEAR modifier support may set 525 * @format_modifiers to NULL. The plane will advertise the linear modifier. 526 * 527 * Returns: 528 * Zero on success, error code on failure. 529 */ 530 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane, 531 uint32_t possible_crtcs, 532 const struct drm_plane_funcs *funcs, 533 const uint32_t *formats, unsigned int format_count, 534 const uint64_t *format_modifiers, 535 enum drm_plane_type type, 536 const char *name, ...) 537 { 538 va_list ap; 539 int ret; 540 541 WARN_ON(!funcs->destroy); 542 543 va_start(ap, name); 544 ret = __drm_universal_plane_init(dev, plane, possible_crtcs, funcs, 545 formats, format_count, format_modifiers, 546 type, name, ap); 547 va_end(ap); 548 return ret; 549 } 550 EXPORT_SYMBOL(drm_universal_plane_init); 551 552 static void drmm_universal_plane_alloc_release(struct drm_device *dev, void *ptr) 553 { 554 struct drm_plane *plane = ptr; 555 556 if (WARN_ON(!plane->dev)) 557 return; 558 559 drm_plane_cleanup(plane); 560 } 561 562 void *__drmm_universal_plane_alloc(struct drm_device *dev, size_t size, 563 size_t offset, uint32_t possible_crtcs, 564 const struct drm_plane_funcs *funcs, 565 const uint32_t *formats, unsigned int format_count, 566 const uint64_t *format_modifiers, 567 enum drm_plane_type type, 568 const char *name, ...) 569 { 570 void *container; 571 struct drm_plane *plane; 572 va_list ap; 573 int ret; 574 575 if (WARN_ON(!funcs || funcs->destroy)) 576 return ERR_PTR(-EINVAL); 577 578 container = drmm_kzalloc(dev, size, GFP_KERNEL); 579 if (!container) 580 return ERR_PTR(-ENOMEM); 581 582 plane = container + offset; 583 584 va_start(ap, name); 585 ret = __drm_universal_plane_init(dev, plane, possible_crtcs, funcs, 586 formats, format_count, format_modifiers, 587 type, name, ap); 588 va_end(ap); 589 if (ret) 590 return ERR_PTR(ret); 591 592 ret = drmm_add_action_or_reset(dev, drmm_universal_plane_alloc_release, 593 plane); 594 if (ret) 595 return ERR_PTR(ret); 596 597 return container; 598 } 599 EXPORT_SYMBOL(__drmm_universal_plane_alloc); 600 601 void *__drm_universal_plane_alloc(struct drm_device *dev, size_t size, 602 size_t offset, uint32_t possible_crtcs, 603 const struct drm_plane_funcs *funcs, 604 const uint32_t *formats, unsigned int format_count, 605 const uint64_t *format_modifiers, 606 enum drm_plane_type type, 607 const char *name, ...) 608 { 609 void *container; 610 struct drm_plane *plane; 611 va_list ap; 612 int ret; 613 614 if (drm_WARN_ON(dev, !funcs)) 615 return ERR_PTR(-EINVAL); 616 617 container = kzalloc(size, GFP_KERNEL); 618 if (!container) 619 return ERR_PTR(-ENOMEM); 620 621 plane = container + offset; 622 623 va_start(ap, name); 624 ret = __drm_universal_plane_init(dev, plane, possible_crtcs, funcs, 625 formats, format_count, format_modifiers, 626 type, name, ap); 627 va_end(ap); 628 if (ret) 629 goto err_kfree; 630 631 return container; 632 633 err_kfree: 634 kfree(container); 635 return ERR_PTR(ret); 636 } 637 EXPORT_SYMBOL(__drm_universal_plane_alloc); 638 639 int drm_plane_register_all(struct drm_device *dev) 640 { 641 unsigned int num_planes = 0; 642 unsigned int num_zpos = 0; 643 struct drm_plane *plane; 644 int ret = 0; 645 646 drm_for_each_plane(plane, dev) { 647 if (plane->funcs->late_register) 648 ret = plane->funcs->late_register(plane); 649 if (ret) 650 return ret; 651 652 if (plane->zpos_property) 653 num_zpos++; 654 num_planes++; 655 } 656 657 drm_WARN(dev, num_zpos && num_planes != num_zpos, 658 "Mixing planes with and without zpos property is invalid\n"); 659 660 return 0; 661 } 662 663 void drm_plane_unregister_all(struct drm_device *dev) 664 { 665 struct drm_plane *plane; 666 667 drm_for_each_plane(plane, dev) { 668 if (plane->funcs->early_unregister) 669 plane->funcs->early_unregister(plane); 670 } 671 } 672 673 /** 674 * drm_plane_cleanup - Clean up the core plane usage 675 * @plane: plane to cleanup 676 * 677 * This function cleans up @plane and removes it from the DRM mode setting 678 * core. Note that the function does *not* free the plane structure itself, 679 * this is the responsibility of the caller. 680 */ 681 void drm_plane_cleanup(struct drm_plane *plane) 682 { 683 struct drm_device *dev = plane->dev; 684 685 drm_modeset_lock_fini(&plane->mutex); 686 687 kfree(plane->format_types); 688 kfree(plane->modifiers); 689 drm_mode_object_unregister(dev, &plane->base); 690 691 BUG_ON(list_empty(&plane->head)); 692 693 /* Note that the plane_list is considered to be static; should we 694 * remove the drm_plane at runtime we would have to decrement all 695 * the indices on the drm_plane after us in the plane_list. 696 */ 697 698 list_del(&plane->head); 699 dev->mode_config.num_total_plane--; 700 701 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state); 702 if (plane->state && plane->funcs->atomic_destroy_state) 703 plane->funcs->atomic_destroy_state(plane, plane->state); 704 705 kfree(plane->name); 706 707 memset(plane, 0, sizeof(*plane)); 708 } 709 EXPORT_SYMBOL(drm_plane_cleanup); 710 711 /** 712 * drm_plane_from_index - find the registered plane at an index 713 * @dev: DRM device 714 * @idx: index of registered plane to find for 715 * 716 * Given a plane index, return the registered plane from DRM device's 717 * list of planes with matching index. This is the inverse of drm_plane_index(). 718 */ 719 struct drm_plane * 720 drm_plane_from_index(struct drm_device *dev, int idx) 721 { 722 struct drm_plane *plane; 723 724 drm_for_each_plane(plane, dev) 725 if (idx == plane->index) 726 return plane; 727 728 return NULL; 729 } 730 EXPORT_SYMBOL(drm_plane_from_index); 731 732 /** 733 * drm_plane_force_disable - Forcibly disable a plane 734 * @plane: plane to disable 735 * 736 * Forces the plane to be disabled. 737 * 738 * Used when the plane's current framebuffer is destroyed, 739 * and when restoring fbdev mode. 740 * 741 * Note that this function is not suitable for atomic drivers, since it doesn't 742 * wire through the lock acquisition context properly and hence can't handle 743 * retries or driver private locks. You probably want to use 744 * drm_atomic_helper_disable_plane() or 745 * drm_atomic_helper_disable_planes_on_crtc() instead. 746 */ 747 void drm_plane_force_disable(struct drm_plane *plane) 748 { 749 int ret; 750 751 if (!plane->fb) 752 return; 753 754 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev)); 755 756 plane->old_fb = plane->fb; 757 ret = plane->funcs->disable_plane(plane, NULL); 758 if (ret) { 759 DRM_ERROR("failed to disable plane with busy fb\n"); 760 plane->old_fb = NULL; 761 return; 762 } 763 /* disconnect the plane from the fb and crtc: */ 764 drm_framebuffer_put(plane->old_fb); 765 plane->old_fb = NULL; 766 plane->fb = NULL; 767 plane->crtc = NULL; 768 } 769 EXPORT_SYMBOL(drm_plane_force_disable); 770 771 /** 772 * drm_mode_plane_set_obj_prop - set the value of a property 773 * @plane: drm plane object to set property value for 774 * @property: property to set 775 * @value: value the property should be set to 776 * 777 * This functions sets a given property on a given plane object. This function 778 * calls the driver's ->set_property callback and changes the software state of 779 * the property if the callback succeeds. 780 * 781 * Returns: 782 * Zero on success, error code on failure. 783 */ 784 int drm_mode_plane_set_obj_prop(struct drm_plane *plane, 785 struct drm_property *property, 786 uint64_t value) 787 { 788 int ret = -EINVAL; 789 struct drm_mode_object *obj = &plane->base; 790 791 if (plane->funcs->set_property) 792 ret = plane->funcs->set_property(plane, property, value); 793 if (!ret) 794 drm_object_property_set_value(obj, property, value); 795 796 return ret; 797 } 798 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop); 799 800 int drm_mode_getplane_res(struct drm_device *dev, void *data, 801 struct drm_file *file_priv) 802 { 803 struct drm_mode_get_plane_res *plane_resp = data; 804 struct drm_plane *plane; 805 uint32_t __user *plane_ptr; 806 int count = 0; 807 808 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 809 return -EOPNOTSUPP; 810 811 plane_ptr = u64_to_user_ptr(plane_resp->plane_id_ptr); 812 813 /* 814 * This ioctl is called twice, once to determine how much space is 815 * needed, and the 2nd time to fill it. 816 */ 817 drm_for_each_plane(plane, dev) { 818 /* 819 * Unless userspace set the 'universal planes' 820 * capability bit, only advertise overlays. 821 */ 822 if (plane->type != DRM_PLANE_TYPE_OVERLAY && 823 !file_priv->universal_planes) 824 continue; 825 826 /* 827 * If we're running on a virtualized driver then, 828 * unless userspace advertizes support for the 829 * virtualized cursor plane, disable cursor planes 830 * because they'll be broken due to missing cursor 831 * hotspot info. 832 */ 833 if (plane->type == DRM_PLANE_TYPE_CURSOR && 834 drm_core_check_feature(dev, DRIVER_CURSOR_HOTSPOT) && 835 file_priv->atomic && 836 !file_priv->supports_virtualized_cursor_plane) 837 continue; 838 839 if (drm_lease_held(file_priv, plane->base.id)) { 840 if (count < plane_resp->count_planes && 841 put_user(plane->base.id, plane_ptr + count)) 842 return -EFAULT; 843 count++; 844 } 845 } 846 plane_resp->count_planes = count; 847 848 return 0; 849 } 850 851 int drm_mode_getplane(struct drm_device *dev, void *data, 852 struct drm_file *file_priv) 853 { 854 struct drm_mode_get_plane *plane_resp = data; 855 struct drm_plane *plane; 856 uint32_t __user *format_ptr; 857 858 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 859 return -EOPNOTSUPP; 860 861 plane = drm_plane_find(dev, file_priv, plane_resp->plane_id); 862 if (!plane) 863 return -ENOENT; 864 865 drm_modeset_lock(&plane->mutex, NULL); 866 if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id)) 867 plane_resp->crtc_id = plane->state->crtc->base.id; 868 else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id)) 869 plane_resp->crtc_id = plane->crtc->base.id; 870 else 871 plane_resp->crtc_id = 0; 872 873 if (plane->state && plane->state->fb) 874 plane_resp->fb_id = plane->state->fb->base.id; 875 else if (!plane->state && plane->fb) 876 plane_resp->fb_id = plane->fb->base.id; 877 else 878 plane_resp->fb_id = 0; 879 drm_modeset_unlock(&plane->mutex); 880 881 plane_resp->plane_id = plane->base.id; 882 plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv, 883 plane->possible_crtcs); 884 885 plane_resp->gamma_size = 0; 886 887 /* 888 * This ioctl is called twice, once to determine how much space is 889 * needed, and the 2nd time to fill it. 890 */ 891 if (plane->format_count && 892 (plane_resp->count_format_types >= plane->format_count)) { 893 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr; 894 if (copy_to_user(format_ptr, 895 plane->format_types, 896 sizeof(uint32_t) * plane->format_count)) { 897 return -EFAULT; 898 } 899 } 900 plane_resp->count_format_types = plane->format_count; 901 902 return 0; 903 } 904 905 /** 906 * drm_plane_has_format - Check whether the plane supports this format and modifier combination 907 * @plane: drm plane 908 * @format: pixel format (DRM_FORMAT_*) 909 * @modifier: data layout modifier 910 * 911 * Returns: 912 * Whether the plane supports the specified format and modifier combination. 913 */ 914 bool drm_plane_has_format(struct drm_plane *plane, 915 u32 format, u64 modifier) 916 { 917 unsigned int i; 918 919 for (i = 0; i < plane->format_count; i++) { 920 if (format == plane->format_types[i]) 921 break; 922 } 923 if (i == plane->format_count) 924 return false; 925 926 if (plane->funcs->format_mod_supported) { 927 if (!plane->funcs->format_mod_supported(plane, format, modifier)) 928 return false; 929 } else { 930 if (!plane->modifier_count) 931 return true; 932 933 for (i = 0; i < plane->modifier_count; i++) { 934 if (modifier == plane->modifiers[i]) 935 break; 936 } 937 if (i == plane->modifier_count) 938 return false; 939 } 940 941 return true; 942 } 943 EXPORT_SYMBOL(drm_plane_has_format); 944 945 static int __setplane_check(struct drm_plane *plane, 946 struct drm_crtc *crtc, 947 struct drm_framebuffer *fb, 948 int32_t crtc_x, int32_t crtc_y, 949 uint32_t crtc_w, uint32_t crtc_h, 950 uint32_t src_x, uint32_t src_y, 951 uint32_t src_w, uint32_t src_h) 952 { 953 int ret; 954 955 /* Check whether this plane is usable on this CRTC */ 956 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) { 957 DRM_DEBUG_KMS("Invalid crtc for plane\n"); 958 return -EINVAL; 959 } 960 961 /* Check whether this plane supports the fb pixel format. */ 962 if (!drm_plane_has_format(plane, fb->format->format, fb->modifier)) { 963 DRM_DEBUG_KMS("Invalid pixel format %p4cc, modifier 0x%llx\n", 964 &fb->format->format, fb->modifier); 965 return -EINVAL; 966 } 967 968 /* Give drivers some help against integer overflows */ 969 if (crtc_w > INT_MAX || 970 crtc_x > INT_MAX - (int32_t) crtc_w || 971 crtc_h > INT_MAX || 972 crtc_y > INT_MAX - (int32_t) crtc_h) { 973 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n", 974 crtc_w, crtc_h, crtc_x, crtc_y); 975 return -ERANGE; 976 } 977 978 ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb); 979 if (ret) 980 return ret; 981 982 return 0; 983 } 984 985 /** 986 * drm_any_plane_has_format - Check whether any plane supports this format and modifier combination 987 * @dev: DRM device 988 * @format: pixel format (DRM_FORMAT_*) 989 * @modifier: data layout modifier 990 * 991 * Returns: 992 * Whether at least one plane supports the specified format and modifier combination. 993 */ 994 bool drm_any_plane_has_format(struct drm_device *dev, 995 u32 format, u64 modifier) 996 { 997 struct drm_plane *plane; 998 999 drm_for_each_plane(plane, dev) { 1000 if (drm_plane_has_format(plane, format, modifier)) 1001 return true; 1002 } 1003 1004 return false; 1005 } 1006 EXPORT_SYMBOL(drm_any_plane_has_format); 1007 1008 /* 1009 * __setplane_internal - setplane handler for internal callers 1010 * 1011 * This function will take a reference on the new fb for the plane 1012 * on success. 1013 * 1014 * src_{x,y,w,h} are provided in 16.16 fixed point format 1015 */ 1016 static int __setplane_internal(struct drm_plane *plane, 1017 struct drm_crtc *crtc, 1018 struct drm_framebuffer *fb, 1019 int32_t crtc_x, int32_t crtc_y, 1020 uint32_t crtc_w, uint32_t crtc_h, 1021 /* src_{x,y,w,h} values are 16.16 fixed point */ 1022 uint32_t src_x, uint32_t src_y, 1023 uint32_t src_w, uint32_t src_h, 1024 struct drm_modeset_acquire_ctx *ctx) 1025 { 1026 int ret = 0; 1027 1028 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev)); 1029 1030 /* No fb means shut it down */ 1031 if (!fb) { 1032 plane->old_fb = plane->fb; 1033 ret = plane->funcs->disable_plane(plane, ctx); 1034 if (!ret) { 1035 plane->crtc = NULL; 1036 plane->fb = NULL; 1037 } else { 1038 plane->old_fb = NULL; 1039 } 1040 goto out; 1041 } 1042 1043 ret = __setplane_check(plane, crtc, fb, 1044 crtc_x, crtc_y, crtc_w, crtc_h, 1045 src_x, src_y, src_w, src_h); 1046 if (ret) 1047 goto out; 1048 1049 plane->old_fb = plane->fb; 1050 ret = plane->funcs->update_plane(plane, crtc, fb, 1051 crtc_x, crtc_y, crtc_w, crtc_h, 1052 src_x, src_y, src_w, src_h, ctx); 1053 if (!ret) { 1054 plane->crtc = crtc; 1055 plane->fb = fb; 1056 drm_framebuffer_get(plane->fb); 1057 } else { 1058 plane->old_fb = NULL; 1059 } 1060 1061 out: 1062 if (plane->old_fb) 1063 drm_framebuffer_put(plane->old_fb); 1064 plane->old_fb = NULL; 1065 1066 return ret; 1067 } 1068 1069 static int __setplane_atomic(struct drm_plane *plane, 1070 struct drm_crtc *crtc, 1071 struct drm_framebuffer *fb, 1072 int32_t crtc_x, int32_t crtc_y, 1073 uint32_t crtc_w, uint32_t crtc_h, 1074 uint32_t src_x, uint32_t src_y, 1075 uint32_t src_w, uint32_t src_h, 1076 struct drm_modeset_acquire_ctx *ctx) 1077 { 1078 int ret; 1079 1080 WARN_ON(!drm_drv_uses_atomic_modeset(plane->dev)); 1081 1082 /* No fb means shut it down */ 1083 if (!fb) 1084 return plane->funcs->disable_plane(plane, ctx); 1085 1086 /* 1087 * FIXME: This is redundant with drm_atomic_plane_check(), 1088 * but the legacy cursor/"async" .update_plane() tricks 1089 * don't call that so we still need this here. Should remove 1090 * this when all .update_plane() implementations have been 1091 * fixed to call drm_atomic_plane_check(). 1092 */ 1093 ret = __setplane_check(plane, crtc, fb, 1094 crtc_x, crtc_y, crtc_w, crtc_h, 1095 src_x, src_y, src_w, src_h); 1096 if (ret) 1097 return ret; 1098 1099 return plane->funcs->update_plane(plane, crtc, fb, 1100 crtc_x, crtc_y, crtc_w, crtc_h, 1101 src_x, src_y, src_w, src_h, ctx); 1102 } 1103 1104 static int setplane_internal(struct drm_plane *plane, 1105 struct drm_crtc *crtc, 1106 struct drm_framebuffer *fb, 1107 int32_t crtc_x, int32_t crtc_y, 1108 uint32_t crtc_w, uint32_t crtc_h, 1109 /* src_{x,y,w,h} values are 16.16 fixed point */ 1110 uint32_t src_x, uint32_t src_y, 1111 uint32_t src_w, uint32_t src_h) 1112 { 1113 struct drm_modeset_acquire_ctx ctx; 1114 int ret; 1115 1116 DRM_MODESET_LOCK_ALL_BEGIN(plane->dev, ctx, 1117 DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret); 1118 1119 if (drm_drv_uses_atomic_modeset(plane->dev)) 1120 ret = __setplane_atomic(plane, crtc, fb, 1121 crtc_x, crtc_y, crtc_w, crtc_h, 1122 src_x, src_y, src_w, src_h, &ctx); 1123 else 1124 ret = __setplane_internal(plane, crtc, fb, 1125 crtc_x, crtc_y, crtc_w, crtc_h, 1126 src_x, src_y, src_w, src_h, &ctx); 1127 1128 DRM_MODESET_LOCK_ALL_END(plane->dev, ctx, ret); 1129 1130 return ret; 1131 } 1132 1133 int drm_mode_setplane(struct drm_device *dev, void *data, 1134 struct drm_file *file_priv) 1135 { 1136 struct drm_mode_set_plane *plane_req = data; 1137 struct drm_plane *plane; 1138 struct drm_crtc *crtc = NULL; 1139 struct drm_framebuffer *fb = NULL; 1140 int ret; 1141 1142 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1143 return -EOPNOTSUPP; 1144 1145 /* 1146 * First, find the plane, crtc, and fb objects. If not available, 1147 * we don't bother to call the driver. 1148 */ 1149 plane = drm_plane_find(dev, file_priv, plane_req->plane_id); 1150 if (!plane) { 1151 DRM_DEBUG_KMS("Unknown plane ID %d\n", 1152 plane_req->plane_id); 1153 return -ENOENT; 1154 } 1155 1156 if (plane_req->fb_id) { 1157 fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id); 1158 if (!fb) { 1159 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n", 1160 plane_req->fb_id); 1161 return -ENOENT; 1162 } 1163 1164 crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id); 1165 if (!crtc) { 1166 drm_framebuffer_put(fb); 1167 DRM_DEBUG_KMS("Unknown crtc ID %d\n", 1168 plane_req->crtc_id); 1169 return -ENOENT; 1170 } 1171 } 1172 1173 ret = setplane_internal(plane, crtc, fb, 1174 plane_req->crtc_x, plane_req->crtc_y, 1175 plane_req->crtc_w, plane_req->crtc_h, 1176 plane_req->src_x, plane_req->src_y, 1177 plane_req->src_w, plane_req->src_h); 1178 1179 if (fb) 1180 drm_framebuffer_put(fb); 1181 1182 return ret; 1183 } 1184 1185 static int drm_mode_cursor_universal(struct drm_crtc *crtc, 1186 struct drm_mode_cursor2 *req, 1187 struct drm_file *file_priv, 1188 struct drm_modeset_acquire_ctx *ctx) 1189 { 1190 struct drm_device *dev = crtc->dev; 1191 struct drm_plane *plane = crtc->cursor; 1192 struct drm_framebuffer *fb = NULL; 1193 struct drm_mode_fb_cmd2 fbreq = { 1194 .width = req->width, 1195 .height = req->height, 1196 .pixel_format = DRM_FORMAT_ARGB8888, 1197 .pitches = { req->width * 4 }, 1198 .handles = { req->handle }, 1199 }; 1200 int32_t crtc_x, crtc_y; 1201 uint32_t crtc_w = 0, crtc_h = 0; 1202 uint32_t src_w = 0, src_h = 0; 1203 int ret = 0; 1204 1205 BUG_ON(!plane); 1206 WARN_ON(plane->crtc != crtc && plane->crtc != NULL); 1207 1208 /* 1209 * Obtain fb we'll be using (either new or existing) and take an extra 1210 * reference to it if fb != null. setplane will take care of dropping 1211 * the reference if the plane update fails. 1212 */ 1213 if (req->flags & DRM_MODE_CURSOR_BO) { 1214 if (req->handle) { 1215 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv); 1216 if (IS_ERR(fb)) { 1217 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n"); 1218 return PTR_ERR(fb); 1219 } 1220 1221 if (plane->hotspot_x_property && plane->state) 1222 plane->state->hotspot_x = req->hot_x; 1223 if (plane->hotspot_y_property && plane->state) 1224 plane->state->hotspot_y = req->hot_y; 1225 } else { 1226 fb = NULL; 1227 } 1228 } else { 1229 if (plane->state) 1230 fb = plane->state->fb; 1231 else 1232 fb = plane->fb; 1233 1234 if (fb) 1235 drm_framebuffer_get(fb); 1236 } 1237 1238 if (req->flags & DRM_MODE_CURSOR_MOVE) { 1239 crtc_x = req->x; 1240 crtc_y = req->y; 1241 } else { 1242 crtc_x = crtc->cursor_x; 1243 crtc_y = crtc->cursor_y; 1244 } 1245 1246 if (fb) { 1247 crtc_w = fb->width; 1248 crtc_h = fb->height; 1249 src_w = fb->width << 16; 1250 src_h = fb->height << 16; 1251 } 1252 1253 if (drm_drv_uses_atomic_modeset(dev)) 1254 ret = __setplane_atomic(plane, crtc, fb, 1255 crtc_x, crtc_y, crtc_w, crtc_h, 1256 0, 0, src_w, src_h, ctx); 1257 else 1258 ret = __setplane_internal(plane, crtc, fb, 1259 crtc_x, crtc_y, crtc_w, crtc_h, 1260 0, 0, src_w, src_h, ctx); 1261 1262 if (fb) 1263 drm_framebuffer_put(fb); 1264 1265 /* Update successful; save new cursor position, if necessary */ 1266 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) { 1267 crtc->cursor_x = req->x; 1268 crtc->cursor_y = req->y; 1269 } 1270 1271 return ret; 1272 } 1273 1274 static int drm_mode_cursor_common(struct drm_device *dev, 1275 struct drm_mode_cursor2 *req, 1276 struct drm_file *file_priv) 1277 { 1278 struct drm_crtc *crtc; 1279 struct drm_modeset_acquire_ctx ctx; 1280 int ret = 0; 1281 1282 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1283 return -EOPNOTSUPP; 1284 1285 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags)) 1286 return -EINVAL; 1287 1288 crtc = drm_crtc_find(dev, file_priv, req->crtc_id); 1289 if (!crtc) { 1290 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id); 1291 return -ENOENT; 1292 } 1293 1294 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE); 1295 retry: 1296 ret = drm_modeset_lock(&crtc->mutex, &ctx); 1297 if (ret) 1298 goto out; 1299 /* 1300 * If this crtc has a universal cursor plane, call that plane's update 1301 * handler rather than using legacy cursor handlers. 1302 */ 1303 if (crtc->cursor) { 1304 ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx); 1305 if (ret) 1306 goto out; 1307 1308 if (!drm_lease_held(file_priv, crtc->cursor->base.id)) { 1309 ret = -EACCES; 1310 goto out; 1311 } 1312 1313 ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx); 1314 goto out; 1315 } 1316 1317 if (req->flags & DRM_MODE_CURSOR_BO) { 1318 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) { 1319 ret = -ENXIO; 1320 goto out; 1321 } 1322 /* Turns off the cursor if handle is 0 */ 1323 if (crtc->funcs->cursor_set2) 1324 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle, 1325 req->width, req->height, req->hot_x, req->hot_y); 1326 else 1327 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle, 1328 req->width, req->height); 1329 } 1330 1331 if (req->flags & DRM_MODE_CURSOR_MOVE) { 1332 if (crtc->funcs->cursor_move) { 1333 ret = crtc->funcs->cursor_move(crtc, req->x, req->y); 1334 } else { 1335 ret = -EFAULT; 1336 goto out; 1337 } 1338 } 1339 out: 1340 if (ret == -EDEADLK) { 1341 ret = drm_modeset_backoff(&ctx); 1342 if (!ret) 1343 goto retry; 1344 } 1345 1346 drm_modeset_drop_locks(&ctx); 1347 drm_modeset_acquire_fini(&ctx); 1348 1349 return ret; 1350 1351 } 1352 1353 1354 int drm_mode_cursor_ioctl(struct drm_device *dev, 1355 void *data, struct drm_file *file_priv) 1356 { 1357 struct drm_mode_cursor *req = data; 1358 struct drm_mode_cursor2 new_req; 1359 1360 memcpy(&new_req, req, sizeof(struct drm_mode_cursor)); 1361 new_req.hot_x = new_req.hot_y = 0; 1362 1363 return drm_mode_cursor_common(dev, &new_req, file_priv); 1364 } 1365 1366 /* 1367 * Set the cursor configuration based on user request. This implements the 2nd 1368 * version of the cursor ioctl, which allows userspace to additionally specify 1369 * the hotspot of the pointer. 1370 */ 1371 int drm_mode_cursor2_ioctl(struct drm_device *dev, 1372 void *data, struct drm_file *file_priv) 1373 { 1374 struct drm_mode_cursor2 *req = data; 1375 1376 return drm_mode_cursor_common(dev, req, file_priv); 1377 } 1378 1379 int drm_mode_page_flip_ioctl(struct drm_device *dev, 1380 void *data, struct drm_file *file_priv) 1381 { 1382 struct drm_mode_crtc_page_flip_target *page_flip = data; 1383 struct drm_crtc *crtc; 1384 struct drm_plane *plane; 1385 struct drm_framebuffer *fb = NULL, *old_fb; 1386 struct drm_pending_vblank_event *e = NULL; 1387 u32 target_vblank = page_flip->sequence; 1388 struct drm_modeset_acquire_ctx ctx; 1389 int ret = -EINVAL; 1390 1391 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1392 return -EOPNOTSUPP; 1393 1394 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS) 1395 return -EINVAL; 1396 1397 if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) 1398 return -EINVAL; 1399 1400 /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags 1401 * can be specified 1402 */ 1403 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET) 1404 return -EINVAL; 1405 1406 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip) 1407 return -EINVAL; 1408 1409 crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id); 1410 if (!crtc) 1411 return -ENOENT; 1412 1413 plane = crtc->primary; 1414 1415 if (!drm_lease_held(file_priv, plane->base.id)) 1416 return -EACCES; 1417 1418 if (crtc->funcs->page_flip_target) { 1419 u32 current_vblank; 1420 int r; 1421 1422 r = drm_crtc_vblank_get(crtc); 1423 if (r) 1424 return r; 1425 1426 current_vblank = (u32)drm_crtc_vblank_count(crtc); 1427 1428 switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) { 1429 case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE: 1430 if ((int)(target_vblank - current_vblank) > 1) { 1431 DRM_DEBUG("Invalid absolute flip target %u, " 1432 "must be <= %u\n", target_vblank, 1433 current_vblank + 1); 1434 drm_crtc_vblank_put(crtc); 1435 return -EINVAL; 1436 } 1437 break; 1438 case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE: 1439 if (target_vblank != 0 && target_vblank != 1) { 1440 DRM_DEBUG("Invalid relative flip target %u, " 1441 "must be 0 or 1\n", target_vblank); 1442 drm_crtc_vblank_put(crtc); 1443 return -EINVAL; 1444 } 1445 target_vblank += current_vblank; 1446 break; 1447 default: 1448 target_vblank = current_vblank + 1449 !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC); 1450 break; 1451 } 1452 } else if (crtc->funcs->page_flip == NULL || 1453 (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) { 1454 return -EINVAL; 1455 } 1456 1457 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE); 1458 retry: 1459 ret = drm_modeset_lock(&crtc->mutex, &ctx); 1460 if (ret) 1461 goto out; 1462 ret = drm_modeset_lock(&plane->mutex, &ctx); 1463 if (ret) 1464 goto out; 1465 1466 if (plane->state) 1467 old_fb = plane->state->fb; 1468 else 1469 old_fb = plane->fb; 1470 1471 if (old_fb == NULL) { 1472 /* The framebuffer is currently unbound, presumably 1473 * due to a hotplug event, that userspace has not 1474 * yet discovered. 1475 */ 1476 ret = -EBUSY; 1477 goto out; 1478 } 1479 1480 fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id); 1481 if (!fb) { 1482 ret = -ENOENT; 1483 goto out; 1484 } 1485 1486 if (plane->state) { 1487 const struct drm_plane_state *state = plane->state; 1488 1489 ret = drm_framebuffer_check_src_coords(state->src_x, 1490 state->src_y, 1491 state->src_w, 1492 state->src_h, 1493 fb); 1494 } else { 1495 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, 1496 &crtc->mode, fb); 1497 } 1498 if (ret) 1499 goto out; 1500 1501 /* 1502 * Only check the FOURCC format code, excluding modifiers. This is 1503 * enough for all legacy drivers. Atomic drivers have their own 1504 * checks in their ->atomic_check implementation, which will 1505 * return -EINVAL if any hw or driver constraint is violated due 1506 * to modifier changes. 1507 */ 1508 if (old_fb->format->format != fb->format->format) { 1509 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n"); 1510 ret = -EINVAL; 1511 goto out; 1512 } 1513 1514 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) { 1515 e = kzalloc_obj(*e); 1516 if (!e) { 1517 ret = -ENOMEM; 1518 goto out; 1519 } 1520 1521 e->event.base.type = DRM_EVENT_FLIP_COMPLETE; 1522 e->event.base.length = sizeof(e->event); 1523 e->event.vbl.user_data = page_flip->user_data; 1524 e->event.vbl.crtc_id = crtc->base.id; 1525 1526 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base); 1527 if (ret) { 1528 kfree(e); 1529 e = NULL; 1530 goto out; 1531 } 1532 } 1533 1534 plane->old_fb = plane->fb; 1535 if (crtc->funcs->page_flip_target) 1536 ret = crtc->funcs->page_flip_target(crtc, fb, e, 1537 page_flip->flags, 1538 target_vblank, 1539 &ctx); 1540 else 1541 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags, 1542 &ctx); 1543 if (ret) { 1544 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) 1545 drm_event_cancel_free(dev, &e->base); 1546 /* Keep the old fb, don't unref it. */ 1547 plane->old_fb = NULL; 1548 } else { 1549 if (!plane->state) { 1550 plane->fb = fb; 1551 drm_framebuffer_get(fb); 1552 } 1553 } 1554 1555 out: 1556 if (fb) 1557 drm_framebuffer_put(fb); 1558 fb = NULL; 1559 if (plane->old_fb) 1560 drm_framebuffer_put(plane->old_fb); 1561 plane->old_fb = NULL; 1562 1563 if (ret == -EDEADLK) { 1564 ret = drm_modeset_backoff(&ctx); 1565 if (!ret) 1566 goto retry; 1567 } 1568 1569 drm_modeset_drop_locks(&ctx); 1570 drm_modeset_acquire_fini(&ctx); 1571 1572 if (ret && crtc->funcs->page_flip_target) 1573 drm_crtc_vblank_put(crtc); 1574 1575 return ret; 1576 } 1577 1578 /** 1579 * DOC: damage tracking 1580 * 1581 * FB_DAMAGE_CLIPS is an optional plane property which provides a means to 1582 * specify a list of damage rectangles on a plane in framebuffer coordinates of 1583 * the framebuffer attached to the plane. In current context damage is the area 1584 * of plane framebuffer that has changed since last plane update (also called 1585 * page-flip), irrespective of whether currently attached framebuffer is same as 1586 * framebuffer attached during last plane update or not. 1587 * 1588 * FB_DAMAGE_CLIPS is a hint to kernel which could be helpful for some drivers 1589 * to optimize internally especially for virtual devices where each framebuffer 1590 * change needs to be transmitted over network, usb, etc. 1591 * 1592 * Since FB_DAMAGE_CLIPS is a hint so it is an optional property. User-space can 1593 * ignore damage clips property and in that case driver will do a full plane 1594 * update. In case damage clips are provided then it is guaranteed that the area 1595 * inside damage clips will be updated to plane. For efficiency driver can do 1596 * full update or can update more than specified in damage clips. Since driver 1597 * is free to read more, user-space must always render the entire visible 1598 * framebuffer. Otherwise there can be corruptions. Also, if a user-space 1599 * provides damage clips which doesn't encompass the actual damage to 1600 * framebuffer (since last plane update) can result in incorrect rendering. 1601 * 1602 * FB_DAMAGE_CLIPS is a blob property with the layout of blob data is simply an 1603 * array of &drm_mode_rect. Unlike plane &drm_plane_state.src coordinates, 1604 * damage clips are not in 16.16 fixed point. Similar to plane src in 1605 * framebuffer, damage clips cannot be negative. In damage clip, x1/y1 are 1606 * inclusive and x2/y2 are exclusive. While kernel does not error for overlapped 1607 * damage clips, it is strongly discouraged. 1608 * 1609 * Drivers that are interested in damage interface for plane should enable 1610 * FB_DAMAGE_CLIPS property by calling drm_plane_enable_fb_damage_clips(). 1611 * Drivers implementing damage can use drm_atomic_helper_damage_iter_init() and 1612 * drm_atomic_helper_damage_iter_next() helper iterator function to get damage 1613 * rectangles clipped to &drm_plane_state.src. 1614 * 1615 * Note that there are two types of damage handling: frame damage and buffer 1616 * damage, the type of damage handling implemented depends on a driver's upload 1617 * target. Drivers implementing a per-plane or per-CRTC upload target need to 1618 * handle frame damage, while drivers implementing a per-buffer upload target 1619 * need to handle buffer damage. 1620 * 1621 * The existing damage helpers only support the frame damage type, there is no 1622 * buffer age support or similar damage accumulation algorithm implemented yet. 1623 * 1624 * Only drivers handling frame damage can use the mentioned damage helpers to 1625 * iterate over the damaged regions. Drivers that handle buffer damage, must set 1626 * &drm_plane_state.ignore_damage_clips for drm_atomic_helper_damage_iter_init() 1627 * to know that damage clips should be ignored and return &drm_plane_state.src 1628 * as the damage rectangle, to force a full plane update. 1629 * 1630 * Drivers with a per-buffer upload target could compare the &drm_plane_state.fb 1631 * of the old and new plane states to determine if the framebuffer attached to a 1632 * plane has changed or not since the last plane update. If &drm_plane_state.fb 1633 * has changed, then &drm_plane_state.ignore_damage_clips must be set to true. 1634 * 1635 * That is because drivers with a per-plane upload target, expect the backing 1636 * storage buffer to not change for a given plane. If the upload buffer changes 1637 * between page flips, the new upload buffer has to be updated as a whole. This 1638 * can be improved in the future if support for frame damage is added to the DRM 1639 * damage helpers, similarly to how user-space already handle this case as it is 1640 * explained in the following documents: 1641 * 1642 * https://registry.khronos.org/EGL/extensions/KHR/EGL_KHR_swap_buffers_with_damage.txt 1643 * https://emersion.fr/blog/2019/intro-to-damage-tracking/ 1644 */ 1645 1646 /** 1647 * drm_plane_enable_fb_damage_clips - Enables plane fb damage clips property. 1648 * @plane: Plane on which to enable damage clips property. 1649 * 1650 * This function lets driver to enable the damage clips property on a plane. 1651 */ 1652 void drm_plane_enable_fb_damage_clips(struct drm_plane *plane) 1653 { 1654 struct drm_device *dev = plane->dev; 1655 struct drm_mode_config *config = &dev->mode_config; 1656 1657 drm_object_attach_property(&plane->base, config->prop_fb_damage_clips, 1658 0); 1659 } 1660 EXPORT_SYMBOL(drm_plane_enable_fb_damage_clips); 1661 1662 /** 1663 * drm_plane_get_damage_clips_count - Returns damage clips count. 1664 * @state: Plane state. 1665 * 1666 * Simple helper to get the number of &drm_mode_rect clips set by user-space 1667 * during plane update. 1668 * 1669 * Return: Number of clips in plane fb_damage_clips blob property. 1670 */ 1671 unsigned int 1672 drm_plane_get_damage_clips_count(const struct drm_plane_state *state) 1673 { 1674 return (state && state->fb_damage_clips) ? 1675 state->fb_damage_clips->length/sizeof(struct drm_mode_rect) : 0; 1676 } 1677 EXPORT_SYMBOL(drm_plane_get_damage_clips_count); 1678 1679 struct drm_mode_rect * 1680 __drm_plane_get_damage_clips(const struct drm_plane_state *state) 1681 { 1682 return (struct drm_mode_rect *)((state && state->fb_damage_clips) ? 1683 state->fb_damage_clips->data : NULL); 1684 } 1685 1686 /** 1687 * drm_plane_get_damage_clips - Returns damage clips. 1688 * @state: Plane state. 1689 * 1690 * Note that this function returns uapi type &drm_mode_rect. Drivers might want 1691 * to use the helper functions drm_atomic_helper_damage_iter_init() and 1692 * drm_atomic_helper_damage_iter_next() or drm_atomic_helper_damage_merged() if 1693 * the driver can only handle a single damage region at most. 1694 * 1695 * Return: Damage clips in plane fb_damage_clips blob property. 1696 */ 1697 struct drm_mode_rect * 1698 drm_plane_get_damage_clips(const struct drm_plane_state *state) 1699 { 1700 struct drm_device *dev = state->plane->dev; 1701 struct drm_mode_config *config = &dev->mode_config; 1702 1703 /* check that drm_plane_enable_fb_damage_clips() was called */ 1704 if (!drm_mode_obj_find_prop_id(&state->plane->base, 1705 config->prop_fb_damage_clips->base.id)) 1706 drm_warn_once(dev, "drm_plane_enable_fb_damage_clips() not called\n"); 1707 1708 return __drm_plane_get_damage_clips(state); 1709 } 1710 EXPORT_SYMBOL(drm_plane_get_damage_clips); 1711 1712 struct drm_property * 1713 drm_create_scaling_filter_prop(struct drm_device *dev, 1714 unsigned int supported_filters) 1715 { 1716 struct drm_property *prop; 1717 static const struct drm_prop_enum_list props[] = { 1718 { DRM_SCALING_FILTER_DEFAULT, "Default" }, 1719 { DRM_SCALING_FILTER_NEAREST_NEIGHBOR, "Nearest Neighbor" }, 1720 }; 1721 unsigned int valid_mode_mask = BIT(DRM_SCALING_FILTER_DEFAULT) | 1722 BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR); 1723 int i; 1724 1725 if (WARN_ON((supported_filters & ~valid_mode_mask) || 1726 ((supported_filters & BIT(DRM_SCALING_FILTER_DEFAULT)) == 0))) 1727 return ERR_PTR(-EINVAL); 1728 1729 prop = drm_property_create(dev, DRM_MODE_PROP_ENUM, 1730 "SCALING_FILTER", 1731 hweight32(supported_filters)); 1732 if (!prop) 1733 return ERR_PTR(-ENOMEM); 1734 1735 for (i = 0; i < ARRAY_SIZE(props); i++) { 1736 int ret; 1737 1738 if (!(BIT(props[i].type) & supported_filters)) 1739 continue; 1740 1741 ret = drm_property_add_enum(prop, props[i].type, 1742 props[i].name); 1743 1744 if (ret) { 1745 drm_property_destroy(dev, prop); 1746 1747 return ERR_PTR(ret); 1748 } 1749 } 1750 1751 return prop; 1752 } 1753 1754 /** 1755 * drm_plane_create_scaling_filter_property - create a new scaling filter 1756 * property 1757 * 1758 * @plane: drm plane 1759 * @supported_filters: bitmask of supported scaling filters, must include 1760 * BIT(DRM_SCALING_FILTER_DEFAULT). 1761 * 1762 * This function lets driver to enable the scaling filter property on a given 1763 * plane. 1764 * 1765 * RETURNS: 1766 * Zero for success or -errno 1767 */ 1768 int drm_plane_create_scaling_filter_property(struct drm_plane *plane, 1769 unsigned int supported_filters) 1770 { 1771 struct drm_property *prop = 1772 drm_create_scaling_filter_prop(plane->dev, supported_filters); 1773 1774 if (IS_ERR(prop)) 1775 return PTR_ERR(prop); 1776 1777 drm_object_attach_property(&plane->base, prop, 1778 DRM_SCALING_FILTER_DEFAULT); 1779 plane->scaling_filter_property = prop; 1780 1781 return 0; 1782 } 1783 EXPORT_SYMBOL(drm_plane_create_scaling_filter_property); 1784 1785 /** 1786 * drm_plane_add_size_hints_property - create a size hints property 1787 * 1788 * @plane: drm plane 1789 * @hints: size hints 1790 * @num_hints: number of size hints 1791 * 1792 * Create a size hints property for the plane. 1793 * 1794 * RETURNS: 1795 * Zero for success or -errno 1796 */ 1797 int drm_plane_add_size_hints_property(struct drm_plane *plane, 1798 const struct drm_plane_size_hint *hints, 1799 int num_hints) 1800 { 1801 struct drm_device *dev = plane->dev; 1802 struct drm_mode_config *config = &dev->mode_config; 1803 struct drm_property_blob *blob; 1804 1805 /* extending to other plane types needs actual thought */ 1806 if (drm_WARN_ON(dev, plane->type != DRM_PLANE_TYPE_CURSOR)) 1807 return -EINVAL; 1808 1809 blob = drm_property_create_blob(dev, 1810 array_size(sizeof(hints[0]), num_hints), 1811 hints); 1812 if (IS_ERR(blob)) 1813 return PTR_ERR(blob); 1814 1815 drm_object_attach_property(&plane->base, config->size_hints_property, 1816 blob->base.id); 1817 1818 return 0; 1819 } 1820 EXPORT_SYMBOL(drm_plane_add_size_hints_property); 1821 1822 /** 1823 * drm_plane_create_color_pipeline_property - create a new color pipeline 1824 * property 1825 * 1826 * @plane: drm plane 1827 * @pipelines: list of pipelines 1828 * @num_pipelines: number of pipelines 1829 * 1830 * Create the COLOR_PIPELINE plane property to specific color pipelines on 1831 * the plane. 1832 * 1833 * RETURNS: 1834 * Zero for success or -errno 1835 */ 1836 int drm_plane_create_color_pipeline_property(struct drm_plane *plane, 1837 const struct drm_prop_enum_list *pipelines, 1838 int num_pipelines) 1839 { 1840 struct drm_prop_enum_list *all_pipelines; 1841 struct drm_property *prop; 1842 int len = 0; 1843 int i; 1844 1845 all_pipelines = kzalloc_objs(*all_pipelines, num_pipelines + 1); 1846 1847 if (!all_pipelines) { 1848 drm_err(plane->dev, "failed to allocate color pipeline\n"); 1849 return -ENOMEM; 1850 } 1851 1852 /* Create default Bypass color pipeline */ 1853 all_pipelines[len].type = 0; 1854 all_pipelines[len].name = "Bypass"; 1855 len++; 1856 1857 /* Add all other color pipelines */ 1858 for (i = 0; i < num_pipelines; i++, len++) { 1859 all_pipelines[len].type = pipelines[i].type; 1860 all_pipelines[len].name = pipelines[i].name; 1861 } 1862 1863 prop = drm_property_create_enum(plane->dev, DRM_MODE_PROP_ATOMIC, 1864 "COLOR_PIPELINE", 1865 all_pipelines, len); 1866 if (!prop) { 1867 kfree(all_pipelines); 1868 return -ENOMEM; 1869 } 1870 1871 drm_object_attach_property(&plane->base, prop, 0); 1872 plane->color_pipeline_property = prop; 1873 1874 kfree(all_pipelines); 1875 return 0; 1876 } 1877 EXPORT_SYMBOL(drm_plane_create_color_pipeline_property); 1878