summaryrefslogtreecommitdiff
path: root/mali_kbase/hwcnt/mali_kbase_hwcnt_watchdog_if_timer.c
blob: 4caa832cd5877829b315d1f920695abdf6631d32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
/*
 *
 * (C) COPYRIGHT 2021-2022 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 */

#include "mali_kbase.h"
#include "hwcnt/mali_kbase_hwcnt_watchdog_if.h"
#include "hwcnt/mali_kbase_hwcnt_watchdog_if_timer.h"

#include <linux/workqueue.h>
#include <linux/slab.h>

/**
 * struct kbase_hwcnt_watchdog_if_timer_info - Timer information for watchdog
 *                                             interface.
 *
 * @workq:          Single threaded work queue in which to execute callbacks.
 * @dwork:          Worker to execute callback function.
 * @timer_enabled:  True if watchdog timer enabled, otherwise false
 * @callback:       Watchdog callback function
 * @user_data:      Pointer to user data passed as argument to the callback
 *                  function
 */
struct kbase_hwcnt_watchdog_if_timer_info {
	struct workqueue_struct *workq;
	struct delayed_work dwork;
	bool timer_enabled;
	kbase_hwcnt_watchdog_callback_fn *callback;
	void *user_data;
};

/**
 * kbasep_hwcnt_watchdog_callback() - Watchdog callback
 *
 * @work: Work structure
 *
 * Function to be called in a work queue after watchdog timer has expired.
 */
static void kbasep_hwcnt_watchdog_callback(struct work_struct *const work)
{
	struct kbase_hwcnt_watchdog_if_timer_info *const info =
		container_of(work, struct kbase_hwcnt_watchdog_if_timer_info, dwork.work);

	if (info->callback)
		info->callback(info->user_data);
}

static int kbasep_hwcnt_watchdog_if_timer_enable(
	const struct kbase_hwcnt_watchdog_info *const timer, u32 const period_ms,
	kbase_hwcnt_watchdog_callback_fn *const callback, void *const user_data)
{
	struct kbase_hwcnt_watchdog_if_timer_info *const timer_info = (void *)timer;

	if (WARN_ON(!timer) || WARN_ON(!callback) || WARN_ON(timer_info->timer_enabled))
		return -EINVAL;

	timer_info->callback = callback;
	timer_info->user_data = user_data;

	queue_delayed_work(timer_info->workq, &timer_info->dwork, msecs_to_jiffies(period_ms));
	timer_info->timer_enabled = true;

	return 0;
}

static void
kbasep_hwcnt_watchdog_if_timer_disable(const struct kbase_hwcnt_watchdog_info *const timer)
{
	struct kbase_hwcnt_watchdog_if_timer_info *const timer_info = (void *)timer;

	if (WARN_ON(!timer))
		return;

	if (!timer_info->timer_enabled)
		return;

	cancel_delayed_work_sync(&timer_info->dwork);
	timer_info->timer_enabled = false;
}

static void
kbasep_hwcnt_watchdog_if_timer_modify(const struct kbase_hwcnt_watchdog_info *const timer,
				      u32 const delay_ms)
{
	struct kbase_hwcnt_watchdog_if_timer_info *const timer_info = (void *)timer;

	if (WARN_ON(!timer) || WARN_ON(!timer_info->timer_enabled))
		return;

	mod_delayed_work(timer_info->workq, &timer_info->dwork, msecs_to_jiffies(delay_ms));
}

void kbase_hwcnt_watchdog_if_timer_destroy(struct kbase_hwcnt_watchdog_interface *const watchdog_if)
{
	struct kbase_hwcnt_watchdog_if_timer_info *timer_info;

	if (WARN_ON(!watchdog_if))
		return;

	timer_info = (void *)watchdog_if->timer;

	if (WARN_ON(!timer_info))
		return;

	destroy_workqueue(timer_info->workq);
	kfree(timer_info);

	*watchdog_if = (struct kbase_hwcnt_watchdog_interface){
		.timer = NULL, .enable = NULL, .disable = NULL, .modify = NULL
	};
}

int kbase_hwcnt_watchdog_if_timer_create(struct kbase_hwcnt_watchdog_interface *const watchdog_if)
{
	struct kbase_hwcnt_watchdog_if_timer_info *timer_info;

	if (WARN_ON(!watchdog_if))
		return -EINVAL;

	timer_info = kmalloc(sizeof(*timer_info), GFP_KERNEL);
	if (!timer_info)
		return -ENOMEM;

	*timer_info = (struct kbase_hwcnt_watchdog_if_timer_info){ .timer_enabled = false };

	INIT_DELAYED_WORK(&timer_info->dwork, kbasep_hwcnt_watchdog_callback);

	*watchdog_if = (struct kbase_hwcnt_watchdog_interface){
		.timer = (void *)timer_info,
		.enable = kbasep_hwcnt_watchdog_if_timer_enable,
		.disable = kbasep_hwcnt_watchdog_if_timer_disable,
		.modify = kbasep_hwcnt_watchdog_if_timer_modify,
	};

	timer_info->workq = alloc_workqueue("mali_hwc_watchdog_wq", WQ_HIGHPRI | WQ_UNBOUND, 1);
	if (timer_info->workq)
		return 0;

	kfree(timer_info);
	return -ENOMEM;
}