aboutsummaryrefslogtreecommitdiff
path: root/clocks.c
blob: 603ebe4482010714cfbd25dbc8297d1978b5f7cc (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
/*******************************************************************************
 * Copyright (C) 2010, Linaro Limited.
 *
 * This file is part of PowerDebug.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Amit Arora <amit.arora@linaro.org> (IBM Corporation)
 *       - initial API and implementation
 *
 *     Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation)
 *       - Rewrote code and API
 *
 *******************************************************************************/

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#endif
#include <mntent.h>
#include <sys/stat.h>

#include "powerdebug.h"
#include "clocks.h"
#include "tree.h"

struct clock_info {
	int flags;
	int rate;
	int usecount;
	bool expanded;
	char *prefix;
} *clocks_info;

static struct tree *clock_tree = NULL;

static int locate_debugfs(char *clk_path)
{
	const char *mtab = "/proc/mounts";
	struct mntent *mntent;
	int ret = -1;
	FILE *file = NULL;

	file = setmntent(mtab, "r");
	if (!file)
		return -1;

	while ((mntent = getmntent(file))) {

		if (strcmp(mntent->mnt_type, "debugfs"))
			continue;

		strcpy(clk_path, mntent->mnt_dir);
		ret = 0;
		break;
	}

	fclose(file);
	return ret;
}

static struct clock_info *clock_alloc(void)
{
	struct clock_info *ci;

	ci = malloc(sizeof(*ci));
	if (ci)
		memset(ci, 0, sizeof(*ci));

	return ci;
}

/*
 * This functions is a helper to read a specific file content and store
 * the content inside a variable pointer passed as parameter, the format
 * parameter gives the variable type to be read from the file.
 *
 * @path : directory path containing the file
 * @name : name of the file to be read
 * @format : the format of the format
 * @value : a pointer to a variable to store the content of the file
 * Returns 0 on success, -1 otherwise
 */
int file_read_value(const char *path, const char *name,
                    const char *format, void *value)
{
        FILE *file;
        char *rpath;
        int ret;

        ret = asprintf(&rpath, "%s/%s", path, name);
        if (ret < 0)
                return ret;

        file = fopen(rpath, "r");
        if (!file) {
                ret = -1;
                goto out_free;
        }

        ret = fscanf(file, format, value) == EOF ? -1 : 0;

        fclose(file);
out_free:
        free(rpath);
        return ret;
}

static int file_read_from_format(const char *file, int *value,
				 const char *format)
{
	FILE *f;
	int ret;

	f = fopen(file, "r");
	if (!f)
		return -1;
	ret = fscanf(f, format, value);
	fclose(f);

	return !ret ? -1 : 0;
}

static inline int file_read_int(const char *file, int *value)
{
	return file_read_from_format(file, value, "%d");
}

static inline int file_read_hex(const char *file, int *value)
{
	return file_read_from_format(file, value, "%x");
}

static inline const char *clock_rate(int *rate)
{
        int r;

        /* GHZ */
        r = *rate >> 30;
        if (r) {
                *rate = r;
                return "GHZ";
        }

        /* MHZ */
        r = *rate >> 20;
        if (r) {
                *rate = r;
                return "MHZ";
        }

        /* KHZ */
        r = *rate >> 10;
        if (r) {
                *rate = r;
                return "KHZ";
        }

        return "";
}

static int dump_clock_cb(struct tree *t, void *data)
{
	struct clock_info *clk = t->private;
	struct clock_info *pclk;
	const char *unit;
	int ret = 0;
	int rate = clk->rate;

	if (!t->parent) {
		printf("/\n");
		clk->prefix = "";
		return 0;
	}

	pclk = t->parent->private;

	if (!clk->prefix)
		ret = asprintf(&clk->prefix, "%s%s%s", pclk->prefix,
			       t->depth > 1 ? "   ": "", t->next ? "|" : " ");
	if (ret < 0)
		return -1;

	unit = clock_rate(&rate);

	printf("%s%s-- %s (flags:0x%x, usecount:%d, rate: %d %s)\n",
	       clk->prefix,  !t->next ? "`" : "", t->name, clk->flags,
	       clk->usecount, rate, unit);

	return 0;
}

int dump_clock_info(void)
{
	return tree_for_each(clock_tree, dump_clock_cb, NULL);
}

static int dump_all_parents(char *clkarg)
{
	struct tree *tree;

	tree = tree_find(clock_tree, clkarg);
	if (!tree) {
		printf("Clock NOT found!\n");
		return -1;
	}

	return tree_for_each_parent(tree, dump_clock_cb, NULL);
}

void find_parents_for_clock(char *clkname, int complete)
{
	char name[256];

	name[0] = '\0';
	if (!complete) {
		char str[256];

		strcat(name, clkname);
		sprintf(str, "Enter Clock Name : %s\n", name);
		display_reset_cursor();
		display_print_line(0, str, 1, NULL);
		display_refresh_pad();
		return;
	}
	sprintf(name, "Parents for \"%s\" Clock : \n", clkname);
	display_reset_cursor();
	display_print_line(0, name, 1, NULL);
	display_refresh_pad();
	dump_all_parents(clkname);
}

static inline int read_clock_cb(struct tree *t, void *data)
{
	struct clock_info *clk = t->private;

	file_read_value(t->path, "flags", "%x", &clk->flags);
	file_read_value(t->path, "rate", "%d", &clk->rate);
	file_read_value(t->path, "usecount", "%d", &clk->usecount);

	return 0;
}

static int read_clock_info(void)
{
	return tree_for_each(clock_tree, read_clock_cb, NULL);
}

static int fill_clock_cb(struct tree *t, void *data)
{
	struct clock_info *clk;

	clk = clock_alloc();
	if (!clk)
		return -1;
	t->private = clk;

        /* we skip the root node but we set it expanded for its children */
	if (!t->parent) {
		clk->expanded = true;
		return 0;
	}

	return read_clock_cb(t, data);
}

static int fill_clock_tree(void)
{
	return tree_for_each(clock_tree, fill_clock_cb, NULL);
}

static int is_collapsed(struct tree *t, void *data)
{
	struct clock_info *clk = t->private;

	if (!clk->expanded)
		return 1;

	return 0;
}

static char *clock_line(struct tree *t)
{
	struct clock_info *clk;
	int rate;
	const char *clkunit;
	char *clkrate, *clkname, *clkline = NULL;

	clk = t->private;
	rate = clk->rate;
	clkunit = clock_rate(&rate);

	if (asprintf(&clkname, "%*s%s", (t->depth - 1) * 2, "", t->name) < 0)
		return NULL;

	if (asprintf(&clkrate, "%d%s", rate, clkunit) < 0)
		goto free_clkname;

	if (asprintf(&clkline, "%-55s 0x%-16x %-12s %-9d %-8d", clkname,
		     clk->flags, clkrate, clk->usecount, t->nrchild) < 0)
		goto free_clkrate;

free_clkrate:
	free(clkrate);
free_clkname:
	free(clkname);

	return clkline;
}

static int clock_print_info_cb(struct tree *t, void *data)
{
	struct clock_info *clock = t->private;
	int *line = data;
	char *buffer;

        /* we skip the root node of the tree */
	if (!t->parent)
		return 0;

        /* show the clock when *all* its parent is expanded */
	if (tree_for_each_parent(t->parent, is_collapsed, NULL))
		return 0;

	buffer = clock_line(t);
	if (!buffer)
		return -1;

	display_print_line(*line, buffer, clock->usecount, t);

	(*line)++;

	free(buffer);

	return 0;
}

static int clock_print_info(void)
{
	int ret, line = 0;

	print_clock_header();

	display_reset_cursor();

	ret = tree_for_each(clock_tree, clock_print_info_cb, &line);

	display_refresh_pad();

	return ret;
}

int clock_toggle_expanded(void)
{
	struct tree *t = display_get_row_data();
	struct clock_info *clk = t->private;

	clk->expanded = !clk->expanded;

	return 0;
}

/*
 * Initialize the clock framework
 */
int clock_init(void)
{
	char clk_dir_path[PATH_MAX];

	if (locate_debugfs(clk_dir_path))
		return -1;

	sprintf(clk_dir_path, "%s/clock", clk_dir_path);

	if (access(clk_dir_path, F_OK))
		return -1;

	clock_tree = tree_load(clk_dir_path, NULL);
	if (!clock_tree)
		return -1;

	return fill_clock_tree();
}

/*
 * Read the clock information and fill the tree with the information
 * found in the files. Then print the result to the text based interface
 * Return 0 on success, < 0 otherwise
 */
int read_and_print_clock_info(void)
{
	if (read_clock_info())
		return -1;

	return clock_print_info();
}

/*
 * Read the clock information and fill the tree with the information
 * found in the files. Then dump to stdout a formatted result.
 * @clk : a name for a specific clock we want to show
 * Return 0 on success, < 0 otherwise
 */
int read_and_dump_clock_info(char *clk)
{
	int ret;

	if (read_clock_info())
		return -1;

	if (clk) {
		printf("\nParents for \"%s\" Clock :\n\n", clk);
		ret = dump_all_parents(clk);
		printf("\n\n");
	} else {
		printf("\nClock Tree :\n");
		printf("**********\n");
		ret = dump_clock_info();
		printf("\n\n");
	}

	return ret;
}