tools/power/x86/intel-speed-select: Improve output of perf-profile commands
[linux-2.6-microblaze.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)(int arg);
15         int arg;
16 };
17
18 static const char *version_str = "v1.2";
19 static const int supported_api_ver = 1;
20 static struct isst_if_platform_info isst_platform_info;
21 static char *progname;
22 static int debug_flag;
23 static FILE *outf;
24
25 static int cpu_model;
26 static int cpu_stepping;
27
28 #define MAX_CPUS_IN_ONE_REQ 64
29 static short max_target_cpus;
30 static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
31
32 static int topo_max_cpus;
33 static size_t present_cpumask_size;
34 static cpu_set_t *present_cpumask;
35 static size_t target_cpumask_size;
36 static cpu_set_t *target_cpumask;
37 static int tdp_level = 0xFF;
38 static int fact_bucket = 0xFF;
39 static int fact_avx = 0xFF;
40 static unsigned long long fact_trl;
41 static int out_format_json;
42 static int cmd_help;
43 static int force_online_offline;
44 static int auto_mode;
45 static int fact_enable_fail;
46
47 /* clos related */
48 static int current_clos = -1;
49 static int clos_epp = -1;
50 static int clos_prop_prio = -1;
51 static int clos_min = -1;
52 static int clos_max = -1;
53 static int clos_desired = -1;
54 static int clos_priority_type;
55
56 struct _cpu_map {
57         unsigned short core_id;
58         unsigned short pkg_id;
59         unsigned short die_id;
60         unsigned short punit_cpu;
61         unsigned short punit_cpu_core;
62 };
63 struct _cpu_map *cpu_map;
64
65 struct cpu_topology {
66         short cpu;
67         short core_id;
68         short pkg_id;
69         short die_id;
70 };
71
72 FILE *get_output_file(void)
73 {
74         return outf;
75 }
76
77 void debug_printf(const char *format, ...)
78 {
79         va_list args;
80
81         va_start(args, format);
82
83         if (debug_flag)
84                 vprintf(format, args);
85
86         va_end(args);
87 }
88
89
90 int is_clx_n_platform(void)
91 {
92         if (cpu_model == 0x55)
93                 if (cpu_stepping == 0x6 || cpu_stepping == 0x7)
94                         return 1;
95         return 0;
96 }
97
98 static int update_cpu_model(void)
99 {
100         unsigned int ebx, ecx, edx;
101         unsigned int fms, family;
102
103         __cpuid(1, fms, ebx, ecx, edx);
104         family = (fms >> 8) & 0xf;
105         cpu_model = (fms >> 4) & 0xf;
106         if (family == 6 || family == 0xf)
107                 cpu_model += ((fms >> 16) & 0xf) << 4;
108
109         cpu_stepping = fms & 0xf;
110         /* only three CascadeLake-N models are supported */
111         if (is_clx_n_platform()) {
112                 FILE *fp;
113                 size_t n = 0;
114                 char *line = NULL;
115                 int ret = 1;
116
117                 fp = fopen("/proc/cpuinfo", "r");
118                 if (!fp)
119                         err(-1, "cannot open /proc/cpuinfo\n");
120
121                 while (getline(&line, &n, fp) > 0) {
122                         if (strstr(line, "model name")) {
123                                 if (strstr(line, "6252N") ||
124                                     strstr(line, "6230N") ||
125                                     strstr(line, "5218N"))
126                                         ret = 0;
127                                 break;
128                         }
129                 }
130                 free(line);
131                 fclose(fp);
132                 return ret;
133         }
134         return 0;
135 }
136
137 /* Open a file, and exit on failure */
138 static FILE *fopen_or_exit(const char *path, const char *mode)
139 {
140         FILE *filep = fopen(path, mode);
141
142         if (!filep)
143                 err(1, "%s: open failed", path);
144
145         return filep;
146 }
147
148 /* Parse a file containing a single int */
149 static int parse_int_file(int fatal, const char *fmt, ...)
150 {
151         va_list args;
152         char path[PATH_MAX];
153         FILE *filep;
154         int value;
155
156         va_start(args, fmt);
157         vsnprintf(path, sizeof(path), fmt, args);
158         va_end(args);
159         if (fatal) {
160                 filep = fopen_or_exit(path, "r");
161         } else {
162                 filep = fopen(path, "r");
163                 if (!filep)
164                         return -1;
165         }
166         if (fscanf(filep, "%d", &value) != 1)
167                 err(1, "%s: failed to parse number from file", path);
168         fclose(filep);
169
170         return value;
171 }
172
173 int cpufreq_sysfs_present(void)
174 {
175         DIR *dir;
176
177         dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
178         if (dir) {
179                 closedir(dir);
180                 return 1;
181         }
182
183         return 0;
184 }
185
186 int out_format_is_json(void)
187 {
188         return out_format_json;
189 }
190
191 static int get_stored_topology_info(int cpu, int *core_id, int *pkg_id, int *die_id)
192 {
193         const char *pathname = "/tmp/isst_cpu_topology.dat";
194         struct cpu_topology cpu_top;
195         FILE *fp;
196         int ret;
197
198         fp = fopen(pathname, "rb");
199         if (!fp)
200                 return -1;
201
202         ret = fseek(fp, cpu * sizeof(cpu_top), SEEK_SET);
203         if (ret)
204                 goto err_ret;
205
206         ret = fread(&cpu_top, sizeof(cpu_top), 1, fp);
207         if (ret != 1) {
208                 ret = -1;
209                 goto err_ret;
210         }
211
212         *pkg_id = cpu_top.pkg_id;
213         *core_id = cpu_top.core_id;
214         *die_id = cpu_top.die_id;
215         ret = 0;
216
217 err_ret:
218         fclose(fp);
219
220         return ret;
221 }
222
223 static void store_cpu_topology(void)
224 {
225         const char *pathname = "/tmp/isst_cpu_topology.dat";
226         FILE *fp;
227         int i;
228
229         fp = fopen(pathname, "rb");
230         if (fp) {
231                 /* Mapping already exists */
232                 fclose(fp);
233                 return;
234         }
235
236         fp = fopen(pathname, "wb");
237         if (!fp) {
238                 fprintf(stderr, "Can't create file:%s\n", pathname);
239                 return;
240         }
241
242         for (i = 0; i < topo_max_cpus; ++i) {
243                 struct cpu_topology cpu_top;
244
245                 cpu_top.core_id = parse_int_file(0,
246                         "/sys/devices/system/cpu/cpu%d/topology/core_id", i);
247                 if (cpu_top.core_id < 0)
248                         cpu_top.core_id = -1;
249
250                 cpu_top.pkg_id = parse_int_file(0,
251                         "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
252                 if (cpu_top.pkg_id < 0)
253                         cpu_top.pkg_id = -1;
254
255                 cpu_top.die_id = parse_int_file(0,
256                         "/sys/devices/system/cpu/cpu%d/topology/die_id", i);
257                 if (cpu_top.die_id < 0)
258                         cpu_top.die_id = -1;
259
260                 cpu_top.cpu = i;
261
262                 if (fwrite(&cpu_top, sizeof(cpu_top), 1, fp) != 1) {
263                         fprintf(stderr, "Can't write to:%s\n", pathname);
264                         break;
265                 }
266         }
267
268         fclose(fp);
269 }
270
271 int get_physical_package_id(int cpu)
272 {
273         int ret;
274
275         ret = parse_int_file(0,
276                         "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
277                         cpu);
278         if (ret < 0) {
279                 int core_id, pkg_id, die_id;
280
281                 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
282                 if (!ret)
283                         return pkg_id;
284         }
285
286         return ret;
287 }
288
289 int get_physical_core_id(int cpu)
290 {
291         int ret;
292
293         ret = parse_int_file(0,
294                         "/sys/devices/system/cpu/cpu%d/topology/core_id",
295                         cpu);
296         if (ret < 0) {
297                 int core_id, pkg_id, die_id;
298
299                 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
300                 if (!ret)
301                         return core_id;
302         }
303
304         return ret;
305 }
306
307 int get_physical_die_id(int cpu)
308 {
309         int ret;
310
311         ret = parse_int_file(0,
312                         "/sys/devices/system/cpu/cpu%d/topology/die_id",
313                         cpu);
314         if (ret < 0) {
315                 int core_id, pkg_id, die_id;
316
317                 ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
318                 if (!ret)
319                         return die_id;
320         }
321
322         if (ret < 0)
323                 ret = 0;
324
325         return ret;
326 }
327
328 int get_cpufreq_base_freq(int cpu)
329 {
330         return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
331 }
332
333 int get_topo_max_cpus(void)
334 {
335         return topo_max_cpus;
336 }
337
338 static void set_cpu_online_offline(int cpu, int state)
339 {
340         char buffer[128];
341         int fd, ret;
342
343         snprintf(buffer, sizeof(buffer),
344                  "/sys/devices/system/cpu/cpu%d/online", cpu);
345
346         fd = open(buffer, O_WRONLY);
347         if (fd < 0) {
348                 if (!cpu && state) {
349                         fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
350                         fprintf(stderr, "Ignoring online request for CPU 0 as this is already online\n");
351                         return;
352                 }
353                 err(-1, "%s open failed", buffer);
354         }
355
356         if (state)
357                 ret = write(fd, "1\n", 2);
358         else
359                 ret = write(fd, "0\n", 2);
360
361         if (ret == -1)
362                 perror("Online/Offline: Operation failed\n");
363
364         close(fd);
365 }
366
367 #define MAX_PACKAGE_COUNT 8
368 #define MAX_DIE_PER_PACKAGE 2
369 static void for_each_online_package_in_set(void (*callback)(int, void *, void *,
370                                                             void *, void *),
371                                            void *arg1, void *arg2, void *arg3,
372                                            void *arg4)
373 {
374         int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
375         int pkg_index = 0, i;
376
377         memset(max_packages, 0xff, sizeof(max_packages));
378         for (i = 0; i < topo_max_cpus; ++i) {
379                 int j, online, pkg_id, die_id = 0, skip = 0;
380
381                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
382                         continue;
383                 if (i)
384                         online = parse_int_file(
385                                 1, "/sys/devices/system/cpu/cpu%d/online", i);
386                 else
387                         online =
388                                 1; /* online entry for CPU 0 needs some special configs */
389
390                 die_id = get_physical_die_id(i);
391                 if (die_id < 0)
392                         die_id = 0;
393
394                 pkg_id = parse_int_file(0,
395                         "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
396                 if (pkg_id < 0)
397                         continue;
398
399                 /* Create an unique id for package, die combination to store */
400                 pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
401
402                 for (j = 0; j < pkg_index; ++j) {
403                         if (max_packages[j] == pkg_id) {
404                                 skip = 1;
405                                 break;
406                         }
407                 }
408
409                 if (!skip && online && callback) {
410                         callback(i, arg1, arg2, arg3, arg4);
411                         max_packages[pkg_index++] = pkg_id;
412                 }
413         }
414 }
415
416 static void for_each_online_target_cpu_in_set(
417         void (*callback)(int, void *, void *, void *, void *), void *arg1,
418         void *arg2, void *arg3, void *arg4)
419 {
420         int i;
421
422         for (i = 0; i < topo_max_cpus; ++i) {
423                 int online;
424
425                 if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
426                         continue;
427                 if (i)
428                         online = parse_int_file(
429                                 1, "/sys/devices/system/cpu/cpu%d/online", i);
430                 else
431                         online =
432                                 1; /* online entry for CPU 0 needs some special configs */
433
434                 if (online && callback)
435                         callback(i, arg1, arg2, arg3, arg4);
436         }
437 }
438
439 #define BITMASK_SIZE 32
440 static void set_max_cpu_num(void)
441 {
442         FILE *filep;
443         unsigned long dummy;
444         int i;
445
446         topo_max_cpus = 0;
447         for (i = 0; i < 256; ++i) {
448                 char path[256];
449
450                 snprintf(path, sizeof(path),
451                          "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
452                 filep = fopen(path, "r");
453                 if (filep)
454                         break;
455         }
456
457         if (!filep) {
458                 fprintf(stderr, "Can't get max cpu number\n");
459                 exit(0);
460         }
461
462         while (fscanf(filep, "%lx,", &dummy) == 1)
463                 topo_max_cpus += BITMASK_SIZE;
464         fclose(filep);
465
466         debug_printf("max cpus %d\n", topo_max_cpus);
467 }
468
469 size_t alloc_cpu_set(cpu_set_t **cpu_set)
470 {
471         cpu_set_t *_cpu_set;
472         size_t size;
473
474         _cpu_set = CPU_ALLOC((topo_max_cpus + 1));
475         if (_cpu_set == NULL)
476                 err(3, "CPU_ALLOC");
477         size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
478         CPU_ZERO_S(size, _cpu_set);
479
480         *cpu_set = _cpu_set;
481         return size;
482 }
483
484 void free_cpu_set(cpu_set_t *cpu_set)
485 {
486         CPU_FREE(cpu_set);
487 }
488
489 static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
490 static long long core_mask[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
491 static void set_cpu_present_cpu_mask(void)
492 {
493         size_t size;
494         DIR *dir;
495         int i;
496
497         size = alloc_cpu_set(&present_cpumask);
498         present_cpumask_size = size;
499         for (i = 0; i < topo_max_cpus; ++i) {
500                 char buffer[256];
501
502                 snprintf(buffer, sizeof(buffer),
503                          "/sys/devices/system/cpu/cpu%d", i);
504                 dir = opendir(buffer);
505                 if (dir) {
506                         int pkg_id, die_id;
507
508                         CPU_SET_S(i, size, present_cpumask);
509                         die_id = get_physical_die_id(i);
510                         if (die_id < 0)
511                                 die_id = 0;
512
513                         pkg_id = get_physical_package_id(i);
514                         if (pkg_id < 0) {
515                                 fprintf(stderr, "Failed to get package id, CPU %d may be offline\n", i);
516                                 continue;
517                         }
518                         if (pkg_id < MAX_PACKAGE_COUNT &&
519                             die_id < MAX_DIE_PER_PACKAGE) {
520                                 int core_id = get_physical_core_id(i);
521
522                                 cpu_cnt[pkg_id][die_id]++;
523                                 core_mask[pkg_id][die_id] |= (1ULL << core_id);
524                         }
525                 }
526                 closedir(dir);
527         }
528 }
529
530 int get_core_count(int pkg_id, int die_id)
531 {
532         int cnt = 0;
533
534         if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE) {
535                 int i;
536
537                 for (i = 0; i < sizeof(long long) * 8; ++i) {
538                         if (core_mask[pkg_id][die_id] & (1ULL << i))
539                                 cnt++;
540                 }
541         }
542
543         return cnt;
544 }
545
546 int get_cpu_count(int pkg_id, int die_id)
547 {
548         if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
549                 return cpu_cnt[pkg_id][die_id];
550
551         return 0;
552 }
553
554 static void set_cpu_target_cpu_mask(void)
555 {
556         size_t size;
557         int i;
558
559         size = alloc_cpu_set(&target_cpumask);
560         target_cpumask_size = size;
561         for (i = 0; i < max_target_cpus; ++i) {
562                 if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
563                                  present_cpumask))
564                         continue;
565
566                 CPU_SET_S(target_cpus[i], size, target_cpumask);
567         }
568 }
569
570 static void create_cpu_map(void)
571 {
572         const char *pathname = "/dev/isst_interface";
573         int i, fd = 0;
574         struct isst_if_cpu_maps map;
575
576         cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
577         if (!cpu_map)
578                 err(3, "cpumap");
579
580         fd = open(pathname, O_RDWR);
581         if (fd < 0)
582                 err(-1, "%s open failed", pathname);
583
584         for (i = 0; i < topo_max_cpus; ++i) {
585                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
586                         continue;
587
588                 map.cmd_count = 1;
589                 map.cpu_map[0].logical_cpu = i;
590
591                 debug_printf(" map logical_cpu:%d\n",
592                              map.cpu_map[0].logical_cpu);
593                 if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
594                         perror("ISST_IF_GET_PHY_ID");
595                         fprintf(outf, "Error: map logical_cpu:%d\n",
596                                 map.cpu_map[0].logical_cpu);
597                         continue;
598                 }
599                 cpu_map[i].core_id = get_physical_core_id(i);
600                 cpu_map[i].pkg_id = get_physical_package_id(i);
601                 cpu_map[i].die_id = get_physical_die_id(i);
602                 cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
603                 cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
604                                              1); // shift to get core id
605
606                 debug_printf(
607                         "map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
608                         i, cpu_map[i].core_id, cpu_map[i].die_id,
609                         cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
610                         cpu_map[i].punit_cpu_core);
611         }
612
613         if (fd)
614                 close(fd);
615 }
616
617 int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
618 {
619         int i;
620
621         for (i = 0; i < topo_max_cpus; ++i) {
622                 if (cpu_map[i].pkg_id == pkg_id &&
623                     cpu_map[i].die_id == die_id &&
624                     cpu_map[i].punit_cpu_core == punit_core_id)
625                         return i;
626         }
627
628         return -EINVAL;
629 }
630
631 void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
632                                       size_t core_cpumask_size,
633                                       cpu_set_t *core_cpumask, int *cpu_cnt)
634 {
635         int i, cnt = 0;
636         int die_id, pkg_id;
637
638         *cpu_cnt = 0;
639         die_id = get_physical_die_id(cpu);
640         pkg_id = get_physical_package_id(cpu);
641
642         for (i = 0; i < 64; ++i) {
643                 if (core_mask & BIT(i)) {
644                         int j;
645
646                         for (j = 0; j < topo_max_cpus; ++j) {
647                                 if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
648                                         continue;
649
650                                 if (cpu_map[j].pkg_id == pkg_id &&
651                                     cpu_map[j].die_id == die_id &&
652                                     cpu_map[j].punit_cpu_core == i) {
653                                         CPU_SET_S(j, core_cpumask_size,
654                                                   core_cpumask);
655                                         ++cnt;
656                                 }
657                         }
658                 }
659         }
660
661         *cpu_cnt = cnt;
662 }
663
664 int find_phy_core_num(int logical_cpu)
665 {
666         if (logical_cpu < topo_max_cpus)
667                 return cpu_map[logical_cpu].punit_cpu_core;
668
669         return -EINVAL;
670 }
671
672 static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
673                                   unsigned int *value)
674 {
675         struct isst_if_io_regs io_regs;
676         const char *pathname = "/dev/isst_interface";
677         int cmd;
678         int fd;
679
680         debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
681
682         fd = open(pathname, O_RDWR);
683         if (fd < 0)
684                 err(-1, "%s open failed", pathname);
685
686         io_regs.req_count = 1;
687         io_regs.io_reg[0].logical_cpu = cpu;
688         io_regs.io_reg[0].reg = reg;
689         cmd = ISST_IF_IO_CMD;
690         if (write) {
691                 io_regs.io_reg[0].read_write = 1;
692                 io_regs.io_reg[0].value = *value;
693         } else {
694                 io_regs.io_reg[0].read_write = 0;
695         }
696
697         if (ioctl(fd, cmd, &io_regs) == -1) {
698                 perror("ISST_IF_IO_CMD");
699                 fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
700                         cpu, reg, write);
701         } else {
702                 if (!write)
703                         *value = io_regs.io_reg[0].value;
704
705                 debug_printf(
706                         "mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
707                         cpu, reg, write, *value);
708         }
709
710         close(fd);
711
712         return 0;
713 }
714
715 int isst_send_mbox_command(unsigned int cpu, unsigned char command,
716                            unsigned char sub_command, unsigned int parameter,
717                            unsigned int req_data, unsigned int *resp)
718 {
719         const char *pathname = "/dev/isst_interface";
720         int fd;
721         struct isst_if_mbox_cmds mbox_cmds = { 0 };
722
723         debug_printf(
724                 "mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
725                 cpu, command, sub_command, parameter, req_data);
726
727         if (isst_platform_info.mmio_supported && command == CONFIG_CLOS &&
728             sub_command != CLOS_PM_QOS_CONFIG) {
729                 unsigned int value;
730                 int write = 0;
731                 int clos_id, core_id, ret = 0;
732
733                 debug_printf("CPU %d\n", cpu);
734
735                 if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
736                         value = req_data;
737                         write = 1;
738                 }
739
740                 switch (sub_command) {
741                 case CLOS_PQR_ASSOC:
742                         core_id = parameter & 0xff;
743                         ret = isst_send_mmio_command(
744                                 cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
745                                 &value);
746                         if (!ret && !write)
747                                 *resp = value;
748                         break;
749                 case CLOS_PM_CLOS:
750                         clos_id = parameter & 0x03;
751                         ret = isst_send_mmio_command(
752                                 cpu, PM_CLOS_OFFSET + clos_id * 4, write,
753                                 &value);
754                         if (!ret && !write)
755                                 *resp = value;
756                         break;
757                 case CLOS_STATUS:
758                         break;
759                 default:
760                         break;
761                 }
762                 return ret;
763         }
764
765         mbox_cmds.cmd_count = 1;
766         mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
767         mbox_cmds.mbox_cmd[0].command = command;
768         mbox_cmds.mbox_cmd[0].sub_command = sub_command;
769         mbox_cmds.mbox_cmd[0].parameter = parameter;
770         mbox_cmds.mbox_cmd[0].req_data = req_data;
771
772         fd = open(pathname, O_RDWR);
773         if (fd < 0)
774                 err(-1, "%s open failed", pathname);
775
776         if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
777                 perror("ISST_IF_MBOX_COMMAND");
778                 fprintf(outf,
779                         "Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
780                         cpu, command, sub_command, parameter, req_data);
781                 return -1;
782         } else {
783                 *resp = mbox_cmds.mbox_cmd[0].resp_data;
784                 debug_printf(
785                         "mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
786                         cpu, command, sub_command, parameter, req_data, *resp);
787         }
788
789         close(fd);
790
791         return 0;
792 }
793
794 int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
795                           unsigned long long *req_resp)
796 {
797         struct isst_if_msr_cmds msr_cmds;
798         const char *pathname = "/dev/isst_interface";
799         int fd;
800
801         fd = open(pathname, O_RDWR);
802         if (fd < 0)
803                 err(-1, "%s open failed", pathname);
804
805         msr_cmds.cmd_count = 1;
806         msr_cmds.msr_cmd[0].logical_cpu = cpu;
807         msr_cmds.msr_cmd[0].msr = msr;
808         msr_cmds.msr_cmd[0].read_write = write;
809         if (write)
810                 msr_cmds.msr_cmd[0].data = *req_resp;
811
812         if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
813                 perror("ISST_IF_MSR_COMMAD");
814                 fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
815                         cpu, msr, write);
816         } else {
817                 if (!write)
818                         *req_resp = msr_cmds.msr_cmd[0].data;
819
820                 debug_printf(
821                         "msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
822                         cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
823         }
824
825         close(fd);
826
827         return 0;
828 }
829
830 static int isst_fill_platform_info(void)
831 {
832         const char *pathname = "/dev/isst_interface";
833         int fd;
834
835         fd = open(pathname, O_RDWR);
836         if (fd < 0)
837                 err(-1, "%s open failed", pathname);
838
839         if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
840                 perror("ISST_IF_GET_PLATFORM_INFO");
841                 close(fd);
842                 return -1;
843         }
844
845         close(fd);
846
847         if (isst_platform_info.api_version > supported_api_ver) {
848                 printf("Incompatible API versions; Upgrade of tool is required\n");
849                 return -1;
850         }
851         return 0;
852 }
853
854 static void isst_print_extended_platform_info(void)
855 {
856         int cp_state, cp_cap, fact_support = 0, pbf_support = 0;
857         struct isst_pkg_ctdp_level_info ctdp_level;
858         struct isst_pkg_ctdp pkg_dev;
859         int ret, i, j;
860         FILE *filep;
861
862         for (i = 0; i < 256; ++i) {
863                 char path[256];
864
865                 snprintf(path, sizeof(path),
866                          "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
867                 filep = fopen(path, "r");
868                 if (filep)
869                         break;
870         }
871
872         if (!filep)
873                 return;
874
875         fclose(filep);
876
877         ret = isst_get_ctdp_levels(i, &pkg_dev);
878         if (ret)
879                 return;
880
881         if (pkg_dev.enabled) {
882                 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
883         } else {
884                 fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
885                 fprintf(outf, "Only performance level 0 (base level) is present\n");
886         }
887
888         if (pkg_dev.locked)
889                 fprintf(outf, "TDP level change control is locked\n");
890         else
891                 fprintf(outf, "TDP level change control is unlocked, max level: %d \n", pkg_dev.levels);
892
893         for (j = 0; j <= pkg_dev.levels; ++j) {
894                 ret = isst_get_ctdp_control(i, j, &ctdp_level);
895                 if (ret)
896                         continue;
897
898                 if (!fact_support && ctdp_level.fact_support)
899                         fact_support = 1;
900
901                 if (!pbf_support && ctdp_level.pbf_support)
902                         pbf_support = 1;
903         }
904
905         if (fact_support)
906                 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
907         else
908                 fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
909
910         if (pbf_support)
911                 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
912         else
913                 fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
914
915         ret = isst_read_pm_config(i, &cp_state, &cp_cap);
916         if (cp_cap)
917                 fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
918         else
919                 fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
920 }
921
922 static void isst_print_platform_information(void)
923 {
924         struct isst_if_platform_info platform_info;
925         const char *pathname = "/dev/isst_interface";
926         int fd;
927
928         if (is_clx_n_platform()) {
929                 fprintf(stderr, "\nThis option in not supported on this platform\n");
930                 exit(0);
931         }
932
933         fd = open(pathname, O_RDWR);
934         if (fd < 0)
935                 err(-1, "%s open failed", pathname);
936
937         if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
938                 perror("ISST_IF_GET_PLATFORM_INFO");
939         } else {
940                 fprintf(outf, "Platform: API version : %d\n",
941                         platform_info.api_version);
942                 fprintf(outf, "Platform: Driver version : %d\n",
943                         platform_info.driver_version);
944                 fprintf(outf, "Platform: mbox supported : %d\n",
945                         platform_info.mbox_supported);
946                 fprintf(outf, "Platform: mmio supported : %d\n",
947                         platform_info.mmio_supported);
948                 isst_print_extended_platform_info();
949         }
950
951         close(fd);
952
953         exit(0);
954 }
955
956 static char *local_str0, *local_str1;
957 static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
958                                  void *arg4)
959 {
960         int (*fn_ptr)(int cpu, void *arg);
961         int ret;
962
963         fn_ptr = arg1;
964         ret = fn_ptr(cpu, arg2);
965         if (ret)
966                 isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
967         else
968                 isst_ctdp_display_core_info(cpu, outf, arg3,
969                                             *(unsigned int *)arg4,
970                                             local_str0, local_str1);
971 }
972
973 #define _get_tdp_level(desc, suffix, object, help, str0, str1)                  \
974         static void get_tdp_##object(int arg)                                    \
975         {                                                                         \
976                 struct isst_pkg_ctdp ctdp;                                        \
977 \
978                 if (cmd_help) {                                                   \
979                         fprintf(stderr,                                           \
980                                 "Print %s [No command arguments are required]\n", \
981                                 help);                                            \
982                         exit(0);                                                  \
983                 }                                                                 \
984                 local_str0 = str0;                                                \
985                 local_str1 = str1;                                                \
986                 isst_ctdp_display_information_start(outf);                        \
987                 if (max_target_cpus)                                              \
988                         for_each_online_target_cpu_in_set(                        \
989                                 exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
990                                 &ctdp, desc, &ctdp.object);                       \
991                 else                                                              \
992                         for_each_online_package_in_set(exec_on_get_ctdp_cpu,      \
993                                                        isst_get_ctdp_##suffix,    \
994                                                        &ctdp, desc,               \
995                                                        &ctdp.object);             \
996                 isst_ctdp_display_information_end(outf);                          \
997         }
998
999 _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
1000 _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
1001 _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
1002 _get_tdp_level("get-config-current_level", levels, current_level,
1003                "Current TDP Level", NULL, NULL);
1004 _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
1005
1006 struct isst_pkg_ctdp clx_n_pkg_dev;
1007
1008 static int clx_n_get_base_ratio(void)
1009 {
1010         FILE *fp;
1011         char *begin, *end, *line = NULL;
1012         char number[5];
1013         float value = 0;
1014         size_t n = 0;
1015
1016         fp = fopen("/proc/cpuinfo", "r");
1017         if (!fp)
1018                 err(-1, "cannot open /proc/cpuinfo\n");
1019
1020         while (getline(&line, &n, fp) > 0) {
1021                 if (strstr(line, "model name")) {
1022                         /* this is true for CascadeLake-N */
1023                         begin = strstr(line, "@ ") + 2;
1024                         end = strstr(line, "GHz");
1025                         strncpy(number, begin, end - begin);
1026                         value = atof(number) * 10;
1027                         break;
1028                 }
1029         }
1030         free(line);
1031         fclose(fp);
1032
1033         return (int)(value);
1034 }
1035
1036 static int clx_n_config(int cpu)
1037 {
1038         int i, ret, pkg_id, die_id;
1039         unsigned long cpu_bf;
1040         struct isst_pkg_ctdp_level_info *ctdp_level;
1041         struct isst_pbf_info *pbf_info;
1042
1043         ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1044         pbf_info = &ctdp_level->pbf_info;
1045         ctdp_level->core_cpumask_size =
1046                         alloc_cpu_set(&ctdp_level->core_cpumask);
1047
1048         /* find the frequency base ratio */
1049         ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1050         if (ctdp_level->tdp_ratio == 0) {
1051                 debug_printf("CLX: cn base ratio is zero\n");
1052                 ret = -1;
1053                 goto error_ret;
1054         }
1055
1056         /* find the high and low priority frequencies */
1057         pbf_info->p1_high = 0;
1058         pbf_info->p1_low = ~0;
1059
1060         pkg_id = get_physical_package_id(cpu);
1061         die_id = get_physical_die_id(cpu);
1062
1063         for (i = 0; i < topo_max_cpus; i++) {
1064                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1065                         continue;
1066
1067                 if (pkg_id != get_physical_package_id(i) ||
1068                     die_id != get_physical_die_id(i))
1069                         continue;
1070
1071                 CPU_SET_S(i, ctdp_level->core_cpumask_size,
1072                           ctdp_level->core_cpumask);
1073
1074                 cpu_bf = parse_int_file(1,
1075                         "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1076                                         i);
1077                 if (cpu_bf > pbf_info->p1_high)
1078                         pbf_info->p1_high = cpu_bf;
1079                 if (cpu_bf < pbf_info->p1_low)
1080                         pbf_info->p1_low = cpu_bf;
1081         }
1082
1083         if (pbf_info->p1_high == ~0UL) {
1084                 debug_printf("CLX: maximum base frequency not set\n");
1085                 ret = -1;
1086                 goto error_ret;
1087         }
1088
1089         if (pbf_info->p1_low == 0) {
1090                 debug_printf("CLX: minimum base frequency not set\n");
1091                 ret = -1;
1092                 goto error_ret;
1093         }
1094
1095         /* convert frequencies back to ratios */
1096         pbf_info->p1_high = pbf_info->p1_high / 100000;
1097         pbf_info->p1_low = pbf_info->p1_low / 100000;
1098
1099         /* create high priority cpu mask */
1100         pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1101         for (i = 0; i < topo_max_cpus; i++) {
1102                 if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1103                         continue;
1104
1105                 if (pkg_id != get_physical_package_id(i) ||
1106                     die_id != get_physical_die_id(i))
1107                         continue;
1108
1109                 cpu_bf = parse_int_file(1,
1110                         "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1111                                         i);
1112                 cpu_bf = cpu_bf / 100000;
1113                 if (cpu_bf == pbf_info->p1_high)
1114                         CPU_SET_S(i, pbf_info->core_cpumask_size,
1115                                   pbf_info->core_cpumask);
1116         }
1117
1118         /* extra ctdp & pbf struct parameters */
1119         ctdp_level->processed = 1;
1120         ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1121         ctdp_level->pbf_enabled = 1;
1122         ctdp_level->fact_support = 0; /* FACT is never supported */
1123         ctdp_level->fact_enabled = 0;
1124
1125         return 0;
1126
1127 error_ret:
1128         free_cpu_set(ctdp_level->core_cpumask);
1129         return ret;
1130 }
1131
1132 static void dump_clx_n_config_for_cpu(int cpu, void *arg1, void *arg2,
1133                                    void *arg3, void *arg4)
1134 {
1135         int ret;
1136
1137         if (tdp_level != 0xff && tdp_level != 0) {
1138                 isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1139                 exit(0);
1140         }
1141
1142         ret = clx_n_config(cpu);
1143         if (ret) {
1144                 perror("isst_get_process_ctdp");
1145         } else {
1146                 struct isst_pkg_ctdp_level_info *ctdp_level;
1147                 struct isst_pbf_info *pbf_info;
1148
1149                 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1150                 pbf_info = &ctdp_level->pbf_info;
1151                 isst_ctdp_display_information(cpu, outf, tdp_level, &clx_n_pkg_dev);
1152                 free_cpu_set(ctdp_level->core_cpumask);
1153                 free_cpu_set(pbf_info->core_cpumask);
1154         }
1155 }
1156
1157 static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
1158                                      void *arg3, void *arg4)
1159 {
1160         struct isst_pkg_ctdp pkg_dev;
1161         int ret;
1162
1163         memset(&pkg_dev, 0, sizeof(pkg_dev));
1164         ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
1165         if (ret) {
1166                 isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, cpu);
1167                 isst_ctdp_display_information_end(outf);
1168                 exit(1);
1169         } else {
1170                 isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
1171                 isst_get_process_ctdp_complete(cpu, &pkg_dev);
1172         }
1173 }
1174
1175 static void dump_isst_config(int arg)
1176 {
1177         void *fn;
1178
1179         if (cmd_help) {
1180                 fprintf(stderr,
1181                         "Print Intel(R) Speed Select Technology Performance profile configuration\n");
1182                 fprintf(stderr,
1183                         "including base frequency and turbo frequency configurations\n");
1184                 fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
1185                 fprintf(stderr,
1186                         "\tIf no arguments, dump information for all TDP levels\n");
1187                 exit(0);
1188         }
1189
1190         if (!is_clx_n_platform())
1191                 fn = dump_isst_config_for_cpu;
1192         else
1193                 fn = dump_clx_n_config_for_cpu;
1194
1195         isst_ctdp_display_information_start(outf);
1196
1197         if (max_target_cpus)
1198                 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1199         else
1200                 for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
1201
1202         isst_ctdp_display_information_end(outf);
1203 }
1204
1205 static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1206                                   void *arg4)
1207 {
1208         int ret;
1209
1210         ret = isst_set_tdp_level(cpu, tdp_level);
1211         if (ret) {
1212                 isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1213                 isst_ctdp_display_information_end(outf);
1214                 exit(1);
1215         } else {
1216                 isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
1217                                     ret);
1218                 if (force_online_offline) {
1219                         struct isst_pkg_ctdp_level_info ctdp_level;
1220                         int pkg_id = get_physical_package_id(cpu);
1221                         int die_id = get_physical_die_id(cpu);
1222
1223                         fprintf(stderr, "Option is set to online/offline\n");
1224                         ctdp_level.core_cpumask_size =
1225                                 alloc_cpu_set(&ctdp_level.core_cpumask);
1226                         isst_get_coremask_info(cpu, tdp_level, &ctdp_level);
1227                         if (ctdp_level.cpu_count) {
1228                                 int i, max_cpus = get_topo_max_cpus();
1229                                 for (i = 0; i < max_cpus; ++i) {
1230                                         if (pkg_id != get_physical_package_id(i) || die_id != get_physical_die_id(i))
1231                                                 continue;
1232                                         if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
1233                                                 fprintf(stderr, "online cpu %d\n", i);
1234                                                 set_cpu_online_offline(i, 1);
1235                                         } else {
1236                                                 fprintf(stderr, "offline cpu %d\n", i);
1237                                                 set_cpu_online_offline(i, 0);
1238                                         }
1239                                 }
1240                         }
1241                 }
1242         }
1243 }
1244
1245 static void set_tdp_level(int arg)
1246 {
1247         if (cmd_help) {
1248                 fprintf(stderr, "Set Config TDP level\n");
1249                 fprintf(stderr,
1250                         "\t Arguments: -l|--level : Specify tdp level\n");
1251                 fprintf(stderr,
1252                         "\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1253                 fprintf(stderr,
1254                         "\t  online/offline operation has limitations, refer to Linux hotplug documentation\n");
1255                 exit(0);
1256         }
1257
1258         if (tdp_level == 0xff) {
1259                 isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
1260                 exit(1);
1261         }
1262         isst_ctdp_display_information_start(outf);
1263         if (max_target_cpus)
1264                 for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
1265                                                   NULL, NULL, NULL);
1266         else
1267                 for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
1268                                                NULL, NULL, NULL);
1269         isst_ctdp_display_information_end(outf);
1270 }
1271
1272 static void clx_n_dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2,
1273                                        void *arg3, void *arg4)
1274 {
1275         int ret;
1276
1277         ret = clx_n_config(cpu);
1278         if (ret) {
1279                 perror("isst_get_process_ctdp");
1280         } else {
1281                 struct isst_pkg_ctdp_level_info *ctdp_level;
1282                 struct isst_pbf_info *pbf_info;
1283
1284                 ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1285                 pbf_info = &ctdp_level->pbf_info;
1286                 isst_pbf_display_information(cpu, outf, tdp_level, pbf_info);
1287                 free_cpu_set(ctdp_level->core_cpumask);
1288                 free_cpu_set(pbf_info->core_cpumask);
1289         }
1290 }
1291
1292 static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1293                                     void *arg4)
1294 {
1295         struct isst_pbf_info pbf_info;
1296         int ret;
1297
1298         ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
1299         if (ret) {
1300                 perror("isst_get_pbf_info");
1301         } else {
1302                 isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
1303                 isst_get_pbf_info_complete(&pbf_info);
1304         }
1305 }
1306
1307 static void dump_pbf_config(int arg)
1308 {
1309         void *fn;
1310
1311         if (cmd_help) {
1312                 fprintf(stderr,
1313                         "Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
1314                 fprintf(stderr,
1315                         "\tArguments: -l|--level : Specify tdp level\n");
1316                 exit(0);
1317         }
1318
1319         if (tdp_level == 0xff) {
1320                 fprintf(outf, "Invalid command: specify tdp_level\n");
1321                 exit(1);
1322         }
1323
1324         if (!is_clx_n_platform())
1325                 fn = dump_pbf_config_for_cpu;
1326         else
1327                 fn = clx_n_dump_pbf_config_for_cpu;
1328
1329         isst_ctdp_display_information_start(outf);
1330
1331         if (max_target_cpus)
1332                 for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
1333         else
1334                 for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
1335
1336         isst_ctdp_display_information_end(outf);
1337 }
1338
1339 static int set_clos_param(int cpu, int clos, int epp, int wt, int min, int max)
1340 {
1341         struct isst_clos_config clos_config;
1342         int ret;
1343
1344         ret = isst_pm_get_clos(cpu, clos, &clos_config);
1345         if (ret) {
1346                 perror("isst_pm_get_clos");
1347                 return ret;
1348         }
1349         clos_config.clos_min = min;
1350         clos_config.clos_max = max;
1351         clos_config.epp = epp;
1352         clos_config.clos_prop_prio = wt;
1353         ret = isst_set_clos(cpu, clos, &clos_config);
1354         if (ret) {
1355                 perror("isst_pm_set_clos");
1356                 return ret;
1357         }
1358
1359         return 0;
1360 }
1361
1362 static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1363 {
1364         char buffer[128], freq_str[16];
1365         int fd, ret, len;
1366
1367         if (max)
1368                 snprintf(buffer, sizeof(buffer),
1369                          "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1370         else
1371                 snprintf(buffer, sizeof(buffer),
1372                          "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1373
1374         fd = open(buffer, O_WRONLY);
1375         if (fd < 0)
1376                 return fd;
1377
1378         snprintf(freq_str, sizeof(freq_str), "%d", freq);
1379         len = strlen(freq_str);
1380         ret = write(fd, freq_str, len);
1381         if (ret == -1) {
1382                 close(fd);
1383                 return ret;
1384         }
1385         close(fd);
1386
1387         return 0;
1388 }
1389
1390 static int set_clx_pbf_cpufreq_scaling_min_max(int cpu)
1391 {
1392         struct isst_pkg_ctdp_level_info *ctdp_level;
1393         struct isst_pbf_info *pbf_info;
1394         int i, pkg_id, die_id, freq, freq_high, freq_low;
1395         int ret;
1396
1397         ret = clx_n_config(cpu);
1398         if (ret) {
1399                 perror("set_clx_pbf_cpufreq_scaling_min_max");
1400                 return ret;
1401         }
1402
1403         ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1404         pbf_info = &ctdp_level->pbf_info;
1405         freq_high = pbf_info->p1_high * 100000;
1406         freq_low = pbf_info->p1_low * 100000;
1407
1408         pkg_id = get_physical_package_id(cpu);
1409         die_id = get_physical_die_id(cpu);
1410         for (i = 0; i < get_topo_max_cpus(); ++i) {
1411                 if (pkg_id != get_physical_package_id(i) ||
1412                     die_id != get_physical_die_id(i))
1413                         continue;
1414
1415                 if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1416                                   pbf_info->core_cpumask))
1417                         freq = freq_high;
1418                 else
1419                         freq = freq_low;
1420
1421                 set_cpufreq_scaling_min_max(i, 1, freq);
1422                 set_cpufreq_scaling_min_max(i, 0, freq);
1423         }
1424
1425         return 0;
1426 }
1427
1428 static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1429 {
1430         char buffer[128], min_freq[16];
1431         int fd, ret, len;
1432
1433         if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1434                 return -1;
1435
1436         if (cpuinfo_max)
1437                 snprintf(buffer, sizeof(buffer),
1438                          "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1439         else
1440                 snprintf(buffer, sizeof(buffer),
1441                          "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1442
1443         fd = open(buffer, O_RDONLY);
1444         if (fd < 0)
1445                 return fd;
1446
1447         len = read(fd, min_freq, sizeof(min_freq));
1448         close(fd);
1449
1450         if (len < 0)
1451                 return len;
1452
1453         if (scaling_max)
1454                 snprintf(buffer, sizeof(buffer),
1455                          "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1456         else
1457                 snprintf(buffer, sizeof(buffer),
1458                          "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1459
1460         fd = open(buffer, O_WRONLY);
1461         if (fd < 0)
1462                 return fd;
1463
1464         len = strlen(min_freq);
1465         ret = write(fd, min_freq, len);
1466         if (ret == -1) {
1467                 close(fd);
1468                 return ret;
1469         }
1470         close(fd);
1471
1472         return 0;
1473 }
1474
1475 static void set_scaling_min_to_cpuinfo_max(int cpu)
1476 {
1477         int i, pkg_id, die_id;
1478
1479         pkg_id = get_physical_package_id(cpu);
1480         die_id = get_physical_die_id(cpu);
1481         for (i = 0; i < get_topo_max_cpus(); ++i) {
1482                 if (pkg_id != get_physical_package_id(i) ||
1483                     die_id != get_physical_die_id(i))
1484                         continue;
1485
1486                 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1487         }
1488 }
1489
1490 static void set_scaling_min_to_cpuinfo_min(int cpu)
1491 {
1492         int i, pkg_id, die_id;
1493
1494         pkg_id = get_physical_package_id(cpu);
1495         die_id = get_physical_die_id(cpu);
1496         for (i = 0; i < get_topo_max_cpus(); ++i) {
1497                 if (pkg_id != get_physical_package_id(i) ||
1498                     die_id != get_physical_die_id(i))
1499                         continue;
1500
1501                 set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1502         }
1503 }
1504
1505 static void set_scaling_max_to_cpuinfo_max(int cpu)
1506 {
1507         int i, pkg_id, die_id;
1508
1509         pkg_id = get_physical_package_id(cpu);
1510         die_id = get_physical_die_id(cpu);
1511         for (i = 0; i < get_topo_max_cpus(); ++i) {
1512                 if (pkg_id != get_physical_package_id(i) ||
1513                     die_id != get_physical_die_id(i))
1514                         continue;
1515
1516                 set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1517         }
1518 }
1519
1520 static int set_core_priority_and_min(int cpu, int mask_size,
1521                                      cpu_set_t *cpu_mask, int min_high,
1522                                      int min_low)
1523 {
1524         int pkg_id, die_id, ret, i;
1525
1526         if (!CPU_COUNT_S(mask_size, cpu_mask))
1527                 return -1;
1528
1529         ret = set_clos_param(cpu, 0, 0, 0, min_high, 0xff);
1530         if (ret)
1531                 return ret;
1532
1533         ret = set_clos_param(cpu, 1, 15, 15, min_low, 0xff);
1534         if (ret)
1535                 return ret;
1536
1537         ret = set_clos_param(cpu, 2, 15, 15, min_low, 0xff);
1538         if (ret)
1539                 return ret;
1540
1541         ret = set_clos_param(cpu, 3, 15, 15, min_low, 0xff);
1542         if (ret)
1543                 return ret;
1544
1545         pkg_id = get_physical_package_id(cpu);
1546         die_id = get_physical_die_id(cpu);
1547         for (i = 0; i < get_topo_max_cpus(); ++i) {
1548                 int clos;
1549
1550                 if (pkg_id != get_physical_package_id(i) ||
1551                     die_id != get_physical_die_id(i))
1552                         continue;
1553
1554                 if (CPU_ISSET_S(i, mask_size, cpu_mask))
1555                         clos = 0;
1556                 else
1557                         clos = 3;
1558
1559                 debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1560                 ret = isst_clos_associate(i, clos);
1561                 if (ret) {
1562                         perror("isst_clos_associate");
1563                         return ret;
1564                 }
1565         }
1566
1567         return 0;
1568 }
1569
1570 static int set_pbf_core_power(int cpu)
1571 {
1572         struct isst_pbf_info pbf_info;
1573         struct isst_pkg_ctdp pkg_dev;
1574         int ret;
1575
1576         ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1577         if (ret) {
1578                 perror("isst_get_ctdp_levels");
1579                 return ret;
1580         }
1581         debug_printf("Current_level: %d\n", pkg_dev.current_level);
1582
1583         ret = isst_get_pbf_info(cpu, pkg_dev.current_level, &pbf_info);
1584         if (ret) {
1585                 perror("isst_get_pbf_info");
1586                 return ret;
1587         }
1588         debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1589                      pbf_info.p1_low);
1590
1591         ret = set_core_priority_and_min(cpu, pbf_info.core_cpumask_size,
1592                                         pbf_info.core_cpumask,
1593                                         pbf_info.p1_high, pbf_info.p1_low);
1594         if (ret) {
1595                 perror("set_core_priority_and_min");
1596                 return ret;
1597         }
1598
1599         ret = isst_pm_qos_config(cpu, 1, 1);
1600         if (ret) {
1601                 perror("isst_pm_qos_config");
1602                 return ret;
1603         }
1604
1605         return 0;
1606 }
1607
1608 static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1609                             void *arg4)
1610 {
1611         int ret;
1612         int status = *(int *)arg4;
1613
1614         if (is_clx_n_platform()) {
1615                 if (status) {
1616                         ret = 0;
1617                         if (auto_mode)
1618                                 set_clx_pbf_cpufreq_scaling_min_max(cpu);
1619
1620                 } else {
1621                         ret = -1;
1622                         if (auto_mode) {
1623                                 set_scaling_max_to_cpuinfo_max(cpu);
1624                                 set_scaling_min_to_cpuinfo_min(cpu);
1625                         }
1626                 }
1627                 goto disp_result;
1628         }
1629
1630         if (auto_mode && status) {
1631                 ret = set_pbf_core_power(cpu);
1632                 if (ret)
1633                         goto disp_result;
1634         }
1635
1636         ret = isst_set_pbf_fact_status(cpu, 1, status);
1637         if (ret) {
1638                 perror("isst_set_pbf");
1639                 if (auto_mode)
1640                         isst_pm_qos_config(cpu, 0, 0);
1641         } else {
1642                 if (auto_mode) {
1643                         if (status)
1644                                 set_scaling_min_to_cpuinfo_max(cpu);
1645                         else
1646                                 set_scaling_min_to_cpuinfo_min(cpu);
1647                 }
1648         }
1649
1650         if (auto_mode && !status)
1651                 isst_pm_qos_config(cpu, 0, 0);
1652
1653 disp_result:
1654         if (status)
1655                 isst_display_result(cpu, outf, "base-freq", "enable",
1656                                     ret);
1657         else
1658                 isst_display_result(cpu, outf, "base-freq", "disable",
1659                                     ret);
1660 }
1661
1662 static void set_pbf_enable(int arg)
1663 {
1664         int enable = arg;
1665
1666         if (cmd_help) {
1667                 if (enable) {
1668                         fprintf(stderr,
1669                                 "Enable Intel Speed Select Technology base frequency feature\n");
1670                         fprintf(stderr,
1671                                 "\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1672                 } else {
1673
1674                         fprintf(stderr,
1675                                 "Disable Intel Speed Select Technology base frequency feature\n");
1676                         fprintf(stderr,
1677                                 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1678                 }
1679                 exit(0);
1680         }
1681
1682         isst_ctdp_display_information_start(outf);
1683         if (max_target_cpus)
1684                 for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1685                                                   NULL, &enable);
1686         else
1687                 for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1688                                                NULL, &enable);
1689         isst_ctdp_display_information_end(outf);
1690 }
1691
1692 static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
1693                                      void *arg3, void *arg4)
1694 {
1695         struct isst_fact_info fact_info;
1696         int ret;
1697
1698         ret = isst_get_fact_info(cpu, tdp_level, &fact_info);
1699         if (ret)
1700                 perror("isst_get_fact_bucket_info");
1701         else
1702                 isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
1703                                               fact_avx, &fact_info);
1704 }
1705
1706 static void dump_fact_config(int arg)
1707 {
1708         if (cmd_help) {
1709                 fprintf(stderr,
1710                         "Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
1711                 fprintf(stderr,
1712                         "\tArguments: -l|--level : Specify tdp level\n");
1713                 fprintf(stderr,
1714                         "\tArguments: -b|--bucket : Bucket index to dump\n");
1715                 fprintf(stderr,
1716                         "\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
1717                 exit(0);
1718         }
1719
1720         if (tdp_level == 0xff) {
1721                 fprintf(outf, "Invalid command: specify tdp_level\n");
1722                 exit(1);
1723         }
1724
1725         isst_ctdp_display_information_start(outf);
1726         if (max_target_cpus)
1727                 for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
1728                                                   NULL, NULL, NULL, NULL);
1729         else
1730                 for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
1731                                                NULL, NULL, NULL);
1732         isst_ctdp_display_information_end(outf);
1733 }
1734
1735 static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1736                              void *arg4)
1737 {
1738         int ret;
1739         int status = *(int *)arg4;
1740
1741         if (auto_mode && status) {
1742                 ret = isst_pm_qos_config(cpu, 1, 1);
1743                 if (ret)
1744                         goto disp_results;
1745         }
1746
1747         ret = isst_set_pbf_fact_status(cpu, 0, status);
1748         if (ret) {
1749                 perror("isst_set_fact");
1750                 if (auto_mode)
1751                         isst_pm_qos_config(cpu, 0, 0);
1752
1753                 goto disp_results;
1754         }
1755
1756         /* Set TRL */
1757         if (status) {
1758                 struct isst_pkg_ctdp pkg_dev;
1759
1760                 ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1761                 if (!ret)
1762                         ret = isst_set_trl(cpu, fact_trl);
1763                 if (ret && auto_mode)
1764                         isst_pm_qos_config(cpu, 0, 0);
1765         } else {
1766                 if (auto_mode)
1767                         isst_pm_qos_config(cpu, 0, 0);
1768         }
1769
1770 disp_results:
1771         if (status) {
1772                 isst_display_result(cpu, outf, "turbo-freq", "enable", ret);
1773                 if (ret)
1774                         fact_enable_fail = ret;
1775         } else {
1776                 /* Since we modified TRL during Fact enable, restore it */
1777                 isst_set_trl_from_current_tdp(cpu, fact_trl);
1778                 isst_display_result(cpu, outf, "turbo-freq", "disable", ret);
1779         }
1780 }
1781
1782 static void set_fact_enable(int arg)
1783 {
1784         int i, ret, enable = arg;
1785
1786         if (cmd_help) {
1787                 if (enable) {
1788                         fprintf(stderr,
1789                                 "Enable Intel Speed Select Technology Turbo frequency feature\n");
1790                         fprintf(stderr,
1791                                 "Optional: -t|--trl : Specify turbo ratio limit\n");
1792                         fprintf(stderr,
1793                                 "\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1794                         fprintf(stderr,
1795                                 "-C|--cpu option as as high priority using core-power feature\n");
1796                 } else {
1797                         fprintf(stderr,
1798                                 "Disable Intel Speed Select Technology turbo frequency feature\n");
1799                         fprintf(stderr,
1800                                 "Optional: -t|--trl : Specify turbo ratio limit\n");
1801                         fprintf(stderr,
1802                                 "\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1803                 }
1804                 exit(0);
1805         }
1806
1807         isst_ctdp_display_information_start(outf);
1808         if (max_target_cpus)
1809                 for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1810                                                   NULL, &enable);
1811         else
1812                 for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
1813                                                NULL, &enable);
1814         isst_ctdp_display_information_end(outf);
1815
1816         if (!fact_enable_fail && enable && auto_mode) {
1817                 /*
1818                  * When we adjust CLOS param, we have to set for siblings also.
1819                  * So for the each user specified CPU, also add the sibling
1820                  * in the present_cpu_mask.
1821                  */
1822                 for (i = 0; i < get_topo_max_cpus(); ++i) {
1823                         char buffer[128], sibling_list[128], *cpu_str;
1824                         int fd, len;
1825
1826                         if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1827                                 continue;
1828
1829                         snprintf(buffer, sizeof(buffer),
1830                                  "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1831
1832                         fd = open(buffer, O_RDONLY);
1833                         if (fd < 0)
1834                                 continue;
1835
1836                         len = read(fd, sibling_list, sizeof(sibling_list));
1837                         close(fd);
1838
1839                         if (len < 0)
1840                                 continue;
1841
1842                         cpu_str = strtok(sibling_list, ",");
1843                         while (cpu_str != NULL) {
1844                                 int cpu;
1845
1846                                 sscanf(cpu_str, "%d", &cpu);
1847                                 CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
1848                                 cpu_str = strtok(NULL, ",");
1849                         }
1850                 }
1851
1852                 for (i = 0; i < get_topo_max_cpus(); ++i) {
1853                         int clos;
1854
1855                         if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1856                                 continue;
1857
1858                         ret = set_clos_param(i, 0, 0, 0, 0, 0xff);
1859                         if (ret)
1860                                 goto error_disp;
1861
1862                         ret = set_clos_param(i, 1, 15, 15, 0, 0xff);
1863                         if (ret)
1864                                 goto error_disp;
1865
1866                         ret = set_clos_param(i, 2, 15, 15, 0, 0xff);
1867                         if (ret)
1868                                 goto error_disp;
1869
1870                         ret = set_clos_param(i, 3, 15, 15, 0, 0xff);
1871                         if (ret)
1872                                 goto error_disp;
1873
1874                         if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1875                                 clos = 0;
1876                         else
1877                                 clos = 3;
1878
1879                         debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1880                         ret = isst_clos_associate(i, clos);
1881                         if (ret)
1882                                 goto error_disp;
1883                 }
1884                 isst_display_result(-1, outf, "turbo-freq --auto", "enable", 0);
1885         }
1886
1887         return;
1888
1889 error_disp:
1890         isst_display_result(i, outf, "turbo-freq --auto", "enable", ret);
1891
1892 }
1893
1894 static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
1895                                    void *arg4)
1896 {
1897         int ret;
1898         int status = *(int *)arg4;
1899
1900         ret = isst_pm_qos_config(cpu, status, clos_priority_type);
1901         if (ret)
1902                 perror("isst_pm_qos_config");
1903
1904         if (status)
1905                 isst_display_result(cpu, outf, "core-power", "enable",
1906                                     ret);
1907         else
1908                 isst_display_result(cpu, outf, "core-power", "disable",
1909                                     ret);
1910 }
1911
1912 static void set_clos_enable(int arg)
1913 {
1914         int enable = arg;
1915
1916         if (cmd_help) {
1917                 if (enable) {
1918                         fprintf(stderr,
1919                                 "Enable core-power for a package/die\n");
1920                         fprintf(stderr,
1921                                 "\tClos Enable: Specify priority type with [--priority|-p]\n");
1922                         fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
1923                 } else {
1924                         fprintf(stderr,
1925                                 "Disable core-power: [No command arguments are required]\n");
1926                 }
1927                 exit(0);
1928         }
1929
1930         if (enable && cpufreq_sysfs_present()) {
1931                 fprintf(stderr,
1932                         "cpufreq subsystem and core-power enable will interfere with each other!\n");
1933         }
1934
1935         isst_ctdp_display_information_start(outf);
1936         if (max_target_cpus)
1937                 for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1938                                                   NULL, NULL, &enable);
1939         else
1940                 for_each_online_package_in_set(enable_clos_qos_config, NULL,
1941                                                NULL, NULL, &enable);
1942         isst_ctdp_display_information_end(outf);
1943 }
1944
1945 static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
1946                                      void *arg3, void *arg4)
1947 {
1948         struct isst_clos_config clos_config;
1949         int ret;
1950
1951         ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
1952         if (ret)
1953                 perror("isst_pm_get_clos");
1954         else
1955                 isst_clos_display_information(cpu, outf, current_clos,
1956                                               &clos_config);
1957 }
1958
1959 static void dump_clos_config(int arg)
1960 {
1961         if (cmd_help) {
1962                 fprintf(stderr,
1963                         "Print Intel Speed Select Technology core power configuration\n");
1964                 fprintf(stderr,
1965                         "\tArguments: [-c | --clos]: Specify clos id\n");
1966                 exit(0);
1967         }
1968         if (current_clos < 0 || current_clos > 3) {
1969                 fprintf(stderr, "Invalid clos id\n");
1970                 exit(0);
1971         }
1972
1973         isst_ctdp_display_information_start(outf);
1974         if (max_target_cpus)
1975                 for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
1976                                                   NULL, NULL, NULL, NULL);
1977         else
1978                 for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
1979                                                NULL, NULL, NULL);
1980         isst_ctdp_display_information_end(outf);
1981 }
1982
1983 static void get_clos_info_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
1984                                   void *arg4)
1985 {
1986         int enable, ret, prio_type;
1987
1988         ret = isst_clos_get_clos_information(cpu, &enable, &prio_type);
1989         if (ret)
1990                 perror("isst_clos_get_info");
1991         else {
1992                 int cp_state, cp_cap;
1993
1994                 isst_read_pm_config(cpu, &cp_state, &cp_cap);
1995                 isst_clos_display_clos_information(cpu, outf, enable, prio_type,
1996                                                    cp_state, cp_cap);
1997         }
1998 }
1999
2000 static void dump_clos_info(int arg)
2001 {
2002         if (cmd_help) {
2003                 fprintf(stderr,
2004                         "Print Intel Speed Select Technology core power information\n");
2005                 fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2006                 exit(0);
2007         }
2008
2009         isst_ctdp_display_information_start(outf);
2010         if (max_target_cpus)
2011                 for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2012                                                   NULL, NULL, NULL);
2013         else
2014                 for_each_online_package_in_set(get_clos_info_for_cpu, NULL,
2015                                                NULL, NULL, NULL);
2016         isst_ctdp_display_information_end(outf);
2017
2018 }
2019
2020 static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2021                                     void *arg4)
2022 {
2023         struct isst_clos_config clos_config;
2024         int ret;
2025
2026         clos_config.pkg_id = get_physical_package_id(cpu);
2027         clos_config.die_id = get_physical_die_id(cpu);
2028
2029         clos_config.epp = clos_epp;
2030         clos_config.clos_prop_prio = clos_prop_prio;
2031         clos_config.clos_min = clos_min;
2032         clos_config.clos_max = clos_max;
2033         clos_config.clos_desired = clos_desired;
2034         ret = isst_set_clos(cpu, current_clos, &clos_config);
2035         if (ret)
2036                 perror("isst_set_clos");
2037         else
2038                 isst_display_result(cpu, outf, "core-power", "config", ret);
2039 }
2040
2041 static void set_clos_config(int arg)
2042 {
2043         if (cmd_help) {
2044                 fprintf(stderr,
2045                         "Set core-power configuration for one of the four clos ids\n");
2046                 fprintf(stderr,
2047                         "\tSpecify targeted clos id with [--clos|-c]\n");
2048                 fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
2049                 fprintf(stderr,
2050                         "\tSpecify clos Proportional Priority [--weight|-w]\n");
2051                 fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
2052                 fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
2053                 fprintf(stderr, "\tSpecify clos desired in MHz with [--desired|-d]\n");
2054                 exit(0);
2055         }
2056
2057         if (current_clos < 0 || current_clos > 3) {
2058                 fprintf(stderr, "Invalid clos id\n");
2059                 exit(0);
2060         }
2061         if (clos_epp < 0 || clos_epp > 0x0F) {
2062                 fprintf(stderr, "clos epp is not specified, default: 0\n");
2063                 clos_epp = 0;
2064         }
2065         if (clos_prop_prio < 0 || clos_prop_prio > 0x0F) {
2066                 fprintf(stderr,
2067                         "clos frequency weight is not specified, default: 0\n");
2068                 clos_prop_prio = 0;
2069         }
2070         if (clos_min < 0) {
2071                 fprintf(stderr, "clos min is not specified, default: 0\n");
2072                 clos_min = 0;
2073         }
2074         if (clos_max < 0) {
2075                 fprintf(stderr, "clos max is not specified, default: 25500 MHz\n");
2076                 clos_max = 0xff;
2077         }
2078         if (clos_desired < 0) {
2079                 fprintf(stderr, "clos desired is not specified, default: 0\n");
2080                 clos_desired = 0x00;
2081         }
2082
2083         isst_ctdp_display_information_start(outf);
2084         if (max_target_cpus)
2085                 for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
2086                                                   NULL, NULL, NULL);
2087         else
2088                 for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
2089                                                NULL, NULL, NULL);
2090         isst_ctdp_display_information_end(outf);
2091 }
2092
2093 static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2094                                    void *arg4)
2095 {
2096         int ret;
2097
2098         ret = isst_clos_associate(cpu, current_clos);
2099         if (ret)
2100                 perror("isst_clos_associate");
2101         else
2102                 isst_display_result(cpu, outf, "core-power", "assoc", ret);
2103 }
2104
2105 static void set_clos_assoc(int arg)
2106 {
2107         if (cmd_help) {
2108                 fprintf(stderr, "Associate a clos id to a CPU\n");
2109                 fprintf(stderr,
2110                         "\tSpecify targeted clos id with [--clos|-c]\n");
2111                 fprintf(stderr,
2112                         "\tFor example to associate clos 1 to CPU 0: issue\n");
2113                 fprintf(stderr,
2114                         "\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
2115                 exit(0);
2116         }
2117
2118         if (current_clos < 0 || current_clos > 3) {
2119                 fprintf(stderr, "Invalid clos id\n");
2120                 exit(0);
2121         }
2122         if (max_target_cpus)
2123                 for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
2124                                                   NULL, NULL, NULL);
2125         else {
2126                 fprintf(stderr,
2127                         "Invalid target cpu. Specify with [-c|--cpu]\n");
2128         }
2129 }
2130
2131 static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2132                                    void *arg4)
2133 {
2134         int clos, ret;
2135
2136         ret = isst_clos_get_assoc_status(cpu, &clos);
2137         if (ret)
2138                 perror("isst_clos_get_assoc_status");
2139         else
2140                 isst_clos_display_assoc_information(cpu, outf, clos);
2141 }
2142
2143 static void get_clos_assoc(int arg)
2144 {
2145         if (cmd_help) {
2146                 fprintf(stderr, "Get associate clos id to a CPU\n");
2147                 fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
2148                 exit(0);
2149         }
2150
2151         if (!max_target_cpus) {
2152                 fprintf(stderr,
2153                         "Invalid target cpu. Specify with [-c|--cpu]\n");
2154                 exit(0);
2155         }
2156
2157         isst_ctdp_display_information_start(outf);
2158         for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2159                                           NULL, NULL, NULL);
2160         isst_ctdp_display_information_end(outf);
2161 }
2162
2163 static struct process_cmd_struct clx_n_cmds[] = {
2164         { "perf-profile", "info", dump_isst_config, 0 },
2165         { "base-freq", "info", dump_pbf_config, 0 },
2166         { "base-freq", "enable", set_pbf_enable, 1 },
2167         { "base-freq", "disable", set_pbf_enable, 0 },
2168         { NULL, NULL, NULL, 0 }
2169 };
2170
2171 static struct process_cmd_struct isst_cmds[] = {
2172         { "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2173         { "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2174         { "perf-profile", "get-config-version", get_tdp_version, 0 },
2175         { "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2176         { "perf-profile", "get-config-current-level", get_tdp_current_level,
2177          0 },
2178         { "perf-profile", "set-config-level", set_tdp_level, 0 },
2179         { "perf-profile", "info", dump_isst_config, 0 },
2180         { "base-freq", "info", dump_pbf_config, 0 },
2181         { "base-freq", "enable", set_pbf_enable, 1 },
2182         { "base-freq", "disable", set_pbf_enable, 0 },
2183         { "turbo-freq", "info", dump_fact_config, 0 },
2184         { "turbo-freq", "enable", set_fact_enable, 1 },
2185         { "turbo-freq", "disable", set_fact_enable, 0 },
2186         { "core-power", "info", dump_clos_info, 0 },
2187         { "core-power", "enable", set_clos_enable, 1 },
2188         { "core-power", "disable", set_clos_enable, 0 },
2189         { "core-power", "config", set_clos_config, 0 },
2190         { "core-power", "get-config", dump_clos_config, 0 },
2191         { "core-power", "assoc", set_clos_assoc, 0 },
2192         { "core-power", "get-assoc", get_clos_assoc, 0 },
2193         { NULL, NULL, NULL }
2194 };
2195
2196 /*
2197  * parse cpuset with following syntax
2198  * 1,2,4..6,8-10 and set bits in cpu_subset
2199  */
2200 void parse_cpu_command(char *optarg)
2201 {
2202         unsigned int start, end;
2203         char *next;
2204
2205         next = optarg;
2206
2207         while (next && *next) {
2208                 if (*next == '-') /* no negative cpu numbers */
2209                         goto error;
2210
2211                 start = strtoul(next, &next, 10);
2212
2213                 if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2214                         target_cpus[max_target_cpus++] = start;
2215
2216                 if (*next == '\0')
2217                         break;
2218
2219                 if (*next == ',') {
2220                         next += 1;
2221                         continue;
2222                 }
2223
2224                 if (*next == '-') {
2225                         next += 1; /* start range */
2226                 } else if (*next == '.') {
2227                         next += 1;
2228                         if (*next == '.')
2229                                 next += 1; /* start range */
2230                         else
2231                                 goto error;
2232                 }
2233
2234                 end = strtoul(next, &next, 10);
2235                 if (end <= start)
2236                         goto error;
2237
2238                 while (++start <= end) {
2239                         if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
2240                                 target_cpus[max_target_cpus++] = start;
2241                 }
2242
2243                 if (*next == ',')
2244                         next += 1;
2245                 else if (*next != '\0')
2246                         goto error;
2247         }
2248
2249 #ifdef DEBUG
2250         {
2251                 int i;
2252
2253                 for (i = 0; i < max_target_cpus; ++i)
2254                         printf("cpu [%d] in arg\n", target_cpus[i]);
2255         }
2256 #endif
2257         return;
2258
2259 error:
2260         fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
2261         exit(-1);
2262 }
2263
2264 static void parse_cmd_args(int argc, int start, char **argv)
2265 {
2266         int opt;
2267         int option_index;
2268
2269         static struct option long_options[] = {
2270                 { "bucket", required_argument, 0, 'b' },
2271                 { "level", required_argument, 0, 'l' },
2272                 { "online", required_argument, 0, 'o' },
2273                 { "trl-type", required_argument, 0, 'r' },
2274                 { "trl", required_argument, 0, 't' },
2275                 { "help", no_argument, 0, 'h' },
2276                 { "clos", required_argument, 0, 'c' },
2277                 { "desired", required_argument, 0, 'd' },
2278                 { "epp", required_argument, 0, 'e' },
2279                 { "min", required_argument, 0, 'n' },
2280                 { "max", required_argument, 0, 'm' },
2281                 { "priority", required_argument, 0, 'p' },
2282                 { "weight", required_argument, 0, 'w' },
2283                 { "auto", no_argument, 0, 'a' },
2284                 { 0, 0, 0, 0 }
2285         };
2286
2287         option_index = start;
2288
2289         optind = start + 1;
2290         while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:hoa",
2291                                   long_options, &option_index)) != -1) {
2292                 switch (opt) {
2293                 case 'a':
2294                         auto_mode = 1;
2295                         break;
2296                 case 'b':
2297                         fact_bucket = atoi(optarg);
2298                         break;
2299                 case 'h':
2300                         cmd_help = 1;
2301                         break;
2302                 case 'l':
2303                         tdp_level = atoi(optarg);
2304                         break;
2305                 case 'o':
2306                         force_online_offline = 1;
2307                         break;
2308                 case 't':
2309                         sscanf(optarg, "0x%llx", &fact_trl);
2310                         break;
2311                 case 'r':
2312                         if (!strncmp(optarg, "sse", 3)) {
2313                                 fact_avx = 0x01;
2314                         } else if (!strncmp(optarg, "avx2", 4)) {
2315                                 fact_avx = 0x02;
2316                         } else if (!strncmp(optarg, "avx512", 4)) {
2317                                 fact_avx = 0x04;
2318                         } else {
2319                                 fprintf(outf, "Invalid sse,avx options\n");
2320                                 exit(1);
2321                         }
2322                         break;
2323                 /* CLOS related */
2324                 case 'c':
2325                         current_clos = atoi(optarg);
2326                         break;
2327                 case 'd':
2328                         clos_desired = atoi(optarg);
2329                         clos_desired /= DISP_FREQ_MULTIPLIER;
2330                         break;
2331                 case 'e':
2332                         clos_epp = atoi(optarg);
2333                         break;
2334                 case 'n':
2335                         clos_min = atoi(optarg);
2336                         clos_min /= DISP_FREQ_MULTIPLIER;
2337                         break;
2338                 case 'm':
2339                         clos_max = atoi(optarg);
2340                         clos_max /= DISP_FREQ_MULTIPLIER;
2341                         break;
2342                 case 'p':
2343                         clos_priority_type = atoi(optarg);
2344                         break;
2345                 case 'w':
2346                         clos_prop_prio = atoi(optarg);
2347                         break;
2348                 default:
2349                         printf("no match\n");
2350                 }
2351         }
2352 }
2353
2354 static void isst_help(void)
2355 {
2356         printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
2357                 performance profiles per system via static and/or dynamic\n\
2358                 adjustment of core count, workload, Tjmax, and\n\
2359                 TDP, etc.\n");
2360         printf("\nCommands : For feature=perf-profile\n");
2361         printf("\tinfo\n");
2362
2363         if (!is_clx_n_platform()) {
2364                 printf("\tget-lock-status\n");
2365                 printf("\tget-config-levels\n");
2366                 printf("\tget-config-version\n");
2367                 printf("\tget-config-enabled\n");
2368                 printf("\tget-config-current-level\n");
2369                 printf("\tset-config-level\n");
2370         }
2371 }
2372
2373 static void pbf_help(void)
2374 {
2375         printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
2376                 on certain cores (high priority cores) in exchange for lower\n\
2377                 base frequency on remaining cores (low priority cores).\n");
2378         printf("\tcommand : info\n");
2379         printf("\tcommand : enable\n");
2380         printf("\tcommand : disable\n");
2381 }
2382
2383 static void fact_help(void)
2384 {
2385         printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
2386                 limits to cores based on priority.\n");
2387         printf("\nCommand: For feature=turbo-freq\n");
2388         printf("\tcommand : info\n");
2389         printf("\tcommand : enable\n");
2390         printf("\tcommand : disable\n");
2391 }
2392
2393 static void core_power_help(void)
2394 {
2395         printf("core-power:\tInterface that allows user to define per core/tile\n\
2396                 priority.\n");
2397         printf("\nCommands : For feature=core-power\n");
2398         printf("\tinfo\n");
2399         printf("\tenable\n");
2400         printf("\tdisable\n");
2401         printf("\tconfig\n");
2402         printf("\tget-config\n");
2403         printf("\tassoc\n");
2404         printf("\tget-assoc\n");
2405 }
2406
2407 struct process_cmd_help_struct {
2408         char *feature;
2409         void (*process_fn)(void);
2410 };
2411
2412 static struct process_cmd_help_struct isst_help_cmds[] = {
2413         { "perf-profile", isst_help },
2414         { "base-freq", pbf_help },
2415         { "turbo-freq", fact_help },
2416         { "core-power", core_power_help },
2417         { NULL, NULL }
2418 };
2419
2420 static struct process_cmd_help_struct clx_n_help_cmds[] = {
2421         { "perf-profile", isst_help },
2422         { "base-freq", pbf_help },
2423         { NULL, NULL }
2424 };
2425
2426 void process_command(int argc, char **argv,
2427                      struct process_cmd_help_struct *help_cmds,
2428                      struct process_cmd_struct *cmds)
2429 {
2430         int i = 0, matched = 0;
2431         char *feature = argv[optind];
2432         char *cmd = argv[optind + 1];
2433
2434         if (!feature || !cmd)
2435                 return;
2436
2437         debug_printf("feature name [%s] command [%s]\n", feature, cmd);
2438         if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
2439                 while (help_cmds[i].feature) {
2440                         if (!strcmp(help_cmds[i].feature, feature)) {
2441                                 help_cmds[i].process_fn();
2442                                 exit(0);
2443                         }
2444                         ++i;
2445                 }
2446         }
2447
2448         if (!is_clx_n_platform())
2449                 create_cpu_map();
2450
2451         i = 0;
2452         while (cmds[i].feature) {
2453                 if (!strcmp(cmds[i].feature, feature) &&
2454                     !strcmp(cmds[i].command, cmd)) {
2455                         parse_cmd_args(argc, optind + 1, argv);
2456                         cmds[i].process_fn(cmds[i].arg);
2457                         matched = 1;
2458                         break;
2459                 }
2460                 ++i;
2461         }
2462
2463         if (!matched)
2464                 fprintf(stderr, "Invalid command\n");
2465 }
2466
2467 static void usage(void)
2468 {
2469         if (is_clx_n_platform()) {
2470                 fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
2471                 fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
2472         }
2473
2474         printf("\nUsage:\n");
2475         printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
2476         printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
2477         if (is_clx_n_platform())
2478                 printf("\nFEATURE : [perf-profile|base-freq]\n");
2479         else
2480                 printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power]\n");
2481         printf("\nFor help on each feature, use -h|--help\n");
2482         printf("\tFor example:  intel-speed-select perf-profile -h\n");
2483
2484         printf("\nFor additional help on each command for a feature, use --h|--help\n");
2485         printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
2486         printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
2487
2488         printf("\nOPTIONS\n");
2489         printf("\t[-c|--cpu] : logical cpu number\n");
2490         printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
2491         printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
2492         printf("\t[-d|--debug] : Debug mode\n");
2493         printf("\t[-f|--format] : output format [json|text]. Default: text\n");
2494         printf("\t[-h|--help] : Print help\n");
2495         printf("\t[-i|--info] : Print platform information\n");
2496         printf("\t[-o|--out] : Output file\n");
2497         printf("\t\t\tDefault : stderr\n");
2498         printf("\t[-v|--version] : Print version\n");
2499
2500         printf("\nResult format\n");
2501         printf("\tResult display uses a common format for each command:\n");
2502         printf("\tResults are formatted in text/JSON with\n");
2503         printf("\t\tPackage, Die, CPU, and command specific results.\n");
2504
2505         printf("\nExamples\n");
2506         printf("\tTo get platform information:\n");
2507         printf("\t\tintel-speed-select --info\n");
2508         printf("\tTo get full perf-profile information dump:\n");
2509         printf("\t\tintel-speed-select perf-profile info\n");
2510         printf("\tTo get full base-freq information dump:\n");
2511         printf("\t\tintel-speed-select base-freq info -l 0\n");
2512         if (!is_clx_n_platform()) {
2513                 printf("\tTo get full turbo-freq information dump:\n");
2514                 printf("\t\tintel-speed-select turbo-freq info -l 0\n");
2515         }
2516         exit(1);
2517 }
2518
2519 static void print_version(void)
2520 {
2521         fprintf(outf, "Version %s\n", version_str);
2522         fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
2523         exit(0);
2524 }
2525
2526 static void cmdline(int argc, char **argv)
2527 {
2528         const char *pathname = "/dev/isst_interface";
2529         FILE *fp;
2530         int opt;
2531         int option_index = 0;
2532         int ret;
2533
2534         static struct option long_options[] = {
2535                 { "cpu", required_argument, 0, 'c' },
2536                 { "debug", no_argument, 0, 'd' },
2537                 { "format", required_argument, 0, 'f' },
2538                 { "help", no_argument, 0, 'h' },
2539                 { "info", no_argument, 0, 'i' },
2540                 { "out", required_argument, 0, 'o' },
2541                 { "version", no_argument, 0, 'v' },
2542                 { 0, 0, 0, 0 }
2543         };
2544
2545         if (geteuid() != 0) {
2546                 fprintf(stderr, "Must run as root\n");
2547                 exit(0);
2548         }
2549
2550         ret = update_cpu_model();
2551         if (ret)
2552                 err(-1, "Invalid CPU model (%d)\n", cpu_model);
2553         printf("Intel(R) Speed Select Technology\n");
2554         printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2555
2556         if (!is_clx_n_platform()) {
2557                 fp = fopen(pathname, "rb");
2558                 if (!fp) {
2559                         fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
2560                         fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
2561                         fprintf(stderr, "If the config is included then this is not a supported platform.\n");
2562                         exit(0);
2563                 }
2564                 fclose(fp);
2565         }
2566
2567         progname = argv[0];
2568         while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v", long_options,
2569                                        &option_index)) != -1) {
2570                 switch (opt) {
2571                 case 'c':
2572                         parse_cpu_command(optarg);
2573                         break;
2574                 case 'd':
2575                         debug_flag = 1;
2576                         printf("Debug Mode ON\n");
2577                         break;
2578                 case 'f':
2579                         if (!strncmp(optarg, "json", 4))
2580                                 out_format_json = 1;
2581                         break;
2582                 case 'h':
2583                         usage();
2584                         break;
2585                 case 'i':
2586                         isst_print_platform_information();
2587                         break;
2588                 case 'o':
2589                         if (outf)
2590                                 fclose(outf);
2591                         outf = fopen_or_exit(optarg, "w");
2592                         break;
2593                 case 'v':
2594                         print_version();
2595                         break;
2596                 default:
2597                         usage();
2598                 }
2599         }
2600
2601         if (optind > (argc - 2)) {
2602                 usage();
2603                 exit(0);
2604         }
2605         set_max_cpu_num();
2606         store_cpu_topology();
2607         set_cpu_present_cpu_mask();
2608         set_cpu_target_cpu_mask();
2609
2610         if (!is_clx_n_platform()) {
2611                 ret = isst_fill_platform_info();
2612                 if (ret)
2613                         goto out;
2614                 process_command(argc, argv, isst_help_cmds, isst_cmds);
2615         } else {
2616                 process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
2617         }
2618 out:
2619         free_cpu_set(present_cpumask);
2620         free_cpu_set(target_cpumask);
2621 }
2622
2623 int main(int argc, char **argv)
2624 {
2625         outf = stderr;
2626         cmdline(argc, argv);
2627         return 0;
2628 }