4e662aedd0f43d0450cf0e548fa9a0f6fb59ba33
[k3conf/k3conf.git] / common / cmd_dump.c
1 /*
2  * K3CONF Command Dump
3  *
4  * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
5  *      Lokesh Vutla <lokeshvutla@ti.com>
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
11  *    Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  *    Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the
17  *    distribution.
18  *
19  *    Neither the name of Texas Instruments Incorporated nor the names of
20  *    its contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <tisci.h>
40 #include <socinfo.h>
41 #include <help.h>
42 #include <autoadjust_table.h>
43 #include <k3conf.h>
45 int dump_clocks_info(int argc, char *argv[])
46 {
47         struct ti_sci_clocks_info *c = soc_info.sci_info.clocks_info;
48         char table[TABLE_MAX_ROW][TABLE_MAX_COL][TABLE_MAX_ELT_LEN];
49         uint32_t row = 0, dev_id;
50         int found = 0, ret;
51         uint64_t freq;
53         autoadjust_table_init(table);
54         strncpy(table[row][0], "Device ID", TABLE_MAX_ELT_LEN);
55         strncpy(table[row][1], "Clock ID", TABLE_MAX_ELT_LEN);
56         strncpy(table[row][2], "Clock Name", TABLE_MAX_ELT_LEN);
57         strncpy(table[row][3], "Status", TABLE_MAX_ELT_LEN);
58         strncpy(table[row][4], "Clock Frequency", TABLE_MAX_ELT_LEN);
60         if (argc)
61                 goto print_single_device;
63         for (row = 0; row < soc_info.sci_info.num_clocks; row++) {
64                 snprintf(table[row + 1][0], TABLE_MAX_ELT_LEN, "%5d",
65                          c[row].dev_id);
66                 snprintf(table[row + 1][1], TABLE_MAX_ELT_LEN, "%5d",
67                          c[row].clk_id);
68                 strncpy(table[row + 1][2], c[row].clk_name, TABLE_MAX_ELT_LEN);
69                 snprintf(table[row + 1][3], TABLE_MAX_ELT_LEN, "%s",
70                          ti_sci_cmd_get_clk_state(c[row].dev_id, c[row].clk_id));
71                 ti_sci_cmd_get_clk_freq(c[row].dev_id, c[row].clk_id, &freq);
72                 snprintf(table[row + 1][4], TABLE_MAX_ELT_LEN, "%lu", freq);
73         }
75         return autoadjust_table_print(table, row + 1, 5);
77 print_single_device:
78         ret = sscanf(argv[0], "%u", &dev_id);
79         if (ret != 1)
80                 return -1;
82         for (row = 0; row < soc_info.sci_info.num_clocks; row++) {
83                 if (dev_id == c[row].dev_id) {
84                         snprintf(table[found + 1][0], TABLE_MAX_ELT_LEN, "%5d",
85                                  c[row].dev_id);
86                         snprintf(table[found + 1][1], TABLE_MAX_ELT_LEN, "%5d",
87                                  c[row].clk_id);
88                         strncpy(table[found + 1][2], c[row].clk_name,
89                                 TABLE_MAX_ELT_LEN);
90                         strncpy(table[found + 1][3],
91                                 ti_sci_cmd_get_clk_state(dev_id, c[row].clk_id),
92                                 TABLE_MAX_ELT_LEN);
93                         ti_sci_cmd_get_clk_freq(c[row].dev_id, c[row].clk_id,
94                                                 &freq);
95                         snprintf(table[found + 1][4], TABLE_MAX_ELT_LEN, "%lu",
96                                  freq);
97                         found++;
98                 }
99         }
101         if (!found)
102                 return -1;
104         return autoadjust_table_print(table, found + 1, 5);
107 int dump_devices_info(int argc, char *argv[])
109         struct ti_sci_devices_info *p = soc_info.sci_info.devices_info;
110         char table[TABLE_MAX_ROW][TABLE_MAX_COL][TABLE_MAX_ELT_LEN];
111         uint32_t row = 0, dev_id;
112         int found = 0, ret;
114         autoadjust_table_init(table);
115         strncpy(table[row][0], "Device ID", TABLE_MAX_ELT_LEN);
116         strncpy(table[row][1], "Device Name", TABLE_MAX_ELT_LEN);
117         strncpy(table[row][2], "Device Status", TABLE_MAX_ELT_LEN);
119         if (argc)
120                 goto print_single_device;
122         for (row = 0; row < soc_info.sci_info.num_devices; row++) {
123                 snprintf(table[row + 1][0], TABLE_MAX_ELT_LEN, "%5d",
124                          p[row].dev_id);
125                 strncpy(table[row + 1][1], p[row].name, TABLE_MAX_ELT_LEN);
126                 snprintf(table[row + 1][2], TABLE_MAX_ELT_LEN, "%s",
127                          ti_sci_cmd_get_device_status(p[row].dev_id));
128         }
130         return autoadjust_table_print(table, row + 1, 3);
132 print_single_device:
133         ret = sscanf(argv[0], "%u", &dev_id);
134         if (ret != 1)
135                 return -1;
137         for (row = 0; row < soc_info.sci_info.num_devices; row++) {
138                 if (dev_id == p[row].dev_id) {
139                         snprintf(table[1][0], TABLE_MAX_ELT_LEN, "%5d",
140                                  p[row].dev_id);
141                         strncpy(table[1][1], p[row].name,
142                                 TABLE_MAX_ELT_LEN);
143                         snprintf(table[1][2], TABLE_MAX_ELT_LEN, "%s",
144                                  ti_sci_cmd_get_device_status(p[row].dev_id));
145                         found = 1;
146                         break;
147                 }
148         }
150         if (!found)
151                 return -1;
153         return autoadjust_table_print(table, 2, 3);
156 static int dump_processors_info(int argc, char *argv[])
158         struct ti_sci_processors_info *p = soc_info.sci_info.processors_info;
159         char table[TABLE_MAX_ROW][TABLE_MAX_COL][TABLE_MAX_ELT_LEN];
160         uint32_t row = 0, proc_id;
161         int found = 0, ret;
162         uint64_t freq;
164         autoadjust_table_init(table);
165         strncpy(table[row][0], "Device ID", TABLE_MAX_ELT_LEN);
166         strncpy(table[row][1], "Processor ID", TABLE_MAX_ELT_LEN);
167         strncpy(table[row][2], "Processor Name", TABLE_MAX_ELT_LEN);
168         strncpy(table[row][3], "Processor State", TABLE_MAX_ELT_LEN);
169         strncpy(table[row][4], "Processor Frequency", TABLE_MAX_ELT_LEN);
171         if (argc)
172                 goto print_single_processor;
174         for (row = 0; row < soc_info.sci_info.num_processors; row++) {
175                 snprintf(table[row + 1][0], TABLE_MAX_ELT_LEN, "%5d",
176                          p[row].dev_id);
177                 snprintf(table[row + 1][1], TABLE_MAX_ELT_LEN, "%7d",
178                          p[row].processor_id);
179                 strncpy(table[row + 1][2], p[row].name, TABLE_MAX_ELT_LEN);
180                 /* ToDo: Should we get the state from proc ops */
181                 snprintf(table[row + 1][3], TABLE_MAX_ELT_LEN, "%s",
182                          ti_sci_cmd_get_device_status(p[row].dev_id));
183                 ti_sci_cmd_get_clk_freq(p[row].dev_id, p[row].clk_id, &freq);
184                 snprintf(table[row + 1][4], TABLE_MAX_ELT_LEN, "%lu", freq);
185         }
187         return autoadjust_table_print(table, row + 1, 5);
189 print_single_processor:
190         ret = sscanf(argv[0], "%u", &proc_id);
191         if (ret != 1)
192                 return -1;
194         for (row = 0; row < soc_info.sci_info.num_processors; row++) {
195                 if (proc_id != p[row].processor_id)
196                         continue;
197                 snprintf(table[found + 1][0], TABLE_MAX_ELT_LEN, "%5d",
198                          p[row].dev_id);
199                 snprintf(table[found + 1][1], TABLE_MAX_ELT_LEN, "%7d",
200                          p[row].processor_id);
201                 strncpy(table[found + 1][2], p[row].name, TABLE_MAX_ELT_LEN);
202                 /* ToDo: Should we get the state from proc ops */
203                 snprintf(table[found + 1][3], TABLE_MAX_ELT_LEN, "%s",
204                          ti_sci_cmd_get_device_status(p[row].dev_id));
205                 ti_sci_cmd_get_clk_freq(p[row].dev_id, p[row].clk_id, &freq);
206                 snprintf(table[found + 1][4], TABLE_MAX_ELT_LEN, "%lu", freq);
207                 found++;
208                 break;
209         }
210         if (!found)
211                 return -1;
213         return autoadjust_table_print(table, found + 1, 5);
216 int process_dump_command(int argc, char *argv[])
218         int ret;
220         if (argc < 1) {
221                 help(HELP_DUMP);
222                 return -1;
223         }
225         if (!strncmp(argv[0], "device", 6)) {
226                 argc--;
227                 argv++;
228                 ret = dump_devices_info(argc, argv);
229                 if (ret) {
230                         fprintf(stderr, "Invalid device arguments\n");
231                         help(HELP_DUMP_DEVICE);
232                 }
233         } else if (!strncmp(argv[0], "clock", 5)) {
234                 argc--;
235                 argv++;
236                 ret = dump_clocks_info(argc, argv);
237                 if (ret) {
238                         fprintf(stderr, "Invalid clock arguments\n");
239                         help(HELP_DUMP_CLOCK);
240                 }
241         } else if(!strncmp(argv[0], "processor", 9)) {
242                 argc--;
243                 argv++;
244                 ret = dump_processors_info(argc, argv);
245                 if (ret)
246                         help(HELP_DUMP_PROCESSOR);
247         } else if (!strcmp(argv[0], "--help")) {
248                 help(HELP_DUMP);
249                 return 0;
250         } else {
251                 fprintf(stderr, "Invalid argument %s\n", argv[1]);
252                 help(HELP_DUMP);
253                 return -1;
254         }
255         return ret;