~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/power/x86/intel-speed-select/isst-config.c

Version: ~ [ linux-5.12-rc1 ] ~ [ linux-5.11.2 ] ~ [ linux-5.10.19 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.101 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.177 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.222 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.258 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.258 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp