| 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_admin.h" |
| 13 | #include "adf_accel_devices.h" |
| 14 | #include "adf_common_drv.h" |
| 15 | #include "adf_timer.h" |
| 16 | |
| 17 | #define ADF_DEFAULT_TIMER_PERIOD_MS 200 |
| 18 | |
| 19 | /* This periodic update is used to trigger HB, RL & TL fw events */ |
| 20 | static void work_handler(struct work_struct *work) |
| 21 | { |
| 22 | struct adf_accel_dev *accel_dev; |
| 23 | struct adf_timer *timer_ctx; |
| 24 | u32 time_periods; |
| 25 | |
| 26 | timer_ctx = container_of(to_delayed_work(work), struct adf_timer, work_ctx); |
| 27 | accel_dev = timer_ctx->accel_dev; |
| 28 | |
| 29 | adf_misc_wq_queue_delayed_work(work: &timer_ctx->work_ctx, |
| 30 | delay: msecs_to_jiffies(ADF_DEFAULT_TIMER_PERIOD_MS)); |
| 31 | |
| 32 | time_periods = div_u64(dividend: ktime_ms_delta(later: ktime_get_real(), earlier: timer_ctx->initial_ktime), |
| 33 | ADF_DEFAULT_TIMER_PERIOD_MS); |
| 34 | |
| 35 | if (adf_send_admin_tim_sync(accel_dev, cnt: time_periods)) |
| 36 | dev_err(&GET_DEV(accel_dev), "Failed to synchronize qat timer\n" ); |
| 37 | } |
| 38 | |
| 39 | int adf_timer_start(struct adf_accel_dev *accel_dev) |
| 40 | { |
| 41 | struct adf_timer *timer_ctx; |
| 42 | |
| 43 | timer_ctx = kzalloc(sizeof(*timer_ctx), GFP_KERNEL); |
| 44 | if (!timer_ctx) |
| 45 | return -ENOMEM; |
| 46 | |
| 47 | timer_ctx->accel_dev = accel_dev; |
| 48 | accel_dev->timer = timer_ctx; |
| 49 | timer_ctx->initial_ktime = ktime_get_real(); |
| 50 | |
| 51 | INIT_DELAYED_WORK(&timer_ctx->work_ctx, work_handler); |
| 52 | adf_misc_wq_queue_delayed_work(work: &timer_ctx->work_ctx, |
| 53 | delay: msecs_to_jiffies(ADF_DEFAULT_TIMER_PERIOD_MS)); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | EXPORT_SYMBOL_GPL(adf_timer_start); |
| 58 | |
| 59 | void adf_timer_stop(struct adf_accel_dev *accel_dev) |
| 60 | { |
| 61 | struct adf_timer *timer_ctx = accel_dev->timer; |
| 62 | |
| 63 | if (!timer_ctx) |
| 64 | return; |
| 65 | |
| 66 | cancel_delayed_work_sync(dwork: &timer_ctx->work_ctx); |
| 67 | |
| 68 | kfree(objp: timer_ctx); |
| 69 | accel_dev->timer = NULL; |
| 70 | } |
| 71 | EXPORT_SYMBOL_GPL(adf_timer_stop); |
| 72 | |