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