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 enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) 14 { 15 struct vkms_output *output = container_of(timer, struct vkms_output, 16 vblank_hrtimer); 17 struct drm_crtc *crtc = &output->crtc; 18 int ret_overrun; 19 bool ret; 20 21 ret = drm_crtc_handle_vblank(crtc); 22 if (!ret) 23 DRM_ERROR("vkms failure on handling vblank"); 24 25 ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer, 26 output->period_ns); 27 28 return HRTIMER_RESTART; 29 } 30 31 static int vkms_enable_vblank(struct drm_crtc *crtc) 32 { 33 struct drm_device *dev = crtc->dev; 34 unsigned int pipe = drm_crtc_index(crtc); 35 struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 36 struct vkms_output *out = drm_crtc_to_vkms_output(crtc); 37 38 drm_calc_timestamping_constants(crtc, &crtc->mode); 39 40 hrtimer_init(&out->vblank_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 41 out->vblank_hrtimer.function = &vkms_vblank_simulate; 42 out->period_ns = ktime_set(0, vblank->framedur_ns); 43 hrtimer_start(&out->vblank_hrtimer, out->period_ns, HRTIMER_MODE_REL); 44 45 return 0; 46 } 47 48 static void vkms_disable_vblank(struct drm_crtc *crtc) 49 { 50 struct vkms_output *out = drm_crtc_to_vkms_output(crtc); 51 52 hrtimer_cancel(&out->vblank_hrtimer); 53 } 54 55 bool vkms_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe, 56 int *max_error, ktime_t *vblank_time, 57 bool in_vblank_irq) 58 { 59 struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev); 60 struct vkms_output *output = &vkmsdev->output; 61 62 *vblank_time = output->vblank_hrtimer.node.expires; 63 64 return true; 65 } 66 67 static const struct drm_crtc_funcs vkms_crtc_funcs = { 68 .set_config = drm_atomic_helper_set_config, 69 .destroy = drm_crtc_cleanup, 70 .page_flip = drm_atomic_helper_page_flip, 71 .reset = drm_atomic_helper_crtc_reset, 72 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 73 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 74 .enable_vblank = vkms_enable_vblank, 75 .disable_vblank = vkms_disable_vblank, 76 }; 77 78 static void vkms_crtc_atomic_enable(struct drm_crtc *crtc, 79 struct drm_crtc_state *old_state) 80 { 81 drm_crtc_vblank_on(crtc); 82 } 83 84 static void vkms_crtc_atomic_disable(struct drm_crtc *crtc, 85 struct drm_crtc_state *old_state) 86 { 87 drm_crtc_vblank_off(crtc); 88 } 89 90 static void vkms_crtc_atomic_flush(struct drm_crtc *crtc, 91 struct drm_crtc_state *old_crtc_state) 92 { 93 unsigned long flags; 94 95 if (crtc->state->event) { 96 spin_lock_irqsave(&crtc->dev->event_lock, flags); 97 98 if (drm_crtc_vblank_get(crtc) != 0) 99 drm_crtc_send_vblank_event(crtc, crtc->state->event); 100 else 101 drm_crtc_arm_vblank_event(crtc, crtc->state->event); 102 103 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 104 105 crtc->state->event = NULL; 106 } 107 } 108 109 static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = { 110 .atomic_flush = vkms_crtc_atomic_flush, 111 .atomic_enable = vkms_crtc_atomic_enable, 112 .atomic_disable = vkms_crtc_atomic_disable, 113 }; 114 115 int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc, 116 struct drm_plane *primary, struct drm_plane *cursor) 117 { 118 int ret; 119 120 ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor, 121 &vkms_crtc_funcs, NULL); 122 if (ret) { 123 DRM_ERROR("Failed to init CRTC\n"); 124 return ret; 125 } 126 127 drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs); 128 129 return ret; 130 } 131