1 /************************************************************************** 2 * 3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA 4 * All Rights Reserved. 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 #include <drm/drm_rect.h> 33 34 35 /* Might need a hrtimer here? */ 36 #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1) 37 38 void vmw_du_cleanup(struct vmw_display_unit *du) 39 { 40 drm_plane_cleanup(&du->primary); 41 drm_plane_cleanup(&du->cursor); 42 43 drm_connector_unregister(&du->connector); 44 drm_crtc_cleanup(&du->crtc); 45 drm_encoder_cleanup(&du->encoder); 46 drm_connector_cleanup(&du->connector); 47 } 48 49 /* 50 * Display Unit Cursor functions 51 */ 52 53 static int vmw_cursor_update_image(struct vmw_private *dev_priv, 54 u32 *image, u32 width, u32 height, 55 u32 hotspotX, u32 hotspotY) 56 { 57 struct { 58 u32 cmd; 59 SVGAFifoCmdDefineAlphaCursor cursor; 60 } *cmd; 61 u32 image_size = width * height * 4; 62 u32 cmd_size = sizeof(*cmd) + image_size; 63 64 if (!image) 65 return -EINVAL; 66 67 cmd = vmw_fifo_reserve(dev_priv, cmd_size); 68 if (unlikely(cmd == NULL)) { 69 DRM_ERROR("Fifo reserve failed.\n"); 70 return -ENOMEM; 71 } 72 73 memset(cmd, 0, sizeof(*cmd)); 74 75 memcpy(&cmd[1], image, image_size); 76 77 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR; 78 cmd->cursor.id = 0; 79 cmd->cursor.width = width; 80 cmd->cursor.height = height; 81 cmd->cursor.hotspotX = hotspotX; 82 cmd->cursor.hotspotY = hotspotY; 83 84 vmw_fifo_commit_flush(dev_priv, cmd_size); 85 86 return 0; 87 } 88 89 static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv, 90 struct vmw_dma_buffer *dmabuf, 91 u32 width, u32 height, 92 u32 hotspotX, u32 hotspotY) 93 { 94 struct ttm_bo_kmap_obj map; 95 unsigned long kmap_offset; 96 unsigned long kmap_num; 97 void *virtual; 98 bool dummy; 99 int ret; 100 101 kmap_offset = 0; 102 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT; 103 104 ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL); 105 if (unlikely(ret != 0)) { 106 DRM_ERROR("reserve failed\n"); 107 return -EINVAL; 108 } 109 110 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map); 111 if (unlikely(ret != 0)) 112 goto err_unreserve; 113 114 virtual = ttm_kmap_obj_virtual(&map, &dummy); 115 ret = vmw_cursor_update_image(dev_priv, virtual, width, height, 116 hotspotX, hotspotY); 117 118 ttm_bo_kunmap(&map); 119 err_unreserve: 120 ttm_bo_unreserve(&dmabuf->base); 121 122 return ret; 123 } 124 125 126 static void vmw_cursor_update_position(struct vmw_private *dev_priv, 127 bool show, int x, int y) 128 { 129 u32 *fifo_mem = dev_priv->mmio_virt; 130 uint32_t count; 131 132 spin_lock(&dev_priv->cursor_lock); 133 vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON); 134 vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X); 135 vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y); 136 count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT); 137 vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT); 138 spin_unlock(&dev_priv->cursor_lock); 139 } 140 141 142 void vmw_kms_cursor_snoop(struct vmw_surface *srf, 143 struct ttm_object_file *tfile, 144 struct ttm_buffer_object *bo, 145 SVGA3dCmdHeader *header) 146 { 147 struct ttm_bo_kmap_obj map; 148 unsigned long kmap_offset; 149 unsigned long kmap_num; 150 SVGA3dCopyBox *box; 151 unsigned box_count; 152 void *virtual; 153 bool dummy; 154 struct vmw_dma_cmd { 155 SVGA3dCmdHeader header; 156 SVGA3dCmdSurfaceDMA dma; 157 } *cmd; 158 int i, ret; 159 160 cmd = container_of(header, struct vmw_dma_cmd, header); 161 162 /* No snooper installed */ 163 if (!srf->snooper.image) 164 return; 165 166 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) { 167 DRM_ERROR("face and mipmap for cursors should never != 0\n"); 168 return; 169 } 170 171 if (cmd->header.size < 64) { 172 DRM_ERROR("at least one full copy box must be given\n"); 173 return; 174 } 175 176 box = (SVGA3dCopyBox *)&cmd[1]; 177 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) / 178 sizeof(SVGA3dCopyBox); 179 180 if (cmd->dma.guest.ptr.offset % PAGE_SIZE || 181 box->x != 0 || box->y != 0 || box->z != 0 || 182 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 || 183 box->d != 1 || box_count != 1) { 184 /* TODO handle none page aligned offsets */ 185 /* TODO handle more dst & src != 0 */ 186 /* TODO handle more then one copy */ 187 DRM_ERROR("Cant snoop dma request for cursor!\n"); 188 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n", 189 box->srcx, box->srcy, box->srcz, 190 box->x, box->y, box->z, 191 box->w, box->h, box->d, box_count, 192 cmd->dma.guest.ptr.offset); 193 return; 194 } 195 196 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT; 197 kmap_num = (64*64*4) >> PAGE_SHIFT; 198 199 ret = ttm_bo_reserve(bo, true, false, NULL); 200 if (unlikely(ret != 0)) { 201 DRM_ERROR("reserve failed\n"); 202 return; 203 } 204 205 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map); 206 if (unlikely(ret != 0)) 207 goto err_unreserve; 208 209 virtual = ttm_kmap_obj_virtual(&map, &dummy); 210 211 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) { 212 memcpy(srf->snooper.image, virtual, 64*64*4); 213 } else { 214 /* Image is unsigned pointer. */ 215 for (i = 0; i < box->h; i++) 216 memcpy(srf->snooper.image + i * 64, 217 virtual + i * cmd->dma.guest.pitch, 218 box->w * 4); 219 } 220 221 srf->snooper.age++; 222 223 ttm_bo_kunmap(&map); 224 err_unreserve: 225 ttm_bo_unreserve(bo); 226 } 227 228 /** 229 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots 230 * 231 * @dev_priv: Pointer to the device private struct. 232 * 233 * Clears all legacy hotspots. 234 */ 235 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv) 236 { 237 struct drm_device *dev = dev_priv->dev; 238 struct vmw_display_unit *du; 239 struct drm_crtc *crtc; 240 241 drm_modeset_lock_all(dev); 242 drm_for_each_crtc(crtc, dev) { 243 du = vmw_crtc_to_du(crtc); 244 245 du->hotspot_x = 0; 246 du->hotspot_y = 0; 247 } 248 drm_modeset_unlock_all(dev); 249 } 250 251 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv) 252 { 253 struct drm_device *dev = dev_priv->dev; 254 struct vmw_display_unit *du; 255 struct drm_crtc *crtc; 256 257 mutex_lock(&dev->mode_config.mutex); 258 259 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 260 du = vmw_crtc_to_du(crtc); 261 if (!du->cursor_surface || 262 du->cursor_age == du->cursor_surface->snooper.age) 263 continue; 264 265 du->cursor_age = du->cursor_surface->snooper.age; 266 vmw_cursor_update_image(dev_priv, 267 du->cursor_surface->snooper.image, 268 64, 64, 269 du->hotspot_x + du->core_hotspot_x, 270 du->hotspot_y + du->core_hotspot_y); 271 } 272 273 mutex_unlock(&dev->mode_config.mutex); 274 } 275 276 277 void vmw_du_cursor_plane_destroy(struct drm_plane *plane) 278 { 279 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0); 280 281 drm_plane_cleanup(plane); 282 } 283 284 285 void vmw_du_primary_plane_destroy(struct drm_plane *plane) 286 { 287 drm_plane_cleanup(plane); 288 289 /* Planes are static in our case so we don't free it */ 290 } 291 292 293 /** 294 * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface 295 * 296 * @vps: plane state associated with the display surface 297 * @unreference: true if we also want to unreference the display. 298 */ 299 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps, 300 bool unreference) 301 { 302 if (vps->surf) { 303 if (vps->pinned) { 304 vmw_resource_unpin(&vps->surf->res); 305 vps->pinned--; 306 } 307 308 if (unreference) { 309 if (vps->pinned) 310 DRM_ERROR("Surface still pinned\n"); 311 vmw_surface_unreference(&vps->surf); 312 } 313 } 314 } 315 316 317 /** 318 * vmw_du_plane_cleanup_fb - Unpins the cursor 319 * 320 * @plane: display plane 321 * @old_state: Contains the FB to clean up 322 * 323 * Unpins the framebuffer surface 324 * 325 * Returns 0 on success 326 */ 327 void 328 vmw_du_plane_cleanup_fb(struct drm_plane *plane, 329 struct drm_plane_state *old_state) 330 { 331 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state); 332 333 vmw_du_plane_unpin_surf(vps, false); 334 } 335 336 337 /** 338 * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it 339 * 340 * @plane: display plane 341 * @new_state: info on the new plane state, including the FB 342 * 343 * Returns 0 on success 344 */ 345 int 346 vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane, 347 struct drm_plane_state *new_state) 348 { 349 struct drm_framebuffer *fb = new_state->fb; 350 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state); 351 352 353 if (vps->surf) 354 vmw_surface_unreference(&vps->surf); 355 356 if (vps->dmabuf) 357 vmw_dmabuf_unreference(&vps->dmabuf); 358 359 if (fb) { 360 if (vmw_framebuffer_to_vfb(fb)->dmabuf) { 361 vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer; 362 vmw_dmabuf_reference(vps->dmabuf); 363 } else { 364 vps->surf = vmw_framebuffer_to_vfbs(fb)->surface; 365 vmw_surface_reference(vps->surf); 366 } 367 } 368 369 return 0; 370 } 371 372 373 void 374 vmw_du_cursor_plane_atomic_update(struct drm_plane *plane, 375 struct drm_plane_state *old_state) 376 { 377 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc; 378 struct vmw_private *dev_priv = vmw_priv(crtc->dev); 379 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 380 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state); 381 s32 hotspot_x, hotspot_y; 382 int ret = 0; 383 384 385 hotspot_x = du->hotspot_x; 386 hotspot_y = du->hotspot_y; 387 388 if (plane->fb) { 389 hotspot_x += plane->fb->hot_x; 390 hotspot_y += plane->fb->hot_y; 391 } 392 393 du->cursor_surface = vps->surf; 394 du->cursor_dmabuf = vps->dmabuf; 395 396 /* setup new image */ 397 if (vps->surf) { 398 du->cursor_age = du->cursor_surface->snooper.age; 399 400 ret = vmw_cursor_update_image(dev_priv, 401 vps->surf->snooper.image, 402 64, 64, hotspot_x, hotspot_y); 403 } else if (vps->dmabuf) { 404 ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf, 405 plane->state->crtc_w, 406 plane->state->crtc_h, 407 hotspot_x, hotspot_y); 408 } else { 409 vmw_cursor_update_position(dev_priv, false, 0, 0); 410 return; 411 } 412 413 if (!ret) { 414 du->cursor_x = plane->state->crtc_x + du->set_gui_x; 415 du->cursor_y = plane->state->crtc_y + du->set_gui_y; 416 417 vmw_cursor_update_position(dev_priv, true, 418 du->cursor_x + hotspot_x, 419 du->cursor_y + hotspot_y); 420 421 du->core_hotspot_x = hotspot_x - du->hotspot_x; 422 du->core_hotspot_y = hotspot_y - du->hotspot_y; 423 } else { 424 DRM_ERROR("Failed to update cursor image\n"); 425 } 426 } 427 428 429 /** 430 * vmw_du_primary_plane_atomic_check - check if the new state is okay 431 * 432 * @plane: display plane 433 * @state: info on the new plane state, including the FB 434 * 435 * Check if the new state is settable given the current state. Other 436 * than what the atomic helper checks, we care about crtc fitting 437 * the FB and maintaining one active framebuffer. 438 * 439 * Returns 0 on success 440 */ 441 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane, 442 struct drm_plane_state *state) 443 { 444 struct drm_crtc_state *crtc_state = NULL; 445 struct drm_framebuffer *new_fb = state->fb; 446 struct drm_rect clip = {}; 447 int ret; 448 449 if (state->crtc) 450 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc); 451 452 if (crtc_state && crtc_state->enable) { 453 clip.x2 = crtc_state->adjusted_mode.hdisplay; 454 clip.y2 = crtc_state->adjusted_mode.vdisplay; 455 } 456 457 ret = drm_atomic_helper_check_plane_state(state, crtc_state, &clip, 458 DRM_PLANE_HELPER_NO_SCALING, 459 DRM_PLANE_HELPER_NO_SCALING, 460 false, true); 461 462 if (!ret && new_fb) { 463 struct drm_crtc *crtc = state->crtc; 464 struct vmw_connector_state *vcs; 465 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 466 struct vmw_private *dev_priv = vmw_priv(crtc->dev); 467 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb); 468 469 vcs = vmw_connector_state_to_vcs(du->connector.state); 470 471 /* Only one active implicit framebuffer at a time. */ 472 mutex_lock(&dev_priv->global_kms_state_mutex); 473 if (vcs->is_implicit && dev_priv->implicit_fb && 474 !(dev_priv->num_implicit == 1 && du->active_implicit) 475 && dev_priv->implicit_fb != vfb) { 476 DRM_ERROR("Multiple implicit framebuffers " 477 "not supported.\n"); 478 ret = -EINVAL; 479 } 480 mutex_unlock(&dev_priv->global_kms_state_mutex); 481 } 482 483 484 return ret; 485 } 486 487 488 /** 489 * vmw_du_cursor_plane_atomic_check - check if the new state is okay 490 * 491 * @plane: cursor plane 492 * @state: info on the new plane state 493 * 494 * This is a chance to fail if the new cursor state does not fit 495 * our requirements. 496 * 497 * Returns 0 on success 498 */ 499 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane, 500 struct drm_plane_state *new_state) 501 { 502 int ret = 0; 503 struct vmw_surface *surface = NULL; 504 struct drm_framebuffer *fb = new_state->fb; 505 506 507 /* Turning off */ 508 if (!fb) 509 return ret; 510 511 /* A lot of the code assumes this */ 512 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) { 513 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n", 514 new_state->crtc_w, new_state->crtc_h); 515 ret = -EINVAL; 516 } 517 518 if (!vmw_framebuffer_to_vfb(fb)->dmabuf) 519 surface = vmw_framebuffer_to_vfbs(fb)->surface; 520 521 if (surface && !surface->snooper.image) { 522 DRM_ERROR("surface not suitable for cursor\n"); 523 ret = -EINVAL; 524 } 525 526 return ret; 527 } 528 529 530 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc, 531 struct drm_crtc_state *new_state) 532 { 533 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc); 534 int connector_mask = 1 << drm_connector_index(&du->connector); 535 bool has_primary = new_state->plane_mask & 536 BIT(drm_plane_index(crtc->primary)); 537 538 /* We always want to have an active plane with an active CRTC */ 539 if (has_primary != new_state->enable) 540 return -EINVAL; 541 542 543 if (new_state->connector_mask != connector_mask && 544 new_state->connector_mask != 0) { 545 DRM_ERROR("Invalid connectors configuration\n"); 546 return -EINVAL; 547 } 548 549 /* 550 * Our virtual device does not have a dot clock, so use the logical 551 * clock value as the dot clock. 552 */ 553 if (new_state->mode.crtc_clock == 0) 554 new_state->adjusted_mode.crtc_clock = new_state->mode.clock; 555 556 return 0; 557 } 558 559 560 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc, 561 struct drm_crtc_state *old_crtc_state) 562 { 563 } 564 565 566 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc, 567 struct drm_crtc_state *old_crtc_state) 568 { 569 struct drm_pending_vblank_event *event = crtc->state->event; 570 571 if (event) { 572 crtc->state->event = NULL; 573 574 spin_lock_irq(&crtc->dev->event_lock); 575 if (drm_crtc_vblank_get(crtc) == 0) 576 drm_crtc_arm_vblank_event(crtc, event); 577 else 578 drm_crtc_send_vblank_event(crtc, event); 579 spin_unlock_irq(&crtc->dev->event_lock); 580 } 581 582 } 583 584 585 /** 586 * vmw_du_crtc_duplicate_state - duplicate crtc state 587 * @crtc: DRM crtc 588 * 589 * Allocates and returns a copy of the crtc state (both common and 590 * vmw-specific) for the specified crtc. 591 * 592 * Returns: The newly allocated crtc state, or NULL on failure. 593 */ 594 struct drm_crtc_state * 595 vmw_du_crtc_duplicate_state(struct drm_crtc *crtc) 596 { 597 struct drm_crtc_state *state; 598 struct vmw_crtc_state *vcs; 599 600 if (WARN_ON(!crtc->state)) 601 return NULL; 602 603 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL); 604 605 if (!vcs) 606 return NULL; 607 608 state = &vcs->base; 609 610 __drm_atomic_helper_crtc_duplicate_state(crtc, state); 611 612 return state; 613 } 614 615 616 /** 617 * vmw_du_crtc_reset - creates a blank vmw crtc state 618 * @crtc: DRM crtc 619 * 620 * Resets the atomic state for @crtc by freeing the state pointer (which 621 * might be NULL, e.g. at driver load time) and allocating a new empty state 622 * object. 623 */ 624 void vmw_du_crtc_reset(struct drm_crtc *crtc) 625 { 626 struct vmw_crtc_state *vcs; 627 628 629 if (crtc->state) { 630 __drm_atomic_helper_crtc_destroy_state(crtc->state); 631 632 kfree(vmw_crtc_state_to_vcs(crtc->state)); 633 } 634 635 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL); 636 637 if (!vcs) { 638 DRM_ERROR("Cannot allocate vmw_crtc_state\n"); 639 return; 640 } 641 642 crtc->state = &vcs->base; 643 crtc->state->crtc = crtc; 644 } 645 646 647 /** 648 * vmw_du_crtc_destroy_state - destroy crtc state 649 * @crtc: DRM crtc 650 * @state: state object to destroy 651 * 652 * Destroys the crtc state (both common and vmw-specific) for the 653 * specified plane. 654 */ 655 void 656 vmw_du_crtc_destroy_state(struct drm_crtc *crtc, 657 struct drm_crtc_state *state) 658 { 659 drm_atomic_helper_crtc_destroy_state(crtc, state); 660 } 661 662 663 /** 664 * vmw_du_plane_duplicate_state - duplicate plane state 665 * @plane: drm plane 666 * 667 * Allocates and returns a copy of the plane state (both common and 668 * vmw-specific) for the specified plane. 669 * 670 * Returns: The newly allocated plane state, or NULL on failure. 671 */ 672 struct drm_plane_state * 673 vmw_du_plane_duplicate_state(struct drm_plane *plane) 674 { 675 struct drm_plane_state *state; 676 struct vmw_plane_state *vps; 677 678 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL); 679 680 if (!vps) 681 return NULL; 682 683 vps->pinned = 0; 684 685 /* Mapping is managed by prepare_fb/cleanup_fb */ 686 memset(&vps->guest_map, 0, sizeof(vps->guest_map)); 687 memset(&vps->host_map, 0, sizeof(vps->host_map)); 688 vps->cpp = 0; 689 690 /* Each ref counted resource needs to be acquired again */ 691 if (vps->surf) 692 (void) vmw_surface_reference(vps->surf); 693 694 if (vps->dmabuf) 695 (void) vmw_dmabuf_reference(vps->dmabuf); 696 697 state = &vps->base; 698 699 __drm_atomic_helper_plane_duplicate_state(plane, state); 700 701 return state; 702 } 703 704 705 /** 706 * vmw_du_plane_reset - creates a blank vmw plane state 707 * @plane: drm plane 708 * 709 * Resets the atomic state for @plane by freeing the state pointer (which might 710 * be NULL, e.g. at driver load time) and allocating a new empty state object. 711 */ 712 void vmw_du_plane_reset(struct drm_plane *plane) 713 { 714 struct vmw_plane_state *vps; 715 716 717 if (plane->state) 718 vmw_du_plane_destroy_state(plane, plane->state); 719 720 vps = kzalloc(sizeof(*vps), GFP_KERNEL); 721 722 if (!vps) { 723 DRM_ERROR("Cannot allocate vmw_plane_state\n"); 724 return; 725 } 726 727 plane->state = &vps->base; 728 plane->state->plane = plane; 729 plane->state->rotation = DRM_MODE_ROTATE_0; 730 } 731 732 733 /** 734 * vmw_du_plane_destroy_state - destroy plane state 735 * @plane: DRM plane 736 * @state: state object to destroy 737 * 738 * Destroys the plane state (both common and vmw-specific) for the 739 * specified plane. 740 */ 741 void 742 vmw_du_plane_destroy_state(struct drm_plane *plane, 743 struct drm_plane_state *state) 744 { 745 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state); 746 747 748 /* Should have been freed by cleanup_fb */ 749 if (vps->guest_map.virtual) { 750 DRM_ERROR("Guest mapping not freed\n"); 751 ttm_bo_kunmap(&vps->guest_map); 752 } 753 754 if (vps->host_map.virtual) { 755 DRM_ERROR("Host mapping not freed\n"); 756 ttm_bo_kunmap(&vps->host_map); 757 } 758 759 if (vps->surf) 760 vmw_surface_unreference(&vps->surf); 761 762 if (vps->dmabuf) 763 vmw_dmabuf_unreference(&vps->dmabuf); 764 765 drm_atomic_helper_plane_destroy_state(plane, state); 766 } 767 768 769 /** 770 * vmw_du_connector_duplicate_state - duplicate connector state 771 * @connector: DRM connector 772 * 773 * Allocates and returns a copy of the connector state (both common and 774 * vmw-specific) for the specified connector. 775 * 776 * Returns: The newly allocated connector state, or NULL on failure. 777 */ 778 struct drm_connector_state * 779 vmw_du_connector_duplicate_state(struct drm_connector *connector) 780 { 781 struct drm_connector_state *state; 782 struct vmw_connector_state *vcs; 783 784 if (WARN_ON(!connector->state)) 785 return NULL; 786 787 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL); 788 789 if (!vcs) 790 return NULL; 791 792 state = &vcs->base; 793 794 __drm_atomic_helper_connector_duplicate_state(connector, state); 795 796 return state; 797 } 798 799 800 /** 801 * vmw_du_connector_reset - creates a blank vmw connector state 802 * @connector: DRM connector 803 * 804 * Resets the atomic state for @connector by freeing the state pointer (which 805 * might be NULL, e.g. at driver load time) and allocating a new empty state 806 * object. 807 */ 808 void vmw_du_connector_reset(struct drm_connector *connector) 809 { 810 struct vmw_connector_state *vcs; 811 812 813 if (connector->state) { 814 __drm_atomic_helper_connector_destroy_state(connector->state); 815 816 kfree(vmw_connector_state_to_vcs(connector->state)); 817 } 818 819 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL); 820 821 if (!vcs) { 822 DRM_ERROR("Cannot allocate vmw_connector_state\n"); 823 return; 824 } 825 826 __drm_atomic_helper_connector_reset(connector, &vcs->base); 827 } 828 829 830 /** 831 * vmw_du_connector_destroy_state - destroy connector state 832 * @connector: DRM connector 833 * @state: state object to destroy 834 * 835 * Destroys the connector state (both common and vmw-specific) for the 836 * specified plane. 837 */ 838 void 839 vmw_du_connector_destroy_state(struct drm_connector *connector, 840 struct drm_connector_state *state) 841 { 842 drm_atomic_helper_connector_destroy_state(connector, state); 843 } 844 /* 845 * Generic framebuffer code 846 */ 847 848 /* 849 * Surface framebuffer code 850 */ 851 852 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer) 853 { 854 struct vmw_framebuffer_surface *vfbs = 855 vmw_framebuffer_to_vfbs(framebuffer); 856 857 drm_framebuffer_cleanup(framebuffer); 858 vmw_surface_unreference(&vfbs->surface); 859 if (vfbs->base.user_obj) 860 ttm_base_object_unref(&vfbs->base.user_obj); 861 862 kfree(vfbs); 863 } 864 865 static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer, 866 struct drm_file *file_priv, 867 unsigned flags, unsigned color, 868 struct drm_clip_rect *clips, 869 unsigned num_clips) 870 { 871 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev); 872 struct vmw_framebuffer_surface *vfbs = 873 vmw_framebuffer_to_vfbs(framebuffer); 874 struct drm_clip_rect norect; 875 int ret, inc = 1; 876 877 /* Legacy Display Unit does not support 3D */ 878 if (dev_priv->active_display_unit == vmw_du_legacy) 879 return -EINVAL; 880 881 drm_modeset_lock_all(dev_priv->dev); 882 883 ret = ttm_read_lock(&dev_priv->reservation_sem, true); 884 if (unlikely(ret != 0)) { 885 drm_modeset_unlock_all(dev_priv->dev); 886 return ret; 887 } 888 889 if (!num_clips) { 890 num_clips = 1; 891 clips = &norect; 892 norect.x1 = norect.y1 = 0; 893 norect.x2 = framebuffer->width; 894 norect.y2 = framebuffer->height; 895 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) { 896 num_clips /= 2; 897 inc = 2; /* skip source rects */ 898 } 899 900 if (dev_priv->active_display_unit == vmw_du_screen_object) 901 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base, 902 clips, NULL, NULL, 0, 0, 903 num_clips, inc, NULL); 904 else 905 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base, 906 clips, NULL, NULL, 0, 0, 907 num_clips, inc, NULL); 908 909 vmw_fifo_flush(dev_priv, false); 910 ttm_read_unlock(&dev_priv->reservation_sem); 911 912 drm_modeset_unlock_all(dev_priv->dev); 913 914 return 0; 915 } 916 917 /** 918 * vmw_kms_readback - Perform a readback from the screen system to 919 * a dma-buffer backed framebuffer. 920 * 921 * @dev_priv: Pointer to the device private structure. 922 * @file_priv: Pointer to a struct drm_file identifying the caller. 923 * Must be set to NULL if @user_fence_rep is NULL. 924 * @vfb: Pointer to the dma-buffer backed framebuffer. 925 * @user_fence_rep: User-space provided structure for fence information. 926 * Must be set to non-NULL if @file_priv is non-NULL. 927 * @vclips: Array of clip rects. 928 * @num_clips: Number of clip rects in @vclips. 929 * 930 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if 931 * interrupted. 932 */ 933 int vmw_kms_readback(struct vmw_private *dev_priv, 934 struct drm_file *file_priv, 935 struct vmw_framebuffer *vfb, 936 struct drm_vmw_fence_rep __user *user_fence_rep, 937 struct drm_vmw_rect *vclips, 938 uint32_t num_clips) 939 { 940 switch (dev_priv->active_display_unit) { 941 case vmw_du_screen_object: 942 return vmw_kms_sou_readback(dev_priv, file_priv, vfb, 943 user_fence_rep, vclips, num_clips); 944 case vmw_du_screen_target: 945 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb, 946 user_fence_rep, NULL, vclips, num_clips, 947 1, false, true); 948 default: 949 WARN_ONCE(true, 950 "Readback called with invalid display system.\n"); 951 } 952 953 return -ENOSYS; 954 } 955 956 957 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = { 958 .destroy = vmw_framebuffer_surface_destroy, 959 .dirty = vmw_framebuffer_surface_dirty, 960 }; 961 962 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv, 963 struct vmw_surface *surface, 964 struct vmw_framebuffer **out, 965 const struct drm_mode_fb_cmd2 966 *mode_cmd, 967 bool is_dmabuf_proxy) 968 969 { 970 struct drm_device *dev = dev_priv->dev; 971 struct vmw_framebuffer_surface *vfbs; 972 enum SVGA3dSurfaceFormat format; 973 int ret; 974 struct drm_format_name_buf format_name; 975 976 /* 3D is only supported on HWv8 and newer hosts */ 977 if (dev_priv->active_display_unit == vmw_du_legacy) 978 return -ENOSYS; 979 980 /* 981 * Sanity checks. 982 */ 983 984 /* Surface must be marked as a scanout. */ 985 if (unlikely(!surface->scanout)) 986 return -EINVAL; 987 988 if (unlikely(surface->mip_levels[0] != 1 || 989 surface->num_sizes != 1 || 990 surface->base_size.width < mode_cmd->width || 991 surface->base_size.height < mode_cmd->height || 992 surface->base_size.depth != 1)) { 993 DRM_ERROR("Incompatible surface dimensions " 994 "for requested mode.\n"); 995 return -EINVAL; 996 } 997 998 switch (mode_cmd->pixel_format) { 999 case DRM_FORMAT_ARGB8888: 1000 format = SVGA3D_A8R8G8B8; 1001 break; 1002 case DRM_FORMAT_XRGB8888: 1003 format = SVGA3D_X8R8G8B8; 1004 break; 1005 case DRM_FORMAT_RGB565: 1006 format = SVGA3D_R5G6B5; 1007 break; 1008 case DRM_FORMAT_XRGB1555: 1009 format = SVGA3D_A1R5G5B5; 1010 break; 1011 default: 1012 DRM_ERROR("Invalid pixel format: %s\n", 1013 drm_get_format_name(mode_cmd->pixel_format, &format_name)); 1014 return -EINVAL; 1015 } 1016 1017 /* 1018 * For DX, surface format validation is done when surface->scanout 1019 * is set. 1020 */ 1021 if (!dev_priv->has_dx && format != surface->format) { 1022 DRM_ERROR("Invalid surface format for requested mode.\n"); 1023 return -EINVAL; 1024 } 1025 1026 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL); 1027 if (!vfbs) { 1028 ret = -ENOMEM; 1029 goto out_err1; 1030 } 1031 1032 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd); 1033 vfbs->surface = vmw_surface_reference(surface); 1034 vfbs->base.user_handle = mode_cmd->handles[0]; 1035 vfbs->is_dmabuf_proxy = is_dmabuf_proxy; 1036 1037 *out = &vfbs->base; 1038 1039 ret = drm_framebuffer_init(dev, &vfbs->base.base, 1040 &vmw_framebuffer_surface_funcs); 1041 if (ret) 1042 goto out_err2; 1043 1044 return 0; 1045 1046 out_err2: 1047 vmw_surface_unreference(&surface); 1048 kfree(vfbs); 1049 out_err1: 1050 return ret; 1051 } 1052 1053 /* 1054 * Dmabuf framebuffer code 1055 */ 1056 1057 static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer) 1058 { 1059 struct vmw_framebuffer_dmabuf *vfbd = 1060 vmw_framebuffer_to_vfbd(framebuffer); 1061 1062 drm_framebuffer_cleanup(framebuffer); 1063 vmw_dmabuf_unreference(&vfbd->buffer); 1064 if (vfbd->base.user_obj) 1065 ttm_base_object_unref(&vfbd->base.user_obj); 1066 1067 kfree(vfbd); 1068 } 1069 1070 static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer, 1071 struct drm_file *file_priv, 1072 unsigned flags, unsigned color, 1073 struct drm_clip_rect *clips, 1074 unsigned num_clips) 1075 { 1076 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev); 1077 struct vmw_framebuffer_dmabuf *vfbd = 1078 vmw_framebuffer_to_vfbd(framebuffer); 1079 struct drm_clip_rect norect; 1080 int ret, increment = 1; 1081 1082 drm_modeset_lock_all(dev_priv->dev); 1083 1084 ret = ttm_read_lock(&dev_priv->reservation_sem, true); 1085 if (unlikely(ret != 0)) { 1086 drm_modeset_unlock_all(dev_priv->dev); 1087 return ret; 1088 } 1089 1090 if (!num_clips) { 1091 num_clips = 1; 1092 clips = &norect; 1093 norect.x1 = norect.y1 = 0; 1094 norect.x2 = framebuffer->width; 1095 norect.y2 = framebuffer->height; 1096 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) { 1097 num_clips /= 2; 1098 increment = 2; 1099 } 1100 1101 switch (dev_priv->active_display_unit) { 1102 case vmw_du_screen_target: 1103 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL, 1104 clips, NULL, num_clips, increment, 1105 true, true); 1106 break; 1107 case vmw_du_screen_object: 1108 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base, 1109 clips, NULL, num_clips, 1110 increment, true, NULL); 1111 break; 1112 case vmw_du_legacy: 1113 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0, 1114 clips, num_clips, increment); 1115 break; 1116 default: 1117 ret = -EINVAL; 1118 WARN_ONCE(true, "Dirty called with invalid display system.\n"); 1119 break; 1120 } 1121 1122 vmw_fifo_flush(dev_priv, false); 1123 ttm_read_unlock(&dev_priv->reservation_sem); 1124 1125 drm_modeset_unlock_all(dev_priv->dev); 1126 1127 return ret; 1128 } 1129 1130 static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = { 1131 .destroy = vmw_framebuffer_dmabuf_destroy, 1132 .dirty = vmw_framebuffer_dmabuf_dirty, 1133 }; 1134 1135 /** 1136 * Pin the dmabuffer to the start of vram. 1137 */ 1138 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb) 1139 { 1140 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev); 1141 struct vmw_dma_buffer *buf; 1142 int ret; 1143 1144 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer : 1145 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup; 1146 1147 if (!buf) 1148 return 0; 1149 1150 switch (dev_priv->active_display_unit) { 1151 case vmw_du_legacy: 1152 vmw_overlay_pause_all(dev_priv); 1153 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false); 1154 vmw_overlay_resume_all(dev_priv); 1155 break; 1156 case vmw_du_screen_object: 1157 case vmw_du_screen_target: 1158 if (vfb->dmabuf) 1159 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf, 1160 false); 1161 1162 return vmw_dmabuf_pin_in_placement(dev_priv, buf, 1163 &vmw_mob_placement, false); 1164 default: 1165 return -EINVAL; 1166 } 1167 1168 return ret; 1169 } 1170 1171 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb) 1172 { 1173 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev); 1174 struct vmw_dma_buffer *buf; 1175 1176 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer : 1177 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup; 1178 1179 if (WARN_ON(!buf)) 1180 return 0; 1181 1182 return vmw_dmabuf_unpin(dev_priv, buf, false); 1183 } 1184 1185 /** 1186 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf 1187 * 1188 * @dev: DRM device 1189 * @mode_cmd: parameters for the new surface 1190 * @dmabuf_mob: MOB backing the DMA buf 1191 * @srf_out: newly created surface 1192 * 1193 * When the content FB is a DMA buf, we create a surface as a proxy to the 1194 * same buffer. This way we can do a surface copy rather than a surface DMA. 1195 * This is a more efficient approach 1196 * 1197 * RETURNS: 1198 * 0 on success, error code otherwise 1199 */ 1200 static int vmw_create_dmabuf_proxy(struct drm_device *dev, 1201 const struct drm_mode_fb_cmd2 *mode_cmd, 1202 struct vmw_dma_buffer *dmabuf_mob, 1203 struct vmw_surface **srf_out) 1204 { 1205 uint32_t format; 1206 struct drm_vmw_size content_base_size = {0}; 1207 struct vmw_resource *res; 1208 unsigned int bytes_pp; 1209 struct drm_format_name_buf format_name; 1210 int ret; 1211 1212 switch (mode_cmd->pixel_format) { 1213 case DRM_FORMAT_ARGB8888: 1214 case DRM_FORMAT_XRGB8888: 1215 format = SVGA3D_X8R8G8B8; 1216 bytes_pp = 4; 1217 break; 1218 1219 case DRM_FORMAT_RGB565: 1220 case DRM_FORMAT_XRGB1555: 1221 format = SVGA3D_R5G6B5; 1222 bytes_pp = 2; 1223 break; 1224 1225 case 8: 1226 format = SVGA3D_P8; 1227 bytes_pp = 1; 1228 break; 1229 1230 default: 1231 DRM_ERROR("Invalid framebuffer format %s\n", 1232 drm_get_format_name(mode_cmd->pixel_format, &format_name)); 1233 return -EINVAL; 1234 } 1235 1236 content_base_size.width = mode_cmd->pitches[0] / bytes_pp; 1237 content_base_size.height = mode_cmd->height; 1238 content_base_size.depth = 1; 1239 1240 ret = vmw_surface_gb_priv_define(dev, 1241 0, /* kernel visible only */ 1242 0, /* flags */ 1243 format, 1244 true, /* can be a scanout buffer */ 1245 1, /* num of mip levels */ 1246 0, 1247 0, 1248 content_base_size, 1249 srf_out); 1250 if (ret) { 1251 DRM_ERROR("Failed to allocate proxy content buffer\n"); 1252 return ret; 1253 } 1254 1255 res = &(*srf_out)->res; 1256 1257 /* Reserve and switch the backing mob. */ 1258 mutex_lock(&res->dev_priv->cmdbuf_mutex); 1259 (void) vmw_resource_reserve(res, false, true); 1260 vmw_dmabuf_unreference(&res->backup); 1261 res->backup = vmw_dmabuf_reference(dmabuf_mob); 1262 res->backup_offset = 0; 1263 vmw_resource_unreserve(res, false, NULL, 0); 1264 mutex_unlock(&res->dev_priv->cmdbuf_mutex); 1265 1266 return 0; 1267 } 1268 1269 1270 1271 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv, 1272 struct vmw_dma_buffer *dmabuf, 1273 struct vmw_framebuffer **out, 1274 const struct drm_mode_fb_cmd2 1275 *mode_cmd) 1276 1277 { 1278 struct drm_device *dev = dev_priv->dev; 1279 struct vmw_framebuffer_dmabuf *vfbd; 1280 unsigned int requested_size; 1281 struct drm_format_name_buf format_name; 1282 int ret; 1283 1284 requested_size = mode_cmd->height * mode_cmd->pitches[0]; 1285 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) { 1286 DRM_ERROR("Screen buffer object size is too small " 1287 "for requested mode.\n"); 1288 return -EINVAL; 1289 } 1290 1291 /* Limited framebuffer color depth support for screen objects */ 1292 if (dev_priv->active_display_unit == vmw_du_screen_object) { 1293 switch (mode_cmd->pixel_format) { 1294 case DRM_FORMAT_XRGB8888: 1295 case DRM_FORMAT_ARGB8888: 1296 break; 1297 case DRM_FORMAT_XRGB1555: 1298 case DRM_FORMAT_RGB565: 1299 break; 1300 default: 1301 DRM_ERROR("Invalid pixel format: %s\n", 1302 drm_get_format_name(mode_cmd->pixel_format, &format_name)); 1303 return -EINVAL; 1304 } 1305 } 1306 1307 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL); 1308 if (!vfbd) { 1309 ret = -ENOMEM; 1310 goto out_err1; 1311 } 1312 1313 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd); 1314 vfbd->base.dmabuf = true; 1315 vfbd->buffer = vmw_dmabuf_reference(dmabuf); 1316 vfbd->base.user_handle = mode_cmd->handles[0]; 1317 *out = &vfbd->base; 1318 1319 ret = drm_framebuffer_init(dev, &vfbd->base.base, 1320 &vmw_framebuffer_dmabuf_funcs); 1321 if (ret) 1322 goto out_err2; 1323 1324 return 0; 1325 1326 out_err2: 1327 vmw_dmabuf_unreference(&dmabuf); 1328 kfree(vfbd); 1329 out_err1: 1330 return ret; 1331 } 1332 1333 1334 /** 1335 * vmw_kms_srf_ok - check if a surface can be created 1336 * 1337 * @width: requested width 1338 * @height: requested height 1339 * 1340 * Surfaces need to be less than texture size 1341 */ 1342 static bool 1343 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height) 1344 { 1345 if (width > dev_priv->texture_max_width || 1346 height > dev_priv->texture_max_height) 1347 return false; 1348 1349 return true; 1350 } 1351 1352 /** 1353 * vmw_kms_new_framebuffer - Create a new framebuffer. 1354 * 1355 * @dev_priv: Pointer to device private struct. 1356 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around. 1357 * Either @dmabuf or @surface must be NULL. 1358 * @surface: Pointer to a surface to wrap the kms framebuffer around. 1359 * Either @dmabuf or @surface must be NULL. 1360 * @only_2d: No presents will occur to this dma buffer based framebuffer. This 1361 * Helps the code to do some important optimizations. 1362 * @mode_cmd: Frame-buffer metadata. 1363 */ 1364 struct vmw_framebuffer * 1365 vmw_kms_new_framebuffer(struct vmw_private *dev_priv, 1366 struct vmw_dma_buffer *dmabuf, 1367 struct vmw_surface *surface, 1368 bool only_2d, 1369 const struct drm_mode_fb_cmd2 *mode_cmd) 1370 { 1371 struct vmw_framebuffer *vfb = NULL; 1372 bool is_dmabuf_proxy = false; 1373 int ret; 1374 1375 /* 1376 * We cannot use the SurfaceDMA command in an non-accelerated VM, 1377 * therefore, wrap the DMA buf in a surface so we can use the 1378 * SurfaceCopy command. 1379 */ 1380 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) && 1381 dmabuf && only_2d && 1382 mode_cmd->width > 64 && /* Don't create a proxy for cursor */ 1383 dev_priv->active_display_unit == vmw_du_screen_target) { 1384 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd, 1385 dmabuf, &surface); 1386 if (ret) 1387 return ERR_PTR(ret); 1388 1389 is_dmabuf_proxy = true; 1390 } 1391 1392 /* Create the new framebuffer depending one what we have */ 1393 if (surface) { 1394 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb, 1395 mode_cmd, 1396 is_dmabuf_proxy); 1397 1398 /* 1399 * vmw_create_dmabuf_proxy() adds a reference that is no longer 1400 * needed 1401 */ 1402 if (is_dmabuf_proxy) 1403 vmw_surface_unreference(&surface); 1404 } else if (dmabuf) { 1405 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb, 1406 mode_cmd); 1407 } else { 1408 BUG(); 1409 } 1410 1411 if (ret) 1412 return ERR_PTR(ret); 1413 1414 vfb->pin = vmw_framebuffer_pin; 1415 vfb->unpin = vmw_framebuffer_unpin; 1416 1417 return vfb; 1418 } 1419 1420 /* 1421 * Generic Kernel modesetting functions 1422 */ 1423 1424 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev, 1425 struct drm_file *file_priv, 1426 const struct drm_mode_fb_cmd2 *mode_cmd) 1427 { 1428 struct vmw_private *dev_priv = vmw_priv(dev); 1429 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 1430 struct vmw_framebuffer *vfb = NULL; 1431 struct vmw_surface *surface = NULL; 1432 struct vmw_dma_buffer *bo = NULL; 1433 struct ttm_base_object *user_obj; 1434 int ret; 1435 1436 /** 1437 * This code should be conditioned on Screen Objects not being used. 1438 * If screen objects are used, we can allocate a GMR to hold the 1439 * requested framebuffer. 1440 */ 1441 1442 if (!vmw_kms_validate_mode_vram(dev_priv, 1443 mode_cmd->pitches[0], 1444 mode_cmd->height)) { 1445 DRM_ERROR("Requested mode exceed bounding box limit.\n"); 1446 return ERR_PTR(-ENOMEM); 1447 } 1448 1449 /* 1450 * Take a reference on the user object of the resource 1451 * backing the kms fb. This ensures that user-space handle 1452 * lookups on that resource will always work as long as 1453 * it's registered with a kms framebuffer. This is important, 1454 * since vmw_execbuf_process identifies resources in the 1455 * command stream using user-space handles. 1456 */ 1457 1458 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]); 1459 if (unlikely(user_obj == NULL)) { 1460 DRM_ERROR("Could not locate requested kms frame buffer.\n"); 1461 return ERR_PTR(-ENOENT); 1462 } 1463 1464 /** 1465 * End conditioned code. 1466 */ 1467 1468 /* returns either a dmabuf or surface */ 1469 ret = vmw_user_lookup_handle(dev_priv, tfile, 1470 mode_cmd->handles[0], 1471 &surface, &bo); 1472 if (ret) 1473 goto err_out; 1474 1475 1476 if (!bo && 1477 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) { 1478 DRM_ERROR("Surface size cannot exceed %dx%d", 1479 dev_priv->texture_max_width, 1480 dev_priv->texture_max_height); 1481 goto err_out; 1482 } 1483 1484 1485 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface, 1486 !(dev_priv->capabilities & SVGA_CAP_3D), 1487 mode_cmd); 1488 if (IS_ERR(vfb)) { 1489 ret = PTR_ERR(vfb); 1490 goto err_out; 1491 } 1492 1493 err_out: 1494 /* vmw_user_lookup_handle takes one ref so does new_fb */ 1495 if (bo) 1496 vmw_dmabuf_unreference(&bo); 1497 if (surface) 1498 vmw_surface_unreference(&surface); 1499 1500 if (ret) { 1501 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret); 1502 ttm_base_object_unref(&user_obj); 1503 return ERR_PTR(ret); 1504 } else 1505 vfb->user_obj = user_obj; 1506 1507 return &vfb->base; 1508 } 1509 1510 1511 1512 /** 1513 * vmw_kms_atomic_check_modeset- validate state object for modeset changes 1514 * 1515 * @dev: DRM device 1516 * @state: the driver state object 1517 * 1518 * This is a simple wrapper around drm_atomic_helper_check_modeset() for 1519 * us to assign a value to mode->crtc_clock so that 1520 * drm_calc_timestamping_constants() won't throw an error message 1521 * 1522 * RETURNS 1523 * Zero for success or -errno 1524 */ 1525 static int 1526 vmw_kms_atomic_check_modeset(struct drm_device *dev, 1527 struct drm_atomic_state *state) 1528 { 1529 struct drm_crtc_state *crtc_state; 1530 struct drm_crtc *crtc; 1531 struct vmw_private *dev_priv = vmw_priv(dev); 1532 int i; 1533 1534 for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 1535 unsigned long requested_bb_mem = 0; 1536 1537 if (dev_priv->active_display_unit == vmw_du_screen_target) { 1538 if (crtc->primary->fb) { 1539 int cpp = crtc->primary->fb->pitches[0] / 1540 crtc->primary->fb->width; 1541 1542 requested_bb_mem += crtc->mode.hdisplay * cpp * 1543 crtc->mode.vdisplay; 1544 } 1545 1546 if (requested_bb_mem > dev_priv->prim_bb_mem) 1547 return -EINVAL; 1548 } 1549 } 1550 1551 return drm_atomic_helper_check(dev, state); 1552 } 1553 1554 1555 /** 1556 * vmw_kms_atomic_commit - Perform an atomic state commit 1557 * 1558 * @dev: DRM device 1559 * @state: the driver state object 1560 * @nonblock: Whether nonblocking behaviour is requested 1561 * 1562 * This is a simple wrapper around drm_atomic_helper_commit() for 1563 * us to clear the nonblocking value. 1564 * 1565 * Nonblocking commits currently cause synchronization issues 1566 * for vmwgfx. 1567 * 1568 * RETURNS 1569 * Zero for success or negative error code on failure. 1570 */ 1571 int vmw_kms_atomic_commit(struct drm_device *dev, 1572 struct drm_atomic_state *state, 1573 bool nonblock) 1574 { 1575 return drm_atomic_helper_commit(dev, state, false); 1576 } 1577 1578 1579 static const struct drm_mode_config_funcs vmw_kms_funcs = { 1580 .fb_create = vmw_kms_fb_create, 1581 .atomic_check = vmw_kms_atomic_check_modeset, 1582 .atomic_commit = vmw_kms_atomic_commit, 1583 }; 1584 1585 static int vmw_kms_generic_present(struct vmw_private *dev_priv, 1586 struct drm_file *file_priv, 1587 struct vmw_framebuffer *vfb, 1588 struct vmw_surface *surface, 1589 uint32_t sid, 1590 int32_t destX, int32_t destY, 1591 struct drm_vmw_rect *clips, 1592 uint32_t num_clips) 1593 { 1594 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips, 1595 &surface->res, destX, destY, 1596 num_clips, 1, NULL); 1597 } 1598 1599 1600 int vmw_kms_present(struct vmw_private *dev_priv, 1601 struct drm_file *file_priv, 1602 struct vmw_framebuffer *vfb, 1603 struct vmw_surface *surface, 1604 uint32_t sid, 1605 int32_t destX, int32_t destY, 1606 struct drm_vmw_rect *clips, 1607 uint32_t num_clips) 1608 { 1609 int ret; 1610 1611 switch (dev_priv->active_display_unit) { 1612 case vmw_du_screen_target: 1613 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips, 1614 &surface->res, destX, destY, 1615 num_clips, 1, NULL); 1616 break; 1617 case vmw_du_screen_object: 1618 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface, 1619 sid, destX, destY, clips, 1620 num_clips); 1621 break; 1622 default: 1623 WARN_ONCE(true, 1624 "Present called with invalid display system.\n"); 1625 ret = -ENOSYS; 1626 break; 1627 } 1628 if (ret) 1629 return ret; 1630 1631 vmw_fifo_flush(dev_priv, false); 1632 1633 return 0; 1634 } 1635 1636 static void 1637 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv) 1638 { 1639 if (dev_priv->hotplug_mode_update_property) 1640 return; 1641 1642 dev_priv->hotplug_mode_update_property = 1643 drm_property_create_range(dev_priv->dev, 1644 DRM_MODE_PROP_IMMUTABLE, 1645 "hotplug_mode_update", 0, 1); 1646 1647 if (!dev_priv->hotplug_mode_update_property) 1648 return; 1649 1650 } 1651 1652 int vmw_kms_init(struct vmw_private *dev_priv) 1653 { 1654 struct drm_device *dev = dev_priv->dev; 1655 int ret; 1656 1657 drm_mode_config_init(dev); 1658 dev->mode_config.funcs = &vmw_kms_funcs; 1659 dev->mode_config.min_width = 1; 1660 dev->mode_config.min_height = 1; 1661 dev->mode_config.max_width = dev_priv->texture_max_width; 1662 dev->mode_config.max_height = dev_priv->texture_max_height; 1663 1664 drm_mode_create_suggested_offset_properties(dev); 1665 vmw_kms_create_hotplug_mode_update_property(dev_priv); 1666 1667 ret = vmw_kms_stdu_init_display(dev_priv); 1668 if (ret) { 1669 ret = vmw_kms_sou_init_display(dev_priv); 1670 if (ret) /* Fallback */ 1671 ret = vmw_kms_ldu_init_display(dev_priv); 1672 } 1673 1674 return ret; 1675 } 1676 1677 int vmw_kms_close(struct vmw_private *dev_priv) 1678 { 1679 int ret = 0; 1680 1681 /* 1682 * Docs says we should take the lock before calling this function 1683 * but since it destroys encoders and our destructor calls 1684 * drm_encoder_cleanup which takes the lock we deadlock. 1685 */ 1686 drm_mode_config_cleanup(dev_priv->dev); 1687 if (dev_priv->active_display_unit == vmw_du_legacy) 1688 ret = vmw_kms_ldu_close_display(dev_priv); 1689 1690 return ret; 1691 } 1692 1693 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data, 1694 struct drm_file *file_priv) 1695 { 1696 struct drm_vmw_cursor_bypass_arg *arg = data; 1697 struct vmw_display_unit *du; 1698 struct drm_crtc *crtc; 1699 int ret = 0; 1700 1701 1702 mutex_lock(&dev->mode_config.mutex); 1703 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) { 1704 1705 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 1706 du = vmw_crtc_to_du(crtc); 1707 du->hotspot_x = arg->xhot; 1708 du->hotspot_y = arg->yhot; 1709 } 1710 1711 mutex_unlock(&dev->mode_config.mutex); 1712 return 0; 1713 } 1714 1715 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id); 1716 if (!crtc) { 1717 ret = -ENOENT; 1718 goto out; 1719 } 1720 1721 du = vmw_crtc_to_du(crtc); 1722 1723 du->hotspot_x = arg->xhot; 1724 du->hotspot_y = arg->yhot; 1725 1726 out: 1727 mutex_unlock(&dev->mode_config.mutex); 1728 1729 return ret; 1730 } 1731 1732 int vmw_kms_write_svga(struct vmw_private *vmw_priv, 1733 unsigned width, unsigned height, unsigned pitch, 1734 unsigned bpp, unsigned depth) 1735 { 1736 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK) 1737 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch); 1738 else if (vmw_fifo_have_pitchlock(vmw_priv)) 1739 vmw_mmio_write(pitch, vmw_priv->mmio_virt + 1740 SVGA_FIFO_PITCHLOCK); 1741 vmw_write(vmw_priv, SVGA_REG_WIDTH, width); 1742 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height); 1743 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp); 1744 1745 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) { 1746 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n", 1747 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH)); 1748 return -EINVAL; 1749 } 1750 1751 return 0; 1752 } 1753 1754 int vmw_kms_save_vga(struct vmw_private *vmw_priv) 1755 { 1756 struct vmw_vga_topology_state *save; 1757 uint32_t i; 1758 1759 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH); 1760 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT); 1761 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL); 1762 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK) 1763 vmw_priv->vga_pitchlock = 1764 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK); 1765 else if (vmw_fifo_have_pitchlock(vmw_priv)) 1766 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt + 1767 SVGA_FIFO_PITCHLOCK); 1768 1769 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) 1770 return 0; 1771 1772 vmw_priv->num_displays = vmw_read(vmw_priv, 1773 SVGA_REG_NUM_GUEST_DISPLAYS); 1774 1775 if (vmw_priv->num_displays == 0) 1776 vmw_priv->num_displays = 1; 1777 1778 for (i = 0; i < vmw_priv->num_displays; ++i) { 1779 save = &vmw_priv->vga_save[i]; 1780 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i); 1781 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY); 1782 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X); 1783 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y); 1784 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH); 1785 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT); 1786 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID); 1787 if (i == 0 && vmw_priv->num_displays == 1 && 1788 save->width == 0 && save->height == 0) { 1789 1790 /* 1791 * It should be fairly safe to assume that these 1792 * values are uninitialized. 1793 */ 1794 1795 save->width = vmw_priv->vga_width - save->pos_x; 1796 save->height = vmw_priv->vga_height - save->pos_y; 1797 } 1798 } 1799 1800 return 0; 1801 } 1802 1803 int vmw_kms_restore_vga(struct vmw_private *vmw_priv) 1804 { 1805 struct vmw_vga_topology_state *save; 1806 uint32_t i; 1807 1808 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width); 1809 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height); 1810 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp); 1811 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK) 1812 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, 1813 vmw_priv->vga_pitchlock); 1814 else if (vmw_fifo_have_pitchlock(vmw_priv)) 1815 vmw_mmio_write(vmw_priv->vga_pitchlock, 1816 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK); 1817 1818 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) 1819 return 0; 1820 1821 for (i = 0; i < vmw_priv->num_displays; ++i) { 1822 save = &vmw_priv->vga_save[i]; 1823 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i); 1824 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary); 1825 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x); 1826 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y); 1827 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width); 1828 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height); 1829 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID); 1830 } 1831 1832 return 0; 1833 } 1834 1835 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv, 1836 uint32_t pitch, 1837 uint32_t height) 1838 { 1839 return ((u64) pitch * (u64) height) < (u64) 1840 ((dev_priv->active_display_unit == vmw_du_screen_target) ? 1841 dev_priv->prim_bb_mem : dev_priv->vram_size); 1842 } 1843 1844 1845 /** 1846 * Function called by DRM code called with vbl_lock held. 1847 */ 1848 u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe) 1849 { 1850 return 0; 1851 } 1852 1853 /** 1854 * Function called by DRM code called with vbl_lock held. 1855 */ 1856 int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe) 1857 { 1858 return -ENOSYS; 1859 } 1860 1861 /** 1862 * Function called by DRM code called with vbl_lock held. 1863 */ 1864 void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe) 1865 { 1866 } 1867 1868 1869 /* 1870 * Small shared kms functions. 1871 */ 1872 1873 static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num, 1874 struct drm_vmw_rect *rects) 1875 { 1876 struct drm_device *dev = dev_priv->dev; 1877 struct vmw_display_unit *du; 1878 struct drm_connector *con; 1879 1880 mutex_lock(&dev->mode_config.mutex); 1881 1882 #if 0 1883 { 1884 unsigned int i; 1885 1886 DRM_INFO("%s: new layout ", __func__); 1887 for (i = 0; i < num; i++) 1888 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y, 1889 rects[i].w, rects[i].h); 1890 DRM_INFO("\n"); 1891 } 1892 #endif 1893 1894 list_for_each_entry(con, &dev->mode_config.connector_list, head) { 1895 du = vmw_connector_to_du(con); 1896 if (num > du->unit) { 1897 du->pref_width = rects[du->unit].w; 1898 du->pref_height = rects[du->unit].h; 1899 du->pref_active = true; 1900 du->gui_x = rects[du->unit].x; 1901 du->gui_y = rects[du->unit].y; 1902 drm_object_property_set_value 1903 (&con->base, dev->mode_config.suggested_x_property, 1904 du->gui_x); 1905 drm_object_property_set_value 1906 (&con->base, dev->mode_config.suggested_y_property, 1907 du->gui_y); 1908 } else { 1909 du->pref_width = 800; 1910 du->pref_height = 600; 1911 du->pref_active = false; 1912 drm_object_property_set_value 1913 (&con->base, dev->mode_config.suggested_x_property, 1914 0); 1915 drm_object_property_set_value 1916 (&con->base, dev->mode_config.suggested_y_property, 1917 0); 1918 } 1919 con->status = vmw_du_connector_detect(con, true); 1920 } 1921 1922 mutex_unlock(&dev->mode_config.mutex); 1923 drm_sysfs_hotplug_event(dev); 1924 1925 return 0; 1926 } 1927 1928 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc, 1929 u16 *r, u16 *g, u16 *b, 1930 uint32_t size, 1931 struct drm_modeset_acquire_ctx *ctx) 1932 { 1933 struct vmw_private *dev_priv = vmw_priv(crtc->dev); 1934 int i; 1935 1936 for (i = 0; i < size; i++) { 1937 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i, 1938 r[i], g[i], b[i]); 1939 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8); 1940 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8); 1941 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8); 1942 } 1943 1944 return 0; 1945 } 1946 1947 int vmw_du_connector_dpms(struct drm_connector *connector, int mode) 1948 { 1949 return 0; 1950 } 1951 1952 enum drm_connector_status 1953 vmw_du_connector_detect(struct drm_connector *connector, bool force) 1954 { 1955 uint32_t num_displays; 1956 struct drm_device *dev = connector->dev; 1957 struct vmw_private *dev_priv = vmw_priv(dev); 1958 struct vmw_display_unit *du = vmw_connector_to_du(connector); 1959 1960 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS); 1961 1962 return ((vmw_connector_to_du(connector)->unit < num_displays && 1963 du->pref_active) ? 1964 connector_status_connected : connector_status_disconnected); 1965 } 1966 1967 static struct drm_display_mode vmw_kms_connector_builtin[] = { 1968 /* 640x480@60Hz */ 1969 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656, 1970 752, 800, 0, 480, 489, 492, 525, 0, 1971 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, 1972 /* 800x600@60Hz */ 1973 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840, 1974 968, 1056, 0, 600, 601, 605, 628, 0, 1975 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, 1976 /* 1024x768@60Hz */ 1977 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048, 1978 1184, 1344, 0, 768, 771, 777, 806, 0, 1979 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, 1980 /* 1152x864@75Hz */ 1981 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216, 1982 1344, 1600, 0, 864, 865, 868, 900, 0, 1983 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, 1984 /* 1280x768@60Hz */ 1985 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344, 1986 1472, 1664, 0, 768, 771, 778, 798, 0, 1987 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, 1988 /* 1280x800@60Hz */ 1989 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352, 1990 1480, 1680, 0, 800, 803, 809, 831, 0, 1991 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) }, 1992 /* 1280x960@60Hz */ 1993 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376, 1994 1488, 1800, 0, 960, 961, 964, 1000, 0, 1995 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, 1996 /* 1280x1024@60Hz */ 1997 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328, 1998 1440, 1688, 0, 1024, 1025, 1028, 1066, 0, 1999 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2000 /* 1360x768@60Hz */ 2001 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424, 2002 1536, 1792, 0, 768, 771, 777, 795, 0, 2003 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2004 /* 1440x1050@60Hz */ 2005 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488, 2006 1632, 1864, 0, 1050, 1053, 1057, 1089, 0, 2007 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2008 /* 1440x900@60Hz */ 2009 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520, 2010 1672, 1904, 0, 900, 903, 909, 934, 0, 2011 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2012 /* 1600x1200@60Hz */ 2013 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664, 2014 1856, 2160, 0, 1200, 1201, 1204, 1250, 0, 2015 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2016 /* 1680x1050@60Hz */ 2017 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784, 2018 1960, 2240, 0, 1050, 1053, 1059, 1089, 0, 2019 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2020 /* 1792x1344@60Hz */ 2021 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920, 2022 2120, 2448, 0, 1344, 1345, 1348, 1394, 0, 2023 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2024 /* 1853x1392@60Hz */ 2025 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952, 2026 2176, 2528, 0, 1392, 1393, 1396, 1439, 0, 2027 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2028 /* 1920x1200@60Hz */ 2029 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056, 2030 2256, 2592, 0, 1200, 1203, 1209, 1245, 0, 2031 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2032 /* 1920x1440@60Hz */ 2033 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048, 2034 2256, 2600, 0, 1440, 1441, 1444, 1500, 0, 2035 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2036 /* 2560x1600@60Hz */ 2037 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752, 2038 3032, 3504, 0, 1600, 1603, 1609, 1658, 0, 2039 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, 2040 /* Terminate */ 2041 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) }, 2042 }; 2043 2044 /** 2045 * vmw_guess_mode_timing - Provide fake timings for a 2046 * 60Hz vrefresh mode. 2047 * 2048 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay 2049 * members filled in. 2050 */ 2051 void vmw_guess_mode_timing(struct drm_display_mode *mode) 2052 { 2053 mode->hsync_start = mode->hdisplay + 50; 2054 mode->hsync_end = mode->hsync_start + 50; 2055 mode->htotal = mode->hsync_end + 50; 2056 2057 mode->vsync_start = mode->vdisplay + 50; 2058 mode->vsync_end = mode->vsync_start + 50; 2059 mode->vtotal = mode->vsync_end + 50; 2060 2061 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6; 2062 mode->vrefresh = drm_mode_vrefresh(mode); 2063 } 2064 2065 2066 int vmw_du_connector_fill_modes(struct drm_connector *connector, 2067 uint32_t max_width, uint32_t max_height) 2068 { 2069 struct vmw_display_unit *du = vmw_connector_to_du(connector); 2070 struct drm_device *dev = connector->dev; 2071 struct vmw_private *dev_priv = vmw_priv(dev); 2072 struct drm_display_mode *mode = NULL; 2073 struct drm_display_mode *bmode; 2074 struct drm_display_mode prefmode = { DRM_MODE("preferred", 2075 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, 2076 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2077 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) 2078 }; 2079 int i; 2080 u32 assumed_bpp = 4; 2081 2082 if (dev_priv->assume_16bpp) 2083 assumed_bpp = 2; 2084 2085 if (dev_priv->active_display_unit == vmw_du_screen_target) { 2086 max_width = min(max_width, dev_priv->stdu_max_width); 2087 max_width = min(max_width, dev_priv->texture_max_width); 2088 2089 max_height = min(max_height, dev_priv->stdu_max_height); 2090 max_height = min(max_height, dev_priv->texture_max_height); 2091 } 2092 2093 /* Add preferred mode */ 2094 mode = drm_mode_duplicate(dev, &prefmode); 2095 if (!mode) 2096 return 0; 2097 mode->hdisplay = du->pref_width; 2098 mode->vdisplay = du->pref_height; 2099 vmw_guess_mode_timing(mode); 2100 2101 if (vmw_kms_validate_mode_vram(dev_priv, 2102 mode->hdisplay * assumed_bpp, 2103 mode->vdisplay)) { 2104 drm_mode_probed_add(connector, mode); 2105 } else { 2106 drm_mode_destroy(dev, mode); 2107 mode = NULL; 2108 } 2109 2110 if (du->pref_mode) { 2111 list_del_init(&du->pref_mode->head); 2112 drm_mode_destroy(dev, du->pref_mode); 2113 } 2114 2115 /* mode might be null here, this is intended */ 2116 du->pref_mode = mode; 2117 2118 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) { 2119 bmode = &vmw_kms_connector_builtin[i]; 2120 if (bmode->hdisplay > max_width || 2121 bmode->vdisplay > max_height) 2122 continue; 2123 2124 if (!vmw_kms_validate_mode_vram(dev_priv, 2125 bmode->hdisplay * assumed_bpp, 2126 bmode->vdisplay)) 2127 continue; 2128 2129 mode = drm_mode_duplicate(dev, bmode); 2130 if (!mode) 2131 return 0; 2132 mode->vrefresh = drm_mode_vrefresh(mode); 2133 2134 drm_mode_probed_add(connector, mode); 2135 } 2136 2137 drm_mode_connector_list_update(connector); 2138 /* Move the prefered mode first, help apps pick the right mode. */ 2139 drm_mode_sort(&connector->modes); 2140 2141 return 1; 2142 } 2143 2144 int vmw_du_connector_set_property(struct drm_connector *connector, 2145 struct drm_property *property, 2146 uint64_t val) 2147 { 2148 struct vmw_display_unit *du = vmw_connector_to_du(connector); 2149 struct vmw_private *dev_priv = vmw_priv(connector->dev); 2150 2151 if (property == dev_priv->implicit_placement_property) 2152 du->is_implicit = val; 2153 2154 return 0; 2155 } 2156 2157 2158 2159 /** 2160 * vmw_du_connector_atomic_set_property - Atomic version of get property 2161 * 2162 * @crtc - crtc the property is associated with 2163 * 2164 * Returns: 2165 * Zero on success, negative errno on failure. 2166 */ 2167 int 2168 vmw_du_connector_atomic_set_property(struct drm_connector *connector, 2169 struct drm_connector_state *state, 2170 struct drm_property *property, 2171 uint64_t val) 2172 { 2173 struct vmw_private *dev_priv = vmw_priv(connector->dev); 2174 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state); 2175 struct vmw_display_unit *du = vmw_connector_to_du(connector); 2176 2177 2178 if (property == dev_priv->implicit_placement_property) { 2179 vcs->is_implicit = val; 2180 2181 /* 2182 * We should really be doing a drm_atomic_commit() to 2183 * commit the new state, but since this doesn't cause 2184 * an immedate state change, this is probably ok 2185 */ 2186 du->is_implicit = vcs->is_implicit; 2187 } else { 2188 return -EINVAL; 2189 } 2190 2191 return 0; 2192 } 2193 2194 2195 /** 2196 * vmw_du_connector_atomic_get_property - Atomic version of get property 2197 * 2198 * @connector - connector the property is associated with 2199 * 2200 * Returns: 2201 * Zero on success, negative errno on failure. 2202 */ 2203 int 2204 vmw_du_connector_atomic_get_property(struct drm_connector *connector, 2205 const struct drm_connector_state *state, 2206 struct drm_property *property, 2207 uint64_t *val) 2208 { 2209 struct vmw_private *dev_priv = vmw_priv(connector->dev); 2210 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state); 2211 2212 if (property == dev_priv->implicit_placement_property) 2213 *val = vcs->is_implicit; 2214 else { 2215 DRM_ERROR("Invalid Property %s\n", property->name); 2216 return -EINVAL; 2217 } 2218 2219 return 0; 2220 } 2221 2222 2223 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data, 2224 struct drm_file *file_priv) 2225 { 2226 struct vmw_private *dev_priv = vmw_priv(dev); 2227 struct drm_vmw_update_layout_arg *arg = 2228 (struct drm_vmw_update_layout_arg *)data; 2229 void __user *user_rects; 2230 struct drm_vmw_rect *rects; 2231 unsigned rects_size; 2232 int ret; 2233 int i; 2234 u64 total_pixels = 0; 2235 struct drm_mode_config *mode_config = &dev->mode_config; 2236 struct drm_vmw_rect bounding_box = {0}; 2237 2238 if (!arg->num_outputs) { 2239 struct drm_vmw_rect def_rect = {0, 0, 800, 600}; 2240 vmw_du_update_layout(dev_priv, 1, &def_rect); 2241 return 0; 2242 } 2243 2244 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect); 2245 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect), 2246 GFP_KERNEL); 2247 if (unlikely(!rects)) 2248 return -ENOMEM; 2249 2250 user_rects = (void __user *)(unsigned long)arg->rects; 2251 ret = copy_from_user(rects, user_rects, rects_size); 2252 if (unlikely(ret != 0)) { 2253 DRM_ERROR("Failed to get rects.\n"); 2254 ret = -EFAULT; 2255 goto out_free; 2256 } 2257 2258 for (i = 0; i < arg->num_outputs; ++i) { 2259 if (rects[i].x < 0 || 2260 rects[i].y < 0 || 2261 rects[i].x + rects[i].w > mode_config->max_width || 2262 rects[i].y + rects[i].h > mode_config->max_height) { 2263 DRM_ERROR("Invalid GUI layout.\n"); 2264 ret = -EINVAL; 2265 goto out_free; 2266 } 2267 2268 /* 2269 * bounding_box.w and bunding_box.h are used as 2270 * lower-right coordinates 2271 */ 2272 if (rects[i].x + rects[i].w > bounding_box.w) 2273 bounding_box.w = rects[i].x + rects[i].w; 2274 2275 if (rects[i].y + rects[i].h > bounding_box.h) 2276 bounding_box.h = rects[i].y + rects[i].h; 2277 2278 total_pixels += (u64) rects[i].w * (u64) rects[i].h; 2279 } 2280 2281 if (dev_priv->active_display_unit == vmw_du_screen_target) { 2282 /* 2283 * For Screen Targets, the limits for a toplogy are: 2284 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem 2285 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem 2286 */ 2287 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4; 2288 u64 pixel_mem = total_pixels * 4; 2289 2290 if (bb_mem > dev_priv->prim_bb_mem) { 2291 DRM_ERROR("Topology is beyond supported limits.\n"); 2292 ret = -EINVAL; 2293 goto out_free; 2294 } 2295 2296 if (pixel_mem > dev_priv->prim_bb_mem) { 2297 DRM_ERROR("Combined output size too large\n"); 2298 ret = -EINVAL; 2299 goto out_free; 2300 } 2301 } 2302 2303 vmw_du_update_layout(dev_priv, arg->num_outputs, rects); 2304 2305 out_free: 2306 kfree(rects); 2307 return ret; 2308 } 2309 2310 /** 2311 * vmw_kms_helper_dirty - Helper to build commands and perform actions based 2312 * on a set of cliprects and a set of display units. 2313 * 2314 * @dev_priv: Pointer to a device private structure. 2315 * @framebuffer: Pointer to the framebuffer on which to perform the actions. 2316 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL. 2317 * Cliprects are given in framebuffer coordinates. 2318 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must 2319 * be NULL. Cliprects are given in source coordinates. 2320 * @dest_x: X coordinate offset for the crtc / destination clip rects. 2321 * @dest_y: Y coordinate offset for the crtc / destination clip rects. 2322 * @num_clips: Number of cliprects in the @clips or @vclips array. 2323 * @increment: Integer with which to increment the clip counter when looping. 2324 * Used to skip a predetermined number of clip rects. 2325 * @dirty: Closure structure. See the description of struct vmw_kms_dirty. 2326 */ 2327 int vmw_kms_helper_dirty(struct vmw_private *dev_priv, 2328 struct vmw_framebuffer *framebuffer, 2329 const struct drm_clip_rect *clips, 2330 const struct drm_vmw_rect *vclips, 2331 s32 dest_x, s32 dest_y, 2332 int num_clips, 2333 int increment, 2334 struct vmw_kms_dirty *dirty) 2335 { 2336 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS]; 2337 struct drm_crtc *crtc; 2338 u32 num_units = 0; 2339 u32 i, k; 2340 2341 dirty->dev_priv = dev_priv; 2342 2343 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) { 2344 if (crtc->primary->fb != &framebuffer->base) 2345 continue; 2346 units[num_units++] = vmw_crtc_to_du(crtc); 2347 } 2348 2349 for (k = 0; k < num_units; k++) { 2350 struct vmw_display_unit *unit = units[k]; 2351 s32 crtc_x = unit->crtc.x; 2352 s32 crtc_y = unit->crtc.y; 2353 s32 crtc_width = unit->crtc.mode.hdisplay; 2354 s32 crtc_height = unit->crtc.mode.vdisplay; 2355 const struct drm_clip_rect *clips_ptr = clips; 2356 const struct drm_vmw_rect *vclips_ptr = vclips; 2357 2358 dirty->unit = unit; 2359 if (dirty->fifo_reserve_size > 0) { 2360 dirty->cmd = vmw_fifo_reserve(dev_priv, 2361 dirty->fifo_reserve_size); 2362 if (!dirty->cmd) { 2363 DRM_ERROR("Couldn't reserve fifo space " 2364 "for dirty blits.\n"); 2365 return -ENOMEM; 2366 } 2367 memset(dirty->cmd, 0, dirty->fifo_reserve_size); 2368 } 2369 dirty->num_hits = 0; 2370 for (i = 0; i < num_clips; i++, clips_ptr += increment, 2371 vclips_ptr += increment) { 2372 s32 clip_left; 2373 s32 clip_top; 2374 2375 /* 2376 * Select clip array type. Note that integer type 2377 * in @clips is unsigned short, whereas in @vclips 2378 * it's 32-bit. 2379 */ 2380 if (clips) { 2381 dirty->fb_x = (s32) clips_ptr->x1; 2382 dirty->fb_y = (s32) clips_ptr->y1; 2383 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x - 2384 crtc_x; 2385 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y - 2386 crtc_y; 2387 } else { 2388 dirty->fb_x = vclips_ptr->x; 2389 dirty->fb_y = vclips_ptr->y; 2390 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w + 2391 dest_x - crtc_x; 2392 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h + 2393 dest_y - crtc_y; 2394 } 2395 2396 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x; 2397 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y; 2398 2399 /* Skip this clip if it's outside the crtc region */ 2400 if (dirty->unit_x1 >= crtc_width || 2401 dirty->unit_y1 >= crtc_height || 2402 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0) 2403 continue; 2404 2405 /* Clip right and bottom to crtc limits */ 2406 dirty->unit_x2 = min_t(s32, dirty->unit_x2, 2407 crtc_width); 2408 dirty->unit_y2 = min_t(s32, dirty->unit_y2, 2409 crtc_height); 2410 2411 /* Clip left and top to crtc limits */ 2412 clip_left = min_t(s32, dirty->unit_x1, 0); 2413 clip_top = min_t(s32, dirty->unit_y1, 0); 2414 dirty->unit_x1 -= clip_left; 2415 dirty->unit_y1 -= clip_top; 2416 dirty->fb_x -= clip_left; 2417 dirty->fb_y -= clip_top; 2418 2419 dirty->clip(dirty); 2420 } 2421 2422 dirty->fifo_commit(dirty); 2423 } 2424 2425 return 0; 2426 } 2427 2428 /** 2429 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before 2430 * command submission. 2431 * 2432 * @dev_priv. Pointer to a device private structure. 2433 * @buf: The buffer object 2434 * @interruptible: Whether to perform waits as interruptible. 2435 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false, 2436 * The buffer will be validated as a GMR. Already pinned buffers will not be 2437 * validated. 2438 * 2439 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if 2440 * interrupted by a signal. 2441 */ 2442 int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv, 2443 struct vmw_dma_buffer *buf, 2444 bool interruptible, 2445 bool validate_as_mob) 2446 { 2447 struct ttm_buffer_object *bo = &buf->base; 2448 int ret; 2449 2450 ttm_bo_reserve(bo, false, false, NULL); 2451 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible, 2452 validate_as_mob); 2453 if (ret) 2454 ttm_bo_unreserve(bo); 2455 2456 return ret; 2457 } 2458 2459 /** 2460 * vmw_kms_helper_buffer_revert - Undo the actions of 2461 * vmw_kms_helper_buffer_prepare. 2462 * 2463 * @res: Pointer to the buffer object. 2464 * 2465 * Helper to be used if an error forces the caller to undo the actions of 2466 * vmw_kms_helper_buffer_prepare. 2467 */ 2468 void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf) 2469 { 2470 if (buf) 2471 ttm_bo_unreserve(&buf->base); 2472 } 2473 2474 /** 2475 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after 2476 * kms command submission. 2477 * 2478 * @dev_priv: Pointer to a device private structure. 2479 * @file_priv: Pointer to a struct drm_file representing the caller's 2480 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely 2481 * if non-NULL, @user_fence_rep must be non-NULL. 2482 * @buf: The buffer object. 2483 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a 2484 * ref-counted fence pointer is returned here. 2485 * @user_fence_rep: Optional pointer to a user-space provided struct 2486 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the 2487 * function copies fence data to user-space in a fail-safe manner. 2488 */ 2489 void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv, 2490 struct drm_file *file_priv, 2491 struct vmw_dma_buffer *buf, 2492 struct vmw_fence_obj **out_fence, 2493 struct drm_vmw_fence_rep __user * 2494 user_fence_rep) 2495 { 2496 struct vmw_fence_obj *fence; 2497 uint32_t handle; 2498 int ret; 2499 2500 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence, 2501 file_priv ? &handle : NULL); 2502 if (buf) 2503 vmw_fence_single_bo(&buf->base, fence); 2504 if (file_priv) 2505 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), 2506 ret, user_fence_rep, fence, 2507 handle, -1, NULL); 2508 if (out_fence) 2509 *out_fence = fence; 2510 else 2511 vmw_fence_obj_unreference(&fence); 2512 2513 vmw_kms_helper_buffer_revert(buf); 2514 } 2515 2516 2517 /** 2518 * vmw_kms_helper_resource_revert - Undo the actions of 2519 * vmw_kms_helper_resource_prepare. 2520 * 2521 * @res: Pointer to the resource. Typically a surface. 2522 * 2523 * Helper to be used if an error forces the caller to undo the actions of 2524 * vmw_kms_helper_resource_prepare. 2525 */ 2526 void vmw_kms_helper_resource_revert(struct vmw_resource *res) 2527 { 2528 vmw_kms_helper_buffer_revert(res->backup); 2529 vmw_resource_unreserve(res, false, NULL, 0); 2530 mutex_unlock(&res->dev_priv->cmdbuf_mutex); 2531 } 2532 2533 /** 2534 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before 2535 * command submission. 2536 * 2537 * @res: Pointer to the resource. Typically a surface. 2538 * @interruptible: Whether to perform waits as interruptible. 2539 * 2540 * Reserves and validates also the backup buffer if a guest-backed resource. 2541 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if 2542 * interrupted by a signal. 2543 */ 2544 int vmw_kms_helper_resource_prepare(struct vmw_resource *res, 2545 bool interruptible) 2546 { 2547 int ret = 0; 2548 2549 if (interruptible) 2550 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex); 2551 else 2552 mutex_lock(&res->dev_priv->cmdbuf_mutex); 2553 2554 if (unlikely(ret != 0)) 2555 return -ERESTARTSYS; 2556 2557 ret = vmw_resource_reserve(res, interruptible, false); 2558 if (ret) 2559 goto out_unlock; 2560 2561 if (res->backup) { 2562 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup, 2563 interruptible, 2564 res->dev_priv->has_mob); 2565 if (ret) 2566 goto out_unreserve; 2567 } 2568 ret = vmw_resource_validate(res); 2569 if (ret) 2570 goto out_revert; 2571 return 0; 2572 2573 out_revert: 2574 vmw_kms_helper_buffer_revert(res->backup); 2575 out_unreserve: 2576 vmw_resource_unreserve(res, false, NULL, 0); 2577 out_unlock: 2578 mutex_unlock(&res->dev_priv->cmdbuf_mutex); 2579 return ret; 2580 } 2581 2582 /** 2583 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after 2584 * kms command submission. 2585 * 2586 * @res: Pointer to the resource. Typically a surface. 2587 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a 2588 * ref-counted fence pointer is returned here. 2589 */ 2590 void vmw_kms_helper_resource_finish(struct vmw_resource *res, 2591 struct vmw_fence_obj **out_fence) 2592 { 2593 if (res->backup || out_fence) 2594 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup, 2595 out_fence, NULL); 2596 2597 vmw_resource_unreserve(res, false, NULL, 0); 2598 mutex_unlock(&res->dev_priv->cmdbuf_mutex); 2599 } 2600 2601 /** 2602 * vmw_kms_update_proxy - Helper function to update a proxy surface from 2603 * its backing MOB. 2604 * 2605 * @res: Pointer to the surface resource 2606 * @clips: Clip rects in framebuffer (surface) space. 2607 * @num_clips: Number of clips in @clips. 2608 * @increment: Integer with which to increment the clip counter when looping. 2609 * Used to skip a predetermined number of clip rects. 2610 * 2611 * This function makes sure the proxy surface is updated from its backing MOB 2612 * using the region given by @clips. The surface resource @res and its backing 2613 * MOB needs to be reserved and validated on call. 2614 */ 2615 int vmw_kms_update_proxy(struct vmw_resource *res, 2616 const struct drm_clip_rect *clips, 2617 unsigned num_clips, 2618 int increment) 2619 { 2620 struct vmw_private *dev_priv = res->dev_priv; 2621 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size; 2622 struct { 2623 SVGA3dCmdHeader header; 2624 SVGA3dCmdUpdateGBImage body; 2625 } *cmd; 2626 SVGA3dBox *box; 2627 size_t copy_size = 0; 2628 int i; 2629 2630 if (!clips) 2631 return 0; 2632 2633 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips); 2634 if (!cmd) { 2635 DRM_ERROR("Couldn't reserve fifo space for proxy surface " 2636 "update.\n"); 2637 return -ENOMEM; 2638 } 2639 2640 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) { 2641 box = &cmd->body.box; 2642 2643 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE; 2644 cmd->header.size = sizeof(cmd->body); 2645 cmd->body.image.sid = res->id; 2646 cmd->body.image.face = 0; 2647 cmd->body.image.mipmap = 0; 2648 2649 if (clips->x1 > size->width || clips->x2 > size->width || 2650 clips->y1 > size->height || clips->y2 > size->height) { 2651 DRM_ERROR("Invalid clips outsize of framebuffer.\n"); 2652 return -EINVAL; 2653 } 2654 2655 box->x = clips->x1; 2656 box->y = clips->y1; 2657 box->z = 0; 2658 box->w = clips->x2 - clips->x1; 2659 box->h = clips->y2 - clips->y1; 2660 box->d = 1; 2661 2662 copy_size += sizeof(*cmd); 2663 } 2664 2665 vmw_fifo_commit(dev_priv, copy_size); 2666 2667 return 0; 2668 } 2669 2670 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv, 2671 unsigned unit, 2672 u32 max_width, 2673 u32 max_height, 2674 struct drm_connector **p_con, 2675 struct drm_crtc **p_crtc, 2676 struct drm_display_mode **p_mode) 2677 { 2678 struct drm_connector *con; 2679 struct vmw_display_unit *du; 2680 struct drm_display_mode *mode; 2681 int i = 0; 2682 2683 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list, 2684 head) { 2685 if (i == unit) 2686 break; 2687 2688 ++i; 2689 } 2690 2691 if (i != unit) { 2692 DRM_ERROR("Could not find initial display unit.\n"); 2693 return -EINVAL; 2694 } 2695 2696 if (list_empty(&con->modes)) 2697 (void) vmw_du_connector_fill_modes(con, max_width, max_height); 2698 2699 if (list_empty(&con->modes)) { 2700 DRM_ERROR("Could not find initial display mode.\n"); 2701 return -EINVAL; 2702 } 2703 2704 du = vmw_connector_to_du(con); 2705 *p_con = con; 2706 *p_crtc = &du->crtc; 2707 2708 list_for_each_entry(mode, &con->modes, head) { 2709 if (mode->type & DRM_MODE_TYPE_PREFERRED) 2710 break; 2711 } 2712 2713 if (mode->type & DRM_MODE_TYPE_PREFERRED) 2714 *p_mode = mode; 2715 else { 2716 WARN_ONCE(true, "Could not find initial preferred mode.\n"); 2717 *p_mode = list_first_entry(&con->modes, 2718 struct drm_display_mode, 2719 head); 2720 } 2721 2722 return 0; 2723 } 2724 2725 /** 2726 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer 2727 * 2728 * @dev_priv: Pointer to a device private struct. 2729 * @du: The display unit of the crtc. 2730 */ 2731 void vmw_kms_del_active(struct vmw_private *dev_priv, 2732 struct vmw_display_unit *du) 2733 { 2734 mutex_lock(&dev_priv->global_kms_state_mutex); 2735 if (du->active_implicit) { 2736 if (--(dev_priv->num_implicit) == 0) 2737 dev_priv->implicit_fb = NULL; 2738 du->active_implicit = false; 2739 } 2740 mutex_unlock(&dev_priv->global_kms_state_mutex); 2741 } 2742 2743 /** 2744 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer 2745 * 2746 * @vmw_priv: Pointer to a device private struct. 2747 * @du: The display unit of the crtc. 2748 * @vfb: The implicit framebuffer 2749 * 2750 * Registers a binding to an implicit framebuffer. 2751 */ 2752 void vmw_kms_add_active(struct vmw_private *dev_priv, 2753 struct vmw_display_unit *du, 2754 struct vmw_framebuffer *vfb) 2755 { 2756 mutex_lock(&dev_priv->global_kms_state_mutex); 2757 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb); 2758 2759 if (!du->active_implicit && du->is_implicit) { 2760 dev_priv->implicit_fb = vfb; 2761 du->active_implicit = true; 2762 dev_priv->num_implicit++; 2763 } 2764 mutex_unlock(&dev_priv->global_kms_state_mutex); 2765 } 2766 2767 /** 2768 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc. 2769 * 2770 * @dev_priv: Pointer to device-private struct. 2771 * @crtc: The crtc we want to flip. 2772 * 2773 * Returns true or false depending whether it's OK to flip this crtc 2774 * based on the criterion that we must not have more than one implicit 2775 * frame-buffer at any one time. 2776 */ 2777 bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv, 2778 struct drm_crtc *crtc) 2779 { 2780 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 2781 bool ret; 2782 2783 mutex_lock(&dev_priv->global_kms_state_mutex); 2784 ret = !du->is_implicit || dev_priv->num_implicit == 1; 2785 mutex_unlock(&dev_priv->global_kms_state_mutex); 2786 2787 return ret; 2788 } 2789 2790 /** 2791 * vmw_kms_update_implicit_fb - Update the implicit fb. 2792 * 2793 * @dev_priv: Pointer to device-private struct. 2794 * @crtc: The crtc the new implicit frame-buffer is bound to. 2795 */ 2796 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv, 2797 struct drm_crtc *crtc) 2798 { 2799 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 2800 struct vmw_framebuffer *vfb; 2801 2802 mutex_lock(&dev_priv->global_kms_state_mutex); 2803 2804 if (!du->is_implicit) 2805 goto out_unlock; 2806 2807 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb); 2808 WARN_ON_ONCE(dev_priv->num_implicit != 1 && 2809 dev_priv->implicit_fb != vfb); 2810 2811 dev_priv->implicit_fb = vfb; 2812 out_unlock: 2813 mutex_unlock(&dev_priv->global_kms_state_mutex); 2814 } 2815 2816 /** 2817 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement 2818 * property. 2819 * 2820 * @dev_priv: Pointer to a device private struct. 2821 * @immutable: Whether the property is immutable. 2822 * 2823 * Sets up the implicit placement property unless it's already set up. 2824 */ 2825 void 2826 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv, 2827 bool immutable) 2828 { 2829 if (dev_priv->implicit_placement_property) 2830 return; 2831 2832 dev_priv->implicit_placement_property = 2833 drm_property_create_range(dev_priv->dev, 2834 immutable ? 2835 DRM_MODE_PROP_IMMUTABLE : 0, 2836 "implicit_placement", 0, 1); 2837 2838 } 2839 2840 2841 /** 2842 * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config 2843 * 2844 * @set: The configuration to set. 2845 * 2846 * The vmwgfx Xorg driver doesn't assign the mode::type member, which 2847 * when drm_mode_set_crtcinfo is called as part of the configuration setting 2848 * causes it to return incorrect crtc dimensions causing severe problems in 2849 * the vmwgfx modesetting. So explicitly clear that member before calling 2850 * into drm_atomic_helper_set_config. 2851 */ 2852 int vmw_kms_set_config(struct drm_mode_set *set, 2853 struct drm_modeset_acquire_ctx *ctx) 2854 { 2855 if (set && set->mode) 2856 set->mode->type = 0; 2857 2858 return drm_atomic_helper_set_config(set, ctx); 2859 } 2860