1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2023 Intel Corporation */
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"
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
23 static inline u64 timespec_to_us(const struct timespec64 *ts)
25 return (u64)DIV_ROUND_CLOSEST_ULL(timespec64_to_ns(ts), NSEC_PER_USEC);
28 static inline u64 timespec_to_ms(const struct timespec64 *ts)
30 return (u64)DIV_ROUND_CLOSEST_ULL(timespec64_to_ns(ts), NSEC_PER_MSEC);
33 u64 adf_clock_get_current_time(void)
37 ktime_get_real_ts64(&ts);
38 return timespec_to_ms(&ts);
41 static int measure_clock(struct adf_accel_dev *accel_dev, u32 *frequency)
43 struct timespec64 ts1, ts2, ts3, ts4;
44 u64 timestamp1, timestamp2, temp;
48 tries = MEASURE_CLOCK_RETRIES;
50 ktime_get_real_ts64(&ts1);
51 ret = adf_get_fw_timestamp(accel_dev, ×tamp1);
53 dev_err(&GET_DEV(accel_dev),
54 "Failed to get fw timestamp\n");
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);
62 dev_err(&GET_DEV(accel_dev), "Excessive clock measure delay\n");
66 fsleep(MEASURE_CLOCK_DELAY_US);
68 tries = MEASURE_CLOCK_RETRIES;
70 ktime_get_real_ts64(&ts3);
71 if (adf_get_fw_timestamp(accel_dev, ×tamp2)) {
72 dev_err(&GET_DEV(accel_dev),
73 "Failed to get fw timestamp\n");
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);
81 dev_err(&GET_DEV(accel_dev), "Excessive clock measure delay\n");
85 delta_us = timespec_to_us(&ts3) - timespec_to_us(&ts1);
89 temp = (timestamp2 - timestamp1) * ME_CLK_DIVIDER * 10;
90 temp = DIV_ROUND_CLOSEST_ULL(temp, delta_us);
92 * Enclose the division to allow the preprocessor to precalculate it,
93 * and avoid promoting r-value to 64-bit before division.
95 *frequency = temp * (HZ_PER_MHZ / 10);
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
107 * If the measurement result will go beyond the min/max thresholds the value
108 * will take the value of the crossed threshold.
110 * This algorithm compares the device firmware timestamp with the kernel
111 * timestamp. So we can't expect too high accuracy from this measurement.
114 * * 0 - measurement succeed
115 * * -ETIMEDOUT - measurement failed
117 int adf_dev_measure_clock(struct adf_accel_dev *accel_dev,
118 u32 *frequency, u32 min, u32 max)
123 ret = measure_clock(accel_dev, &freq);
127 *frequency = clamp(freq, min, max);
129 if (*frequency != freq)
130 dev_warn(&GET_DEV(accel_dev),
131 "Measured clock %d Hz is out of range, assuming %d\n",
135 EXPORT_SYMBOL_GPL(adf_dev_measure_clock);