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 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 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 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 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, ×tamp1); 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, ×tamp2)) { 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 temp = (timestamp2 - timestamp1) * ME_CLK_DIVIDER * 10; 86 temp = DIV_ROUND_CLOSEST_ULL(temp, delta_us); 87 /* 88 * Enclose the division to allow the preprocessor to precalculate it, 89 * and avoid promoting r-value to 64-bit before division. 90 */ 91 *frequency = temp * (HZ_PER_MHZ / 10); 92 93 return 0; 94 } 95 96 /** 97 * adf_dev_measure_clock() - measures device clock frequency 98 * @accel_dev: Pointer to acceleration device. 99 * @frequency: Pointer to variable where result will be stored 100 * @min: Minimal allowed frequency value 101 * @max: Maximal allowed frequency value 102 * 103 * If the measurement result will go beyond the min/max thresholds the value 104 * will take the value of the crossed threshold. 105 * 106 * This algorithm compares the device firmware timestamp with the kernel 107 * timestamp. So we can't expect too high accuracy from this measurement. 108 * 109 * Return: 110 * * 0 - measurement succeed 111 * * -ETIMEDOUT - measurement failed 112 */ 113 int adf_dev_measure_clock(struct adf_accel_dev *accel_dev, 114 u32 *frequency, u32 min, u32 max) 115 { 116 int ret; 117 u32 freq; 118 119 ret = measure_clock(accel_dev, &freq); 120 if (ret) 121 return ret; 122 123 *frequency = clamp(freq, min, max); 124 125 if (*frequency != freq) 126 dev_warn(&GET_DEV(accel_dev), 127 "Measured clock %d Hz is out of range, assuming %d\n", 128 freq, *frequency); 129 return 0; 130 } 131 EXPORT_SYMBOL_GPL(adf_dev_measure_clock); 132