1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This program is free software; you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation; either version 2 of the License, or 6 * (at your option) any later version. 7 */ 8 9 #include "vkms_drv.h" 10 #include <drm/drm_atomic_helper.h> 11 #include <drm/drm_crtc_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 bool ret; 18 19 spin_lock(&output->lock); 20 ret = drm_crtc_handle_vblank(crtc); 21 if (!ret) 22 DRM_ERROR("vkms failure on handling vblank"); 23 24 if (state && output->crc_enabled) { 25 state->n_frame = drm_crtc_accurate_vblank_count(crtc); 26 queue_work(output->crc_workq, &state->crc_work); 27 } 28 29 spin_unlock(&output->lock); 30 } 31 32 static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) 33 { 34 struct vkms_output *output = container_of(timer, struct vkms_output, 35 vblank_hrtimer); 36 int ret_overrun; 37 38 _vblank_handle(output); 39 40 ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer, 41 output->period_ns); 42 43 return HRTIMER_RESTART; 44 } 45 46 static int vkms_enable_vblank(struct drm_crtc *crtc) 47 { 48 struct drm_device *dev = crtc->dev; 49 unsigned int pipe = drm_crtc_index(crtc); 50 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 51 struct vkms_output *out = drm_crtc_to_vkms_output(crtc); 52 53 drm_calc_timestamping_constants(crtc, &crtc->mode); 54 55 hrtimer_init(&out->vblank_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 56 out->vblank_hrtimer.function = &vkms_vblank_simulate; 57 out->period_ns = ktime_set(0, vblank->framedur_ns); 58 hrtimer_start(&out->vblank_hrtimer, out->period_ns, HRTIMER_MODE_REL); 59 60 return 0; 61 } 62 63 static void vkms_disable_vblank(struct drm_crtc *crtc) 64 { 65 struct vkms_output *out = drm_crtc_to_vkms_output(crtc); 66 67 hrtimer_cancel(&out->vblank_hrtimer); 68 } 69 70 bool vkms_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe, 71 int *max_error, ktime_t *vblank_time, 72 bool in_vblank_irq) 73 { 74 struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev); 75 struct vkms_output *output = &vkmsdev->output; 76 77 *vblank_time = output->vblank_hrtimer.node.expires; 78 79 return true; 80 } 81 82 static void vkms_atomic_crtc_reset(struct drm_crtc *crtc) 83 { 84 struct vkms_crtc_state *vkms_state = NULL; 85 86 if (crtc->state) { 87 vkms_state = to_vkms_crtc_state(crtc->state); 88 __drm_atomic_helper_crtc_destroy_state(crtc->state); 89 kfree(vkms_state); 90 crtc->state = NULL; 91 } 92 93 vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL); 94 if (!vkms_state) 95 return; 96 97 crtc->state = &vkms_state->base; 98 crtc->state->crtc = crtc; 99 } 100 101 static struct drm_crtc_state * 102 vkms_atomic_crtc_duplicate_state(struct drm_crtc *crtc) 103 { 104 struct vkms_crtc_state *vkms_state; 105 106 if (WARN_ON(!crtc->state)) 107 return NULL; 108 109 vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL); 110 if (!vkms_state) 111 return NULL; 112 113 __drm_atomic_helper_crtc_duplicate_state(crtc, &vkms_state->base); 114 115 INIT_WORK(&vkms_state->crc_work, vkms_crc_work_handle); 116 117 return &vkms_state->base; 118 } 119 120 static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc, 121 struct drm_crtc_state *state) 122 { 123 struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state); 124 125 __drm_atomic_helper_crtc_destroy_state(state); 126 127 if (vkms_state) { 128 flush_work(&vkms_state->crc_work); 129 kfree(vkms_state); 130 } 131 } 132 133 static const struct drm_crtc_funcs vkms_crtc_funcs = { 134 .set_config = drm_atomic_helper_set_config, 135 .destroy = drm_crtc_cleanup, 136 .page_flip = drm_atomic_helper_page_flip, 137 .reset = vkms_atomic_crtc_reset, 138 .atomic_duplicate_state = vkms_atomic_crtc_duplicate_state, 139 .atomic_destroy_state = vkms_atomic_crtc_destroy_state, 140 .enable_vblank = vkms_enable_vblank, 141 .disable_vblank = vkms_disable_vblank, 142 .set_crc_source = vkms_set_crc_source, 143 .verify_crc_source = vkms_verify_crc_source, 144 }; 145 146 static void vkms_crtc_atomic_enable(struct drm_crtc *crtc, 147 struct drm_crtc_state *old_state) 148 { 149 drm_crtc_vblank_on(crtc); 150 } 151 152 static void vkms_crtc_atomic_disable(struct drm_crtc *crtc, 153 struct drm_crtc_state *old_state) 154 { 155 drm_crtc_vblank_off(crtc); 156 } 157 158 static void vkms_crtc_atomic_begin(struct drm_crtc *crtc, 159 struct drm_crtc_state *old_crtc_state) 160 { 161 struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc); 162 163 /* This lock is held across the atomic commit to block vblank timer 164 * from scheduling vkms_crc_work_handle until the crc_data is updated 165 */ 166 spin_lock_irq(&vkms_output->lock); 167 } 168 169 static void vkms_crtc_atomic_flush(struct drm_crtc *crtc, 170 struct drm_crtc_state *old_crtc_state) 171 { 172 struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc); 173 unsigned long flags; 174 175 if (crtc->state->event) { 176 spin_lock_irqsave(&crtc->dev->event_lock, flags); 177 178 if (drm_crtc_vblank_get(crtc) != 0) 179 drm_crtc_send_vblank_event(crtc, crtc->state->event); 180 else 181 drm_crtc_arm_vblank_event(crtc, crtc->state->event); 182 183 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 184 185 crtc->state->event = NULL; 186 } 187 188 spin_unlock_irq(&vkms_output->lock); 189 } 190 191 static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = { 192 .atomic_begin = vkms_crtc_atomic_begin, 193 .atomic_flush = vkms_crtc_atomic_flush, 194 .atomic_enable = vkms_crtc_atomic_enable, 195 .atomic_disable = vkms_crtc_atomic_disable, 196 }; 197 198 int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc, 199 struct drm_plane *primary, struct drm_plane *cursor) 200 { 201 struct vkms_output *vkms_out = drm_crtc_to_vkms_output(crtc); 202 int ret; 203 204 ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor, 205 &vkms_crtc_funcs, NULL); 206 if (ret) { 207 DRM_ERROR("Failed to init CRTC\n"); 208 return ret; 209 } 210 211 drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs); 212 213 spin_lock_init(&vkms_out->lock); 214 215 vkms_out->crc_workq = alloc_ordered_workqueue("vkms_crc_workq", 0); 216 217 return ret; 218 } 219