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 | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 152 | static inline const char *clock_rate(int *rate) |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 153 | { |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 154 | int r; |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 155 | |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 156 | /* GHZ */ |
| 157 | r = *rate >> 30; |
| 158 | if (r) { |
| 159 | *rate = r; |
| 160 | return "GHZ"; |
| 161 | } |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 162 | |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 163 | /* MHZ */ |
| 164 | r = *rate >> 20; |
| 165 | if (r) { |
| 166 | *rate = r; |
| 167 | return "MHZ"; |
| 168 | } |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 169 | |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 170 | /* KHZ */ |
| 171 | r = *rate >> 10; |
| 172 | if (r) { |
| 173 | *rate = r; |
| 174 | return "KHZ"; |
| 175 | } |
| 176 | |
| 177 | return ""; |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 180 | static int dump_clock_cb(struct tree *t, void *data) |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 181 | { |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 182 | struct clock_info *clk = t->private; |
| 183 | struct clock_info *pclk; |
| 184 | const char *unit; |
| 185 | int ret = 0; |
| 186 | int rate = clk->rate; |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 187 | |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 188 | if (!t->parent) { |
| 189 | printf("/\n"); |
| 190 | clk->prefix = ""; |
| 191 | return 0; |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 194 | pclk = t->parent->private; |
| 195 | |
| 196 | if (!clk->prefix) |
| 197 | ret = asprintf(&clk->prefix, "%s%s%s", pclk->prefix, |
| 198 | t->depth > 1 ? " ": "", t->next ? "|" : " "); |
| 199 | if (ret < 0) |
| 200 | return -1; |
| 201 | |
| 202 | unit = clock_rate(&rate); |
| 203 | |
| 204 | printf("%s%s-- %s (flags:0x%x, usecount:%d, rate: %d %s)\n", |
| 205 | clk->prefix, !t->next ? "`" : "", t->name, clk->flags, |
| 206 | clk->usecount, rate, unit); |
| 207 | |
| 208 | return 0; |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 211 | int dump_clock_info(void) |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 212 | { |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 213 | return tree_for_each(clock_tree, dump_clock_cb, NULL); |
| 214 | } |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 215 | |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 216 | static int dump_all_parents(char *clkarg, bool dump) |
| 217 | { |
| 218 | struct tree *tree; |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 219 | |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 220 | tree = tree_find(clock_tree, clkarg); |
| 221 | if (!tree) { |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 222 | printf("Clock NOT found!\n"); |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 223 | return -1; |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 224 | } |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame^] | 225 | |
| 226 | return tree_for_each_parent(tree, dump_clock_cb, NULL); |
Daniel Lezcano | c671833 | 2011-03-23 14:37:39 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Daniel Lezcano | 897f733 | 2011-03-26 22:06:06 +0100 | [diff] [blame] | 229 | void find_parents_for_clock(char *clkname, int complete) |
Amit Arora | 3bd7916 | 2010-12-01 13:51:42 +0530 | [diff] [blame] | 230 | { |
| 231 | char name[256]; |
| 232 | |
| 233 | name[0] = '\0'; |
| 234 | if (!complete) { |
| 235 | char str[256]; |
| 236 | |
| 237 | strcat(name, clkname); |
| 238 | sprintf(str, "Enter Clock Name : %s\n", name); |
| 239 | print_one_clock(2, str, 1, 0); |
| 240 | return; |
| 241 | } |
| 242 | sprintf(name, "Parents for \"%s\" Clock : \n", clkname); |
| 243 | print_one_clock(0, name, 1, 1); |
Daniel Lezcano | 897f733 | 2011-03-26 22:06:06 +0100 | [diff] [blame] | 244 | dump_all_parents(clkname, false); |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 245 | } |
Amit Arora | 3bd7916 | 2010-12-01 13:51:42 +0530 | [diff] [blame] | 246 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 247 | static void destroy_clocks_info_recur(struct clock_info *clock) |
| 248 | { |
| 249 | int i; |
| 250 | |
| 251 | if (clock && clock->num_children) { |
| 252 | for (i = (clock->num_children - 1); i >= 0; i--) { |
| 253 | fflush(stdin); |
| 254 | destroy_clocks_info_recur(clock->children[i]); |
| 255 | if (!i) { |
| 256 | free(clock->children); |
| 257 | clock->children = NULL; |
| 258 | clock->num_children = 0; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static void destroy_clocks_info(void) |
| 265 | { |
| 266 | int i; |
| 267 | |
| 268 | if (!clocks_info) |
| 269 | return; |
| 270 | |
| 271 | if (clocks_info->num_children) { |
| 272 | for (i = (clocks_info->num_children - 1); i >= 0 ; i--) { |
| 273 | destroy_clocks_info_recur(clocks_info->children[i]); |
| 274 | if (!i) { |
| 275 | free(clocks_info->children); |
| 276 | clocks_info->children = NULL; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | clocks_info->num_children = 0; |
| 281 | free(clocks_info); |
| 282 | clocks_info = NULL; |
| 283 | } |
| 284 | |
| 285 | |
Amit Arora | 24ed7d1 | 2010-09-14 12:12:58 +0530 | [diff] [blame] | 286 | int read_and_print_clock_info(int verbose, int hrow, int selected) |
| 287 | { |
Amit Arora | 3bd7916 | 2010-12-01 13:51:42 +0530 | [diff] [blame] | 288 | print_one_clock(0, "Reading Clock Tree ...", 1, 1); |
Amit Arora | a06a730 | 2010-12-02 15:59:37 +0530 | [diff] [blame] | 289 | |
| 290 | if (!old_clock_line_no || selected == REFRESH_WINDOW) { |
| 291 | destroy_clocks_info(); |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 292 | read_clock_info(clk_dir_path); |
Amit Arora | a06a730 | 2010-12-02 15:59:37 +0530 | [diff] [blame] | 293 | } |
Amit Arora | 29cb757 | 2010-10-05 17:40:29 +0530 | [diff] [blame] | 294 | |
Daniel Lezcano | 95b0dac | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 295 | if (!clocks_info || !clocks_info->num_children) { |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 296 | fprintf(stderr, "powerdebug: No clocks found. Exiting..\n"); |
| 297 | exit(1); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 298 | } |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 299 | |
Amit Arora | a06a730 | 2010-12-02 15:59:37 +0530 | [diff] [blame] | 300 | if (selected == CLOCK_SELECTED) |
| 301 | selected = 1; |
| 302 | else |
| 303 | selected = 0; |
| 304 | |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 305 | print_clock_info(verbose, hrow, selected); |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 306 | hrow = (hrow < old_clock_line_no) ? hrow : old_clock_line_no - 1; |
| 307 | |
| 308 | return hrow; |
| 309 | } |
| 310 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 311 | static int calc_delta_screen_size(int hrow) |
Amit Arora | 3bc8c92 | 2010-11-16 11:27:38 +0530 | [diff] [blame] | 312 | { |
Amit Arora | 51d1b9c | 2010-11-30 13:55:15 +0530 | [diff] [blame] | 313 | if (hrow >= (maxy - 3)) |
| 314 | return hrow - (maxy - 4); |
Amit Arora | 3bc8c92 | 2010-11-16 11:27:38 +0530 | [diff] [blame] | 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 319 | static void prepare_name_str(char *namestr, struct clock_info *clock) |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 320 | { |
| 321 | int i; |
| 322 | |
| 323 | strcpy(namestr, ""); |
| 324 | if (clock->level > 1) |
| 325 | for (i = 0; i < (clock->level - 1); i++) |
| 326 | strcat(namestr, " "); |
| 327 | strcat(namestr, clock->name); |
| 328 | } |
| 329 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 330 | static void collapse_all_subclocks(struct clock_info *clock) |
| 331 | { |
| 332 | int i; |
| 333 | |
| 334 | clock->expanded = 0; |
| 335 | if (clock->num_children) |
| 336 | for (i = 0; i < clock->num_children; i++) |
| 337 | collapse_all_subclocks(clock->children[i]); |
| 338 | } |
| 339 | |
| 340 | static void add_clock_details_recur(struct clock_info *clock, |
| 341 | int hrow, int selected) |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 342 | { |
| 343 | int i; |
| 344 | char *unit = " Hz"; |
| 345 | char rate_str[64]; |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 346 | char name_str[256]; |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 347 | double drate = (double)clock->rate; |
| 348 | |
| 349 | if (drate > 1000 && drate < 1000000) { |
| 350 | unit = "KHz"; |
| 351 | drate /= 1000; |
| 352 | } |
| 353 | if (drate > 1000000) { |
| 354 | unit = "MHz"; |
| 355 | drate /= 1000000; |
| 356 | } |
| 357 | if (clock->usecount) |
| 358 | bold[clock_line_no] = 1; |
| 359 | else |
| 360 | bold[clock_line_no] = 0; |
| 361 | |
| 362 | sprintf(rate_str, "%.2f %s", drate, unit); |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 363 | prepare_name_str(name_str, clock); |
Daniel Lezcano | 4b66907 | 2011-05-24 15:27:49 +0200 | [diff] [blame] | 364 | sprintf(clock_lines[clock_line_no++], "%-55s 0x%-4x %-12s %-12d %-12d", |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 365 | name_str, clock->flags, rate_str, clock->usecount, |
| 366 | clock->num_children); |
| 367 | |
| 368 | if (selected && (hrow == (clock_line_no - 1))) { |
| 369 | if (clock->expanded) |
| 370 | collapse_all_subclocks(clock); |
| 371 | else |
| 372 | clock->expanded = 1; |
| 373 | selected = 0; |
| 374 | } |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 375 | |
| 376 | if (clock->expanded && clock->num_children) |
| 377 | for (i = 0; i < clock->num_children; i++) |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 378 | add_clock_details_recur(clock->children[i], |
| 379 | hrow, selected); |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 380 | strcpy(clock_lines[clock_line_no], ""); |
| 381 | } |
| 382 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 383 | void print_clock_info(int verbose, int hrow, int selected) |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 384 | { |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 385 | int i, count = 0, delta; |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 386 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 387 | (void)verbose; |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 388 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 389 | print_clock_header(); |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 390 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 391 | for (i = 0; i < clocks_info->num_children; i++) |
| 392 | add_clock_details_recur(clocks_info->children[i], |
| 393 | hrow, selected); |
Amit Arora | a06a730 | 2010-12-02 15:59:37 +0530 | [diff] [blame] | 394 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 395 | delta = calc_delta_screen_size(hrow); |
| 396 | |
| 397 | while (clock_lines[count + delta] && |
| 398 | strcmp(clock_lines[count + delta], "")) { |
| 399 | if (count < delta) { |
| 400 | count++; |
| 401 | continue; |
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 | print_one_clock(count - delta, clock_lines[count + delta], |
| 404 | bold[count + delta], (hrow == (count + delta))); |
| 405 | count++; |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 406 | } |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 407 | |
Daniel Lezcano | ef32319 | 2011-03-26 22:06:20 +0100 | [diff] [blame] | 408 | old_clock_line_no = clock_line_no; |
| 409 | clock_line_no = 0; |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 410 | } |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 411 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 412 | static void insert_children(struct clock_info **parent, struct clock_info *clk) |
Amit Arora | f4fb810 | 2010-11-30 13:55:50 +0530 | [diff] [blame] | 413 | { |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 414 | if (!(*parent)->num_children || (*parent)->children == NULL) { |
| 415 | (*parent)->children = (struct clock_info **) |
| 416 | malloc(sizeof(struct clock_info *)*2); |
| 417 | (*parent)->num_children = 0; |
| 418 | } else |
| 419 | (*parent)->children = (struct clock_info **) |
| 420 | realloc((*parent)->children, |
| 421 | sizeof(struct clock_info *) * |
| 422 | ((*parent)->num_children + 2)); |
| 423 | if ((*parent)->num_children > 0) |
| 424 | (*parent)->children[(*parent)->num_children - 1]->last_child |
| 425 | = 0; |
| 426 | clk->last_child = 1; |
| 427 | (*parent)->children[(*parent)->num_children] = clk; |
| 428 | (*parent)->children[(*parent)->num_children + 1] = NULL; |
| 429 | (*parent)->num_children++; |
Amit Arora | f4fb810 | 2010-11-30 13:55:50 +0530 | [diff] [blame] | 430 | } |
| 431 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 432 | static struct clock_info *read_clock_info_recur(char *clkpath, int level, |
| 433 | struct clock_info *parent) |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 434 | { |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 435 | int ret = 0; |
| 436 | DIR *dir; |
| 437 | char filename[PATH_MAX]; |
| 438 | struct dirent *item; |
| 439 | struct clock_info *cur = NULL; |
| 440 | struct stat buf; |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 441 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 442 | dir = opendir(clkpath); |
| 443 | if (!dir) |
| 444 | return NULL; |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 445 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 446 | while ((item = readdir(dir))) { |
| 447 | struct clock_info *child; |
| 448 | /* skip hidden dirs except ".." */ |
| 449 | if (item->d_name[0] == '.' ) |
| 450 | continue; |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 451 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 452 | sprintf(filename, "%s/%s", clkpath, item->d_name); |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 453 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 454 | ret = stat(filename, &buf); |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 455 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 456 | if (ret < 0) { |
| 457 | printf("Error doing a stat on %s\n", filename); |
| 458 | exit(1); |
| 459 | } |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 460 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 461 | if (S_ISREG(buf.st_mode)) { |
| 462 | if (!strcmp(item->d_name, "flags")) |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 463 | file_read_hex(filename, &parent->flags); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 464 | if (!strcmp(item->d_name, "rate")) |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 465 | file_read_int(filename, &parent->rate); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 466 | if (!strcmp(item->d_name, "usecount")) |
Daniel Lezcano | 2d19ae8 | 2011-03-26 22:06:09 +0100 | [diff] [blame] | 467 | file_read_int(filename, &parent->usecount); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 468 | continue; |
| 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_ISDIR(buf.st_mode)) |
| 472 | continue; |
Amit Arora | 0e51272 | 2010-10-01 12:24:16 +0530 | [diff] [blame] | 473 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 474 | cur = (struct clock_info *)malloc(sizeof(struct clock_info)); |
| 475 | memset(cur, 0, sizeof(cur)); |
| 476 | strcpy(cur->name, item->d_name); |
| 477 | cur->children = NULL; |
| 478 | cur->parent = NULL; |
| 479 | cur->num_children = 0; |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 480 | cur->expanded = 0; |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 481 | cur->level = level; |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 482 | child = read_clock_info_recur(filename, level + 1, cur); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 483 | insert_children(&parent, cur); |
| 484 | cur->parent = parent; |
| 485 | } |
| 486 | closedir(dir); |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 487 | |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 488 | return cur; |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 489 | } |
| 490 | |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 491 | static struct clock_info *clock_alloc(const char *name) |
| 492 | { |
| 493 | struct clock_info *ci; |
| 494 | |
| 495 | ci = malloc(sizeof(*ci)); |
| 496 | if (ci) { |
| 497 | memset(ci, 0, sizeof(*ci)); |
| 498 | strcpy(ci->name, name); |
| 499 | } |
| 500 | |
| 501 | return ci; |
| 502 | } |
| 503 | |
Daniel Lezcano | 9d5431c | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 504 | static int fill_clock_cb(struct tree *t, void *data) |
| 505 | { |
| 506 | struct clock_info *clkinfo; |
| 507 | |
| 508 | clkinfo = clock_alloc(t->name); |
| 509 | if (!clkinfo) |
| 510 | return -1; |
| 511 | |
| 512 | t->private = clkinfo; |
| 513 | clkinfo->level = t->depth; |
| 514 | |
| 515 | file_read_value(t->path, "flags", "%x", &clkinfo->flags); |
| 516 | file_read_value(t->path, "rate", "%d", &clkinfo->rate); |
| 517 | file_read_value(t->path, "usecount", "%d", &clkinfo->usecount); |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 522 | int read_clock_info(char *clkpath) |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 523 | { |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 524 | DIR *dir; |
| 525 | struct dirent *item; |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 526 | char filename[NAME_MAX]; |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 527 | struct clock_info *child; |
| 528 | struct clock_info *cur; |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 529 | int ret = -1; |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 530 | |
Daniel Lezcano | 9d5431c | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 531 | if (tree_for_each(clock_tree, fill_clock_cb, NULL)) |
| 532 | return -1; |
| 533 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 534 | dir = opendir(clkpath); |
| 535 | if (!dir) |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 536 | return -1; |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 537 | |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 538 | clocks_info = clock_alloc("/"); |
| 539 | if (!clocks_info) |
| 540 | return -1; |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 541 | |
| 542 | while ((item = readdir(dir))) { |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 543 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 544 | /* skip hidden dirs except ".." */ |
| 545 | if (item->d_name[0] == '.') |
| 546 | continue; |
| 547 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 548 | sprintf(filename, "%s/%s", clkpath, item->d_name); |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 549 | |
| 550 | cur = clock_alloc(item->d_name); |
| 551 | if (!cur) |
| 552 | goto out; |
| 553 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 554 | cur->parent = clocks_info; |
| 555 | cur->num_children = 0; |
| 556 | cur->expanded = 0; |
| 557 | cur->level = 1; |
| 558 | insert_children(&clocks_info, cur); |
| 559 | child = read_clock_info_recur(filename, 2, cur); |
| 560 | } |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 561 | |
| 562 | ret = 0; |
| 563 | |
| 564 | out: |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 565 | closedir(dir); |
Daniel Lezcano | b2565a8 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 566 | |
| 567 | return ret; |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | void read_and_dump_clock_info_one(char *clk, bool dump) |
| 571 | { |
| 572 | printf("\nParents for \"%s\" Clock :\n\n", clk); |
| 573 | read_clock_info(clk_dir_path); |
| 574 | dump_all_parents(clk, dump); |
| 575 | printf("\n\n"); |
Amit Arora | eb6cba9 | 2010-10-25 16:03:21 +0530 | [diff] [blame] | 576 | } |
| 577 | |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 578 | void read_and_dump_clock_info(int verbose) |
| 579 | { |
| 580 | (void)verbose; |
| 581 | printf("\nClock Tree :\n"); |
| 582 | printf("**********\n"); |
| 583 | read_clock_info(clk_dir_path); |
Daniel Lezcano | 7801c2d | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 584 | dump_clock_info(); |
Daniel Lezcano | 28b53cd | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 585 | printf("\n\n"); |
| 586 | } |