Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / tools / power / x86 / intel-speed-select / isst-config.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel Speed Select -- Enumerate and control features
4  * Copyright (c) 2019 Intel Corporation.
5  */
6
7 #include <linux/isst_if.h>
8
9 #include "isst.h"
10
11 struct process_cmd_struct {
12         char *feature;
13         char *command;
14         void (*process_fn)(void);
15 };
16
17 static const char *version_str = "v1.0";
18 static const int supported_api_ver = 1;
19 static struct isst_if_platform_info isst_platform_info;
20 static char *progname;
21 static int debug_flag;
22 static FILE *outf;
23
24 static int cpu_model;
25
26 #define MAX_CPUS_IN_ONE_REQ 64
27 static short max_target_cpus;
28 static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
29
30 static int topo_max_cpus;
31 static size_t present_cpumask_size;
32 static cpu_set_t *present_cpumask;
33 static size_t target_cpumask_size;
34 static cpu_set_t *target_cpumask;
35 static int tdp_level = 0xFF;
36 static int fact_bucket = 0xFF;
37 static int fact_avx = 0xFF;
38 static unsigned long long fact_trl;
39 static int out_format_json;
40 static int cmd_help;
41
42 /* clos related */
43 static int current_clos = -1;
44 static int clos_epp = -1;
45 static int clos_prop_prio = -1;
46 static int clos_min = -1;
47 static int clos_max = -1;
48 static int clos_desired = -1;
49 static int clos_priority_type;
50
51 struct _cpu_map {
52         unsigned short core_id;
53         unsigned short pkg_id;
54         unsigned short die_id;
55         unsigned short punit_cpu;
56         unsigned short punit_cpu_core;
57 };
58 struct _cpu_map *cpu_map;
59
60 void debug_printf(const char *format, ...)
61 {
62         va_list args;
63
64         va_start(args, format);
65
66         if (debug_flag)
67                 vprintf(format, args);
68
69         va_end(args);
70 }
71
72 static void update_cpu_model(void)
73 {
74         unsigned int ebx, ecx, edx;
75         unsigned int fms, family;
76
77         __cpuid(1, fms, ebx, ecx, edx);
78         family = (fms >> 8) & 0xf;
79         cpu_model = (fms >> 4) & 0xf;
80         if (family == 6 || family == 0xf)
81                 cpu_model += ((fms >> 16) & 0xf) << 4;
82 }
83
84 /* Open a file, and exit on failure */
85 static FILE *fopen_or_exit(const char *path, const char *mode)
86 {
87         FILE *filep = fopen(path, mode);
88
89         if (!filep)
90                 err(1, "%s: open failed", path);
91
92         return filep;
93 }
94
95 /* Parse a file containing a single int */
96 static int parse_int_file(int fatal, const char *fmt, ...)
97 {
98         va_list args;
99         char path[PATH_MAX];
100         FILE *filep;
101         int value;
102
103         va_start(args, fmt);
104         vsnprintf(path, sizeof(path), fmt, args);
105         va_end(args);
106         if (fatal) {
107                 filep = fopen_or_exit(path, "r");
108         } else {
109                 filep = fopen(path, "r");
110                 if (!filep)
111                         return -1;
112         }
113         if (fscanf(filep, "%d", &value) != 1)
114                 err(1, "%s: failed to parse number from file", path);
115         fclose(filep);
116
117         return value;
118 }
119
120 int cpufreq_sysfs_present(void)
121 {
122         DIR *dir;
123
124         dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
125         if (dir) {
126                 closedir(dir);
127                 return 1;
128         }
129
130         return 0;
131 }
132
133 int out_format_is_json(void)
134 {
135         return out_format_json;
136 }
137
138 int get_physical_package_id(int cpu)
139 {
140         return parse_int_file(
141                 1, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
142                 cpu);
143 }
144
145 int get_physical_core_id(int cpu)
146 {
147         return parse_int_file(
148                 1, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
149 }
150
151 int get_physical_die_id(int cpu)
152 {
153         int ret;
154
155         ret = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/topology/die_id",
156                              cpu);
157         if (ret < 0)
158                 ret = 0;
159
160         return ret;
161 }
162
163 int get_topo_max_cpus(void)
164 {
165         return topo_max_cpus;
166 }
167
168 #define MAX_PACKAGE_COUNT 8
169 #define MAX_DIE_PER_PACKAGE 2
170 static void for_each_online_package_in_set(void (*callback)(int, void *, void *,
171                                                             void *, void *),
172                                            void *arg1, void *arg2, void *arg3,
173                                            void *arg4)
174 {
175         int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
176         int pkg_index = 0, i;
177
178         memset(max_packages, 0xff, sizeof(max_packages));
179         for (i = 0; i < topo_max_cpus; ++i) {
180                 int j, online, pkg_id, die_id = 0, skip = 0;
181
182                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
183                         continue;
184                 if (i)
185                         online = parse_int_file(
186                                 1, "/sys/devices/system/cpu/cpu%d/online", i);
187                 else
188                         online =
189                                 1; /* online entry for CPU 0 needs some special configs */
190
191                 die_id = get_physical_die_id(i);
192                 if (die_id < 0)
193                         die_id = 0;
194                 pkg_id = get_physical_package_id(i);
195                 /* Create an unique id for package, die combination to store */
196                 pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
197
198                 for (j = 0; j < pkg_index; ++j) {
199                         if (max_packages[j] == pkg_id) {
200                                 skip = 1;
201                                 break;
202                         }
203                 }
204
205                 if (!skip && online && callback) {
206                         callback(i, arg1, arg2, arg3, arg4);
207                         max_packages[pkg_index++] = pkg_id;
208                 }
209         }
210 }
211
212 static void for_each_online_target_cpu_in_set(
213         void (*callback)(int, void *, void *, void *, void *), void *arg1,
214         void *arg2, void *arg3, void *arg4)
215 {
216         int i;
217
218         for (i = 0; i < topo_max_cpus; ++i) {
219                 int online;
220
221                 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
222                         continue;
223                 if (i)
224                         online = parse_int_file(
225                                 1, "/sys/devices/system/cpu/cpu%d/online", i);
226                 else
227                         online =
228                                 1; /* online entry for CPU 0 needs some special configs */
229
230                 if (online && callback)
231                         callback(i, arg1, arg2, arg3, arg4);
232         }
233 }
234
235 #define BITMASK_SIZE 32
236 static void set_max_cpu_num(void)
237 {
238         FILE *filep;
239         unsigned long dummy;
240
241         topo_max_cpus = 0;
242         filep = fopen_or_exit(
243                 "/sys/devices/system/cpu/cpu0/topology/thread_siblings", "r");
244         while (fscanf(filep, "%lx,", &dummy) == 1)
245                 topo_max_cpus += BITMASK_SIZE;
246         fclose(filep);
247         topo_max_cpus--; /* 0 based */
248
249         debug_printf("max cpus %d\n", topo_max_cpus);
250 }
251
252 size_t alloc_cpu_set(cpu_set_t **cpu_set)
253 {
254         cpu_set_t *_cpu_set;
255         size_t size;
256
257         _cpu_set = CPU_ALLOC((topo_max_cpus + 1));
258         if (_cpu_set == NULL)
259                 err(3, "CPU_ALLOC");
260         size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
261         CPU_ZERO_S(size, _cpu_set);
262
263         *cpu_set = _cpu_set;
264         return size;
265 }
266
267 void free_cpu_set(cpu_set_t *cpu_set)
268 {
269         CPU_FREE(cpu_set);
270 }
271
272 static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
273 static void set_cpu_present_cpu_mask(void)
274 {
275         size_t size;
276         DIR *dir;
277         int i;
278
279         size = alloc_cpu_set(&present_cpumask);
280         present_cpumask_size = size;
281         for (i = 0; i < topo_max_cpus; ++i) {
282                 char buffer[256];
283
284                 snprintf(buffer, sizeof(buffer),
285                          "/sys/devices/system/cpu/cpu%d", i);
286                 dir = opendir(buffer);
287                 if (dir) {
288                         int pkg_id, die_id;
289
290                         CPU_SET_S(i, size, present_cpumask);
291                         die_id = get_physical_die_id(i);
292                         if (die_id < 0)
293                                 die_id = 0;
294
295                         pkg_id = get_physical_package_id(i);
296                         if (pkg_id < MAX_PACKAGE_COUNT &&
297                             die_id < MAX_DIE_PER_PACKAGE)
298                                 cpu_cnt[pkg_id][die_id]++;
299                 }
300                 closedir(dir);
301         }
302 }
303
304 int get_cpu_count(int pkg_id, int die_id)
305 {
306         if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
307                 return cpu_cnt[pkg_id][die_id] + 1;
308
309         return 0;
310 }
311
312 static void set_cpu_target_cpu_mask(void)
313 {
314         size_t size;
315         int i;
316
317         size = alloc_cpu_set(&target_cpumask);
318         target_cpumask_size = size;
319         for (i = 0; i < max_target_cpus; ++i) {
320                 if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
321                                  present_cpumask))
322                         continue;
323
324                 CPU_SET_S(target_cpus[i], size, target_cpumask);
325         }
326 }
327
328 static void create_cpu_map(void)
329 {
330         const char *pathname = "/dev/isst_interface";
331         int i, fd = 0;
332         struct isst_if_cpu_maps map;
333
334         cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
335         if (!cpu_map)
336                 err(3, "cpumap");
337
338         fd = open(pathname, O_RDWR);
339         if (fd < 0)
340                 err(-1, "%s open failed", pathname);
341
342         for (i = 0; i < topo_max_cpus; ++i) {
343                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
344                         continue;
345
346                 map.cmd_count = 1;
347                 map.cpu_map[0].logical_cpu = i;
348
349                 debug_printf(" map logical_cpu:%d\n",
350                              map.cpu_map[0].logical_cpu);
351                 if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
352                         perror("ISST_IF_GET_PHY_ID");
353                         fprintf(outf, "Error: map logical_cpu:%d\n",
354                                 map.cpu_map[0].logical_cpu);
355                         continue;
356                 }
357                 cpu_map[i].core_id = get_physical_core_id(i);
358                 cpu_map[i].pkg_id = get_physical_package_id(i);
359                 cpu_map[i].die_id = get_physical_die_id(i);
360                 cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
361                 cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
362                                              1); // shift to get core id
363
364                 debug_printf(
365                         "map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
366                         i, cpu_map[i].core_id, cpu_map[i].die_id,
367                         cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
368                         cpu_map[i].punit_cpu_core);
369         }
370
371         if (fd)
372                 close(fd);
373 }
374
375 int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
376 {
377         int i;
378
379         for (i = 0; i < topo_max_cpus; ++i) {
380                 if (cpu_map[i].pkg_id == pkg_id &&
381                     cpu_map[i].die_id == die_id &&
382                     cpu_map[i].punit_cpu_core == punit_core_id)
383                         return i;
384         }
385
386         return -EINVAL;
387 }
388
389 void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
390                                       size_t core_cpumask_size,
391                                       cpu_set_t *core_cpumask, int *cpu_cnt)
392 {
393         int i, cnt = 0;
394         int die_id, pkg_id;
395
396         *cpu_cnt = 0;
397         die_id = get_physical_die_id(cpu);
398         pkg_id = get_physical_package_id(cpu);
399
400         for (i = 0; i < 64; ++i) {
401                 if (core_mask & BIT(i)) {
402                         int j;
403
404                         for (j = 0; j < topo_max_cpus; ++j) {
405                                 if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
406                                         continue;
407
408                                 if (cpu_map[j].pkg_id == pkg_id &&
409                                     cpu_map[j].die_id == die_id &&
410                                     cpu_map[j].punit_cpu_core == i) {
411                                         CPU_SET_S(j, core_cpumask_size,
412                                                   core_cpumask);
413                                         ++cnt;
414                                 }
415                         }
416                 }
417         }
418
419         *cpu_cnt = cnt;
420 }
421
422 int find_phy_core_num(int logical_cpu)
423 {
424         if (logical_cpu < topo_max_cpus)
425                 return cpu_map[logical_cpu].punit_cpu_core;
426
427         return -EINVAL;
428 }
429
430 static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
431                                   unsigned int *value)
432 {
433         struct isst_if_io_regs io_regs;
434         const char *pathname = "/dev/isst_interface";
435         int cmd;
436         int fd;
437
438         debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
439
440         fd = open(pathname, O_RDWR);
441         if (fd < 0)
442                 err(-1, "%s open failed", pathname);
443
444         io_regs.req_count = 1;
445         io_regs.io_reg[0].logical_cpu = cpu;
446         io_regs.io_reg[0].reg = reg;
447         cmd = ISST_IF_IO_CMD;
448         if (write) {
449                 io_regs.io_reg[0].read_write = 1;
450                 io_regs.io_reg[0].value = *value;
451         } else {
452                 io_regs.io_reg[0].read_write = 0;
453         }
454
455         if (ioctl(fd, cmd, &io_regs) == -1) {
456                 perror("ISST_IF_IO_CMD");
457                 fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
458                         cpu, reg, write);
459         } else {
460                 if (!write)
461                         *value = io_regs.io_reg[0].value;
462
463                 debug_printf(
464                         "mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
465                         cpu, reg, write, *value);
466         }
467
468         close(fd);
469
470         return 0;
471 }
472
473 int isst_send_mbox_command(unsigned int cpu, unsigned char command,
474                            unsigned char sub_command, unsigned int parameter,
475                            unsigned int req_data, unsigned int *resp)
476 {
477         const char *pathname = "/dev/isst_interface";
478         int fd;
479         struct isst_if_mbox_cmds mbox_cmds = { 0 };
480
481         debug_printf(
482                 "mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
483                 cpu, command, sub_command, parameter, req_data);
484
485         if (isst_platform_info.mmio_supported && command == CONFIG_CLOS) {
486                 unsigned int value;
487                 int write = 0;
488                 int clos_id, core_id, ret = 0;
489
490                 debug_printf("CLOS %d\n", cpu);
491
492                 if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
493                         value = req_data;
494                         write = 1;
495                 }
496
497                 switch (sub_command) {
498                 case CLOS_PQR_ASSOC:
499                         core_id = parameter & 0xff;
500                         ret = isst_send_mmio_command(
501                                 cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
502                                 &value);
503                         if (!ret && !write)
504                                 *resp = value;
505                         break;
506                 case CLOS_PM_CLOS:
507                         clos_id = parameter & 0x03;
508                         ret = isst_send_mmio_command(
509                                 cpu, PM_CLOS_OFFSET + clos_id * 4, write,
510                                 &value);
511                         if (!ret && !write)
512                                 *resp = value;
513                         break;
514                 case CLOS_PM_QOS_CONFIG:
515                         ret = isst_send_mmio_command(cpu, PM_QOS_CONFIG_OFFSET,
516                                                      write, &value);
517                         if (!ret && !write)
518                                 *resp = value;
519                         break;
520                 case CLOS_STATUS:
521                         break;
522                 default:
523                         break;
524                 }
525                 return ret;
526         }
527
528         mbox_cmds.cmd_count = 1;
529         mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
530         mbox_cmds.mbox_cmd[0].command = command;
531         mbox_cmds.mbox_cmd[0].sub_command = sub_command;
532         mbox_cmds.mbox_cmd[0].parameter = parameter;
533         mbox_cmds.mbox_cmd[0].req_data = req_data;
534
535         fd = open(pathname, O_RDWR);
536         if (fd < 0)
537                 err(-1, "%s open failed", pathname);
538
539         if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
540                 perror("ISST_IF_MBOX_COMMAND");
541                 fprintf(outf,
542                         "Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
543                         cpu, command, sub_command, parameter, req_data);
544         } else {
545                 *resp = mbox_cmds.mbox_cmd[0].resp_data;
546                 debug_printf(
547                         "mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
548                         cpu, command, sub_command, parameter, req_data, *resp);
549         }
550
551         close(fd);
552
553         return 0;
554 }
555
556 int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
557                           unsigned long long *req_resp)
558 {
559         struct isst_if_msr_cmds msr_cmds;
560         const char *pathname = "/dev/isst_interface";
561         int fd;
562
563         fd = open(pathname, O_RDWR);
564         if (fd < 0)
565                 err(-1, "%s open failed", pathname);
566
567         msr_cmds.cmd_count = 1;
568         msr_cmds.msr_cmd[0].logical_cpu = cpu;
569         msr_cmds.msr_cmd[0].msr = msr;
570         msr_cmds.msr_cmd[0].read_write = write;
571         if (write)
572                 msr_cmds.msr_cmd[0].data = *req_resp;
573
574         if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
575                 perror("ISST_IF_MSR_COMMAD");
576                 fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
577                         cpu, msr, write);
578         } else {
579                 if (!write)
580                         *req_resp = msr_cmds.msr_cmd[0].data;
581
582                 debug_printf(
583                         "msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
584                         cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
585         }
586
587         close(fd);
588
589         return 0;
590 }
591
592 static int isst_fill_platform_info(void)
593 {
594         const char *pathname = "/dev/isst_interface";
595         int fd;
596
597         fd = open(pathname, O_RDWR);
598         if (fd < 0)
599                 err(-1, "%s open failed", pathname);
600
601         if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
602                 perror("ISST_IF_GET_PLATFORM_INFO");
603                 close(fd);
604                 return -1;
605         }
606
607         close(fd);
608
609         if (isst_platform_info.api_version > supported_api_ver) {
610                 printf("Incompatible API versions; Upgrade of tool is required\n");
611                 return -1;
612         }
613         return 0;
614 }
615
616 static void isst_print_platform_information(void)
617 {
618         struct isst_if_platform_info platform_info;
619         const char *pathname = "/dev/isst_interface";
620         int fd;
621
622         fd = open(pathname, O_RDWR);
623         if (fd < 0)
624                 err(-1, "%s open failed", pathname);
625
626         if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
627                 perror("ISST_IF_GET_PLATFORM_INFO");
628         } else {
629                 fprintf(outf, "Platform: API version : %d\n",
630                         platform_info.api_version);
631                 fprintf(outf, "Platform: Driver version : %d\n",
632                         platform_info.driver_version);
633                 fprintf(outf, "Platform: mbox supported : %d\n",
634                         platform_info.mbox_supported);
635                 fprintf(outf, "Platform: mmio supported : %d\n",
636                         platform_info.mmio_supported);
637         }
638
639         close(fd);
640
641         exit(0);
642 }
643
644 static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
645                                  void *arg4)
646 {
647         int (*fn_ptr)(int cpu, void *arg);
648         int ret;
649
650         fn_ptr = arg1;
651         ret = fn_ptr(cpu, arg2);
652         if (ret)
653                 perror("get_tdp_*");
654         else
655                 isst_display_result(cpu, outf, "perf-profile", (char *)arg3,
656                                     *(unsigned int *)arg4);
657 }
658
659 #define _get_tdp_level(desc, suffix, object, help)                                \
660         static void get_tdp_##object(void)                                        \
661         {                                                                         \
662                 struct isst_pkg_ctdp ctdp;                                        \
663 \
664                 if (cmd_help) {                                                   \
665                         fprintf(stderr,                                           \
666                                 "Print %s [No command arguments are required]\n", \
667                                 help);                                            \
668                         exit(0);                                                  \
669                 }                                                                 \
670                 isst_ctdp_display_information_start(outf);                        \
671                 if (max_target_cpus)                                              \
672                         for_each_online_target_cpu_in_set(                        \
673                                 exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
674                                 &ctdp, desc, &ctdp.object);                       \
675                 else                                                              \
676                         for_each_online_package_in_set(exec_on_get_ctdp_cpu,      \
677                                                        isst_get_ctdp_##suffix,    \
678                                                        &ctdp, desc,               \
679                                                        &ctdp.object);             \
680                 isst_ctdp_display_information_end(outf);                          \
681         }
682
683 _get_tdp_level("get-config-levels", levels, levels, "TDP levels");
684 _get_tdp_level("get-config-version", levels, version, "TDP version");
685 _get_tdp_level("get-config-enabled", levels, enabled, "TDP enable status");
686 _get_tdp_level("get-config-current_level", levels, current_level,
687                "Current TDP Level");
688 _get_tdp_level("get-lock-status", levels, locked, "TDP lock status");
689
690 static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
691                                      void *arg3, void *arg4)
692 {
693         struct isst_pkg_ctdp pkg_dev;
694         int ret;
695
696         memset(&pkg_dev, 0, sizeof(pkg_dev));
697         ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
698         if (ret) {
699                 perror("isst_get_process_ctdp");
700         } else {
701                 isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
702                 isst_get_process_ctdp_complete(cpu, &pkg_dev);
703         }
704 }
705
706 static void dump_isst_config(void)
707 {
708         if (cmd_help) {
709                 fprintf(stderr,
710                         "Print Intel(R) Speed Select Technology Performance profile configuration\n");
711                 fprintf(stderr,
712                         "including base frequency and turbo frequency configurations\n");
713                 fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
714                 fprintf(stderr,
715                         "\tIf no arguments, dump information for all TDP levels\n");
716                 exit(0);
717         }
718
719         isst_ctdp_display_information_start(outf);
720
721         if (max_target_cpus)
722                 for_each_online_target_cpu_in_set(dump_isst_config_for_cpu,
723                                                   NULL, NULL, NULL, NULL);
724         else
725                 for_each_online_package_in_set(dump_isst_config_for_cpu, NULL,
726                                                NULL, NULL, NULL);
727
728         isst_ctdp_display_information_end(outf);
729 }
730
731 static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
732                                   void *arg4)
733 {
734         int ret;
735
736         ret = isst_set_tdp_level(cpu, tdp_level);
737         if (ret)
738                 perror("set_tdp_level_for_cpu");
739         else
740                 isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
741                                     ret);
742 }
743
744 static void set_tdp_level(void)
745 {
746         if (cmd_help) {
747                 fprintf(stderr, "Set Config TDP level\n");
748                 fprintf(stderr,
749                         "\t Arguments: -l|--level : Specify tdp level\n");
750                 exit(0);
751         }
752
753         if (tdp_level == 0xff) {
754                 fprintf(outf, "Invalid command: specify tdp_level\n");
755                 exit(1);
756         }
757         isst_ctdp_display_information_start(outf);
758         if (max_target_cpus)
759                 for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
760                                                   NULL, NULL, NULL);
761         else
762                 for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
763                                                NULL, NULL, NULL);
764         isst_ctdp_display_information_end(outf);
765 }
766
767 static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
768                                     void *arg4)
769 {
770         struct isst_pbf_info pbf_info;
771         int ret;
772
773         ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
774         if (ret) {
775                 perror("isst_get_pbf_info");
776         } else {
777                 isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
778                 isst_get_pbf_info_complete(&pbf_info);
779         }
780 }
781
782 static void dump_pbf_config(void)
783 {
784         if (cmd_help) {
785                 fprintf(stderr,
786                         "Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
787                 fprintf(stderr,
788                         "\tArguments: -l|--level : Specify tdp level\n");
789                 exit(0);
790         }
791
792         if (tdp_level == 0xff) {
793                 fprintf(outf, "Invalid command: specify tdp_level\n");
794                 exit(1);
795         }
796
797         isst_ctdp_display_information_start(outf);
798         if (max_target_cpus)
799                 for_each_online_target_cpu_in_set(dump_pbf_config_for_cpu, NULL,
800                                                   NULL, NULL, NULL);
801         else
802                 for_each_online_package_in_set(dump_pbf_config_for_cpu, NULL,
803                                                NULL, NULL, NULL);
804         isst_ctdp_display_information_end(outf);
805 }
806
807 static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
808                             void *arg4)
809 {
810         int ret;
811         int status = *(int *)arg4;
812
813         ret = isst_set_pbf_fact_status(cpu, 1, status);
814         if (ret) {
815                 perror("isst_set_pbf");
816         } else {
817                 if (status)
818                         isst_display_result(cpu, outf, "base-freq", "enable",
819                                             ret);
820                 else
821                         isst_display_result(cpu, outf, "base-freq", "disable",
822                                             ret);
823         }
824 }
825
826 static void set_pbf_enable(void)
827 {
828         int status = 1;
829
830         if (cmd_help) {
831                 fprintf(stderr,
832                         "Enable Intel Speed Select Technology base frequency feature [No command arguments are required]\n");
833                 exit(0);
834         }
835
836         isst_ctdp_display_information_start(outf);
837         if (max_target_cpus)
838                 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
839                                                   NULL, &status);
840         else
841                 for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
842                                                NULL, &status);
843         isst_ctdp_display_information_end(outf);
844 }
845
846 static void set_pbf_disable(void)
847 {
848         int status = 0;
849
850         if (cmd_help) {
851                 fprintf(stderr,
852                         "Disable Intel Speed Select Technology base frequency feature [No command arguments are required]\n");
853                 exit(0);
854         }
855
856         isst_ctdp_display_information_start(outf);
857         if (max_target_cpus)
858                 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
859                                                   NULL, &status);
860         else
861                 for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
862                                                NULL, &status);
863         isst_ctdp_display_information_end(outf);
864 }
865
866 static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
867                                      void *arg3, void *arg4)
868 {
869         struct isst_fact_info fact_info;
870         int ret;
871
872         ret = isst_get_fact_info(cpu, tdp_level, &fact_info);
873         if (ret)
874                 perror("isst_get_fact_bucket_info");
875         else
876                 isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
877                                               fact_avx, &fact_info);
878 }
879
880 static void dump_fact_config(void)
881 {
882         if (cmd_help) {
883                 fprintf(stderr,
884                         "Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
885                 fprintf(stderr,
886                         "\tArguments: -l|--level : Specify tdp level\n");
887                 fprintf(stderr,
888                         "\tArguments: -b|--bucket : Bucket index to dump\n");
889                 fprintf(stderr,
890                         "\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
891                 exit(0);
892         }
893
894         if (tdp_level == 0xff) {
895                 fprintf(outf, "Invalid command: specify tdp_level\n");
896                 exit(1);
897         }
898
899         isst_ctdp_display_information_start(outf);
900         if (max_target_cpus)
901                 for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
902                                                   NULL, NULL, NULL, NULL);
903         else
904                 for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
905                                                NULL, NULL, NULL);
906         isst_ctdp_display_information_end(outf);
907 }
908
909 static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
910                              void *arg4)
911 {
912         int ret;
913         int status = *(int *)arg4;
914
915         ret = isst_set_pbf_fact_status(cpu, 0, status);
916         if (ret)
917                 perror("isst_set_fact");
918         else {
919                 if (status) {
920                         struct isst_pkg_ctdp pkg_dev;
921
922                         ret = isst_get_ctdp_levels(cpu, &pkg_dev);
923                         if (ret) {
924                                 isst_display_result(cpu, outf, "turbo-freq",
925                                                     "enable", ret);
926                                 return;
927                         }
928                         ret = isst_set_trl(cpu, fact_trl);
929                         isst_display_result(cpu, outf, "turbo-freq", "enable",
930                                             ret);
931                 } else {
932                         /* Since we modified TRL during Fact enable, restore it */
933                         isst_set_trl_from_current_tdp(cpu, fact_trl);
934                         isst_display_result(cpu, outf, "turbo-freq", "disable",
935                                             ret);
936                 }
937         }
938 }
939
940 static void set_fact_enable(void)
941 {
942         int status = 1;
943
944         if (cmd_help) {
945                 fprintf(stderr,
946                         "Enable Intel Speed Select Technology Turbo frequency feature\n");
947                 fprintf(stderr,
948                         "Optional: -t|--trl : Specify turbo ratio limit\n");
949                 exit(0);
950         }
951
952         isst_ctdp_display_information_start(outf);
953         if (max_target_cpus)
954                 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
955                                                   NULL, &status);
956         else
957                 for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
958                                                NULL, &status);
959         isst_ctdp_display_information_end(outf);
960 }
961
962 static void set_fact_disable(void)
963 {
964         int status = 0;
965
966         if (cmd_help) {
967                 fprintf(stderr,
968                         "Disable Intel Speed Select Technology turbo frequency feature\n");
969                 fprintf(stderr,
970                         "Optional: -t|--trl : Specify turbo ratio limit\n");
971                 exit(0);
972         }
973
974         isst_ctdp_display_information_start(outf);
975         if (max_target_cpus)
976                 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
977                                                   NULL, &status);
978         else
979                 for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
980                                                NULL, &status);
981         isst_ctdp_display_information_end(outf);
982 }
983
984 static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
985                                    void *arg4)
986 {
987         int ret;
988         int status = *(int *)arg4;
989
990         ret = isst_pm_qos_config(cpu, status, clos_priority_type);
991         if (ret) {
992                 perror("isst_pm_qos_config");
993         } else {
994                 if (status)
995                         isst_display_result(cpu, outf, "core-power", "enable",
996                                             ret);
997                 else
998                         isst_display_result(cpu, outf, "core-power", "disable",
999                                             ret);
1000         }
1001 }
1002
1003 static void set_clos_enable(void)
1004 {
1005         int status = 1;
1006
1007         if (cmd_help) {
1008                 fprintf(stderr, "Enable core-power for a package/die\n");
1009                 fprintf(stderr,
1010                         "\tClos Enable: Specify priority type with [--priority|-p]\n");
1011                 fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
1012                 exit(0);
1013         }
1014
1015         if (cpufreq_sysfs_present()) {
1016                 fprintf(stderr,
1017                         "cpufreq subsystem and core-power enable will interfere with each other!\n");
1018         }
1019
1020         isst_ctdp_display_information_start(outf);
1021         if (max_target_cpus)
1022                 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1023                                                   NULL, NULL, &status);
1024         else
1025                 for_each_online_package_in_set(enable_clos_qos_config, NULL,
1026                                                NULL, NULL, &status);
1027         isst_ctdp_display_information_end(outf);
1028 }
1029
1030 static void set_clos_disable(void)
1031 {
1032         int status = 0;
1033
1034         if (cmd_help) {
1035                 fprintf(stderr,
1036                         "Disable core-power: [No command arguments are required]\n");
1037                 exit(0);
1038         }
1039
1040         isst_ctdp_display_information_start(outf);
1041         if (max_target_cpus)
1042                 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1043                                                   NULL, NULL, &status);
1044         else
1045                 for_each_online_package_in_set(enable_clos_qos_config, NULL,
1046                                                NULL, NULL, &status);
1047         isst_ctdp_display_information_end(outf);
1048 }
1049
1050 static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
1051                                      void *arg3, void *arg4)
1052 {
1053         struct isst_clos_config clos_config;
1054         int ret;
1055
1056         ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
1057         if (ret)
1058                 perror("isst_pm_get_clos");
1059         else
1060                 isst_clos_display_information(cpu, outf, current_clos,
1061                                               &clos_config);
1062 }
1063
1064 static void dump_clos_config(void)
1065 {
1066         if (cmd_help) {
1067                 fprintf(stderr,
1068                         "Print Intel Speed Select Technology core power configuration\n");
1069                 fprintf(stderr,
1070                         "\tArguments: [-c | --clos]: Specify clos id\n");
1071                 exit(0);
1072         }
1073         if (current_clos < 0 || current_clos > 3) {
1074                 fprintf(stderr, "Invalid clos id\n");
1075                 exit(0);
1076         }
1077
1078         isst_ctdp_display_information_start(outf);
1079         if (max_target_cpus)
1080                 for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
1081                                                   NULL, NULL, NULL, NULL);
1082         else
1083                 for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
1084                                                NULL, NULL, NULL);
1085         isst_ctdp_display_information_end(outf);
1086 }
1087
1088 static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1089                                     void *arg4)
1090 {
1091         struct isst_clos_config clos_config;
1092         int ret;
1093
1094         clos_config.pkg_id = get_physical_package_id(cpu);
1095         clos_config.die_id = get_physical_die_id(cpu);
1096
1097         clos_config.epp = clos_epp;
1098         clos_config.clos_prop_prio = clos_prop_prio;
1099         clos_config.clos_min = clos_min;
1100         clos_config.clos_max = clos_max;
1101         clos_config.clos_desired = clos_desired;
1102         ret = isst_set_clos(cpu, current_clos, &clos_config);
1103         if (ret)
1104                 perror("isst_set_clos");
1105         else
1106                 isst_display_result(cpu, outf, "core-power", "config", ret);
1107 }
1108
1109 static void set_clos_config(void)
1110 {
1111         if (cmd_help) {
1112                 fprintf(stderr,
1113                         "Set core-power configuration for one of the four clos ids\n");
1114                 fprintf(stderr,
1115                         "\tSpecify targeted clos id with [--clos|-c]\n");
1116                 fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
1117                 fprintf(stderr,
1118                         "\tSpecify clos Proportional Priority [--weight|-w]\n");
1119                 fprintf(stderr, "\tSpecify clos min with [--min|-n]\n");
1120                 fprintf(stderr, "\tSpecify clos max with [--max|-m]\n");
1121                 fprintf(stderr, "\tSpecify clos desired with [--desired|-d]\n");
1122                 exit(0);
1123         }
1124
1125         if (current_clos < 0 || current_clos > 3) {
1126                 fprintf(stderr, "Invalid clos id\n");
1127                 exit(0);
1128         }
1129         if (clos_epp < 0 || clos_epp > 0x0F) {
1130                 fprintf(stderr, "clos epp is not specified, default: 0\n");
1131                 clos_epp = 0;
1132         }
1133         if (clos_prop_prio < 0 || clos_prop_prio > 0x0F) {
1134                 fprintf(stderr,
1135                         "clos frequency weight is not specified, default: 0\n");
1136                 clos_prop_prio = 0;
1137         }
1138         if (clos_min < 0) {
1139                 fprintf(stderr, "clos min is not specified, default: 0\n");
1140                 clos_min = 0;
1141         }
1142         if (clos_max < 0) {
1143                 fprintf(stderr, "clos max is not specified, default: 0xff\n");
1144                 clos_max = 0xff;
1145         }
1146         if (clos_desired < 0) {
1147                 fprintf(stderr, "clos desired is not specified, default: 0\n");
1148                 clos_desired = 0x00;
1149         }
1150
1151         isst_ctdp_display_information_start(outf);
1152         if (max_target_cpus)
1153                 for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
1154                                                   NULL, NULL, NULL);
1155         else
1156                 for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
1157                                                NULL, NULL, NULL);
1158         isst_ctdp_display_information_end(outf);
1159 }
1160
1161 static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1162                                    void *arg4)
1163 {
1164         int ret;
1165
1166         ret = isst_clos_associate(cpu, current_clos);
1167         if (ret)
1168                 perror("isst_clos_associate");
1169         else
1170                 isst_display_result(cpu, outf, "core-power", "assoc", ret);
1171 }
1172
1173 static void set_clos_assoc(void)
1174 {
1175         if (cmd_help) {
1176                 fprintf(stderr, "Associate a clos id to a CPU\n");
1177                 fprintf(stderr,
1178                         "\tSpecify targeted clos id with [--clos|-c]\n");
1179                 exit(0);
1180         }
1181
1182         if (current_clos < 0 || current_clos > 3) {
1183                 fprintf(stderr, "Invalid clos id\n");
1184                 exit(0);
1185         }
1186         if (max_target_cpus)
1187                 for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
1188                                                   NULL, NULL, NULL);
1189         else {
1190                 fprintf(stderr,
1191                         "Invalid target cpu. Specify with [-c|--cpu]\n");
1192         }
1193 }
1194
1195 static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1196                                    void *arg4)
1197 {
1198         int clos, ret;
1199
1200         ret = isst_clos_get_assoc_status(cpu, &clos);
1201         if (ret)
1202                 perror("isst_clos_get_assoc_status");
1203         else
1204                 isst_display_result(cpu, outf, "core-power", "get-assoc", clos);
1205 }
1206
1207 static void get_clos_assoc(void)
1208 {
1209         if (cmd_help) {
1210                 fprintf(stderr, "Get associate clos id to a CPU\n");
1211                 fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
1212                 exit(0);
1213         }
1214         if (max_target_cpus)
1215                 for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
1216                                                   NULL, NULL, NULL);
1217         else {
1218                 fprintf(stderr,
1219                         "Invalid target cpu. Specify with [-c|--cpu]\n");
1220         }
1221 }
1222
1223 static struct process_cmd_struct isst_cmds[] = {
1224         { "perf-profile", "get-lock-status", get_tdp_locked },
1225         { "perf-profile", "get-config-levels", get_tdp_levels },
1226         { "perf-profile", "get-config-version", get_tdp_version },
1227         { "perf-profile", "get-config-enabled", get_tdp_enabled },
1228         { "perf-profile", "get-config-current-level", get_tdp_current_level },
1229         { "perf-profile", "set-config-level", set_tdp_level },
1230         { "perf-profile", "info", dump_isst_config },
1231         { "base-freq", "info", dump_pbf_config },
1232         { "base-freq", "enable", set_pbf_enable },
1233         { "base-freq", "disable", set_pbf_disable },
1234         { "turbo-freq", "info", dump_fact_config },
1235         { "turbo-freq", "enable", set_fact_enable },
1236         { "turbo-freq", "disable", set_fact_disable },
1237         { "core-power", "info", dump_clos_config },
1238         { "core-power", "enable", set_clos_enable },
1239         { "core-power", "disable", set_clos_disable },
1240         { "core-power", "config", set_clos_config },
1241         { "core-power", "assoc", set_clos_assoc },
1242         { "core-power", "get-assoc", get_clos_assoc },
1243         { NULL, NULL, NULL }
1244 };
1245
1246 /*
1247  * parse cpuset with following syntax
1248  * 1,2,4..6,8-10 and set bits in cpu_subset
1249  */
1250 void parse_cpu_command(char *optarg)
1251 {
1252         unsigned int start, end;
1253         char *next;
1254
1255         next = optarg;
1256
1257         while (next && *next) {
1258                 if (*next == '-') /* no negative cpu numbers */
1259                         goto error;
1260
1261                 start = strtoul(next, &next, 10);
1262
1263                 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
1264                         target_cpus[max_target_cpus++] = start;
1265
1266                 if (*next == '\0')
1267                         break;
1268
1269                 if (*next == ',') {
1270                         next += 1;
1271                         continue;
1272                 }
1273
1274                 if (*next == '-') {
1275                         next += 1; /* start range */
1276                 } else if (*next == '.') {
1277                         next += 1;
1278                         if (*next == '.')
1279                                 next += 1; /* start range */
1280                         else
1281                                 goto error;
1282                 }
1283
1284                 end = strtoul(next, &next, 10);
1285                 if (end <= start)
1286                         goto error;
1287
1288                 while (++start <= end) {
1289                         if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
1290                                 target_cpus[max_target_cpus++] = start;
1291                 }
1292
1293                 if (*next == ',')
1294                         next += 1;
1295                 else if (*next != '\0')
1296                         goto error;
1297         }
1298
1299 #ifdef DEBUG
1300         {
1301                 int i;
1302
1303                 for (i = 0; i < max_target_cpus; ++i)
1304                         printf("cpu [%d] in arg\n", target_cpus[i]);
1305         }
1306 #endif
1307         return;
1308
1309 error:
1310         fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
1311         exit(-1);
1312 }
1313
1314 static void parse_cmd_args(int argc, int start, char **argv)
1315 {
1316         int opt;
1317         int option_index;
1318
1319         static struct option long_options[] = {
1320                 { "bucket", required_argument, 0, 'b' },
1321                 { "level", required_argument, 0, 'l' },
1322                 { "trl-type", required_argument, 0, 'r' },
1323                 { "trl", required_argument, 0, 't' },
1324                 { "help", no_argument, 0, 'h' },
1325                 { "clos", required_argument, 0, 'c' },
1326                 { "desired", required_argument, 0, 'd' },
1327                 { "epp", required_argument, 0, 'e' },
1328                 { "min", required_argument, 0, 'n' },
1329                 { "max", required_argument, 0, 'm' },
1330                 { "priority", required_argument, 0, 'p' },
1331                 { "weight", required_argument, 0, 'w' },
1332                 { 0, 0, 0, 0 }
1333         };
1334
1335         option_index = start;
1336
1337         optind = start + 1;
1338         while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:h",
1339                                   long_options, &option_index)) != -1) {
1340                 switch (opt) {
1341                 case 'b':
1342                         fact_bucket = atoi(optarg);
1343                         break;
1344                 case 'h':
1345                         cmd_help = 1;
1346                         break;
1347                 case 'l':
1348                         tdp_level = atoi(optarg);
1349                         break;
1350                 case 't':
1351                         sscanf(optarg, "0x%llx", &fact_trl);
1352                         break;
1353                 case 'r':
1354                         if (!strncmp(optarg, "sse", 3)) {
1355                                 fact_avx = 0x01;
1356                         } else if (!strncmp(optarg, "avx2", 4)) {
1357                                 fact_avx = 0x02;
1358                         } else if (!strncmp(optarg, "avx512", 4)) {
1359                                 fact_avx = 0x04;
1360                         } else {
1361                                 fprintf(outf, "Invalid sse,avx options\n");
1362                                 exit(1);
1363                         }
1364                         break;
1365                 /* CLOS related */
1366                 case 'c':
1367                         current_clos = atoi(optarg);
1368                         printf("clos %d\n", current_clos);
1369                         break;
1370                 case 'd':
1371                         clos_desired = atoi(optarg);
1372                         break;
1373                 case 'e':
1374                         clos_epp = atoi(optarg);
1375                         break;
1376                 case 'n':
1377                         clos_min = atoi(optarg);
1378                         break;
1379                 case 'm':
1380                         clos_max = atoi(optarg);
1381                         break;
1382                 case 'p':
1383                         clos_priority_type = atoi(optarg);
1384                         break;
1385                 case 'w':
1386                         clos_prop_prio = atoi(optarg);
1387                         break;
1388                 default:
1389                         printf("no match\n");
1390                 }
1391         }
1392 }
1393
1394 static void isst_help(void)
1395 {
1396         printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
1397                 performance profiles per system via static and/or dynamic\n\
1398                 adjustment of core count, workload, Tjmax, and\n\
1399                 TDP, etc.\n");
1400         printf("\nCommands : For feature=perf-profile\n");
1401         printf("\tinfo\n");
1402         printf("\tget-lock-status\n");
1403         printf("\tget-config-levels\n");
1404         printf("\tget-config-version\n");
1405         printf("\tget-config-enabled\n");
1406         printf("\tget-config-current-level\n");
1407         printf("\tset-config-level\n");
1408 }
1409
1410 static void pbf_help(void)
1411 {
1412         printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
1413                 on certain cores (high priority cores) in exchange for lower\n\
1414                 base frequency on remaining cores (low priority cores).\n");
1415         printf("\tcommand : info\n");
1416         printf("\tcommand : enable\n");
1417         printf("\tcommand : disable\n");
1418 }
1419
1420 static void fact_help(void)
1421 {
1422         printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
1423                 limits to cores based on priority.\n");
1424         printf("\nCommand: For feature=turbo-freq\n");
1425         printf("\tcommand : info\n");
1426         printf("\tcommand : enable\n");
1427         printf("\tcommand : disable\n");
1428 }
1429
1430 static void core_power_help(void)
1431 {
1432         printf("core-power:\tInterface that allows user to define per core/tile\n\
1433                 priority.\n");
1434         printf("\nCommands : For feature=core-power\n");
1435         printf("\tinfo\n");
1436         printf("\tenable\n");
1437         printf("\tdisable\n");
1438         printf("\tconfig\n");
1439         printf("\tassoc\n");
1440         printf("\tget-assoc\n");
1441 }
1442
1443 struct process_cmd_help_struct {
1444         char *feature;
1445         void (*process_fn)(void);
1446 };
1447
1448 static struct process_cmd_help_struct isst_help_cmds[] = {
1449         { "perf-profile", isst_help },
1450         { "base-freq", pbf_help },
1451         { "turbo-freq", fact_help },
1452         { "core-power", core_power_help },
1453         { NULL, NULL }
1454 };
1455
1456 void process_command(int argc, char **argv)
1457 {
1458         int i = 0, matched = 0;
1459         char *feature = argv[optind];
1460         char *cmd = argv[optind + 1];
1461
1462         if (!feature || !cmd)
1463                 return;
1464
1465         debug_printf("feature name [%s] command [%s]\n", feature, cmd);
1466         if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
1467                 while (isst_help_cmds[i].feature) {
1468                         if (!strcmp(isst_help_cmds[i].feature, feature)) {
1469                                 isst_help_cmds[i].process_fn();
1470                                 exit(0);
1471                         }
1472                         ++i;
1473                 }
1474         }
1475
1476         create_cpu_map();
1477
1478         i = 0;
1479         while (isst_cmds[i].feature) {
1480                 if (!strcmp(isst_cmds[i].feature, feature) &&
1481                     !strcmp(isst_cmds[i].command, cmd)) {
1482                         parse_cmd_args(argc, optind + 1, argv);
1483                         isst_cmds[i].process_fn();
1484                         matched = 1;
1485                         break;
1486                 }
1487                 ++i;
1488         }
1489
1490         if (!matched)
1491                 fprintf(stderr, "Invalid command\n");
1492 }
1493
1494 static void usage(void)
1495 {
1496         printf("Intel(R) Speed Select Technology\n");
1497         printf("\nUsage:\n");
1498         printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
1499         printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features,\n");
1500         printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power]\n");
1501         printf("\nFor help on each feature, use --h|--help\n");
1502         printf("\tFor example:  intel-speed-select perf-profile -h\n");
1503
1504         printf("\nFor additional help on each command for a feature, use --h|--help\n");
1505         printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
1506         printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
1507
1508         printf("\nOPTIONS\n");
1509         printf("\t[-c|--cpu] : logical cpu number\n");
1510         printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
1511         printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
1512         printf("\t[-d|--debug] : Debug mode\n");
1513         printf("\t[-h|--help] : Print help\n");
1514         printf("\t[-i|--info] : Print platform information\n");
1515         printf("\t[-o|--out] : Output file\n");
1516         printf("\t\t\tDefault : stderr\n");
1517         printf("\t[-f|--format] : output format [json|text]. Default: text\n");
1518         printf("\t[-v|--version] : Print version\n");
1519
1520         printf("\nResult format\n");
1521         printf("\tResult display uses a common format for each command:\n");
1522         printf("\tResults are formatted in text/JSON with\n");
1523         printf("\t\tPackage, Die, CPU, and command specific results.\n");
1524         printf("\t\t\tFor Set commands, status is 0 for success and rest for failures\n");
1525         exit(1);
1526 }
1527
1528 static void print_version(void)
1529 {
1530         fprintf(outf, "Version %s\n", version_str);
1531         fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
1532         exit(0);
1533 }
1534
1535 static void cmdline(int argc, char **argv)
1536 {
1537         int opt;
1538         int option_index = 0;
1539         int ret;
1540
1541         static struct option long_options[] = {
1542                 { "cpu", required_argument, 0, 'c' },
1543                 { "debug", no_argument, 0, 'd' },
1544                 { "format", required_argument, 0, 'f' },
1545                 { "help", no_argument, 0, 'h' },
1546                 { "info", no_argument, 0, 'i' },
1547                 { "out", required_argument, 0, 'o' },
1548                 { "version", no_argument, 0, 'v' },
1549                 { 0, 0, 0, 0 }
1550         };
1551
1552         progname = argv[0];
1553         while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v", long_options,
1554                                        &option_index)) != -1) {
1555                 switch (opt) {
1556                 case 'c':
1557                         parse_cpu_command(optarg);
1558                         break;
1559                 case 'd':
1560                         debug_flag = 1;
1561                         printf("Debug Mode ON\n");
1562                         break;
1563                 case 'f':
1564                         if (!strncmp(optarg, "json", 4))
1565                                 out_format_json = 1;
1566                         break;
1567                 case 'h':
1568                         usage();
1569                         break;
1570                 case 'i':
1571                         isst_print_platform_information();
1572                         break;
1573                 case 'o':
1574                         if (outf)
1575                                 fclose(outf);
1576                         outf = fopen_or_exit(optarg, "w");
1577                         break;
1578                 case 'v':
1579                         print_version();
1580                         break;
1581                 default:
1582                         usage();
1583                 }
1584         }
1585
1586         if (geteuid() != 0) {
1587                 fprintf(stderr, "Must run as root\n");
1588                 exit(0);
1589         }
1590
1591         if (optind > (argc - 2)) {
1592                 fprintf(stderr, "Feature name and|or command not specified\n");
1593                 exit(0);
1594         }
1595         update_cpu_model();
1596         printf("Intel(R) Speed Select Technology\n");
1597         printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
1598         set_max_cpu_num();
1599         set_cpu_present_cpu_mask();
1600         set_cpu_target_cpu_mask();
1601         ret = isst_fill_platform_info();
1602         if (ret)
1603                 goto out;
1604
1605         process_command(argc, argv);
1606 out:
1607         free_cpu_set(present_cpumask);
1608         free_cpu_set(target_cpumask);
1609 }
1610
1611 int main(int argc, char **argv)
1612 {
1613         outf = stderr;
1614         cmdline(argc, argv);
1615         return 0;
1616 }