1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2023 Intel Corporation */
3
4 #include <linux/container_of.h>
5 #include <linux/dev_printk.h>
6 #include <linux/export.h>
7 #include <linux/jiffies.h>
8 #include <linux/ktime.h>
9 #include <linux/slab.h>
10 #include <linux/workqueue.h>
11
12 #include "adf_accel_devices.h"
13 #include "adf_common_drv.h"
14 #include "adf_gen4_timer.h"
15
16 #define ADF_GEN4_TIMER_PERIOD_MS 200
17
18 /* This periodic update is used to trigger HB, RL & TL fw events */
work_handler(struct work_struct * work)19 static void work_handler(struct work_struct *work)
20 {
21 struct adf_accel_dev *accel_dev;
22 struct adf_timer *timer_ctx;
23 u32 time_periods;
24
25 timer_ctx = container_of(to_delayed_work(work), struct adf_timer, work_ctx);
26 accel_dev = timer_ctx->accel_dev;
27
28 adf_misc_wq_queue_delayed_work(&timer_ctx->work_ctx,
29 msecs_to_jiffies(ADF_GEN4_TIMER_PERIOD_MS));
30
31 time_periods = div_u64(ktime_ms_delta(ktime_get_real(), timer_ctx->initial_ktime),
32 ADF_GEN4_TIMER_PERIOD_MS);
33
34 if (adf_send_admin_tim_sync(accel_dev, time_periods))
35 dev_err(&GET_DEV(accel_dev), "Failed to synchronize qat timer\n");
36 }
37
adf_gen4_timer_start(struct adf_accel_dev * accel_dev)38 int adf_gen4_timer_start(struct adf_accel_dev *accel_dev)
39 {
40 struct adf_timer *timer_ctx;
41
42 timer_ctx = kzalloc(sizeof(*timer_ctx), GFP_KERNEL);
43 if (!timer_ctx)
44 return -ENOMEM;
45
46 timer_ctx->accel_dev = accel_dev;
47 accel_dev->timer = timer_ctx;
48 timer_ctx->initial_ktime = ktime_get_real();
49
50 INIT_DELAYED_WORK(&timer_ctx->work_ctx, work_handler);
51 adf_misc_wq_queue_delayed_work(&timer_ctx->work_ctx,
52 msecs_to_jiffies(ADF_GEN4_TIMER_PERIOD_MS));
53
54 return 0;
55 }
56 EXPORT_SYMBOL_GPL(adf_gen4_timer_start);
57
adf_gen4_timer_stop(struct adf_accel_dev * accel_dev)58 void adf_gen4_timer_stop(struct adf_accel_dev *accel_dev)
59 {
60 struct adf_timer *timer_ctx = accel_dev->timer;
61
62 if (!timer_ctx)
63 return;
64
65 cancel_delayed_work_sync(&timer_ctx->work_ctx);
66
67 kfree(timer_ctx);
68 accel_dev->timer = NULL;
69 }
70 EXPORT_SYMBOL_GPL(adf_gen4_timer_stop);
71