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