vkms_crtc.c 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// SPDX-License-Identifier: GPL-2.0
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "vkms_drv.h"
#include <drm/drm_atomic_helper.h>
11
#include <drm/drm_probe_helper.h>
12

13
static void _vblank_handle(struct vkms_output *output)
14 15
{
	struct drm_crtc *crtc = &output->crtc;
16
	struct vkms_crtc_state *state = to_vkms_crtc_state(crtc->state);
17 18
	bool ret;

19
	spin_lock(&output->lock);
20 21 22 23
	ret = drm_crtc_handle_vblank(crtc);
	if (!ret)
		DRM_ERROR("vkms failure on handling vblank");

24
	if (state && output->crc_enabled) {
25 26 27 28 29 30 31 32 33 34 35 36 37
		u64 frame = drm_crtc_accurate_vblank_count(crtc);

		/* update frame_start only if a queued vkms_crc_work_handle()
		 * has read the data
		 */
		spin_lock(&output->state_lock);
		if (!state->frame_start)
			state->frame_start = frame;
		spin_unlock(&output->state_lock);

		ret = queue_work(output->crc_workq, &state->crc_work);
		if (!ret)
			DRM_WARN("failed to queue vkms_crc_work_handle");
38 39 40 41 42 43 44 45 46 47 48 49 50
	}

	spin_unlock(&output->lock);
}

static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer)
{
	struct vkms_output *output = container_of(timer, struct vkms_output,
						  vblank_hrtimer);
	int ret_overrun;

	_vblank_handle(output);

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
	ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer,
					  output->period_ns);

	return HRTIMER_RESTART;
}

static int vkms_enable_vblank(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	unsigned int pipe = drm_crtc_index(crtc);
	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);

	drm_calc_timestamping_constants(crtc, &crtc->mode);

	hrtimer_init(&out->vblank_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	out->vblank_hrtimer.function = &vkms_vblank_simulate;
	out->period_ns = ktime_set(0, vblank->framedur_ns);
	hrtimer_start(&out->vblank_hrtimer, out->period_ns, HRTIMER_MODE_REL);

	return 0;
}

static void vkms_disable_vblank(struct drm_crtc *crtc)
{
	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);

	hrtimer_cancel(&out->vblank_hrtimer);
}

bool vkms_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
			       int *max_error, ktime_t *vblank_time,
			       bool in_vblank_irq)
{
	struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
	struct vkms_output *output = &vkmsdev->output;

	*vblank_time = output->vblank_hrtimer.node.expires;

90 91 92
	if (!in_vblank_irq)
		*vblank_time -= output->period_ns;

93 94 95
	return true;
}

H
Haneen Mohammed 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109
static void vkms_atomic_crtc_reset(struct drm_crtc *crtc)
{
	struct vkms_crtc_state *vkms_state = NULL;

	if (crtc->state) {
		vkms_state = to_vkms_crtc_state(crtc->state);
		__drm_atomic_helper_crtc_destroy_state(crtc->state);
		kfree(vkms_state);
		crtc->state = NULL;
	}

	vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
	if (!vkms_state)
		return;
110
	INIT_WORK(&vkms_state->crc_work, vkms_crc_work_handle);
H
Haneen Mohammed 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

	crtc->state = &vkms_state->base;
	crtc->state->crtc = crtc;
}

static struct drm_crtc_state *
vkms_atomic_crtc_duplicate_state(struct drm_crtc *crtc)
{
	struct vkms_crtc_state *vkms_state;

	if (WARN_ON(!crtc->state))
		return NULL;

	vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
	if (!vkms_state)
		return NULL;

	__drm_atomic_helper_crtc_duplicate_state(crtc, &vkms_state->base);

130 131
	INIT_WORK(&vkms_state->crc_work, vkms_crc_work_handle);

H
Haneen Mohammed 已提交
132 133 134 135 136 137
	return &vkms_state->base;
}

static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc,
					   struct drm_crtc_state *state)
{
138
	struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state);
H
Haneen Mohammed 已提交
139 140

	__drm_atomic_helper_crtc_destroy_state(state);
141 142 143 144 145

	if (vkms_state) {
		flush_work(&vkms_state->crc_work);
		kfree(vkms_state);
	}
H
Haneen Mohammed 已提交
146 147
}

148 149 150 151
static const struct drm_crtc_funcs vkms_crtc_funcs = {
	.set_config             = drm_atomic_helper_set_config,
	.destroy                = drm_crtc_cleanup,
	.page_flip              = drm_atomic_helper_page_flip,
H
Haneen Mohammed 已提交
152 153 154
	.reset                  = vkms_atomic_crtc_reset,
	.atomic_duplicate_state = vkms_atomic_crtc_duplicate_state,
	.atomic_destroy_state   = vkms_atomic_crtc_destroy_state,
155 156
	.enable_vblank		= vkms_enable_vblank,
	.disable_vblank		= vkms_disable_vblank,
157
	.set_crc_source		= vkms_set_crc_source,
158
	.verify_crc_source	= vkms_verify_crc_source,
159 160 161 162 163 164 165 166 167 168 169 170 171 172
};

static void vkms_crtc_atomic_enable(struct drm_crtc *crtc,
				    struct drm_crtc_state *old_state)
{
	drm_crtc_vblank_on(crtc);
}

static void vkms_crtc_atomic_disable(struct drm_crtc *crtc,
				     struct drm_crtc_state *old_state)
{
	drm_crtc_vblank_off(crtc);
}

173 174 175 176 177 178 179 180 181 182 183
static void vkms_crtc_atomic_begin(struct drm_crtc *crtc,
				   struct drm_crtc_state *old_crtc_state)
{
	struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);

	/* This lock is held across the atomic commit to block vblank timer
	 * from scheduling vkms_crc_work_handle until the crc_data is updated
	 */
	spin_lock_irq(&vkms_output->lock);
}

184 185 186
static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
				   struct drm_crtc_state *old_crtc_state)
{
187
	struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
188 189 190 191 192 193 194 195 196 197 198 199 200 201
	unsigned long flags;

	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;
	}
202 203

	spin_unlock_irq(&vkms_output->lock);
204 205 206
}

static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = {
207
	.atomic_begin	= vkms_crtc_atomic_begin,
208 209 210
	.atomic_flush	= vkms_crtc_atomic_flush,
	.atomic_enable	= vkms_crtc_atomic_enable,
	.atomic_disable	= vkms_crtc_atomic_disable,
211 212 213 214 215
};

int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
		   struct drm_plane *primary, struct drm_plane *cursor)
{
216
	struct vkms_output *vkms_out = drm_crtc_to_vkms_output(crtc);
217 218 219 220 221 222 223 224 225
	int ret;

	ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
					&vkms_crtc_funcs, NULL);
	if (ret) {
		DRM_ERROR("Failed to init CRTC\n");
		return ret;
	}

226 227
	drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs);

228
	spin_lock_init(&vkms_out->lock);
229
	spin_lock_init(&vkms_out->state_lock);
230 231 232

	vkms_out->crc_workq = alloc_ordered_workqueue("vkms_crc_workq", 0);

233 234
	return ret;
}