Path: blob/master/drivers/crypto/intel/qat/qat_common/adf_clock.c
29278 views
// SPDX-License-Identifier: GPL-2.0-only1/* Copyright(c) 2023 Intel Corporation */23#include <linux/delay.h>4#include <linux/dev_printk.h>5#include <linux/export.h>6#include <linux/math.h>7#include <linux/minmax.h>8#include <linux/time64.h>9#include <linux/types.h>10#include <linux/units.h>11#include <asm/errno.h>12#include "adf_admin.h"13#include "adf_accel_devices.h"14#include "adf_clock.h"15#include "adf_common_drv.h"1617#define MEASURE_CLOCK_RETRIES 1018#define MEASURE_CLOCK_DELAY_US 1000019#define ME_CLK_DIVIDER 1620#define MEASURE_CLOCK_DELTA_THRESHOLD_US 1002122static inline u64 timespec_to_us(const struct timespec64 *ts)23{24return (u64)DIV_ROUND_CLOSEST_ULL(timespec64_to_ns(ts), NSEC_PER_USEC);25}2627static inline u64 timespec_to_ms(const struct timespec64 *ts)28{29return (u64)DIV_ROUND_CLOSEST_ULL(timespec64_to_ns(ts), NSEC_PER_MSEC);30}3132u64 adf_clock_get_current_time(void)33{34struct timespec64 ts;3536ktime_get_real_ts64(&ts);37return timespec_to_ms(&ts);38}3940static int measure_clock(struct adf_accel_dev *accel_dev, u32 *frequency)41{42struct timespec64 ts1, ts2, ts3, ts4;43u64 timestamp1, timestamp2, temp;44u32 delta_us, tries;45int ret;4647tries = MEASURE_CLOCK_RETRIES;48do {49ktime_get_real_ts64(&ts1);50ret = adf_get_fw_timestamp(accel_dev, ×tamp1);51if (ret) {52dev_err(&GET_DEV(accel_dev),53"Failed to get fw timestamp\n");54return ret;55}56ktime_get_real_ts64(&ts2);57delta_us = timespec_to_us(&ts2) - timespec_to_us(&ts1);58} while (delta_us > MEASURE_CLOCK_DELTA_THRESHOLD_US && --tries);5960if (!tries) {61dev_err(&GET_DEV(accel_dev), "Excessive clock measure delay\n");62return -ETIMEDOUT;63}6465fsleep(MEASURE_CLOCK_DELAY_US);6667tries = MEASURE_CLOCK_RETRIES;68do {69ktime_get_real_ts64(&ts3);70if (adf_get_fw_timestamp(accel_dev, ×tamp2)) {71dev_err(&GET_DEV(accel_dev),72"Failed to get fw timestamp\n");73return -EIO;74}75ktime_get_real_ts64(&ts4);76delta_us = timespec_to_us(&ts4) - timespec_to_us(&ts3);77} while (delta_us > MEASURE_CLOCK_DELTA_THRESHOLD_US && --tries);7879if (!tries) {80dev_err(&GET_DEV(accel_dev), "Excessive clock measure delay\n");81return -ETIMEDOUT;82}8384delta_us = timespec_to_us(&ts3) - timespec_to_us(&ts1);85if (!delta_us)86return -EINVAL;8788temp = (timestamp2 - timestamp1) * ME_CLK_DIVIDER * 10;89temp = 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);9596return 0;97}9899/**100* adf_dev_measure_clock() - measures device clock frequency101* @accel_dev: Pointer to acceleration device.102* @frequency: Pointer to variable where result will be stored103* @min: Minimal allowed frequency value104* @max: Maximal allowed frequency value105*106* If the measurement result will go beyond the min/max thresholds the value107* will take the value of the crossed threshold.108*109* This algorithm compares the device firmware timestamp with the kernel110* timestamp. So we can't expect too high accuracy from this measurement.111*112* Return:113* * 0 - measurement succeed114* * -ETIMEDOUT - measurement failed115*/116int adf_dev_measure_clock(struct adf_accel_dev *accel_dev,117u32 *frequency, u32 min, u32 max)118{119int ret;120u32 freq;121122ret = measure_clock(accel_dev, &freq);123if (ret)124return ret;125126*frequency = clamp(freq, min, max);127128if (*frequency != freq)129dev_warn(&GET_DEV(accel_dev),130"Measured clock %d Hz is out of range, assuming %d\n",131freq, *frequency);132return 0;133}134EXPORT_SYMBOL_GPL(adf_dev_measure_clock);135136137