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