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