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