xref: /linux/drivers/gpu/drm/vmwgfx/vmwgfx_vkms.c (revision 8e1bb4a41aa78d6105e59186af3dcd545fc66e70)
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 int
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 	return 0;
106 }
107 
108 static void
109 crc_generate_worker(struct work_struct *work)
110 {
111 	struct vmw_display_unit *du =
112 		container_of(work, struct vmw_display_unit, vkms.crc_generator_work);
113 	struct drm_crtc *crtc = &du->crtc;
114 	struct vmw_private *vmw = vmw_priv(crtc->dev);
115 	bool crc_pending;
116 	u64 frame_start, frame_end;
117 	u32 crc32 = 0;
118 	struct vmw_surface *surf = 0;
119 	int ret;
120 
121 	spin_lock_irq(&du->vkms.crc_state_lock);
122 	crc_pending = du->vkms.crc_pending;
123 	spin_unlock_irq(&du->vkms.crc_state_lock);
124 
125 	/*
126 	 * We raced with the vblank hrtimer and previous work already computed
127 	 * the crc, nothing to do.
128 	 */
129 	if (!crc_pending)
130 		return;
131 
132 	spin_lock_irq(&du->vkms.crc_state_lock);
133 	surf = du->vkms.surface;
134 	spin_unlock_irq(&du->vkms.crc_state_lock);
135 
136 	if (vmw_surface_sync(vmw, surf)) {
137 		drm_warn(crtc->dev, "CRC worker wasn't able to sync the crc surface!\n");
138 		return;
139 	}
140 
141 	ret = compute_crc(crtc, surf, &crc32);
142 	if (ret)
143 		return;
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 	crc_pending = du->vkms.crc_pending;
149 	du->vkms.frame_start = 0;
150 	du->vkms.frame_end = 0;
151 	du->vkms.crc_pending = false;
152 	spin_unlock_irq(&du->vkms.crc_state_lock);
153 
154 	/*
155 	 * The worker can fall behind the vblank hrtimer, make sure we catch up.
156 	 */
157 	while (frame_start <= frame_end)
158 		drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
159 }
160 
161 static enum hrtimer_restart
162 vmw_vkms_vblank_simulate(struct hrtimer *timer)
163 {
164 	struct vmw_display_unit *du = container_of(timer, struct vmw_display_unit, vkms.timer);
165 	struct drm_crtc *crtc = &du->crtc;
166 	struct vmw_private *vmw = vmw_priv(crtc->dev);
167 	struct vmw_surface *surf = NULL;
168 	u64 ret_overrun;
169 	bool locked, ret;
170 
171 	ret_overrun = hrtimer_forward_now(&du->vkms.timer,
172 					  du->vkms.period_ns);
173 	if (ret_overrun != 1)
174 		drm_dbg_driver(crtc->dev, "vblank timer missed %lld frames.\n",
175 			       ret_overrun - 1);
176 
177 	locked = vmw_vkms_vblank_trylock(crtc);
178 	ret = drm_crtc_handle_vblank(crtc);
179 	WARN_ON(!ret);
180 	if (!locked)
181 		return HRTIMER_RESTART;
182 	surf = du->vkms.surface;
183 	vmw_vkms_unlock(crtc);
184 
185 	if (du->vkms.crc_enabled && surf) {
186 		u64 frame = drm_crtc_accurate_vblank_count(crtc);
187 
188 		spin_lock(&du->vkms.crc_state_lock);
189 		if (!du->vkms.crc_pending)
190 			du->vkms.frame_start = frame;
191 		else
192 			drm_dbg_driver(crtc->dev,
193 				       "crc worker falling behind, frame_start: %llu, frame_end: %llu\n",
194 				       du->vkms.frame_start, frame);
195 		du->vkms.frame_end = frame;
196 		du->vkms.crc_pending = true;
197 		spin_unlock(&du->vkms.crc_state_lock);
198 
199 		ret = queue_work(vmw->crc_workq, &du->vkms.crc_generator_work);
200 		if (!ret)
201 			drm_dbg_driver(crtc->dev, "Composer worker already queued\n");
202 	}
203 
204 	return HRTIMER_RESTART;
205 }
206 
207 void
208 vmw_vkms_init(struct vmw_private *vmw)
209 {
210 	char buffer[64];
211 	const size_t max_buf_len = sizeof(buffer) - 1;
212 	size_t buf_len = max_buf_len;
213 	int ret;
214 
215 	vmw->vkms_enabled = false;
216 
217 	ret = vmw_host_get_guestinfo(GUESTINFO_VBLANK, buffer, &buf_len);
218 	if (ret || buf_len > max_buf_len)
219 		return;
220 	buffer[buf_len] = '\0';
221 
222 	ret = kstrtobool(buffer, &vmw->vkms_enabled);
223 	if (!ret && vmw->vkms_enabled) {
224 		ret = drm_vblank_init(&vmw->drm, VMWGFX_NUM_DISPLAY_UNITS);
225 		vmw->vkms_enabled = (ret == 0);
226 	}
227 
228 	vmw->crc_workq = alloc_ordered_workqueue("vmwgfx_crc_generator", 0);
229 	if (!vmw->crc_workq) {
230 		drm_warn(&vmw->drm, "crc workqueue allocation failed. Disabling vkms.");
231 		vmw->vkms_enabled = false;
232 	}
233 	if (vmw->vkms_enabled)
234 		drm_info(&vmw->drm, "VKMS enabled\n");
235 }
236 
237 void
238 vmw_vkms_cleanup(struct vmw_private *vmw)
239 {
240 	destroy_workqueue(vmw->crc_workq);
241 }
242 
243 bool
244 vmw_vkms_get_vblank_timestamp(struct drm_crtc *crtc,
245 			      int *max_error,
246 			      ktime_t *vblank_time,
247 			      bool in_vblank_irq)
248 {
249 	struct drm_device *dev = crtc->dev;
250 	struct vmw_private *vmw = vmw_priv(dev);
251 	unsigned int pipe = crtc->index;
252 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
253 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
254 
255 	if (!vmw->vkms_enabled)
256 		return false;
257 
258 	if (!READ_ONCE(vblank->enabled)) {
259 		*vblank_time = ktime_get();
260 		return true;
261 	}
262 
263 	*vblank_time = READ_ONCE(du->vkms.timer.node.expires);
264 
265 	if (WARN_ON(*vblank_time == vblank->time))
266 		return true;
267 
268 	/*
269 	 * To prevent races we roll the hrtimer forward before we do any
270 	 * interrupt processing - this is how real hw works (the interrupt is
271 	 * only generated after all the vblank registers are updated) and what
272 	 * the vblank core expects. Therefore we need to always correct the
273 	 * timestampe by one frame.
274 	 */
275 	*vblank_time -= du->vkms.period_ns;
276 
277 	return true;
278 }
279 
280 int
281 vmw_vkms_enable_vblank(struct drm_crtc *crtc)
282 {
283 	struct drm_device *dev = crtc->dev;
284 	struct vmw_private *vmw = vmw_priv(dev);
285 	unsigned int pipe = drm_crtc_index(crtc);
286 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
287 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
288 
289 	if (!vmw->vkms_enabled)
290 		return -EINVAL;
291 
292 	drm_calc_timestamping_constants(crtc, &crtc->mode);
293 
294 	hrtimer_init(&du->vkms.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
295 	du->vkms.timer.function = &vmw_vkms_vblank_simulate;
296 	du->vkms.period_ns = ktime_set(0, vblank->framedur_ns);
297 	hrtimer_start(&du->vkms.timer, du->vkms.period_ns, HRTIMER_MODE_REL);
298 
299 	return 0;
300 }
301 
302 void
303 vmw_vkms_disable_vblank(struct drm_crtc *crtc)
304 {
305 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
306 	struct vmw_private *vmw = vmw_priv(crtc->dev);
307 
308 	if (!vmw->vkms_enabled)
309 		return;
310 
311 	hrtimer_cancel(&du->vkms.timer);
312 	du->vkms.surface = NULL;
313 	du->vkms.period_ns = ktime_set(0, 0);
314 }
315 
316 enum vmw_vkms_lock_state {
317 	VMW_VKMS_LOCK_UNLOCKED     = 0,
318 	VMW_VKMS_LOCK_MODESET      = 1,
319 	VMW_VKMS_LOCK_VBLANK       = 2
320 };
321 
322 void
323 vmw_vkms_crtc_init(struct drm_crtc *crtc)
324 {
325 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
326 
327 	atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED);
328 	spin_lock_init(&du->vkms.crc_state_lock);
329 
330 	INIT_WORK(&du->vkms.crc_generator_work, crc_generate_worker);
331 	du->vkms.surface = NULL;
332 }
333 
334 void
335 vmw_vkms_crtc_cleanup(struct drm_crtc *crtc)
336 {
337 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
338 
339 	WARN_ON(work_pending(&du->vkms.crc_generator_work));
340 	hrtimer_cancel(&du->vkms.timer);
341 }
342 
343 void
344 vmw_vkms_crtc_atomic_begin(struct drm_crtc *crtc,
345 			   struct drm_atomic_state *state)
346 {
347 	struct vmw_private *vmw = vmw_priv(crtc->dev);
348 
349 	if (vmw->vkms_enabled)
350 		vmw_vkms_modeset_lock(crtc);
351 }
352 
353 void
354 vmw_vkms_crtc_atomic_flush(struct drm_crtc *crtc,
355 			   struct drm_atomic_state *state)
356 {
357 	unsigned long flags;
358 	struct vmw_private *vmw = vmw_priv(crtc->dev);
359 
360 	if (!vmw->vkms_enabled)
361 		return;
362 
363 	if (crtc->state->event) {
364 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
365 
366 		if (drm_crtc_vblank_get(crtc) != 0)
367 			drm_crtc_send_vblank_event(crtc, crtc->state->event);
368 		else
369 			drm_crtc_arm_vblank_event(crtc, crtc->state->event);
370 
371 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
372 
373 		crtc->state->event = NULL;
374 	}
375 
376 	vmw_vkms_unlock(crtc);
377 }
378 
379 void
380 vmw_vkms_crtc_atomic_enable(struct drm_crtc *crtc,
381 			    struct drm_atomic_state *state)
382 {
383 	struct vmw_private *vmw = vmw_priv(crtc->dev);
384 
385 	if (vmw->vkms_enabled)
386 		drm_crtc_vblank_on(crtc);
387 }
388 
389 void
390 vmw_vkms_crtc_atomic_disable(struct drm_crtc *crtc,
391 			     struct drm_atomic_state *state)
392 {
393 	struct vmw_private *vmw = vmw_priv(crtc->dev);
394 
395 	if (vmw->vkms_enabled)
396 		drm_crtc_vblank_off(crtc);
397 }
398 
399 static bool
400 is_crc_supported(struct drm_crtc *crtc)
401 {
402 	struct vmw_private *vmw = vmw_priv(crtc->dev);
403 
404 	if (!vmw->vkms_enabled)
405 		return false;
406 
407 	if (vmw->active_display_unit != vmw_du_screen_target)
408 		return false;
409 
410 	return true;
411 }
412 
413 static const char * const pipe_crc_sources[] = {"auto"};
414 
415 static int
416 crc_parse_source(const char *src_name,
417 		 bool *enabled)
418 {
419 	int ret = 0;
420 
421 	if (!src_name) {
422 		*enabled = false;
423 	} else if (strcmp(src_name, "auto") == 0) {
424 		*enabled = true;
425 	} else {
426 		*enabled = false;
427 		ret = -EINVAL;
428 	}
429 
430 	return ret;
431 }
432 
433 const char *const *
434 vmw_vkms_get_crc_sources(struct drm_crtc *crtc,
435 			 size_t *count)
436 {
437 	*count = 0;
438 	if (!is_crc_supported(crtc))
439 		return NULL;
440 
441 	*count = ARRAY_SIZE(pipe_crc_sources);
442 	return pipe_crc_sources;
443 }
444 
445 int
446 vmw_vkms_verify_crc_source(struct drm_crtc *crtc,
447 			   const char *src_name,
448 			   size_t *values_cnt)
449 {
450 	bool enabled;
451 
452 	if (!is_crc_supported(crtc))
453 		return -EINVAL;
454 
455 	if (crc_parse_source(src_name, &enabled) < 0) {
456 		drm_dbg_driver(crtc->dev, "unknown source '%s'\n", src_name);
457 		return -EINVAL;
458 	}
459 
460 	*values_cnt = 1;
461 
462 	return 0;
463 }
464 
465 int
466 vmw_vkms_set_crc_source(struct drm_crtc *crtc,
467 			const char *src_name)
468 {
469 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
470 	bool enabled, prev_enabled, locked;
471 	int ret;
472 
473 	if (!is_crc_supported(crtc))
474 		return -EINVAL;
475 
476 	ret = crc_parse_source(src_name, &enabled);
477 
478 	if (enabled)
479 		drm_crtc_vblank_get(crtc);
480 
481 	locked = vmw_vkms_modeset_lock_relaxed(crtc);
482 	prev_enabled = du->vkms.crc_enabled;
483 	du->vkms.crc_enabled = enabled;
484 	if (locked)
485 		vmw_vkms_unlock(crtc);
486 
487 	if (prev_enabled)
488 		drm_crtc_vblank_put(crtc);
489 
490 	return ret;
491 }
492 
493 void
494 vmw_vkms_set_crc_surface(struct drm_crtc *crtc,
495 			 struct vmw_surface *surf)
496 {
497 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
498 	struct vmw_private *vmw = vmw_priv(crtc->dev);
499 
500 	if (vmw->vkms_enabled) {
501 		WARN_ON(atomic_read(&du->vkms.atomic_lock) != VMW_VKMS_LOCK_MODESET);
502 		du->vkms.surface = surf;
503 	}
504 }
505 
506 /**
507  * vmw_vkms_lock_max_wait_ns - Return the max wait for the vkms lock
508  * @du: The vmw_display_unit from which to grab the vblank timings
509  *
510  * Returns the maximum wait time used to acquire the vkms lock. By
511  * default uses a time of a single frame and in case where vblank
512  * was not initialized for the display unit 1/60th of a second.
513  */
514 static inline u64
515 vmw_vkms_lock_max_wait_ns(struct vmw_display_unit *du)
516 {
517 	s64 nsecs = ktime_to_ns(du->vkms.period_ns);
518 
519 	return  (nsecs > 0) ? nsecs : 16666666;
520 }
521 
522 /**
523  * vmw_vkms_modeset_lock - Protects access to crtc during modeset
524  * @crtc: The crtc to lock for vkms
525  *
526  * This function prevents the VKMS timers/callbacks from being called
527  * while a modeset operation is in process. We don't want the callbacks
528  * e.g. the vblank simulator to be trying to access incomplete state
529  * so we need to make sure they execute only when the modeset has
530  * finished.
531  *
532  * Normally this would have been done with a spinlock but locking the
533  * entire atomic modeset with vmwgfx is impossible because kms prepare
534  * executes non-atomic ops (e.g. vmw_validation_prepare holds a mutex to
535  * guard various bits of state). Which means that we need to synchronize
536  * atomic context (the vblank handler) with the non-atomic entirity
537  * of kms - so use an atomic_t to track which part of vkms has access
538  * to the basic vkms state.
539  */
540 void
541 vmw_vkms_modeset_lock(struct drm_crtc *crtc)
542 {
543 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
544 	const u64 nsecs_delay = 10;
545 	const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du);
546 	u64 total_delay = 0;
547 	int ret;
548 
549 	do {
550 		ret = atomic_cmpxchg(&du->vkms.atomic_lock,
551 				     VMW_VKMS_LOCK_UNLOCKED,
552 				     VMW_VKMS_LOCK_MODESET);
553 		if (ret == VMW_VKMS_LOCK_UNLOCKED || total_delay >= MAX_NSECS_DELAY)
554 			break;
555 		ndelay(nsecs_delay);
556 		total_delay += nsecs_delay;
557 	} while (1);
558 
559 	if (total_delay >= MAX_NSECS_DELAY) {
560 		drm_warn(crtc->dev, "VKMS lock expired! total_delay = %lld, ret = %d, cur = %d\n",
561 			 total_delay, ret, atomic_read(&du->vkms.atomic_lock));
562 	}
563 }
564 
565 /**
566  * vmw_vkms_modeset_lock_relaxed - Protects access to crtc during modeset
567  * @crtc: The crtc to lock for vkms
568  *
569  * Much like vmw_vkms_modeset_lock except that when the crtc is currently
570  * in a modeset it will return immediately.
571  *
572  * Returns true if actually locked vkms to modeset or false otherwise.
573  */
574 bool
575 vmw_vkms_modeset_lock_relaxed(struct drm_crtc *crtc)
576 {
577 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
578 	const u64 nsecs_delay = 10;
579 	const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du);
580 	u64 total_delay = 0;
581 	int ret;
582 
583 	do {
584 		ret = atomic_cmpxchg(&du->vkms.atomic_lock,
585 				     VMW_VKMS_LOCK_UNLOCKED,
586 				     VMW_VKMS_LOCK_MODESET);
587 		if (ret == VMW_VKMS_LOCK_UNLOCKED ||
588 		    ret == VMW_VKMS_LOCK_MODESET ||
589 		    total_delay >= MAX_NSECS_DELAY)
590 			break;
591 		ndelay(nsecs_delay);
592 		total_delay += nsecs_delay;
593 	} while (1);
594 
595 	if (total_delay >= MAX_NSECS_DELAY) {
596 		drm_warn(crtc->dev, "VKMS relaxed lock expired!\n");
597 		return false;
598 	}
599 
600 	return ret == VMW_VKMS_LOCK_UNLOCKED;
601 }
602 
603 /**
604  * vmw_vkms_vblank_trylock - Protects access to crtc during vblank
605  * @crtc: The crtc to lock for vkms
606  *
607  * Tries to lock vkms for vblank, returns immediately.
608  *
609  * Returns true if locked vkms to vblank or false otherwise.
610  */
611 bool
612 vmw_vkms_vblank_trylock(struct drm_crtc *crtc)
613 {
614 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
615 	u32 ret;
616 
617 	ret = atomic_cmpxchg(&du->vkms.atomic_lock,
618 			     VMW_VKMS_LOCK_UNLOCKED,
619 			     VMW_VKMS_LOCK_VBLANK);
620 
621 	return ret == VMW_VKMS_LOCK_UNLOCKED;
622 }
623 
624 void
625 vmw_vkms_unlock(struct drm_crtc *crtc)
626 {
627 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
628 
629 	/* Release flag; mark it as unlocked. */
630 	atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED);
631 }
632