1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /************************************************************************** 3 * 4 * Copyright 2011-2015 VMware, Inc., Palo Alto, CA., USA 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24 * USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 #include "vmwgfx_kms.h" 29 #include <drm/drm_plane_helper.h> 30 #include <drm/drm_atomic.h> 31 #include <drm/drm_atomic_helper.h> 32 33 34 #define vmw_crtc_to_sou(x) \ 35 container_of(x, struct vmw_screen_object_unit, base.crtc) 36 #define vmw_encoder_to_sou(x) \ 37 container_of(x, struct vmw_screen_object_unit, base.encoder) 38 #define vmw_connector_to_sou(x) \ 39 container_of(x, struct vmw_screen_object_unit, base.connector) 40 41 /** 42 * struct vmw_kms_sou_surface_dirty - Closure structure for 43 * blit surface to screen command. 44 * @base: The base type we derive from. Used by vmw_kms_helper_dirty(). 45 * @left: Left side of bounding box. 46 * @right: Right side of bounding box. 47 * @top: Top side of bounding box. 48 * @bottom: Bottom side of bounding box. 49 * @dst_x: Difference between source clip rects and framebuffer coordinates. 50 * @dst_y: Difference between source clip rects and framebuffer coordinates. 51 * @sid: Surface id of surface to copy from. 52 */ 53 struct vmw_kms_sou_surface_dirty { 54 struct vmw_kms_dirty base; 55 s32 left, right, top, bottom; 56 s32 dst_x, dst_y; 57 u32 sid; 58 }; 59 60 /* 61 * SVGA commands that are used by this code. Please see the device headers 62 * for explanation. 63 */ 64 struct vmw_kms_sou_readback_blit { 65 uint32 header; 66 SVGAFifoCmdBlitScreenToGMRFB body; 67 }; 68 69 struct vmw_kms_sou_bo_blit { 70 uint32 header; 71 SVGAFifoCmdBlitGMRFBToScreen body; 72 }; 73 74 struct vmw_kms_sou_dirty_cmd { 75 SVGA3dCmdHeader header; 76 SVGA3dCmdBlitSurfaceToScreen body; 77 }; 78 79 /** 80 * Display unit using screen objects. 81 */ 82 struct vmw_screen_object_unit { 83 struct vmw_display_unit base; 84 85 unsigned long buffer_size; /**< Size of allocated buffer */ 86 struct vmw_buffer_object *buffer; /**< Backing store buffer */ 87 88 bool defined; 89 }; 90 91 static void vmw_sou_destroy(struct vmw_screen_object_unit *sou) 92 { 93 vmw_du_cleanup(&sou->base); 94 kfree(sou); 95 } 96 97 98 /* 99 * Screen Object Display Unit CRTC functions 100 */ 101 102 static void vmw_sou_crtc_destroy(struct drm_crtc *crtc) 103 { 104 vmw_sou_destroy(vmw_crtc_to_sou(crtc)); 105 } 106 107 /** 108 * Send the fifo command to create a screen. 109 */ 110 static int vmw_sou_fifo_create(struct vmw_private *dev_priv, 111 struct vmw_screen_object_unit *sou, 112 int x, int y, 113 struct drm_display_mode *mode) 114 { 115 size_t fifo_size; 116 117 struct { 118 struct { 119 uint32_t cmdType; 120 } header; 121 SVGAScreenObject obj; 122 } *cmd; 123 124 BUG_ON(!sou->buffer); 125 126 fifo_size = sizeof(*cmd); 127 cmd = vmw_fifo_reserve(dev_priv, fifo_size); 128 /* The hardware has hung, nothing we can do about it here. */ 129 if (unlikely(cmd == NULL)) { 130 DRM_ERROR("Fifo reserve failed.\n"); 131 return -ENOMEM; 132 } 133 134 memset(cmd, 0, fifo_size); 135 cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN; 136 cmd->obj.structSize = sizeof(SVGAScreenObject); 137 cmd->obj.id = sou->base.unit; 138 cmd->obj.flags = SVGA_SCREEN_HAS_ROOT | 139 (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0); 140 cmd->obj.size.width = mode->hdisplay; 141 cmd->obj.size.height = mode->vdisplay; 142 cmd->obj.root.x = x; 143 cmd->obj.root.y = y; 144 sou->base.set_gui_x = cmd->obj.root.x; 145 sou->base.set_gui_y = cmd->obj.root.y; 146 147 /* Ok to assume that buffer is pinned in vram */ 148 vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr); 149 cmd->obj.backingStore.pitch = mode->hdisplay * 4; 150 151 vmw_fifo_commit(dev_priv, fifo_size); 152 153 sou->defined = true; 154 155 return 0; 156 } 157 158 /** 159 * Send the fifo command to destroy a screen. 160 */ 161 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv, 162 struct vmw_screen_object_unit *sou) 163 { 164 size_t fifo_size; 165 int ret; 166 167 struct { 168 struct { 169 uint32_t cmdType; 170 } header; 171 SVGAFifoCmdDestroyScreen body; 172 } *cmd; 173 174 /* no need to do anything */ 175 if (unlikely(!sou->defined)) 176 return 0; 177 178 fifo_size = sizeof(*cmd); 179 cmd = vmw_fifo_reserve(dev_priv, fifo_size); 180 /* the hardware has hung, nothing we can do about it here */ 181 if (unlikely(cmd == NULL)) { 182 DRM_ERROR("Fifo reserve failed.\n"); 183 return -ENOMEM; 184 } 185 186 memset(cmd, 0, fifo_size); 187 cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN; 188 cmd->body.screenId = sou->base.unit; 189 190 vmw_fifo_commit(dev_priv, fifo_size); 191 192 /* Force sync */ 193 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ); 194 if (unlikely(ret != 0)) 195 DRM_ERROR("Failed to sync with HW"); 196 else 197 sou->defined = false; 198 199 return ret; 200 } 201 202 /** 203 * vmw_sou_crtc_mode_set_nofb - Create new screen 204 * 205 * @crtc: CRTC associated with the new screen 206 * 207 * This function creates/destroys a screen. This function cannot fail, so if 208 * somehow we run into a failure, just do the best we can to get out. 209 */ 210 static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc) 211 { 212 struct vmw_private *dev_priv; 213 struct vmw_screen_object_unit *sou; 214 struct vmw_framebuffer *vfb; 215 struct drm_framebuffer *fb; 216 struct drm_plane_state *ps; 217 struct vmw_plane_state *vps; 218 int ret; 219 220 sou = vmw_crtc_to_sou(crtc); 221 dev_priv = vmw_priv(crtc->dev); 222 ps = crtc->primary->state; 223 fb = ps->fb; 224 vps = vmw_plane_state_to_vps(ps); 225 226 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL; 227 228 if (sou->defined) { 229 ret = vmw_sou_fifo_destroy(dev_priv, sou); 230 if (ret) { 231 DRM_ERROR("Failed to destroy Screen Object\n"); 232 return; 233 } 234 } 235 236 if (vfb) { 237 struct drm_connector_state *conn_state; 238 struct vmw_connector_state *vmw_conn_state; 239 int x, y; 240 241 sou->buffer = vps->bo; 242 sou->buffer_size = vps->bo_size; 243 244 if (sou->base.is_implicit) { 245 x = crtc->x; 246 y = crtc->y; 247 } else { 248 conn_state = sou->base.connector.state; 249 vmw_conn_state = vmw_connector_state_to_vcs(conn_state); 250 251 x = vmw_conn_state->gui_x; 252 y = vmw_conn_state->gui_y; 253 } 254 255 ret = vmw_sou_fifo_create(dev_priv, sou, x, y, &crtc->mode); 256 if (ret) 257 DRM_ERROR("Failed to define Screen Object %dx%d\n", 258 crtc->x, crtc->y); 259 260 vmw_kms_add_active(dev_priv, &sou->base, vfb); 261 } else { 262 sou->buffer = NULL; 263 sou->buffer_size = 0; 264 265 vmw_kms_del_active(dev_priv, &sou->base); 266 } 267 } 268 269 /** 270 * vmw_sou_crtc_helper_prepare - Noop 271 * 272 * @crtc: CRTC associated with the new screen 273 * 274 * Prepares the CRTC for a mode set, but we don't need to do anything here. 275 */ 276 static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc) 277 { 278 } 279 280 /** 281 * vmw_sou_crtc_atomic_enable - Noop 282 * 283 * @crtc: CRTC associated with the new screen 284 * 285 * This is called after a mode set has been completed. 286 */ 287 static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc, 288 struct drm_crtc_state *old_state) 289 { 290 } 291 292 /** 293 * vmw_sou_crtc_atomic_disable - Turns off CRTC 294 * 295 * @crtc: CRTC to be turned off 296 */ 297 static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc, 298 struct drm_crtc_state *old_state) 299 { 300 struct vmw_private *dev_priv; 301 struct vmw_screen_object_unit *sou; 302 int ret; 303 304 305 if (!crtc) { 306 DRM_ERROR("CRTC is NULL\n"); 307 return; 308 } 309 310 sou = vmw_crtc_to_sou(crtc); 311 dev_priv = vmw_priv(crtc->dev); 312 313 if (sou->defined) { 314 ret = vmw_sou_fifo_destroy(dev_priv, sou); 315 if (ret) 316 DRM_ERROR("Failed to destroy Screen Object\n"); 317 } 318 } 319 320 static int vmw_sou_crtc_page_flip(struct drm_crtc *crtc, 321 struct drm_framebuffer *new_fb, 322 struct drm_pending_vblank_event *event, 323 uint32_t flags, 324 struct drm_modeset_acquire_ctx *ctx) 325 { 326 struct vmw_private *dev_priv = vmw_priv(crtc->dev); 327 int ret; 328 329 if (!vmw_kms_crtc_flippable(dev_priv, crtc)) 330 return -EINVAL; 331 332 ret = drm_atomic_helper_page_flip(crtc, new_fb, event, flags, ctx); 333 if (ret) { 334 DRM_ERROR("Page flip error %d.\n", ret); 335 return ret; 336 } 337 338 if (vmw_crtc_to_du(crtc)->is_implicit) 339 vmw_kms_update_implicit_fb(dev_priv, crtc); 340 341 return ret; 342 } 343 344 static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = { 345 .gamma_set = vmw_du_crtc_gamma_set, 346 .destroy = vmw_sou_crtc_destroy, 347 .reset = vmw_du_crtc_reset, 348 .atomic_duplicate_state = vmw_du_crtc_duplicate_state, 349 .atomic_destroy_state = vmw_du_crtc_destroy_state, 350 .set_config = vmw_kms_set_config, 351 .page_flip = vmw_sou_crtc_page_flip, 352 }; 353 354 /* 355 * Screen Object Display Unit encoder functions 356 */ 357 358 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder) 359 { 360 vmw_sou_destroy(vmw_encoder_to_sou(encoder)); 361 } 362 363 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = { 364 .destroy = vmw_sou_encoder_destroy, 365 }; 366 367 /* 368 * Screen Object Display Unit connector functions 369 */ 370 371 static void vmw_sou_connector_destroy(struct drm_connector *connector) 372 { 373 vmw_sou_destroy(vmw_connector_to_sou(connector)); 374 } 375 376 static const struct drm_connector_funcs vmw_sou_connector_funcs = { 377 .dpms = vmw_du_connector_dpms, 378 .detect = vmw_du_connector_detect, 379 .fill_modes = vmw_du_connector_fill_modes, 380 .set_property = vmw_du_connector_set_property, 381 .destroy = vmw_sou_connector_destroy, 382 .reset = vmw_du_connector_reset, 383 .atomic_duplicate_state = vmw_du_connector_duplicate_state, 384 .atomic_destroy_state = vmw_du_connector_destroy_state, 385 .atomic_set_property = vmw_du_connector_atomic_set_property, 386 .atomic_get_property = vmw_du_connector_atomic_get_property, 387 }; 388 389 390 static const struct 391 drm_connector_helper_funcs vmw_sou_connector_helper_funcs = { 392 }; 393 394 395 396 /* 397 * Screen Object Display Plane Functions 398 */ 399 400 /** 401 * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer 402 * 403 * @plane: display plane 404 * @old_state: Contains the FB to clean up 405 * 406 * Unpins the display surface 407 * 408 * Returns 0 on success 409 */ 410 static void 411 vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane, 412 struct drm_plane_state *old_state) 413 { 414 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state); 415 struct drm_crtc *crtc = plane->state->crtc ? 416 plane->state->crtc : old_state->crtc; 417 418 if (vps->bo) 419 vmw_bo_unpin(vmw_priv(crtc->dev), vps->bo, false); 420 vmw_bo_unreference(&vps->bo); 421 vps->bo_size = 0; 422 423 vmw_du_plane_cleanup_fb(plane, old_state); 424 } 425 426 427 /** 428 * vmw_sou_primary_plane_prepare_fb - allocate backing buffer 429 * 430 * @plane: display plane 431 * @new_state: info on the new plane state, including the FB 432 * 433 * The SOU backing buffer is our equivalent of the display plane. 434 * 435 * Returns 0 on success 436 */ 437 static int 438 vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane, 439 struct drm_plane_state *new_state) 440 { 441 struct drm_framebuffer *new_fb = new_state->fb; 442 struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc; 443 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state); 444 struct vmw_private *dev_priv; 445 size_t size; 446 int ret; 447 448 449 if (!new_fb) { 450 vmw_bo_unreference(&vps->bo); 451 vps->bo_size = 0; 452 453 return 0; 454 } 455 456 size = new_state->crtc_w * new_state->crtc_h * 4; 457 dev_priv = vmw_priv(crtc->dev); 458 459 if (vps->bo) { 460 if (vps->bo_size == size) { 461 /* 462 * Note that this might temporarily up the pin-count 463 * to 2, until cleanup_fb() is called. 464 */ 465 return vmw_bo_pin_in_vram(dev_priv, vps->bo, 466 true); 467 } 468 469 vmw_bo_unreference(&vps->bo); 470 vps->bo_size = 0; 471 } 472 473 vps->bo = kzalloc(sizeof(*vps->bo), GFP_KERNEL); 474 if (!vps->bo) 475 return -ENOMEM; 476 477 vmw_svga_enable(dev_priv); 478 479 /* After we have alloced the backing store might not be able to 480 * resume the overlays, this is preferred to failing to alloc. 481 */ 482 vmw_overlay_pause_all(dev_priv); 483 ret = vmw_bo_init(dev_priv, vps->bo, size, 484 &vmw_vram_ne_placement, 485 false, &vmw_bo_bo_free); 486 vmw_overlay_resume_all(dev_priv); 487 if (ret) { 488 vps->bo = NULL; /* vmw_bo_init frees on error */ 489 return ret; 490 } 491 492 vps->bo_size = size; 493 494 /* 495 * TTM already thinks the buffer is pinned, but make sure the 496 * pin_count is upped. 497 */ 498 return vmw_bo_pin_in_vram(dev_priv, vps->bo, true); 499 } 500 501 502 static void 503 vmw_sou_primary_plane_atomic_update(struct drm_plane *plane, 504 struct drm_plane_state *old_state) 505 { 506 struct drm_crtc *crtc = plane->state->crtc; 507 struct drm_pending_vblank_event *event = NULL; 508 struct vmw_fence_obj *fence = NULL; 509 int ret; 510 511 if (crtc && plane->state->fb) { 512 struct vmw_private *dev_priv = vmw_priv(crtc->dev); 513 struct vmw_framebuffer *vfb = 514 vmw_framebuffer_to_vfb(plane->state->fb); 515 struct drm_vmw_rect vclips; 516 517 vclips.x = crtc->x; 518 vclips.y = crtc->y; 519 vclips.w = crtc->mode.hdisplay; 520 vclips.h = crtc->mode.vdisplay; 521 522 if (vfb->bo) 523 ret = vmw_kms_sou_do_bo_dirty(dev_priv, vfb, NULL, 524 &vclips, 1, 1, true, 525 &fence, crtc); 526 else 527 ret = vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, 528 &vclips, NULL, 0, 0, 529 1, 1, &fence, crtc); 530 531 /* 532 * We cannot really fail this function, so if we do, then output 533 * an error and maintain consistent atomic state. 534 */ 535 if (ret != 0) 536 DRM_ERROR("Failed to update screen.\n"); 537 } else { 538 /* 539 * When disabling a plane, CRTC and FB should always be NULL 540 * together, otherwise it's an error. 541 * Here primary plane is being disable so should really blank 542 * the screen object display unit, if not already done. 543 */ 544 return; 545 } 546 547 event = crtc->state->event; 548 /* 549 * In case of failure and other cases, vblank event will be sent in 550 * vmw_du_crtc_atomic_flush. 551 */ 552 if (event && fence) { 553 struct drm_file *file_priv = event->base.file_priv; 554 555 ret = vmw_event_fence_action_queue(file_priv, 556 fence, 557 &event->base, 558 &event->event.vbl.tv_sec, 559 &event->event.vbl.tv_usec, 560 true); 561 562 if (unlikely(ret != 0)) 563 DRM_ERROR("Failed to queue event on fence.\n"); 564 else 565 crtc->state->event = NULL; 566 } 567 568 if (fence) 569 vmw_fence_obj_unreference(&fence); 570 } 571 572 573 static const struct drm_plane_funcs vmw_sou_plane_funcs = { 574 .update_plane = drm_atomic_helper_update_plane, 575 .disable_plane = drm_atomic_helper_disable_plane, 576 .destroy = vmw_du_primary_plane_destroy, 577 .reset = vmw_du_plane_reset, 578 .atomic_duplicate_state = vmw_du_plane_duplicate_state, 579 .atomic_destroy_state = vmw_du_plane_destroy_state, 580 }; 581 582 static const struct drm_plane_funcs vmw_sou_cursor_funcs = { 583 .update_plane = drm_atomic_helper_update_plane, 584 .disable_plane = drm_atomic_helper_disable_plane, 585 .destroy = vmw_du_cursor_plane_destroy, 586 .reset = vmw_du_plane_reset, 587 .atomic_duplicate_state = vmw_du_plane_duplicate_state, 588 .atomic_destroy_state = vmw_du_plane_destroy_state, 589 }; 590 591 /* 592 * Atomic Helpers 593 */ 594 static const struct 595 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = { 596 .atomic_check = vmw_du_cursor_plane_atomic_check, 597 .atomic_update = vmw_du_cursor_plane_atomic_update, 598 .prepare_fb = vmw_du_cursor_plane_prepare_fb, 599 .cleanup_fb = vmw_du_plane_cleanup_fb, 600 }; 601 602 static const struct 603 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = { 604 .atomic_check = vmw_du_primary_plane_atomic_check, 605 .atomic_update = vmw_sou_primary_plane_atomic_update, 606 .prepare_fb = vmw_sou_primary_plane_prepare_fb, 607 .cleanup_fb = vmw_sou_primary_plane_cleanup_fb, 608 }; 609 610 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = { 611 .prepare = vmw_sou_crtc_helper_prepare, 612 .mode_set_nofb = vmw_sou_crtc_mode_set_nofb, 613 .atomic_check = vmw_du_crtc_atomic_check, 614 .atomic_begin = vmw_du_crtc_atomic_begin, 615 .atomic_flush = vmw_du_crtc_atomic_flush, 616 .atomic_enable = vmw_sou_crtc_atomic_enable, 617 .atomic_disable = vmw_sou_crtc_atomic_disable, 618 }; 619 620 621 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit) 622 { 623 struct vmw_screen_object_unit *sou; 624 struct drm_device *dev = dev_priv->dev; 625 struct drm_connector *connector; 626 struct drm_encoder *encoder; 627 struct drm_plane *primary, *cursor; 628 struct drm_crtc *crtc; 629 int ret; 630 631 sou = kzalloc(sizeof(*sou), GFP_KERNEL); 632 if (!sou) 633 return -ENOMEM; 634 635 sou->base.unit = unit; 636 crtc = &sou->base.crtc; 637 encoder = &sou->base.encoder; 638 connector = &sou->base.connector; 639 primary = &sou->base.primary; 640 cursor = &sou->base.cursor; 641 642 sou->base.active_implicit = false; 643 sou->base.pref_active = (unit == 0); 644 sou->base.pref_width = dev_priv->initial_width; 645 sou->base.pref_height = dev_priv->initial_height; 646 sou->base.pref_mode = NULL; 647 648 /* 649 * Remove this after enabling atomic because property values can 650 * only exist in a state object 651 */ 652 sou->base.is_implicit = false; 653 654 /* Initialize primary plane */ 655 vmw_du_plane_reset(primary); 656 657 ret = drm_universal_plane_init(dev, &sou->base.primary, 658 0, &vmw_sou_plane_funcs, 659 vmw_primary_plane_formats, 660 ARRAY_SIZE(vmw_primary_plane_formats), 661 NULL, DRM_PLANE_TYPE_PRIMARY, NULL); 662 if (ret) { 663 DRM_ERROR("Failed to initialize primary plane"); 664 goto err_free; 665 } 666 667 drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs); 668 669 /* Initialize cursor plane */ 670 vmw_du_plane_reset(cursor); 671 672 ret = drm_universal_plane_init(dev, &sou->base.cursor, 673 0, &vmw_sou_cursor_funcs, 674 vmw_cursor_plane_formats, 675 ARRAY_SIZE(vmw_cursor_plane_formats), 676 NULL, DRM_PLANE_TYPE_CURSOR, NULL); 677 if (ret) { 678 DRM_ERROR("Failed to initialize cursor plane"); 679 drm_plane_cleanup(&sou->base.primary); 680 goto err_free; 681 } 682 683 drm_plane_helper_add(cursor, &vmw_sou_cursor_plane_helper_funcs); 684 685 vmw_du_connector_reset(connector); 686 ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs, 687 DRM_MODE_CONNECTOR_VIRTUAL); 688 if (ret) { 689 DRM_ERROR("Failed to initialize connector\n"); 690 goto err_free; 691 } 692 693 drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs); 694 connector->status = vmw_du_connector_detect(connector, true); 695 vmw_connector_state_to_vcs(connector->state)->is_implicit = false; 696 697 698 ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs, 699 DRM_MODE_ENCODER_VIRTUAL, NULL); 700 if (ret) { 701 DRM_ERROR("Failed to initialize encoder\n"); 702 goto err_free_connector; 703 } 704 705 (void) drm_connector_attach_encoder(connector, encoder); 706 encoder->possible_crtcs = (1 << unit); 707 encoder->possible_clones = 0; 708 709 ret = drm_connector_register(connector); 710 if (ret) { 711 DRM_ERROR("Failed to register connector\n"); 712 goto err_free_encoder; 713 } 714 715 716 vmw_du_crtc_reset(crtc); 717 ret = drm_crtc_init_with_planes(dev, crtc, &sou->base.primary, 718 &sou->base.cursor, 719 &vmw_screen_object_crtc_funcs, NULL); 720 if (ret) { 721 DRM_ERROR("Failed to initialize CRTC\n"); 722 goto err_free_unregister; 723 } 724 725 drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs); 726 727 drm_mode_crtc_set_gamma_size(crtc, 256); 728 729 drm_object_attach_property(&connector->base, 730 dev_priv->hotplug_mode_update_property, 1); 731 drm_object_attach_property(&connector->base, 732 dev->mode_config.suggested_x_property, 0); 733 drm_object_attach_property(&connector->base, 734 dev->mode_config.suggested_y_property, 0); 735 if (dev_priv->implicit_placement_property) 736 drm_object_attach_property 737 (&connector->base, 738 dev_priv->implicit_placement_property, 739 sou->base.is_implicit); 740 741 return 0; 742 743 err_free_unregister: 744 drm_connector_unregister(connector); 745 err_free_encoder: 746 drm_encoder_cleanup(encoder); 747 err_free_connector: 748 drm_connector_cleanup(connector); 749 err_free: 750 kfree(sou); 751 return ret; 752 } 753 754 int vmw_kms_sou_init_display(struct vmw_private *dev_priv) 755 { 756 struct drm_device *dev = dev_priv->dev; 757 int i, ret; 758 759 if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) { 760 DRM_INFO("Not using screen objects," 761 " missing cap SCREEN_OBJECT_2\n"); 762 return -ENOSYS; 763 } 764 765 ret = -ENOMEM; 766 dev_priv->num_implicit = 0; 767 dev_priv->implicit_fb = NULL; 768 769 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS); 770 if (unlikely(ret != 0)) 771 return ret; 772 773 vmw_kms_create_implicit_placement_property(dev_priv, false); 774 775 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) 776 vmw_sou_init(dev_priv, i); 777 778 dev_priv->active_display_unit = vmw_du_screen_object; 779 780 DRM_INFO("Screen Objects Display Unit initialized\n"); 781 782 return 0; 783 } 784 785 static int do_bo_define_gmrfb(struct vmw_private *dev_priv, 786 struct vmw_framebuffer *framebuffer) 787 { 788 struct vmw_buffer_object *buf = 789 container_of(framebuffer, struct vmw_framebuffer_bo, 790 base)->buffer; 791 int depth = framebuffer->base.format->depth; 792 struct { 793 uint32_t header; 794 SVGAFifoCmdDefineGMRFB body; 795 } *cmd; 796 797 /* Emulate RGBA support, contrary to svga_reg.h this is not 798 * supported by hosts. This is only a problem if we are reading 799 * this value later and expecting what we uploaded back. 800 */ 801 if (depth == 32) 802 depth = 24; 803 804 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); 805 if (!cmd) { 806 DRM_ERROR("Out of fifo space for dirty framebuffer command.\n"); 807 return -ENOMEM; 808 } 809 810 cmd->header = SVGA_CMD_DEFINE_GMRFB; 811 cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8; 812 cmd->body.format.colorDepth = depth; 813 cmd->body.format.reserved = 0; 814 cmd->body.bytesPerLine = framebuffer->base.pitches[0]; 815 /* Buffer is reserved in vram or GMR */ 816 vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr); 817 vmw_fifo_commit(dev_priv, sizeof(*cmd)); 818 819 return 0; 820 } 821 822 /** 823 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a 824 * blit surface to screen command. 825 * 826 * @dirty: The closure structure. 827 * 828 * Fills in the missing fields in the command, and translates the cliprects 829 * to match the destination bounding box encoded. 830 */ 831 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty) 832 { 833 struct vmw_kms_sou_surface_dirty *sdirty = 834 container_of(dirty, typeof(*sdirty), base); 835 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd; 836 s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x; 837 s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y; 838 size_t region_size = dirty->num_hits * sizeof(SVGASignedRect); 839 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1]; 840 int i; 841 842 if (!dirty->num_hits) { 843 vmw_fifo_commit(dirty->dev_priv, 0); 844 return; 845 } 846 847 cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN; 848 cmd->header.size = sizeof(cmd->body) + region_size; 849 850 /* 851 * Use the destination bounding box to specify destination - and 852 * source bounding regions. 853 */ 854 cmd->body.destRect.left = sdirty->left; 855 cmd->body.destRect.right = sdirty->right; 856 cmd->body.destRect.top = sdirty->top; 857 cmd->body.destRect.bottom = sdirty->bottom; 858 859 cmd->body.srcRect.left = sdirty->left + trans_x; 860 cmd->body.srcRect.right = sdirty->right + trans_x; 861 cmd->body.srcRect.top = sdirty->top + trans_y; 862 cmd->body.srcRect.bottom = sdirty->bottom + trans_y; 863 864 cmd->body.srcImage.sid = sdirty->sid; 865 cmd->body.destScreenId = dirty->unit->unit; 866 867 /* Blits are relative to the destination rect. Translate. */ 868 for (i = 0; i < dirty->num_hits; ++i, ++blit) { 869 blit->left -= sdirty->left; 870 blit->right -= sdirty->left; 871 blit->top -= sdirty->top; 872 blit->bottom -= sdirty->top; 873 } 874 875 vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd)); 876 877 sdirty->left = sdirty->top = S32_MAX; 878 sdirty->right = sdirty->bottom = S32_MIN; 879 } 880 881 /** 882 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect. 883 * 884 * @dirty: The closure structure 885 * 886 * Encodes a SVGASignedRect cliprect and updates the bounding box of the 887 * BLIT_SURFACE_TO_SCREEN command. 888 */ 889 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty) 890 { 891 struct vmw_kms_sou_surface_dirty *sdirty = 892 container_of(dirty, typeof(*sdirty), base); 893 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd; 894 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1]; 895 896 /* Destination rect. */ 897 blit += dirty->num_hits; 898 blit->left = dirty->unit_x1; 899 blit->top = dirty->unit_y1; 900 blit->right = dirty->unit_x2; 901 blit->bottom = dirty->unit_y2; 902 903 /* Destination bounding box */ 904 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1); 905 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1); 906 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2); 907 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2); 908 909 dirty->num_hits++; 910 } 911 912 /** 913 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer 914 * 915 * @dev_priv: Pointer to the device private structure. 916 * @framebuffer: Pointer to the surface-buffer backed framebuffer. 917 * @clips: Array of clip rects. Either @clips or @vclips must be NULL. 918 * @vclips: Alternate array of clip rects. Either @clips or @vclips must 919 * be NULL. 920 * @srf: Pointer to surface to blit from. If NULL, the surface attached 921 * to @framebuffer will be used. 922 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates. 923 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates. 924 * @num_clips: Number of clip rects in @clips. 925 * @inc: Increment to use when looping over @clips. 926 * @out_fence: If non-NULL, will return a ref-counted pointer to a 927 * struct vmw_fence_obj. The returned fence pointer may be NULL in which 928 * case the device has already synchronized. 929 * @crtc: If crtc is passed, perform surface dirty on that crtc only. 930 * 931 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if 932 * interrupted. 933 */ 934 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv, 935 struct vmw_framebuffer *framebuffer, 936 struct drm_clip_rect *clips, 937 struct drm_vmw_rect *vclips, 938 struct vmw_resource *srf, 939 s32 dest_x, 940 s32 dest_y, 941 unsigned num_clips, int inc, 942 struct vmw_fence_obj **out_fence, 943 struct drm_crtc *crtc) 944 { 945 struct vmw_framebuffer_surface *vfbs = 946 container_of(framebuffer, typeof(*vfbs), base); 947 struct vmw_kms_sou_surface_dirty sdirty; 948 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0); 949 int ret; 950 951 if (!srf) 952 srf = &vfbs->surface->res; 953 954 ret = vmw_validation_add_resource(&val_ctx, srf, 0, NULL, NULL); 955 if (ret) 956 return ret; 957 958 ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true); 959 if (ret) 960 goto out_unref; 961 962 sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit; 963 sdirty.base.clip = vmw_sou_surface_clip; 964 sdirty.base.dev_priv = dev_priv; 965 sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) + 966 sizeof(SVGASignedRect) * num_clips; 967 sdirty.base.crtc = crtc; 968 969 sdirty.sid = srf->id; 970 sdirty.left = sdirty.top = S32_MAX; 971 sdirty.right = sdirty.bottom = S32_MIN; 972 sdirty.dst_x = dest_x; 973 sdirty.dst_y = dest_y; 974 975 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips, 976 dest_x, dest_y, num_clips, inc, 977 &sdirty.base); 978 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence, 979 NULL); 980 981 return ret; 982 983 out_unref: 984 vmw_validation_unref_lists(&val_ctx); 985 return ret; 986 } 987 988 /** 989 * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips. 990 * 991 * @dirty: The closure structure. 992 * 993 * Commits a previously built command buffer of readback clips. 994 */ 995 static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty) 996 { 997 if (!dirty->num_hits) { 998 vmw_fifo_commit(dirty->dev_priv, 0); 999 return; 1000 } 1001 1002 vmw_fifo_commit(dirty->dev_priv, 1003 sizeof(struct vmw_kms_sou_bo_blit) * 1004 dirty->num_hits); 1005 } 1006 1007 /** 1008 * vmw_sou_bo_clip - Callback to encode a readback cliprect. 1009 * 1010 * @dirty: The closure structure 1011 * 1012 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect. 1013 */ 1014 static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty) 1015 { 1016 struct vmw_kms_sou_bo_blit *blit = dirty->cmd; 1017 1018 blit += dirty->num_hits; 1019 blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN; 1020 blit->body.destScreenId = dirty->unit->unit; 1021 blit->body.srcOrigin.x = dirty->fb_x; 1022 blit->body.srcOrigin.y = dirty->fb_y; 1023 blit->body.destRect.left = dirty->unit_x1; 1024 blit->body.destRect.top = dirty->unit_y1; 1025 blit->body.destRect.right = dirty->unit_x2; 1026 blit->body.destRect.bottom = dirty->unit_y2; 1027 dirty->num_hits++; 1028 } 1029 1030 /** 1031 * vmw_kms_do_bo_dirty - Dirty part of a buffer-object backed framebuffer 1032 * 1033 * @dev_priv: Pointer to the device private structure. 1034 * @framebuffer: Pointer to the buffer-object backed framebuffer. 1035 * @clips: Array of clip rects. 1036 * @vclips: Alternate array of clip rects. Either @clips or @vclips must 1037 * be NULL. 1038 * @num_clips: Number of clip rects in @clips. 1039 * @increment: Increment to use when looping over @clips. 1040 * @interruptible: Whether to perform waits interruptible if possible. 1041 * @out_fence: If non-NULL, will return a ref-counted pointer to a 1042 * struct vmw_fence_obj. The returned fence pointer may be NULL in which 1043 * case the device has already synchronized. 1044 * @crtc: If crtc is passed, perform bo dirty on that crtc only. 1045 * 1046 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if 1047 * interrupted. 1048 */ 1049 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv, 1050 struct vmw_framebuffer *framebuffer, 1051 struct drm_clip_rect *clips, 1052 struct drm_vmw_rect *vclips, 1053 unsigned num_clips, int increment, 1054 bool interruptible, 1055 struct vmw_fence_obj **out_fence, 1056 struct drm_crtc *crtc) 1057 { 1058 struct vmw_buffer_object *buf = 1059 container_of(framebuffer, struct vmw_framebuffer_bo, 1060 base)->buffer; 1061 struct vmw_kms_dirty dirty; 1062 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0); 1063 int ret; 1064 1065 ret = vmw_validation_add_bo(&val_ctx, buf, false, false); 1066 if (ret) 1067 return ret; 1068 1069 ret = vmw_validation_prepare(&val_ctx, NULL, interruptible); 1070 if (ret) 1071 goto out_unref; 1072 1073 ret = do_bo_define_gmrfb(dev_priv, framebuffer); 1074 if (unlikely(ret != 0)) 1075 goto out_revert; 1076 1077 dirty.crtc = crtc; 1078 dirty.fifo_commit = vmw_sou_bo_fifo_commit; 1079 dirty.clip = vmw_sou_bo_clip; 1080 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_bo_blit) * 1081 num_clips; 1082 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips, 1083 0, 0, num_clips, increment, &dirty); 1084 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence, 1085 NULL); 1086 1087 return ret; 1088 1089 out_revert: 1090 vmw_validation_revert(&val_ctx); 1091 out_unref: 1092 vmw_validation_unref_lists(&val_ctx); 1093 1094 return ret; 1095 } 1096 1097 1098 /** 1099 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips. 1100 * 1101 * @dirty: The closure structure. 1102 * 1103 * Commits a previously built command buffer of readback clips. 1104 */ 1105 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty) 1106 { 1107 if (!dirty->num_hits) { 1108 vmw_fifo_commit(dirty->dev_priv, 0); 1109 return; 1110 } 1111 1112 vmw_fifo_commit(dirty->dev_priv, 1113 sizeof(struct vmw_kms_sou_readback_blit) * 1114 dirty->num_hits); 1115 } 1116 1117 /** 1118 * vmw_sou_readback_clip - Callback to encode a readback cliprect. 1119 * 1120 * @dirty: The closure structure 1121 * 1122 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect. 1123 */ 1124 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty) 1125 { 1126 struct vmw_kms_sou_readback_blit *blit = dirty->cmd; 1127 1128 blit += dirty->num_hits; 1129 blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB; 1130 blit->body.srcScreenId = dirty->unit->unit; 1131 blit->body.destOrigin.x = dirty->fb_x; 1132 blit->body.destOrigin.y = dirty->fb_y; 1133 blit->body.srcRect.left = dirty->unit_x1; 1134 blit->body.srcRect.top = dirty->unit_y1; 1135 blit->body.srcRect.right = dirty->unit_x2; 1136 blit->body.srcRect.bottom = dirty->unit_y2; 1137 dirty->num_hits++; 1138 } 1139 1140 /** 1141 * vmw_kms_sou_readback - Perform a readback from the screen object system to 1142 * a buffer-object backed framebuffer. 1143 * 1144 * @dev_priv: Pointer to the device private structure. 1145 * @file_priv: Pointer to a struct drm_file identifying the caller. 1146 * Must be set to NULL if @user_fence_rep is NULL. 1147 * @vfb: Pointer to the buffer-object backed framebuffer. 1148 * @user_fence_rep: User-space provided structure for fence information. 1149 * Must be set to non-NULL if @file_priv is non-NULL. 1150 * @vclips: Array of clip rects. 1151 * @num_clips: Number of clip rects in @vclips. 1152 * @crtc: If crtc is passed, readback on that crtc only. 1153 * 1154 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if 1155 * interrupted. 1156 */ 1157 int vmw_kms_sou_readback(struct vmw_private *dev_priv, 1158 struct drm_file *file_priv, 1159 struct vmw_framebuffer *vfb, 1160 struct drm_vmw_fence_rep __user *user_fence_rep, 1161 struct drm_vmw_rect *vclips, 1162 uint32_t num_clips, 1163 struct drm_crtc *crtc) 1164 { 1165 struct vmw_buffer_object *buf = 1166 container_of(vfb, struct vmw_framebuffer_bo, base)->buffer; 1167 struct vmw_kms_dirty dirty; 1168 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0); 1169 int ret; 1170 1171 ret = vmw_validation_add_bo(&val_ctx, buf, false, false); 1172 if (ret) 1173 return ret; 1174 1175 ret = vmw_validation_prepare(&val_ctx, NULL, true); 1176 if (ret) 1177 goto out_unref; 1178 1179 ret = do_bo_define_gmrfb(dev_priv, vfb); 1180 if (unlikely(ret != 0)) 1181 goto out_revert; 1182 1183 dirty.crtc = crtc; 1184 dirty.fifo_commit = vmw_sou_readback_fifo_commit; 1185 dirty.clip = vmw_sou_readback_clip; 1186 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) * 1187 num_clips; 1188 ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips, 1189 0, 0, num_clips, 1, &dirty); 1190 vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL, 1191 user_fence_rep); 1192 1193 return ret; 1194 1195 out_revert: 1196 vmw_validation_revert(&val_ctx); 1197 out_unref: 1198 vmw_validation_unref_lists(&val_ctx); 1199 1200 return ret; 1201 } 1202