summaryrefstree
path: root/kernel/drivers/mpsd/mpsd_timer.c
blob: 1453a2fc06191544767183e949edf90fb7b82c8f (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * drivers/sysintelligence/mpsd_timer.c
 *
 * Source file for mpsd param synchronous read code.
 *
 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
 *	   Ravi Solanki <ravi.siso@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/hrtimer.h>
#include <linux/workqueue.h>

#include "mpsd_param.h"

#define NSEC_PER_MSEC 1000000L
#define MIN_TIMER_LIMIT (DEFAULT_TIMER_MIN_LIMIT)

static DEFINE_SPINLOCK(mpsd_timer_list_lock);

static struct workqueue_struct *mpsd_timer_wq;

static int timer_pid = DEFAULT_PID;

/**
 * struct mpsd_timer - Struct for timer framework.
 * @value: Value in msec for the timer to be run.
 * @node_count: Total number of timers (params reg) with same timer value.
 * @timer: Timer structure handle.
 * @list: Head node of the timer list.
 * @app_field_mask: Bit field to be update on the app params registration.
 * @dev_field_mask: Bit field to be update on the dev params registration.
 *
 * This structure contains the information of the mpsd timer framework
 * for registering the mpsd params for timed update.
 */
struct mpsd_timer {
	long long value;
	int node_count;
	struct hrtimer timer;
	struct list_head list;
	unsigned long long app_field_mask;
	unsigned long long dev_field_mask;
};

static struct mpsd_timer mpsd_timers = {
	.list = LIST_HEAD_INIT(mpsd_timers.list),
};

/**
 * struct mpsd_timer_work - Struct to pass timer work related data.
 * @timer_work: work handle for work queue.
 * @app_field_mask: Bit field to be update on the app params registration.
 * @dev_field_mask: Bit field to be update on the dev params registration.
 *
 * This structure contains the information of the mpsd timer work queue
 * related data which will be filled from timer callback.
 */
struct mpsd_timer_work {
	struct work_struct timer_work;
	unsigned long long app_field_mask;
	unsigned long long dev_field_mask;
};

struct mpsd_timer_work mpsd_timer_cb_work;

static inline int get_timer_pid(void)
{
	return timer_pid;
}

void mpsd_set_timer_pid(int pid)
{
	if (pid <= 0)
		pid = DEFAULT_PID;
	timer_pid = pid;
}

static struct mpsd_timer *mpsd_timer_set(s64 time)
{
	struct mpsd_timer *t = NULL;
	struct mpsd_timer *tl_head = &mpsd_timers;

	if (time < MIN_TIMER_LIMIT)
		time = MIN_TIMER_LIMIT;

	t = kmalloc(sizeof(*t), GFP_KERNEL);
	if (!t)
		return t;

	t->value = time;
	t->node_count = 0;
	t->app_field_mask = 0;
	t->dev_field_mask = 0;
	INIT_LIST_HEAD(&t->list);

	spin_lock(&mpsd_timer_list_lock);
	list_add_tail(&t->list, &tl_head->list);
	spin_unlock(&mpsd_timer_list_lock);

	return t;
}

static enum hrtimer_restart timer_callback(struct hrtimer *timer)
{
	struct mpsd_timer *t;

#ifdef CONFIG_MPSD_DEBUG
	if (get_mpsd_debug_level() > DEBUG_LEVEL_MIN)
		pr_info("%s: %s:\n", tag, __func__);
#endif

	t = container_of(timer, struct mpsd_timer, timer);

	mpsd_timer_cb_work.app_field_mask = t->app_field_mask;
	mpsd_timer_cb_work.dev_field_mask = t->dev_field_mask;
	queue_work(mpsd_timer_wq, &mpsd_timer_cb_work.timer_work);

	hrtimer_forward(timer, hrtimer_cb_get_time(timer),
		ms_to_ktime(t->value));
#ifdef CONFIG_MPSD_DEBUG
	if (get_mpsd_debug_level() > DEBUG_LEVEL_MIN) {
		pr_info("%s: %s: Callback Done => Val[%lld]:Masks[%llu %llu]\n",
			tag, __func__, t->value, t->app_field_mask,
			t->dev_field_mask);
	}

	if (get_debug_level_timer() <=	get_mpsd_debug_level()) {
		print_timer_events_data(t->value, t->node_count,
			t->app_field_mask, t->dev_field_mask);
	}
#endif
	return HRTIMER_RESTART;
}

static int start_timer(struct mpsd_timer *t)
{
	ktime_t ktime;

	pr_info("%s: %s: Starting the timer for Val[%lld]\n", tag, __func__,
			t->value);

	hrtimer_init(&t->timer, CLOCK_MONOTONIC,  HRTIMER_MODE_REL);
	ktime = ms_to_ktime(t->value);
	t->timer.function = &timer_callback;
	hrtimer_start(&t->timer, ktime, HRTIMER_MODE_REL);

	return 0;
}

int mpsd_timer_register(int param_id, int val)
{
	long long time = (long long int)val;
	int is_app_param = 0;
	struct mpsd_timer *t;
	struct mpsd_timer *tl_head = &mpsd_timers;

	if (is_valid_param(param_id) == 0) {
		pr_err("%s: %s: Parameter is not Valid\n", tag, __func__);
		return -ENOENT;
	}

	//delete existing param entry
	spin_lock(&mpsd_timer_list_lock);
	if (is_valid_app_param(param_id) == 1)
		is_app_param = 1;

	list_for_each_entry(t, &tl_head->list, list) {
		unsigned long long bit_field = is_app_param ?
			t->app_field_mask : t->dev_field_mask;

		if (get_param_bit(bit_field, param_id)) {
			spin_unlock(&mpsd_timer_list_lock);
#ifdef CONFIG_MPSD_DEBUG
		if (get_mpsd_debug_level() > DEBUG_LEVEL_MIN)
			pr_info("%s: %s Exist, Dereg first from VAL[%lld]\n",
				tag, __func__, t->value);
#endif
			mpsd_timer_deregister(param_id);
			spin_lock(&mpsd_timer_list_lock);
			goto deleted;
		}
	}

deleted:
	list_for_each_entry(t, &tl_head->list, list) {
		if (t->value == time) {
			spin_unlock(&mpsd_timer_list_lock);
			goto found;
		}
	}
	spin_unlock(&mpsd_timer_list_lock);
	t = mpsd_timer_set(time);
	if (!t) {
		pr_err("%s: timer allocation failed\n", __func__);
		return -ENOMEM;
	}

#ifdef CONFIG_MPSD_DEBUG
	if (get_mpsd_debug_level() > DEBUG_LEVEL_MIN) {
		pr_info("%s: %s Added Timer[%lld] in List\n", tag, __func__,
			t->value);
	}
#endif

found:
	spin_lock(&mpsd_timer_list_lock);

	if (is_app_param == 1)
		set_param_bit(&t->app_field_mask, param_id);
	else
		set_param_bit(&t->dev_field_mask, param_id);
	if ((++t->node_count) == 1) {
		pr_info("%s: %s: ID:%d Started VAL[%lld] node_count=%d\n",
			tag, __func__, param_id, t->value, t->node_count);
		start_timer(t);
	}
	spin_unlock(&mpsd_timer_list_lock);

#ifdef CONFIG_MPSD_DEBUG
	if (get_mpsd_debug_level() > DEBUG_LEVEL_MIN) {
		pr_info("%s: %s:ID:%d Reg VAL[%lld] count=%d MASK[%llu %llu]\n",
			tag, __func__, param_id,
			t->value, t->node_count, t->app_field_mask,
			t->dev_field_mask);
	}

	if (get_debug_level_timer() <=	get_mpsd_debug_level())
		print_timer_reg_data(0, NULL);
#endif
	return 0;
}

int mpsd_timer_deregister(int param_id)
{
	int is_app_param = 0;
	struct mpsd_timer *t;
	struct mpsd_timer *tl_head = &mpsd_timers;

	if (is_valid_param(param_id) == 0) {
		pr_err("%s: %s: Parameter is not Valid\n", tag, __func__);
		return -ENOENT;
	}

#ifdef CONFIG_MPSD_DEBUG
	if (get_mpsd_debug_level() > DEBUG_LEVEL_MIN)
		pr_info("%s: %s: ID:%d Unreg came\n", tag, __func__, param_id);
#endif

	spin_lock(&mpsd_timer_list_lock);
	if (is_valid_app_param(param_id) == 1)
		is_app_param = 1;

	list_for_each_entry(t, &tl_head->list, list) {
		unsigned long long bit_field = is_app_param ?
			t->app_field_mask : t->dev_field_mask;

		if (get_param_bit(bit_field, param_id)) {
			clear_param_bit(&bit_field, param_id);
			if (is_app_param == 1)
				t->app_field_mask = bit_field;
			else
				t->dev_field_mask = bit_field;
			--t->node_count;
			goto done;
		}
	}

	pr_info("%s: %s: ID[%d] Not Found\n", tag, __func__, param_id);
	spin_unlock(&mpsd_timer_list_lock);
	return -EINVAL;

done:

#ifdef CONFIG_MPSD_DEBUG
	if (get_mpsd_debug_level() > DEBUG_LEVEL_MIN) {
		pr_info("%s: %s:ID:%d Unreg VAL[%lld] ct=%d MASK[%llu %llu]\n",
			tag, __func__, param_id,
			t->value, t->node_count, t->app_field_mask,
			t->dev_field_mask);
	}
#endif

	if (t->node_count == 0) {
		hrtimer_cancel(&t->timer);
		list_del(&t->list);
		kfree(t);
	}
	spin_unlock(&mpsd_timer_list_lock);

#ifdef CONFIG_MPSD_DEBUG
	if (get_mpsd_debug_level() > DEBUG_LEVEL_MIN)
		pr_info("%s: %s: ID:%d Unreg Done!\n", tag, __func__, param_id);

	if (get_debug_level_timer() <=	get_mpsd_debug_level())
		print_timer_reg_data(0, NULL);
#endif

	return 0;
}

void mpsd_timer_deregister_all(void)
{
	struct mpsd_timer *t, *q;
	struct mpsd_timer *tl_head = &mpsd_timers;

	spin_lock(&mpsd_timer_list_lock);
	list_for_each_entry_safe(t, q, &tl_head->list, list) {
		hrtimer_cancel(&t->timer);
		list_del(&t->list);
		kfree(t);
	}
	spin_unlock(&mpsd_timer_list_lock);

	pr_info("%s: %s: Done!\n", tag, __func__);
#ifdef CONFIG_MPSD_DEBUG
	if (get_debug_level_timer() <=	get_mpsd_debug_level())
		print_timer_reg_data(0, NULL);
#endif
	flush_work(&mpsd_timer_cb_work.timer_work);
}

void mpsd_timer_suspend(void)
{
	struct mpsd_timer *t, *q;
	struct mpsd_timer *tl_head =  &mpsd_timers;

	spin_lock(&mpsd_timer_list_lock);
	list_for_each_entry_safe(t, q, &tl_head->list, list) {
		hrtimer_cancel(&t->timer);
	}
	spin_unlock(&mpsd_timer_list_lock);
	pr_info("%s: %s: Done!\n", tag, __func__);
#ifdef CONFIG_MPSD_DEBUG
	if (get_debug_level_timer() <=	get_mpsd_debug_level())
		print_timer_reg_data(0, NULL);
#endif
}

void mpsd_timer_resume(void)
{
	ktime_t ktime;
	struct mpsd_timer *t, *q;
	struct mpsd_timer *tl_head =  &mpsd_timers;

	spin_lock(&mpsd_timer_list_lock);
	list_for_each_entry_safe(t, q, &tl_head->list, list) {
		hrtimer_cancel(&t->timer);
		ktime = ms_to_ktime(t->value);
		hrtimer_start(&t->timer, ktime, HRTIMER_MODE_REL);
	}
	spin_unlock(&mpsd_timer_list_lock);
	pr_info("%s: %s: Done!\n", tag, __func__);
#ifdef CONFIG_MPSD_DEBUG
	if (get_debug_level_timer() <=	get_mpsd_debug_level())
		print_timer_reg_data(0, NULL);
#endif
}

static void mpsd_timer_cb_work_fn(struct work_struct *work)
{
	struct mpsd_timer_work *cb_work = container_of(work,
		struct mpsd_timer_work, timer_work);

#ifdef CONFIG_MPSD_DEBUG
	if (get_mpsd_debug_level() > DEBUG_LEVEL_MIN) {
		pr_info("%s: %s: Added in queue => Masks[%llu %llu]\n",
			tag, __func__, cb_work->app_field_mask,
			cb_work->dev_field_mask);
	}
#endif

	mpsd_populate_mmap(cb_work->app_field_mask, cb_work->dev_field_mask,
		get_timer_pid(), TIMED_UPDATE, DEFAULT_EVENTS);
}

void mpsd_timer_init(void)
{
	pr_info("%s: %s:\n", tag, __func__);

	mpsd_timer_wq = alloc_ordered_workqueue("mpsd_timer_wq",
		WQ_FREEZABLE);

	BUG_ON(!mpsd_timer_wq);

	mpsd_timer_cb_work.app_field_mask = DEFAULT_APP_FIELD_MASK;
	mpsd_timer_cb_work.dev_field_mask = DEFAULT_DEV_FIELD_MASK;
	INIT_WORK(&mpsd_timer_cb_work.timer_work, mpsd_timer_cb_work_fn);
	pr_info("%s: %s: Done!\n", tag, __func__);
}

void mpsd_timer_exit(void)
{
	pr_info("%s: %s:\n", tag, __func__);

	mpsd_timer_deregister_all();
	destroy_workqueue(mpsd_timer_wq);
	pr_info("%s: %s: Done!\n", tag, __func__);
}

#ifdef CONFIG_MPSD_DEBUG
int print_timer_reg_data(int in_buf, char *buf)
{
	int ret = 0;
	struct mpsd_timer *t = NULL;
	struct mpsd_timer *tl_head = &mpsd_timers;

	spin_lock(&mpsd_timer_list_lock);
	pr_info("%s: %s: MPSD_TIMER_REG_DATA==>\n",	 tag, __func__);
	list_for_each_entry(t, &tl_head->list, list) {
		pr_info("%s: %s: VAL[%lld] COUNT[%d] MASKS[%llu %llu]\n",
			tag, __func__, t->value, t->node_count,
			t->app_field_mask, t->dev_field_mask);
	}
	spin_unlock(&mpsd_timer_list_lock);
	if (in_buf == 1) {
		if (!buf) {
			pr_err("%s: %s: Buf passed in NULL\n",	tag, __func__);
			return ret;
		}
		ret += snprintf(buf, BUFFER_LENGTH_MAX,
			"\nMPSD_TIMER_REG_DATA==>\n");
		spin_lock(&mpsd_timer_list_lock);
		list_for_each_entry(t, &tl_head->list, list) {
			ret += snprintf(buf + ret, BUFFER_LENGTH_MAX,
				"VAL[%lld] COUNT[%d] MASKS[%llu %llu]\n",
				t->value, t->node_count,
				t->app_field_mask, t->dev_field_mask);
		}
		spin_unlock(&mpsd_timer_list_lock);
	}

	return ret;
}
#endif