1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2023 Intel Corporation */
3 
4 #include <linux/delay.h>
5 #include <linux/dev_printk.h>
6 #include <linux/export.h>
7 #include <linux/math.h>
8 #include <linux/minmax.h>
9 #include <linux/time64.h>
10 #include <linux/types.h>
11 #include <linux/units.h>
12 #include <asm/errno.h>
13 #include "adf_accel_devices.h"
14 #include "adf_clock.h"
15 #include "adf_common_drv.h"
16 
17 #define MEASURE_CLOCK_RETRIES 10
18 #define MEASURE_CLOCK_DELAY_US 10000
19 #define ME_CLK_DIVIDER 16
20 #define MEASURE_CLOCK_DELTA_THRESHOLD_US 100
21 
timespec_to_us(const struct timespec64 * ts)22 static inline u64 timespec_to_us(const struct timespec64 *ts)
23 {
24 	return (u64)DIV_ROUND_CLOSEST_ULL(timespec64_to_ns(ts), NSEC_PER_USEC);
25 }
26 
timespec_to_ms(const struct timespec64 * ts)27 static inline u64 timespec_to_ms(const struct timespec64 *ts)
28 {
29 	return (u64)DIV_ROUND_CLOSEST_ULL(timespec64_to_ns(ts), NSEC_PER_MSEC);
30 }
31 
adf_clock_get_current_time(void)32 u64 adf_clock_get_current_time(void)
33 {
34 	struct timespec64 ts;
35 
36 	ktime_get_real_ts64(&ts);
37 	return timespec_to_ms(&ts);
38 }
39 
measure_clock(struct adf_accel_dev * accel_dev,u32 * frequency)40 static int measure_clock(struct adf_accel_dev *accel_dev, u32 *frequency)
41 {
42 	struct timespec64 ts1, ts2, ts3, ts4;
43 	u64 timestamp1, timestamp2, temp;
44 	u32 delta_us, tries;
45 	int ret;
46 
47 	tries = MEASURE_CLOCK_RETRIES;
48 	do {
49 		ktime_get_real_ts64(&ts1);
50 		ret = adf_get_fw_timestamp(accel_dev, &timestamp1);
51 		if (ret) {
52 			dev_err(&GET_DEV(accel_dev),
53 				"Failed to get fw timestamp\n");
54 			return ret;
55 		}
56 		ktime_get_real_ts64(&ts2);
57 		delta_us = timespec_to_us(&ts2) - timespec_to_us(&ts1);
58 	} while (delta_us > MEASURE_CLOCK_DELTA_THRESHOLD_US && --tries);
59 
60 	if (!tries) {
61 		dev_err(&GET_DEV(accel_dev), "Excessive clock measure delay\n");
62 		return -ETIMEDOUT;
63 	}
64 
65 	fsleep(MEASURE_CLOCK_DELAY_US);
66 
67 	tries = MEASURE_CLOCK_RETRIES;
68 	do {
69 		ktime_get_real_ts64(&ts3);
70 		if (adf_get_fw_timestamp(accel_dev, &timestamp2)) {
71 			dev_err(&GET_DEV(accel_dev),
72 				"Failed to get fw timestamp\n");
73 			return -EIO;
74 		}
75 		ktime_get_real_ts64(&ts4);
76 		delta_us = timespec_to_us(&ts4) - timespec_to_us(&ts3);
77 	} while (delta_us > MEASURE_CLOCK_DELTA_THRESHOLD_US && --tries);
78 
79 	if (!tries) {
80 		dev_err(&GET_DEV(accel_dev), "Excessive clock measure delay\n");
81 		return -ETIMEDOUT;
82 	}
83 
84 	delta_us = timespec_to_us(&ts3) - timespec_to_us(&ts1);
85 	if (!delta_us)
86 		return -EINVAL;
87 
88 	temp = (timestamp2 - timestamp1) * ME_CLK_DIVIDER * 10;
89 	temp = DIV_ROUND_CLOSEST_ULL(temp, delta_us);
90 	/*
91 	 * Enclose the division to allow the preprocessor to precalculate it,
92 	 * and avoid promoting r-value to 64-bit before division.
93 	 */
94 	*frequency = temp * (HZ_PER_MHZ / 10);
95 
96 	return 0;
97 }
98 
99 /**
100  * adf_dev_measure_clock() - measures device clock frequency
101  * @accel_dev: Pointer to acceleration device.
102  * @frequency: Pointer to variable where result will be stored
103  * @min: Minimal allowed frequency value
104  * @max: Maximal allowed frequency value
105  *
106  * If the measurement result will go beyond the min/max thresholds the value
107  * will take the value of the crossed threshold.
108  *
109  * This algorithm compares the device firmware timestamp with the kernel
110  * timestamp. So we can't expect too high accuracy from this measurement.
111  *
112  * Return:
113  * * 0 - measurement succeed
114  * * -ETIMEDOUT - measurement failed
115  */
adf_dev_measure_clock(struct adf_accel_dev * accel_dev,u32 * frequency,u32 min,u32 max)116 int adf_dev_measure_clock(struct adf_accel_dev *accel_dev,
117 			  u32 *frequency, u32 min, u32 max)
118 {
119 	int ret;
120 	u32 freq;
121 
122 	ret = measure_clock(accel_dev, &freq);
123 	if (ret)
124 		return ret;
125 
126 	*frequency = clamp(freq, min, max);
127 
128 	if (*frequency != freq)
129 		dev_warn(&GET_DEV(accel_dev),
130 			 "Measured clock %d Hz is out of range, assuming %d\n",
131 			 freq, *frequency);
132 	return 0;
133 }
134 EXPORT_SYMBOL_GPL(adf_dev_measure_clock);
135