summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/vmwgfx/vmwgfx_vkms.c
blob: 8651b788e98bc41f9dbba4d68f56e734defdf76e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// SPDX-License-Identifier: GPL-2.0 OR MIT
/**************************************************************************
 *
 * Copyright (c) 2024 Broadcom. All Rights Reserved. The term
 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "vmwgfx_vkms.h"

#include "vmwgfx_bo.h"
#include "vmwgfx_drv.h"
#include "vmwgfx_kms.h"

#include "vmw_surface_cache.h"

#include <drm/drm_crtc.h>
#include <drm/drm_debugfs_crc.h>
#include <drm/drm_print.h>
#include <drm/drm_vblank.h>

#include <linux/crc32.h>
#include <linux/delay.h>

#define GUESTINFO_VBLANK  "guestinfo.vmwgfx.vkms_enable"

static int
vmw_surface_sync(struct vmw_private *vmw,
		 struct vmw_surface *surf)
{
	int ret;
	struct vmw_fence_obj *fence = NULL;
	struct vmw_bo *bo = surf->res.guest_memory_bo;

	vmw_resource_clean(&surf->res);

	ret = ttm_bo_reserve(&bo->tbo, false, false, NULL);
	if (ret != 0) {
		drm_warn(&vmw->drm, "%s: failed reserve\n", __func__);
		goto done;
	}

	ret = vmw_execbuf_fence_commands(NULL, vmw, &fence, NULL);
	if (ret != 0) {
		drm_warn(&vmw->drm, "%s: failed execbuf\n", __func__);
		ttm_bo_unreserve(&bo->tbo);
		goto done;
	}

	dma_fence_wait(&fence->base, false);
	dma_fence_put(&fence->base);

	ttm_bo_unreserve(&bo->tbo);
done:
	return ret;
}

static void
compute_crc(struct drm_crtc *crtc,
	    struct vmw_surface *surf,
	    u32 *crc)
{
	u8 *mapped_surface;
	struct vmw_bo *bo = surf->res.guest_memory_bo;
	const struct SVGA3dSurfaceDesc *desc =
		vmw_surface_get_desc(surf->metadata.format);
	u32 row_pitch_bytes;
	SVGA3dSize blocks;
	u32 y;

	*crc = 0;

	vmw_surface_get_size_in_blocks(desc, &surf->metadata.base_size, &blocks);
	row_pitch_bytes = blocks.width * desc->pitchBytesPerBlock;
	WARN_ON(!bo);
	mapped_surface = vmw_bo_map_and_cache(bo);

	for (y = 0; y < blocks.height; y++) {
		*crc = crc32_le(*crc, mapped_surface, row_pitch_bytes);
		mapped_surface += row_pitch_bytes;
	}

	vmw_bo_unmap(bo);
}

static void
crc_generate_worker(struct work_struct *work)
{
	struct vmw_display_unit *du =
		container_of(work, struct vmw_display_unit, vkms.crc_generator_work);
	struct drm_crtc *crtc = &du->crtc;
	struct vmw_private *vmw = vmw_priv(crtc->dev);
	bool crc_pending;
	u64 frame_start, frame_end;
	u32 crc32 = 0;
	struct vmw_surface *surf = 0;

	spin_lock_irq(&du->vkms.crc_state_lock);
	crc_pending = du->vkms.crc_pending;
	spin_unlock_irq(&du->vkms.crc_state_lock);

	/*
	 * We raced with the vblank hrtimer and previous work already computed
	 * the crc, nothing to do.
	 */
	if (!crc_pending)
		return;

	spin_lock_irq(&du->vkms.crc_state_lock);
	surf = vmw_surface_reference(du->vkms.surface);
	spin_unlock_irq(&du->vkms.crc_state_lock);

	if (surf) {
		if (vmw_surface_sync(vmw, surf)) {
			drm_warn(
				crtc->dev,
				"CRC worker wasn't able to sync the crc surface!\n");
			return;
		}

		compute_crc(crtc, surf, &crc32);
		vmw_surface_unreference(&surf);
	}

	spin_lock_irq(&du->vkms.crc_state_lock);
	frame_start = du->vkms.frame_start;
	frame_end = du->vkms.frame_end;
	du->vkms.frame_start = 0;
	du->vkms.frame_end = 0;
	du->vkms.crc_pending = false;
	spin_unlock_irq(&du->vkms.crc_state_lock);

	/*
	 * The worker can fall behind the vblank hrtimer, make sure we catch up.
	 */
	while (frame_start <= frame_end)
		drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
}

static enum hrtimer_restart
vmw_vkms_vblank_simulate(struct hrtimer *timer)
{
	struct vmw_display_unit *du = container_of(timer, struct vmw_display_unit, vkms.timer);
	struct drm_crtc *crtc = &du->crtc;
	struct vmw_private *vmw = vmw_priv(crtc->dev);
	bool has_surface = false;
	u64 ret_overrun;
	bool locked, ret;

	ret_overrun = hrtimer_forward_now(&du->vkms.timer,
					  du->vkms.period_ns);
	if (ret_overrun != 1)
		drm_dbg_driver(crtc->dev, "vblank timer missed %lld frames.\n",
			       ret_overrun - 1);

	locked = vmw_vkms_vblank_trylock(crtc);
	ret = drm_crtc_handle_vblank(crtc);
	WARN_ON(!ret);
	if (!locked)
		return HRTIMER_RESTART;
	has_surface = du->vkms.surface != NULL;
	vmw_vkms_unlock(crtc);

	if (du->vkms.crc_enabled && has_surface) {
		u64 frame = drm_crtc_accurate_vblank_count(crtc);

		spin_lock(&du->vkms.crc_state_lock);
		if (!du->vkms.crc_pending)
			du->vkms.frame_start = frame;
		else
			drm_dbg_driver(crtc->dev,
				       "crc worker falling behind, frame_start: %llu, frame_end: %llu\n",
				       du->vkms.frame_start, frame);
		du->vkms.frame_end = frame;
		du->vkms.crc_pending = true;
		spin_unlock(&du->vkms.crc_state_lock);

		ret = queue_work(vmw->crc_workq, &du->vkms.crc_generator_work);
		if (!ret)
			drm_dbg_driver(crtc->dev, "Composer worker already queued\n");
	}

	return HRTIMER_RESTART;
}

void
vmw_vkms_init(struct vmw_private *vmw)
{
	char buffer[64];
	const size_t max_buf_len = sizeof(buffer) - 1;
	size_t buf_len = max_buf_len;
	int ret;

	vmw->vkms_enabled = false;

	ret = vmw_host_get_guestinfo(GUESTINFO_VBLANK, buffer, &buf_len);
	if (ret || buf_len > max_buf_len)
		return;
	buffer[buf_len] = '\0';

	ret = kstrtobool(buffer, &vmw->vkms_enabled);
	if (!ret && vmw->vkms_enabled) {
		ret = drm_vblank_init(&vmw->drm, VMWGFX_NUM_DISPLAY_UNITS);
		vmw->vkms_enabled = (ret == 0);
	}

	vmw->crc_workq = alloc_ordered_workqueue("vmwgfx_crc_generator", 0);
	if (!vmw->crc_workq) {
		drm_warn(&vmw->drm, "crc workqueue allocation failed. Disabling vkms.");
		vmw->vkms_enabled = false;
	}
	if (vmw->vkms_enabled)
		drm_info(&vmw->drm, "VKMS enabled\n");
}

void
vmw_vkms_cleanup(struct vmw_private *vmw)
{
	destroy_workqueue(vmw->crc_workq);
}

bool
vmw_vkms_get_vblank_timestamp(struct drm_crtc *crtc,
			      int *max_error,
			      ktime_t *vblank_time,
			      bool in_vblank_irq)
{
	struct drm_device *dev = crtc->dev;
	struct vmw_private *vmw = vmw_priv(dev);
	unsigned int pipe = crtc->index;
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];

	if (!vmw->vkms_enabled)
		return false;

	if (!READ_ONCE(vblank->enabled)) {
		*vblank_time = ktime_get();
		return true;
	}

	*vblank_time = READ_ONCE(du->vkms.timer.node.expires);

	if (WARN_ON(*vblank_time == vblank->time))
		return true;

	/*
	 * To prevent races we roll the hrtimer forward before we do any
	 * interrupt processing - this is how real hw works (the interrupt is
	 * only generated after all the vblank registers are updated) and what
	 * the vblank core expects. Therefore we need to always correct the
	 * timestampe by one frame.
	 */
	*vblank_time -= du->vkms.period_ns;

	return true;
}

int
vmw_vkms_enable_vblank(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	struct vmw_private *vmw = vmw_priv(dev);
	unsigned int pipe = drm_crtc_index(crtc);
	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);

	if (!vmw->vkms_enabled)
		return -EINVAL;

	drm_calc_timestamping_constants(crtc, &crtc->mode);

	hrtimer_init(&du->vkms.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	du->vkms.timer.function = &vmw_vkms_vblank_simulate;
	du->vkms.period_ns = ktime_set(0, vblank->framedur_ns);
	hrtimer_start(&du->vkms.timer, du->vkms.period_ns, HRTIMER_MODE_REL);

	return 0;
}

void
vmw_vkms_disable_vblank(struct drm_crtc *crtc)
{
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
	struct vmw_private *vmw = vmw_priv(crtc->dev);

	if (!vmw->vkms_enabled)
		return;

	hrtimer_cancel(&du->vkms.timer);
	du->vkms.surface = NULL;
	du->vkms.period_ns = ktime_set(0, 0);
}

enum vmw_vkms_lock_state {
	VMW_VKMS_LOCK_UNLOCKED     = 0,
	VMW_VKMS_LOCK_MODESET      = 1,
	VMW_VKMS_LOCK_VBLANK       = 2
};

void
vmw_vkms_crtc_init(struct drm_crtc *crtc)
{
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);

	atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED);
	spin_lock_init(&du->vkms.crc_state_lock);

	INIT_WORK(&du->vkms.crc_generator_work, crc_generate_worker);
	du->vkms.surface = NULL;
}

void
vmw_vkms_crtc_cleanup(struct drm_crtc *crtc)
{
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);

	if (du->vkms.surface)
		vmw_surface_unreference(&du->vkms.surface);
	WARN_ON(work_pending(&du->vkms.crc_generator_work));
	hrtimer_cancel(&du->vkms.timer);
}

void
vmw_vkms_crtc_atomic_begin(struct drm_crtc *crtc,
			   struct drm_atomic_state *state)
{
	struct vmw_private *vmw = vmw_priv(crtc->dev);

	if (vmw->vkms_enabled)
		vmw_vkms_modeset_lock(crtc);
}

void
vmw_vkms_crtc_atomic_flush(struct drm_crtc *crtc,
			   struct drm_atomic_state *state)
{
	unsigned long flags;
	struct vmw_private *vmw = vmw_priv(crtc->dev);

	if (!vmw->vkms_enabled)
		return;

	if (crtc->state->event) {
		spin_lock_irqsave(&crtc->dev->event_lock, flags);

		if (drm_crtc_vblank_get(crtc) != 0)
			drm_crtc_send_vblank_event(crtc, crtc->state->event);
		else
			drm_crtc_arm_vblank_event(crtc, crtc->state->event);

		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);

		crtc->state->event = NULL;
	}

	vmw_vkms_unlock(crtc);
}

void
vmw_vkms_crtc_atomic_enable(struct drm_crtc *crtc,
			    struct drm_atomic_state *state)
{
	struct vmw_private *vmw = vmw_priv(crtc->dev);

	if (vmw->vkms_enabled)
		drm_crtc_vblank_on(crtc);
}

void
vmw_vkms_crtc_atomic_disable(struct drm_crtc *crtc,
			     struct drm_atomic_state *state)
{
	struct vmw_private *vmw = vmw_priv(crtc->dev);

	if (vmw->vkms_enabled)
		drm_crtc_vblank_off(crtc);
}

static bool
is_crc_supported(struct drm_crtc *crtc)
{
	struct vmw_private *vmw = vmw_priv(crtc->dev);

	if (!vmw->vkms_enabled)
		return false;

	if (vmw->active_display_unit != vmw_du_screen_target)
		return false;

	return true;
}

static const char * const pipe_crc_sources[] = {"auto"};

static int
crc_parse_source(const char *src_name,
		 bool *enabled)
{
	int ret = 0;

	if (!src_name) {
		*enabled = false;
	} else if (strcmp(src_name, "auto") == 0) {
		*enabled = true;
	} else {
		*enabled = false;
		ret = -EINVAL;
	}

	return ret;
}

const char *const *
vmw_vkms_get_crc_sources(struct drm_crtc *crtc,
			 size_t *count)
{
	*count = 0;
	if (!is_crc_supported(crtc))
		return NULL;

	*count = ARRAY_SIZE(pipe_crc_sources);
	return pipe_crc_sources;
}

int
vmw_vkms_verify_crc_source(struct drm_crtc *crtc,
			   const char *src_name,
			   size_t *values_cnt)
{
	bool enabled;

	if (!is_crc_supported(crtc))
		return -EINVAL;

	if (crc_parse_source(src_name, &enabled) < 0) {
		drm_dbg_driver(crtc->dev, "unknown source '%s'\n", src_name);
		return -EINVAL;
	}

	*values_cnt = 1;

	return 0;
}

int
vmw_vkms_set_crc_source(struct drm_crtc *crtc,
			const char *src_name)
{
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
	bool enabled, prev_enabled, locked;
	int ret;

	if (!is_crc_supported(crtc))
		return -EINVAL;

	ret = crc_parse_source(src_name, &enabled);

	if (enabled)
		drm_crtc_vblank_get(crtc);

	locked = vmw_vkms_modeset_lock_relaxed(crtc);
	prev_enabled = du->vkms.crc_enabled;
	du->vkms.crc_enabled = enabled;
	if (locked)
		vmw_vkms_unlock(crtc);

	if (prev_enabled)
		drm_crtc_vblank_put(crtc);

	return ret;
}

void
vmw_vkms_set_crc_surface(struct drm_crtc *crtc,
			 struct vmw_surface *surf)
{
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
	struct vmw_private *vmw = vmw_priv(crtc->dev);

	if (vmw->vkms_enabled && du->vkms.surface != surf) {
		WARN_ON(atomic_read(&du->vkms.atomic_lock) != VMW_VKMS_LOCK_MODESET);
		if (du->vkms.surface)
			vmw_surface_unreference(&du->vkms.surface);
		if (surf)
			du->vkms.surface = vmw_surface_reference(surf);
	}
}

/**
 * vmw_vkms_lock_max_wait_ns - Return the max wait for the vkms lock
 * @du: The vmw_display_unit from which to grab the vblank timings
 *
 * Returns the maximum wait time used to acquire the vkms lock. By
 * default uses a time of a single frame and in case where vblank
 * was not initialized for the display unit 1/60th of a second.
 */
static inline u64
vmw_vkms_lock_max_wait_ns(struct vmw_display_unit *du)
{
	s64 nsecs = ktime_to_ns(du->vkms.period_ns);

	return  (nsecs > 0) ? nsecs : 16666666;
}

/**
 * vmw_vkms_modeset_lock - Protects access to crtc during modeset
 * @crtc: The crtc to lock for vkms
 *
 * This function prevents the VKMS timers/callbacks from being called
 * while a modeset operation is in process. We don't want the callbacks
 * e.g. the vblank simulator to be trying to access incomplete state
 * so we need to make sure they execute only when the modeset has
 * finished.
 *
 * Normally this would have been done with a spinlock but locking the
 * entire atomic modeset with vmwgfx is impossible because kms prepare
 * executes non-atomic ops (e.g. vmw_validation_prepare holds a mutex to
 * guard various bits of state). Which means that we need to synchronize
 * atomic context (the vblank handler) with the non-atomic entirity
 * of kms - so use an atomic_t to track which part of vkms has access
 * to the basic vkms state.
 */
void
vmw_vkms_modeset_lock(struct drm_crtc *crtc)
{
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
	const u64 nsecs_delay = 10;
	const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du);
	u64 total_delay = 0;
	int ret;

	do {
		ret = atomic_cmpxchg(&du->vkms.atomic_lock,
				     VMW_VKMS_LOCK_UNLOCKED,
				     VMW_VKMS_LOCK_MODESET);
		if (ret == VMW_VKMS_LOCK_UNLOCKED || total_delay >= MAX_NSECS_DELAY)
			break;
		ndelay(nsecs_delay);
		total_delay += nsecs_delay;
	} while (1);

	if (total_delay >= MAX_NSECS_DELAY) {
		drm_warn(crtc->dev, "VKMS lock expired! total_delay = %lld, ret = %d, cur = %d\n",
			 total_delay, ret, atomic_read(&du->vkms.atomic_lock));
	}
}

/**
 * vmw_vkms_modeset_lock_relaxed - Protects access to crtc during modeset
 * @crtc: The crtc to lock for vkms
 *
 * Much like vmw_vkms_modeset_lock except that when the crtc is currently
 * in a modeset it will return immediately.
 *
 * Returns true if actually locked vkms to modeset or false otherwise.
 */
bool
vmw_vkms_modeset_lock_relaxed(struct drm_crtc *crtc)
{
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
	const u64 nsecs_delay = 10;
	const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du);
	u64 total_delay = 0;
	int ret;

	do {
		ret = atomic_cmpxchg(&du->vkms.atomic_lock,
				     VMW_VKMS_LOCK_UNLOCKED,
				     VMW_VKMS_LOCK_MODESET);
		if (ret == VMW_VKMS_LOCK_UNLOCKED ||
		    ret == VMW_VKMS_LOCK_MODESET ||
		    total_delay >= MAX_NSECS_DELAY)
			break;
		ndelay(nsecs_delay);
		total_delay += nsecs_delay;
	} while (1);

	if (total_delay >= MAX_NSECS_DELAY) {
		drm_warn(crtc->dev, "VKMS relaxed lock expired!\n");
		return false;
	}

	return ret == VMW_VKMS_LOCK_UNLOCKED;
}

/**
 * vmw_vkms_vblank_trylock - Protects access to crtc during vblank
 * @crtc: The crtc to lock for vkms
 *
 * Tries to lock vkms for vblank, returns immediately.
 *
 * Returns true if locked vkms to vblank or false otherwise.
 */
bool
vmw_vkms_vblank_trylock(struct drm_crtc *crtc)
{
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
	u32 ret;

	ret = atomic_cmpxchg(&du->vkms.atomic_lock,
			     VMW_VKMS_LOCK_UNLOCKED,
			     VMW_VKMS_LOCK_VBLANK);

	return ret == VMW_VKMS_LOCK_UNLOCKED;
}

void
vmw_vkms_unlock(struct drm_crtc *crtc)
{
	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);

	/* Release flag; mark it as unlocked. */
	atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED);
}