1 /* $NetBSD: vmwgfx_stdu.c,v 1.2 2018/08/27 04:58:37 riastradh Exp $ */ 2 3 /****************************************************************************** 4 * 5 * COPYRIGHT © 2014-2015 VMware, Inc., Palo Alto, CA., USA 6 * All Rights Reserved. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the 10 * "Software"), to deal in the Software without restriction, including 11 * without limitation the rights to use, copy, modify, merge, publish, 12 * distribute, sub license, and/or sell copies of the Software, and to 13 * permit persons to whom the Software is furnished to do so, subject to 14 * the following conditions: 15 * 16 * The above copyright notice and this permission notice (including the 17 * next paragraph) shall be included in all copies or substantial portions 18 * of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 26 * USE OR OTHER DEALINGS IN THE SOFTWARE. 27 * 28 ******************************************************************************/ 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_stdu.c,v 1.2 2018/08/27 04:58:37 riastradh Exp $"); 32 33 #include "vmwgfx_kms.h" 34 #include "device_include/svga3d_surfacedefs.h" 35 #include <drm/drm_plane_helper.h> 36 37 #define vmw_crtc_to_stdu(x) \ 38 container_of(x, struct vmw_screen_target_display_unit, base.crtc) 39 #define vmw_encoder_to_stdu(x) \ 40 container_of(x, struct vmw_screen_target_display_unit, base.encoder) 41 #define vmw_connector_to_stdu(x) \ 42 container_of(x, struct vmw_screen_target_display_unit, base.connector) 43 44 45 46 enum stdu_content_type { 47 SAME_AS_DISPLAY = 0, 48 SEPARATE_SURFACE, 49 SEPARATE_DMA 50 }; 51 52 /** 53 * struct vmw_stdu_dirty - closure structure for the update functions 54 * 55 * @base: The base type we derive from. Used by vmw_kms_helper_dirty(). 56 * @transfer: Transfer direction for DMA command. 57 * @left: Left side of bounding box. 58 * @right: Right side of bounding box. 59 * @top: Top side of bounding box. 60 * @bottom: Bottom side of bounding box. 61 * @buf: DMA buffer when DMA-ing between buffer and screen targets. 62 * @sid: Surface ID when copying between surface and screen targets. 63 */ 64 struct vmw_stdu_dirty { 65 struct vmw_kms_dirty base; 66 SVGA3dTransferType transfer; 67 s32 left, right, top, bottom; 68 u32 pitch; 69 union { 70 struct vmw_dma_buffer *buf; 71 u32 sid; 72 }; 73 }; 74 75 /* 76 * SVGA commands that are used by this code. Please see the device headers 77 * for explanation. 78 */ 79 struct vmw_stdu_update { 80 SVGA3dCmdHeader header; 81 SVGA3dCmdUpdateGBScreenTarget body; 82 }; 83 84 struct vmw_stdu_dma { 85 SVGA3dCmdHeader header; 86 SVGA3dCmdSurfaceDMA body; 87 }; 88 89 struct vmw_stdu_surface_copy { 90 SVGA3dCmdHeader header; 91 SVGA3dCmdSurfaceCopy body; 92 }; 93 94 95 /** 96 * struct vmw_screen_target_display_unit 97 * 98 * @base: VMW specific DU structure 99 * @display_srf: surface to be displayed. The dimension of this will always 100 * match the display mode. If the display mode matches 101 * content_vfbs dimensions, then this is a pointer into the 102 * corresponding field in content_vfbs. If not, then this 103 * is a separate buffer to which content_vfbs will blit to. 104 * @content_fb: holds the rendered content, can be a surface or DMA buffer 105 * @content_type: content_fb type 106 * @defined: true if the current display unit has been initialized 107 */ 108 struct vmw_screen_target_display_unit { 109 struct vmw_display_unit base; 110 111 struct vmw_surface *display_srf; 112 struct drm_framebuffer *content_fb; 113 114 enum stdu_content_type content_fb_type; 115 116 bool defined; 117 }; 118 119 120 121 static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu); 122 123 124 125 /****************************************************************************** 126 * Screen Target Display Unit helper Functions 127 *****************************************************************************/ 128 129 /** 130 * vmw_stdu_pin_display - pins the resource associated with the display surface 131 * 132 * @stdu: contains the display surface 133 * 134 * Since the display surface can either be a private surface allocated by us, 135 * or it can point to the content surface, we use this function to not pin the 136 * same resource twice. 137 */ 138 static int vmw_stdu_pin_display(struct vmw_screen_target_display_unit *stdu) 139 { 140 return vmw_resource_pin(&stdu->display_srf->res, false); 141 } 142 143 144 145 /** 146 * vmw_stdu_unpin_display - unpins the resource associated with display surface 147 * 148 * @stdu: contains the display surface 149 * 150 * If the display surface was privatedly allocated by 151 * vmw_surface_gb_priv_define() and not registered as a framebuffer, then it 152 * won't be automatically cleaned up when all the framebuffers are freed. As 153 * such, we have to explicitly call vmw_resource_unreference() to get it freed. 154 */ 155 static void vmw_stdu_unpin_display(struct vmw_screen_target_display_unit *stdu) 156 { 157 if (stdu->display_srf) { 158 struct vmw_resource *res = &stdu->display_srf->res; 159 160 vmw_resource_unpin(res); 161 162 if (stdu->content_fb_type != SAME_AS_DISPLAY) { 163 vmw_resource_unreference(&res); 164 stdu->content_fb_type = SAME_AS_DISPLAY; 165 } 166 167 stdu->display_srf = NULL; 168 } 169 } 170 171 172 173 /****************************************************************************** 174 * Screen Target Display Unit CRTC Functions 175 *****************************************************************************/ 176 177 178 /** 179 * vmw_stdu_crtc_destroy - cleans up the STDU 180 * 181 * @crtc: used to get a reference to the containing STDU 182 */ 183 static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc) 184 { 185 vmw_stdu_destroy(vmw_crtc_to_stdu(crtc)); 186 } 187 188 /** 189 * vmw_stdu_define_st - Defines a Screen Target 190 * 191 * @dev_priv: VMW DRM device 192 * @stdu: display unit to create a Screen Target for 193 * 194 * Creates a STDU that we can used later. This function is called whenever the 195 * framebuffer size changes. 196 * 197 * RETURNs: 198 * 0 on success, error code on failure 199 */ 200 static int vmw_stdu_define_st(struct vmw_private *dev_priv, 201 struct vmw_screen_target_display_unit *stdu) 202 { 203 struct { 204 SVGA3dCmdHeader header; 205 SVGA3dCmdDefineGBScreenTarget body; 206 } *cmd; 207 208 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); 209 210 if (unlikely(cmd == NULL)) { 211 DRM_ERROR("Out of FIFO space defining Screen Target\n"); 212 return -ENOMEM; 213 } 214 215 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET; 216 cmd->header.size = sizeof(cmd->body); 217 218 cmd->body.stid = stdu->base.unit; 219 cmd->body.width = stdu->display_srf->base_size.width; 220 cmd->body.height = stdu->display_srf->base_size.height; 221 cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0; 222 cmd->body.dpi = 0; 223 cmd->body.xRoot = stdu->base.crtc.x; 224 cmd->body.yRoot = stdu->base.crtc.y; 225 226 if (!stdu->base.is_implicit) { 227 cmd->body.xRoot = stdu->base.gui_x; 228 cmd->body.yRoot = stdu->base.gui_y; 229 } 230 231 vmw_fifo_commit(dev_priv, sizeof(*cmd)); 232 233 stdu->defined = true; 234 235 return 0; 236 } 237 238 239 240 /** 241 * vmw_stdu_bind_st - Binds a surface to a Screen Target 242 * 243 * @dev_priv: VMW DRM device 244 * @stdu: display unit affected 245 * @res: Buffer to bind to the screen target. Set to NULL to blank screen. 246 * 247 * Binding a surface to a Screen Target the same as flipping 248 */ 249 static int vmw_stdu_bind_st(struct vmw_private *dev_priv, 250 struct vmw_screen_target_display_unit *stdu, 251 struct vmw_resource *res) 252 { 253 SVGA3dSurfaceImageId image; 254 255 struct { 256 SVGA3dCmdHeader header; 257 SVGA3dCmdBindGBScreenTarget body; 258 } *cmd; 259 260 261 if (!stdu->defined) { 262 DRM_ERROR("No screen target defined\n"); 263 return -EINVAL; 264 } 265 266 /* Set up image using information in vfb */ 267 memset(&image, 0, sizeof(image)); 268 image.sid = res ? res->id : SVGA3D_INVALID_ID; 269 270 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); 271 272 if (unlikely(cmd == NULL)) { 273 DRM_ERROR("Out of FIFO space binding a screen target\n"); 274 return -ENOMEM; 275 } 276 277 cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET; 278 cmd->header.size = sizeof(cmd->body); 279 280 cmd->body.stid = stdu->base.unit; 281 cmd->body.image = image; 282 283 vmw_fifo_commit(dev_priv, sizeof(*cmd)); 284 285 return 0; 286 } 287 288 /** 289 * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a 290 * bounding box. 291 * 292 * @cmd: Pointer to command stream. 293 * @unit: Screen target unit. 294 * @left: Left side of bounding box. 295 * @right: Right side of bounding box. 296 * @top: Top side of bounding box. 297 * @bottom: Bottom side of bounding box. 298 */ 299 static void vmw_stdu_populate_update(void *cmd, int unit, 300 s32 left, s32 right, s32 top, s32 bottom) 301 { 302 struct vmw_stdu_update *update = cmd; 303 304 update->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET; 305 update->header.size = sizeof(update->body); 306 307 update->body.stid = unit; 308 update->body.rect.x = left; 309 update->body.rect.y = top; 310 update->body.rect.w = right - left; 311 update->body.rect.h = bottom - top; 312 } 313 314 /** 315 * vmw_stdu_update_st - Full update of a Screen Target 316 * 317 * @dev_priv: VMW DRM device 318 * @stdu: display unit affected 319 * 320 * This function needs to be called whenever the content of a screen 321 * target has changed completely. Typically as a result of a backing 322 * surface change. 323 * 324 * RETURNS: 325 * 0 on success, error code on failure 326 */ 327 static int vmw_stdu_update_st(struct vmw_private *dev_priv, 328 struct vmw_screen_target_display_unit *stdu) 329 { 330 struct vmw_stdu_update *cmd; 331 struct drm_crtc *crtc = &stdu->base.crtc; 332 333 if (!stdu->defined) { 334 DRM_ERROR("No screen target defined"); 335 return -EINVAL; 336 } 337 338 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); 339 340 if (unlikely(cmd == NULL)) { 341 DRM_ERROR("Out of FIFO space updating a Screen Target\n"); 342 return -ENOMEM; 343 } 344 345 vmw_stdu_populate_update(cmd, stdu->base.unit, 0, crtc->mode.hdisplay, 346 0, crtc->mode.vdisplay); 347 348 vmw_fifo_commit(dev_priv, sizeof(*cmd)); 349 350 return 0; 351 } 352 353 354 355 /** 356 * vmw_stdu_destroy_st - Destroy a Screen Target 357 * 358 * @dev_priv: VMW DRM device 359 * @stdu: display unit to destroy 360 */ 361 static int vmw_stdu_destroy_st(struct vmw_private *dev_priv, 362 struct vmw_screen_target_display_unit *stdu) 363 { 364 int ret; 365 366 struct { 367 SVGA3dCmdHeader header; 368 SVGA3dCmdDestroyGBScreenTarget body; 369 } *cmd; 370 371 372 /* Nothing to do if not successfully defined */ 373 if (unlikely(!stdu->defined)) 374 return 0; 375 376 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); 377 378 if (unlikely(cmd == NULL)) { 379 DRM_ERROR("Out of FIFO space, screen target not destroyed\n"); 380 return -ENOMEM; 381 } 382 383 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET; 384 cmd->header.size = sizeof(cmd->body); 385 386 cmd->body.stid = stdu->base.unit; 387 388 vmw_fifo_commit(dev_priv, sizeof(*cmd)); 389 390 /* Force sync */ 391 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ); 392 if (unlikely(ret != 0)) 393 DRM_ERROR("Failed to sync with HW"); 394 395 stdu->defined = false; 396 397 return ret; 398 } 399 400 401 402 /** 403 * vmw_stdu_crtc_set_config - Sets a mode 404 * 405 * @set: mode parameters 406 * 407 * This function is the device-specific portion of the DRM CRTC mode set. 408 * For the SVGA device, we do this by defining a Screen Target, binding a 409 * GB Surface to that target, and finally update the screen target. 410 * 411 * RETURNS: 412 * 0 on success, error code otherwise 413 */ 414 static int vmw_stdu_crtc_set_config(struct drm_mode_set *set) 415 { 416 struct vmw_private *dev_priv; 417 struct vmw_screen_target_display_unit *stdu; 418 struct vmw_framebuffer *vfb; 419 struct vmw_framebuffer_surface *new_vfbs; 420 struct drm_display_mode *mode; 421 struct drm_framebuffer *new_fb; 422 struct drm_crtc *crtc; 423 struct drm_encoder *encoder; 424 struct drm_connector *connector; 425 int ret; 426 427 428 if (!set || !set->crtc) 429 return -EINVAL; 430 431 crtc = set->crtc; 432 crtc->x = set->x; 433 crtc->y = set->y; 434 stdu = vmw_crtc_to_stdu(crtc); 435 mode = set->mode; 436 new_fb = set->fb; 437 dev_priv = vmw_priv(crtc->dev); 438 439 440 if (set->num_connectors > 1) { 441 DRM_ERROR("Too many connectors\n"); 442 return -EINVAL; 443 } 444 445 if (set->num_connectors == 1 && 446 set->connectors[0] != &stdu->base.connector) { 447 DRM_ERROR("Connectors don't match %p %p\n", 448 set->connectors[0], &stdu->base.connector); 449 return -EINVAL; 450 } 451 452 453 /* Since they always map one to one these are safe */ 454 connector = &stdu->base.connector; 455 encoder = &stdu->base.encoder; 456 457 458 /* 459 * After this point the CRTC will be considered off unless a new fb 460 * is bound 461 */ 462 if (stdu->defined) { 463 /* Unbind current surface by binding an invalid one */ 464 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL); 465 if (unlikely(ret != 0)) 466 return ret; 467 468 /* Update Screen Target, display will now be blank */ 469 if (crtc->primary->fb) { 470 vmw_stdu_update_st(dev_priv, stdu); 471 if (unlikely(ret != 0)) 472 return ret; 473 } 474 475 crtc->primary->fb = NULL; 476 crtc->enabled = false; 477 encoder->crtc = NULL; 478 connector->encoder = NULL; 479 480 vmw_stdu_unpin_display(stdu); 481 stdu->content_fb = NULL; 482 stdu->content_fb_type = SAME_AS_DISPLAY; 483 484 ret = vmw_stdu_destroy_st(dev_priv, stdu); 485 /* The hardware is hung, give up */ 486 if (unlikely(ret != 0)) 487 return ret; 488 } 489 490 491 /* Any of these conditions means the caller wants CRTC off */ 492 if (set->num_connectors == 0 || !mode || !new_fb) 493 return 0; 494 495 496 if (set->x + mode->hdisplay > new_fb->width || 497 set->y + mode->vdisplay > new_fb->height) { 498 DRM_ERROR("Set outside of framebuffer\n"); 499 return -EINVAL; 500 } 501 502 stdu->content_fb = new_fb; 503 vfb = vmw_framebuffer_to_vfb(stdu->content_fb); 504 505 if (vfb->dmabuf) 506 stdu->content_fb_type = SEPARATE_DMA; 507 508 /* 509 * If the requested mode is different than the width and height 510 * of the FB or if the content buffer is a DMA buf, then allocate 511 * a display FB that matches the dimension of the mode 512 */ 513 if (mode->hdisplay != new_fb->width || 514 mode->vdisplay != new_fb->height || 515 stdu->content_fb_type != SAME_AS_DISPLAY) { 516 struct vmw_surface content_srf; 517 struct drm_vmw_size display_base_size = {0}; 518 struct vmw_surface *display_srf; 519 520 521 display_base_size.width = mode->hdisplay; 522 display_base_size.height = mode->vdisplay; 523 display_base_size.depth = 1; 524 525 /* 526 * If content buffer is a DMA buf, then we have to construct 527 * surface info 528 */ 529 if (stdu->content_fb_type == SEPARATE_DMA) { 530 531 switch (new_fb->bits_per_pixel) { 532 case 32: 533 content_srf.format = SVGA3D_X8R8G8B8; 534 break; 535 536 case 16: 537 content_srf.format = SVGA3D_R5G6B5; 538 break; 539 540 case 8: 541 content_srf.format = SVGA3D_P8; 542 break; 543 544 default: 545 DRM_ERROR("Invalid format\n"); 546 ret = -EINVAL; 547 goto err_unref_content; 548 } 549 550 content_srf.flags = 0; 551 content_srf.mip_levels[0] = 1; 552 content_srf.multisample_count = 0; 553 } else { 554 555 stdu->content_fb_type = SEPARATE_SURFACE; 556 557 new_vfbs = vmw_framebuffer_to_vfbs(new_fb); 558 content_srf = *new_vfbs->surface; 559 } 560 561 562 ret = vmw_surface_gb_priv_define(crtc->dev, 563 0, /* because kernel visible only */ 564 content_srf.flags, 565 content_srf.format, 566 true, /* a scanout buffer */ 567 content_srf.mip_levels[0], 568 content_srf.multisample_count, 569 0, 570 display_base_size, 571 &display_srf); 572 if (unlikely(ret != 0)) { 573 DRM_ERROR("Cannot allocate a display FB.\n"); 574 goto err_unref_content; 575 } 576 577 stdu->display_srf = display_srf; 578 } else { 579 new_vfbs = vmw_framebuffer_to_vfbs(new_fb); 580 stdu->display_srf = new_vfbs->surface; 581 } 582 583 584 ret = vmw_stdu_pin_display(stdu); 585 if (unlikely(ret != 0)) { 586 stdu->display_srf = NULL; 587 goto err_unref_content; 588 } 589 590 vmw_svga_enable(dev_priv); 591 592 /* 593 * Steps to displaying a surface, assume surface is already 594 * bound: 595 * 1. define a screen target 596 * 2. bind a fb to the screen target 597 * 3. update that screen target (this is done later by 598 * vmw_kms_stdu_do_surface_dirty_or_present) 599 */ 600 ret = vmw_stdu_define_st(dev_priv, stdu); 601 if (unlikely(ret != 0)) 602 goto err_unpin_display_and_content; 603 604 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res); 605 if (unlikely(ret != 0)) 606 goto err_unpin_destroy_st; 607 608 609 connector->encoder = encoder; 610 encoder->crtc = crtc; 611 612 crtc->mode = *mode; 613 crtc->primary->fb = new_fb; 614 crtc->enabled = true; 615 616 return ret; 617 618 err_unpin_destroy_st: 619 vmw_stdu_destroy_st(dev_priv, stdu); 620 err_unpin_display_and_content: 621 vmw_stdu_unpin_display(stdu); 622 err_unref_content: 623 stdu->content_fb = NULL; 624 return ret; 625 } 626 627 628 629 /** 630 * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target 631 * 632 * @crtc: CRTC to attach FB to 633 * @fb: FB to attach 634 * @event: Event to be posted. This event should've been alloced 635 * using k[mz]alloc, and should've been completely initialized. 636 * @page_flip_flags: Input flags. 637 * 638 * If the STDU uses the same display and content buffers, i.e. a true flip, 639 * this function will replace the existing display buffer with the new content 640 * buffer. 641 * 642 * If the STDU uses different display and content buffers, i.e. a blit, then 643 * only the content buffer will be updated. 644 * 645 * RETURNS: 646 * 0 on success, error code on failure 647 */ 648 static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc, 649 struct drm_framebuffer *new_fb, 650 struct drm_pending_vblank_event *event, 651 uint32_t flags) 652 653 { 654 struct vmw_private *dev_priv = vmw_priv(crtc->dev); 655 struct vmw_screen_target_display_unit *stdu; 656 int ret; 657 658 if (crtc == NULL) 659 return -EINVAL; 660 661 dev_priv = vmw_priv(crtc->dev); 662 stdu = vmw_crtc_to_stdu(crtc); 663 crtc->primary->fb = new_fb; 664 stdu->content_fb = new_fb; 665 666 if (stdu->display_srf) { 667 /* 668 * If the display surface is the same as the content surface 669 * then remove the reference 670 */ 671 if (stdu->content_fb_type == SAME_AS_DISPLAY) { 672 if (stdu->defined) { 673 /* Unbind the current surface */ 674 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL); 675 if (unlikely(ret != 0)) 676 goto err_out; 677 } 678 vmw_stdu_unpin_display(stdu); 679 stdu->display_srf = NULL; 680 } 681 } 682 683 684 if (!new_fb) { 685 /* Blanks the display */ 686 (void) vmw_stdu_update_st(dev_priv, stdu); 687 688 return 0; 689 } 690 691 692 if (stdu->content_fb_type == SAME_AS_DISPLAY) { 693 stdu->display_srf = vmw_framebuffer_to_vfbs(new_fb)->surface; 694 ret = vmw_stdu_pin_display(stdu); 695 if (ret) { 696 stdu->display_srf = NULL; 697 goto err_out; 698 } 699 700 /* Bind display surface */ 701 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res); 702 if (unlikely(ret != 0)) 703 goto err_unpin_display_and_content; 704 } 705 706 /* Update display surface: after this point everything is bound */ 707 ret = vmw_stdu_update_st(dev_priv, stdu); 708 if (unlikely(ret != 0)) 709 return ret; 710 711 if (event) { 712 struct vmw_fence_obj *fence = NULL; 713 struct drm_file *file_priv = event->base.file_priv; 714 715 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL); 716 if (!fence) 717 return -ENOMEM; 718 719 ret = vmw_event_fence_action_queue(file_priv, fence, 720 &event->base, 721 &event->event.tv_sec, 722 &event->event.tv_usec, 723 true); 724 vmw_fence_obj_unreference(&fence); 725 } else { 726 vmw_fifo_flush(dev_priv, false); 727 } 728 729 return ret; 730 731 err_unpin_display_and_content: 732 vmw_stdu_unpin_display(stdu); 733 err_out: 734 crtc->primary->fb = NULL; 735 stdu->content_fb = NULL; 736 return ret; 737 } 738 739 740 /** 741 * vmw_stdu_dmabuf_clip - Callback to encode a suface DMA command cliprect 742 * 743 * @dirty: The closure structure. 744 * 745 * Encodes a surface DMA command cliprect and updates the bounding box 746 * for the DMA. 747 */ 748 static void vmw_stdu_dmabuf_clip(struct vmw_kms_dirty *dirty) 749 { 750 struct vmw_stdu_dirty *ddirty = 751 container_of(dirty, struct vmw_stdu_dirty, base); 752 struct vmw_stdu_dma *cmd = dirty->cmd; 753 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1]; 754 755 blit += dirty->num_hits; 756 blit->srcx = dirty->fb_x; 757 blit->srcy = dirty->fb_y; 758 blit->x = dirty->unit_x1; 759 blit->y = dirty->unit_y1; 760 blit->d = 1; 761 blit->w = dirty->unit_x2 - dirty->unit_x1; 762 blit->h = dirty->unit_y2 - dirty->unit_y1; 763 dirty->num_hits++; 764 765 if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM) 766 return; 767 768 /* Destination bounding box */ 769 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1); 770 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1); 771 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2); 772 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2); 773 } 774 775 /** 776 * vmw_stdu_dmabuf_fifo_commit - Callback to fill in and submit a DMA command. 777 * 778 * @dirty: The closure structure. 779 * 780 * Fills in the missing fields in a DMA command, and optionally encodes 781 * a screen target update command, depending on transfer direction. 782 */ 783 static void vmw_stdu_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty) 784 { 785 struct vmw_stdu_dirty *ddirty = 786 container_of(dirty, struct vmw_stdu_dirty, base); 787 struct vmw_screen_target_display_unit *stdu = 788 container_of(dirty->unit, typeof(*stdu), base); 789 struct vmw_stdu_dma *cmd = dirty->cmd; 790 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1]; 791 SVGA3dCmdSurfaceDMASuffix *suffix = 792 (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits]; 793 size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix); 794 795 if (!dirty->num_hits) { 796 vmw_fifo_commit(dirty->dev_priv, 0); 797 return; 798 } 799 800 cmd->header.id = SVGA_3D_CMD_SURFACE_DMA; 801 cmd->header.size = sizeof(cmd->body) + blit_size; 802 vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr); 803 cmd->body.guest.pitch = ddirty->pitch; 804 cmd->body.host.sid = stdu->display_srf->res.id; 805 cmd->body.host.face = 0; 806 cmd->body.host.mipmap = 0; 807 cmd->body.transfer = ddirty->transfer; 808 suffix->suffixSize = sizeof(*suffix); 809 suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE; 810 811 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) { 812 blit_size += sizeof(struct vmw_stdu_update); 813 814 vmw_stdu_populate_update(&suffix[1], stdu->base.unit, 815 ddirty->left, ddirty->right, 816 ddirty->top, ddirty->bottom); 817 } 818 819 vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size); 820 821 ddirty->left = ddirty->top = S32_MAX; 822 ddirty->right = ddirty->bottom = S32_MIN; 823 } 824 825 /** 826 * vmw_kms_stdu_dma - Perform a DMA transfer between a dma-buffer backed 827 * framebuffer and the screen target system. 828 * 829 * @dev_priv: Pointer to the device private structure. 830 * @file_priv: Pointer to a struct drm-file identifying the caller. May be 831 * set to NULL, but then @user_fence_rep must also be set to NULL. 832 * @vfb: Pointer to the dma-buffer backed framebuffer. 833 * @clips: Array of clip rects. Either @clips or @vclips must be NULL. 834 * @vclips: Alternate array of clip rects. Either @clips or @vclips must 835 * be NULL. 836 * @num_clips: Number of clip rects in @clips or @vclips. 837 * @increment: Increment to use when looping over @clips or @vclips. 838 * @to_surface: Whether to DMA to the screen target system as opposed to 839 * from the screen target system. 840 * @interruptible: Whether to perform waits interruptible if possible. 841 * 842 * If DMA-ing till the screen target system, the function will also notify 843 * the screen target system that a bounding box of the cliprects has been 844 * updated. 845 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if 846 * interrupted. 847 */ 848 int vmw_kms_stdu_dma(struct vmw_private *dev_priv, 849 struct drm_file *file_priv, 850 struct vmw_framebuffer *vfb, 851 struct drm_vmw_fence_rep __user *user_fence_rep, 852 struct drm_clip_rect *clips, 853 struct drm_vmw_rect *vclips, 854 uint32_t num_clips, 855 int increment, 856 bool to_surface, 857 bool interruptible) 858 { 859 struct vmw_dma_buffer *buf = 860 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer; 861 struct vmw_stdu_dirty ddirty; 862 int ret; 863 864 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible, 865 false); 866 if (ret) 867 return ret; 868 869 ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM : 870 SVGA3D_READ_HOST_VRAM; 871 ddirty.left = ddirty.top = S32_MAX; 872 ddirty.right = ddirty.bottom = S32_MIN; 873 ddirty.pitch = vfb->base.pitches[0]; 874 ddirty.buf = buf; 875 ddirty.base.fifo_commit = vmw_stdu_dmabuf_fifo_commit; 876 ddirty.base.clip = vmw_stdu_dmabuf_clip; 877 ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) + 878 num_clips * sizeof(SVGA3dCopyBox) + 879 sizeof(SVGA3dCmdSurfaceDMASuffix); 880 if (to_surface) 881 ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update); 882 883 ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips, 884 0, 0, num_clips, increment, &ddirty.base); 885 vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL, 886 user_fence_rep); 887 888 return ret; 889 } 890 891 /** 892 * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect 893 * 894 * @dirty: The closure structure. 895 * 896 * Encodes a surface copy command cliprect and updates the bounding box 897 * for the copy. 898 */ 899 static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty) 900 { 901 struct vmw_stdu_dirty *sdirty = 902 container_of(dirty, struct vmw_stdu_dirty, base); 903 struct vmw_stdu_surface_copy *cmd = dirty->cmd; 904 struct vmw_screen_target_display_unit *stdu = 905 container_of(dirty->unit, typeof(*stdu), base); 906 907 if (sdirty->sid != stdu->display_srf->res.id) { 908 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1]; 909 910 blit += dirty->num_hits; 911 blit->srcx = dirty->fb_x; 912 blit->srcy = dirty->fb_y; 913 blit->x = dirty->unit_x1; 914 blit->y = dirty->unit_y1; 915 blit->d = 1; 916 blit->w = dirty->unit_x2 - dirty->unit_x1; 917 blit->h = dirty->unit_y2 - dirty->unit_y1; 918 } 919 920 dirty->num_hits++; 921 922 /* Destination bounding box */ 923 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1); 924 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1); 925 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2); 926 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2); 927 } 928 929 /** 930 * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface 931 * copy command. 932 * 933 * @dirty: The closure structure. 934 * 935 * Fills in the missing fields in a surface copy command, and encodes a screen 936 * target update command. 937 */ 938 static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty) 939 { 940 struct vmw_stdu_dirty *sdirty = 941 container_of(dirty, struct vmw_stdu_dirty, base); 942 struct vmw_screen_target_display_unit *stdu = 943 container_of(dirty->unit, typeof(*stdu), base); 944 struct vmw_stdu_surface_copy *cmd = dirty->cmd; 945 struct vmw_stdu_update *update; 946 size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits; 947 size_t commit_size; 948 949 if (!dirty->num_hits) { 950 vmw_fifo_commit(dirty->dev_priv, 0); 951 return; 952 } 953 954 if (sdirty->sid != stdu->display_srf->res.id) { 955 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1]; 956 957 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY; 958 cmd->header.size = sizeof(cmd->body) + blit_size; 959 cmd->body.src.sid = sdirty->sid; 960 cmd->body.dest.sid = stdu->display_srf->res.id; 961 update = (struct vmw_stdu_update *) &blit[dirty->num_hits]; 962 commit_size = sizeof(*cmd) + blit_size + sizeof(*update); 963 } else { 964 update = dirty->cmd; 965 commit_size = sizeof(*update); 966 } 967 968 vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left, 969 sdirty->right, sdirty->top, sdirty->bottom); 970 971 vmw_fifo_commit(dirty->dev_priv, commit_size); 972 973 sdirty->left = sdirty->top = S32_MAX; 974 sdirty->right = sdirty->bottom = S32_MIN; 975 } 976 977 /** 978 * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer 979 * 980 * @dev_priv: Pointer to the device private structure. 981 * @framebuffer: Pointer to the surface-buffer backed framebuffer. 982 * @clips: Array of clip rects. Either @clips or @vclips must be NULL. 983 * @vclips: Alternate array of clip rects. Either @clips or @vclips must 984 * be NULL. 985 * @srf: Pointer to surface to blit from. If NULL, the surface attached 986 * to @framebuffer will be used. 987 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates. 988 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates. 989 * @num_clips: Number of clip rects in @clips. 990 * @inc: Increment to use when looping over @clips. 991 * @out_fence: If non-NULL, will return a ref-counted pointer to a 992 * struct vmw_fence_obj. The returned fence pointer may be NULL in which 993 * case the device has already synchronized. 994 * 995 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if 996 * interrupted. 997 */ 998 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv, 999 struct vmw_framebuffer *framebuffer, 1000 struct drm_clip_rect *clips, 1001 struct drm_vmw_rect *vclips, 1002 struct vmw_resource *srf, 1003 s32 dest_x, 1004 s32 dest_y, 1005 unsigned num_clips, int inc, 1006 struct vmw_fence_obj **out_fence) 1007 { 1008 struct vmw_framebuffer_surface *vfbs = 1009 container_of(framebuffer, typeof(*vfbs), base); 1010 struct vmw_stdu_dirty sdirty; 1011 struct vmw_validation_ctx ctx; 1012 int ret; 1013 1014 if (!srf) 1015 srf = &vfbs->surface->res; 1016 1017 ret = vmw_kms_helper_resource_prepare(srf, true, &ctx); 1018 if (ret) 1019 return ret; 1020 1021 if (vfbs->is_dmabuf_proxy) { 1022 ret = vmw_kms_update_proxy(srf, clips, num_clips, inc); 1023 if (ret) 1024 goto out_finish; 1025 } 1026 1027 sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit; 1028 sdirty.base.clip = vmw_kms_stdu_surface_clip; 1029 sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) + 1030 sizeof(SVGA3dCopyBox) * num_clips + 1031 sizeof(struct vmw_stdu_update); 1032 sdirty.sid = srf->id; 1033 sdirty.left = sdirty.top = S32_MAX; 1034 sdirty.right = sdirty.bottom = S32_MIN; 1035 1036 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips, 1037 dest_x, dest_y, num_clips, inc, 1038 &sdirty.base); 1039 out_finish: 1040 vmw_kms_helper_resource_finish(&ctx, out_fence); 1041 1042 return ret; 1043 } 1044 1045 1046 /* 1047 * Screen Target CRTC dispatch table 1048 */ 1049 static struct drm_crtc_funcs vmw_stdu_crtc_funcs = { 1050 .save = vmw_du_crtc_save, 1051 .restore = vmw_du_crtc_restore, 1052 .cursor_set2 = vmw_du_crtc_cursor_set2, 1053 .cursor_move = vmw_du_crtc_cursor_move, 1054 .gamma_set = vmw_du_crtc_gamma_set, 1055 .destroy = vmw_stdu_crtc_destroy, 1056 .set_config = vmw_stdu_crtc_set_config, 1057 .page_flip = vmw_stdu_crtc_page_flip, 1058 }; 1059 1060 1061 1062 /****************************************************************************** 1063 * Screen Target Display Unit Encoder Functions 1064 *****************************************************************************/ 1065 1066 /** 1067 * vmw_stdu_encoder_destroy - cleans up the STDU 1068 * 1069 * @encoder: used the get the containing STDU 1070 * 1071 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically 1072 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case 1073 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't 1074 * get called. 1075 */ 1076 static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder) 1077 { 1078 vmw_stdu_destroy(vmw_encoder_to_stdu(encoder)); 1079 } 1080 1081 static struct drm_encoder_funcs vmw_stdu_encoder_funcs = { 1082 .destroy = vmw_stdu_encoder_destroy, 1083 }; 1084 1085 1086 1087 /****************************************************************************** 1088 * Screen Target Display Unit Connector Functions 1089 *****************************************************************************/ 1090 1091 /** 1092 * vmw_stdu_connector_destroy - cleans up the STDU 1093 * 1094 * @connector: used to get the containing STDU 1095 * 1096 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically 1097 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case 1098 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't 1099 * get called. 1100 */ 1101 static void vmw_stdu_connector_destroy(struct drm_connector *connector) 1102 { 1103 vmw_stdu_destroy(vmw_connector_to_stdu(connector)); 1104 } 1105 1106 1107 1108 static struct drm_connector_funcs vmw_stdu_connector_funcs = { 1109 .dpms = vmw_du_connector_dpms, 1110 .save = vmw_du_connector_save, 1111 .restore = vmw_du_connector_restore, 1112 .detect = vmw_du_connector_detect, 1113 .fill_modes = vmw_du_connector_fill_modes, 1114 .set_property = vmw_du_connector_set_property, 1115 .destroy = vmw_stdu_connector_destroy, 1116 }; 1117 1118 1119 1120 /** 1121 * vmw_stdu_init - Sets up a Screen Target Display Unit 1122 * 1123 * @dev_priv: VMW DRM device 1124 * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS 1125 * 1126 * This function is called once per CRTC, and allocates one Screen Target 1127 * display unit to represent that CRTC. Since the SVGA device does not separate 1128 * out encoder and connector, they are represented as part of the STDU as well. 1129 */ 1130 static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit) 1131 { 1132 struct vmw_screen_target_display_unit *stdu; 1133 struct drm_device *dev = dev_priv->dev; 1134 struct drm_connector *connector; 1135 struct drm_encoder *encoder; 1136 struct drm_crtc *crtc; 1137 1138 1139 stdu = kzalloc(sizeof(*stdu), GFP_KERNEL); 1140 if (!stdu) 1141 return -ENOMEM; 1142 1143 stdu->base.unit = unit; 1144 crtc = &stdu->base.crtc; 1145 encoder = &stdu->base.encoder; 1146 connector = &stdu->base.connector; 1147 1148 stdu->base.pref_active = (unit == 0); 1149 stdu->base.pref_width = dev_priv->initial_width; 1150 stdu->base.pref_height = dev_priv->initial_height; 1151 stdu->base.is_implicit = true; 1152 1153 drm_connector_init(dev, connector, &vmw_stdu_connector_funcs, 1154 DRM_MODE_CONNECTOR_VIRTUAL); 1155 connector->status = vmw_du_connector_detect(connector, false); 1156 1157 drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs, 1158 DRM_MODE_ENCODER_VIRTUAL); 1159 drm_mode_connector_attach_encoder(connector, encoder); 1160 encoder->possible_crtcs = (1 << unit); 1161 encoder->possible_clones = 0; 1162 1163 (void) drm_connector_register(connector); 1164 1165 drm_crtc_init(dev, crtc, &vmw_stdu_crtc_funcs); 1166 1167 drm_mode_crtc_set_gamma_size(crtc, 256); 1168 1169 drm_object_attach_property(&connector->base, 1170 dev->mode_config.dirty_info_property, 1171 1); 1172 1173 return 0; 1174 } 1175 1176 1177 1178 /** 1179 * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit 1180 * 1181 * @stdu: Screen Target Display Unit to be destroyed 1182 * 1183 * Clean up after vmw_stdu_init 1184 */ 1185 static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu) 1186 { 1187 vmw_stdu_unpin_display(stdu); 1188 1189 vmw_du_cleanup(&stdu->base); 1190 kfree(stdu); 1191 } 1192 1193 1194 1195 /****************************************************************************** 1196 * Screen Target Display KMS Functions 1197 * 1198 * These functions are called by the common KMS code in vmwgfx_kms.c 1199 *****************************************************************************/ 1200 1201 /** 1202 * vmw_kms_stdu_init_display - Initializes a Screen Target based display 1203 * 1204 * @dev_priv: VMW DRM device 1205 * 1206 * This function initialize a Screen Target based display device. It checks 1207 * the capability bits to make sure the underlying hardware can support 1208 * screen targets, and then creates the maximum number of CRTCs, a.k.a Display 1209 * Units, as supported by the display hardware. 1210 * 1211 * RETURNS: 1212 * 0 on success, error code otherwise 1213 */ 1214 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv) 1215 { 1216 struct drm_device *dev = dev_priv->dev; 1217 int i, ret; 1218 1219 1220 /* Do nothing if Screen Target support is turned off */ 1221 if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE) 1222 return -ENOSYS; 1223 1224 if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS)) 1225 return -ENOSYS; 1226 1227 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS); 1228 if (unlikely(ret != 0)) 1229 return ret; 1230 1231 ret = drm_mode_create_dirty_info_property(dev); 1232 if (unlikely(ret != 0)) 1233 goto err_vblank_cleanup; 1234 1235 dev_priv->active_display_unit = vmw_du_screen_target; 1236 1237 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) { 1238 ret = vmw_stdu_init(dev_priv, i); 1239 1240 if (unlikely(ret != 0)) { 1241 DRM_ERROR("Failed to initialize STDU %d", i); 1242 goto err_vblank_cleanup; 1243 } 1244 } 1245 1246 DRM_INFO("Screen Target Display device initialized\n"); 1247 1248 return 0; 1249 1250 err_vblank_cleanup: 1251 drm_vblank_cleanup(dev); 1252 return ret; 1253 } 1254 1255 1256 1257 /** 1258 * vmw_kms_stdu_close_display - Cleans up after vmw_kms_stdu_init_display 1259 * 1260 * @dev_priv: VMW DRM device 1261 * 1262 * Frees up any resources allocated by vmw_kms_stdu_init_display 1263 * 1264 * RETURNS: 1265 * 0 on success 1266 */ 1267 int vmw_kms_stdu_close_display(struct vmw_private *dev_priv) 1268 { 1269 struct drm_device *dev = dev_priv->dev; 1270 1271 drm_vblank_cleanup(dev); 1272 1273 return 0; 1274 } 1275