blob: 7bedfa523f29a18277275933467bd4b6b3cd9224 [file] [log] [blame]
Amit Arora728e0c92010-09-14 12:06:09 +05301/*******************************************************************************
2 * Copyright (C) 2010, Linaro
3 * Copyright (C) 2010, IBM Corporation
4 *
5 * This file is part of PowerDebug.
6 *
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * http://www.eclipse.org/legal/epl-v10.html
11 *
12 * Contributors:
13 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
14 * - initial API and implementation
15 *******************************************************************************/
16
17#include "powerdebug.h"
Amit Aroraed3e5652010-10-27 12:02:53 +053018#include "clocks.h"
Amit Arora24ed7d12010-09-14 12:12:58 +053019#include <errno.h>
Amit Aroraeb6cba92010-10-25 16:03:21 +053020#include <sys/stat.h>
Amit Arora24ed7d12010-09-14 12:12:58 +053021
22static int clk_tree_level = 1;
23static char clk_dir_path[PATH_MAX];
24static char highlighted_path[PATH_MAX];
25static char clk_name[NAME_MAX];
26static int gadder = 0;
27
28
29void init_clock_details(void)
30{
Amit Aroraed3e5652010-10-27 12:02:53 +053031 char *path = debugfs_locate_mpoint();
32 struct stat buf;
33
34
35 if (path)
36 strcpy(clk_dir_path, path);
37 else {
38 fprintf(stderr, "powerdebug: Unable to locate debugfs mount"
39 " point. Mount debugfs and try again..\n");
40 exit(1);
41 }
42 sprintf(clk_dir_path, "%s/clock", clk_dir_path);
43 //strcpy(clk_dir_path, "/debug/clock"); // Hardcoded for testing..
44 if (stat(clk_dir_path, &buf)) {
45 fprintf(stderr, "powerdebug: Unable to find clock tree"
46 " information at %s. Exiting..\n", clk_dir_path);
47 exit(1);
48 }
Amit Arora24ed7d12010-09-14 12:12:58 +053049 strcpy(clk_name, "");
50 strcpy(highlighted_path, "");
51}
Amit Arora728e0c92010-09-14 12:06:09 +053052
53int get_int_from(char *file)
54{
55 FILE *filep;
56 char result[NAME_MAX];
57 int ret;
58
59 filep = fopen(file, "r");
60
61 if (!filep)
62 return -1; //TBD : What should we return on failure, here ?
63
64 ret = fscanf(filep, "%s", result);
65 fclose(filep);
66
67 return atoi(result);
68}
69
Amit Arora24ed7d12010-09-14 12:12:58 +053070int read_and_print_clock_info(int verbose, int hrow, int selected)
71{
72 if (selected) {
73 // if (!(strcmp(clk_dir_path, "/debug/clock") &&
74 // strcmp(highlighted_path, ".."))) {
75 strcpy(clk_dir_path, highlighted_path);
76 hrow = 0;
77 clk_tree_level += gadder;
78 if (clk_tree_level <=0)
79 clk_tree_level = 1;
80 }
81
82 hrow = read_and_print_clock_one_level(verbose, hrow, selected);
83
84 return hrow;
85}
86
87void set_hl_dir_path_to_parent(void)
88{
89 char *ptr;
90
91 strcpy(highlighted_path, clk_dir_path);
92 if (strcmp(clk_dir_path, "/clock/debug")) {
93 ptr = strrchr(highlighted_path, '/');
94 if (ptr)
95 ptr[0]='\0';
96 }
97}
98
99int read_and_print_clock_one_level(int verbose, int hrow, int selected)
Amit Arora728e0c92010-09-14 12:06:09 +0530100{
101 int line = 0, usecount = 0, flags = 0, rate = 0;
Amit Arora24ed7d12010-09-14 12:12:58 +0530102// int parent_dir_row = 1000;
Amit Arora728e0c92010-09-14 12:06:09 +0530103 DIR *dir, *subdir;
104 char filename[PATH_MAX], devpath[PATH_MAX], clockname[NAME_MAX];
105 struct dirent *item, *subitem;
106
Amit Arora29cb7572010-10-05 17:40:29 +0530107 (void)verbose;
108
Amit Arora24ed7d12010-09-14 12:12:58 +0530109 print_clock_header(clk_tree_level);
Amit Arora728e0c92010-09-14 12:06:09 +0530110
Amit Arora24ed7d12010-09-14 12:12:58 +0530111 sprintf(filename, "%s", clk_dir_path);
Amit Arora728e0c92010-09-14 12:06:09 +0530112
113 dir = opendir(filename);
114 if (!dir)
115 return 0;
116
117 while ((item = readdir(dir))) {
Amit Arora24ed7d12010-09-14 12:12:58 +0530118 /* skip hidden dirs except ".." */
119 if (item->d_name[0] == '.' && strcmp(item->d_name, ".."))
Amit Arora728e0c92010-09-14 12:06:09 +0530120 continue;
121
Amit Arora24ed7d12010-09-14 12:12:58 +0530122 if (selected && hrow == line && !strcmp(item->d_name, "..")) {
123 sprintf(devpath, "%s", clk_dir_path);
124 strcpy(clockname, "..");
125 } else {
126 sprintf(devpath, "%s/%s", clk_dir_path, item->d_name);
127 strcpy(clockname, item->d_name);
128 }
Amit Arora728e0c92010-09-14 12:06:09 +0530129
130 subdir = opendir(devpath);
131
132 if (!subdir)
133 continue;
134
135 while ((subitem = readdir(subdir))) {
136 if (subitem->d_name[0] == '.') /* skip hidden files */
137 continue;
138
139 sprintf(filename, "%s/%s", devpath, subitem->d_name);
140
141 if (!strcmp(subitem->d_name, "flags"))
142 flags = get_int_from(filename);
143
144 if (!strcmp(subitem->d_name, "rate"))
145 rate = get_int_from(filename);
146
147 if (!strcmp(subitem->d_name, "usecount"))
148 usecount = get_int_from(filename);
149 }
150
Amit Arora24ed7d12010-09-14 12:12:58 +0530151 if (hrow == line) {
152 if (!strcmp(clockname, "..")) {
153 if (clk_tree_level != 1) {
154 set_hl_dir_path_to_parent();
155 gadder = -1;
156 }
157 } else {
158 strcpy(highlighted_path, devpath);
159 gadder = 1;
160 }
161 }
162
163// sprintf(clockname, "%s:dp-%s:n-%s:hp-%s",
164// clockname, clk_dir_path, clk_name, highlighted_path);
165
166/* if (strcmp(clockname, "..")) {
167 int row = line + 1;
168
169 if (line > parent_dir_row)
170 row--;
171 print_clock_info_line(row, clockname, flags, rate, usecount,
172 (hrow == line) ? 1 : 0);
173 } else {
174 print_clock_info_line(0, clockname, flags, rate, usecount,
175 (hrow == line) ? 1 : 0);
176 parent_dir_row = line;
177 }
178*/
179
Amit Arora29cb7572010-10-05 17:40:29 +0530180 print_clock_info_line(line, clockname, flags, rate, usecount,
181 (hrow == line) ? 1 : 0);
182 line++;
183
Amit Arora728e0c92010-09-14 12:06:09 +0530184 closedir(subdir);
185 }
186
187 closedir(dir);
188
189 if (hrow >= (line - 1))
190 hrow = -1;
191 return hrow;
192}
Amit Arora0e512722010-10-01 12:24:16 +0530193
194void dump_clock_info(int verbose)
195{
Amit Aroraeb6cba92010-10-25 16:03:21 +0530196 (void)verbose;
Amit Arora0e512722010-10-01 12:24:16 +0530197 printf("Clock Tree :\n");
198 printf("**********\n");
Amit Aroraeb6cba92010-10-25 16:03:21 +0530199 read_clock_info(clk_dir_path);
200 print_clock_info(clocks_info, 1, 1);
Amit Arora0e512722010-10-01 12:24:16 +0530201}
202
Amit Aroraeb6cba92010-10-25 16:03:21 +0530203void read_clock_info(char *clkpath)
Amit Arora0e512722010-10-01 12:24:16 +0530204{
Amit Aroraeb6cba92010-10-25 16:03:21 +0530205 DIR *dir;
206 struct dirent *item;
207 char filename[NAME_MAX], clockname[NAME_MAX];
208 struct clock_info *child;
209 struct clock_info *cur;
Amit Arora0e512722010-10-01 12:24:16 +0530210
Amit Aroraeb6cba92010-10-25 16:03:21 +0530211 dir = opendir(clkpath);
Amit Arora0e512722010-10-01 12:24:16 +0530212 if (!dir)
213 return;
214
Amit Aroraeb6cba92010-10-25 16:03:21 +0530215 clocks_info = (struct clock_info *)malloc(sizeof(struct clock_info));
216 memset(clocks_info, 0, sizeof(clocks_info));
217 strcpy(clocks_info->name, "/");
Amit Arora0e512722010-10-01 12:24:16 +0530218
Amit Aroraeb6cba92010-10-25 16:03:21 +0530219 while ((item = readdir(dir))) {
220 /* skip hidden dirs except ".." */
221 if (item->d_name[0] == '.')
222 continue;
223
224 strcpy(clockname, item->d_name);
225 sprintf(filename, "%s/%s", clkpath, item->d_name);
226 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
227 memset(cur, 0, sizeof(cur));
228 strcpy(cur->name, clockname);
229 cur->parent = clocks_info;
230 insert_children(&clocks_info, cur);
231 child = read_clock_info_recur(filename, 2, cur);
232 }
233 closedir(dir);
234}
235
236struct clock_info *read_clock_info_recur(char *clkpath, int level,
237 struct clock_info *parent)
238{
239 int ret = 0;
240 DIR *dir;
241 char filename[PATH_MAX];
242 struct dirent *item;
243 struct clock_info *cur = NULL;
244 struct stat buf;
245
246 dir = opendir(clkpath);
247 if (!dir)
248 return NULL;
249
250 while ((item = readdir(dir))) {
251 struct clock_info *child;
Amit Arora0e512722010-10-01 12:24:16 +0530252 /* skip hidden dirs except ".." */
253 if (item->d_name[0] == '.' )
254 continue;
255
Amit Aroraeb6cba92010-10-25 16:03:21 +0530256 sprintf(filename, "%s/%s", clkpath, item->d_name);
Amit Arora0e512722010-10-01 12:24:16 +0530257
Amit Aroraeb6cba92010-10-25 16:03:21 +0530258 ret = stat(filename, &buf);
Amit Arora0e512722010-10-01 12:24:16 +0530259
Amit Aroraeb6cba92010-10-25 16:03:21 +0530260 if (ret < 0) {
261 printf("Error doing a stat on %s\n", filename);
262 exit(1);
263 }
264
265 if (S_ISREG(buf.st_mode))
266 {
267 if (!strcmp(item->d_name, "flags"))
268 parent->flags = get_int_from(filename);
269 if (!strcmp(item->d_name, "rate"))
270 parent->rate = get_int_from(filename);
271 if (!strcmp(item->d_name, "usecount"))
272 parent->usecount = get_int_from(filename);
273 continue;
274 }
275
276 if (!S_ISDIR(buf.st_mode))
Amit Arora0e512722010-10-01 12:24:16 +0530277 continue;
278
Amit Aroraeb6cba92010-10-25 16:03:21 +0530279 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
280 memset(cur, 0, sizeof(cur));
281 strcpy(cur->name, item->d_name);
282 cur->children = NULL;
283 cur->parent = NULL;
284 cur->num_children = 0;
285 child = read_clock_info_recur(filename, level + 1, cur);
Amit Arora0e512722010-10-01 12:24:16 +0530286
Amit Aroraeb6cba92010-10-25 16:03:21 +0530287 insert_children(&parent, cur);
288 cur->parent = parent;
Amit Arora0e512722010-10-01 12:24:16 +0530289 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530290 closedir(dir);
291
292 return cur;
293}
294
295void insert_children(struct clock_info **parent, struct clock_info *clk)
296{
297 if (!(*parent)->children) {
298 (*parent)->children = (struct clock_info **)
299 malloc(sizeof(struct clock_info *)*2);
300 (*parent)->num_children = 0;
301 } else
302 (*parent)->children = (struct clock_info **)
303 realloc((*parent)->children,
304 sizeof(struct clock_info *) *
305 ((*parent)->num_children + 2));
306 if ((*parent)->num_children > 0)
307 (*parent)->children[(*parent)->num_children - 1]->last_child = 0;
308 clk->last_child = 1;
309 (*parent)->children[(*parent)->num_children] = clk;
310 (*parent)->children[(*parent)->num_children + 1] = NULL;
311 (*parent)->num_children++;
312}
313
314
315void print_clock_info(struct clock_info *clk, int level, int bmp)
316{
317 int i, j;
318
319 if (!clk)
320 return;
321
322 for (i = 1, j = 0; i < level; i++, j = (i - 1)) {
323 if (i == (level - 1)) {
324 if (clk->last_child)
325 printf("`-- ");
326 else
327 printf("|-- ");
328 } else {
329 if ((1<<j) & bmp)
330 printf("| ");
331 else
332 printf(" ");
333 }
334 }
335 if (clk == clocks_info)
336 printf("%s\n", clk->name);
337 else {
338 char *unit = "Hz";
339 double drate = (double)clk->rate;
340
341 if (drate > 1000 && drate < 1000000) {
342 unit = "KHz";
343 drate /= 1000;
344 }
345 if (drate > 1000000) {
346 unit = "MHz";
347 drate /= 1000000;
348 }
349 printf("%s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
350 clk->name, clk->flags, clk->usecount, drate, unit);
351 //printf("%s (flags:%d,usecount:%d,rate:%5.2f %s, bmp=0x%x)\n",
352 // clk->name, clk->flags, clk->usecount, drate, unit, bmp);
353 }
354 if (clk->children) {
355 int tbmp = bmp;
356 int xbmp = -1;
357
358 if (clk->last_child) {
359 xbmp ^= 1 << (level - 2 );
360
361 xbmp = tbmp & xbmp;
362 } else
363 xbmp = bmp;
364 for (i = 0; i<clk->num_children; i++) {
365 //if (clk->children[i]->last_child)
366 tbmp = xbmp | (1<<level);
367 print_clock_info(clk->children[i], level + 1, tbmp);
368 }
369 }
Amit Arora0e512722010-10-01 12:24:16 +0530370}
Amit Aroraed3e5652010-10-27 12:02:53 +0530371
372char *debugfs_locate_mpoint(void)
373{
374 int ret;
375 FILE *filep;
376 char **path;
377 char fsname[64];
378 struct statfs sfs;
379
380 path = likely_mpoints;
381 while (*path) {
382 ret = statfs(*path, &sfs);
383 if (ret >= 0 && sfs.f_type == (long)DEBUGFS_MAGIC)
384 return *path;
385 path++;
386 }
387
388 filep = fopen("/proc/mounts", "r");
389 if (filep == NULL) {
390 fprintf(stderr, "powerdebug: Error opening /proc/mounts.");
391 exit(1);
392 }
393
394 while (fscanf(filep, "%*s %s %s %*s %*d %*d\n",
395 debugfs_mntpoint, fsname) == 2)
396 if (!strcmp(fsname, "debugfs"))
397 break;
398 fclose(filep);
399
400 if (strcmp(fsname, "debugfs"))
401 return NULL;
402
403 return debugfs_mntpoint;
404}