<div dir="ltr"><div>LGTM. This one is</div><div><br></div><div>Reviewed-by: Mario Kleiner <<a href="mailto:mario.kleiner.de@gmail.com">mario.kleiner.de@gmail.com</a>></div><div><br></div><div>-mario</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 27, 2023 at 8:36 PM Rob Clark <<a href="mailto:robdclark@gmail.com">robdclark@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">From: Rob Clark <<a href="mailto:robdclark@chromium.org" target="_blank">robdclark@chromium.org</a>><br>
<br>
Will be used in the next commit to set a deadline on fences that an<br>
atomic update is waiting on.<br>
<br>
v2: Calculate time at *start* of vblank period, not end<br>
v3: Fix kbuild complaints<br>
<br>
Signed-off-by: Rob Clark <<a href="mailto:robdclark@chromium.org" target="_blank">robdclark@chromium.org</a>><br>
---<br>
drivers/gpu/drm/drm_vblank.c | 53 ++++++++++++++++++++++++++++++------<br>
include/drm/drm_vblank.h | 1 +<br>
2 files changed, 45 insertions(+), 9 deletions(-)<br>
<br>
diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c<br>
index 2ff31717a3de..299fa2a19a90 100644<br>
--- a/drivers/gpu/drm/drm_vblank.c<br>
+++ b/drivers/gpu/drm/drm_vblank.c<br>
@@ -844,10 +844,9 @@ bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,<br>
EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp);<br>
<br>
/**<br>
- * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent<br>
- * vblank interval<br>
- * @dev: DRM device<br>
- * @pipe: index of CRTC whose vblank timestamp to retrieve<br>
+ * drm_crtc_get_last_vbltimestamp - retrieve raw timestamp for the most<br>
+ * recent vblank interval<br>
+ * @crtc: CRTC whose vblank timestamp to retrieve<br>
* @tvblank: Pointer to target time which should receive the timestamp<br>
* @in_vblank_irq:<br>
* True when called from drm_crtc_handle_vblank(). Some drivers<br>
@@ -865,10 +864,9 @@ EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp);<br>
* True if timestamp is considered to be very precise, false otherwise.<br>
*/<br>
static bool<br>
-drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,<br>
- ktime_t *tvblank, bool in_vblank_irq)<br>
+drm_crtc_get_last_vbltimestamp(struct drm_crtc *crtc, ktime_t *tvblank,<br>
+ bool in_vblank_irq)<br>
{<br>
- struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);<br>
bool ret = false;<br>
<br>
/* Define requested maximum error on timestamps (nanoseconds). */<br>
@@ -876,8 +874,6 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,<br>
<br>
/* Query driver if possible and precision timestamping enabled. */<br>
if (crtc && crtc->funcs->get_vblank_timestamp && max_error > 0) {<br>
- struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);<br>
-<br>
ret = crtc->funcs->get_vblank_timestamp(crtc, &max_error,<br>
tvblank, in_vblank_irq);<br>
}<br>
@@ -891,6 +887,15 @@ drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,<br>
return ret;<br>
}<br>
<br>
+static bool<br>
+drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,<br>
+ ktime_t *tvblank, bool in_vblank_irq)<br>
+{<br>
+ struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);<br>
+<br>
+ return drm_crtc_get_last_vbltimestamp(crtc, tvblank, in_vblank_irq);<br>
+}<br>
+<br>
/**<br>
* drm_crtc_vblank_count - retrieve "cooked" vblank counter value<br>
* @crtc: which counter to retrieve<br>
@@ -980,6 +985,36 @@ u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,<br>
}<br>
EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);<br>
<br>
+/**<br>
+ * drm_crtc_next_vblank_start - calculate the time of the next vblank<br>
+ * @crtc: the crtc for which to calculate next vblank time<br>
+ * @vblanktime: pointer to time to receive the next vblank timestamp.<br>
+ *<br>
+ * Calculate the expected time of the start of the next vblank period,<br>
+ * based on time of previous vblank and frame duration<br>
+ */<br>
+int drm_crtc_next_vblank_start(struct drm_crtc *crtc, ktime_t *vblanktime)<br>
+{<br>
+ unsigned int pipe = drm_crtc_index(crtc);<br>
+ struct drm_vblank_crtc *vblank = &crtc->dev->vblank[pipe];<br>
+ struct drm_display_mode *mode = &vblank->hwmode;<br>
+ u64 vblank_start;<br>
+<br>
+ if (!vblank->framedur_ns || !vblank->linedur_ns)<br>
+ return -EINVAL;<br>
+<br>
+ if (!drm_crtc_get_last_vbltimestamp(crtc, vblanktime, false))<br>
+ return -EINVAL;<br>
+<br>
+ vblank_start = DIV_ROUND_DOWN_ULL(<br>
+ (u64)vblank->framedur_ns * mode->crtc_vblank_start,<br>
+ mode->crtc_vtotal);<br>
+ *vblanktime = ktime_add(*vblanktime, ns_to_ktime(vblank_start));<br>
+<br>
+ return 0;<br>
+}<br>
+EXPORT_SYMBOL(drm_crtc_next_vblank_start);<br>
+<br>
static void send_vblank_event(struct drm_device *dev,<br>
struct drm_pending_vblank_event *e,<br>
u64 seq, ktime_t now)<br>
diff --git a/include/drm/drm_vblank.h b/include/drm/drm_vblank.h<br>
index 733a3e2d1d10..7f3957943dd1 100644<br>
--- a/include/drm/drm_vblank.h<br>
+++ b/include/drm/drm_vblank.h<br>
@@ -230,6 +230,7 @@ bool drm_dev_has_vblank(const struct drm_device *dev);<br>
u64 drm_crtc_vblank_count(struct drm_crtc *crtc);<br>
u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,<br>
ktime_t *vblanktime);<br>
+int drm_crtc_next_vblank_start(struct drm_crtc *crtc, ktime_t *vblanktime);<br>
void drm_crtc_send_vblank_event(struct drm_crtc *crtc,<br>
struct drm_pending_vblank_event *e);<br>
void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,<br>
-- <br>
2.39.1<br>
<br>
</blockquote></div>