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