Linux Audio

Check our new training course

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