blob: 815f59836ab32074186538725bbe239363809f20 [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 Arora6e774cd2010-10-28 11:31:24 +053031 char *path = debugfs_locate_mpoint();
32 struct stat buf;
Amit Aroraed3e5652010-10-27 12:02:53 +053033
34
Amit Arora6e774cd2010-10-28 11:31:24 +053035 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 }
49 strcpy(clk_name, "");
50 strcpy(highlighted_path, "");
Amit Arora24ed7d12010-09-14 12:12:58 +053051}
Amit Arora728e0c92010-09-14 12:06:09 +053052
53int get_int_from(char *file)
54{
Amit Arora6e774cd2010-10-28 11:31:24 +053055 FILE *filep;
56 char result[NAME_MAX];
57 int ret;
Amit Arora728e0c92010-09-14 12:06:09 +053058
Amit Arora6e774cd2010-10-28 11:31:24 +053059 filep = fopen(file, "r");
Amit Arora728e0c92010-09-14 12:06:09 +053060
Amit Arora6e774cd2010-10-28 11:31:24 +053061 if (!filep)
62 return -1; //TBD : What should we return on failure, here ?
Amit Arora728e0c92010-09-14 12:06:09 +053063
Amit Arora6e774cd2010-10-28 11:31:24 +053064 ret = fscanf(filep, "%s", result);
65 fclose(filep);
Amit Arora728e0c92010-09-14 12:06:09 +053066
Amit Arora6e774cd2010-10-28 11:31:24 +053067 return atoi(result);
Amit Arora728e0c92010-09-14 12:06:09 +053068}
69
Amit Arora24ed7d12010-09-14 12:12:58 +053070int read_and_print_clock_info(int verbose, int hrow, int selected)
71{
Amit Arora6e774cd2010-10-28 11:31:24 +053072 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 }
Amit Arora24ed7d12010-09-14 12:12:58 +053081
Amit Arora6e774cd2010-10-28 11:31:24 +053082 hrow = read_and_print_clock_one_level(verbose, hrow, selected);
Amit Arora24ed7d12010-09-14 12:12:58 +053083
Amit Arora6e774cd2010-10-28 11:31:24 +053084 return hrow;
Amit Arora24ed7d12010-09-14 12:12:58 +053085}
86
87void set_hl_dir_path_to_parent(void)
88{
Amit Arora6e774cd2010-10-28 11:31:24 +053089 char *ptr;
Amit Arora24ed7d12010-09-14 12:12:58 +053090
Amit Arora6e774cd2010-10-28 11:31:24 +053091 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 }
Amit Arora24ed7d12010-09-14 12:12:58 +053097}
98
99int read_and_print_clock_one_level(int verbose, int hrow, int selected)
Amit Arora728e0c92010-09-14 12:06:09 +0530100{
Amit Arora6e774cd2010-10-28 11:31:24 +0530101 int line = 0, usecount = 0, flags = 0, rate = 0;
Amit Arora24ed7d12010-09-14 12:12:58 +0530102// int parent_dir_row = 1000;
Amit Arora6e774cd2010-10-28 11:31:24 +0530103 DIR *dir, *subdir;
104 char filename[PATH_MAX], devpath[PATH_MAX], clockname[NAME_MAX];
105 struct dirent *item, *subitem;
Amit Arora728e0c92010-09-14 12:06:09 +0530106
Amit Arora6e774cd2010-10-28 11:31:24 +0530107 (void)verbose;
Amit Arora29cb7572010-10-05 17:40:29 +0530108
Amit Arora6e774cd2010-10-28 11:31:24 +0530109 print_clock_header(clk_tree_level);
Amit Arora728e0c92010-09-14 12:06:09 +0530110
Amit Arora6e774cd2010-10-28 11:31:24 +0530111 sprintf(filename, "%s", clk_dir_path);
Amit Arora728e0c92010-09-14 12:06:09 +0530112
Amit Arora6e774cd2010-10-28 11:31:24 +0530113 dir = opendir(filename);
114 if (!dir)
115 return 0;
Amit Arora728e0c92010-09-14 12:06:09 +0530116
Amit Arora6e774cd2010-10-28 11:31:24 +0530117 while ((item = readdir(dir))) {
118 /* skip hidden dirs except ".." */
119 if (item->d_name[0] == '.' && strcmp(item->d_name, ".."))
120 continue;
Amit Arora728e0c92010-09-14 12:06:09 +0530121
Amit Arora6e774cd2010-10-28 11:31:24 +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
Amit Arora6e774cd2010-10-28 11:31:24 +0530130 subdir = opendir(devpath);
Amit Arora728e0c92010-09-14 12:06:09 +0530131
Amit Arora6e774cd2010-10-28 11:31:24 +0530132 if (!subdir)
133 continue;
Amit Arora728e0c92010-09-14 12:06:09 +0530134
Amit Arora6e774cd2010-10-28 11:31:24 +0530135 while ((subitem = readdir(subdir))) {
136 if (subitem->d_name[0] == '.') /* skip hidden files */
137 continue;
Amit Arora728e0c92010-09-14 12:06:09 +0530138
Amit Arora6e774cd2010-10-28 11:31:24 +0530139 sprintf(filename, "%s/%s", devpath, subitem->d_name);
Amit Arora728e0c92010-09-14 12:06:09 +0530140
Amit Arora6e774cd2010-10-28 11:31:24 +0530141 if (!strcmp(subitem->d_name, "flags"))
142 flags = get_int_from(filename);
Amit Arora728e0c92010-09-14 12:06:09 +0530143
Amit Arora6e774cd2010-10-28 11:31:24 +0530144 if (!strcmp(subitem->d_name, "rate"))
145 rate = get_int_from(filename);
Amit Arora728e0c92010-09-14 12:06:09 +0530146
Amit Arora6e774cd2010-10-28 11:31:24 +0530147 if (!strcmp(subitem->d_name, "usecount"))
148 usecount = get_int_from(filename);
149 }
Amit Arora24ed7d12010-09-14 12:12:58 +0530150
Amit Arora6e774cd2010-10-28 11:31:24 +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 }
Amit Arora24ed7d12010-09-14 12:12:58 +0530162
Amit Arora6e774cd2010-10-28 11:31:24 +0530163// sprintf(clockname, "%s:dp-%s:n-%s:hp-%s",
164// clockname, clk_dir_path, clk_name, highlighted_path);
Amit Arora24ed7d12010-09-14 12:12:58 +0530165
Amit Arora6e774cd2010-10-28 11:31:24 +0530166/* 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 }
Amit Arora24ed7d12010-09-14 12:12:58 +0530178*/
179
Amit Arora6e774cd2010-10-28 11:31:24 +0530180 print_clock_info_line(line, clockname, flags, rate, usecount,
181 (hrow == line) ? 1 : 0);
182 line++;
Amit Arora29cb7572010-10-05 17:40:29 +0530183
Amit Arora6e774cd2010-10-28 11:31:24 +0530184 closedir(subdir);
185 }
Amit Arora728e0c92010-09-14 12:06:09 +0530186
Amit Arora6e774cd2010-10-28 11:31:24 +0530187 closedir(dir);
Amit Arora728e0c92010-09-14 12:06:09 +0530188
Amit Arora6e774cd2010-10-28 11:31:24 +0530189 if (hrow >= (line - 1))
190 hrow = -1;
191 return hrow;
Amit Arora728e0c92010-09-14 12:06:09 +0530192}
Amit Arora0e512722010-10-01 12:24:16 +0530193
194void dump_clock_info(int verbose)
195{
Amit Arora6e774cd2010-10-28 11:31:24 +0530196 (void)verbose;
197 printf("Clock Tree :\n");
198 printf("**********\n");
199 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 Arora6e774cd2010-10-28 11:31:24 +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 Arora6e774cd2010-10-28 11:31:24 +0530211 dir = opendir(clkpath);
212 if (!dir)
213 return;
Amit Arora0e512722010-10-01 12:24:16 +0530214
Amit Arora6e774cd2010-10-28 11:31:24 +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 Arora6e774cd2010-10-28 11:31:24 +0530219 while ((item = readdir(dir))) {
220 /* skip hidden dirs except ".." */
221 if (item->d_name[0] == '.')
222 continue;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530223
Amit Arora6e774cd2010-10-28 11:31:24 +0530224 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);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530234}
235
236struct clock_info *read_clock_info_recur(char *clkpath, int level,
Amit Arora6e774cd2010-10-28 11:31:24 +0530237 struct clock_info *parent)
Amit Aroraeb6cba92010-10-25 16:03:21 +0530238{
Amit Arora6e774cd2010-10-28 11:31:24 +0530239 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;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530245
Amit Arora6e774cd2010-10-28 11:31:24 +0530246 dir = opendir(clkpath);
247 if (!dir)
248 return NULL;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530249
Amit Arora6e774cd2010-10-28 11:31:24 +0530250 while ((item = readdir(dir))) {
251 struct clock_info *child;
252 /* skip hidden dirs except ".." */
253 if (item->d_name[0] == '.' )
254 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530255
Amit Arora6e774cd2010-10-28 11:31:24 +0530256 sprintf(filename, "%s/%s", clkpath, item->d_name);
Amit Arora0e512722010-10-01 12:24:16 +0530257
Amit Arora6e774cd2010-10-28 11:31:24 +0530258 ret = stat(filename, &buf);
Amit Arora0e512722010-10-01 12:24:16 +0530259
Amit Arora6e774cd2010-10-28 11:31:24 +0530260 if (ret < 0) {
261 printf("Error doing a stat on %s\n", filename);
262 exit(1);
263 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530264
Amit Arora6e774cd2010-10-28 11:31:24 +0530265 if (S_ISREG(buf.st_mode)) {
266 if (!strcmp(item->d_name, "flags"))
267 parent->flags = get_int_from(filename);
268 if (!strcmp(item->d_name, "rate"))
269 parent->rate = get_int_from(filename);
270 if (!strcmp(item->d_name, "usecount"))
271 parent->usecount = get_int_from(filename);
272 continue;
273 }
Amit Aroraeb6cba92010-10-25 16:03:21 +0530274
Amit Arora6e774cd2010-10-28 11:31:24 +0530275 if (!S_ISDIR(buf.st_mode))
276 continue;
Amit Arora0e512722010-10-01 12:24:16 +0530277
Amit Arora6e774cd2010-10-28 11:31:24 +0530278 cur = (struct clock_info *)malloc(sizeof(struct clock_info));
279 memset(cur, 0, sizeof(cur));
280 strcpy(cur->name, item->d_name);
281 cur->children = NULL;
282 cur->parent = NULL;
283 cur->num_children = 0;
284 child = read_clock_info_recur(filename, level + 1, cur);
Amit Arora0e512722010-10-01 12:24:16 +0530285
Amit Arora6e774cd2010-10-28 11:31:24 +0530286 insert_children(&parent, cur);
287 cur->parent = parent;
288 }
289 closedir(dir);
Amit Aroraeb6cba92010-10-25 16:03:21 +0530290
Amit Arora6e774cd2010-10-28 11:31:24 +0530291 return cur;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530292}
293
294void insert_children(struct clock_info **parent, struct clock_info *clk)
295{
Amit Arora6e774cd2010-10-28 11:31:24 +0530296 if (!(*parent)->children) {
297 (*parent)->children = (struct clock_info **)
298 malloc(sizeof(struct clock_info *)*2);
299 (*parent)->num_children = 0;
300 } else
301 (*parent)->children = (struct clock_info **)
302 realloc((*parent)->children,
303 sizeof(struct clock_info *) *
304 ((*parent)->num_children + 2));
305 if ((*parent)->num_children > 0)
306 (*parent)->children[(*parent)->num_children - 1]->last_child
307 = 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++;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530312}
313
314
315void print_clock_info(struct clock_info *clk, int level, int bmp)
316{
Amit Arora6e774cd2010-10-28 11:31:24 +0530317 int i, j;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530318
Amit Arora6e774cd2010-10-28 11:31:24 +0530319 if (!clk)
320 return;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530321
Amit Arora6e774cd2010-10-28 11:31:24 +0530322 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;
Amit Aroraeb6cba92010-10-25 16:03:21 +0530357
Amit Arora6e774cd2010-10-28 11:31:24 +0530358 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) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530382 ret = statfs(*path, &sfs);
383 if (ret >= 0 && sfs.f_type == (long)DEBUGFS_MAGIC)
Amit Aroraed3e5652010-10-27 12:02:53 +0530384 return *path;
385 path++;
386 }
387
388 filep = fopen("/proc/mounts", "r");
389 if (filep == NULL) {
390 fprintf(stderr, "powerdebug: Error opening /proc/mounts.");
Amit Arora6e774cd2010-10-28 11:31:24 +0530391 exit(1);
392 }
Amit Aroraed3e5652010-10-27 12:02:53 +0530393
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}