summaryrefslogtreecommitdiff
path: root/drivers/misc/tegra-profiler/power_clk.c
blob: aaa5e5de7779a6f060fc0d83ac9cf15085069777 (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
442
443
/*
 * drivers/misc/tegra-profiler/power_clk.c
 *
 * Copyright (c) 2015, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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.
 *
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>
#include <linux/cpufreq.h>
#include <linux/clk.h>
#include <linux/notifier.h>
#include <linux/cpu.h>
#include <linux/timer.h>

#include <linux/tegra_profiler.h>

#include "power_clk.h"
#include "quadd.h"
#include "hrt.h"
#include "comm.h"
#include "debug.h"

#define POWER_CLK_MAX_VALUES	32

typedef int (*notifier_call_ft)(struct notifier_block *, unsigned long, void *);

struct power_clk_data {
	unsigned long value;
	unsigned long prev;
};

struct power_clk_source {
	int type;

	struct clk *clkp;
	struct notifier_block nb;

	int nr;
	struct power_clk_data data[POWER_CLK_MAX_VALUES];

	unsigned long long counter;
	atomic_t active;

	struct mutex lock;
};

struct power_clk_context_s {
	struct power_clk_source cpu;
	struct power_clk_source gpu;
	struct power_clk_source emc;

	struct timer_list timer;
	unsigned int period;

	struct quadd_ctx *quadd_ctx;
};

enum {
	QUADD_POWER_CLK_CPU = 1,
	QUADD_POWER_CLK_GPU,
	QUADD_POWER_CLK_EMC,
};

static struct power_clk_context_s power_ctx;

static void check_clks(void);

static void read_source(struct power_clk_source *s)
{
	int i;

	mutex_lock(&s->lock);

	switch (s->type) {
	case QUADD_POWER_CLK_CPU:
		/* update cpu frequency */
		for (i = 0; i < nr_cpu_ids; i++)
			s->data[i].value = cpufreq_get(i);
		break;

	case QUADD_POWER_CLK_GPU:
		/* update gpu frequency */
		s->clkp = clk_get_sys("3d", NULL);
		if (s->clkp) {
			s->data[0].value =
				clk_get_rate(s->clkp) / 1000;
			clk_put(s->clkp);
		}
		break;

	case QUADD_POWER_CLK_EMC:
		/* update emc frequency */
		s->clkp = clk_get_sys("cpu", "emc");
		if (s->clkp) {
			s->data[0].value =
				clk_get_rate(s->clkp) / 1000;
			clk_put(s->clkp);
		}
		break;

	default:
		pr_err_once("%s: error: invalid power_clk type\n", __func__);
		return;
	}

	mutex_unlock(&s->lock);
	s->counter++;
}

static int
gpu_notifier_call(struct notifier_block *nb, unsigned long val, void *ptr)
{
	read_source(&power_ctx.gpu);
	check_clks();

	return 0;
}

static int
emc_notifier_call(struct notifier_block *nb, unsigned long val, void *ptr)
{
	read_source(&power_ctx.emc);
	check_clks();

	return 0;
}

static int
cpu_notifier_call(struct notifier_block *nb, unsigned long val, void *ptr)
{
	read_source(&power_ctx.cpu);

#ifndef CONFIG_COMMON_CLK
	read_source(&power_ctx.gpu);
	read_source(&power_ctx.emc);
#endif

	check_clks();

	return 0;
}

static void make_sample(void)
{
	int i;
	u32 extra_cpus[NR_CPUS];
	struct power_clk_source *s;
	struct quadd_iovec vec;

	struct quadd_record_data record;
	struct quadd_power_rate_data *power_rate = &record.power_rate;

	record.record_type = QUADD_RECORD_TYPE_POWER_RATE;

	power_rate->time = quadd_get_time();

	s = &power_ctx.cpu;
	mutex_lock(&s->lock);
	if (atomic_read(&s->active)) {
		power_rate->nr_cpus = s->nr;
		for (i = 0; i < s->nr; i++)
			extra_cpus[i] = s->data[i].value;
	} else {
		power_rate->nr_cpus = 0;
	}
	mutex_unlock(&s->lock);

	s = &power_ctx.gpu;
	mutex_lock(&s->lock);
	if (atomic_read(&s->active))
		power_rate->gpu = s->data[0].value;
	else
		power_rate->gpu = 0;

	mutex_unlock(&s->lock);

	s = &power_ctx.emc;
	mutex_lock(&s->lock);
	if (atomic_read(&s->active))
		power_rate->emc = s->data[0].value;
	else
		power_rate->emc = 0;

	mutex_unlock(&s->lock);
/*
	pr_debug("make_sample: cpu: %u/%u/%u/%u, gpu: %u, emc: %u\n",
		 extra_cpus[0], extra_cpus[1], extra_cpus[2], extra_cpus[3],
		 power_rate->gpu, power_rate->emc);
*/
	vec.base = extra_cpus;
	vec.len = power_rate->nr_cpus * sizeof(extra_cpus[0]);

	quadd_put_sample(&record, &vec, 1);
}

static inline int is_data_changed(struct power_clk_source *s)
{
	int i;

	mutex_lock(&s->lock);
	for (i = 0; i < s->nr; i++) {
		if (s->data[i].value != s->data[i].prev) {
			mutex_unlock(&s->lock);
			return 1;
		}
	}
	mutex_unlock(&s->lock);

	return 0;
}

static inline void update_data(struct power_clk_source *s)
{
	int i;

	mutex_lock(&s->lock);

	for (i = 0; i < s->nr; i++)
		s->data[i].prev = s->data[i].value;

	mutex_unlock(&s->lock);
}

static void check_clks(void)
{
	int changed = 0;

	if (is_data_changed(&power_ctx.cpu)) {
		update_data(&power_ctx.cpu);
		changed = 1;
	}

	if (is_data_changed(&power_ctx.gpu)) {
		update_data(&power_ctx.gpu);
		changed = 1;
	}

	if (is_data_changed(&power_ctx.emc)) {
		update_data(&power_ctx.emc);
		changed = 1;
	}
/*
	pr_debug("cpu: %lu/%lu/%lu/%lu, gpu: %lu, emc: %lu, changed: %s\n",
		 power_ctx.cpu.data[0].value, power_ctx.cpu.data[1].value,
		 power_ctx.cpu.data[2].value, power_ctx.cpu.data[3].value,
		 power_ctx.gpu.data[0].value, power_ctx.emc.data[0].value,
		 changed ? "yes" : "no");
*/
	if (changed)
		make_sample();
}

static void reset_data(struct power_clk_source *s)
{
	int i;

	mutex_lock(&s->lock);
	for (i = 0; i < s->nr; i++) {
		s->data[i].value = 0;
		s->data[i].prev = 0;
	}
	atomic_set(s, 0);
	mutex_unlock(&s->lock);
}

static void init_source(struct power_clk_source *s,
			notifier_call_ft notifier,
			int nr_values,
			int type)
{
	s->type = type;
	s->nb.notifier_call = notifier;
	s->nr = nr_values;

	mutex_init(&s->lock);
	reset_data(s);
}

static void
power_clk_work_func(struct work_struct *dummy)
{
#ifndef CONFIG_COMMON_CLK
	read_source(&power_ctx.gpu);
	read_source(&power_ctx.emc);

	check_clks();
#endif
}

static DECLARE_WORK(power_clk_work, power_clk_work_func);

static void power_clk_timer(unsigned long data)
{
	struct timer_list *timer = &power_ctx.timer;

	schedule_work(&power_clk_work);
	timer->expires = jiffies + msecs_to_jiffies(power_ctx.period);
	add_timer(timer);
}

int quadd_power_clk_start(void)
{
	struct power_clk_source *s;
	int status;
	struct timer_list *timer = &power_ctx.timer;
	struct quadd_parameters *param = &power_ctx.quadd_ctx->param;

	if (param->power_rate_freq == 0) {
		pr_info("power_clk is not started\n");
		return 0;
	}

#ifdef CONFIG_COMMON_CLK
	power_ctx.period = 0;
#else
	power_ctx.period = MSEC_PER_SEC / param->power_rate_freq;
#endif
	pr_info("power_clk: start, freq: %d\n",
		param->power_rate_freq);

	/* setup gpu frequency */
	s = &power_ctx.gpu;
	s->clkp = clk_get_sys("3d", NULL);
	if (s->clkp) {
#ifdef CONFIG_COMMON_CLK
		status = clk_notifier_register(s->clkp, s->nb);
		if (status < 0) {
			pr_err("error: could not setup gpu freq\n");
			return status;
		}
		clk_put(s->clkp);
#endif
		reset_data(s);
		atomic_set(&s->active, 1);
	} else {
		pr_err("error: could not setup gpu freq\n");
		atomic_set(&s->active, 0);
	}

	/* setup emc frequency */
	s = &power_ctx.emc;
	s->clkp = clk_get_sys("cpu", "emc");
	if (s->clkp) {
#ifdef CONFIG_COMMON_CLK
		status = clk_notifier_register(s->clkp, s->nb);
		if (status < 0) {
			pr_err("error: could not setup emc freq\n");
			return status;
		}
		clk_put(s->clkp);
#endif
		reset_data(s);
		atomic_set(&s->active, 1);
	} else {
		pr_err("error: could not setup emc freq\n");
		atomic_set(&s->active, 0);
	}

	/* setup cpu frequency notifier */
	s = &power_ctx.cpu;
	status = register_cpu_notifier(&s->nb);
	if (status < 0) {
		pr_err("error: could not setup cpu freq\n");
		return status;
	}
	reset_data(s);

	if (power_ctx.period > 0) {
		init_timer(timer);
		timer->function = power_clk_timer;
		timer->expires = jiffies + msecs_to_jiffies(power_ctx.period);
		timer->data = 0;
		add_timer(timer);
	}

	atomic_set(&s->active, 1);

	return 0;
}

void quadd_power_clk_stop(void)
{
	struct power_clk_source *s;

	if (power_ctx.quadd_ctx->param.power_rate_freq == 0)
		return;

	if (power_ctx.period > 0)
		del_timer_sync(&power_ctx.timer);

	s = &power_ctx.gpu;
	if (atomic_cmpxchg(&s->active, 1, 0)) {
#ifdef CONFIG_COMMON_CLK
		if (s->clkp)
			clk_notifier_unregister(s->clkp, &s->nb);
#endif
	}

	s = &power_ctx.emc;
	if (atomic_cmpxchg(&s->active, 1, 0)) {
#ifdef CONFIG_COMMON_CLK
		if (s->clkp)
			clk_notifier_unregister(s->clkp, &s->nb);
#endif
	}

	s = &power_ctx.cpu;
	if (atomic_cmpxchg(&s->active, 1, 0)) {
		pr_info("power_clk: stop\n");
		unregister_cpu_notifier(&s->nb);
	}
}

int quadd_power_clk_init(struct quadd_ctx *quadd_ctx)
{
	pr_info("power_clk: init\n");

	init_source(&power_ctx.cpu, cpu_notifier_call, nr_cpu_ids,
		    QUADD_POWER_CLK_CPU);
	init_source(&power_ctx.gpu, gpu_notifier_call, 1, QUADD_POWER_CLK_GPU);
	init_source(&power_ctx.emc, emc_notifier_call, 1, QUADD_POWER_CLK_EMC);

	power_ctx.quadd_ctx = quadd_ctx;

	return 0;
}

void quadd_power_clk_deinit(void)
{
	pr_info("power_clk: deinit\n");
	quadd_power_clk_stop();
}