1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /************************************************************************** 3 * 4 * Copyright (c) 2024 Broadcom. All Rights Reserved. The term 5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 29 #include "vmwgfx_vkms.h" 30 31 #include "vmwgfx_bo.h" 32 #include "vmwgfx_drv.h" 33 #include "vmwgfx_kms.h" 34 35 #include "vmw_surface_cache.h" 36 37 #include <drm/drm_crtc.h> 38 #include <drm/drm_debugfs_crc.h> 39 #include <drm/drm_print.h> 40 #include <drm/drm_vblank.h> 41 42 #include <linux/crc32.h> 43 #include <linux/delay.h> 44 45 #define GUESTINFO_VBLANK "guestinfo.vmwgfx.vkms_enable" 46 47 static int 48 vmw_surface_sync(struct vmw_private *vmw, 49 struct vmw_surface *surf) 50 { 51 int ret; 52 struct vmw_fence_obj *fence = NULL; 53 struct vmw_bo *bo = surf->res.guest_memory_bo; 54 55 vmw_resource_clean(&surf->res); 56 57 ret = ttm_bo_reserve(&bo->tbo, false, false, NULL); 58 if (ret != 0) { 59 drm_warn(&vmw->drm, "%s: failed reserve\n", __func__); 60 goto done; 61 } 62 63 ret = vmw_execbuf_fence_commands(NULL, vmw, &fence, NULL); 64 if (ret != 0) { 65 drm_warn(&vmw->drm, "%s: failed execbuf\n", __func__); 66 ttm_bo_unreserve(&bo->tbo); 67 goto done; 68 } 69 70 dma_fence_wait(&fence->base, false); 71 dma_fence_put(&fence->base); 72 73 ttm_bo_unreserve(&bo->tbo); 74 done: 75 return ret; 76 } 77 78 static void 79 compute_crc(struct drm_crtc *crtc, 80 struct vmw_surface *surf, 81 u32 *crc) 82 { 83 u8 *mapped_surface; 84 struct vmw_bo *bo = surf->res.guest_memory_bo; 85 const struct SVGA3dSurfaceDesc *desc = 86 vmw_surface_get_desc(surf->metadata.format); 87 u32 row_pitch_bytes; 88 SVGA3dSize blocks; 89 u32 y; 90 91 *crc = 0; 92 93 vmw_surface_get_size_in_blocks(desc, &surf->metadata.base_size, &blocks); 94 row_pitch_bytes = blocks.width * desc->pitchBytesPerBlock; 95 WARN_ON(!bo); 96 mapped_surface = vmw_bo_map_and_cache(bo); 97 98 for (y = 0; y < blocks.height; y++) { 99 *crc = crc32_le(*crc, mapped_surface, row_pitch_bytes); 100 mapped_surface += row_pitch_bytes; 101 } 102 103 vmw_bo_unmap(bo); 104 } 105 106 static void 107 crc_generate_worker(struct work_struct *work) 108 { 109 struct vmw_display_unit *du = 110 container_of(work, struct vmw_display_unit, vkms.crc_generator_work); 111 struct drm_crtc *crtc = &du->crtc; 112 struct vmw_private *vmw = vmw_priv(crtc->dev); 113 bool crc_pending; 114 u64 frame_start, frame_end; 115 u32 crc32 = 0; 116 struct vmw_surface *surf = 0; 117 118 spin_lock_irq(&du->vkms.crc_state_lock); 119 crc_pending = du->vkms.crc_pending; 120 spin_unlock_irq(&du->vkms.crc_state_lock); 121 122 /* 123 * We raced with the vblank hrtimer and previous work already computed 124 * the crc, nothing to do. 125 */ 126 if (!crc_pending) 127 return; 128 129 spin_lock_irq(&du->vkms.crc_state_lock); 130 surf = vmw_surface_reference(du->vkms.surface); 131 spin_unlock_irq(&du->vkms.crc_state_lock); 132 133 if (surf) { 134 if (vmw_surface_sync(vmw, surf)) { 135 drm_warn( 136 crtc->dev, 137 "CRC worker wasn't able to sync the crc surface!\n"); 138 return; 139 } 140 141 compute_crc(crtc, surf, &crc32); 142 vmw_surface_unreference(&surf); 143 } 144 145 spin_lock_irq(&du->vkms.crc_state_lock); 146 frame_start = du->vkms.frame_start; 147 frame_end = du->vkms.frame_end; 148 du->vkms.frame_start = 0; 149 du->vkms.frame_end = 0; 150 du->vkms.crc_pending = false; 151 spin_unlock_irq(&du->vkms.crc_state_lock); 152 153 /* 154 * The worker can fall behind the vblank hrtimer, make sure we catch up. 155 */ 156 while (frame_start <= frame_end) 157 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32); 158 } 159 160 bool 161 vmw_vkms_handle_vblank_timeout(struct drm_crtc *crtc) 162 { 163 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 164 struct vmw_private *vmw = vmw_priv(crtc->dev); 165 bool has_surface = false; 166 bool locked, ret; 167 168 locked = vmw_vkms_vblank_trylock(crtc); 169 ret = drm_crtc_handle_vblank(crtc); 170 WARN_ON(!ret); 171 if (!locked) 172 return true; 173 has_surface = du->vkms.surface != NULL; 174 vmw_vkms_unlock(crtc); 175 176 if (du->vkms.crc_enabled && has_surface) { 177 u64 frame = drm_crtc_accurate_vblank_count(crtc); 178 179 spin_lock(&du->vkms.crc_state_lock); 180 if (!du->vkms.crc_pending) 181 du->vkms.frame_start = frame; 182 else 183 drm_dbg_driver(crtc->dev, 184 "crc worker falling behind, frame_start: %llu, frame_end: %llu\n", 185 du->vkms.frame_start, frame); 186 du->vkms.frame_end = frame; 187 du->vkms.crc_pending = true; 188 spin_unlock(&du->vkms.crc_state_lock); 189 190 ret = queue_work(vmw->crc_workq, &du->vkms.crc_generator_work); 191 if (!ret) 192 drm_dbg_driver(crtc->dev, "Composer worker already queued\n"); 193 } 194 195 return true; 196 } 197 198 void 199 vmw_vkms_init(struct vmw_private *vmw) 200 { 201 char buffer[64]; 202 const size_t max_buf_len = sizeof(buffer) - 1; 203 size_t buf_len = max_buf_len; 204 int ret; 205 206 vmw->vkms_enabled = false; 207 208 ret = vmw_host_get_guestinfo(GUESTINFO_VBLANK, buffer, &buf_len); 209 if (!ret && buf_len <= max_buf_len) { 210 buffer[buf_len] = '\0'; 211 212 ret = kstrtobool(buffer, &vmw->vkms_enabled); 213 if (!ret && vmw->vkms_enabled) { 214 ret = drm_vblank_init(&vmw->drm, VMWGFX_NUM_DISPLAY_UNITS); 215 vmw->vkms_enabled = (ret == 0); 216 } 217 } 218 219 vmw->crc_workq = alloc_ordered_workqueue("vmwgfx_crc_generator", 0); 220 if (!vmw->crc_workq) { 221 drm_warn(&vmw->drm, "crc workqueue allocation failed. Disabling vkms."); 222 vmw->vkms_enabled = false; 223 } 224 if (vmw->vkms_enabled) 225 drm_info(&vmw->drm, "VKMS enabled\n"); 226 } 227 228 void 229 vmw_vkms_cleanup(struct vmw_private *vmw) 230 { 231 if (vmw->crc_workq) 232 destroy_workqueue(vmw->crc_workq); 233 } 234 235 bool 236 vmw_vkms_get_vblank_timestamp(struct drm_crtc *crtc, 237 int *max_error, 238 ktime_t *vblank_time, 239 bool in_vblank_irq) 240 { 241 struct vmw_private *vmw = vmw_priv(crtc->dev); 242 243 if (!vmw->vkms_enabled) 244 return false; 245 246 drm_crtc_vblank_get_vblank_timeout(crtc, vblank_time); 247 248 return true; 249 } 250 251 int 252 vmw_vkms_enable_vblank(struct drm_crtc *crtc) 253 { 254 struct drm_device *dev = crtc->dev; 255 struct vmw_private *vmw = vmw_priv(dev); 256 257 if (!vmw->vkms_enabled) 258 return -EINVAL; 259 260 return drm_crtc_vblank_start_timer(crtc); 261 } 262 263 void 264 vmw_vkms_disable_vblank(struct drm_crtc *crtc) 265 { 266 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 267 struct vmw_private *vmw = vmw_priv(crtc->dev); 268 269 if (!vmw->vkms_enabled) 270 return; 271 272 drm_crtc_vblank_cancel_timer(crtc); 273 274 du->vkms.surface = NULL; 275 } 276 277 enum vmw_vkms_lock_state { 278 VMW_VKMS_LOCK_UNLOCKED = 0, 279 VMW_VKMS_LOCK_MODESET = 1, 280 VMW_VKMS_LOCK_VBLANK = 2 281 }; 282 283 void 284 vmw_vkms_crtc_init(struct drm_crtc *crtc) 285 { 286 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 287 288 atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED); 289 spin_lock_init(&du->vkms.crc_state_lock); 290 291 INIT_WORK(&du->vkms.crc_generator_work, crc_generate_worker); 292 du->vkms.surface = NULL; 293 } 294 295 void 296 vmw_vkms_crtc_cleanup(struct drm_crtc *crtc) 297 { 298 struct vmw_private *vmw = vmw_priv(crtc->dev); 299 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 300 301 if (vmw->vkms_enabled) 302 drm_crtc_vblank_cancel_timer(crtc); 303 304 if (du->vkms.surface) 305 vmw_surface_unreference(&du->vkms.surface); 306 WARN_ON(work_pending(&du->vkms.crc_generator_work)); 307 } 308 309 void 310 vmw_vkms_crtc_atomic_begin(struct drm_crtc *crtc, 311 struct drm_atomic_commit *state) 312 { 313 struct vmw_private *vmw = vmw_priv(crtc->dev); 314 315 if (vmw->vkms_enabled) 316 vmw_vkms_modeset_lock(crtc); 317 } 318 319 void 320 vmw_vkms_crtc_atomic_flush(struct drm_crtc *crtc, 321 struct drm_atomic_commit *state) 322 { 323 unsigned long flags; 324 struct vmw_private *vmw = vmw_priv(crtc->dev); 325 326 if (!vmw->vkms_enabled) 327 return; 328 329 if (crtc->state->event) { 330 spin_lock_irqsave(&crtc->dev->event_lock, flags); 331 332 if (drm_crtc_vblank_get(crtc) != 0) 333 drm_crtc_send_vblank_event(crtc, crtc->state->event); 334 else 335 drm_crtc_arm_vblank_event(crtc, crtc->state->event); 336 337 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 338 339 crtc->state->event = NULL; 340 } 341 342 vmw_vkms_unlock(crtc); 343 } 344 345 void 346 vmw_vkms_crtc_atomic_enable(struct drm_crtc *crtc, 347 struct drm_atomic_commit *state) 348 { 349 struct vmw_private *vmw = vmw_priv(crtc->dev); 350 351 if (vmw->vkms_enabled) 352 drm_crtc_vblank_on(crtc); 353 } 354 355 void 356 vmw_vkms_crtc_atomic_disable(struct drm_crtc *crtc, 357 struct drm_atomic_commit *state) 358 { 359 struct vmw_private *vmw = vmw_priv(crtc->dev); 360 361 if (vmw->vkms_enabled) 362 drm_crtc_vblank_off(crtc); 363 } 364 365 static bool 366 is_crc_supported(struct drm_crtc *crtc) 367 { 368 struct vmw_private *vmw = vmw_priv(crtc->dev); 369 370 if (!vmw->vkms_enabled) 371 return false; 372 373 if (vmw->active_display_unit != vmw_du_screen_target) 374 return false; 375 376 return true; 377 } 378 379 static const char * const pipe_crc_sources[] = {"auto"}; 380 381 static int 382 crc_parse_source(const char *src_name, 383 bool *enabled) 384 { 385 int ret = 0; 386 387 if (!src_name) { 388 *enabled = false; 389 } else if (strcmp(src_name, "auto") == 0) { 390 *enabled = true; 391 } else { 392 *enabled = false; 393 ret = -EINVAL; 394 } 395 396 return ret; 397 } 398 399 const char *const * 400 vmw_vkms_get_crc_sources(struct drm_crtc *crtc, 401 size_t *count) 402 { 403 *count = 0; 404 if (!is_crc_supported(crtc)) 405 return NULL; 406 407 *count = ARRAY_SIZE(pipe_crc_sources); 408 return pipe_crc_sources; 409 } 410 411 int 412 vmw_vkms_verify_crc_source(struct drm_crtc *crtc, 413 const char *src_name, 414 size_t *values_cnt) 415 { 416 bool enabled; 417 418 if (!is_crc_supported(crtc)) 419 return -EINVAL; 420 421 if (crc_parse_source(src_name, &enabled) < 0) { 422 drm_dbg_driver(crtc->dev, "unknown source '%s'\n", src_name); 423 return -EINVAL; 424 } 425 426 *values_cnt = 1; 427 428 return 0; 429 } 430 431 int 432 vmw_vkms_set_crc_source(struct drm_crtc *crtc, 433 const char *src_name) 434 { 435 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 436 bool enabled, prev_enabled, locked; 437 int ret; 438 439 if (!is_crc_supported(crtc)) 440 return -EINVAL; 441 442 ret = crc_parse_source(src_name, &enabled); 443 444 if (enabled) 445 drm_crtc_vblank_get(crtc); 446 447 locked = vmw_vkms_modeset_lock_relaxed(crtc); 448 prev_enabled = du->vkms.crc_enabled; 449 du->vkms.crc_enabled = enabled; 450 if (locked) 451 vmw_vkms_unlock(crtc); 452 453 if (prev_enabled) 454 drm_crtc_vblank_put(crtc); 455 456 return ret; 457 } 458 459 void 460 vmw_vkms_set_crc_surface(struct drm_crtc *crtc, 461 struct vmw_surface *surf) 462 { 463 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 464 struct vmw_private *vmw = vmw_priv(crtc->dev); 465 466 if (vmw->vkms_enabled && du->vkms.surface != surf) { 467 WARN_ON(atomic_read(&du->vkms.atomic_lock) != VMW_VKMS_LOCK_MODESET); 468 if (du->vkms.surface) 469 vmw_surface_unreference(&du->vkms.surface); 470 if (surf) 471 du->vkms.surface = vmw_surface_reference(surf); 472 } 473 } 474 475 /** 476 * vmw_vkms_lock_max_wait_ns - Return the max wait for the vkms lock 477 * @du: The vmw_display_unit from which to grab the vblank timings 478 * 479 * Returns the maximum wait time used to acquire the vkms lock. By 480 * default uses a time of a single frame and in case where vblank 481 * was not initialized for the display unit 1/60th of a second. 482 */ 483 static inline u64 484 vmw_vkms_lock_max_wait_ns(struct vmw_display_unit *du) 485 { 486 struct drm_crtc *crtc = &du->crtc; 487 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc); 488 489 if (!vblank || !vblank->framedur_ns) 490 return NSEC_PER_SEC / 60; /* disabled; assume 60 Hz */ 491 492 return vblank->framedur_ns; 493 } 494 495 /** 496 * vmw_vkms_modeset_lock - Protects access to crtc during modeset 497 * @crtc: The crtc to lock for vkms 498 * 499 * This function prevents the VKMS timers/callbacks from being called 500 * while a modeset operation is in process. We don't want the callbacks 501 * e.g. the vblank simulator to be trying to access incomplete state 502 * so we need to make sure they execute only when the modeset has 503 * finished. 504 * 505 * Normally this would have been done with a spinlock but locking the 506 * entire atomic modeset with vmwgfx is impossible because kms prepare 507 * executes non-atomic ops (e.g. vmw_validation_prepare holds a mutex to 508 * guard various bits of state). Which means that we need to synchronize 509 * atomic context (the vblank handler) with the non-atomic entirity 510 * of kms - so use an atomic_t to track which part of vkms has access 511 * to the basic vkms state. 512 */ 513 void 514 vmw_vkms_modeset_lock(struct drm_crtc *crtc) 515 { 516 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 517 const u64 nsecs_delay = 10; 518 const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du); 519 u64 total_delay = 0; 520 int ret; 521 522 do { 523 ret = atomic_cmpxchg(&du->vkms.atomic_lock, 524 VMW_VKMS_LOCK_UNLOCKED, 525 VMW_VKMS_LOCK_MODESET); 526 if (ret == VMW_VKMS_LOCK_UNLOCKED || total_delay >= MAX_NSECS_DELAY) 527 break; 528 ndelay(nsecs_delay); 529 total_delay += nsecs_delay; 530 } while (1); 531 532 if (total_delay >= MAX_NSECS_DELAY) { 533 drm_warn(crtc->dev, "VKMS lock expired! total_delay = %lld, ret = %d, cur = %d\n", 534 total_delay, ret, atomic_read(&du->vkms.atomic_lock)); 535 } 536 } 537 538 /** 539 * vmw_vkms_modeset_lock_relaxed - Protects access to crtc during modeset 540 * @crtc: The crtc to lock for vkms 541 * 542 * Much like vmw_vkms_modeset_lock except that when the crtc is currently 543 * in a modeset it will return immediately. 544 * 545 * Returns true if actually locked vkms to modeset or false otherwise. 546 */ 547 bool 548 vmw_vkms_modeset_lock_relaxed(struct drm_crtc *crtc) 549 { 550 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 551 const u64 nsecs_delay = 10; 552 const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du); 553 u64 total_delay = 0; 554 int ret; 555 556 do { 557 ret = atomic_cmpxchg(&du->vkms.atomic_lock, 558 VMW_VKMS_LOCK_UNLOCKED, 559 VMW_VKMS_LOCK_MODESET); 560 if (ret == VMW_VKMS_LOCK_UNLOCKED || 561 ret == VMW_VKMS_LOCK_MODESET || 562 total_delay >= MAX_NSECS_DELAY) 563 break; 564 ndelay(nsecs_delay); 565 total_delay += nsecs_delay; 566 } while (1); 567 568 if (total_delay >= MAX_NSECS_DELAY) { 569 drm_warn(crtc->dev, "VKMS relaxed lock expired!\n"); 570 return false; 571 } 572 573 return ret == VMW_VKMS_LOCK_UNLOCKED; 574 } 575 576 /** 577 * vmw_vkms_vblank_trylock - Protects access to crtc during vblank 578 * @crtc: The crtc to lock for vkms 579 * 580 * Tries to lock vkms for vblank, returns immediately. 581 * 582 * Returns true if locked vkms to vblank or false otherwise. 583 */ 584 bool 585 vmw_vkms_vblank_trylock(struct drm_crtc *crtc) 586 { 587 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 588 u32 ret; 589 590 ret = atomic_cmpxchg(&du->vkms.atomic_lock, 591 VMW_VKMS_LOCK_UNLOCKED, 592 VMW_VKMS_LOCK_VBLANK); 593 594 return ret == VMW_VKMS_LOCK_UNLOCKED; 595 } 596 597 void 598 vmw_vkms_unlock(struct drm_crtc *crtc) 599 { 600 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 601 602 /* Release flag; mark it as unlocked. */ 603 atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED); 604 } 605