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