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