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 return; 211 buffer[buf_len] = '\0'; 212 213 ret = kstrtobool(buffer, &vmw->vkms_enabled); 214 if (!ret && vmw->vkms_enabled) { 215 ret = drm_vblank_init(&vmw->drm, VMWGFX_NUM_DISPLAY_UNITS); 216 vmw->vkms_enabled = (ret == 0); 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 destroy_workqueue(vmw->crc_workq); 232 } 233 234 bool 235 vmw_vkms_get_vblank_timestamp(struct drm_crtc *crtc, 236 int *max_error, 237 ktime_t *vblank_time, 238 bool in_vblank_irq) 239 { 240 struct vmw_private *vmw = vmw_priv(crtc->dev); 241 242 if (!vmw->vkms_enabled) 243 return false; 244 245 drm_crtc_vblank_get_vblank_timeout(crtc, vblank_time); 246 247 return true; 248 } 249 250 int 251 vmw_vkms_enable_vblank(struct drm_crtc *crtc) 252 { 253 struct drm_device *dev = crtc->dev; 254 struct vmw_private *vmw = vmw_priv(dev); 255 256 if (!vmw->vkms_enabled) 257 return -EINVAL; 258 259 return drm_crtc_vblank_start_timer(crtc); 260 } 261 262 void 263 vmw_vkms_disable_vblank(struct drm_crtc *crtc) 264 { 265 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 266 struct vmw_private *vmw = vmw_priv(crtc->dev); 267 268 if (!vmw->vkms_enabled) 269 return; 270 271 drm_crtc_vblank_cancel_timer(crtc); 272 273 du->vkms.surface = NULL; 274 } 275 276 enum vmw_vkms_lock_state { 277 VMW_VKMS_LOCK_UNLOCKED = 0, 278 VMW_VKMS_LOCK_MODESET = 1, 279 VMW_VKMS_LOCK_VBLANK = 2 280 }; 281 282 void 283 vmw_vkms_crtc_init(struct drm_crtc *crtc) 284 { 285 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 286 287 atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED); 288 spin_lock_init(&du->vkms.crc_state_lock); 289 290 INIT_WORK(&du->vkms.crc_generator_work, crc_generate_worker); 291 du->vkms.surface = NULL; 292 } 293 294 void 295 vmw_vkms_crtc_cleanup(struct drm_crtc *crtc) 296 { 297 struct vmw_private *vmw = vmw_priv(crtc->dev); 298 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 299 300 if (vmw->vkms_enabled) 301 drm_crtc_vblank_cancel_timer(crtc); 302 303 if (du->vkms.surface) 304 vmw_surface_unreference(&du->vkms.surface); 305 WARN_ON(work_pending(&du->vkms.crc_generator_work)); 306 } 307 308 void 309 vmw_vkms_crtc_atomic_begin(struct drm_crtc *crtc, 310 struct drm_atomic_commit *state) 311 { 312 struct vmw_private *vmw = vmw_priv(crtc->dev); 313 314 if (vmw->vkms_enabled) 315 vmw_vkms_modeset_lock(crtc); 316 } 317 318 void 319 vmw_vkms_crtc_atomic_flush(struct drm_crtc *crtc, 320 struct drm_atomic_commit *state) 321 { 322 unsigned long flags; 323 struct vmw_private *vmw = vmw_priv(crtc->dev); 324 325 if (!vmw->vkms_enabled) 326 return; 327 328 if (crtc->state->event) { 329 spin_lock_irqsave(&crtc->dev->event_lock, flags); 330 331 if (drm_crtc_vblank_get(crtc) != 0) 332 drm_crtc_send_vblank_event(crtc, crtc->state->event); 333 else 334 drm_crtc_arm_vblank_event(crtc, crtc->state->event); 335 336 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 337 338 crtc->state->event = NULL; 339 } 340 341 vmw_vkms_unlock(crtc); 342 } 343 344 void 345 vmw_vkms_crtc_atomic_enable(struct drm_crtc *crtc, 346 struct drm_atomic_commit *state) 347 { 348 struct vmw_private *vmw = vmw_priv(crtc->dev); 349 350 if (vmw->vkms_enabled) 351 drm_crtc_vblank_on(crtc); 352 } 353 354 void 355 vmw_vkms_crtc_atomic_disable(struct drm_crtc *crtc, 356 struct drm_atomic_commit *state) 357 { 358 struct vmw_private *vmw = vmw_priv(crtc->dev); 359 360 if (vmw->vkms_enabled) 361 drm_crtc_vblank_off(crtc); 362 } 363 364 static bool 365 is_crc_supported(struct drm_crtc *crtc) 366 { 367 struct vmw_private *vmw = vmw_priv(crtc->dev); 368 369 if (!vmw->vkms_enabled) 370 return false; 371 372 if (vmw->active_display_unit != vmw_du_screen_target) 373 return false; 374 375 return true; 376 } 377 378 static const char * const pipe_crc_sources[] = {"auto"}; 379 380 static int 381 crc_parse_source(const char *src_name, 382 bool *enabled) 383 { 384 int ret = 0; 385 386 if (!src_name) { 387 *enabled = false; 388 } else if (strcmp(src_name, "auto") == 0) { 389 *enabled = true; 390 } else { 391 *enabled = false; 392 ret = -EINVAL; 393 } 394 395 return ret; 396 } 397 398 const char *const * 399 vmw_vkms_get_crc_sources(struct drm_crtc *crtc, 400 size_t *count) 401 { 402 *count = 0; 403 if (!is_crc_supported(crtc)) 404 return NULL; 405 406 *count = ARRAY_SIZE(pipe_crc_sources); 407 return pipe_crc_sources; 408 } 409 410 int 411 vmw_vkms_verify_crc_source(struct drm_crtc *crtc, 412 const char *src_name, 413 size_t *values_cnt) 414 { 415 bool enabled; 416 417 if (!is_crc_supported(crtc)) 418 return -EINVAL; 419 420 if (crc_parse_source(src_name, &enabled) < 0) { 421 drm_dbg_driver(crtc->dev, "unknown source '%s'\n", src_name); 422 return -EINVAL; 423 } 424 425 *values_cnt = 1; 426 427 return 0; 428 } 429 430 int 431 vmw_vkms_set_crc_source(struct drm_crtc *crtc, 432 const char *src_name) 433 { 434 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 435 bool enabled, prev_enabled, locked; 436 int ret; 437 438 if (!is_crc_supported(crtc)) 439 return -EINVAL; 440 441 ret = crc_parse_source(src_name, &enabled); 442 443 if (enabled) 444 drm_crtc_vblank_get(crtc); 445 446 locked = vmw_vkms_modeset_lock_relaxed(crtc); 447 prev_enabled = du->vkms.crc_enabled; 448 du->vkms.crc_enabled = enabled; 449 if (locked) 450 vmw_vkms_unlock(crtc); 451 452 if (prev_enabled) 453 drm_crtc_vblank_put(crtc); 454 455 return ret; 456 } 457 458 void 459 vmw_vkms_set_crc_surface(struct drm_crtc *crtc, 460 struct vmw_surface *surf) 461 { 462 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 463 struct vmw_private *vmw = vmw_priv(crtc->dev); 464 465 if (vmw->vkms_enabled && du->vkms.surface != surf) { 466 WARN_ON(atomic_read(&du->vkms.atomic_lock) != VMW_VKMS_LOCK_MODESET); 467 if (du->vkms.surface) 468 vmw_surface_unreference(&du->vkms.surface); 469 if (surf) 470 du->vkms.surface = vmw_surface_reference(surf); 471 } 472 } 473 474 /** 475 * vmw_vkms_lock_max_wait_ns - Return the max wait for the vkms lock 476 * @du: The vmw_display_unit from which to grab the vblank timings 477 * 478 * Returns the maximum wait time used to acquire the vkms lock. By 479 * default uses a time of a single frame and in case where vblank 480 * was not initialized for the display unit 1/60th of a second. 481 */ 482 static inline u64 483 vmw_vkms_lock_max_wait_ns(struct vmw_display_unit *du) 484 { 485 struct drm_crtc *crtc = &du->crtc; 486 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc); 487 488 if (!vblank || !vblank->framedur_ns) 489 return NSEC_PER_SEC / 60; /* disabled; assume 60 Hz */ 490 491 return vblank->framedur_ns; 492 } 493 494 /** 495 * vmw_vkms_modeset_lock - Protects access to crtc during modeset 496 * @crtc: The crtc to lock for vkms 497 * 498 * This function prevents the VKMS timers/callbacks from being called 499 * while a modeset operation is in process. We don't want the callbacks 500 * e.g. the vblank simulator to be trying to access incomplete state 501 * so we need to make sure they execute only when the modeset has 502 * finished. 503 * 504 * Normally this would have been done with a spinlock but locking the 505 * entire atomic modeset with vmwgfx is impossible because kms prepare 506 * executes non-atomic ops (e.g. vmw_validation_prepare holds a mutex to 507 * guard various bits of state). Which means that we need to synchronize 508 * atomic context (the vblank handler) with the non-atomic entirity 509 * of kms - so use an atomic_t to track which part of vkms has access 510 * to the basic vkms state. 511 */ 512 void 513 vmw_vkms_modeset_lock(struct drm_crtc *crtc) 514 { 515 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 516 const u64 nsecs_delay = 10; 517 const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du); 518 u64 total_delay = 0; 519 int ret; 520 521 do { 522 ret = atomic_cmpxchg(&du->vkms.atomic_lock, 523 VMW_VKMS_LOCK_UNLOCKED, 524 VMW_VKMS_LOCK_MODESET); 525 if (ret == VMW_VKMS_LOCK_UNLOCKED || total_delay >= MAX_NSECS_DELAY) 526 break; 527 ndelay(nsecs_delay); 528 total_delay += nsecs_delay; 529 } while (1); 530 531 if (total_delay >= MAX_NSECS_DELAY) { 532 drm_warn(crtc->dev, "VKMS lock expired! total_delay = %lld, ret = %d, cur = %d\n", 533 total_delay, ret, atomic_read(&du->vkms.atomic_lock)); 534 } 535 } 536 537 /** 538 * vmw_vkms_modeset_lock_relaxed - Protects access to crtc during modeset 539 * @crtc: The crtc to lock for vkms 540 * 541 * Much like vmw_vkms_modeset_lock except that when the crtc is currently 542 * in a modeset it will return immediately. 543 * 544 * Returns true if actually locked vkms to modeset or false otherwise. 545 */ 546 bool 547 vmw_vkms_modeset_lock_relaxed(struct drm_crtc *crtc) 548 { 549 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 550 const u64 nsecs_delay = 10; 551 const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du); 552 u64 total_delay = 0; 553 int ret; 554 555 do { 556 ret = atomic_cmpxchg(&du->vkms.atomic_lock, 557 VMW_VKMS_LOCK_UNLOCKED, 558 VMW_VKMS_LOCK_MODESET); 559 if (ret == VMW_VKMS_LOCK_UNLOCKED || 560 ret == VMW_VKMS_LOCK_MODESET || 561 total_delay >= MAX_NSECS_DELAY) 562 break; 563 ndelay(nsecs_delay); 564 total_delay += nsecs_delay; 565 } while (1); 566 567 if (total_delay >= MAX_NSECS_DELAY) { 568 drm_warn(crtc->dev, "VKMS relaxed lock expired!\n"); 569 return false; 570 } 571 572 return ret == VMW_VKMS_LOCK_UNLOCKED; 573 } 574 575 /** 576 * vmw_vkms_vblank_trylock - Protects access to crtc during vblank 577 * @crtc: The crtc to lock for vkms 578 * 579 * Tries to lock vkms for vblank, returns immediately. 580 * 581 * Returns true if locked vkms to vblank or false otherwise. 582 */ 583 bool 584 vmw_vkms_vblank_trylock(struct drm_crtc *crtc) 585 { 586 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 587 u32 ret; 588 589 ret = atomic_cmpxchg(&du->vkms.atomic_lock, 590 VMW_VKMS_LOCK_UNLOCKED, 591 VMW_VKMS_LOCK_VBLANK); 592 593 return ret == VMW_VKMS_LOCK_UNLOCKED; 594 } 595 596 void 597 vmw_vkms_unlock(struct drm_crtc *crtc) 598 { 599 struct vmw_display_unit *du = vmw_crtc_to_du(crtc); 600 601 /* Release flag; mark it as unlocked. */ 602 atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED); 603 } 604