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