blob: 80ff835567a55aa5b8949b801b3c55a72ee0a3ad [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
Amit Arora24ed7d12010-09-14 12:12:58 +053020static char clk_dir_path[PATH_MAX];
Amit Arora24ed7d12010-09-14 12:12:58 +053021static char clk_name[NAME_MAX];
Amit Aroraac4e8652010-11-09 11:16:53 +053022static int bold[MAX_LINES];
Amit Arora24ed7d12010-09-14 12:12:58 +053023
24void init_clock_details(void)
25{
Amit Arora6e774cd2010-10-28 11:31:24 +053026 char *path = debugfs_locate_mpoint();
27 struct stat buf;
Amit Aroraed3e5652010-10-27 12:02:53 +053028
29
Amit Arora6e774cd2010-10-28 11:31:24 +053030 if (path)
31 strcpy(clk_dir_path, path);
32 else {
33 fprintf(stderr, "powerdebug: Unable to locate debugfs mount"
34 " point. Mount debugfs and try again..\n");
35 exit(1);
36 }
37 sprintf(clk_dir_path, "%s/clock", clk_dir_path);
Amit Arora3bc8c922010-11-16 11:27:38 +053038 strcpy(clk_dir_path, "/debug/clock"); // Hardcoded for testing..
Amit Arora6e774cd2010-10-28 11:31:24 +053039 if (stat(clk_dir_path, &buf)) {
40 fprintf(stderr, "powerdebug: Unable to find clock tree"
41 " information at %s. Exiting..\n", clk_dir_path);
42 exit(1);
43 }
44 strcpy(clk_name, "");
Amit Arora24ed7d12010-09-14 12:12:58 +053045}
Amit Arora728e0c92010-09-14 12:06:09 +053046
47int get_int_from(char *file)
48{
Amit Arora6e774cd2010-10-28 11:31:24 +053049 FILE *filep;
50 char result[NAME_MAX];
51 int ret;
Amit Arora728e0c92010-09-14 12:06:09 +053052
Amit Arora6e774cd2010-10-28 11:31:24 +053053 filep = fopen(file, "r");
Amit Arora728e0c92010-09-14 12:06:09 +053054
Amit Arora6e774cd2010-10-28 11:31:24 +053055 if (!filep)
56 return -1; //TBD : What should we return on failure, here ?
Amit Arora728e0c92010-09-14 12:06:09 +053057
Amit Arora6e774cd2010-10-28 11:31:24 +053058 ret = fscanf(filep, "%s", result);
59 fclose(filep);
Amit Arora728e0c92010-09-14 12:06:09 +053060
Amit Arora6e774cd2010-10-28 11:31:24 +053061 return atoi(result);
Amit Arora728e0c92010-09-14 12:06:09 +053062}
63
Amit Arora24ed7d12010-09-14 12:12:58 +053064int read_and_print_clock_info(int verbose, int hrow, int selected)
65{
Amit Aroraac4e8652010-11-09 11:16:53 +053066 if (!old_clock_line_no)
67 read_clock_info(clk_dir_path);
Amit Arora29cb7572010-10-05 17:40:29 +053068
Amit Arora031263a2010-11-09 11:12:41 +053069 if (!clocks_info->num_children) {
70 fprintf(stderr, "powerdebug: No clocks found. Exiting..\n");
71 exit(1);
Amit Arora6e774cd2010-10-28 11:31:24 +053072 }
Amit Arora728e0c92010-09-14 12:06:09 +053073
Amit Aroraac4e8652010-11-09 11:16:53 +053074 print_clock_info(verbose, hrow, selected);
75 //destroy_clocks_info();
76 hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1;
77
78 return hrow;
79}
80
Amit Arora3bc8c922010-11-16 11:27:38 +053081int calc_delta_screen_size(int hrow)
82{
83 if (hrow > (maxy - 3))
84 return hrow - (maxy - 3);
85
86 return 0;
87}
88
Amit Aroraac4e8652010-11-09 11:16:53 +053089void print_clock_info(int verbose, int hrow, int selected)
90{
Amit Arora3bc8c922010-11-16 11:27:38 +053091 int i, count = 0, delta;
Amit Aroraac4e8652010-11-09 11:16:53 +053092
93 (void)verbose;
94
95 print_clock_header();
Amit Arora728e0c92010-09-14 12:06:09 +053096
Amit Arora031263a2010-11-09 11:12:41 +053097 for (i = 0; i < clocks_info->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +053098 add_clock_details_recur(clocks_info->children[i],
99 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530100
Amit Arora3bc8c922010-11-16 11:27:38 +0530101 delta = calc_delta_screen_size(hrow);
102
103 while (clock_lines[count + delta] &&
104 strcmp(clock_lines[count + delta], "")) {
105 if (count < delta) {
106 count++;
107 continue;
108 }
109 print_one_clock(count - delta, clock_lines[count + delta],
110 bold[count + delta], (hrow == (count + delta)));
Amit Arora031263a2010-11-09 11:12:41 +0530111 count++;
112 }
113
Amit Aroraac4e8652010-11-09 11:16:53 +0530114 old_clock_line_no = clock_line_no;
Amit Arora031263a2010-11-09 11:12:41 +0530115 clock_line_no = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530116}
117
Amit Aroraac4e8652010-11-09 11:16:53 +0530118void prepare_name_str(char *namestr, struct clock_info *clock)
119{
120 int i;
121
122 strcpy(namestr, "");
123 if (clock->level > 1)
124 for (i = 0; i < (clock->level - 1); i++)
125 strcat(namestr, " ");
126 strcat(namestr, clock->name);
127}
128
129void add_clock_details_recur(struct clock_info *clock, int hrow, int selected)
Amit Arora031263a2010-11-09 11:12:41 +0530130{
131 int i;
132 char *unit = " Hz";
133 char rate_str[64];
Amit Aroraac4e8652010-11-09 11:16:53 +0530134 char name_str[256];
Amit Arora031263a2010-11-09 11:12:41 +0530135 double drate = (double)clock->rate;
136
137 if (drate > 1000 && drate < 1000000) {
138 unit = "KHz";
139 drate /= 1000;
140 }
141 if (drate > 1000000) {
142 unit = "MHz";
143 drate /= 1000000;
144 }
145 if (clock->usecount)
146 bold[clock_line_no] = 1;
147 else
148 bold[clock_line_no] = 0;
149
150 sprintf(rate_str, "%.2f %s", drate, unit);
Amit Aroraac4e8652010-11-09 11:16:53 +0530151 prepare_name_str(name_str, clock);
152 sprintf(clock_lines[clock_line_no++], "%-55s %-4d %-12s %-12d %-12d",
153 name_str, clock->flags, rate_str, clock->usecount,
154 clock->num_children);
155
156 if (selected && (hrow == (clock_line_no - 1))) {
157 if (clock->expanded)
158 collapse_all_subclocks(clock);
159 else
160 clock->expanded = 1;
161 selected = 0;
162 }
Amit Arora031263a2010-11-09 11:12:41 +0530163
164 if (clock->expanded && clock->num_children)
165 for (i = 0; i < clock->num_children; i++)
Amit Aroraac4e8652010-11-09 11:16:53 +0530166 add_clock_details_recur(clock->children[i],
167 hrow, selected);
Amit Arora031263a2010-11-09 11:12:41 +0530168 strcpy(clock_lines[clock_line_no], "");
169}
170
Amit Aroraac4e8652010-11-09 11:16:53 +0530171void collapse_all_subclocks(struct clock_info *clock)
172{
173 int i;
174
175 clock->expanded = 0;
176 if (clock->num_children)
177 for (i = 0; i < clock->num_children; i++)
178 collapse_all_subclocks(clock->children[i]);
179}
180
Amit Arora031263a2010-11-09 11:12:41 +0530181void destroy_clocks_info(void)
182{
183 int i;
184
185 if (clocks_info->num_children) {
186 for (i = (clocks_info->num_children - 1); i >= 0 ; i--) {
187 destroy_clocks_info_recur(clocks_info->children[i]);
188 if (!i) {
189 free(clocks_info->children);
190 clocks_info->children = NULL;
191 }
192 }
193 }
194 free(clocks_info);
195 clocks_info = NULL;
196}
197
198void destroy_clocks_info_recur(struct clock_info *clock)
199{
200 int i;
201
202 if (clock && clock->num_children) {
203 for (i = (clock->num_children - 1); i >= 0; i--) {
204 fflush(stdin);
205 destroy_clocks_info_recur(clock->children[i]);
206 if (!i) {
Amit Arora031263a2010-11-09 11:12:41 +0530207 free(clock->children);
208 clock->children = NULL;
209 clock->num_children = 0;
210 }
211 }
212 }
Amit Arora728e0c92010-09-14 12:06:09 +0530213}
Amit Arora0e512722010-10-01 12:24:16 +0530214
Amit Aroraac4e8652010-11-09 11:16:53 +0530215void read_and_dump_clock_info(int verbose)
Amit Arora0e512722010-10-01 12:24:16 +0530216{
Amit Arora6e774cd2010-10-28 11:31:24 +0530217 (void)verbose;
218 printf("Clock Tree :\n");
219 printf("**********\n");
220 read_clock_info(clk_dir_path);
Amit Aroraac4e8652010-11-09 11:16:53 +0530221 dump_clock_info(clocks_info, 1, 1);
Amit Arora0e512722010-10-01 12:24:16 +0530222}
223
Amit Aroraeb6cba92010-10-25 16:03:21 +0530224void read_clock_info(char *clkpath)
Amit Arora0e512722010-10-01 12:24:16 +0530225{
Amit Arora6e774cd2010-10-28 11:31:24 +0530226 DIR *dir;
227 struct dirent *item;
228 char filename[NAME_MAX], clockname[NAME_MAX];
229 struct clock_info *child;
230 struct clock_info *cur;
Amit Arora0e512722010-10-01 12:24:16 +0530231
Amit Arora6e774cd2010-10-28 11:31:24 +0530232 dir = opendir(clkpath);
233 if (!dir)
234 return;
Amit Arora0e512722010-10-01 12:24:16 +0530235
Amit Arora6e774cd2010-10-28 11:31:24 +0530236 clocks_info = (struct clock_info *)malloc(sizeof(struct clock_info));
237 memset(clocks_info, 0, sizeof(clocks_info));
238 strcpy(clocks_info->name, "/");
Amit Aroraac4e8652010-11-09 11:16:53 +0530239 clocks_info->level = 0;
Amit Arora0e512722010-10-01 12:24:16 +0530240
Amit Arora6e774cd2010-10-28 11:31:24 +0530241 while ((item = readdir(dir))) {
242 /* skip hidden dirs except ".." */
243 if (item->d_name[0] == '.')
244 continue;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530245
Amit Arora6e774cd2010-10-28 11:31:24 +0530246 strcpy(clockname, item->d_name);
247 sprintf(filename, "%s/%s", clkpath, item->d_name);
248 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
Amit Aroraac4e8652010-11-09 11:16:53 +0530249 memset(cur, 0, sizeof(struct clock_info));
Amit Arora6e774cd2010-10-28 11:31:24 +0530250 strcpy(cur->name, clockname);
251 cur->parent = clocks_info;
Amit Arora031263a2010-11-09 11:12:41 +0530252 cur->num_children = 0;
253 cur->expanded = 0;
Amit Aroraac4e8652010-11-09 11:16:53 +0530254 cur->level = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530255 insert_children(&clocks_info, cur);
256 child = read_clock_info_recur(filename, 2, cur);
257 }
258 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530259}
260
261struct clock_info *read_clock_info_recur(char *clkpath, int level,
Amit Arora6e774cd2010-10-28 11:31:24 +0530262 struct clock_info *parent)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530263{
Amit Arora6e774cd2010-10-28 11:31:24 +0530264 int ret = 0;
265 DIR *dir;
266 char filename[PATH_MAX];
267 struct dirent *item;
268 struct clock_info *cur = NULL;
269 struct stat buf;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530270
Amit Arora6e774cd2010-10-28 11:31:24 +0530271 dir = opendir(clkpath);
272 if (!dir)
273 return NULL;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530274
Amit Arora6e774cd2010-10-28 11:31:24 +0530275 while ((item = readdir(dir))) {
276 struct clock_info *child;
277 /* skip hidden dirs except ".." */
278 if (item->d_name[0] == '.' )
279 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530280
Amit Arora6e774cd2010-10-28 11:31:24 +0530281 sprintf(filename, "%s/%s", clkpath, item->d_name);
Amit Arora0e512722010-10-01 12:24:16 +0530282
Amit Arora6e774cd2010-10-28 11:31:24 +0530283 ret = stat(filename, &buf);
Amit Arora0e512722010-10-01 12:24:16 +0530284
Amit Arora6e774cd2010-10-28 11:31:24 +0530285 if (ret < 0) {
286 printf("Error doing a stat on %s\n", filename);
287 exit(1);
288 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530289
Amit Arora6e774cd2010-10-28 11:31:24 +0530290 if (S_ISREG(buf.st_mode)) {
291 if (!strcmp(item->d_name, "flags"))
292 parent->flags = get_int_from(filename);
293 if (!strcmp(item->d_name, "rate"))
294 parent->rate = get_int_from(filename);
295 if (!strcmp(item->d_name, "usecount"))
296 parent->usecount = get_int_from(filename);
297 continue;
298 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530299
Amit Arora6e774cd2010-10-28 11:31:24 +0530300 if (!S_ISDIR(buf.st_mode))
301 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530302
Amit Arora6e774cd2010-10-28 11:31:24 +0530303 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
304 memset(cur, 0, sizeof(cur));
305 strcpy(cur->name, item->d_name);
306 cur->children = NULL;
307 cur->parent = NULL;
308 cur->num_children = 0;
Amit Arora031263a2010-11-09 11:12:41 +0530309 cur->expanded = 0;
Amit Aroraac4e8652010-11-09 11:16:53 +0530310 cur->level = level;
Amit Arora6e774cd2010-10-28 11:31:24 +0530311 child = read_clock_info_recur(filename, level + 1, cur);
Amit Arora6e774cd2010-10-28 11:31:24 +0530312 insert_children(&parent, cur);
313 cur->parent = parent;
314 }
315 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530316
Amit Arora6e774cd2010-10-28 11:31:24 +0530317 return cur;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530318}
319
320void insert_children(struct clock_info **parent, struct clock_info *clk)
321{
Amit Arora031263a2010-11-09 11:12:41 +0530322 if (!(*parent)->num_children || (*parent)->children == NULL) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530323 (*parent)->children = (struct clock_info **)
324 malloc(sizeof(struct clock_info *)*2);
325 (*parent)->num_children = 0;
326 } else
327 (*parent)->children = (struct clock_info **)
328 realloc((*parent)->children,
329 sizeof(struct clock_info *) *
330 ((*parent)->num_children + 2));
331 if ((*parent)->num_children > 0)
332 (*parent)->children[(*parent)->num_children - 1]->last_child
333 = 0;
334 clk->last_child = 1;
335 (*parent)->children[(*parent)->num_children] = clk;
336 (*parent)->children[(*parent)->num_children + 1] = NULL;
337 (*parent)->num_children++;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530338}
339
340
Amit Aroraac4e8652010-11-09 11:16:53 +0530341void dump_clock_info(struct clock_info *clk, int level, int bmp)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530342{
Amit Arora6e774cd2010-10-28 11:31:24 +0530343 int i, j;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530344
Amit Arora6e774cd2010-10-28 11:31:24 +0530345 if (!clk)
346 return;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530347
Amit Arora6e774cd2010-10-28 11:31:24 +0530348 for (i = 1, j = 0; i < level; i++, j = (i - 1)) {
349 if (i == (level - 1)) {
350 if (clk->last_child)
351 printf("`-- ");
352 else
353 printf("|-- ");
354 } else {
355 if ((1<<j) & bmp)
356 printf("| ");
357 else
358 printf(" ");
359 }
360 }
Amit Arora031263a2010-11-09 11:12:41 +0530361
Amit Arora6e774cd2010-10-28 11:31:24 +0530362 if (clk == clocks_info)
363 printf("%s\n", clk->name);
364 else {
365 char *unit = "Hz";
366 double drate = (double)clk->rate;
367
368 if (drate > 1000 && drate < 1000000) {
369 unit = "KHz";
370 drate /= 1000;
371 }
372 if (drate > 1000000) {
373 unit = "MHz";
374 drate /= 1000000;
375 }
376 printf("%s (flags:%d,usecount:%d,rate:%5.2f %s)\n",
377 clk->name, clk->flags, clk->usecount, drate, unit);
Amit Arora6e774cd2010-10-28 11:31:24 +0530378 }
379 if (clk->children) {
380 int tbmp = bmp;
381 int xbmp = -1;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530382
Amit Arora6e774cd2010-10-28 11:31:24 +0530383 if (clk->last_child) {
384 xbmp ^= 1 << (level - 2);
385
386 xbmp = tbmp & xbmp;
387 } else
388 xbmp = bmp;
389 for (i = 0; i < clk->num_children; i++) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530390 tbmp = xbmp | (1 << level);
Amit Aroraac4e8652010-11-09 11:16:53 +0530391 dump_clock_info(clk->children[i], level + 1, tbmp);
Amit Arora6e774cd2010-10-28 11:31:24 +0530392 }
393 }
Amit Arora0e512722010-10-01 12:24:16 +0530394}
Amit Aroraed3e5652010-10-27 12:02:53 +0530395
396char *debugfs_locate_mpoint(void)
397{
398 int ret;
399 FILE *filep;
400 char **path;
401 char fsname[64];
402 struct statfs sfs;
403
404 path = likely_mpoints;
405 while (*path) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530406 ret = statfs(*path, &sfs);
407 if (ret >= 0 && sfs.f_type == (long)DEBUGFS_MAGIC)
Amit Aroraed3e5652010-10-27 12:02:53 +0530408 return *path;
409 path++;
410 }
411
412 filep = fopen("/proc/mounts", "r");
413 if (filep == NULL) {
414 fprintf(stderr, "powerdebug: Error opening /proc/mounts.");
Amit Arora6e774cd2010-10-28 11:31:24 +0530415 exit(1);
416 }
Amit Aroraed3e5652010-10-27 12:02:53 +0530417
418 while (fscanf(filep, "%*s %s %s %*s %*d %*d\n",
419 debugfs_mntpoint, fsname) == 2)
420 if (!strcmp(fsname, "debugfs"))
421 break;
422 fclose(filep);
423
424 if (strcmp(fsname, "debugfs"))
425 return NULL;
426
427 return debugfs_mntpoint;
428}