Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 1 | /******************************************************************************* |
Amit Kucheria | c0e17fc | 2011-01-17 09:35:52 +0200 | [diff] [blame] | 2 | * Copyright (C) 2010, Linaro Limited. |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 3 | * |
| 4 | * This file is part of PowerDebug. |
| 5 | * |
| 6 | * All rights reserved. This program and the accompanying materials |
| 7 | * are made available under the terms of the Eclipse Public License v1.0 |
| 8 | * which accompanies this distribution, and is available at |
| 9 | * http://www.eclipse.org/legal/epl-v10.html |
| 10 | * |
| 11 | * Contributors: |
| 12 | * Amit Arora <amit.arora@linaro.org> (IBM Corporation) |
| 13 | * - initial API and implementation |
| 14 | *******************************************************************************/ |
| 15 | |
Daniel Lezcano | 9dc3fb3 | 2011-03-26 22:06:08 +0100 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <mntent.h> |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 18 | #include <sys/stat.h> |
Daniel Lezcano | 9dc3fb3 | 2011-03-26 22:06:08 +0100 | [diff] [blame] | 19 | |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 20 | #include "powerdebug.h" |
Amit Arora | ed3e565 | 2010-10-27 12:02:53 +0530 | [diff] [blame] | 21 | #include "clocks.h" |
Amit Arora | 24ed7d1 | 2010-09-14 12:12:58 +0530 | [diff] [blame] | 22 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 23 | #define MAX_LINES 120 |
| 24 | |
Amit Arora | 24ed7d1 | 2010-09-14 12:12:58 +0530 | [diff] [blame] | 25 | static char clk_dir_path[PATH_MAX]; |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 26 | static int bold[MAX_LINES]; |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 27 | static char clock_lines[MAX_LINES][128]; |
| 28 | static int clock_line_no; |
| 29 | static int old_clock_line_no; |
Amit Arora | 24ed7d1 | 2010-09-14 12:12:58 +0530 | [diff] [blame] | 30 | |
Daniel Lezcano | c45662b | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 31 | struct clock_info { |
| 32 | char name[NAME_MAX]; |
| 33 | int flags; |
| 34 | int rate; |
| 35 | int usecount; |
| 36 | int num_children; |
| 37 | int last_child; |
| 38 | int expanded; |
| 39 | int level; |
| 40 | struct clock_info *parent; |
| 41 | struct clock_info **children; |
| 42 | } *clocks_info; |
| 43 | |
Daniel Lezcano | 9dc3fb3 | 2011-03-26 22:06:08 +0100 | [diff] [blame] | 44 | static int locate_debugfs(char *clk_path) |
| 45 | { |
| 46 | const char *mtab = "/proc/mounts"; |
| 47 | struct mntent *mntent; |
| 48 | int ret = -1; |
| 49 | FILE *file = NULL; |
| 50 | |
| 51 | file = setmntent(mtab, "r"); |
| 52 | if (!file) |
| 53 | return -1; |
| 54 | |
| 55 | while ((mntent = getmntent(file))) { |
| 56 | |
| 57 | if (strcmp(mntent->mnt_type, "debugfs")) |
| 58 | continue; |
| 59 | |
| 60 | strcpy(clk_path, mntent->mnt_dir); |
| 61 | ret = 0; |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | fclose(file); |
| 66 | return ret; |
| 67 | } |
| 68 | |
Daniel Lezcano | f0e0665 | 2011-03-26 22:06:19 +0100 | [diff] [blame] | 69 | int clock_init(void) |
Amit Arora | 24ed7d1 | 2010-09-14 12:12:58 +0530 | [diff] [blame] | 70 | { |
Daniel Lezcano | f0e0665 | 2011-03-26 22:06:19 +0100 | [diff] [blame] | 71 | if (locate_debugfs(clk_dir_path)) |
| 72 | return -1; |
Daniel Lezcano | 9dc3fb3 | 2011-03-26 22:06:08 +0100 | [diff] [blame] | 73 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 74 | sprintf(clk_dir_path, "%s/clock", clk_dir_path); |
Daniel Lezcano | c5a65bf | 2011-03-26 22:06:11 +0100 | [diff] [blame] | 75 | |
Daniel Lezcano | f0e0665 | 2011-03-26 22:06:19 +0100 | [diff] [blame] | 76 | return access(clk_dir_path, F_OK); |
Amit Arora | 24ed7d1 | 2010-09-14 12:12:58 +0530 | [diff] [blame] | 77 | } |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 78 | |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 79 | static int file_read_from_format(char *file, int *value, const char *format) |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 80 | { |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 81 | FILE *f; |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 82 | int ret; |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 83 | |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 84 | f = fopen(file, "r"); |
| 85 | if (!f) |
| 86 | return -1; |
| 87 | ret = fscanf(f, format, value); |
| 88 | fclose(f); |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 89 | |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 90 | return !ret ? -1 : 0; |
| 91 | } |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 92 | |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 93 | static inline int file_read_int(char *file, int *value) |
| 94 | { |
| 95 | return file_read_from_format(file, value, "%d"); |
| 96 | } |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 97 | |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 98 | static inline int file_read_hex(char *file, int *value) |
| 99 | { |
| 100 | return file_read_from_format(file, value, "%x"); |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 103 | static void dump_parent(struct clock_info *clk, int line, bool dump) |
| 104 | { |
| 105 | char *unit = "Hz"; |
| 106 | double drate; |
| 107 | static char spaces[64]; |
| 108 | char str[256]; |
| 109 | static int maxline; |
| 110 | |
| 111 | if (maxline < line) |
| 112 | maxline = line; |
| 113 | |
| 114 | if (clk && clk->parent) |
| 115 | dump_parent(clk->parent, ++line, dump); |
| 116 | |
| 117 | drate = (double)clk->rate; |
| 118 | if (drate > 1000 && drate < 1000000) { |
| 119 | unit = "KHz"; |
| 120 | drate /= 1000; |
| 121 | } |
| 122 | if (drate > 1000000) { |
| 123 | unit = "MHz"; |
| 124 | drate /= 1000000; |
| 125 | } |
| 126 | if (clk == clocks_info) { |
| 127 | line++; |
| 128 | strcpy(spaces, ""); |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 129 | sprintf(str, "%s%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n", |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 130 | spaces, clk->name, clk->flags, clk->usecount, drate, |
| 131 | unit); |
| 132 | } else { |
| 133 | if (!(clk->parent == clocks_info)) |
| 134 | strcat(spaces, " "); |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 135 | sprintf(str, "%s`- %s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n", |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 136 | spaces, clk->name, clk->flags, clk->usecount, drate, |
| 137 | unit); |
| 138 | } |
| 139 | if (dump) |
| 140 | //printf("line=%d:m%d:l%d %s", maxline - line + 2, maxline, line, str); |
| 141 | printf("%s", str); |
| 142 | else |
| 143 | print_one_clock(maxline - line + 2, str, 1, 0); |
| 144 | } |
| 145 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 146 | static struct clock_info *find_clock(struct clock_info *clk, char *clkarg) |
| 147 | { |
| 148 | int i; |
| 149 | struct clock_info *ret = clk; |
| 150 | |
| 151 | if (!strcmp(clk->name, clkarg)) |
| 152 | return ret; |
| 153 | |
| 154 | if (clk->children) { |
| 155 | for (i = 0; i < clk->num_children; i++) { |
| 156 | if (!strcmp(clk->children[i]->name, clkarg)) |
| 157 | return clk->children[i]; |
| 158 | } |
| 159 | for (i = 0; i < clk->num_children; i++) { |
| 160 | ret = find_clock(clk->children[i], clkarg); |
| 161 | if (ret) |
| 162 | return ret; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return NULL; |
| 167 | } |
| 168 | |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 169 | static void dump_all_parents(char *clkarg, bool dump) |
| 170 | { |
| 171 | struct clock_info *clk; |
| 172 | char spaces[1024]; |
| 173 | |
| 174 | strcpy(spaces, ""); |
| 175 | |
| 176 | clk = find_clock(clocks_info, clkarg); |
| 177 | |
| 178 | if (!clk) |
| 179 | printf("Clock NOT found!\n"); |
| 180 | else { |
| 181 | /* while(clk && clk != clocks_info) { */ |
| 182 | /* printf("%s\n", clk->name); */ |
| 183 | /* strcat(spaces, " "); */ |
| 184 | /* clk = clk->parent; */ |
| 185 | /* printf("%s <-- ", spaces); */ |
| 186 | /* } */ |
| 187 | /* printf(" /\n"); */ |
| 188 | dump_parent(clk, 1, dump); |
| 189 | } |
| 190 | } |
| 191 | |
Daniel Lezcano | 897f733 | 2011-03-26 22:06:06 +0100 | [diff] [blame] | 192 | void find_parents_for_clock(char *clkname, int complete) |
Amit Arora | 3bd7916 | 2010-12-01 13:51:42 +0530 | [diff] [blame] | 193 | { |
| 194 | char name[256]; |
| 195 | |
| 196 | name[0] = '\0'; |
| 197 | if (!complete) { |
| 198 | char str[256]; |
| 199 | |
| 200 | strcat(name, clkname); |
| 201 | sprintf(str, "Enter Clock Name : %s\n", name); |
| 202 | print_one_clock(2, str, 1, 0); |
| 203 | return; |
| 204 | } |
| 205 | sprintf(name, "Parents for \"%s\" Clock : \n", clkname); |
| 206 | print_one_clock(0, name, 1, 1); |
Daniel Lezcano | 897f733 | 2011-03-26 22:06:06 +0100 | [diff] [blame] | 207 | dump_all_parents(clkname, false); |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 208 | } |
Amit Arora | 3bd7916 | 2010-12-01 13:51:42 +0530 | [diff] [blame] | 209 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 210 | static void destroy_clocks_info_recur(struct clock_info *clock) |
| 211 | { |
| 212 | int i; |
| 213 | |
| 214 | if (clock && clock->num_children) { |
| 215 | for (i = (clock->num_children - 1); i >= 0; i--) { |
| 216 | fflush(stdin); |
| 217 | destroy_clocks_info_recur(clock->children[i]); |
| 218 | if (!i) { |
| 219 | free(clock->children); |
| 220 | clock->children = NULL; |
| 221 | clock->num_children = 0; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | static void destroy_clocks_info(void) |
| 228 | { |
| 229 | int i; |
| 230 | |
| 231 | if (!clocks_info) |
| 232 | return; |
| 233 | |
| 234 | if (clocks_info->num_children) { |
| 235 | for (i = (clocks_info->num_children - 1); i >= 0 ; i--) { |
| 236 | destroy_clocks_info_recur(clocks_info->children[i]); |
| 237 | if (!i) { |
| 238 | free(clocks_info->children); |
| 239 | clocks_info->children = NULL; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | clocks_info->num_children = 0; |
| 244 | free(clocks_info); |
| 245 | clocks_info = NULL; |
| 246 | } |
| 247 | |
| 248 | |
Amit Arora | 24ed7d1 | 2010-09-14 12:12:58 +0530 | [diff] [blame] | 249 | int read_and_print_clock_info(int verbose, int hrow, int selected) |
| 250 | { |
Amit Arora | 3bd7916 | 2010-12-01 13:51:42 +0530 | [diff] [blame] | 251 | print_one_clock(0, "Reading Clock Tree ...", 1, 1); |
Amit Arora | a06a730 | 2010-12-02 15:59:37 +0530 | [diff] [blame] | 252 | |
| 253 | if (!old_clock_line_no || selected == REFRESH_WINDOW) { |
| 254 | destroy_clocks_info(); |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 255 | read_clock_info(clk_dir_path); |
Amit Arora | a06a730 | 2010-12-02 15:59:37 +0530 | [diff] [blame] | 256 | } |
Amit Arora | 29cb757 | 2010-10-05 17:40:29 +0530 | [diff] [blame] | 257 | |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 258 | if (!clocks_info->num_children) { |
| 259 | fprintf(stderr, "powerdebug: No clocks found. Exiting..\n"); |
| 260 | exit(1); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 261 | } |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 262 | |
Amit Arora | a06a730 | 2010-12-02 15:59:37 +0530 | [diff] [blame] | 263 | if (selected == CLOCK_SELECTED) |
| 264 | selected = 1; |
| 265 | else |
| 266 | selected = 0; |
| 267 | |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 268 | print_clock_info(verbose, hrow, selected); |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 269 | hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1; |
| 270 | |
| 271 | return hrow; |
| 272 | } |
| 273 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 274 | static int calc_delta_screen_size(int hrow) |
Amit Arora | 3bc8c92 | 2010-11-16 11:27:38 +0530 | [diff] [blame] | 275 | { |
Amit Arora | 51d1b9c | 2010-11-30 13:55:15 +0530 | [diff] [blame] | 276 | if (hrow >= (maxy - 3)) |
| 277 | return hrow - (maxy - 4); |
Amit Arora | 3bc8c92 | 2010-11-16 11:27:38 +0530 | [diff] [blame] | 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 282 | static void prepare_name_str(char *namestr, struct clock_info *clock) |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 283 | { |
| 284 | int i; |
| 285 | |
| 286 | strcpy(namestr, ""); |
| 287 | if (clock->level > 1) |
| 288 | for (i = 0; i < (clock->level - 1); i++) |
| 289 | strcat(namestr, " "); |
| 290 | strcat(namestr, clock->name); |
| 291 | } |
| 292 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 293 | static void collapse_all_subclocks(struct clock_info *clock) |
| 294 | { |
| 295 | int i; |
| 296 | |
| 297 | clock->expanded = 0; |
| 298 | if (clock->num_children) |
| 299 | for (i = 0; i < clock->num_children; i++) |
| 300 | collapse_all_subclocks(clock->children[i]); |
| 301 | } |
| 302 | |
| 303 | static void add_clock_details_recur(struct clock_info *clock, |
| 304 | int hrow, int selected) |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 305 | { |
| 306 | int i; |
| 307 | char *unit = " Hz"; |
| 308 | char rate_str[64]; |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 309 | char name_str[256]; |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 310 | double drate = (double)clock->rate; |
| 311 | |
| 312 | if (drate > 1000 && drate < 1000000) { |
| 313 | unit = "KHz"; |
| 314 | drate /= 1000; |
| 315 | } |
| 316 | if (drate > 1000000) { |
| 317 | unit = "MHz"; |
| 318 | drate /= 1000000; |
| 319 | } |
| 320 | if (clock->usecount) |
| 321 | bold[clock_line_no] = 1; |
| 322 | else |
| 323 | bold[clock_line_no] = 0; |
| 324 | |
| 325 | sprintf(rate_str, "%.2f %s", drate, unit); |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 326 | prepare_name_str(name_str, clock); |
Daniel Lezcano | 4b66907 | 2011-05-24 15:27:49 +0200 | [diff] [blame] | 327 | sprintf(clock_lines[clock_line_no++], "%-55s 0x%-4x %-12s %-12d %-12d", |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 328 | name_str, clock->flags, rate_str, clock->usecount, |
| 329 | clock->num_children); |
| 330 | |
| 331 | if (selected && (hrow == (clock_line_no - 1))) { |
| 332 | if (clock->expanded) |
| 333 | collapse_all_subclocks(clock); |
| 334 | else |
| 335 | clock->expanded = 1; |
| 336 | selected = 0; |
| 337 | } |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 338 | |
| 339 | if (clock->expanded && clock->num_children) |
| 340 | for (i = 0; i < clock->num_children; i++) |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 341 | add_clock_details_recur(clock->children[i], |
| 342 | hrow, selected); |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 343 | strcpy(clock_lines[clock_line_no], ""); |
| 344 | } |
| 345 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 346 | void print_clock_info(int verbose, int hrow, int selected) |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 347 | { |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 348 | int i, count = 0, delta; |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 349 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 350 | (void)verbose; |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 351 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 352 | print_clock_header(); |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 353 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 354 | for (i = 0; i < clocks_info->num_children; i++) |
| 355 | add_clock_details_recur(clocks_info->children[i], |
| 356 | hrow, selected); |
Amit Arora | a06a730 | 2010-12-02 15:59:37 +0530 | [diff] [blame] | 357 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 358 | delta = calc_delta_screen_size(hrow); |
| 359 | |
| 360 | while (clock_lines[count + delta] && |
| 361 | strcmp(clock_lines[count + delta], "")) { |
| 362 | if (count < delta) { |
| 363 | count++; |
| 364 | continue; |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 365 | } |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 366 | print_one_clock(count - delta, clock_lines[count + delta], |
| 367 | bold[count + delta], (hrow == (count + delta))); |
| 368 | count++; |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 369 | } |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 370 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 371 | old_clock_line_no = clock_line_no; |
| 372 | clock_line_no = 0; |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 373 | } |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 374 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 375 | static void insert_children(struct clock_info **parent, struct clock_info *clk) |
Amit Arora | f4fb810 | 2010-11-30 13:55:50 +0530 | [diff] [blame] | 376 | { |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 377 | if (!(*parent)->num_children || (*parent)->children == NULL) { |
| 378 | (*parent)->children = (struct clock_info **) |
| 379 | malloc(sizeof(struct clock_info *)*2); |
| 380 | (*parent)->num_children = 0; |
| 381 | } else |
| 382 | (*parent)->children = (struct clock_info **) |
| 383 | realloc((*parent)->children, |
| 384 | sizeof(struct clock_info *) * |
| 385 | ((*parent)->num_children + 2)); |
| 386 | if ((*parent)->num_children > 0) |
| 387 | (*parent)->children[(*parent)->num_children - 1]->last_child |
| 388 | = 0; |
| 389 | clk->last_child = 1; |
| 390 | (*parent)->children[(*parent)->num_children] = clk; |
| 391 | (*parent)->children[(*parent)->num_children + 1] = NULL; |
| 392 | (*parent)->num_children++; |
Amit Arora | f4fb810 | 2010-11-30 13:55:50 +0530 | [diff] [blame] | 393 | } |
| 394 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 395 | static struct clock_info *read_clock_info_recur(char *clkpath, int level, |
| 396 | struct clock_info *parent) |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 397 | { |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 398 | int ret = 0; |
| 399 | DIR *dir; |
| 400 | char filename[PATH_MAX]; |
| 401 | struct dirent *item; |
| 402 | struct clock_info *cur = NULL; |
| 403 | struct stat buf; |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 404 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 405 | dir = opendir(clkpath); |
| 406 | if (!dir) |
| 407 | return NULL; |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 408 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 409 | while ((item = readdir(dir))) { |
| 410 | struct clock_info *child; |
| 411 | /* skip hidden dirs except ".." */ |
| 412 | if (item->d_name[0] == '.' ) |
| 413 | continue; |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 414 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 415 | sprintf(filename, "%s/%s", clkpath, item->d_name); |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 416 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 417 | ret = stat(filename, &buf); |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 418 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 419 | if (ret < 0) { |
| 420 | printf("Error doing a stat on %s\n", filename); |
| 421 | exit(1); |
| 422 | } |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 423 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 424 | if (S_ISREG(buf.st_mode)) { |
| 425 | if (!strcmp(item->d_name, "flags")) |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 426 | file_read_hex(filename, &parent->flags); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 427 | if (!strcmp(item->d_name, "rate")) |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 428 | file_read_int(filename, &parent->rate); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 429 | if (!strcmp(item->d_name, "usecount")) |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 430 | file_read_int(filename, &parent->usecount); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 431 | continue; |
| 432 | } |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 433 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 434 | if (!S_ISDIR(buf.st_mode)) |
| 435 | continue; |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 436 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 437 | cur = (struct clock_info *)malloc(sizeof(struct clock_info)); |
| 438 | memset(cur, 0, sizeof(cur)); |
| 439 | strcpy(cur->name, item->d_name); |
| 440 | cur->children = NULL; |
| 441 | cur->parent = NULL; |
| 442 | cur->num_children = 0; |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 443 | cur->expanded = 0; |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 444 | cur->level = level; |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 445 | child = read_clock_info_recur(filename, level + 1, cur); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 446 | insert_children(&parent, cur); |
| 447 | cur->parent = parent; |
| 448 | } |
| 449 | closedir(dir); |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 450 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 451 | return cur; |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 452 | } |
| 453 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 454 | void read_clock_info(char *clkpath) |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 455 | { |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 456 | DIR *dir; |
| 457 | struct dirent *item; |
| 458 | char filename[NAME_MAX], clockname[NAME_MAX]; |
| 459 | struct clock_info *child; |
| 460 | struct clock_info *cur; |
| 461 | |
| 462 | dir = opendir(clkpath); |
| 463 | if (!dir) |
| 464 | return; |
| 465 | |
| 466 | clocks_info = (struct clock_info *)malloc(sizeof(struct clock_info)); |
| 467 | memset(clocks_info, 0, sizeof(clocks_info)); |
| 468 | strcpy(clocks_info->name, "/"); |
| 469 | clocks_info->level = 0; |
| 470 | |
| 471 | while ((item = readdir(dir))) { |
| 472 | /* skip hidden dirs except ".." */ |
| 473 | if (item->d_name[0] == '.') |
| 474 | continue; |
| 475 | |
| 476 | strcpy(clockname, item->d_name); |
| 477 | sprintf(filename, "%s/%s", clkpath, item->d_name); |
| 478 | cur = (struct clock_info *)malloc(sizeof(struct clock_info)); |
| 479 | memset(cur, 0, sizeof(struct clock_info)); |
| 480 | strcpy(cur->name, clockname); |
| 481 | cur->parent = clocks_info; |
| 482 | cur->num_children = 0; |
| 483 | cur->expanded = 0; |
| 484 | cur->level = 1; |
| 485 | insert_children(&clocks_info, cur); |
| 486 | child = read_clock_info_recur(filename, 2, cur); |
| 487 | } |
| 488 | closedir(dir); |
| 489 | } |
| 490 | |
| 491 | void read_and_dump_clock_info_one(char *clk, bool dump) |
| 492 | { |
| 493 | printf("\nParents for \"%s\" Clock :\n\n", clk); |
| 494 | read_clock_info(clk_dir_path); |
| 495 | dump_all_parents(clk, dump); |
| 496 | printf("\n\n"); |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 497 | } |
| 498 | |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 499 | void dump_clock_info(struct clock_info *clk, int level, int bmp) |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 500 | { |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 501 | int i, j; |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 502 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 503 | if (!clk) |
| 504 | return; |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 505 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 506 | for (i = 1, j = 0; i < level; i++, j = (i - 1)) { |
| 507 | if (i == (level - 1)) { |
| 508 | if (clk->last_child) |
| 509 | printf("`-- "); |
| 510 | else |
| 511 | printf("|-- "); |
| 512 | } else { |
| 513 | if ((1<<j) & bmp) |
| 514 | printf("| "); |
| 515 | else |
| 516 | printf(" "); |
| 517 | } |
| 518 | } |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 519 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 520 | if (clk == clocks_info) |
| 521 | printf("%s\n", clk->name); |
| 522 | else { |
| 523 | char *unit = "Hz"; |
| 524 | double drate = (double)clk->rate; |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 525 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 526 | if (drate > 1000 && drate < 1000000) { |
| 527 | unit = "KHz"; |
| 528 | drate /= 1000; |
| 529 | } |
| 530 | if (drate > 1000000) { |
| 531 | unit = "MHz"; |
| 532 | drate /= 1000000; |
| 533 | } |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 534 | printf("%s (flags:0x%x,usecount:%d,rate:%5.2f %s)\n", |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 535 | clk->name, clk->flags, clk->usecount, drate, unit); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 536 | } |
| 537 | if (clk->children) { |
| 538 | int tbmp = bmp; |
| 539 | int xbmp = -1; |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 540 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 541 | if (clk->last_child) { |
| 542 | xbmp ^= 1 << (level - 2); |
| 543 | |
| 544 | xbmp = tbmp & xbmp; |
| 545 | } else |
| 546 | xbmp = bmp; |
| 547 | for (i = 0; i < clk->num_children; i++) { |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 548 | tbmp = xbmp | (1 << level); |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 549 | dump_clock_info(clk->children[i], level + 1, tbmp); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 550 | } |
| 551 | } |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 552 | } |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 553 | |
| 554 | void read_and_dump_clock_info(int verbose) |
| 555 | { |
| 556 | (void)verbose; |
| 557 | printf("\nClock Tree :\n"); |
| 558 | printf("**********\n"); |
| 559 | read_clock_info(clk_dir_path); |
| 560 | dump_clock_info(clocks_info, 1, 1); |
| 561 | printf("\n\n"); |
| 562 | } |