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