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