1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /************************************************************************** 3 * 4 * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24 * USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 #include "vmwgfx_bo.h" 29 #include "vmwgfx_kms.h" 30 #include "vmwgfx_vkms.h" 31 32 #include <drm/drm_atomic.h> 33 #include <drm/drm_atomic_helper.h> 34 #include <drm/drm_fourcc.h> 35 36 37 #define vmw_crtc_to_ldu(x) \ 38 container_of(x, struct vmw_legacy_display_unit, base.crtc) 39 #define vmw_encoder_to_ldu(x) \ 40 container_of(x, struct vmw_legacy_display_unit, base.encoder) 41 #define vmw_connector_to_ldu(x) \ 42 container_of(x, struct vmw_legacy_display_unit, base.connector) 43 44 struct vmw_legacy_display { 45 struct list_head active; 46 47 unsigned num_active; 48 unsigned last_num_active; 49 50 struct vmw_framebuffer *fb; 51 }; 52 53 /* 54 * Display unit using the legacy register interface. 55 */ 56 struct vmw_legacy_display_unit { 57 struct vmw_display_unit base; 58 59 struct list_head active; 60 }; 61 62 static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu) 63 { 64 list_del_init(&ldu->active); 65 vmw_du_cleanup(&ldu->base); 66 kfree(ldu); 67 } 68 69 70 /* 71 * Legacy Display Unit CRTC functions 72 */ 73 74 static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc) 75 { 76 vmw_ldu_destroy(vmw_crtc_to_ldu(crtc)); 77 } 78 79 static int vmw_ldu_commit_list(struct vmw_private *dev_priv) 80 { 81 struct vmw_legacy_display *lds = dev_priv->ldu_priv; 82 struct vmw_legacy_display_unit *entry; 83 struct drm_framebuffer *fb = NULL; 84 struct drm_crtc *crtc = NULL; 85 int i; 86 87 /* If there is no display topology the host just assumes 88 * that the guest will set the same layout as the host. 89 */ 90 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) { 91 int w = 0, h = 0; 92 list_for_each_entry(entry, &lds->active, active) { 93 crtc = &entry->base.crtc; 94 w = max(w, crtc->x + crtc->mode.hdisplay); 95 h = max(h, crtc->y + crtc->mode.vdisplay); 96 } 97 98 if (crtc == NULL) 99 return 0; 100 fb = crtc->primary->state->fb; 101 102 return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0], 103 fb->format->cpp[0] * 8, 104 fb->format->depth); 105 } 106 107 if (!list_empty(&lds->active)) { 108 entry = list_entry(lds->active.next, typeof(*entry), active); 109 fb = entry->base.crtc.primary->state->fb; 110 111 vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0], 112 fb->format->cpp[0] * 8, fb->format->depth); 113 } 114 115 /* Make sure we always show something. */ 116 vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS, 117 lds->num_active ? lds->num_active : 1); 118 119 i = 0; 120 list_for_each_entry(entry, &lds->active, active) { 121 crtc = &entry->base.crtc; 122 123 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i); 124 vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i); 125 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x); 126 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y); 127 vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay); 128 vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay); 129 130 i++; 131 } 132 133 BUG_ON(i != lds->num_active); 134 135 lds->last_num_active = lds->num_active; 136 137 return 0; 138 } 139 140 /* 141 * Pin the buffer in a location suitable for access by the 142 * display system. 143 */ 144 static int vmw_ldu_fb_pin(struct vmw_framebuffer *vfb) 145 { 146 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev); 147 struct vmw_bo *buf; 148 int ret; 149 150 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer : 151 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.guest_memory_bo; 152 153 if (!buf) 154 return 0; 155 WARN_ON(dev_priv->active_display_unit != vmw_du_legacy); 156 157 if (dev_priv->active_display_unit == vmw_du_legacy) { 158 vmw_overlay_pause_all(dev_priv); 159 ret = vmw_bo_pin_in_start_of_vram(dev_priv, buf, false); 160 vmw_overlay_resume_all(dev_priv); 161 } else 162 ret = -EINVAL; 163 164 return ret; 165 } 166 167 static int vmw_ldu_fb_unpin(struct vmw_framebuffer *vfb) 168 { 169 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev); 170 struct vmw_bo *buf; 171 172 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer : 173 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.guest_memory_bo; 174 175 if (WARN_ON(!buf)) 176 return 0; 177 178 return vmw_bo_unpin(dev_priv, buf, false); 179 } 180 181 static int vmw_ldu_del_active(struct vmw_private *vmw_priv, 182 struct vmw_legacy_display_unit *ldu) 183 { 184 struct vmw_legacy_display *ld = vmw_priv->ldu_priv; 185 if (list_empty(&ldu->active)) 186 return 0; 187 188 /* Must init otherwise list_empty(&ldu->active) will not work. */ 189 list_del_init(&ldu->active); 190 if (--(ld->num_active) == 0) { 191 BUG_ON(!ld->fb); 192 WARN_ON(vmw_ldu_fb_unpin(ld->fb)); 193 ld->fb = NULL; 194 } 195 196 return 0; 197 } 198 199 static int vmw_ldu_add_active(struct vmw_private *vmw_priv, 200 struct vmw_legacy_display_unit *ldu, 201 struct vmw_framebuffer *vfb) 202 { 203 struct vmw_legacy_display *ld = vmw_priv->ldu_priv; 204 struct vmw_legacy_display_unit *entry; 205 struct list_head *at; 206 207 BUG_ON(!ld->num_active && ld->fb); 208 if (vfb != ld->fb) { 209 if (ld->fb) 210 WARN_ON(vmw_ldu_fb_unpin(ld->fb)); 211 vmw_svga_enable(vmw_priv); 212 WARN_ON(vmw_ldu_fb_pin(vfb)); 213 ld->fb = vfb; 214 } 215 216 if (!list_empty(&ldu->active)) 217 return 0; 218 219 at = &ld->active; 220 list_for_each_entry(entry, &ld->active, active) { 221 if (entry->base.unit > ldu->base.unit) 222 break; 223 224 at = &entry->active; 225 } 226 227 list_add(&ldu->active, at); 228 229 ld->num_active++; 230 231 return 0; 232 } 233 234 /** 235 * vmw_ldu_crtc_mode_set_nofb - Enable svga 236 * 237 * @crtc: CRTC associated with the new screen 238 * 239 * For LDU, just enable the svga 240 */ 241 static void vmw_ldu_crtc_mode_set_nofb(struct drm_crtc *crtc) 242 { 243 } 244 245 static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = { 246 .gamma_set = vmw_du_crtc_gamma_set, 247 .destroy = vmw_ldu_crtc_destroy, 248 .reset = vmw_du_crtc_reset, 249 .atomic_duplicate_state = vmw_du_crtc_duplicate_state, 250 .atomic_destroy_state = vmw_du_crtc_destroy_state, 251 .set_config = drm_atomic_helper_set_config, 252 .page_flip = drm_atomic_helper_page_flip, 253 .enable_vblank = vmw_vkms_enable_vblank, 254 .disable_vblank = vmw_vkms_disable_vblank, 255 .get_vblank_timestamp = vmw_vkms_get_vblank_timestamp, 256 }; 257 258 259 /* 260 * Legacy Display Unit encoder functions 261 */ 262 263 static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder) 264 { 265 vmw_ldu_destroy(vmw_encoder_to_ldu(encoder)); 266 } 267 268 static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = { 269 .destroy = vmw_ldu_encoder_destroy, 270 }; 271 272 /* 273 * Legacy Display Unit connector functions 274 */ 275 276 static void vmw_ldu_connector_destroy(struct drm_connector *connector) 277 { 278 vmw_ldu_destroy(vmw_connector_to_ldu(connector)); 279 } 280 281 static const struct drm_connector_funcs vmw_legacy_connector_funcs = { 282 .dpms = vmw_du_connector_dpms, 283 .detect = vmw_du_connector_detect, 284 .fill_modes = drm_helper_probe_single_connector_modes, 285 .destroy = vmw_ldu_connector_destroy, 286 .reset = vmw_du_connector_reset, 287 .atomic_duplicate_state = vmw_du_connector_duplicate_state, 288 .atomic_destroy_state = vmw_du_connector_destroy_state, 289 }; 290 291 static const struct 292 drm_connector_helper_funcs vmw_ldu_connector_helper_funcs = { 293 .get_modes = vmw_connector_get_modes, 294 .mode_valid = vmw_connector_mode_valid 295 }; 296 297 static int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv, 298 struct vmw_framebuffer *framebuffer, 299 unsigned int flags, unsigned int color, 300 struct drm_mode_rect *clips, 301 unsigned int num_clips); 302 303 /* 304 * Legacy Display Plane Functions 305 */ 306 307 static void 308 vmw_ldu_primary_plane_atomic_update(struct drm_plane *plane, 309 struct drm_atomic_state *state) 310 { 311 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, 312 plane); 313 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 314 plane); 315 struct vmw_private *dev_priv; 316 struct vmw_legacy_display_unit *ldu; 317 struct vmw_framebuffer *vfb; 318 struct drm_framebuffer *fb; 319 struct drm_crtc *crtc = new_state->crtc ?: old_state->crtc; 320 321 ldu = vmw_crtc_to_ldu(crtc); 322 dev_priv = vmw_priv(plane->dev); 323 fb = new_state->fb; 324 325 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL; 326 327 if (vfb) 328 vmw_ldu_add_active(dev_priv, ldu, vfb); 329 else 330 vmw_ldu_del_active(dev_priv, ldu); 331 332 vmw_ldu_commit_list(dev_priv); 333 334 if (vfb && vmw_cmd_supported(dev_priv)) { 335 struct drm_mode_rect fb_rect = { 336 .x1 = 0, 337 .y1 = 0, 338 .x2 = vfb->base.width, 339 .y2 = vfb->base.height 340 }; 341 struct drm_mode_rect *damage_rects = drm_plane_get_damage_clips(new_state); 342 u32 rect_count = drm_plane_get_damage_clips_count(new_state); 343 int ret; 344 345 if (!damage_rects) { 346 damage_rects = &fb_rect; 347 rect_count = 1; 348 } 349 350 ret = vmw_kms_ldu_do_bo_dirty(dev_priv, vfb, 0, 0, damage_rects, rect_count); 351 352 drm_WARN_ONCE(plane->dev, ret, 353 "vmw_kms_ldu_do_bo_dirty failed with: ret=%d\n", ret); 354 355 vmw_cmd_flush(dev_priv, false); 356 } 357 } 358 359 static const struct drm_plane_funcs vmw_ldu_plane_funcs = { 360 .update_plane = drm_atomic_helper_update_plane, 361 .disable_plane = drm_atomic_helper_disable_plane, 362 .destroy = vmw_du_primary_plane_destroy, 363 .reset = vmw_du_plane_reset, 364 .atomic_duplicate_state = vmw_du_plane_duplicate_state, 365 .atomic_destroy_state = vmw_du_plane_destroy_state, 366 }; 367 368 static const struct drm_plane_funcs vmw_ldu_cursor_funcs = { 369 .update_plane = drm_atomic_helper_update_plane, 370 .disable_plane = drm_atomic_helper_disable_plane, 371 .destroy = vmw_du_cursor_plane_destroy, 372 .reset = vmw_du_plane_reset, 373 .atomic_duplicate_state = vmw_du_plane_duplicate_state, 374 .atomic_destroy_state = vmw_du_plane_destroy_state, 375 }; 376 377 /* 378 * Atomic Helpers 379 */ 380 static const struct 381 drm_plane_helper_funcs vmw_ldu_cursor_plane_helper_funcs = { 382 .atomic_check = vmw_du_cursor_plane_atomic_check, 383 .atomic_update = vmw_du_cursor_plane_atomic_update, 384 .prepare_fb = vmw_du_cursor_plane_prepare_fb, 385 .cleanup_fb = vmw_du_cursor_plane_cleanup_fb, 386 }; 387 388 static const struct 389 drm_plane_helper_funcs vmw_ldu_primary_plane_helper_funcs = { 390 .atomic_check = vmw_du_primary_plane_atomic_check, 391 .atomic_update = vmw_ldu_primary_plane_atomic_update, 392 }; 393 394 static const struct drm_crtc_helper_funcs vmw_ldu_crtc_helper_funcs = { 395 .mode_set_nofb = vmw_ldu_crtc_mode_set_nofb, 396 .atomic_check = vmw_du_crtc_atomic_check, 397 .atomic_begin = vmw_du_crtc_atomic_begin, 398 .atomic_flush = vmw_vkms_crtc_atomic_flush, 399 .atomic_enable = vmw_vkms_crtc_atomic_enable, 400 .atomic_disable = vmw_vkms_crtc_atomic_disable, 401 }; 402 403 404 static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit) 405 { 406 struct vmw_legacy_display_unit *ldu; 407 struct drm_device *dev = &dev_priv->drm; 408 struct drm_connector *connector; 409 struct drm_encoder *encoder; 410 struct drm_plane *primary; 411 struct vmw_cursor_plane *cursor; 412 struct drm_crtc *crtc; 413 int ret; 414 415 ldu = kzalloc(sizeof(*ldu), GFP_KERNEL); 416 if (!ldu) 417 return -ENOMEM; 418 419 ldu->base.unit = unit; 420 crtc = &ldu->base.crtc; 421 encoder = &ldu->base.encoder; 422 connector = &ldu->base.connector; 423 primary = &ldu->base.primary; 424 cursor = &ldu->base.cursor; 425 426 INIT_LIST_HEAD(&ldu->active); 427 428 ldu->base.pref_active = (unit == 0); 429 ldu->base.pref_width = dev_priv->initial_width; 430 ldu->base.pref_height = dev_priv->initial_height; 431 432 /* 433 * Remove this after enabling atomic because property values can 434 * only exist in a state object 435 */ 436 ldu->base.is_implicit = true; 437 438 /* Initialize primary plane */ 439 ret = drm_universal_plane_init(dev, primary, 440 0, &vmw_ldu_plane_funcs, 441 vmw_primary_plane_formats, 442 ARRAY_SIZE(vmw_primary_plane_formats), 443 NULL, DRM_PLANE_TYPE_PRIMARY, NULL); 444 if (ret) { 445 DRM_ERROR("Failed to initialize primary plane"); 446 goto err_free; 447 } 448 449 drm_plane_helper_add(primary, &vmw_ldu_primary_plane_helper_funcs); 450 451 /* 452 * We're going to be using traces and software cursors 453 */ 454 if (vmw_cmd_supported(dev_priv)) { 455 /* Initialize cursor plane */ 456 ret = drm_universal_plane_init(dev, &cursor->base, 457 0, &vmw_ldu_cursor_funcs, 458 vmw_cursor_plane_formats, 459 ARRAY_SIZE(vmw_cursor_plane_formats), 460 NULL, DRM_PLANE_TYPE_CURSOR, NULL); 461 if (ret) { 462 DRM_ERROR("Failed to initialize cursor plane"); 463 drm_plane_cleanup(&ldu->base.primary); 464 goto err_free; 465 } 466 467 drm_plane_helper_add(&cursor->base, &vmw_ldu_cursor_plane_helper_funcs); 468 } 469 470 ret = drm_connector_init(dev, connector, &vmw_legacy_connector_funcs, 471 DRM_MODE_CONNECTOR_VIRTUAL); 472 if (ret) { 473 DRM_ERROR("Failed to initialize connector\n"); 474 goto err_free; 475 } 476 477 drm_connector_helper_add(connector, &vmw_ldu_connector_helper_funcs); 478 connector->status = vmw_du_connector_detect(connector, true); 479 480 ret = drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs, 481 DRM_MODE_ENCODER_VIRTUAL, NULL); 482 if (ret) { 483 DRM_ERROR("Failed to initialize encoder\n"); 484 goto err_free_connector; 485 } 486 487 (void) drm_connector_attach_encoder(connector, encoder); 488 encoder->possible_crtcs = (1 << unit); 489 encoder->possible_clones = 0; 490 491 ret = drm_connector_register(connector); 492 if (ret) { 493 DRM_ERROR("Failed to register connector\n"); 494 goto err_free_encoder; 495 } 496 497 ret = drm_crtc_init_with_planes(dev, crtc, primary, 498 vmw_cmd_supported(dev_priv) ? &cursor->base : NULL, 499 &vmw_legacy_crtc_funcs, NULL); 500 if (ret) { 501 DRM_ERROR("Failed to initialize CRTC\n"); 502 goto err_free_unregister; 503 } 504 505 drm_crtc_helper_add(crtc, &vmw_ldu_crtc_helper_funcs); 506 507 drm_mode_crtc_set_gamma_size(crtc, 256); 508 509 drm_object_attach_property(&connector->base, 510 dev_priv->hotplug_mode_update_property, 1); 511 drm_object_attach_property(&connector->base, 512 dev->mode_config.suggested_x_property, 0); 513 drm_object_attach_property(&connector->base, 514 dev->mode_config.suggested_y_property, 0); 515 if (dev_priv->implicit_placement_property) 516 drm_object_attach_property 517 (&connector->base, 518 dev_priv->implicit_placement_property, 519 1); 520 521 vmw_du_init(&ldu->base); 522 523 return 0; 524 525 err_free_unregister: 526 drm_connector_unregister(connector); 527 err_free_encoder: 528 drm_encoder_cleanup(encoder); 529 err_free_connector: 530 drm_connector_cleanup(connector); 531 err_free: 532 kfree(ldu); 533 return ret; 534 } 535 536 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv) 537 { 538 struct drm_device *dev = &dev_priv->drm; 539 int i, ret; 540 int num_display_units = (dev_priv->capabilities & SVGA_CAP_MULTIMON) ? 541 VMWGFX_NUM_DISPLAY_UNITS : 1; 542 543 if (unlikely(dev_priv->ldu_priv)) { 544 return -EINVAL; 545 } 546 547 dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL); 548 if (!dev_priv->ldu_priv) 549 return -ENOMEM; 550 551 INIT_LIST_HEAD(&dev_priv->ldu_priv->active); 552 dev_priv->ldu_priv->num_active = 0; 553 dev_priv->ldu_priv->last_num_active = 0; 554 dev_priv->ldu_priv->fb = NULL; 555 556 vmw_kms_create_implicit_placement_property(dev_priv); 557 558 for (i = 0; i < num_display_units; ++i) { 559 ret = vmw_ldu_init(dev_priv, i); 560 if (ret != 0) 561 goto err_free; 562 } 563 564 dev_priv->active_display_unit = vmw_du_legacy; 565 566 drm_mode_config_reset(dev); 567 568 return 0; 569 570 err_free: 571 kfree(dev_priv->ldu_priv); 572 dev_priv->ldu_priv = NULL; 573 return ret; 574 } 575 576 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv) 577 { 578 if (!dev_priv->ldu_priv) 579 return -ENOSYS; 580 581 BUG_ON(!list_empty(&dev_priv->ldu_priv->active)); 582 583 kfree(dev_priv->ldu_priv); 584 585 return 0; 586 } 587 588 589 static int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv, 590 struct vmw_framebuffer *framebuffer, 591 unsigned int flags, unsigned int color, 592 struct drm_mode_rect *clips, 593 unsigned int num_clips) 594 { 595 size_t fifo_size; 596 int i; 597 598 struct { 599 uint32_t header; 600 SVGAFifoCmdUpdate body; 601 } *cmd; 602 603 fifo_size = sizeof(*cmd) * num_clips; 604 cmd = VMW_CMD_RESERVE(dev_priv, fifo_size); 605 if (unlikely(cmd == NULL)) 606 return -ENOMEM; 607 608 memset(cmd, 0, fifo_size); 609 for (i = 0; i < num_clips; i++, clips++) { 610 cmd[i].header = SVGA_CMD_UPDATE; 611 cmd[i].body.x = clips->x1; 612 cmd[i].body.y = clips->y1; 613 cmd[i].body.width = clips->x2 - clips->x1; 614 cmd[i].body.height = clips->y2 - clips->y1; 615 } 616 617 vmw_cmd_commit(dev_priv, fifo_size); 618 return 0; 619 } 620