xref: /linux/tools/power/x86/intel-speed-select/isst-config.c (revision f3874e96fad596cd2c2441802be7e1d0c9fa8677)
13fb4f7cdSSrinivas Pandruvada // SPDX-License-Identifier: GPL-2.0
23fb4f7cdSSrinivas Pandruvada /*
33fb4f7cdSSrinivas Pandruvada  * Intel Speed Select -- Enumerate and control features
43fb4f7cdSSrinivas Pandruvada  * Copyright (c) 2019 Intel Corporation.
53fb4f7cdSSrinivas Pandruvada  */
63fb4f7cdSSrinivas Pandruvada 
73fb4f7cdSSrinivas Pandruvada #include <linux/isst_if.h>
83fb4f7cdSSrinivas Pandruvada 
93fb4f7cdSSrinivas Pandruvada #include "isst.h"
103fb4f7cdSSrinivas Pandruvada 
113fb4f7cdSSrinivas Pandruvada struct process_cmd_struct {
123fb4f7cdSSrinivas Pandruvada 	char *feature;
133fb4f7cdSSrinivas Pandruvada 	char *command;
14ce1326a2SPrarit Bhargava 	void (*process_fn)(int arg);
15ce1326a2SPrarit Bhargava 	int arg;
163fb4f7cdSSrinivas Pandruvada };
173fb4f7cdSSrinivas Pandruvada 
18*f3874e96SSrinivas Pandruvada static const char *version_str = "v1.12";
19*f3874e96SSrinivas Pandruvada 
203fb4f7cdSSrinivas Pandruvada static const int supported_api_ver = 1;
213fb4f7cdSSrinivas Pandruvada static struct isst_if_platform_info isst_platform_info;
223fb4f7cdSSrinivas Pandruvada static char *progname;
233fb4f7cdSSrinivas Pandruvada static int debug_flag;
243fb4f7cdSSrinivas Pandruvada static FILE *outf;
253fb4f7cdSSrinivas Pandruvada 
263fb4f7cdSSrinivas Pandruvada static int cpu_model;
271c1d935cSPrarit Bhargava static int cpu_stepping;
283fb4f7cdSSrinivas Pandruvada 
29e16ea663SSrinivas Pandruvada #define MAX_CPUS_IN_ONE_REQ 256
303fb4f7cdSSrinivas Pandruvada static short max_target_cpus;
313fb4f7cdSSrinivas Pandruvada static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
323fb4f7cdSSrinivas Pandruvada 
333fb4f7cdSSrinivas Pandruvada static int topo_max_cpus;
343fb4f7cdSSrinivas Pandruvada static size_t present_cpumask_size;
353fb4f7cdSSrinivas Pandruvada static cpu_set_t *present_cpumask;
363fb4f7cdSSrinivas Pandruvada static size_t target_cpumask_size;
373fb4f7cdSSrinivas Pandruvada static cpu_set_t *target_cpumask;
383fb4f7cdSSrinivas Pandruvada static int tdp_level = 0xFF;
393fb4f7cdSSrinivas Pandruvada static int fact_bucket = 0xFF;
403fb4f7cdSSrinivas Pandruvada static int fact_avx = 0xFF;
413fb4f7cdSSrinivas Pandruvada static unsigned long long fact_trl;
423fb4f7cdSSrinivas Pandruvada static int out_format_json;
433fb4f7cdSSrinivas Pandruvada static int cmd_help;
443c64c81aSSrinivas Pandruvada static int force_online_offline;
45354bd06fSSrinivas Pandruvada static int auto_mode;
4614a8aa49SSrinivas Pandruvada static int fact_enable_fail;
473fb4f7cdSSrinivas Pandruvada 
48a85a35fcSSrinivas Pandruvada static int mbox_delay;
4932279be7SSrinivas Pandruvada static int mbox_retries = 3;
50a85a35fcSSrinivas Pandruvada 
513fb4f7cdSSrinivas Pandruvada /* clos related */
523fb4f7cdSSrinivas Pandruvada static int current_clos = -1;
533fb4f7cdSSrinivas Pandruvada static int clos_epp = -1;
543fb4f7cdSSrinivas Pandruvada static int clos_prop_prio = -1;
553fb4f7cdSSrinivas Pandruvada static int clos_min = -1;
563fb4f7cdSSrinivas Pandruvada static int clos_max = -1;
573fb4f7cdSSrinivas Pandruvada static int clos_desired = -1;
583fb4f7cdSSrinivas Pandruvada static int clos_priority_type;
593fb4f7cdSSrinivas Pandruvada 
603fb4f7cdSSrinivas Pandruvada struct _cpu_map {
613fb4f7cdSSrinivas Pandruvada 	unsigned short core_id;
623fb4f7cdSSrinivas Pandruvada 	unsigned short pkg_id;
633fb4f7cdSSrinivas Pandruvada 	unsigned short die_id;
643fb4f7cdSSrinivas Pandruvada 	unsigned short punit_cpu;
653fb4f7cdSSrinivas Pandruvada 	unsigned short punit_cpu_core;
663fb4f7cdSSrinivas Pandruvada };
673fb4f7cdSSrinivas Pandruvada struct _cpu_map *cpu_map;
683fb4f7cdSSrinivas Pandruvada 
69fb186158SSrinivas Pandruvada struct cpu_topology {
70fb186158SSrinivas Pandruvada 	short cpu;
71fb186158SSrinivas Pandruvada 	short core_id;
72fb186158SSrinivas Pandruvada 	short pkg_id;
73fb186158SSrinivas Pandruvada 	short die_id;
74fb186158SSrinivas Pandruvada };
75fb186158SSrinivas Pandruvada 
7687e115b3SSrinivas Pandruvada FILE *get_output_file(void)
7787e115b3SSrinivas Pandruvada {
7887e115b3SSrinivas Pandruvada 	return outf;
7987e115b3SSrinivas Pandruvada }
8087e115b3SSrinivas Pandruvada 
813fb4f7cdSSrinivas Pandruvada void debug_printf(const char *format, ...)
823fb4f7cdSSrinivas Pandruvada {
833fb4f7cdSSrinivas Pandruvada 	va_list args;
843fb4f7cdSSrinivas Pandruvada 
853fb4f7cdSSrinivas Pandruvada 	va_start(args, format);
863fb4f7cdSSrinivas Pandruvada 
873fb4f7cdSSrinivas Pandruvada 	if (debug_flag)
883fb4f7cdSSrinivas Pandruvada 		vprintf(format, args);
893fb4f7cdSSrinivas Pandruvada 
903fb4f7cdSSrinivas Pandruvada 	va_end(args);
913fb4f7cdSSrinivas Pandruvada }
923fb4f7cdSSrinivas Pandruvada 
931c1d935cSPrarit Bhargava 
941c1d935cSPrarit Bhargava int is_clx_n_platform(void)
951c1d935cSPrarit Bhargava {
961c1d935cSPrarit Bhargava 	if (cpu_model == 0x55)
971c1d935cSPrarit Bhargava 		if (cpu_stepping == 0x6 || cpu_stepping == 0x7)
981c1d935cSPrarit Bhargava 			return 1;
991c1d935cSPrarit Bhargava 	return 0;
1001c1d935cSPrarit Bhargava }
1011c1d935cSPrarit Bhargava 
10295f8e569SSrinivas Pandruvada int is_skx_based_platform(void)
10395f8e569SSrinivas Pandruvada {
10495f8e569SSrinivas Pandruvada 	if (cpu_model == 0x55)
10595f8e569SSrinivas Pandruvada 		return 1;
10695f8e569SSrinivas Pandruvada 
10795f8e569SSrinivas Pandruvada 	return 0;
10895f8e569SSrinivas Pandruvada }
10995f8e569SSrinivas Pandruvada 
110159f130fSSrinivas Pandruvada int is_spr_platform(void)
111159f130fSSrinivas Pandruvada {
112159f130fSSrinivas Pandruvada 	if (cpu_model == 0x8F)
113159f130fSSrinivas Pandruvada 		return 1;
114159f130fSSrinivas Pandruvada 
115159f130fSSrinivas Pandruvada 	return 0;
116159f130fSSrinivas Pandruvada }
117159f130fSSrinivas Pandruvada 
118159f130fSSrinivas Pandruvada int is_icx_platform(void)
119159f130fSSrinivas Pandruvada {
120159f130fSSrinivas Pandruvada 	if (cpu_model == 0x6A || cpu_model == 0x6C)
121159f130fSSrinivas Pandruvada 		return 1;
122159f130fSSrinivas Pandruvada 
123159f130fSSrinivas Pandruvada 	return 0;
124159f130fSSrinivas Pandruvada }
125159f130fSSrinivas Pandruvada 
1261c1d935cSPrarit Bhargava static int update_cpu_model(void)
1273fb4f7cdSSrinivas Pandruvada {
1283fb4f7cdSSrinivas Pandruvada 	unsigned int ebx, ecx, edx;
1293fb4f7cdSSrinivas Pandruvada 	unsigned int fms, family;
1303fb4f7cdSSrinivas Pandruvada 
1313fb4f7cdSSrinivas Pandruvada 	__cpuid(1, fms, ebx, ecx, edx);
1323fb4f7cdSSrinivas Pandruvada 	family = (fms >> 8) & 0xf;
1333fb4f7cdSSrinivas Pandruvada 	cpu_model = (fms >> 4) & 0xf;
1343fb4f7cdSSrinivas Pandruvada 	if (family == 6 || family == 0xf)
1353fb4f7cdSSrinivas Pandruvada 		cpu_model += ((fms >> 16) & 0xf) << 4;
1361c1d935cSPrarit Bhargava 
1371c1d935cSPrarit Bhargava 	cpu_stepping = fms & 0xf;
1381c1d935cSPrarit Bhargava 	/* only three CascadeLake-N models are supported */
1391c1d935cSPrarit Bhargava 	if (is_clx_n_platform()) {
1401c1d935cSPrarit Bhargava 		FILE *fp;
1411c1d935cSPrarit Bhargava 		size_t n = 0;
1421c1d935cSPrarit Bhargava 		char *line = NULL;
1431c1d935cSPrarit Bhargava 		int ret = 1;
1441c1d935cSPrarit Bhargava 
1451c1d935cSPrarit Bhargava 		fp = fopen("/proc/cpuinfo", "r");
1461c1d935cSPrarit Bhargava 		if (!fp)
1471c1d935cSPrarit Bhargava 			err(-1, "cannot open /proc/cpuinfo\n");
1481c1d935cSPrarit Bhargava 
1491c1d935cSPrarit Bhargava 		while (getline(&line, &n, fp) > 0) {
1501c1d935cSPrarit Bhargava 			if (strstr(line, "model name")) {
1511c1d935cSPrarit Bhargava 				if (strstr(line, "6252N") ||
1521c1d935cSPrarit Bhargava 				    strstr(line, "6230N") ||
1531c1d935cSPrarit Bhargava 				    strstr(line, "5218N"))
1541c1d935cSPrarit Bhargava 					ret = 0;
1551c1d935cSPrarit Bhargava 				break;
1561c1d935cSPrarit Bhargava 			}
1571c1d935cSPrarit Bhargava 		}
1581c1d935cSPrarit Bhargava 		free(line);
1591c1d935cSPrarit Bhargava 		fclose(fp);
1601c1d935cSPrarit Bhargava 		return ret;
1611c1d935cSPrarit Bhargava 	}
1621c1d935cSPrarit Bhargava 	return 0;
1633fb4f7cdSSrinivas Pandruvada }
1643fb4f7cdSSrinivas Pandruvada 
1653fb4f7cdSSrinivas Pandruvada /* Open a file, and exit on failure */
1663fb4f7cdSSrinivas Pandruvada static FILE *fopen_or_exit(const char *path, const char *mode)
1673fb4f7cdSSrinivas Pandruvada {
1683fb4f7cdSSrinivas Pandruvada 	FILE *filep = fopen(path, mode);
1693fb4f7cdSSrinivas Pandruvada 
1703fb4f7cdSSrinivas Pandruvada 	if (!filep)
1713fb4f7cdSSrinivas Pandruvada 		err(1, "%s: open failed", path);
1723fb4f7cdSSrinivas Pandruvada 
1733fb4f7cdSSrinivas Pandruvada 	return filep;
1743fb4f7cdSSrinivas Pandruvada }
1753fb4f7cdSSrinivas Pandruvada 
1763fb4f7cdSSrinivas Pandruvada /* Parse a file containing a single int */
1773fb4f7cdSSrinivas Pandruvada static int parse_int_file(int fatal, const char *fmt, ...)
1783fb4f7cdSSrinivas Pandruvada {
1793fb4f7cdSSrinivas Pandruvada 	va_list args;
1803fb4f7cdSSrinivas Pandruvada 	char path[PATH_MAX];
1813fb4f7cdSSrinivas Pandruvada 	FILE *filep;
1823fb4f7cdSSrinivas Pandruvada 	int value;
1833fb4f7cdSSrinivas Pandruvada 
1843fb4f7cdSSrinivas Pandruvada 	va_start(args, fmt);
1853fb4f7cdSSrinivas Pandruvada 	vsnprintf(path, sizeof(path), fmt, args);
1863fb4f7cdSSrinivas Pandruvada 	va_end(args);
1873fb4f7cdSSrinivas Pandruvada 	if (fatal) {
1883fb4f7cdSSrinivas Pandruvada 		filep = fopen_or_exit(path, "r");
1893fb4f7cdSSrinivas Pandruvada 	} else {
1903fb4f7cdSSrinivas Pandruvada 		filep = fopen(path, "r");
1913fb4f7cdSSrinivas Pandruvada 		if (!filep)
1923fb4f7cdSSrinivas Pandruvada 			return -1;
1933fb4f7cdSSrinivas Pandruvada 	}
1943fb4f7cdSSrinivas Pandruvada 	if (fscanf(filep, "%d", &value) != 1)
1953fb4f7cdSSrinivas Pandruvada 		err(1, "%s: failed to parse number from file", path);
1963fb4f7cdSSrinivas Pandruvada 	fclose(filep);
1973fb4f7cdSSrinivas Pandruvada 
1983fb4f7cdSSrinivas Pandruvada 	return value;
1993fb4f7cdSSrinivas Pandruvada }
2003fb4f7cdSSrinivas Pandruvada 
2013fb4f7cdSSrinivas Pandruvada int cpufreq_sysfs_present(void)
2023fb4f7cdSSrinivas Pandruvada {
2033fb4f7cdSSrinivas Pandruvada 	DIR *dir;
2043fb4f7cdSSrinivas Pandruvada 
2053fb4f7cdSSrinivas Pandruvada 	dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
2063fb4f7cdSSrinivas Pandruvada 	if (dir) {
2073fb4f7cdSSrinivas Pandruvada 		closedir(dir);
2083fb4f7cdSSrinivas Pandruvada 		return 1;
2093fb4f7cdSSrinivas Pandruvada 	}
2103fb4f7cdSSrinivas Pandruvada 
2113fb4f7cdSSrinivas Pandruvada 	return 0;
2123fb4f7cdSSrinivas Pandruvada }
2133fb4f7cdSSrinivas Pandruvada 
2143fb4f7cdSSrinivas Pandruvada int out_format_is_json(void)
2153fb4f7cdSSrinivas Pandruvada {
2163fb4f7cdSSrinivas Pandruvada 	return out_format_json;
2173fb4f7cdSSrinivas Pandruvada }
2183fb4f7cdSSrinivas Pandruvada 
219fb186158SSrinivas Pandruvada static int get_stored_topology_info(int cpu, int *core_id, int *pkg_id, int *die_id)
220fb186158SSrinivas Pandruvada {
221b1d12cefSSrinivas Pandruvada 	const char *pathname = "/var/run/isst_cpu_topology.dat";
222fb186158SSrinivas Pandruvada 	struct cpu_topology cpu_top;
223fb186158SSrinivas Pandruvada 	FILE *fp;
224fb186158SSrinivas Pandruvada 	int ret;
225fb186158SSrinivas Pandruvada 
226fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "rb");
227fb186158SSrinivas Pandruvada 	if (!fp)
228fb186158SSrinivas Pandruvada 		return -1;
229fb186158SSrinivas Pandruvada 
230fb186158SSrinivas Pandruvada 	ret = fseek(fp, cpu * sizeof(cpu_top), SEEK_SET);
231fb186158SSrinivas Pandruvada 	if (ret)
232fb186158SSrinivas Pandruvada 		goto err_ret;
233fb186158SSrinivas Pandruvada 
234fb186158SSrinivas Pandruvada 	ret = fread(&cpu_top, sizeof(cpu_top), 1, fp);
235fb186158SSrinivas Pandruvada 	if (ret != 1) {
236fb186158SSrinivas Pandruvada 		ret = -1;
237fb186158SSrinivas Pandruvada 		goto err_ret;
238fb186158SSrinivas Pandruvada 	}
239fb186158SSrinivas Pandruvada 
240fb186158SSrinivas Pandruvada 	*pkg_id = cpu_top.pkg_id;
241fb186158SSrinivas Pandruvada 	*core_id = cpu_top.core_id;
242fb186158SSrinivas Pandruvada 	*die_id = cpu_top.die_id;
243fb186158SSrinivas Pandruvada 	ret = 0;
244fb186158SSrinivas Pandruvada 
245fb186158SSrinivas Pandruvada err_ret:
246fb186158SSrinivas Pandruvada 	fclose(fp);
247fb186158SSrinivas Pandruvada 
248fb186158SSrinivas Pandruvada 	return ret;
249fb186158SSrinivas Pandruvada }
250fb186158SSrinivas Pandruvada 
251fb186158SSrinivas Pandruvada static void store_cpu_topology(void)
252fb186158SSrinivas Pandruvada {
253b1d12cefSSrinivas Pandruvada 	const char *pathname = "/var/run/isst_cpu_topology.dat";
254fb186158SSrinivas Pandruvada 	FILE *fp;
255fb186158SSrinivas Pandruvada 	int i;
256fb186158SSrinivas Pandruvada 
257fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "rb");
258fb186158SSrinivas Pandruvada 	if (fp) {
259fb186158SSrinivas Pandruvada 		/* Mapping already exists */
260fb186158SSrinivas Pandruvada 		fclose(fp);
261fb186158SSrinivas Pandruvada 		return;
262fb186158SSrinivas Pandruvada 	}
263fb186158SSrinivas Pandruvada 
264fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "wb");
265fb186158SSrinivas Pandruvada 	if (!fp) {
266fb186158SSrinivas Pandruvada 		fprintf(stderr, "Can't create file:%s\n", pathname);
267fb186158SSrinivas Pandruvada 		return;
268fb186158SSrinivas Pandruvada 	}
269fb186158SSrinivas Pandruvada 
270b1d12cefSSrinivas Pandruvada 	fprintf(stderr, "Caching topology information\n");
271b1d12cefSSrinivas Pandruvada 
272fb186158SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
273fb186158SSrinivas Pandruvada 		struct cpu_topology cpu_top;
274fb186158SSrinivas Pandruvada 
275fb186158SSrinivas Pandruvada 		cpu_top.core_id = parse_int_file(0,
276fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/core_id", i);
277fb186158SSrinivas Pandruvada 		if (cpu_top.core_id < 0)
278fb186158SSrinivas Pandruvada 			cpu_top.core_id = -1;
279fb186158SSrinivas Pandruvada 
280fb186158SSrinivas Pandruvada 		cpu_top.pkg_id = parse_int_file(0,
281fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
282fb186158SSrinivas Pandruvada 		if (cpu_top.pkg_id < 0)
283fb186158SSrinivas Pandruvada 			cpu_top.pkg_id = -1;
284fb186158SSrinivas Pandruvada 
285fb186158SSrinivas Pandruvada 		cpu_top.die_id = parse_int_file(0,
286fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/die_id", i);
287fb186158SSrinivas Pandruvada 		if (cpu_top.die_id < 0)
288fb186158SSrinivas Pandruvada 			cpu_top.die_id = -1;
289fb186158SSrinivas Pandruvada 
290fb186158SSrinivas Pandruvada 		cpu_top.cpu = i;
291fb186158SSrinivas Pandruvada 
292fb186158SSrinivas Pandruvada 		if (fwrite(&cpu_top, sizeof(cpu_top), 1, fp) != 1) {
293fb186158SSrinivas Pandruvada 			fprintf(stderr, "Can't write to:%s\n", pathname);
294fb186158SSrinivas Pandruvada 			break;
295fb186158SSrinivas Pandruvada 		}
296fb186158SSrinivas Pandruvada 	}
297fb186158SSrinivas Pandruvada 
298fb186158SSrinivas Pandruvada 	fclose(fp);
299fb186158SSrinivas Pandruvada }
300fb186158SSrinivas Pandruvada 
3013fb4f7cdSSrinivas Pandruvada int get_physical_package_id(int cpu)
3023fb4f7cdSSrinivas Pandruvada {
303fb186158SSrinivas Pandruvada 	int ret;
304fb186158SSrinivas Pandruvada 
305fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
306fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
3073fb4f7cdSSrinivas Pandruvada 			cpu);
308fb186158SSrinivas Pandruvada 	if (ret < 0) {
309fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
310fb186158SSrinivas Pandruvada 
311fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
312fb186158SSrinivas Pandruvada 		if (!ret)
313fb186158SSrinivas Pandruvada 			return pkg_id;
314fb186158SSrinivas Pandruvada 	}
315fb186158SSrinivas Pandruvada 
316fb186158SSrinivas Pandruvada 	return ret;
3173fb4f7cdSSrinivas Pandruvada }
3183fb4f7cdSSrinivas Pandruvada 
3193fb4f7cdSSrinivas Pandruvada int get_physical_core_id(int cpu)
3203fb4f7cdSSrinivas Pandruvada {
321fb186158SSrinivas Pandruvada 	int ret;
322fb186158SSrinivas Pandruvada 
323fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
324fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/core_id",
325fb186158SSrinivas Pandruvada 			cpu);
326fb186158SSrinivas Pandruvada 	if (ret < 0) {
327fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
328fb186158SSrinivas Pandruvada 
329fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
330fb186158SSrinivas Pandruvada 		if (!ret)
331fb186158SSrinivas Pandruvada 			return core_id;
332fb186158SSrinivas Pandruvada 	}
333fb186158SSrinivas Pandruvada 
334fb186158SSrinivas Pandruvada 	return ret;
3353fb4f7cdSSrinivas Pandruvada }
3363fb4f7cdSSrinivas Pandruvada 
3373fb4f7cdSSrinivas Pandruvada int get_physical_die_id(int cpu)
3383fb4f7cdSSrinivas Pandruvada {
3393fb4f7cdSSrinivas Pandruvada 	int ret;
3403fb4f7cdSSrinivas Pandruvada 
341fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
342fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/die_id",
3433fb4f7cdSSrinivas Pandruvada 			cpu);
344fb186158SSrinivas Pandruvada 	if (ret < 0) {
345fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
346fb186158SSrinivas Pandruvada 
347fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
3486c483225SSrinivas Pandruvada 		if (!ret) {
3496c483225SSrinivas Pandruvada 			if (die_id < 0)
3506c483225SSrinivas Pandruvada 				die_id = 0;
3516c483225SSrinivas Pandruvada 
352fb186158SSrinivas Pandruvada 			return die_id;
353fb186158SSrinivas Pandruvada 		}
3546c483225SSrinivas Pandruvada 	}
355fb186158SSrinivas Pandruvada 
3563fb4f7cdSSrinivas Pandruvada 	if (ret < 0)
3573fb4f7cdSSrinivas Pandruvada 		ret = 0;
3583fb4f7cdSSrinivas Pandruvada 
3593fb4f7cdSSrinivas Pandruvada 	return ret;
3603fb4f7cdSSrinivas Pandruvada }
3613fb4f7cdSSrinivas Pandruvada 
3627af5a95bSSrinivas Pandruvada int get_cpufreq_base_freq(int cpu)
3637af5a95bSSrinivas Pandruvada {
3647af5a95bSSrinivas Pandruvada 	return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
3657af5a95bSSrinivas Pandruvada }
3667af5a95bSSrinivas Pandruvada 
3673fb4f7cdSSrinivas Pandruvada int get_topo_max_cpus(void)
3683fb4f7cdSSrinivas Pandruvada {
3693fb4f7cdSSrinivas Pandruvada 	return topo_max_cpus;
3703fb4f7cdSSrinivas Pandruvada }
3713fb4f7cdSSrinivas Pandruvada 
3727fd786dfSSrinivas Pandruvada void set_cpu_online_offline(int cpu, int state)
3733c64c81aSSrinivas Pandruvada {
3743c64c81aSSrinivas Pandruvada 	char buffer[128];
375abd120e3SSrinivas Pandruvada 	int fd, ret;
3763c64c81aSSrinivas Pandruvada 
3773c64c81aSSrinivas Pandruvada 	snprintf(buffer, sizeof(buffer),
3783c64c81aSSrinivas Pandruvada 		 "/sys/devices/system/cpu/cpu%d/online", cpu);
3793c64c81aSSrinivas Pandruvada 
3803c64c81aSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
38169669198SSrinivas Pandruvada 	if (fd < 0) {
38269669198SSrinivas Pandruvada 		if (!cpu && state) {
38369669198SSrinivas Pandruvada 			fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
38469669198SSrinivas Pandruvada 			fprintf(stderr, "Ignoring online request for CPU 0 as this is already online\n");
38569669198SSrinivas Pandruvada 			return;
38669669198SSrinivas Pandruvada 		}
3873c64c81aSSrinivas Pandruvada 		err(-1, "%s open failed", buffer);
38869669198SSrinivas Pandruvada 	}
3893c64c81aSSrinivas Pandruvada 
3903c64c81aSSrinivas Pandruvada 	if (state)
391abd120e3SSrinivas Pandruvada 		ret = write(fd, "1\n", 2);
3923c64c81aSSrinivas Pandruvada 	else
393abd120e3SSrinivas Pandruvada 		ret = write(fd, "0\n", 2);
394abd120e3SSrinivas Pandruvada 
395abd120e3SSrinivas Pandruvada 	if (ret == -1)
396abd120e3SSrinivas Pandruvada 		perror("Online/Offline: Operation failed\n");
3973c64c81aSSrinivas Pandruvada 
3983c64c81aSSrinivas Pandruvada 	close(fd);
3993c64c81aSSrinivas Pandruvada }
4003c64c81aSSrinivas Pandruvada 
4010d3dfd75SSrinivas Pandruvada static void force_all_cpus_online(void)
4020d3dfd75SSrinivas Pandruvada {
4030d3dfd75SSrinivas Pandruvada 	int i;
4040d3dfd75SSrinivas Pandruvada 
4050d3dfd75SSrinivas Pandruvada 	fprintf(stderr, "Forcing all CPUs online\n");
4060d3dfd75SSrinivas Pandruvada 
4070d3dfd75SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i)
4080d3dfd75SSrinivas Pandruvada 		set_cpu_online_offline(i, 1);
4090d3dfd75SSrinivas Pandruvada 
4100d3dfd75SSrinivas Pandruvada 	unlink("/var/run/isst_cpu_topology.dat");
4110d3dfd75SSrinivas Pandruvada }
4120d3dfd75SSrinivas Pandruvada 
4137fd786dfSSrinivas Pandruvada void for_each_online_package_in_set(void (*callback)(int, void *, void *,
4143fb4f7cdSSrinivas Pandruvada 						     void *, void *),
4153fb4f7cdSSrinivas Pandruvada 				    void *arg1, void *arg2, void *arg3,
4163fb4f7cdSSrinivas Pandruvada 				    void *arg4)
4173fb4f7cdSSrinivas Pandruvada {
4183fb4f7cdSSrinivas Pandruvada 	int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
4193fb4f7cdSSrinivas Pandruvada 	int pkg_index = 0, i;
4203fb4f7cdSSrinivas Pandruvada 
4213fb4f7cdSSrinivas Pandruvada 	memset(max_packages, 0xff, sizeof(max_packages));
4223fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
4233fb4f7cdSSrinivas Pandruvada 		int j, online, pkg_id, die_id = 0, skip = 0;
4243fb4f7cdSSrinivas Pandruvada 
4253fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
4263fb4f7cdSSrinivas Pandruvada 			continue;
4273fb4f7cdSSrinivas Pandruvada 		if (i)
4283fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
4293fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
4303fb4f7cdSSrinivas Pandruvada 		else
4313fb4f7cdSSrinivas Pandruvada 			online =
4323fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
4333fb4f7cdSSrinivas Pandruvada 
4343fb4f7cdSSrinivas Pandruvada 		die_id = get_physical_die_id(i);
4353fb4f7cdSSrinivas Pandruvada 		if (die_id < 0)
4363fb4f7cdSSrinivas Pandruvada 			die_id = 0;
437fb186158SSrinivas Pandruvada 
438fb186158SSrinivas Pandruvada 		pkg_id = parse_int_file(0,
439fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
440fb186158SSrinivas Pandruvada 		if (pkg_id < 0)
441f0e0b4d1SSrinivas Pandruvada 			continue;
442fb186158SSrinivas Pandruvada 
4433fb4f7cdSSrinivas Pandruvada 		/* Create an unique id for package, die combination to store */
4443fb4f7cdSSrinivas Pandruvada 		pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
4453fb4f7cdSSrinivas Pandruvada 
4463fb4f7cdSSrinivas Pandruvada 		for (j = 0; j < pkg_index; ++j) {
4473fb4f7cdSSrinivas Pandruvada 			if (max_packages[j] == pkg_id) {
4483fb4f7cdSSrinivas Pandruvada 				skip = 1;
4493fb4f7cdSSrinivas Pandruvada 				break;
4503fb4f7cdSSrinivas Pandruvada 			}
4513fb4f7cdSSrinivas Pandruvada 		}
4523fb4f7cdSSrinivas Pandruvada 
4533fb4f7cdSSrinivas Pandruvada 		if (!skip && online && callback) {
4543fb4f7cdSSrinivas Pandruvada 			callback(i, arg1, arg2, arg3, arg4);
4553fb4f7cdSSrinivas Pandruvada 			max_packages[pkg_index++] = pkg_id;
4563fb4f7cdSSrinivas Pandruvada 		}
4573fb4f7cdSSrinivas Pandruvada 	}
4583fb4f7cdSSrinivas Pandruvada }
4593fb4f7cdSSrinivas Pandruvada 
4603fb4f7cdSSrinivas Pandruvada static void for_each_online_target_cpu_in_set(
4613fb4f7cdSSrinivas Pandruvada 	void (*callback)(int, void *, void *, void *, void *), void *arg1,
4623fb4f7cdSSrinivas Pandruvada 	void *arg2, void *arg3, void *arg4)
4633fb4f7cdSSrinivas Pandruvada {
464070fdea1SSrinivas Pandruvada 	int i, found = 0;
4653fb4f7cdSSrinivas Pandruvada 
4663fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
4673fb4f7cdSSrinivas Pandruvada 		int online;
4683fb4f7cdSSrinivas Pandruvada 
4693fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
4703fb4f7cdSSrinivas Pandruvada 			continue;
4713fb4f7cdSSrinivas Pandruvada 		if (i)
4723fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
4733fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
4743fb4f7cdSSrinivas Pandruvada 		else
4753fb4f7cdSSrinivas Pandruvada 			online =
4763fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
4773fb4f7cdSSrinivas Pandruvada 
478070fdea1SSrinivas Pandruvada 		if (online && callback) {
4793fb4f7cdSSrinivas Pandruvada 			callback(i, arg1, arg2, arg3, arg4);
480070fdea1SSrinivas Pandruvada 			found = 1;
4813fb4f7cdSSrinivas Pandruvada 		}
4823fb4f7cdSSrinivas Pandruvada 	}
4833fb4f7cdSSrinivas Pandruvada 
484070fdea1SSrinivas Pandruvada 	if (!found)
485070fdea1SSrinivas Pandruvada 		fprintf(stderr, "No valid CPU in the list\n");
486070fdea1SSrinivas Pandruvada }
487070fdea1SSrinivas Pandruvada 
4883fb4f7cdSSrinivas Pandruvada #define BITMASK_SIZE 32
4893fb4f7cdSSrinivas Pandruvada static void set_max_cpu_num(void)
4903fb4f7cdSSrinivas Pandruvada {
4913fb4f7cdSSrinivas Pandruvada 	FILE *filep;
4923fb4f7cdSSrinivas Pandruvada 	unsigned long dummy;
493864dc09eSSrinivas Pandruvada 	int i;
4943fb4f7cdSSrinivas Pandruvada 
4953fb4f7cdSSrinivas Pandruvada 	topo_max_cpus = 0;
496864dc09eSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
497864dc09eSSrinivas Pandruvada 		char path[256];
498864dc09eSSrinivas Pandruvada 
499864dc09eSSrinivas Pandruvada 		snprintf(path, sizeof(path),
500864dc09eSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
501864dc09eSSrinivas Pandruvada 		filep = fopen(path, "r");
502864dc09eSSrinivas Pandruvada 		if (filep)
503864dc09eSSrinivas Pandruvada 			break;
504864dc09eSSrinivas Pandruvada 	}
505864dc09eSSrinivas Pandruvada 
506864dc09eSSrinivas Pandruvada 	if (!filep) {
507864dc09eSSrinivas Pandruvada 		fprintf(stderr, "Can't get max cpu number\n");
508864dc09eSSrinivas Pandruvada 		exit(0);
509864dc09eSSrinivas Pandruvada 	}
510864dc09eSSrinivas Pandruvada 
5113fb4f7cdSSrinivas Pandruvada 	while (fscanf(filep, "%lx,", &dummy) == 1)
5123fb4f7cdSSrinivas Pandruvada 		topo_max_cpus += BITMASK_SIZE;
5133fb4f7cdSSrinivas Pandruvada 	fclose(filep);
5143fb4f7cdSSrinivas Pandruvada 
5153fb4f7cdSSrinivas Pandruvada 	debug_printf("max cpus %d\n", topo_max_cpus);
5163fb4f7cdSSrinivas Pandruvada }
5173fb4f7cdSSrinivas Pandruvada 
5183fb4f7cdSSrinivas Pandruvada size_t alloc_cpu_set(cpu_set_t **cpu_set)
5193fb4f7cdSSrinivas Pandruvada {
5203fb4f7cdSSrinivas Pandruvada 	cpu_set_t *_cpu_set;
5213fb4f7cdSSrinivas Pandruvada 	size_t size;
5223fb4f7cdSSrinivas Pandruvada 
5233fb4f7cdSSrinivas Pandruvada 	_cpu_set = CPU_ALLOC((topo_max_cpus + 1));
5243fb4f7cdSSrinivas Pandruvada 	if (_cpu_set == NULL)
5253fb4f7cdSSrinivas Pandruvada 		err(3, "CPU_ALLOC");
5263fb4f7cdSSrinivas Pandruvada 	size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
5273fb4f7cdSSrinivas Pandruvada 	CPU_ZERO_S(size, _cpu_set);
5283fb4f7cdSSrinivas Pandruvada 
5293fb4f7cdSSrinivas Pandruvada 	*cpu_set = _cpu_set;
5303fb4f7cdSSrinivas Pandruvada 	return size;
5313fb4f7cdSSrinivas Pandruvada }
5323fb4f7cdSSrinivas Pandruvada 
5333fb4f7cdSSrinivas Pandruvada void free_cpu_set(cpu_set_t *cpu_set)
5343fb4f7cdSSrinivas Pandruvada {
5353fb4f7cdSSrinivas Pandruvada 	CPU_FREE(cpu_set);
5363fb4f7cdSSrinivas Pandruvada }
5373fb4f7cdSSrinivas Pandruvada 
5383fb4f7cdSSrinivas Pandruvada static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
539de7f9d3dSSrinivas Pandruvada static long long core_mask[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
5403fb4f7cdSSrinivas Pandruvada static void set_cpu_present_cpu_mask(void)
5413fb4f7cdSSrinivas Pandruvada {
5423fb4f7cdSSrinivas Pandruvada 	size_t size;
5433fb4f7cdSSrinivas Pandruvada 	DIR *dir;
5443fb4f7cdSSrinivas Pandruvada 	int i;
5453fb4f7cdSSrinivas Pandruvada 
5463fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&present_cpumask);
5473fb4f7cdSSrinivas Pandruvada 	present_cpumask_size = size;
5483fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
5493fb4f7cdSSrinivas Pandruvada 		char buffer[256];
5503fb4f7cdSSrinivas Pandruvada 
5513fb4f7cdSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
5523fb4f7cdSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d", i);
5533fb4f7cdSSrinivas Pandruvada 		dir = opendir(buffer);
5543fb4f7cdSSrinivas Pandruvada 		if (dir) {
5553fb4f7cdSSrinivas Pandruvada 			int pkg_id, die_id;
5563fb4f7cdSSrinivas Pandruvada 
5573fb4f7cdSSrinivas Pandruvada 			CPU_SET_S(i, size, present_cpumask);
5583fb4f7cdSSrinivas Pandruvada 			die_id = get_physical_die_id(i);
5593fb4f7cdSSrinivas Pandruvada 			if (die_id < 0)
5603fb4f7cdSSrinivas Pandruvada 				die_id = 0;
5613fb4f7cdSSrinivas Pandruvada 
5623fb4f7cdSSrinivas Pandruvada 			pkg_id = get_physical_package_id(i);
563f0e0b4d1SSrinivas Pandruvada 			if (pkg_id < 0) {
564f0e0b4d1SSrinivas Pandruvada 				fprintf(stderr, "Failed to get package id, CPU %d may be offline\n", i);
565f0e0b4d1SSrinivas Pandruvada 				continue;
566f0e0b4d1SSrinivas Pandruvada 			}
5673fb4f7cdSSrinivas Pandruvada 			if (pkg_id < MAX_PACKAGE_COUNT &&
568de7f9d3dSSrinivas Pandruvada 			    die_id < MAX_DIE_PER_PACKAGE) {
569de7f9d3dSSrinivas Pandruvada 				int core_id = get_physical_core_id(i);
570de7f9d3dSSrinivas Pandruvada 
5713fb4f7cdSSrinivas Pandruvada 				cpu_cnt[pkg_id][die_id]++;
572de7f9d3dSSrinivas Pandruvada 				core_mask[pkg_id][die_id] |= (1ULL << core_id);
573de7f9d3dSSrinivas Pandruvada 			}
5743fb4f7cdSSrinivas Pandruvada 		}
5753fb4f7cdSSrinivas Pandruvada 		closedir(dir);
5763fb4f7cdSSrinivas Pandruvada 	}
5773fb4f7cdSSrinivas Pandruvada }
5783fb4f7cdSSrinivas Pandruvada 
5797566616fSJonathan Doman int get_max_punit_core_id(int pkg_id, int die_id)
580de7f9d3dSSrinivas Pandruvada {
5817566616fSJonathan Doman 	int max_id = 0;
582de7f9d3dSSrinivas Pandruvada 	int i;
583de7f9d3dSSrinivas Pandruvada 
5847566616fSJonathan Doman 	for (i = 0; i < topo_max_cpus; ++i)
5857566616fSJonathan Doman 	{
5867566616fSJonathan Doman 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
5877566616fSJonathan Doman 			continue;
5887566616fSJonathan Doman 
5897566616fSJonathan Doman 		if (cpu_map[i].pkg_id == pkg_id &&
5907566616fSJonathan Doman 			cpu_map[i].die_id == die_id &&
5917566616fSJonathan Doman 			cpu_map[i].punit_cpu_core > max_id)
5927566616fSJonathan Doman 			max_id = cpu_map[i].punit_cpu_core;
593de7f9d3dSSrinivas Pandruvada 	}
594de7f9d3dSSrinivas Pandruvada 
5957566616fSJonathan Doman 	return max_id;
596de7f9d3dSSrinivas Pandruvada }
597de7f9d3dSSrinivas Pandruvada 
5983fb4f7cdSSrinivas Pandruvada int get_cpu_count(int pkg_id, int die_id)
5993fb4f7cdSSrinivas Pandruvada {
6003fb4f7cdSSrinivas Pandruvada 	if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
6013ec2aef1SPrarit Bhargava 		return cpu_cnt[pkg_id][die_id];
6023fb4f7cdSSrinivas Pandruvada 
6033fb4f7cdSSrinivas Pandruvada 	return 0;
6043fb4f7cdSSrinivas Pandruvada }
6053fb4f7cdSSrinivas Pandruvada 
6063fb4f7cdSSrinivas Pandruvada static void set_cpu_target_cpu_mask(void)
6073fb4f7cdSSrinivas Pandruvada {
6083fb4f7cdSSrinivas Pandruvada 	size_t size;
6093fb4f7cdSSrinivas Pandruvada 	int i;
6103fb4f7cdSSrinivas Pandruvada 
6113fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&target_cpumask);
6123fb4f7cdSSrinivas Pandruvada 	target_cpumask_size = size;
6133fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < max_target_cpus; ++i) {
6143fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
6153fb4f7cdSSrinivas Pandruvada 				 present_cpumask))
6163fb4f7cdSSrinivas Pandruvada 			continue;
6173fb4f7cdSSrinivas Pandruvada 
6183fb4f7cdSSrinivas Pandruvada 		CPU_SET_S(target_cpus[i], size, target_cpumask);
6193fb4f7cdSSrinivas Pandruvada 	}
6203fb4f7cdSSrinivas Pandruvada }
6213fb4f7cdSSrinivas Pandruvada 
6223fb4f7cdSSrinivas Pandruvada static void create_cpu_map(void)
6233fb4f7cdSSrinivas Pandruvada {
6243fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
6253fb4f7cdSSrinivas Pandruvada 	int i, fd = 0;
6263fb4f7cdSSrinivas Pandruvada 	struct isst_if_cpu_maps map;
6273fb4f7cdSSrinivas Pandruvada 
6283fb4f7cdSSrinivas Pandruvada 	cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
6293fb4f7cdSSrinivas Pandruvada 	if (!cpu_map)
6303fb4f7cdSSrinivas Pandruvada 		err(3, "cpumap");
6313fb4f7cdSSrinivas Pandruvada 
6323fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
6333fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
6343fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
6353fb4f7cdSSrinivas Pandruvada 
6363fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
6373fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
6383fb4f7cdSSrinivas Pandruvada 			continue;
6393fb4f7cdSSrinivas Pandruvada 
6403fb4f7cdSSrinivas Pandruvada 		map.cmd_count = 1;
6413fb4f7cdSSrinivas Pandruvada 		map.cpu_map[0].logical_cpu = i;
6423fb4f7cdSSrinivas Pandruvada 
6433fb4f7cdSSrinivas Pandruvada 		debug_printf(" map logical_cpu:%d\n",
6443fb4f7cdSSrinivas Pandruvada 			     map.cpu_map[0].logical_cpu);
6453fb4f7cdSSrinivas Pandruvada 		if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
6463fb4f7cdSSrinivas Pandruvada 			perror("ISST_IF_GET_PHY_ID");
6473fb4f7cdSSrinivas Pandruvada 			fprintf(outf, "Error: map logical_cpu:%d\n",
6483fb4f7cdSSrinivas Pandruvada 				map.cpu_map[0].logical_cpu);
6493fb4f7cdSSrinivas Pandruvada 			continue;
6503fb4f7cdSSrinivas Pandruvada 		}
6513fb4f7cdSSrinivas Pandruvada 		cpu_map[i].core_id = get_physical_core_id(i);
6523fb4f7cdSSrinivas Pandruvada 		cpu_map[i].pkg_id = get_physical_package_id(i);
6533fb4f7cdSSrinivas Pandruvada 		cpu_map[i].die_id = get_physical_die_id(i);
6543fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
6553fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
6563fb4f7cdSSrinivas Pandruvada 					     1); // shift to get core id
6573fb4f7cdSSrinivas Pandruvada 
6583fb4f7cdSSrinivas Pandruvada 		debug_printf(
6593fb4f7cdSSrinivas Pandruvada 			"map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
6603fb4f7cdSSrinivas Pandruvada 			i, cpu_map[i].core_id, cpu_map[i].die_id,
6613fb4f7cdSSrinivas Pandruvada 			cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
6623fb4f7cdSSrinivas Pandruvada 			cpu_map[i].punit_cpu_core);
6633fb4f7cdSSrinivas Pandruvada 	}
6643fb4f7cdSSrinivas Pandruvada 
6653fb4f7cdSSrinivas Pandruvada 	if (fd)
6663fb4f7cdSSrinivas Pandruvada 		close(fd);
6673fb4f7cdSSrinivas Pandruvada }
6683fb4f7cdSSrinivas Pandruvada 
6693fb4f7cdSSrinivas Pandruvada int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
6703fb4f7cdSSrinivas Pandruvada {
6713fb4f7cdSSrinivas Pandruvada 	int i;
6723fb4f7cdSSrinivas Pandruvada 
6733fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
6743fb4f7cdSSrinivas Pandruvada 		if (cpu_map[i].pkg_id == pkg_id &&
6753fb4f7cdSSrinivas Pandruvada 		    cpu_map[i].die_id == die_id &&
6763fb4f7cdSSrinivas Pandruvada 		    cpu_map[i].punit_cpu_core == punit_core_id)
6773fb4f7cdSSrinivas Pandruvada 			return i;
6783fb4f7cdSSrinivas Pandruvada 	}
6793fb4f7cdSSrinivas Pandruvada 
6803fb4f7cdSSrinivas Pandruvada 	return -EINVAL;
6813fb4f7cdSSrinivas Pandruvada }
6823fb4f7cdSSrinivas Pandruvada 
6833fb4f7cdSSrinivas Pandruvada void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
6843fb4f7cdSSrinivas Pandruvada 				      size_t core_cpumask_size,
6853fb4f7cdSSrinivas Pandruvada 				      cpu_set_t *core_cpumask, int *cpu_cnt)
6863fb4f7cdSSrinivas Pandruvada {
6873fb4f7cdSSrinivas Pandruvada 	int i, cnt = 0;
6883fb4f7cdSSrinivas Pandruvada 	int die_id, pkg_id;
6893fb4f7cdSSrinivas Pandruvada 
6903fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = 0;
6913fb4f7cdSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
6923fb4f7cdSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
6933fb4f7cdSSrinivas Pandruvada 
6943fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < 64; ++i) {
695873e391fSSrinivas Pandruvada 		if (core_mask & BIT_ULL(i)) {
6963fb4f7cdSSrinivas Pandruvada 			int j;
6973fb4f7cdSSrinivas Pandruvada 
6983fb4f7cdSSrinivas Pandruvada 			for (j = 0; j < topo_max_cpus; ++j) {
69944460efeSYouquan Song 				if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
70044460efeSYouquan Song 					continue;
70144460efeSYouquan Song 
7023fb4f7cdSSrinivas Pandruvada 				if (cpu_map[j].pkg_id == pkg_id &&
7033fb4f7cdSSrinivas Pandruvada 				    cpu_map[j].die_id == die_id &&
7043fb4f7cdSSrinivas Pandruvada 				    cpu_map[j].punit_cpu_core == i) {
7053fb4f7cdSSrinivas Pandruvada 					CPU_SET_S(j, core_cpumask_size,
7063fb4f7cdSSrinivas Pandruvada 						  core_cpumask);
7073fb4f7cdSSrinivas Pandruvada 					++cnt;
7083fb4f7cdSSrinivas Pandruvada 				}
7093fb4f7cdSSrinivas Pandruvada 			}
7103fb4f7cdSSrinivas Pandruvada 		}
7113fb4f7cdSSrinivas Pandruvada 	}
7123fb4f7cdSSrinivas Pandruvada 
7133fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = cnt;
7143fb4f7cdSSrinivas Pandruvada }
7153fb4f7cdSSrinivas Pandruvada 
7163fb4f7cdSSrinivas Pandruvada int find_phy_core_num(int logical_cpu)
7173fb4f7cdSSrinivas Pandruvada {
7183fb4f7cdSSrinivas Pandruvada 	if (logical_cpu < topo_max_cpus)
7193fb4f7cdSSrinivas Pandruvada 		return cpu_map[logical_cpu].punit_cpu_core;
7203fb4f7cdSSrinivas Pandruvada 
7213fb4f7cdSSrinivas Pandruvada 	return -EINVAL;
7223fb4f7cdSSrinivas Pandruvada }
7233fb4f7cdSSrinivas Pandruvada 
7243fb4f7cdSSrinivas Pandruvada static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
7253fb4f7cdSSrinivas Pandruvada 				  unsigned int *value)
7263fb4f7cdSSrinivas Pandruvada {
7273fb4f7cdSSrinivas Pandruvada 	struct isst_if_io_regs io_regs;
7283fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
7293fb4f7cdSSrinivas Pandruvada 	int cmd;
7303fb4f7cdSSrinivas Pandruvada 	int fd;
7313fb4f7cdSSrinivas Pandruvada 
7323fb4f7cdSSrinivas Pandruvada 	debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
7333fb4f7cdSSrinivas Pandruvada 
7343fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
7353fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
7363fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
7373fb4f7cdSSrinivas Pandruvada 
7383fb4f7cdSSrinivas Pandruvada 	io_regs.req_count = 1;
7393fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].logical_cpu = cpu;
7403fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].reg = reg;
7413fb4f7cdSSrinivas Pandruvada 	cmd = ISST_IF_IO_CMD;
7423fb4f7cdSSrinivas Pandruvada 	if (write) {
7433fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 1;
7443fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].value = *value;
7453fb4f7cdSSrinivas Pandruvada 	} else {
7463fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 0;
7473fb4f7cdSSrinivas Pandruvada 	}
7483fb4f7cdSSrinivas Pandruvada 
7493fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, cmd, &io_regs) == -1) {
75095f8e569SSrinivas Pandruvada 		if (errno == ENOTTY) {
75195f8e569SSrinivas Pandruvada 			perror("ISST_IF_IO_COMMAND\n");
75295f8e569SSrinivas Pandruvada 			fprintf(stderr, "Check presence of kernel modules: isst_if_mmio\n");
75395f8e569SSrinivas Pandruvada 			exit(0);
75495f8e569SSrinivas Pandruvada 		}
7553fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
7563fb4f7cdSSrinivas Pandruvada 			cpu, reg, write);
7573fb4f7cdSSrinivas Pandruvada 	} else {
7583fb4f7cdSSrinivas Pandruvada 		if (!write)
7593fb4f7cdSSrinivas Pandruvada 			*value = io_regs.io_reg[0].value;
7603fb4f7cdSSrinivas Pandruvada 
7613fb4f7cdSSrinivas Pandruvada 		debug_printf(
7623fb4f7cdSSrinivas Pandruvada 			"mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
7633fb4f7cdSSrinivas Pandruvada 			cpu, reg, write, *value);
7643fb4f7cdSSrinivas Pandruvada 	}
7653fb4f7cdSSrinivas Pandruvada 
7663fb4f7cdSSrinivas Pandruvada 	close(fd);
7673fb4f7cdSSrinivas Pandruvada 
7683fb4f7cdSSrinivas Pandruvada 	return 0;
7693fb4f7cdSSrinivas Pandruvada }
7703fb4f7cdSSrinivas Pandruvada 
7713fb4f7cdSSrinivas Pandruvada int isst_send_mbox_command(unsigned int cpu, unsigned char command,
7723fb4f7cdSSrinivas Pandruvada 			   unsigned char sub_command, unsigned int parameter,
7733fb4f7cdSSrinivas Pandruvada 			   unsigned int req_data, unsigned int *resp)
7743fb4f7cdSSrinivas Pandruvada {
7753fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
77632279be7SSrinivas Pandruvada 	int fd, retry;
7773fb4f7cdSSrinivas Pandruvada 	struct isst_if_mbox_cmds mbox_cmds = { 0 };
7783fb4f7cdSSrinivas Pandruvada 
7793fb4f7cdSSrinivas Pandruvada 	debug_printf(
7803fb4f7cdSSrinivas Pandruvada 		"mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
7813fb4f7cdSSrinivas Pandruvada 		cpu, command, sub_command, parameter, req_data);
7823fb4f7cdSSrinivas Pandruvada 
78395f8e569SSrinivas Pandruvada 	if (!is_skx_based_platform() && command == CONFIG_CLOS &&
7848ddbda76SSrinivas Pandruvada 	    sub_command != CLOS_PM_QOS_CONFIG) {
7853fb4f7cdSSrinivas Pandruvada 		unsigned int value;
7863fb4f7cdSSrinivas Pandruvada 		int write = 0;
7873fb4f7cdSSrinivas Pandruvada 		int clos_id, core_id, ret = 0;
7883fb4f7cdSSrinivas Pandruvada 
789d2d1f304SSrinivas Pandruvada 		debug_printf("CPU %d\n", cpu);
7903fb4f7cdSSrinivas Pandruvada 
7913fb4f7cdSSrinivas Pandruvada 		if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
7923fb4f7cdSSrinivas Pandruvada 			value = req_data;
7933fb4f7cdSSrinivas Pandruvada 			write = 1;
7943fb4f7cdSSrinivas Pandruvada 		}
7953fb4f7cdSSrinivas Pandruvada 
7963fb4f7cdSSrinivas Pandruvada 		switch (sub_command) {
7973fb4f7cdSSrinivas Pandruvada 		case CLOS_PQR_ASSOC:
7983fb4f7cdSSrinivas Pandruvada 			core_id = parameter & 0xff;
7993fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
8003fb4f7cdSSrinivas Pandruvada 				cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
8013fb4f7cdSSrinivas Pandruvada 				&value);
8023fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
8033fb4f7cdSSrinivas Pandruvada 				*resp = value;
8043fb4f7cdSSrinivas Pandruvada 			break;
8053fb4f7cdSSrinivas Pandruvada 		case CLOS_PM_CLOS:
8063fb4f7cdSSrinivas Pandruvada 			clos_id = parameter & 0x03;
8073fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
8083fb4f7cdSSrinivas Pandruvada 				cpu, PM_CLOS_OFFSET + clos_id * 4, write,
8093fb4f7cdSSrinivas Pandruvada 				&value);
8103fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
8113fb4f7cdSSrinivas Pandruvada 				*resp = value;
8123fb4f7cdSSrinivas Pandruvada 			break;
8133fb4f7cdSSrinivas Pandruvada 		case CLOS_STATUS:
8143fb4f7cdSSrinivas Pandruvada 			break;
8153fb4f7cdSSrinivas Pandruvada 		default:
8163fb4f7cdSSrinivas Pandruvada 			break;
8173fb4f7cdSSrinivas Pandruvada 		}
8183fb4f7cdSSrinivas Pandruvada 		return ret;
8193fb4f7cdSSrinivas Pandruvada 	}
8203fb4f7cdSSrinivas Pandruvada 
8213fb4f7cdSSrinivas Pandruvada 	mbox_cmds.cmd_count = 1;
8223fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
8233fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].command = command;
8243fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].sub_command = sub_command;
8253fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].parameter = parameter;
8263fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].req_data = req_data;
8273fb4f7cdSSrinivas Pandruvada 
828a85a35fcSSrinivas Pandruvada 	if (mbox_delay)
829a85a35fcSSrinivas Pandruvada 		usleep(mbox_delay * 1000);
830a85a35fcSSrinivas Pandruvada 
8313fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8323fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8333fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
8343fb4f7cdSSrinivas Pandruvada 
83532279be7SSrinivas Pandruvada 	retry = mbox_retries;
83632279be7SSrinivas Pandruvada 
83732279be7SSrinivas Pandruvada 	do {
8383fb4f7cdSSrinivas Pandruvada 		if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
83995f8e569SSrinivas Pandruvada 			if (errno == ENOTTY) {
84095f8e569SSrinivas Pandruvada 				perror("ISST_IF_MBOX_COMMAND\n");
84195f8e569SSrinivas Pandruvada 				fprintf(stderr, "Check presence of kernel modules: isst_if_mbox_pci or isst_if_mbox_msr\n");
84295f8e569SSrinivas Pandruvada 				exit(0);
84395f8e569SSrinivas Pandruvada 			}
84495f8e569SSrinivas Pandruvada 			debug_printf(
84595f8e569SSrinivas Pandruvada 				"Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x errorno:%d\n",
84695f8e569SSrinivas Pandruvada 				cpu, command, sub_command, parameter, req_data, errno);
84732279be7SSrinivas Pandruvada 			--retry;
8483fb4f7cdSSrinivas Pandruvada 		} else {
8493fb4f7cdSSrinivas Pandruvada 			*resp = mbox_cmds.mbox_cmd[0].resp_data;
8503fb4f7cdSSrinivas Pandruvada 			debug_printf(
8513fb4f7cdSSrinivas Pandruvada 				"mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
8523fb4f7cdSSrinivas Pandruvada 				cpu, command, sub_command, parameter, req_data, *resp);
85332279be7SSrinivas Pandruvada 			break;
8543fb4f7cdSSrinivas Pandruvada 		}
85532279be7SSrinivas Pandruvada 	} while (retry);
8563fb4f7cdSSrinivas Pandruvada 
8573fb4f7cdSSrinivas Pandruvada 	close(fd);
8583fb4f7cdSSrinivas Pandruvada 
85932279be7SSrinivas Pandruvada 	if (!retry) {
86032279be7SSrinivas Pandruvada 		debug_printf("Failed mbox command even after retries\n");
86132279be7SSrinivas Pandruvada 		return -1;
86232279be7SSrinivas Pandruvada 
86332279be7SSrinivas Pandruvada 	}
8643fb4f7cdSSrinivas Pandruvada 	return 0;
8653fb4f7cdSSrinivas Pandruvada }
8663fb4f7cdSSrinivas Pandruvada 
8673fb4f7cdSSrinivas Pandruvada int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
8683fb4f7cdSSrinivas Pandruvada 			  unsigned long long *req_resp)
8693fb4f7cdSSrinivas Pandruvada {
8703fb4f7cdSSrinivas Pandruvada 	struct isst_if_msr_cmds msr_cmds;
8713fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
8723fb4f7cdSSrinivas Pandruvada 	int fd;
8733fb4f7cdSSrinivas Pandruvada 
8743fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8753fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8763fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
8773fb4f7cdSSrinivas Pandruvada 
8783fb4f7cdSSrinivas Pandruvada 	msr_cmds.cmd_count = 1;
8793fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].logical_cpu = cpu;
8803fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].msr = msr;
8813fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].read_write = write;
8823fb4f7cdSSrinivas Pandruvada 	if (write)
8833fb4f7cdSSrinivas Pandruvada 		msr_cmds.msr_cmd[0].data = *req_resp;
8843fb4f7cdSSrinivas Pandruvada 
8853fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
8869945a247SMasanari Iida 		perror("ISST_IF_MSR_COMMAND");
8873fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
8883fb4f7cdSSrinivas Pandruvada 			cpu, msr, write);
8893fb4f7cdSSrinivas Pandruvada 	} else {
8903fb4f7cdSSrinivas Pandruvada 		if (!write)
8913fb4f7cdSSrinivas Pandruvada 			*req_resp = msr_cmds.msr_cmd[0].data;
8923fb4f7cdSSrinivas Pandruvada 
8933fb4f7cdSSrinivas Pandruvada 		debug_printf(
8943fb4f7cdSSrinivas Pandruvada 			"msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
8953fb4f7cdSSrinivas Pandruvada 			cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
8963fb4f7cdSSrinivas Pandruvada 	}
8973fb4f7cdSSrinivas Pandruvada 
8983fb4f7cdSSrinivas Pandruvada 	close(fd);
8993fb4f7cdSSrinivas Pandruvada 
9003fb4f7cdSSrinivas Pandruvada 	return 0;
9013fb4f7cdSSrinivas Pandruvada }
9023fb4f7cdSSrinivas Pandruvada 
9033fb4f7cdSSrinivas Pandruvada static int isst_fill_platform_info(void)
9043fb4f7cdSSrinivas Pandruvada {
9053fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
9063fb4f7cdSSrinivas Pandruvada 	int fd;
9073fb4f7cdSSrinivas Pandruvada 
9083fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
9093fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
9103fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
9113fb4f7cdSSrinivas Pandruvada 
9123fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
9133fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
9143fb4f7cdSSrinivas Pandruvada 		close(fd);
9153fb4f7cdSSrinivas Pandruvada 		return -1;
9163fb4f7cdSSrinivas Pandruvada 	}
9173fb4f7cdSSrinivas Pandruvada 
9183fb4f7cdSSrinivas Pandruvada 	close(fd);
9193fb4f7cdSSrinivas Pandruvada 
9203bc3d30cSPrarit Bhargava 	if (isst_platform_info.api_version > supported_api_ver) {
9213bc3d30cSPrarit Bhargava 		printf("Incompatible API versions; Upgrade of tool is required\n");
9223bc3d30cSPrarit Bhargava 		return -1;
9233bc3d30cSPrarit Bhargava 	}
9243fb4f7cdSSrinivas Pandruvada 	return 0;
9253fb4f7cdSSrinivas Pandruvada }
9263fb4f7cdSSrinivas Pandruvada 
9271ba148aeSSrinivas Pandruvada static void isst_print_extended_platform_info(void)
9281ba148aeSSrinivas Pandruvada {
9291ba148aeSSrinivas Pandruvada 	int cp_state, cp_cap, fact_support = 0, pbf_support = 0;
9301ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
9311ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
9321ba148aeSSrinivas Pandruvada 	int ret, i, j;
9331ba148aeSSrinivas Pandruvada 	FILE *filep;
9341ba148aeSSrinivas Pandruvada 
9351ba148aeSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
9361ba148aeSSrinivas Pandruvada 		char path[256];
9371ba148aeSSrinivas Pandruvada 
9381ba148aeSSrinivas Pandruvada 		snprintf(path, sizeof(path),
9391ba148aeSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
9401ba148aeSSrinivas Pandruvada 		filep = fopen(path, "r");
9411ba148aeSSrinivas Pandruvada 		if (filep)
9421ba148aeSSrinivas Pandruvada 			break;
9431ba148aeSSrinivas Pandruvada 	}
9441ba148aeSSrinivas Pandruvada 
9451ba148aeSSrinivas Pandruvada 	if (!filep)
9461ba148aeSSrinivas Pandruvada 		return;
9471ba148aeSSrinivas Pandruvada 
9481ba148aeSSrinivas Pandruvada 	fclose(filep);
9491ba148aeSSrinivas Pandruvada 
9501ba148aeSSrinivas Pandruvada 	ret = isst_get_ctdp_levels(i, &pkg_dev);
9511ba148aeSSrinivas Pandruvada 	if (ret)
9521ba148aeSSrinivas Pandruvada 		return;
9531ba148aeSSrinivas Pandruvada 
9541ba148aeSSrinivas Pandruvada 	if (pkg_dev.enabled) {
9551ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
9561ba148aeSSrinivas Pandruvada 	} else {
9571ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
9581ba148aeSSrinivas Pandruvada 		fprintf(outf, "Only performance level 0 (base level) is present\n");
9591ba148aeSSrinivas Pandruvada 	}
9601ba148aeSSrinivas Pandruvada 
9611ba148aeSSrinivas Pandruvada 	if (pkg_dev.locked)
9621ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is locked\n");
9631ba148aeSSrinivas Pandruvada 	else
9641ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is unlocked, max level: %d \n", pkg_dev.levels);
9651ba148aeSSrinivas Pandruvada 
9661ba148aeSSrinivas Pandruvada 	for (j = 0; j <= pkg_dev.levels; ++j) {
9671ba148aeSSrinivas Pandruvada 		ret = isst_get_ctdp_control(i, j, &ctdp_level);
9681ba148aeSSrinivas Pandruvada 		if (ret)
9691ba148aeSSrinivas Pandruvada 			continue;
9701ba148aeSSrinivas Pandruvada 
9711ba148aeSSrinivas Pandruvada 		if (!fact_support && ctdp_level.fact_support)
9721ba148aeSSrinivas Pandruvada 			fact_support = 1;
9731ba148aeSSrinivas Pandruvada 
9741ba148aeSSrinivas Pandruvada 		if (!pbf_support && ctdp_level.pbf_support)
9751ba148aeSSrinivas Pandruvada 			pbf_support = 1;
9761ba148aeSSrinivas Pandruvada 	}
9771ba148aeSSrinivas Pandruvada 
9781ba148aeSSrinivas Pandruvada 	if (fact_support)
9791ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
9801ba148aeSSrinivas Pandruvada 	else
9811ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
9821ba148aeSSrinivas Pandruvada 
9831ba148aeSSrinivas Pandruvada 	if (pbf_support)
9841ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
9851ba148aeSSrinivas Pandruvada 	else
9861ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
9871ba148aeSSrinivas Pandruvada 
9881ba148aeSSrinivas Pandruvada 	ret = isst_read_pm_config(i, &cp_state, &cp_cap);
989b84733a1SSrinivas Pandruvada 	if (ret) {
990b84733a1SSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) status is unknown\n");
991b84733a1SSrinivas Pandruvada 		return;
992b84733a1SSrinivas Pandruvada 	}
9931ba148aeSSrinivas Pandruvada 	if (cp_cap)
9941ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
9951ba148aeSSrinivas Pandruvada 	else
9961ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
9971ba148aeSSrinivas Pandruvada }
9981ba148aeSSrinivas Pandruvada 
9993fb4f7cdSSrinivas Pandruvada static void isst_print_platform_information(void)
10003fb4f7cdSSrinivas Pandruvada {
10013fb4f7cdSSrinivas Pandruvada 	struct isst_if_platform_info platform_info;
10023fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
10033fb4f7cdSSrinivas Pandruvada 	int fd;
10043fb4f7cdSSrinivas Pandruvada 
10051ba148aeSSrinivas Pandruvada 	if (is_clx_n_platform()) {
10061ba148aeSSrinivas Pandruvada 		fprintf(stderr, "\nThis option in not supported on this platform\n");
10071ba148aeSSrinivas Pandruvada 		exit(0);
10081ba148aeSSrinivas Pandruvada 	}
10091ba148aeSSrinivas Pandruvada 
10103fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
10113fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
10123fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
10133fb4f7cdSSrinivas Pandruvada 
10143fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
10153fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
10163fb4f7cdSSrinivas Pandruvada 	} else {
10173fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: API version : %d\n",
10183fb4f7cdSSrinivas Pandruvada 			platform_info.api_version);
10193fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: Driver version : %d\n",
10203fb4f7cdSSrinivas Pandruvada 			platform_info.driver_version);
10213fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mbox supported : %d\n",
10223fb4f7cdSSrinivas Pandruvada 			platform_info.mbox_supported);
10233fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mmio supported : %d\n",
10243fb4f7cdSSrinivas Pandruvada 			platform_info.mmio_supported);
10251ba148aeSSrinivas Pandruvada 		isst_print_extended_platform_info();
10263fb4f7cdSSrinivas Pandruvada 	}
10273fb4f7cdSSrinivas Pandruvada 
10283fb4f7cdSSrinivas Pandruvada 	close(fd);
10293fb4f7cdSSrinivas Pandruvada 
10303fb4f7cdSSrinivas Pandruvada 	exit(0);
10313fb4f7cdSSrinivas Pandruvada }
10323fb4f7cdSSrinivas Pandruvada 
10333d1a8579SSrinivas Pandruvada static char *local_str0, *local_str1;
10343fb4f7cdSSrinivas Pandruvada static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
10353fb4f7cdSSrinivas Pandruvada 				 void *arg4)
10363fb4f7cdSSrinivas Pandruvada {
10373fb4f7cdSSrinivas Pandruvada 	int (*fn_ptr)(int cpu, void *arg);
10383fb4f7cdSSrinivas Pandruvada 	int ret;
10393fb4f7cdSSrinivas Pandruvada 
10403fb4f7cdSSrinivas Pandruvada 	fn_ptr = arg1;
10413fb4f7cdSSrinivas Pandruvada 	ret = fn_ptr(cpu, arg2);
10423fb4f7cdSSrinivas Pandruvada 	if (ret)
10433d1a8579SSrinivas Pandruvada 		isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
10443fb4f7cdSSrinivas Pandruvada 	else
1045b3abfd77SSrinivas Pandruvada 		isst_ctdp_display_core_info(cpu, outf, arg3,
10463d1a8579SSrinivas Pandruvada 					    *(unsigned int *)arg4,
10473d1a8579SSrinivas Pandruvada 					    local_str0, local_str1);
10483fb4f7cdSSrinivas Pandruvada }
10493fb4f7cdSSrinivas Pandruvada 
10503d1a8579SSrinivas Pandruvada #define _get_tdp_level(desc, suffix, object, help, str0, str1)			\
1051ce1326a2SPrarit Bhargava 	static void get_tdp_##object(int arg)                                    \
10523fb4f7cdSSrinivas Pandruvada 	{                                                                         \
10533fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp ctdp;                                        \
10543fb4f7cdSSrinivas Pandruvada \
10553fb4f7cdSSrinivas Pandruvada 		if (cmd_help) {                                                   \
10563fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,                                           \
10573fb4f7cdSSrinivas Pandruvada 				"Print %s [No command arguments are required]\n", \
10583fb4f7cdSSrinivas Pandruvada 				help);                                            \
10593fb4f7cdSSrinivas Pandruvada 			exit(0);                                                  \
10603fb4f7cdSSrinivas Pandruvada 		}                                                                 \
10613d1a8579SSrinivas Pandruvada 		local_str0 = str0;						  \
10623d1a8579SSrinivas Pandruvada 		local_str1 = str1;						  \
10633fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_start(outf);                        \
10643fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus)                                              \
10653fb4f7cdSSrinivas Pandruvada 			for_each_online_target_cpu_in_set(                        \
10663fb4f7cdSSrinivas Pandruvada 				exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
10673fb4f7cdSSrinivas Pandruvada 				&ctdp, desc, &ctdp.object);                       \
10683fb4f7cdSSrinivas Pandruvada 		else                                                              \
10693fb4f7cdSSrinivas Pandruvada 			for_each_online_package_in_set(exec_on_get_ctdp_cpu,      \
10703fb4f7cdSSrinivas Pandruvada 						       isst_get_ctdp_##suffix,    \
10713fb4f7cdSSrinivas Pandruvada 						       &ctdp, desc,               \
10723fb4f7cdSSrinivas Pandruvada 						       &ctdp.object);             \
10733fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);                          \
10743fb4f7cdSSrinivas Pandruvada 	}
10753fb4f7cdSSrinivas Pandruvada 
10763d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
10773d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
10783d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
10793fb4f7cdSSrinivas Pandruvada _get_tdp_level("get-config-current_level", levels, current_level,
10803d1a8579SSrinivas Pandruvada 	       "Current TDP Level", NULL, NULL);
10813d1a8579SSrinivas Pandruvada _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
10823fb4f7cdSSrinivas Pandruvada 
1083062e4aacSPrarit Bhargava struct isst_pkg_ctdp clx_n_pkg_dev;
1084062e4aacSPrarit Bhargava 
1085062e4aacSPrarit Bhargava static int clx_n_get_base_ratio(void)
1086062e4aacSPrarit Bhargava {
1087062e4aacSPrarit Bhargava 	FILE *fp;
1088062e4aacSPrarit Bhargava 	char *begin, *end, *line = NULL;
1089062e4aacSPrarit Bhargava 	char number[5];
1090062e4aacSPrarit Bhargava 	float value = 0;
1091062e4aacSPrarit Bhargava 	size_t n = 0;
1092062e4aacSPrarit Bhargava 
1093062e4aacSPrarit Bhargava 	fp = fopen("/proc/cpuinfo", "r");
1094062e4aacSPrarit Bhargava 	if (!fp)
1095062e4aacSPrarit Bhargava 		err(-1, "cannot open /proc/cpuinfo\n");
1096062e4aacSPrarit Bhargava 
1097062e4aacSPrarit Bhargava 	while (getline(&line, &n, fp) > 0) {
1098062e4aacSPrarit Bhargava 		if (strstr(line, "model name")) {
1099062e4aacSPrarit Bhargava 			/* this is true for CascadeLake-N */
1100062e4aacSPrarit Bhargava 			begin = strstr(line, "@ ") + 2;
1101062e4aacSPrarit Bhargava 			end = strstr(line, "GHz");
1102062e4aacSPrarit Bhargava 			strncpy(number, begin, end - begin);
1103062e4aacSPrarit Bhargava 			value = atof(number) * 10;
1104062e4aacSPrarit Bhargava 			break;
1105062e4aacSPrarit Bhargava 		}
1106062e4aacSPrarit Bhargava 	}
1107062e4aacSPrarit Bhargava 	free(line);
1108062e4aacSPrarit Bhargava 	fclose(fp);
1109062e4aacSPrarit Bhargava 
1110062e4aacSPrarit Bhargava 	return (int)(value);
1111062e4aacSPrarit Bhargava }
1112062e4aacSPrarit Bhargava 
1113062e4aacSPrarit Bhargava static int clx_n_config(int cpu)
1114062e4aacSPrarit Bhargava {
1115062e4aacSPrarit Bhargava 	int i, ret, pkg_id, die_id;
1116062e4aacSPrarit Bhargava 	unsigned long cpu_bf;
1117062e4aacSPrarit Bhargava 	struct isst_pkg_ctdp_level_info *ctdp_level;
1118062e4aacSPrarit Bhargava 	struct isst_pbf_info *pbf_info;
1119062e4aacSPrarit Bhargava 
1120062e4aacSPrarit Bhargava 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1121062e4aacSPrarit Bhargava 	pbf_info = &ctdp_level->pbf_info;
1122062e4aacSPrarit Bhargava 	ctdp_level->core_cpumask_size =
1123062e4aacSPrarit Bhargava 			alloc_cpu_set(&ctdp_level->core_cpumask);
1124062e4aacSPrarit Bhargava 
1125062e4aacSPrarit Bhargava 	/* find the frequency base ratio */
1126062e4aacSPrarit Bhargava 	ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1127062e4aacSPrarit Bhargava 	if (ctdp_level->tdp_ratio == 0) {
1128062e4aacSPrarit Bhargava 		debug_printf("CLX: cn base ratio is zero\n");
1129062e4aacSPrarit Bhargava 		ret = -1;
1130062e4aacSPrarit Bhargava 		goto error_ret;
1131062e4aacSPrarit Bhargava 	}
1132062e4aacSPrarit Bhargava 
1133062e4aacSPrarit Bhargava 	/* find the high and low priority frequencies */
1134062e4aacSPrarit Bhargava 	pbf_info->p1_high = 0;
1135062e4aacSPrarit Bhargava 	pbf_info->p1_low = ~0;
1136062e4aacSPrarit Bhargava 
1137062e4aacSPrarit Bhargava 	pkg_id = get_physical_package_id(cpu);
1138062e4aacSPrarit Bhargava 	die_id = get_physical_die_id(cpu);
1139062e4aacSPrarit Bhargava 
1140062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1141062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1142062e4aacSPrarit Bhargava 			continue;
1143062e4aacSPrarit Bhargava 
1144062e4aacSPrarit Bhargava 		if (pkg_id != get_physical_package_id(i) ||
1145062e4aacSPrarit Bhargava 		    die_id != get_physical_die_id(i))
1146062e4aacSPrarit Bhargava 			continue;
1147062e4aacSPrarit Bhargava 
1148062e4aacSPrarit Bhargava 		CPU_SET_S(i, ctdp_level->core_cpumask_size,
1149062e4aacSPrarit Bhargava 			  ctdp_level->core_cpumask);
1150062e4aacSPrarit Bhargava 
1151062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1152062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1153062e4aacSPrarit Bhargava 					i);
1154062e4aacSPrarit Bhargava 		if (cpu_bf > pbf_info->p1_high)
1155062e4aacSPrarit Bhargava 			pbf_info->p1_high = cpu_bf;
1156062e4aacSPrarit Bhargava 		if (cpu_bf < pbf_info->p1_low)
1157062e4aacSPrarit Bhargava 			pbf_info->p1_low = cpu_bf;
1158062e4aacSPrarit Bhargava 	}
1159062e4aacSPrarit Bhargava 
1160062e4aacSPrarit Bhargava 	if (pbf_info->p1_high == ~0UL) {
1161062e4aacSPrarit Bhargava 		debug_printf("CLX: maximum base frequency not set\n");
1162062e4aacSPrarit Bhargava 		ret = -1;
1163062e4aacSPrarit Bhargava 		goto error_ret;
1164062e4aacSPrarit Bhargava 	}
1165062e4aacSPrarit Bhargava 
1166062e4aacSPrarit Bhargava 	if (pbf_info->p1_low == 0) {
1167062e4aacSPrarit Bhargava 		debug_printf("CLX: minimum base frequency not set\n");
1168062e4aacSPrarit Bhargava 		ret = -1;
1169062e4aacSPrarit Bhargava 		goto error_ret;
1170062e4aacSPrarit Bhargava 	}
1171062e4aacSPrarit Bhargava 
1172062e4aacSPrarit Bhargava 	/* convert frequencies back to ratios */
117391d92814SSrinivas Pandruvada 	pbf_info->p1_high = pbf_info->p1_high / 100000;
117491d92814SSrinivas Pandruvada 	pbf_info->p1_low = pbf_info->p1_low / 100000;
1175062e4aacSPrarit Bhargava 
1176062e4aacSPrarit Bhargava 	/* create high priority cpu mask */
1177062e4aacSPrarit Bhargava 	pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1178062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1179062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1180062e4aacSPrarit Bhargava 			continue;
1181062e4aacSPrarit Bhargava 
1182062e4aacSPrarit Bhargava 		if (pkg_id != get_physical_package_id(i) ||
1183062e4aacSPrarit Bhargava 		    die_id != get_physical_die_id(i))
1184062e4aacSPrarit Bhargava 			continue;
1185062e4aacSPrarit Bhargava 
1186062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1187062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1188062e4aacSPrarit Bhargava 					i);
118991d92814SSrinivas Pandruvada 		cpu_bf = cpu_bf / 100000;
1190062e4aacSPrarit Bhargava 		if (cpu_bf == pbf_info->p1_high)
1191062e4aacSPrarit Bhargava 			CPU_SET_S(i, pbf_info->core_cpumask_size,
1192062e4aacSPrarit Bhargava 				  pbf_info->core_cpumask);
1193062e4aacSPrarit Bhargava 	}
1194062e4aacSPrarit Bhargava 
1195062e4aacSPrarit Bhargava 	/* extra ctdp & pbf struct parameters */
1196062e4aacSPrarit Bhargava 	ctdp_level->processed = 1;
1197062e4aacSPrarit Bhargava 	ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1198062e4aacSPrarit Bhargava 	ctdp_level->pbf_enabled = 1;
1199062e4aacSPrarit Bhargava 	ctdp_level->fact_support = 0; /* FACT is never supported */
1200062e4aacSPrarit Bhargava 	ctdp_level->fact_enabled = 0;
1201062e4aacSPrarit Bhargava 
1202062e4aacSPrarit Bhargava 	return 0;
1203062e4aacSPrarit Bhargava 
1204062e4aacSPrarit Bhargava error_ret:
1205062e4aacSPrarit Bhargava 	free_cpu_set(ctdp_level->core_cpumask);
1206062e4aacSPrarit Bhargava 	return ret;
1207062e4aacSPrarit Bhargava }
1208062e4aacSPrarit Bhargava 
1209062e4aacSPrarit Bhargava static void dump_clx_n_config_for_cpu(int cpu, void *arg1, void *arg2,
1210062e4aacSPrarit Bhargava 				   void *arg3, void *arg4)
1211062e4aacSPrarit Bhargava {
1212062e4aacSPrarit Bhargava 	int ret;
1213062e4aacSPrarit Bhargava 
1214ac9d05eaSSrinivas Pandruvada 	if (tdp_level != 0xff && tdp_level != 0) {
1215ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1216ac9d05eaSSrinivas Pandruvada 		exit(0);
1217ac9d05eaSSrinivas Pandruvada 	}
1218ac9d05eaSSrinivas Pandruvada 
1219062e4aacSPrarit Bhargava 	ret = clx_n_config(cpu);
1220062e4aacSPrarit Bhargava 	if (ret) {
12217fc9fefdSSrinivas Pandruvada 		debug_printf("clx_n_config failed");
1222062e4aacSPrarit Bhargava 	} else {
1223062e4aacSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
1224062e4aacSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
1225062e4aacSPrarit Bhargava 
1226062e4aacSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1227062e4aacSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
122828c59ae6SPrarit Bhargava 		clx_n_pkg_dev.processed = 1;
1229062e4aacSPrarit Bhargava 		isst_ctdp_display_information(cpu, outf, tdp_level, &clx_n_pkg_dev);
1230062e4aacSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
1231062e4aacSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
1232062e4aacSPrarit Bhargava 	}
1233062e4aacSPrarit Bhargava }
1234062e4aacSPrarit Bhargava 
12353fb4f7cdSSrinivas Pandruvada static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
12363fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
12373fb4f7cdSSrinivas Pandruvada {
12383fb4f7cdSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
12393fb4f7cdSSrinivas Pandruvada 	int ret;
12403fb4f7cdSSrinivas Pandruvada 
12413fb4f7cdSSrinivas Pandruvada 	memset(&pkg_dev, 0, sizeof(pkg_dev));
12423fb4f7cdSSrinivas Pandruvada 	ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
12433fb4f7cdSSrinivas Pandruvada 	if (ret) {
1244ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, cpu);
1245ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1246ac9d05eaSSrinivas Pandruvada 		exit(1);
12473fb4f7cdSSrinivas Pandruvada 	} else {
12483fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
12493fb4f7cdSSrinivas Pandruvada 		isst_get_process_ctdp_complete(cpu, &pkg_dev);
12503fb4f7cdSSrinivas Pandruvada 	}
12513fb4f7cdSSrinivas Pandruvada }
12523fb4f7cdSSrinivas Pandruvada 
1253ce1326a2SPrarit Bhargava static void dump_isst_config(int arg)
12543fb4f7cdSSrinivas Pandruvada {
1255062e4aacSPrarit Bhargava 	void *fn;
1256062e4aacSPrarit Bhargava 
12573fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
12583fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12593fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology Performance profile configuration\n");
12603fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12613fb4f7cdSSrinivas Pandruvada 			"including base frequency and turbo frequency configurations\n");
12623fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
12633fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12643fb4f7cdSSrinivas Pandruvada 			"\tIf no arguments, dump information for all TDP levels\n");
12653fb4f7cdSSrinivas Pandruvada 		exit(0);
12663fb4f7cdSSrinivas Pandruvada 	}
12673fb4f7cdSSrinivas Pandruvada 
1268062e4aacSPrarit Bhargava 	if (!is_clx_n_platform())
1269062e4aacSPrarit Bhargava 		fn = dump_isst_config_for_cpu;
1270062e4aacSPrarit Bhargava 	else
1271062e4aacSPrarit Bhargava 		fn = dump_clx_n_config_for_cpu;
1272062e4aacSPrarit Bhargava 
12733fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
12743fb4f7cdSSrinivas Pandruvada 
12753fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
1276062e4aacSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
12773fb4f7cdSSrinivas Pandruvada 	else
1278062e4aacSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
12793fb4f7cdSSrinivas Pandruvada 
12803fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
12813fb4f7cdSSrinivas Pandruvada }
12823fb4f7cdSSrinivas Pandruvada 
1283f981dc17SSrinivas Pandruvada static void adjust_scaling_max_from_base_freq(int cpu);
1284f981dc17SSrinivas Pandruvada 
12853fb4f7cdSSrinivas Pandruvada static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
12863fb4f7cdSSrinivas Pandruvada 				  void *arg4)
12873fb4f7cdSSrinivas Pandruvada {
12883fb4f7cdSSrinivas Pandruvada 	int ret;
12893fb4f7cdSSrinivas Pandruvada 
12903fb4f7cdSSrinivas Pandruvada 	ret = isst_set_tdp_level(cpu, tdp_level);
1291ac9d05eaSSrinivas Pandruvada 	if (ret) {
1292ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1293ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1294ac9d05eaSSrinivas Pandruvada 		exit(1);
1295ac9d05eaSSrinivas Pandruvada 	} else {
12963fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
12973fb4f7cdSSrinivas Pandruvada 				    ret);
12983c64c81aSSrinivas Pandruvada 		if (force_online_offline) {
12993c64c81aSSrinivas Pandruvada 			struct isst_pkg_ctdp_level_info ctdp_level;
13003c64c81aSSrinivas Pandruvada 			int pkg_id = get_physical_package_id(cpu);
13013c64c81aSSrinivas Pandruvada 			int die_id = get_physical_die_id(cpu);
13023c64c81aSSrinivas Pandruvada 
1303f981dc17SSrinivas Pandruvada 			/* Wait for updated base frequencies */
1304f981dc17SSrinivas Pandruvada 			usleep(2000);
1305f981dc17SSrinivas Pandruvada 
13063c64c81aSSrinivas Pandruvada 			fprintf(stderr, "Option is set to online/offline\n");
13073c64c81aSSrinivas Pandruvada 			ctdp_level.core_cpumask_size =
13083c64c81aSSrinivas Pandruvada 				alloc_cpu_set(&ctdp_level.core_cpumask);
13096374de84SSrinivas Pandruvada 			ret = isst_get_coremask_info(cpu, tdp_level, &ctdp_level);
13106374de84SSrinivas Pandruvada 			if (ret) {
13116374de84SSrinivas Pandruvada 				isst_display_error_info_message(1, "Can't get coremask, online/offline option is ignored", 0, 0);
13126374de84SSrinivas Pandruvada 				return;
13136374de84SSrinivas Pandruvada 			}
13143c64c81aSSrinivas Pandruvada 			if (ctdp_level.cpu_count) {
13153c64c81aSSrinivas Pandruvada 				int i, max_cpus = get_topo_max_cpus();
13163c64c81aSSrinivas Pandruvada 				for (i = 0; i < max_cpus; ++i) {
13173c64c81aSSrinivas Pandruvada 					if (pkg_id != get_physical_package_id(i) || die_id != get_physical_die_id(i))
13183c64c81aSSrinivas Pandruvada 						continue;
13193c64c81aSSrinivas Pandruvada 					if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
13203c64c81aSSrinivas Pandruvada 						fprintf(stderr, "online cpu %d\n", i);
13213c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 1);
1322f981dc17SSrinivas Pandruvada 						adjust_scaling_max_from_base_freq(i);
13233c64c81aSSrinivas Pandruvada 					} else {
13243c64c81aSSrinivas Pandruvada 						fprintf(stderr, "offline cpu %d\n", i);
13253c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 0);
13263c64c81aSSrinivas Pandruvada 					}
13273c64c81aSSrinivas Pandruvada 				}
13283c64c81aSSrinivas Pandruvada 			}
13293c64c81aSSrinivas Pandruvada 		}
13303c64c81aSSrinivas Pandruvada 	}
13313fb4f7cdSSrinivas Pandruvada }
13323fb4f7cdSSrinivas Pandruvada 
1333ce1326a2SPrarit Bhargava static void set_tdp_level(int arg)
13343fb4f7cdSSrinivas Pandruvada {
13353fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
13363fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Set Config TDP level\n");
13373fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
13383fb4f7cdSSrinivas Pandruvada 			"\t Arguments: -l|--level : Specify tdp level\n");
13393c64c81aSSrinivas Pandruvada 		fprintf(stderr,
13403c64c81aSSrinivas Pandruvada 			"\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1341ac9d05eaSSrinivas Pandruvada 		fprintf(stderr,
1342ac9d05eaSSrinivas Pandruvada 			"\t  online/offline operation has limitations, refer to Linux hotplug documentation\n");
13433fb4f7cdSSrinivas Pandruvada 		exit(0);
13443fb4f7cdSSrinivas Pandruvada 	}
13453fb4f7cdSSrinivas Pandruvada 
13463fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1347ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
13483fb4f7cdSSrinivas Pandruvada 		exit(1);
13493fb4f7cdSSrinivas Pandruvada 	}
13503fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
13513fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
13523fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
13533fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
13543fb4f7cdSSrinivas Pandruvada 	else
13553fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
13563fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
13573fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
13583fb4f7cdSSrinivas Pandruvada }
13593fb4f7cdSSrinivas Pandruvada 
13601aa7177cSPrarit Bhargava static void clx_n_dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2,
13611aa7177cSPrarit Bhargava 				       void *arg3, void *arg4)
13621aa7177cSPrarit Bhargava {
13631aa7177cSPrarit Bhargava 	int ret;
13641aa7177cSPrarit Bhargava 
13651aa7177cSPrarit Bhargava 	ret = clx_n_config(cpu);
13661aa7177cSPrarit Bhargava 	if (ret) {
136739bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "clx_n_config failed", 0, 0);
13681aa7177cSPrarit Bhargava 	} else {
13691aa7177cSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
13701aa7177cSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
13711aa7177cSPrarit Bhargava 
13721aa7177cSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
13731aa7177cSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
13741aa7177cSPrarit Bhargava 		isst_pbf_display_information(cpu, outf, tdp_level, pbf_info);
13751aa7177cSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
13761aa7177cSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
13771aa7177cSPrarit Bhargava 	}
13781aa7177cSPrarit Bhargava }
13791aa7177cSPrarit Bhargava 
13803fb4f7cdSSrinivas Pandruvada static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
13813fb4f7cdSSrinivas Pandruvada 				    void *arg4)
13823fb4f7cdSSrinivas Pandruvada {
13833fb4f7cdSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
13843fb4f7cdSSrinivas Pandruvada 	int ret;
13853fb4f7cdSSrinivas Pandruvada 
13863fb4f7cdSSrinivas Pandruvada 	ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
13873fb4f7cdSSrinivas Pandruvada 	if (ret) {
138839bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get base-freq info at this level", 1, tdp_level);
138939bae0fcSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
139039bae0fcSSrinivas Pandruvada 		exit(1);
13913fb4f7cdSSrinivas Pandruvada 	} else {
13923fb4f7cdSSrinivas Pandruvada 		isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
13933fb4f7cdSSrinivas Pandruvada 		isst_get_pbf_info_complete(&pbf_info);
13943fb4f7cdSSrinivas Pandruvada 	}
13953fb4f7cdSSrinivas Pandruvada }
13963fb4f7cdSSrinivas Pandruvada 
1397ce1326a2SPrarit Bhargava static void dump_pbf_config(int arg)
13983fb4f7cdSSrinivas Pandruvada {
13991aa7177cSPrarit Bhargava 	void *fn;
14001aa7177cSPrarit Bhargava 
14013fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
14023fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
14033fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
14043fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
14053fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
14063fb4f7cdSSrinivas Pandruvada 		exit(0);
14073fb4f7cdSSrinivas Pandruvada 	}
14083fb4f7cdSSrinivas Pandruvada 
14093fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
141039bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
14113fb4f7cdSSrinivas Pandruvada 		exit(1);
14123fb4f7cdSSrinivas Pandruvada 	}
14133fb4f7cdSSrinivas Pandruvada 
14141aa7177cSPrarit Bhargava 	if (!is_clx_n_platform())
14151aa7177cSPrarit Bhargava 		fn = dump_pbf_config_for_cpu;
14163fb4f7cdSSrinivas Pandruvada 	else
14171aa7177cSPrarit Bhargava 		fn = clx_n_dump_pbf_config_for_cpu;
14181aa7177cSPrarit Bhargava 
14191aa7177cSPrarit Bhargava 	isst_ctdp_display_information_start(outf);
14201aa7177cSPrarit Bhargava 
14211aa7177cSPrarit Bhargava 	if (max_target_cpus)
14221aa7177cSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
14231aa7177cSPrarit Bhargava 	else
14241aa7177cSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
14251aa7177cSPrarit Bhargava 
14263fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
14273fb4f7cdSSrinivas Pandruvada }
14283fb4f7cdSSrinivas Pandruvada 
1429354bd06fSSrinivas Pandruvada static int set_clos_param(int cpu, int clos, int epp, int wt, int min, int max)
1430354bd06fSSrinivas Pandruvada {
1431354bd06fSSrinivas Pandruvada 	struct isst_clos_config clos_config;
1432354bd06fSSrinivas Pandruvada 	int ret;
1433354bd06fSSrinivas Pandruvada 
1434354bd06fSSrinivas Pandruvada 	ret = isst_pm_get_clos(cpu, clos, &clos_config);
1435354bd06fSSrinivas Pandruvada 	if (ret) {
1436fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
1437354bd06fSSrinivas Pandruvada 		return ret;
1438354bd06fSSrinivas Pandruvada 	}
1439354bd06fSSrinivas Pandruvada 	clos_config.clos_min = min;
1440354bd06fSSrinivas Pandruvada 	clos_config.clos_max = max;
1441354bd06fSSrinivas Pandruvada 	clos_config.epp = epp;
1442354bd06fSSrinivas Pandruvada 	clos_config.clos_prop_prio = wt;
1443354bd06fSSrinivas Pandruvada 	ret = isst_set_clos(cpu, clos, &clos_config);
1444354bd06fSSrinivas Pandruvada 	if (ret) {
1445fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
1446354bd06fSSrinivas Pandruvada 		return ret;
1447354bd06fSSrinivas Pandruvada 	}
1448354bd06fSSrinivas Pandruvada 
1449354bd06fSSrinivas Pandruvada 	return 0;
1450354bd06fSSrinivas Pandruvada }
1451354bd06fSSrinivas Pandruvada 
1452a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1453a9b2f8e2SSrinivas Pandruvada {
1454a9b2f8e2SSrinivas Pandruvada 	char buffer[128], freq_str[16];
1455a9b2f8e2SSrinivas Pandruvada 	int fd, ret, len;
1456a9b2f8e2SSrinivas Pandruvada 
1457a9b2f8e2SSrinivas Pandruvada 	if (max)
1458a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1459a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1460a9b2f8e2SSrinivas Pandruvada 	else
1461a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1462a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1463a9b2f8e2SSrinivas Pandruvada 
1464a9b2f8e2SSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1465a9b2f8e2SSrinivas Pandruvada 	if (fd < 0)
1466a9b2f8e2SSrinivas Pandruvada 		return fd;
1467a9b2f8e2SSrinivas Pandruvada 
1468a9b2f8e2SSrinivas Pandruvada 	snprintf(freq_str, sizeof(freq_str), "%d", freq);
1469a9b2f8e2SSrinivas Pandruvada 	len = strlen(freq_str);
1470a9b2f8e2SSrinivas Pandruvada 	ret = write(fd, freq_str, len);
1471a9b2f8e2SSrinivas Pandruvada 	if (ret == -1) {
1472a9b2f8e2SSrinivas Pandruvada 		close(fd);
1473a9b2f8e2SSrinivas Pandruvada 		return ret;
1474a9b2f8e2SSrinivas Pandruvada 	}
1475a9b2f8e2SSrinivas Pandruvada 	close(fd);
1476a9b2f8e2SSrinivas Pandruvada 
1477a9b2f8e2SSrinivas Pandruvada 	return 0;
1478a9b2f8e2SSrinivas Pandruvada }
1479a9b2f8e2SSrinivas Pandruvada 
1480f981dc17SSrinivas Pandruvada static int no_turbo(void)
1481f981dc17SSrinivas Pandruvada {
1482f981dc17SSrinivas Pandruvada 	return parse_int_file(0, "/sys/devices/system/cpu/intel_pstate/no_turbo");
1483f981dc17SSrinivas Pandruvada }
1484f981dc17SSrinivas Pandruvada 
1485f981dc17SSrinivas Pandruvada static void adjust_scaling_max_from_base_freq(int cpu)
1486f981dc17SSrinivas Pandruvada {
1487f981dc17SSrinivas Pandruvada 	int base_freq, scaling_max_freq;
1488f981dc17SSrinivas Pandruvada 
1489f981dc17SSrinivas Pandruvada 	scaling_max_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1490f981dc17SSrinivas Pandruvada 	base_freq = get_cpufreq_base_freq(cpu);
1491f981dc17SSrinivas Pandruvada 	if (scaling_max_freq < base_freq || no_turbo())
1492f981dc17SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(cpu, 1, base_freq);
1493f981dc17SSrinivas Pandruvada }
1494f981dc17SSrinivas Pandruvada 
1495bbaa2e95SSrinivas Pandruvada static void adjust_scaling_min_from_base_freq(int cpu)
1496bbaa2e95SSrinivas Pandruvada {
1497bbaa2e95SSrinivas Pandruvada 	int base_freq, scaling_min_freq;
1498bbaa2e95SSrinivas Pandruvada 
1499bbaa2e95SSrinivas Pandruvada 	scaling_min_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1500bbaa2e95SSrinivas Pandruvada 	base_freq = get_cpufreq_base_freq(cpu);
1501bbaa2e95SSrinivas Pandruvada 	if (scaling_min_freq < base_freq)
1502bbaa2e95SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(cpu, 0, base_freq);
1503bbaa2e95SSrinivas Pandruvada }
1504bbaa2e95SSrinivas Pandruvada 
1505a9b2f8e2SSrinivas Pandruvada static int set_clx_pbf_cpufreq_scaling_min_max(int cpu)
1506a9b2f8e2SSrinivas Pandruvada {
1507a9b2f8e2SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info *ctdp_level;
1508a9b2f8e2SSrinivas Pandruvada 	struct isst_pbf_info *pbf_info;
1509a9b2f8e2SSrinivas Pandruvada 	int i, pkg_id, die_id, freq, freq_high, freq_low;
1510a9b2f8e2SSrinivas Pandruvada 	int ret;
1511a9b2f8e2SSrinivas Pandruvada 
1512a9b2f8e2SSrinivas Pandruvada 	ret = clx_n_config(cpu);
1513a9b2f8e2SSrinivas Pandruvada 	if (ret) {
15147fc9fefdSSrinivas Pandruvada 		debug_printf("cpufreq_scaling_min_max failed for CLX");
1515a9b2f8e2SSrinivas Pandruvada 		return ret;
1516a9b2f8e2SSrinivas Pandruvada 	}
1517a9b2f8e2SSrinivas Pandruvada 
1518a9b2f8e2SSrinivas Pandruvada 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1519a9b2f8e2SSrinivas Pandruvada 	pbf_info = &ctdp_level->pbf_info;
1520a9b2f8e2SSrinivas Pandruvada 	freq_high = pbf_info->p1_high * 100000;
1521a9b2f8e2SSrinivas Pandruvada 	freq_low = pbf_info->p1_low * 100000;
1522a9b2f8e2SSrinivas Pandruvada 
1523a9b2f8e2SSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1524a9b2f8e2SSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1525a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1526a9b2f8e2SSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1527a9b2f8e2SSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1528a9b2f8e2SSrinivas Pandruvada 			continue;
1529a9b2f8e2SSrinivas Pandruvada 
1530a9b2f8e2SSrinivas Pandruvada 		if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1531a9b2f8e2SSrinivas Pandruvada 				  pbf_info->core_cpumask))
1532a9b2f8e2SSrinivas Pandruvada 			freq = freq_high;
1533a9b2f8e2SSrinivas Pandruvada 		else
1534a9b2f8e2SSrinivas Pandruvada 			freq = freq_low;
1535a9b2f8e2SSrinivas Pandruvada 
1536a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 1, freq);
1537a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 0, freq);
1538a9b2f8e2SSrinivas Pandruvada 	}
1539a9b2f8e2SSrinivas Pandruvada 
1540a9b2f8e2SSrinivas Pandruvada 	return 0;
1541a9b2f8e2SSrinivas Pandruvada }
1542a9b2f8e2SSrinivas Pandruvada 
1543a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1544354bd06fSSrinivas Pandruvada {
1545354bd06fSSrinivas Pandruvada 	char buffer[128], min_freq[16];
1546354bd06fSSrinivas Pandruvada 	int fd, ret, len;
1547354bd06fSSrinivas Pandruvada 
1548354bd06fSSrinivas Pandruvada 	if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1549354bd06fSSrinivas Pandruvada 		return -1;
1550354bd06fSSrinivas Pandruvada 
1551a9b2f8e2SSrinivas Pandruvada 	if (cpuinfo_max)
1552354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1553354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1554354bd06fSSrinivas Pandruvada 	else
1555354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1556354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1557354bd06fSSrinivas Pandruvada 
1558354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_RDONLY);
1559354bd06fSSrinivas Pandruvada 	if (fd < 0)
1560354bd06fSSrinivas Pandruvada 		return fd;
1561354bd06fSSrinivas Pandruvada 
1562354bd06fSSrinivas Pandruvada 	len = read(fd, min_freq, sizeof(min_freq));
1563354bd06fSSrinivas Pandruvada 	close(fd);
1564354bd06fSSrinivas Pandruvada 
1565354bd06fSSrinivas Pandruvada 	if (len < 0)
1566354bd06fSSrinivas Pandruvada 		return len;
1567354bd06fSSrinivas Pandruvada 
1568a9b2f8e2SSrinivas Pandruvada 	if (scaling_max)
1569a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1570a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1571a9b2f8e2SSrinivas Pandruvada 	else
1572354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1573354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1574354bd06fSSrinivas Pandruvada 
1575354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1576354bd06fSSrinivas Pandruvada 	if (fd < 0)
1577354bd06fSSrinivas Pandruvada 		return fd;
1578354bd06fSSrinivas Pandruvada 
1579354bd06fSSrinivas Pandruvada 	len = strlen(min_freq);
1580354bd06fSSrinivas Pandruvada 	ret = write(fd, min_freq, len);
1581354bd06fSSrinivas Pandruvada 	if (ret == -1) {
1582354bd06fSSrinivas Pandruvada 		close(fd);
1583354bd06fSSrinivas Pandruvada 		return ret;
1584354bd06fSSrinivas Pandruvada 	}
1585354bd06fSSrinivas Pandruvada 	close(fd);
1586354bd06fSSrinivas Pandruvada 
1587354bd06fSSrinivas Pandruvada 	return 0;
1588354bd06fSSrinivas Pandruvada }
1589354bd06fSSrinivas Pandruvada 
1590354bd06fSSrinivas Pandruvada static void set_scaling_min_to_cpuinfo_max(int cpu)
1591354bd06fSSrinivas Pandruvada {
1592354bd06fSSrinivas Pandruvada 	int i, pkg_id, die_id;
1593354bd06fSSrinivas Pandruvada 
1594354bd06fSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1595354bd06fSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1596354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1597354bd06fSSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1598354bd06fSSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1599354bd06fSSrinivas Pandruvada 			continue;
1600354bd06fSSrinivas Pandruvada 
16019734213eSSrinivas Pandruvada 		adjust_scaling_max_from_base_freq(i);
1602a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1603bbaa2e95SSrinivas Pandruvada 		adjust_scaling_min_from_base_freq(i);
1604354bd06fSSrinivas Pandruvada 	}
1605354bd06fSSrinivas Pandruvada }
1606354bd06fSSrinivas Pandruvada 
1607354bd06fSSrinivas Pandruvada static void set_scaling_min_to_cpuinfo_min(int cpu)
1608354bd06fSSrinivas Pandruvada {
1609354bd06fSSrinivas Pandruvada 	int i, pkg_id, die_id;
1610354bd06fSSrinivas Pandruvada 
1611354bd06fSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1612354bd06fSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1613354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1614354bd06fSSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1615354bd06fSSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1616354bd06fSSrinivas Pandruvada 			continue;
1617354bd06fSSrinivas Pandruvada 
16189734213eSSrinivas Pandruvada 		adjust_scaling_max_from_base_freq(i);
1619a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1620a9b2f8e2SSrinivas Pandruvada 	}
1621a9b2f8e2SSrinivas Pandruvada }
1622a9b2f8e2SSrinivas Pandruvada 
1623a9b2f8e2SSrinivas Pandruvada static void set_scaling_max_to_cpuinfo_max(int cpu)
1624a9b2f8e2SSrinivas Pandruvada {
1625a9b2f8e2SSrinivas Pandruvada 	int i, pkg_id, die_id;
1626a9b2f8e2SSrinivas Pandruvada 
1627a9b2f8e2SSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1628a9b2f8e2SSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1629a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1630a9b2f8e2SSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1631a9b2f8e2SSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1632a9b2f8e2SSrinivas Pandruvada 			continue;
1633a9b2f8e2SSrinivas Pandruvada 
1634a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1635354bd06fSSrinivas Pandruvada 	}
1636354bd06fSSrinivas Pandruvada }
1637354bd06fSSrinivas Pandruvada 
1638354bd06fSSrinivas Pandruvada static int set_core_priority_and_min(int cpu, int mask_size,
1639354bd06fSSrinivas Pandruvada 				     cpu_set_t *cpu_mask, int min_high,
1640354bd06fSSrinivas Pandruvada 				     int min_low)
1641354bd06fSSrinivas Pandruvada {
1642354bd06fSSrinivas Pandruvada 	int pkg_id, die_id, ret, i;
1643354bd06fSSrinivas Pandruvada 
1644354bd06fSSrinivas Pandruvada 	if (!CPU_COUNT_S(mask_size, cpu_mask))
1645354bd06fSSrinivas Pandruvada 		return -1;
1646354bd06fSSrinivas Pandruvada 
1647354bd06fSSrinivas Pandruvada 	ret = set_clos_param(cpu, 0, 0, 0, min_high, 0xff);
1648354bd06fSSrinivas Pandruvada 	if (ret)
1649354bd06fSSrinivas Pandruvada 		return ret;
1650354bd06fSSrinivas Pandruvada 
165121c3390dSSrinivas Pandruvada 	ret = set_clos_param(cpu, 1, 15, 15, min_low, 0xff);
1652354bd06fSSrinivas Pandruvada 	if (ret)
1653354bd06fSSrinivas Pandruvada 		return ret;
1654354bd06fSSrinivas Pandruvada 
165521c3390dSSrinivas Pandruvada 	ret = set_clos_param(cpu, 2, 15, 15, min_low, 0xff);
1656354bd06fSSrinivas Pandruvada 	if (ret)
1657354bd06fSSrinivas Pandruvada 		return ret;
1658354bd06fSSrinivas Pandruvada 
165921c3390dSSrinivas Pandruvada 	ret = set_clos_param(cpu, 3, 15, 15, min_low, 0xff);
1660354bd06fSSrinivas Pandruvada 	if (ret)
1661354bd06fSSrinivas Pandruvada 		return ret;
1662354bd06fSSrinivas Pandruvada 
1663354bd06fSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1664354bd06fSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1665354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1666354bd06fSSrinivas Pandruvada 		int clos;
1667354bd06fSSrinivas Pandruvada 
1668354bd06fSSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1669354bd06fSSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1670354bd06fSSrinivas Pandruvada 			continue;
1671354bd06fSSrinivas Pandruvada 
1672354bd06fSSrinivas Pandruvada 		if (CPU_ISSET_S(i, mask_size, cpu_mask))
1673354bd06fSSrinivas Pandruvada 			clos = 0;
1674354bd06fSSrinivas Pandruvada 		else
1675354bd06fSSrinivas Pandruvada 			clos = 3;
1676354bd06fSSrinivas Pandruvada 
1677354bd06fSSrinivas Pandruvada 		debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1678354bd06fSSrinivas Pandruvada 		ret = isst_clos_associate(i, clos);
1679354bd06fSSrinivas Pandruvada 		if (ret) {
1680fe6fb216SSrinivas Pandruvada 			isst_display_error_info_message(1, "isst_clos_associate failed", 0, 0);
1681354bd06fSSrinivas Pandruvada 			return ret;
1682354bd06fSSrinivas Pandruvada 		}
1683354bd06fSSrinivas Pandruvada 	}
1684354bd06fSSrinivas Pandruvada 
1685354bd06fSSrinivas Pandruvada 	return 0;
1686354bd06fSSrinivas Pandruvada }
1687354bd06fSSrinivas Pandruvada 
1688354bd06fSSrinivas Pandruvada static int set_pbf_core_power(int cpu)
1689354bd06fSSrinivas Pandruvada {
1690354bd06fSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
1691354bd06fSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
1692354bd06fSSrinivas Pandruvada 	int ret;
1693354bd06fSSrinivas Pandruvada 
1694354bd06fSSrinivas Pandruvada 	ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1695354bd06fSSrinivas Pandruvada 	if (ret) {
1696fe6fb216SSrinivas Pandruvada 		debug_printf("isst_get_ctdp_levels failed");
1697354bd06fSSrinivas Pandruvada 		return ret;
1698354bd06fSSrinivas Pandruvada 	}
1699354bd06fSSrinivas Pandruvada 	debug_printf("Current_level: %d\n", pkg_dev.current_level);
1700354bd06fSSrinivas Pandruvada 
1701354bd06fSSrinivas Pandruvada 	ret = isst_get_pbf_info(cpu, pkg_dev.current_level, &pbf_info);
1702354bd06fSSrinivas Pandruvada 	if (ret) {
1703fe6fb216SSrinivas Pandruvada 		debug_printf("isst_get_pbf_info failed");
1704354bd06fSSrinivas Pandruvada 		return ret;
1705354bd06fSSrinivas Pandruvada 	}
1706354bd06fSSrinivas Pandruvada 	debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1707354bd06fSSrinivas Pandruvada 		     pbf_info.p1_low);
1708354bd06fSSrinivas Pandruvada 
1709354bd06fSSrinivas Pandruvada 	ret = set_core_priority_and_min(cpu, pbf_info.core_cpumask_size,
1710354bd06fSSrinivas Pandruvada 					pbf_info.core_cpumask,
1711354bd06fSSrinivas Pandruvada 					pbf_info.p1_high, pbf_info.p1_low);
1712354bd06fSSrinivas Pandruvada 	if (ret) {
1713fe6fb216SSrinivas Pandruvada 		debug_printf("set_core_priority_and_min failed");
1714354bd06fSSrinivas Pandruvada 		return ret;
1715354bd06fSSrinivas Pandruvada 	}
1716354bd06fSSrinivas Pandruvada 
1717354bd06fSSrinivas Pandruvada 	ret = isst_pm_qos_config(cpu, 1, 1);
1718354bd06fSSrinivas Pandruvada 	if (ret) {
1719fe6fb216SSrinivas Pandruvada 		debug_printf("isst_pm_qos_config failed");
1720354bd06fSSrinivas Pandruvada 		return ret;
1721354bd06fSSrinivas Pandruvada 	}
1722354bd06fSSrinivas Pandruvada 
1723354bd06fSSrinivas Pandruvada 	return 0;
1724354bd06fSSrinivas Pandruvada }
1725354bd06fSSrinivas Pandruvada 
17263fb4f7cdSSrinivas Pandruvada static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
17273fb4f7cdSSrinivas Pandruvada 			    void *arg4)
17283fb4f7cdSSrinivas Pandruvada {
17293d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
17303d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
17313fb4f7cdSSrinivas Pandruvada 	int ret;
17323fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
17333fb4f7cdSSrinivas Pandruvada 
17341aa7177cSPrarit Bhargava 	if (is_clx_n_platform()) {
17351aa7177cSPrarit Bhargava 		ret = 0;
17367fc9fefdSSrinivas Pandruvada 		if (status) {
1737a9b2f8e2SSrinivas Pandruvada 			set_clx_pbf_cpufreq_scaling_min_max(cpu);
1738a9b2f8e2SSrinivas Pandruvada 
1739a9b2f8e2SSrinivas Pandruvada 		} else {
1740a9b2f8e2SSrinivas Pandruvada 			set_scaling_max_to_cpuinfo_max(cpu);
1741a9b2f8e2SSrinivas Pandruvada 			set_scaling_min_to_cpuinfo_min(cpu);
1742a9b2f8e2SSrinivas Pandruvada 		}
17431aa7177cSPrarit Bhargava 		goto disp_result;
17441aa7177cSPrarit Bhargava 	}
17451aa7177cSPrarit Bhargava 
17463d904f06SSrinivas Pandruvada 	ret = isst_get_ctdp_levels(cpu, &pkg_dev);
17473d904f06SSrinivas Pandruvada 	if (ret) {
17483d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
17493d904f06SSrinivas Pandruvada 		goto disp_result;
17503d904f06SSrinivas Pandruvada 	}
17513d904f06SSrinivas Pandruvada 
17523d904f06SSrinivas Pandruvada 	ret = isst_get_ctdp_control(cpu, pkg_dev.current_level, &ctdp_level);
17533d904f06SSrinivas Pandruvada 	if (ret) {
17543d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get current level", 0, 0);
17553d904f06SSrinivas Pandruvada 		goto disp_result;
17563d904f06SSrinivas Pandruvada 	}
17573d904f06SSrinivas Pandruvada 
17583d904f06SSrinivas Pandruvada 	if (!ctdp_level.pbf_support) {
17593d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "base-freq feature is not present at this level", 1, pkg_dev.current_level);
17603d904f06SSrinivas Pandruvada 		ret = -1;
17613d904f06SSrinivas Pandruvada 		goto disp_result;
17623d904f06SSrinivas Pandruvada 	}
17633d904f06SSrinivas Pandruvada 
1764097a5222SSrinivas Pandruvada 	if (auto_mode && status) {
1765354bd06fSSrinivas Pandruvada 		ret = set_pbf_core_power(cpu);
1766354bd06fSSrinivas Pandruvada 		if (ret)
1767354bd06fSSrinivas Pandruvada 			goto disp_result;
1768354bd06fSSrinivas Pandruvada 	}
1769354bd06fSSrinivas Pandruvada 
17703fb4f7cdSSrinivas Pandruvada 	ret = isst_set_pbf_fact_status(cpu, 1, status);
17713fb4f7cdSSrinivas Pandruvada 	if (ret) {
177239bae0fcSSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1773354bd06fSSrinivas Pandruvada 		if (auto_mode)
1774354bd06fSSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
17753fb4f7cdSSrinivas Pandruvada 	} else {
1776354bd06fSSrinivas Pandruvada 		if (auto_mode) {
1777354bd06fSSrinivas Pandruvada 			if (status)
1778354bd06fSSrinivas Pandruvada 				set_scaling_min_to_cpuinfo_max(cpu);
1779354bd06fSSrinivas Pandruvada 			else
1780354bd06fSSrinivas Pandruvada 				set_scaling_min_to_cpuinfo_min(cpu);
1781354bd06fSSrinivas Pandruvada 		}
1782354bd06fSSrinivas Pandruvada 	}
1783354bd06fSSrinivas Pandruvada 
1784097a5222SSrinivas Pandruvada 	if (auto_mode && !status)
1785fe6fb216SSrinivas Pandruvada 		isst_pm_qos_config(cpu, 0, 1);
1786097a5222SSrinivas Pandruvada 
1787354bd06fSSrinivas Pandruvada disp_result:
17883fb4f7cdSSrinivas Pandruvada 	if (status)
17893fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "base-freq", "enable",
17903fb4f7cdSSrinivas Pandruvada 				    ret);
17913fb4f7cdSSrinivas Pandruvada 	else
17923fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "base-freq", "disable",
17933fb4f7cdSSrinivas Pandruvada 				    ret);
17943fb4f7cdSSrinivas Pandruvada }
17953fb4f7cdSSrinivas Pandruvada 
1796ce1326a2SPrarit Bhargava static void set_pbf_enable(int arg)
17973fb4f7cdSSrinivas Pandruvada {
1798ce1326a2SPrarit Bhargava 	int enable = arg;
17993fb4f7cdSSrinivas Pandruvada 
18003fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1801ce1326a2SPrarit Bhargava 		if (enable) {
18023fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1803354bd06fSSrinivas Pandruvada 				"Enable Intel Speed Select Technology base frequency feature\n");
180439bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
180539bae0fcSSrinivas Pandruvada 				fprintf(stderr,
180639bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't enable feature in the hardware.\n");
180739bae0fcSSrinivas Pandruvada 				fprintf(stderr,
180839bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpufreq base_frequency.\n");
180939bae0fcSSrinivas Pandruvada 				exit(0);
181039bae0fcSSrinivas Pandruvada 
181139bae0fcSSrinivas Pandruvada 			}
1812354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1813354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1814ce1326a2SPrarit Bhargava 		} else {
1815354bd06fSSrinivas Pandruvada 
181639bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
181739bae0fcSSrinivas Pandruvada 				fprintf(stderr,
181839bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't disable feature in the hardware.\n");
181939bae0fcSSrinivas Pandruvada 				fprintf(stderr,
182039bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpuinfo_min_freq\n");
182139bae0fcSSrinivas Pandruvada 				exit(0);
182239bae0fcSSrinivas Pandruvada 			}
18233fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1824354bd06fSSrinivas Pandruvada 				"Disable Intel Speed Select Technology base frequency feature\n");
1825354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1826354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1827ce1326a2SPrarit Bhargava 		}
18283fb4f7cdSSrinivas Pandruvada 		exit(0);
18293fb4f7cdSSrinivas Pandruvada 	}
18303fb4f7cdSSrinivas Pandruvada 
18313fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
18323fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
18333fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1834ce1326a2SPrarit Bhargava 						  NULL, &enable);
18353fb4f7cdSSrinivas Pandruvada 	else
18363fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1837ce1326a2SPrarit Bhargava 					       NULL, &enable);
18383fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
18393fb4f7cdSSrinivas Pandruvada }
18403fb4f7cdSSrinivas Pandruvada 
18413fb4f7cdSSrinivas Pandruvada static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
18423fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
18433fb4f7cdSSrinivas Pandruvada {
18443fb4f7cdSSrinivas Pandruvada 	struct isst_fact_info fact_info;
18453fb4f7cdSSrinivas Pandruvada 	int ret;
18463fb4f7cdSSrinivas Pandruvada 
1847a9fd6ae7SSrinivas Pandruvada 	ret = isst_get_fact_info(cpu, tdp_level, fact_bucket, &fact_info);
1848a9fd6ae7SSrinivas Pandruvada 	if (ret) {
1849a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get turbo-freq info at this level", 1, tdp_level);
1850a9fd6ae7SSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1851a9fd6ae7SSrinivas Pandruvada 		exit(1);
1852a9fd6ae7SSrinivas Pandruvada 	} else {
18533fb4f7cdSSrinivas Pandruvada 		isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
18543fb4f7cdSSrinivas Pandruvada 					      fact_avx, &fact_info);
18553fb4f7cdSSrinivas Pandruvada 	}
1856a9fd6ae7SSrinivas Pandruvada }
18573fb4f7cdSSrinivas Pandruvada 
1858ce1326a2SPrarit Bhargava static void dump_fact_config(int arg)
18593fb4f7cdSSrinivas Pandruvada {
18603fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
18613fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18623fb4f7cdSSrinivas Pandruvada 			"Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
18633fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18643fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
18653fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18663fb4f7cdSSrinivas Pandruvada 			"\tArguments: -b|--bucket : Bucket index to dump\n");
18673fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18683fb4f7cdSSrinivas Pandruvada 			"\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
18693fb4f7cdSSrinivas Pandruvada 		exit(0);
18703fb4f7cdSSrinivas Pandruvada 	}
18713fb4f7cdSSrinivas Pandruvada 
18723fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1873a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level\n", 0, 0);
18743fb4f7cdSSrinivas Pandruvada 		exit(1);
18753fb4f7cdSSrinivas Pandruvada 	}
18763fb4f7cdSSrinivas Pandruvada 
18773fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
18783fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
18793fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
18803fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
18813fb4f7cdSSrinivas Pandruvada 	else
18823fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
18833fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
18843fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
18853fb4f7cdSSrinivas Pandruvada }
18863fb4f7cdSSrinivas Pandruvada 
18873fb4f7cdSSrinivas Pandruvada static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
18883fb4f7cdSSrinivas Pandruvada 			     void *arg4)
18893fb4f7cdSSrinivas Pandruvada {
18903d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
18913d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
18923fb4f7cdSSrinivas Pandruvada 	int ret;
18933fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
18943fb4f7cdSSrinivas Pandruvada 
18953d904f06SSrinivas Pandruvada 	ret = isst_get_ctdp_levels(cpu, &pkg_dev);
18963d904f06SSrinivas Pandruvada 	if (ret) {
18973d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
18983d904f06SSrinivas Pandruvada 		goto disp_results;
18993d904f06SSrinivas Pandruvada 	}
19003d904f06SSrinivas Pandruvada 
19013d904f06SSrinivas Pandruvada 	ret = isst_get_ctdp_control(cpu, pkg_dev.current_level, &ctdp_level);
19023d904f06SSrinivas Pandruvada 	if (ret) {
19033d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get current level", 0, 0);
19043d904f06SSrinivas Pandruvada 		goto disp_results;
19053d904f06SSrinivas Pandruvada 	}
19063d904f06SSrinivas Pandruvada 
19073d904f06SSrinivas Pandruvada 	if (!ctdp_level.fact_support) {
19083d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "turbo-freq feature is not present at this level", 1, pkg_dev.current_level);
19093d904f06SSrinivas Pandruvada 		ret = -1;
19103d904f06SSrinivas Pandruvada 		goto disp_results;
19113d904f06SSrinivas Pandruvada 	}
19123d904f06SSrinivas Pandruvada 
19137983ed6fSSrinivas Pandruvada 	if (status) {
1914a6a82f9bSSrinivas Pandruvada 		ret = isst_pm_qos_config(cpu, 1, 1);
19153fb4f7cdSSrinivas Pandruvada 		if (ret)
1916a6a82f9bSSrinivas Pandruvada 			goto disp_results;
1917a6a82f9bSSrinivas Pandruvada 	}
1918a6a82f9bSSrinivas Pandruvada 
1919a6a82f9bSSrinivas Pandruvada 	ret = isst_set_pbf_fact_status(cpu, 0, status);
1920a6a82f9bSSrinivas Pandruvada 	if (ret) {
1921a9fd6ae7SSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1922a6a82f9bSSrinivas Pandruvada 		if (auto_mode)
1923a6a82f9bSSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
1924a6a82f9bSSrinivas Pandruvada 
1925a6a82f9bSSrinivas Pandruvada 		goto disp_results;
1926a6a82f9bSSrinivas Pandruvada 	}
1927a6a82f9bSSrinivas Pandruvada 
1928a6a82f9bSSrinivas Pandruvada 	/* Set TRL */
19293fb4f7cdSSrinivas Pandruvada 	if (status) {
19303fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp pkg_dev;
19313fb4f7cdSSrinivas Pandruvada 
19323fb4f7cdSSrinivas Pandruvada 		ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1933a6a82f9bSSrinivas Pandruvada 		if (!ret)
19343fb4f7cdSSrinivas Pandruvada 			ret = isst_set_trl(cpu, fact_trl);
1935a6a82f9bSSrinivas Pandruvada 		if (ret && auto_mode)
1936a6a82f9bSSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
1937097a5222SSrinivas Pandruvada 	} else {
1938097a5222SSrinivas Pandruvada 		if (auto_mode)
1939097a5222SSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
1940a6a82f9bSSrinivas Pandruvada 	}
1941a6a82f9bSSrinivas Pandruvada 
1942a6a82f9bSSrinivas Pandruvada disp_results:
1943a6a82f9bSSrinivas Pandruvada 	if (status) {
1944a6a82f9bSSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-freq", "enable", ret);
194514a8aa49SSrinivas Pandruvada 		if (ret)
194614a8aa49SSrinivas Pandruvada 			fact_enable_fail = ret;
19473fb4f7cdSSrinivas Pandruvada 	} else {
19483fb4f7cdSSrinivas Pandruvada 		/* Since we modified TRL during Fact enable, restore it */
19493fb4f7cdSSrinivas Pandruvada 		isst_set_trl_from_current_tdp(cpu, fact_trl);
1950a6a82f9bSSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-freq", "disable", ret);
19513fb4f7cdSSrinivas Pandruvada 	}
19523fb4f7cdSSrinivas Pandruvada }
19533fb4f7cdSSrinivas Pandruvada 
1954ce1326a2SPrarit Bhargava static void set_fact_enable(int arg)
19553fb4f7cdSSrinivas Pandruvada {
1956ce1326a2SPrarit Bhargava 	int i, ret, enable = arg;
19573fb4f7cdSSrinivas Pandruvada 
19583fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1959ce1326a2SPrarit Bhargava 		if (enable) {
19603fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
19613fb4f7cdSSrinivas Pandruvada 				"Enable Intel Speed Select Technology Turbo frequency feature\n");
19623fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
19633fb4f7cdSSrinivas Pandruvada 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1964a6a82f9bSSrinivas Pandruvada 			fprintf(stderr,
1965a6a82f9bSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1966ce1326a2SPrarit Bhargava 			fprintf(stderr,
1967ce1326a2SPrarit Bhargava 				"-C|--cpu option as as high priority using core-power feature\n");
1968ce1326a2SPrarit Bhargava 		} else {
1969ce1326a2SPrarit Bhargava 			fprintf(stderr,
1970ce1326a2SPrarit Bhargava 				"Disable Intel Speed Select Technology turbo frequency feature\n");
1971ce1326a2SPrarit Bhargava 			fprintf(stderr,
1972ce1326a2SPrarit Bhargava 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1973ce1326a2SPrarit Bhargava 			fprintf(stderr,
1974ce1326a2SPrarit Bhargava 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1975ce1326a2SPrarit Bhargava 		}
19763fb4f7cdSSrinivas Pandruvada 		exit(0);
19773fb4f7cdSSrinivas Pandruvada 	}
19783fb4f7cdSSrinivas Pandruvada 
19793fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
19803fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
19813fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1982ce1326a2SPrarit Bhargava 						  NULL, &enable);
19833fb4f7cdSSrinivas Pandruvada 	else
19843fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
1985ce1326a2SPrarit Bhargava 					       NULL, &enable);
19863fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
1987a6a82f9bSSrinivas Pandruvada 
198814a8aa49SSrinivas Pandruvada 	if (!fact_enable_fail && enable && auto_mode) {
1989a6a82f9bSSrinivas Pandruvada 		/*
1990a6a82f9bSSrinivas Pandruvada 		 * When we adjust CLOS param, we have to set for siblings also.
1991a6a82f9bSSrinivas Pandruvada 		 * So for the each user specified CPU, also add the sibling
1992a6a82f9bSSrinivas Pandruvada 		 * in the present_cpu_mask.
1993a6a82f9bSSrinivas Pandruvada 		 */
1994a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
1995a6a82f9bSSrinivas Pandruvada 			char buffer[128], sibling_list[128], *cpu_str;
1996a6a82f9bSSrinivas Pandruvada 			int fd, len;
1997a6a82f9bSSrinivas Pandruvada 
1998a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1999a6a82f9bSSrinivas Pandruvada 				continue;
2000a6a82f9bSSrinivas Pandruvada 
2001a6a82f9bSSrinivas Pandruvada 			snprintf(buffer, sizeof(buffer),
2002a6a82f9bSSrinivas Pandruvada 				 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
2003a6a82f9bSSrinivas Pandruvada 
2004a6a82f9bSSrinivas Pandruvada 			fd = open(buffer, O_RDONLY);
2005a6a82f9bSSrinivas Pandruvada 			if (fd < 0)
2006a6a82f9bSSrinivas Pandruvada 				continue;
2007a6a82f9bSSrinivas Pandruvada 
2008a6a82f9bSSrinivas Pandruvada 			len = read(fd, sibling_list, sizeof(sibling_list));
2009a6a82f9bSSrinivas Pandruvada 			close(fd);
2010a6a82f9bSSrinivas Pandruvada 
2011a6a82f9bSSrinivas Pandruvada 			if (len < 0)
2012a6a82f9bSSrinivas Pandruvada 				continue;
2013a6a82f9bSSrinivas Pandruvada 
2014a6a82f9bSSrinivas Pandruvada 			cpu_str = strtok(sibling_list, ",");
2015a6a82f9bSSrinivas Pandruvada 			while (cpu_str != NULL) {
2016a6a82f9bSSrinivas Pandruvada 				int cpu;
2017a6a82f9bSSrinivas Pandruvada 
2018a6a82f9bSSrinivas Pandruvada 				sscanf(cpu_str, "%d", &cpu);
2019a6a82f9bSSrinivas Pandruvada 				CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
2020a6a82f9bSSrinivas Pandruvada 				cpu_str = strtok(NULL, ",");
2021a6a82f9bSSrinivas Pandruvada 			}
2022a6a82f9bSSrinivas Pandruvada 		}
2023a6a82f9bSSrinivas Pandruvada 
2024a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
2025a6a82f9bSSrinivas Pandruvada 			int clos;
2026a6a82f9bSSrinivas Pandruvada 
2027a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
2028a6a82f9bSSrinivas Pandruvada 				continue;
2029a6a82f9bSSrinivas Pandruvada 
2030a6a82f9bSSrinivas Pandruvada 			ret = set_clos_param(i, 0, 0, 0, 0, 0xff);
2031a6a82f9bSSrinivas Pandruvada 			if (ret)
2032a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2033a6a82f9bSSrinivas Pandruvada 
203421c3390dSSrinivas Pandruvada 			ret = set_clos_param(i, 1, 15, 15, 0, 0xff);
2035a6a82f9bSSrinivas Pandruvada 			if (ret)
2036a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2037a6a82f9bSSrinivas Pandruvada 
203821c3390dSSrinivas Pandruvada 			ret = set_clos_param(i, 2, 15, 15, 0, 0xff);
2039a6a82f9bSSrinivas Pandruvada 			if (ret)
2040a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2041a6a82f9bSSrinivas Pandruvada 
204221c3390dSSrinivas Pandruvada 			ret = set_clos_param(i, 3, 15, 15, 0, 0xff);
2043a6a82f9bSSrinivas Pandruvada 			if (ret)
2044a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2045a6a82f9bSSrinivas Pandruvada 
2046a6a82f9bSSrinivas Pandruvada 			if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
2047a6a82f9bSSrinivas Pandruvada 				clos = 0;
2048a6a82f9bSSrinivas Pandruvada 			else
2049a6a82f9bSSrinivas Pandruvada 				clos = 3;
2050a6a82f9bSSrinivas Pandruvada 
2051a6a82f9bSSrinivas Pandruvada 			debug_printf("Associate cpu: %d clos: %d\n", i, clos);
2052a6a82f9bSSrinivas Pandruvada 			ret = isst_clos_associate(i, clos);
2053a6a82f9bSSrinivas Pandruvada 			if (ret)
2054a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2055a6a82f9bSSrinivas Pandruvada 		}
205678e77b7dSSrinivas Pandruvada 		isst_display_result(-1, outf, "turbo-freq --auto", "enable", 0);
2057a6a82f9bSSrinivas Pandruvada 	}
2058a6a82f9bSSrinivas Pandruvada 
2059a6a82f9bSSrinivas Pandruvada 	return;
2060a6a82f9bSSrinivas Pandruvada 
2061a6a82f9bSSrinivas Pandruvada error_disp:
2062a6a82f9bSSrinivas Pandruvada 	isst_display_result(i, outf, "turbo-freq --auto", "enable", ret);
2063a6a82f9bSSrinivas Pandruvada 
20643fb4f7cdSSrinivas Pandruvada }
20653fb4f7cdSSrinivas Pandruvada 
20663fb4f7cdSSrinivas Pandruvada static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
20673fb4f7cdSSrinivas Pandruvada 				   void *arg4)
20683fb4f7cdSSrinivas Pandruvada {
20693fb4f7cdSSrinivas Pandruvada 	int ret;
20703fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
20713fb4f7cdSSrinivas Pandruvada 
2072fe6fb216SSrinivas Pandruvada 	if (is_skx_based_platform())
2073fe6fb216SSrinivas Pandruvada 		clos_priority_type = 1;
2074fe6fb216SSrinivas Pandruvada 
20753fb4f7cdSSrinivas Pandruvada 	ret = isst_pm_qos_config(cpu, status, clos_priority_type);
2076a6a82f9bSSrinivas Pandruvada 	if (ret)
2077fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_qos_config failed", 0, 0);
2078a6a82f9bSSrinivas Pandruvada 
20793fb4f7cdSSrinivas Pandruvada 	if (status)
20803fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "enable",
20813fb4f7cdSSrinivas Pandruvada 				    ret);
20823fb4f7cdSSrinivas Pandruvada 	else
20833fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "disable",
20843fb4f7cdSSrinivas Pandruvada 				    ret);
20853fb4f7cdSSrinivas Pandruvada }
20863fb4f7cdSSrinivas Pandruvada 
2087ce1326a2SPrarit Bhargava static void set_clos_enable(int arg)
20883fb4f7cdSSrinivas Pandruvada {
2089ce1326a2SPrarit Bhargava 	int enable = arg;
20903fb4f7cdSSrinivas Pandruvada 
20913fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
2092ce1326a2SPrarit Bhargava 		if (enable) {
2093ce1326a2SPrarit Bhargava 			fprintf(stderr,
2094ce1326a2SPrarit Bhargava 				"Enable core-power for a package/die\n");
2095fe6fb216SSrinivas Pandruvada 			if (!is_skx_based_platform()) {
20963fb4f7cdSSrinivas Pandruvada 				fprintf(stderr,
20973fb4f7cdSSrinivas Pandruvada 					"\tClos Enable: Specify priority type with [--priority|-p]\n");
20983fb4f7cdSSrinivas Pandruvada 				fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
2099fe6fb216SSrinivas Pandruvada 			}
2100ce1326a2SPrarit Bhargava 		} else {
2101ce1326a2SPrarit Bhargava 			fprintf(stderr,
2102ce1326a2SPrarit Bhargava 				"Disable core-power: [No command arguments are required]\n");
2103ce1326a2SPrarit Bhargava 		}
21043fb4f7cdSSrinivas Pandruvada 		exit(0);
21053fb4f7cdSSrinivas Pandruvada 	}
21063fb4f7cdSSrinivas Pandruvada 
2107ce1326a2SPrarit Bhargava 	if (enable && cpufreq_sysfs_present()) {
21083fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21093fb4f7cdSSrinivas Pandruvada 			"cpufreq subsystem and core-power enable will interfere with each other!\n");
21103fb4f7cdSSrinivas Pandruvada 	}
21113fb4f7cdSSrinivas Pandruvada 
21123fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
21133fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21143fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
2115ce1326a2SPrarit Bhargava 						  NULL, NULL, &enable);
21163fb4f7cdSSrinivas Pandruvada 	else
21173fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(enable_clos_qos_config, NULL,
2118ce1326a2SPrarit Bhargava 					       NULL, NULL, &enable);
21193fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
21203fb4f7cdSSrinivas Pandruvada }
21213fb4f7cdSSrinivas Pandruvada 
21223fb4f7cdSSrinivas Pandruvada static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
21233fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
21243fb4f7cdSSrinivas Pandruvada {
21253fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
21263fb4f7cdSSrinivas Pandruvada 	int ret;
21273fb4f7cdSSrinivas Pandruvada 
21283fb4f7cdSSrinivas Pandruvada 	ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
21293fb4f7cdSSrinivas Pandruvada 	if (ret)
2130fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
21313fb4f7cdSSrinivas Pandruvada 	else
21323fb4f7cdSSrinivas Pandruvada 		isst_clos_display_information(cpu, outf, current_clos,
21333fb4f7cdSSrinivas Pandruvada 					      &clos_config);
21343fb4f7cdSSrinivas Pandruvada }
21353fb4f7cdSSrinivas Pandruvada 
2136ce1326a2SPrarit Bhargava static void dump_clos_config(int arg)
21373fb4f7cdSSrinivas Pandruvada {
21383fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
21393fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21403fb4f7cdSSrinivas Pandruvada 			"Print Intel Speed Select Technology core power configuration\n");
21413fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21423fb4f7cdSSrinivas Pandruvada 			"\tArguments: [-c | --clos]: Specify clos id\n");
21433fb4f7cdSSrinivas Pandruvada 		exit(0);
21443fb4f7cdSSrinivas Pandruvada 	}
21453fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2146fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2147fe6fb216SSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
21483fb4f7cdSSrinivas Pandruvada 		exit(0);
21493fb4f7cdSSrinivas Pandruvada 	}
21503fb4f7cdSSrinivas Pandruvada 
21513fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
21523fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21533fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
21543fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
21553fb4f7cdSSrinivas Pandruvada 	else
21563fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
21573fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
21583fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
21593fb4f7cdSSrinivas Pandruvada }
21603fb4f7cdSSrinivas Pandruvada 
2161188afed9SSrinivas Pandruvada static void get_clos_info_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2162188afed9SSrinivas Pandruvada 				  void *arg4)
2163188afed9SSrinivas Pandruvada {
2164188afed9SSrinivas Pandruvada 	int enable, ret, prio_type;
2165188afed9SSrinivas Pandruvada 
2166188afed9SSrinivas Pandruvada 	ret = isst_clos_get_clos_information(cpu, &enable, &prio_type);
2167188afed9SSrinivas Pandruvada 	if (ret)
2168fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_clos_get_info failed", 0, 0);
2169143ad322SSrinivas Pandruvada 	else {
2170143ad322SSrinivas Pandruvada 		int cp_state, cp_cap;
2171143ad322SSrinivas Pandruvada 
2172143ad322SSrinivas Pandruvada 		isst_read_pm_config(cpu, &cp_state, &cp_cap);
2173143ad322SSrinivas Pandruvada 		isst_clos_display_clos_information(cpu, outf, enable, prio_type,
2174143ad322SSrinivas Pandruvada 						   cp_state, cp_cap);
2175143ad322SSrinivas Pandruvada 	}
2176188afed9SSrinivas Pandruvada }
2177188afed9SSrinivas Pandruvada 
2178ce1326a2SPrarit Bhargava static void dump_clos_info(int arg)
2179188afed9SSrinivas Pandruvada {
2180188afed9SSrinivas Pandruvada 	if (cmd_help) {
2181188afed9SSrinivas Pandruvada 		fprintf(stderr,
2182188afed9SSrinivas Pandruvada 			"Print Intel Speed Select Technology core power information\n");
2183f5205f49SSrinivas Pandruvada 		fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2184188afed9SSrinivas Pandruvada 		exit(0);
2185188afed9SSrinivas Pandruvada 	}
2186188afed9SSrinivas Pandruvada 
2187188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2188f5205f49SSrinivas Pandruvada 	if (max_target_cpus)
2189188afed9SSrinivas Pandruvada 		for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2190188afed9SSrinivas Pandruvada 						  NULL, NULL, NULL);
2191f5205f49SSrinivas Pandruvada 	else
2192f5205f49SSrinivas Pandruvada 		for_each_online_package_in_set(get_clos_info_for_cpu, NULL,
2193f5205f49SSrinivas Pandruvada 					       NULL, NULL, NULL);
2194188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
2195188afed9SSrinivas Pandruvada 
2196188afed9SSrinivas Pandruvada }
2197188afed9SSrinivas Pandruvada 
21983fb4f7cdSSrinivas Pandruvada static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
21993fb4f7cdSSrinivas Pandruvada 				    void *arg4)
22003fb4f7cdSSrinivas Pandruvada {
22013fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
22023fb4f7cdSSrinivas Pandruvada 	int ret;
22033fb4f7cdSSrinivas Pandruvada 
22043fb4f7cdSSrinivas Pandruvada 	clos_config.pkg_id = get_physical_package_id(cpu);
22053fb4f7cdSSrinivas Pandruvada 	clos_config.die_id = get_physical_die_id(cpu);
22063fb4f7cdSSrinivas Pandruvada 
22073fb4f7cdSSrinivas Pandruvada 	clos_config.epp = clos_epp;
22083fb4f7cdSSrinivas Pandruvada 	clos_config.clos_prop_prio = clos_prop_prio;
22093fb4f7cdSSrinivas Pandruvada 	clos_config.clos_min = clos_min;
22103fb4f7cdSSrinivas Pandruvada 	clos_config.clos_max = clos_max;
22113fb4f7cdSSrinivas Pandruvada 	clos_config.clos_desired = clos_desired;
22123fb4f7cdSSrinivas Pandruvada 	ret = isst_set_clos(cpu, current_clos, &clos_config);
22133fb4f7cdSSrinivas Pandruvada 	if (ret)
2214fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
22153fb4f7cdSSrinivas Pandruvada 	else
22163fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "config", ret);
22173fb4f7cdSSrinivas Pandruvada }
22183fb4f7cdSSrinivas Pandruvada 
2219ce1326a2SPrarit Bhargava static void set_clos_config(int arg)
22203fb4f7cdSSrinivas Pandruvada {
22213fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
22223fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
22233fb4f7cdSSrinivas Pandruvada 			"Set core-power configuration for one of the four clos ids\n");
22243fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
22253fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
2226fe6fb216SSrinivas Pandruvada 		if (!is_skx_based_platform()) {
22273fb4f7cdSSrinivas Pandruvada 			fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
22283fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
22293fb4f7cdSSrinivas Pandruvada 				"\tSpecify clos Proportional Priority [--weight|-w]\n");
2230fe6fb216SSrinivas Pandruvada 		}
223140dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
223240dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
22333fb4f7cdSSrinivas Pandruvada 		exit(0);
22343fb4f7cdSSrinivas Pandruvada 	}
22353fb4f7cdSSrinivas Pandruvada 
22363fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2237fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
22383fb4f7cdSSrinivas Pandruvada 		exit(0);
22393fb4f7cdSSrinivas Pandruvada 	}
2240fe6fb216SSrinivas Pandruvada 	if (!is_skx_based_platform() && (clos_epp < 0 || clos_epp > 0x0F)) {
2241fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos epp is not specified or invalid, default: 0\n");
22423fb4f7cdSSrinivas Pandruvada 		clos_epp = 0;
22433fb4f7cdSSrinivas Pandruvada 	}
2244fe6fb216SSrinivas Pandruvada 	if (!is_skx_based_platform() && (clos_prop_prio < 0 || clos_prop_prio > 0x0F)) {
22453fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
2246fe6fb216SSrinivas Pandruvada 			"clos frequency weight is not specified or invalid, default: 0\n");
22473fb4f7cdSSrinivas Pandruvada 		clos_prop_prio = 0;
22483fb4f7cdSSrinivas Pandruvada 	}
22493fb4f7cdSSrinivas Pandruvada 	if (clos_min < 0) {
22503fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "clos min is not specified, default: 0\n");
22513fb4f7cdSSrinivas Pandruvada 		clos_min = 0;
22523fb4f7cdSSrinivas Pandruvada 	}
22533fb4f7cdSSrinivas Pandruvada 	if (clos_max < 0) {
2254fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos max is not specified, default: Max frequency (ratio 0xff)\n");
22553fb4f7cdSSrinivas Pandruvada 		clos_max = 0xff;
22563fb4f7cdSSrinivas Pandruvada 	}
2257fe6fb216SSrinivas Pandruvada 	if (clos_desired) {
2258fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos desired is not supported on this platform\n");
22593fb4f7cdSSrinivas Pandruvada 		clos_desired = 0x00;
22603fb4f7cdSSrinivas Pandruvada 	}
22613fb4f7cdSSrinivas Pandruvada 
22623fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
22633fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
22643fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
22653fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
22663fb4f7cdSSrinivas Pandruvada 	else
22673fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
22683fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
22693fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
22703fb4f7cdSSrinivas Pandruvada }
22713fb4f7cdSSrinivas Pandruvada 
22723fb4f7cdSSrinivas Pandruvada static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
22733fb4f7cdSSrinivas Pandruvada 				   void *arg4)
22743fb4f7cdSSrinivas Pandruvada {
22753fb4f7cdSSrinivas Pandruvada 	int ret;
22763fb4f7cdSSrinivas Pandruvada 
22773fb4f7cdSSrinivas Pandruvada 	ret = isst_clos_associate(cpu, current_clos);
22783fb4f7cdSSrinivas Pandruvada 	if (ret)
2279fe6fb216SSrinivas Pandruvada 		debug_printf("isst_clos_associate failed");
22803fb4f7cdSSrinivas Pandruvada 	else
22813fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "assoc", ret);
22823fb4f7cdSSrinivas Pandruvada }
22833fb4f7cdSSrinivas Pandruvada 
2284ce1326a2SPrarit Bhargava static void set_clos_assoc(int arg)
22853fb4f7cdSSrinivas Pandruvada {
22863fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
22873fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Associate a clos id to a CPU\n");
22883fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
22893fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
229068e2f109SSrinivas Pandruvada 		fprintf(stderr,
229168e2f109SSrinivas Pandruvada 			"\tFor example to associate clos 1 to CPU 0: issue\n");
229268e2f109SSrinivas Pandruvada 		fprintf(stderr,
229368e2f109SSrinivas Pandruvada 			"\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
22943fb4f7cdSSrinivas Pandruvada 		exit(0);
22953fb4f7cdSSrinivas Pandruvada 	}
22963fb4f7cdSSrinivas Pandruvada 
22973fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2298fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
22993fb4f7cdSSrinivas Pandruvada 		exit(0);
23003fb4f7cdSSrinivas Pandruvada 	}
23013fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
23023fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
23033fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
23043fb4f7cdSSrinivas Pandruvada 	else {
2305fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
23063fb4f7cdSSrinivas Pandruvada 	}
23073fb4f7cdSSrinivas Pandruvada }
23083fb4f7cdSSrinivas Pandruvada 
23093fb4f7cdSSrinivas Pandruvada static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
23103fb4f7cdSSrinivas Pandruvada 				   void *arg4)
23113fb4f7cdSSrinivas Pandruvada {
23123fb4f7cdSSrinivas Pandruvada 	int clos, ret;
23133fb4f7cdSSrinivas Pandruvada 
23143fb4f7cdSSrinivas Pandruvada 	ret = isst_clos_get_assoc_status(cpu, &clos);
23153fb4f7cdSSrinivas Pandruvada 	if (ret)
2316fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_clos_get_assoc_status failed", 0, 0);
23173fb4f7cdSSrinivas Pandruvada 	else
2318e118fbe3SSrinivas Pandruvada 		isst_clos_display_assoc_information(cpu, outf, clos);
23193fb4f7cdSSrinivas Pandruvada }
23203fb4f7cdSSrinivas Pandruvada 
2321ce1326a2SPrarit Bhargava static void get_clos_assoc(int arg)
23223fb4f7cdSSrinivas Pandruvada {
23233fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
23243fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Get associate clos id to a CPU\n");
23253fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
23263fb4f7cdSSrinivas Pandruvada 		exit(0);
23273fb4f7cdSSrinivas Pandruvada 	}
2328e118fbe3SSrinivas Pandruvada 
2329e118fbe3SSrinivas Pandruvada 	if (!max_target_cpus) {
2330fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2331e118fbe3SSrinivas Pandruvada 		exit(0);
23323fb4f7cdSSrinivas Pandruvada 	}
2333e118fbe3SSrinivas Pandruvada 
2334e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2335e118fbe3SSrinivas Pandruvada 	for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2336e118fbe3SSrinivas Pandruvada 					  NULL, NULL, NULL);
2337e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
23383fb4f7cdSSrinivas Pandruvada }
23393fb4f7cdSSrinivas Pandruvada 
2340006050a6SSrinivas Pandruvada static void set_turbo_mode_for_cpu(int cpu, int status)
2341006050a6SSrinivas Pandruvada {
2342006050a6SSrinivas Pandruvada 	int base_freq;
2343006050a6SSrinivas Pandruvada 
2344006050a6SSrinivas Pandruvada 	if (status) {
2345006050a6SSrinivas Pandruvada 		base_freq = get_cpufreq_base_freq(cpu);
2346006050a6SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(cpu, 1, base_freq);
2347006050a6SSrinivas Pandruvada 	} else {
2348006050a6SSrinivas Pandruvada 		set_scaling_max_to_cpuinfo_max(cpu);
2349006050a6SSrinivas Pandruvada 	}
2350006050a6SSrinivas Pandruvada 
2351006050a6SSrinivas Pandruvada 	if (status) {
2352006050a6SSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-mode", "enable", 0);
2353006050a6SSrinivas Pandruvada 	} else {
2354006050a6SSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-mode", "disable", 0);
2355006050a6SSrinivas Pandruvada 	}
2356006050a6SSrinivas Pandruvada }
2357006050a6SSrinivas Pandruvada 
2358006050a6SSrinivas Pandruvada static void set_turbo_mode(int arg)
2359006050a6SSrinivas Pandruvada {
2360006050a6SSrinivas Pandruvada 	int i, enable = arg;
2361006050a6SSrinivas Pandruvada 
2362006050a6SSrinivas Pandruvada 	if (cmd_help) {
2363006050a6SSrinivas Pandruvada 		if (enable)
2364006050a6SSrinivas Pandruvada 			fprintf(stderr, "Set turbo mode enable\n");
2365006050a6SSrinivas Pandruvada 		else
2366006050a6SSrinivas Pandruvada 			fprintf(stderr, "Set turbo mode disable\n");
2367006050a6SSrinivas Pandruvada 		exit(0);
2368006050a6SSrinivas Pandruvada 	}
2369006050a6SSrinivas Pandruvada 
2370006050a6SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2371006050a6SSrinivas Pandruvada 
2372006050a6SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
2373006050a6SSrinivas Pandruvada 		int online;
2374006050a6SSrinivas Pandruvada 
2375006050a6SSrinivas Pandruvada 		if (i)
2376006050a6SSrinivas Pandruvada 			online = parse_int_file(
2377006050a6SSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
2378006050a6SSrinivas Pandruvada 		else
2379006050a6SSrinivas Pandruvada 			online =
2380006050a6SSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
2381006050a6SSrinivas Pandruvada 
2382006050a6SSrinivas Pandruvada 		if (online)
2383006050a6SSrinivas Pandruvada 			set_turbo_mode_for_cpu(i, enable);
2384006050a6SSrinivas Pandruvada 
2385006050a6SSrinivas Pandruvada 	}
2386006050a6SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
2387006050a6SSrinivas Pandruvada }
2388006050a6SSrinivas Pandruvada 
23892c7dc57eSSrinivas Pandruvada static void get_set_trl(int cpu, void *arg1, void *arg2, void *arg3,
23902c7dc57eSSrinivas Pandruvada 			void *arg4)
23912c7dc57eSSrinivas Pandruvada {
23922c7dc57eSSrinivas Pandruvada 	unsigned long long trl;
23932c7dc57eSSrinivas Pandruvada 	int set = *(int *)arg4;
23942c7dc57eSSrinivas Pandruvada 	int ret;
23952c7dc57eSSrinivas Pandruvada 
23962c7dc57eSSrinivas Pandruvada 	if (set && !fact_trl) {
23972c7dc57eSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid TRL. Specify with [-t|--trl]", 0, 0);
23982c7dc57eSSrinivas Pandruvada 		exit(0);
23992c7dc57eSSrinivas Pandruvada 	}
24002c7dc57eSSrinivas Pandruvada 
24012c7dc57eSSrinivas Pandruvada 	if (set) {
24022c7dc57eSSrinivas Pandruvada 		ret = isst_set_trl(cpu, fact_trl);
24032c7dc57eSSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-mode", "set-trl", ret);
24042c7dc57eSSrinivas Pandruvada 		return;
24052c7dc57eSSrinivas Pandruvada 	}
24062c7dc57eSSrinivas Pandruvada 
24072c7dc57eSSrinivas Pandruvada 	ret = isst_get_trl(cpu, &trl);
24082c7dc57eSSrinivas Pandruvada 	if (ret)
24092c7dc57eSSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-mode", "get-trl", ret);
24102c7dc57eSSrinivas Pandruvada 	else
24112c7dc57eSSrinivas Pandruvada 		isst_trl_display_information(cpu, outf, trl);
24122c7dc57eSSrinivas Pandruvada }
24132c7dc57eSSrinivas Pandruvada 
24142c7dc57eSSrinivas Pandruvada static void process_trl(int arg)
24152c7dc57eSSrinivas Pandruvada {
24162c7dc57eSSrinivas Pandruvada 	if (cmd_help) {
24172c7dc57eSSrinivas Pandruvada 		if (arg) {
24182c7dc57eSSrinivas Pandruvada 			fprintf(stderr, "Set TRL (turbo ratio limits)\n");
24192c7dc57eSSrinivas Pandruvada 			fprintf(stderr, "\t t|--trl: Specify turbo ratio limit for setting TRL\n");
24202c7dc57eSSrinivas Pandruvada 		} else {
24212c7dc57eSSrinivas Pandruvada 			fprintf(stderr, "Get TRL (turbo ratio limits)\n");
24222c7dc57eSSrinivas Pandruvada 		}
24232c7dc57eSSrinivas Pandruvada 		exit(0);
24242c7dc57eSSrinivas Pandruvada 	}
24252c7dc57eSSrinivas Pandruvada 
24262c7dc57eSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
24272c7dc57eSSrinivas Pandruvada 	if (max_target_cpus)
24282c7dc57eSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(get_set_trl, NULL,
24292c7dc57eSSrinivas Pandruvada 						  NULL, NULL, &arg);
24302c7dc57eSSrinivas Pandruvada 	else
24312c7dc57eSSrinivas Pandruvada 		for_each_online_package_in_set(get_set_trl, NULL,
24322c7dc57eSSrinivas Pandruvada 					       NULL, NULL, &arg);
24332c7dc57eSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
24342c7dc57eSSrinivas Pandruvada }
24352c7dc57eSSrinivas Pandruvada 
2436c829f0efSPrarit Bhargava static struct process_cmd_struct clx_n_cmds[] = {
2437062e4aacSPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
24381aa7177cSPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
24391aa7177cSPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
24401aa7177cSPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2441c829f0efSPrarit Bhargava 	{ NULL, NULL, NULL, 0 }
2442c829f0efSPrarit Bhargava };
2443c829f0efSPrarit Bhargava 
24443fb4f7cdSSrinivas Pandruvada static struct process_cmd_struct isst_cmds[] = {
2445ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2446ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2447ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-version", get_tdp_version, 0 },
2448ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2449ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-current-level", get_tdp_current_level,
2450ce1326a2SPrarit Bhargava 	 0 },
2451ce1326a2SPrarit Bhargava 	{ "perf-profile", "set-config-level", set_tdp_level, 0 },
2452ce1326a2SPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
2453ce1326a2SPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
2454ce1326a2SPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
2455ce1326a2SPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2456ce1326a2SPrarit Bhargava 	{ "turbo-freq", "info", dump_fact_config, 0 },
2457ce1326a2SPrarit Bhargava 	{ "turbo-freq", "enable", set_fact_enable, 1 },
2458ce1326a2SPrarit Bhargava 	{ "turbo-freq", "disable", set_fact_enable, 0 },
2459ce1326a2SPrarit Bhargava 	{ "core-power", "info", dump_clos_info, 0 },
2460ce1326a2SPrarit Bhargava 	{ "core-power", "enable", set_clos_enable, 1 },
2461ce1326a2SPrarit Bhargava 	{ "core-power", "disable", set_clos_enable, 0 },
2462ce1326a2SPrarit Bhargava 	{ "core-power", "config", set_clos_config, 0 },
2463ce1326a2SPrarit Bhargava 	{ "core-power", "get-config", dump_clos_config, 0 },
2464ce1326a2SPrarit Bhargava 	{ "core-power", "assoc", set_clos_assoc, 0 },
2465ce1326a2SPrarit Bhargava 	{ "core-power", "get-assoc", get_clos_assoc, 0 },
2466006050a6SSrinivas Pandruvada 	{ "turbo-mode", "enable", set_turbo_mode, 0 },
2467006050a6SSrinivas Pandruvada 	{ "turbo-mode", "disable", set_turbo_mode, 1 },
24682c7dc57eSSrinivas Pandruvada 	{ "turbo-mode", "get-trl", process_trl, 0 },
24692c7dc57eSSrinivas Pandruvada 	{ "turbo-mode", "set-trl", process_trl, 1 },
24703fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL, NULL }
24713fb4f7cdSSrinivas Pandruvada };
24723fb4f7cdSSrinivas Pandruvada 
24733fb4f7cdSSrinivas Pandruvada /*
24743fb4f7cdSSrinivas Pandruvada  * parse cpuset with following syntax
24753fb4f7cdSSrinivas Pandruvada  * 1,2,4..6,8-10 and set bits in cpu_subset
24763fb4f7cdSSrinivas Pandruvada  */
24773fb4f7cdSSrinivas Pandruvada void parse_cpu_command(char *optarg)
24783fb4f7cdSSrinivas Pandruvada {
24793fb4f7cdSSrinivas Pandruvada 	unsigned int start, end;
24803fb4f7cdSSrinivas Pandruvada 	char *next;
24813fb4f7cdSSrinivas Pandruvada 
24823fb4f7cdSSrinivas Pandruvada 	next = optarg;
24833fb4f7cdSSrinivas Pandruvada 
24843fb4f7cdSSrinivas Pandruvada 	while (next && *next) {
24853fb4f7cdSSrinivas Pandruvada 		if (*next == '-') /* no negative cpu numbers */
24863fb4f7cdSSrinivas Pandruvada 			goto error;
24873fb4f7cdSSrinivas Pandruvada 
24883fb4f7cdSSrinivas Pandruvada 		start = strtoul(next, &next, 10);
24893fb4f7cdSSrinivas Pandruvada 
24903fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
24913fb4f7cdSSrinivas Pandruvada 			target_cpus[max_target_cpus++] = start;
24923fb4f7cdSSrinivas Pandruvada 
24933fb4f7cdSSrinivas Pandruvada 		if (*next == '\0')
24943fb4f7cdSSrinivas Pandruvada 			break;
24953fb4f7cdSSrinivas Pandruvada 
24963fb4f7cdSSrinivas Pandruvada 		if (*next == ',') {
24973fb4f7cdSSrinivas Pandruvada 			next += 1;
24983fb4f7cdSSrinivas Pandruvada 			continue;
24993fb4f7cdSSrinivas Pandruvada 		}
25003fb4f7cdSSrinivas Pandruvada 
25013fb4f7cdSSrinivas Pandruvada 		if (*next == '-') {
25023fb4f7cdSSrinivas Pandruvada 			next += 1; /* start range */
25033fb4f7cdSSrinivas Pandruvada 		} else if (*next == '.') {
25043fb4f7cdSSrinivas Pandruvada 			next += 1;
25053fb4f7cdSSrinivas Pandruvada 			if (*next == '.')
25063fb4f7cdSSrinivas Pandruvada 				next += 1; /* start range */
25073fb4f7cdSSrinivas Pandruvada 			else
25083fb4f7cdSSrinivas Pandruvada 				goto error;
25093fb4f7cdSSrinivas Pandruvada 		}
25103fb4f7cdSSrinivas Pandruvada 
25113fb4f7cdSSrinivas Pandruvada 		end = strtoul(next, &next, 10);
25123fb4f7cdSSrinivas Pandruvada 		if (end <= start)
25133fb4f7cdSSrinivas Pandruvada 			goto error;
25143fb4f7cdSSrinivas Pandruvada 
25153fb4f7cdSSrinivas Pandruvada 		while (++start <= end) {
25163fb4f7cdSSrinivas Pandruvada 			if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
25173fb4f7cdSSrinivas Pandruvada 				target_cpus[max_target_cpus++] = start;
25183fb4f7cdSSrinivas Pandruvada 		}
25193fb4f7cdSSrinivas Pandruvada 
25203fb4f7cdSSrinivas Pandruvada 		if (*next == ',')
25213fb4f7cdSSrinivas Pandruvada 			next += 1;
25223fb4f7cdSSrinivas Pandruvada 		else if (*next != '\0')
25233fb4f7cdSSrinivas Pandruvada 			goto error;
25243fb4f7cdSSrinivas Pandruvada 	}
25253fb4f7cdSSrinivas Pandruvada 
25263fb4f7cdSSrinivas Pandruvada #ifdef DEBUG
25273fb4f7cdSSrinivas Pandruvada 	{
25283fb4f7cdSSrinivas Pandruvada 		int i;
25293fb4f7cdSSrinivas Pandruvada 
25303fb4f7cdSSrinivas Pandruvada 		for (i = 0; i < max_target_cpus; ++i)
25313fb4f7cdSSrinivas Pandruvada 			printf("cpu [%d] in arg\n", target_cpus[i]);
25323fb4f7cdSSrinivas Pandruvada 	}
25333fb4f7cdSSrinivas Pandruvada #endif
25343fb4f7cdSSrinivas Pandruvada 	return;
25353fb4f7cdSSrinivas Pandruvada 
25363fb4f7cdSSrinivas Pandruvada error:
25373fb4f7cdSSrinivas Pandruvada 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
25383fb4f7cdSSrinivas Pandruvada 	exit(-1);
25393fb4f7cdSSrinivas Pandruvada }
25403fb4f7cdSSrinivas Pandruvada 
25413fb4f7cdSSrinivas Pandruvada static void parse_cmd_args(int argc, int start, char **argv)
25423fb4f7cdSSrinivas Pandruvada {
25433fb4f7cdSSrinivas Pandruvada 	int opt;
25443fb4f7cdSSrinivas Pandruvada 	int option_index;
25453fb4f7cdSSrinivas Pandruvada 
25463fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
25473fb4f7cdSSrinivas Pandruvada 		{ "bucket", required_argument, 0, 'b' },
25483fb4f7cdSSrinivas Pandruvada 		{ "level", required_argument, 0, 'l' },
25493c64c81aSSrinivas Pandruvada 		{ "online", required_argument, 0, 'o' },
25503fb4f7cdSSrinivas Pandruvada 		{ "trl-type", required_argument, 0, 'r' },
25513fb4f7cdSSrinivas Pandruvada 		{ "trl", required_argument, 0, 't' },
25523fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
25533fb4f7cdSSrinivas Pandruvada 		{ "clos", required_argument, 0, 'c' },
25543fb4f7cdSSrinivas Pandruvada 		{ "desired", required_argument, 0, 'd' },
25553fb4f7cdSSrinivas Pandruvada 		{ "epp", required_argument, 0, 'e' },
25563fb4f7cdSSrinivas Pandruvada 		{ "min", required_argument, 0, 'n' },
25573fb4f7cdSSrinivas Pandruvada 		{ "max", required_argument, 0, 'm' },
25583fb4f7cdSSrinivas Pandruvada 		{ "priority", required_argument, 0, 'p' },
25593fb4f7cdSSrinivas Pandruvada 		{ "weight", required_argument, 0, 'w' },
2560354bd06fSSrinivas Pandruvada 		{ "auto", no_argument, 0, 'a' },
25613fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
25623fb4f7cdSSrinivas Pandruvada 	};
25633fb4f7cdSSrinivas Pandruvada 
25643fb4f7cdSSrinivas Pandruvada 	option_index = start;
25653fb4f7cdSSrinivas Pandruvada 
25663fb4f7cdSSrinivas Pandruvada 	optind = start + 1;
2567b86639e1SSrinivas Pandruvada 	while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:r:hoa",
25683fb4f7cdSSrinivas Pandruvada 				  long_options, &option_index)) != -1) {
25693fb4f7cdSSrinivas Pandruvada 		switch (opt) {
2570354bd06fSSrinivas Pandruvada 		case 'a':
2571354bd06fSSrinivas Pandruvada 			auto_mode = 1;
2572354bd06fSSrinivas Pandruvada 			break;
25733fb4f7cdSSrinivas Pandruvada 		case 'b':
25743fb4f7cdSSrinivas Pandruvada 			fact_bucket = atoi(optarg);
25753fb4f7cdSSrinivas Pandruvada 			break;
25763fb4f7cdSSrinivas Pandruvada 		case 'h':
25773fb4f7cdSSrinivas Pandruvada 			cmd_help = 1;
25783fb4f7cdSSrinivas Pandruvada 			break;
25793fb4f7cdSSrinivas Pandruvada 		case 'l':
25803fb4f7cdSSrinivas Pandruvada 			tdp_level = atoi(optarg);
25813fb4f7cdSSrinivas Pandruvada 			break;
25823c64c81aSSrinivas Pandruvada 		case 'o':
25833c64c81aSSrinivas Pandruvada 			force_online_offline = 1;
25843c64c81aSSrinivas Pandruvada 			break;
25853fb4f7cdSSrinivas Pandruvada 		case 't':
25863fb4f7cdSSrinivas Pandruvada 			sscanf(optarg, "0x%llx", &fact_trl);
25873fb4f7cdSSrinivas Pandruvada 			break;
25883fb4f7cdSSrinivas Pandruvada 		case 'r':
25893fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "sse", 3)) {
25903fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x01;
25913fb4f7cdSSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx2", 4)) {
25923fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x02;
2593b86639e1SSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx512", 6)) {
25943fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x04;
25953fb4f7cdSSrinivas Pandruvada 			} else {
25963fb4f7cdSSrinivas Pandruvada 				fprintf(outf, "Invalid sse,avx options\n");
25973fb4f7cdSSrinivas Pandruvada 				exit(1);
25983fb4f7cdSSrinivas Pandruvada 			}
25993fb4f7cdSSrinivas Pandruvada 			break;
26003fb4f7cdSSrinivas Pandruvada 		/* CLOS related */
26013fb4f7cdSSrinivas Pandruvada 		case 'c':
26023fb4f7cdSSrinivas Pandruvada 			current_clos = atoi(optarg);
26033fb4f7cdSSrinivas Pandruvada 			break;
26043fb4f7cdSSrinivas Pandruvada 		case 'd':
26053fb4f7cdSSrinivas Pandruvada 			clos_desired = atoi(optarg);
260640dee9ddSSrinivas Pandruvada 			clos_desired /= DISP_FREQ_MULTIPLIER;
26073fb4f7cdSSrinivas Pandruvada 			break;
26083fb4f7cdSSrinivas Pandruvada 		case 'e':
26093fb4f7cdSSrinivas Pandruvada 			clos_epp = atoi(optarg);
2610fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform()) {
2611fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "epp can't be specified on this platform", 0, 0);
2612fe6fb216SSrinivas Pandruvada 				exit(0);
2613fe6fb216SSrinivas Pandruvada 			}
26143fb4f7cdSSrinivas Pandruvada 			break;
26153fb4f7cdSSrinivas Pandruvada 		case 'n':
26163fb4f7cdSSrinivas Pandruvada 			clos_min = atoi(optarg);
261740dee9ddSSrinivas Pandruvada 			clos_min /= DISP_FREQ_MULTIPLIER;
26183fb4f7cdSSrinivas Pandruvada 			break;
26193fb4f7cdSSrinivas Pandruvada 		case 'm':
26203fb4f7cdSSrinivas Pandruvada 			clos_max = atoi(optarg);
262140dee9ddSSrinivas Pandruvada 			clos_max /= DISP_FREQ_MULTIPLIER;
26223fb4f7cdSSrinivas Pandruvada 			break;
26233fb4f7cdSSrinivas Pandruvada 		case 'p':
26243fb4f7cdSSrinivas Pandruvada 			clos_priority_type = atoi(optarg);
2625fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform() && !clos_priority_type) {
2626fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "Invalid clos priority type: proportional for this platform", 0, 0);
2627fe6fb216SSrinivas Pandruvada 				exit(0);
2628fe6fb216SSrinivas Pandruvada 			}
26293fb4f7cdSSrinivas Pandruvada 			break;
26303fb4f7cdSSrinivas Pandruvada 		case 'w':
26313fb4f7cdSSrinivas Pandruvada 			clos_prop_prio = atoi(optarg);
2632fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform()) {
2633fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "weight can't be specified on this platform", 0, 0);
2634fe6fb216SSrinivas Pandruvada 				exit(0);
2635fe6fb216SSrinivas Pandruvada 			}
26363fb4f7cdSSrinivas Pandruvada 			break;
26373fb4f7cdSSrinivas Pandruvada 		default:
26384a960353SSrinivas Pandruvada 			printf("Unknown option: ignore\n");
26393fb4f7cdSSrinivas Pandruvada 		}
26403fb4f7cdSSrinivas Pandruvada 	}
26414a960353SSrinivas Pandruvada 
26424a960353SSrinivas Pandruvada 	if (argv[optind])
26434a960353SSrinivas Pandruvada 		printf("Garbage at the end of command: ignore\n");
26443fb4f7cdSSrinivas Pandruvada }
26453fb4f7cdSSrinivas Pandruvada 
26463fb4f7cdSSrinivas Pandruvada static void isst_help(void)
26473fb4f7cdSSrinivas Pandruvada {
26483fb4f7cdSSrinivas Pandruvada 	printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
26493fb4f7cdSSrinivas Pandruvada 		performance profiles per system via static and/or dynamic\n\
26503fb4f7cdSSrinivas Pandruvada 		adjustment of core count, workload, Tjmax, and\n\
26513fb4f7cdSSrinivas Pandruvada 		TDP, etc.\n");
26523fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=perf-profile\n");
26533fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
2654c829f0efSPrarit Bhargava 
2655c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
26563fb4f7cdSSrinivas Pandruvada 		printf("\tget-lock-status\n");
26573fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-levels\n");
26583fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-version\n");
26593fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-enabled\n");
26603fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-current-level\n");
26613fb4f7cdSSrinivas Pandruvada 		printf("\tset-config-level\n");
26623fb4f7cdSSrinivas Pandruvada 	}
2663c829f0efSPrarit Bhargava }
26643fb4f7cdSSrinivas Pandruvada 
26653fb4f7cdSSrinivas Pandruvada static void pbf_help(void)
26663fb4f7cdSSrinivas Pandruvada {
26673fb4f7cdSSrinivas Pandruvada 	printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
26683fb4f7cdSSrinivas Pandruvada 		on certain cores (high priority cores) in exchange for lower\n\
26693fb4f7cdSSrinivas Pandruvada 		base frequency on remaining cores (low priority cores).\n");
26703fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
26713fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
26723fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
26733fb4f7cdSSrinivas Pandruvada }
26743fb4f7cdSSrinivas Pandruvada 
26753fb4f7cdSSrinivas Pandruvada static void fact_help(void)
26763fb4f7cdSSrinivas Pandruvada {
26773fb4f7cdSSrinivas Pandruvada 	printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
26783fb4f7cdSSrinivas Pandruvada 		limits to cores based on priority.\n");
26793fb4f7cdSSrinivas Pandruvada 	printf("\nCommand: For feature=turbo-freq\n");
26803fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
26813fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
26823fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
26833fb4f7cdSSrinivas Pandruvada }
26843fb4f7cdSSrinivas Pandruvada 
2685006050a6SSrinivas Pandruvada static void turbo_mode_help(void)
2686006050a6SSrinivas Pandruvada {
26872c7dc57eSSrinivas Pandruvada 	printf("turbo-mode:\tEnables users to enable/disable turbo mode by adjusting frequency settings. Also allows to get and set turbo ratio limits (TRL).\n");
2688006050a6SSrinivas Pandruvada 	printf("\tcommand : enable\n");
2689006050a6SSrinivas Pandruvada 	printf("\tcommand : disable\n");
26902c7dc57eSSrinivas Pandruvada 	printf("\tcommand : get-trl\n");
26912c7dc57eSSrinivas Pandruvada 	printf("\tcommand : set-trl\n");
2692006050a6SSrinivas Pandruvada }
2693006050a6SSrinivas Pandruvada 
2694006050a6SSrinivas Pandruvada 
26953fb4f7cdSSrinivas Pandruvada static void core_power_help(void)
26963fb4f7cdSSrinivas Pandruvada {
26973fb4f7cdSSrinivas Pandruvada 	printf("core-power:\tInterface that allows user to define per core/tile\n\
26983fb4f7cdSSrinivas Pandruvada 		priority.\n");
26993fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=core-power\n");
27003fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
27013fb4f7cdSSrinivas Pandruvada 	printf("\tenable\n");
27023fb4f7cdSSrinivas Pandruvada 	printf("\tdisable\n");
27033fb4f7cdSSrinivas Pandruvada 	printf("\tconfig\n");
2704188afed9SSrinivas Pandruvada 	printf("\tget-config\n");
27053fb4f7cdSSrinivas Pandruvada 	printf("\tassoc\n");
27063fb4f7cdSSrinivas Pandruvada 	printf("\tget-assoc\n");
27073fb4f7cdSSrinivas Pandruvada }
27083fb4f7cdSSrinivas Pandruvada 
27093fb4f7cdSSrinivas Pandruvada struct process_cmd_help_struct {
27103fb4f7cdSSrinivas Pandruvada 	char *feature;
27113fb4f7cdSSrinivas Pandruvada 	void (*process_fn)(void);
27123fb4f7cdSSrinivas Pandruvada };
27133fb4f7cdSSrinivas Pandruvada 
27143fb4f7cdSSrinivas Pandruvada static struct process_cmd_help_struct isst_help_cmds[] = {
27153fb4f7cdSSrinivas Pandruvada 	{ "perf-profile", isst_help },
27163fb4f7cdSSrinivas Pandruvada 	{ "base-freq", pbf_help },
27173fb4f7cdSSrinivas Pandruvada 	{ "turbo-freq", fact_help },
27183fb4f7cdSSrinivas Pandruvada 	{ "core-power", core_power_help },
2719006050a6SSrinivas Pandruvada 	{ "turbo-mode", turbo_mode_help },
27203fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL }
27213fb4f7cdSSrinivas Pandruvada };
27223fb4f7cdSSrinivas Pandruvada 
2723c829f0efSPrarit Bhargava static struct process_cmd_help_struct clx_n_help_cmds[] = {
2724c829f0efSPrarit Bhargava 	{ "perf-profile", isst_help },
2725c829f0efSPrarit Bhargava 	{ "base-freq", pbf_help },
2726c829f0efSPrarit Bhargava 	{ NULL, NULL }
2727c829f0efSPrarit Bhargava };
2728c829f0efSPrarit Bhargava 
2729210369dcSPrarit Bhargava void process_command(int argc, char **argv,
2730210369dcSPrarit Bhargava 		     struct process_cmd_help_struct *help_cmds,
2731210369dcSPrarit Bhargava 		     struct process_cmd_struct *cmds)
27323fb4f7cdSSrinivas Pandruvada {
27333fb4f7cdSSrinivas Pandruvada 	int i = 0, matched = 0;
27343fb4f7cdSSrinivas Pandruvada 	char *feature = argv[optind];
27353fb4f7cdSSrinivas Pandruvada 	char *cmd = argv[optind + 1];
27363fb4f7cdSSrinivas Pandruvada 
27373fb4f7cdSSrinivas Pandruvada 	if (!feature || !cmd)
27383fb4f7cdSSrinivas Pandruvada 		return;
27393fb4f7cdSSrinivas Pandruvada 
27403fb4f7cdSSrinivas Pandruvada 	debug_printf("feature name [%s] command [%s]\n", feature, cmd);
27413fb4f7cdSSrinivas Pandruvada 	if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
2742210369dcSPrarit Bhargava 		while (help_cmds[i].feature) {
2743210369dcSPrarit Bhargava 			if (!strcmp(help_cmds[i].feature, feature)) {
2744210369dcSPrarit Bhargava 				help_cmds[i].process_fn();
27453fb4f7cdSSrinivas Pandruvada 				exit(0);
27463fb4f7cdSSrinivas Pandruvada 			}
27473fb4f7cdSSrinivas Pandruvada 			++i;
27483fb4f7cdSSrinivas Pandruvada 		}
27493fb4f7cdSSrinivas Pandruvada 	}
27503fb4f7cdSSrinivas Pandruvada 
2751062e4aacSPrarit Bhargava 	if (!is_clx_n_platform())
27523fb4f7cdSSrinivas Pandruvada 		create_cpu_map();
27533fb4f7cdSSrinivas Pandruvada 
27543fb4f7cdSSrinivas Pandruvada 	i = 0;
2755210369dcSPrarit Bhargava 	while (cmds[i].feature) {
2756210369dcSPrarit Bhargava 		if (!strcmp(cmds[i].feature, feature) &&
2757210369dcSPrarit Bhargava 		    !strcmp(cmds[i].command, cmd)) {
27583fb4f7cdSSrinivas Pandruvada 			parse_cmd_args(argc, optind + 1, argv);
2759210369dcSPrarit Bhargava 			cmds[i].process_fn(cmds[i].arg);
27603fb4f7cdSSrinivas Pandruvada 			matched = 1;
27613fb4f7cdSSrinivas Pandruvada 			break;
27623fb4f7cdSSrinivas Pandruvada 		}
27633fb4f7cdSSrinivas Pandruvada 		++i;
27643fb4f7cdSSrinivas Pandruvada 	}
27653fb4f7cdSSrinivas Pandruvada 
27663fb4f7cdSSrinivas Pandruvada 	if (!matched)
27673fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Invalid command\n");
27683fb4f7cdSSrinivas Pandruvada }
27693fb4f7cdSSrinivas Pandruvada 
27703fb4f7cdSSrinivas Pandruvada static void usage(void)
27713fb4f7cdSSrinivas Pandruvada {
2772addd116dSSrinivas Pandruvada 	if (is_clx_n_platform()) {
2773addd116dSSrinivas Pandruvada 		fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
2774addd116dSSrinivas Pandruvada 		fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
2775addd116dSSrinivas Pandruvada 	}
2776addd116dSSrinivas Pandruvada 
27773fb4f7cdSSrinivas Pandruvada 	printf("\nUsage:\n");
27783fb4f7cdSSrinivas Pandruvada 	printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
2779addd116dSSrinivas Pandruvada 	printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
2780addd116dSSrinivas Pandruvada 	if (is_clx_n_platform())
2781addd116dSSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq]\n");
2782addd116dSSrinivas Pandruvada 	else
2783006050a6SSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power|turbo-mode]\n");
278443774c0dSPrarit Bhargava 	printf("\nFor help on each feature, use -h|--help\n");
27853fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile -h\n");
27863fb4f7cdSSrinivas Pandruvada 
27873fb4f7cdSSrinivas Pandruvada 	printf("\nFor additional help on each command for a feature, use --h|--help\n");
27883fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
27893fb4f7cdSSrinivas Pandruvada 	printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
27903fb4f7cdSSrinivas Pandruvada 
27913fb4f7cdSSrinivas Pandruvada 	printf("\nOPTIONS\n");
27923fb4f7cdSSrinivas Pandruvada 	printf("\t[-c|--cpu] : logical cpu number\n");
27933fb4f7cdSSrinivas Pandruvada 	printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
27943fb4f7cdSSrinivas Pandruvada 	printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
27953fb4f7cdSSrinivas Pandruvada 	printf("\t[-d|--debug] : Debug mode\n");
2796addd116dSSrinivas Pandruvada 	printf("\t[-f|--format] : output format [json|text]. Default: text\n");
27973fb4f7cdSSrinivas Pandruvada 	printf("\t[-h|--help] : Print help\n");
27983fb4f7cdSSrinivas Pandruvada 	printf("\t[-i|--info] : Print platform information\n");
27990d3dfd75SSrinivas Pandruvada 	printf("\t[-a|--all-cpus-online] : Force online every CPU in the system\n");
28003fb4f7cdSSrinivas Pandruvada 	printf("\t[-o|--out] : Output file\n");
28013fb4f7cdSSrinivas Pandruvada 	printf("\t\t\tDefault : stderr\n");
2802a85a35fcSSrinivas Pandruvada 	printf("\t[-p|--pause] : Delay between two mail box commands in milliseconds\n");
280332279be7SSrinivas Pandruvada 	printf("\t[-r|--retry] : Retry count for mail box commands on failure, default 3\n");
28043fb4f7cdSSrinivas Pandruvada 	printf("\t[-v|--version] : Print version\n");
28057fd786dfSSrinivas Pandruvada 	printf("\t[-b|--oob : Start a daemon to process HFI events for perf profile change from Out of Band agent.\n");
28067fd786dfSSrinivas Pandruvada 	printf("\t[-n|--no-daemon : Don't run as daemon. By default --oob will turn on daemon mode\n");
28077fd786dfSSrinivas Pandruvada 	printf("\t[-w|--delay : Delay for reading config level state change in OOB poll mode.\n");
28083fb4f7cdSSrinivas Pandruvada 	printf("\nResult format\n");
28093fb4f7cdSSrinivas Pandruvada 	printf("\tResult display uses a common format for each command:\n");
28103fb4f7cdSSrinivas Pandruvada 	printf("\tResults are formatted in text/JSON with\n");
28113fb4f7cdSSrinivas Pandruvada 	printf("\t\tPackage, Die, CPU, and command specific results.\n");
2812addd116dSSrinivas Pandruvada 
2813addd116dSSrinivas Pandruvada 	printf("\nExamples\n");
2814addd116dSSrinivas Pandruvada 	printf("\tTo get platform information:\n");
2815addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select --info\n");
2816addd116dSSrinivas Pandruvada 	printf("\tTo get full perf-profile information dump:\n");
2817addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select perf-profile info\n");
2818addd116dSSrinivas Pandruvada 	printf("\tTo get full base-freq information dump:\n");
2819addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select base-freq info -l 0\n");
2820addd116dSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2821addd116dSSrinivas Pandruvada 		printf("\tTo get full turbo-freq information dump:\n");
2822addd116dSSrinivas Pandruvada 		printf("\t\tintel-speed-select turbo-freq info -l 0\n");
2823addd116dSSrinivas Pandruvada 	}
28243fb4f7cdSSrinivas Pandruvada 	exit(1);
28253fb4f7cdSSrinivas Pandruvada }
28263fb4f7cdSSrinivas Pandruvada 
28273fb4f7cdSSrinivas Pandruvada static void print_version(void)
28283fb4f7cdSSrinivas Pandruvada {
28293fb4f7cdSSrinivas Pandruvada 	fprintf(outf, "Version %s\n", version_str);
28303fb4f7cdSSrinivas Pandruvada 	exit(0);
28313fb4f7cdSSrinivas Pandruvada }
28323fb4f7cdSSrinivas Pandruvada 
28333fb4f7cdSSrinivas Pandruvada static void cmdline(int argc, char **argv)
28343fb4f7cdSSrinivas Pandruvada {
2835f362cdccSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
2836a85a35fcSSrinivas Pandruvada 	char *ptr;
2837f362cdccSSrinivas Pandruvada 	FILE *fp;
28380d3dfd75SSrinivas Pandruvada 	int opt, force_cpus_online = 0;
28393fb4f7cdSSrinivas Pandruvada 	int option_index = 0;
28403bc3d30cSPrarit Bhargava 	int ret;
28417fd786dfSSrinivas Pandruvada 	int oob_mode = 0;
28427fd786dfSSrinivas Pandruvada 	int poll_interval = -1;
28437fd786dfSSrinivas Pandruvada 	int no_daemon = 0;
28443fb4f7cdSSrinivas Pandruvada 
28453fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
28460d3dfd75SSrinivas Pandruvada 		{ "all-cpus-online", no_argument, 0, 'a' },
28473fb4f7cdSSrinivas Pandruvada 		{ "cpu", required_argument, 0, 'c' },
28483fb4f7cdSSrinivas Pandruvada 		{ "debug", no_argument, 0, 'd' },
28493fb4f7cdSSrinivas Pandruvada 		{ "format", required_argument, 0, 'f' },
28503fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
28513fb4f7cdSSrinivas Pandruvada 		{ "info", no_argument, 0, 'i' },
2852a85a35fcSSrinivas Pandruvada 		{ "pause", required_argument, 0, 'p' },
28533fb4f7cdSSrinivas Pandruvada 		{ "out", required_argument, 0, 'o' },
285432279be7SSrinivas Pandruvada 		{ "retry", required_argument, 0, 'r' },
28553fb4f7cdSSrinivas Pandruvada 		{ "version", no_argument, 0, 'v' },
28567fd786dfSSrinivas Pandruvada 		{ "oob", no_argument, 0, 'b' },
28577fd786dfSSrinivas Pandruvada 		{ "no-daemon", no_argument, 0, 'n' },
28587fd786dfSSrinivas Pandruvada 		{ "poll-interval", required_argument, 0, 'w' },
28593fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
28603fb4f7cdSSrinivas Pandruvada 	};
28613fb4f7cdSSrinivas Pandruvada 
2862f362cdccSSrinivas Pandruvada 	if (geteuid() != 0) {
2863f362cdccSSrinivas Pandruvada 		fprintf(stderr, "Must run as root\n");
2864f362cdccSSrinivas Pandruvada 		exit(0);
2865f362cdccSSrinivas Pandruvada 	}
2866f362cdccSSrinivas Pandruvada 
2867f362cdccSSrinivas Pandruvada 	ret = update_cpu_model();
2868f362cdccSSrinivas Pandruvada 	if (ret)
2869f362cdccSSrinivas Pandruvada 		err(-1, "Invalid CPU model (%d)\n", cpu_model);
2870f362cdccSSrinivas Pandruvada 	printf("Intel(R) Speed Select Technology\n");
2871f362cdccSSrinivas Pandruvada 	printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2872f362cdccSSrinivas Pandruvada 
2873f362cdccSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2874f362cdccSSrinivas Pandruvada 		fp = fopen(pathname, "rb");
2875f362cdccSSrinivas Pandruvada 		if (!fp) {
2876f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
2877f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
2878f362cdccSSrinivas Pandruvada 			fprintf(stderr, "If the config is included then this is not a supported platform.\n");
2879f362cdccSSrinivas Pandruvada 			exit(0);
2880f362cdccSSrinivas Pandruvada 		}
2881f362cdccSSrinivas Pandruvada 		fclose(fp);
2882f362cdccSSrinivas Pandruvada 	}
2883f362cdccSSrinivas Pandruvada 
28843fb4f7cdSSrinivas Pandruvada 	progname = argv[0];
28857fd786dfSSrinivas Pandruvada 	while ((opt = getopt_long_only(argc, argv, "+c:df:hio:vabw:n", long_options,
28863fb4f7cdSSrinivas Pandruvada 				       &option_index)) != -1) {
28873fb4f7cdSSrinivas Pandruvada 		switch (opt) {
28880d3dfd75SSrinivas Pandruvada 		case 'a':
28890d3dfd75SSrinivas Pandruvada 			force_cpus_online = 1;
28900d3dfd75SSrinivas Pandruvada 			break;
28913fb4f7cdSSrinivas Pandruvada 		case 'c':
28923fb4f7cdSSrinivas Pandruvada 			parse_cpu_command(optarg);
28933fb4f7cdSSrinivas Pandruvada 			break;
28943fb4f7cdSSrinivas Pandruvada 		case 'd':
28953fb4f7cdSSrinivas Pandruvada 			debug_flag = 1;
28963fb4f7cdSSrinivas Pandruvada 			printf("Debug Mode ON\n");
28973fb4f7cdSSrinivas Pandruvada 			break;
28983fb4f7cdSSrinivas Pandruvada 		case 'f':
28993fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "json", 4))
29003fb4f7cdSSrinivas Pandruvada 				out_format_json = 1;
29013fb4f7cdSSrinivas Pandruvada 			break;
29023fb4f7cdSSrinivas Pandruvada 		case 'h':
29033fb4f7cdSSrinivas Pandruvada 			usage();
29043fb4f7cdSSrinivas Pandruvada 			break;
29053fb4f7cdSSrinivas Pandruvada 		case 'i':
29063fb4f7cdSSrinivas Pandruvada 			isst_print_platform_information();
29073fb4f7cdSSrinivas Pandruvada 			break;
29083fb4f7cdSSrinivas Pandruvada 		case 'o':
29093fb4f7cdSSrinivas Pandruvada 			if (outf)
29103fb4f7cdSSrinivas Pandruvada 				fclose(outf);
29113fb4f7cdSSrinivas Pandruvada 			outf = fopen_or_exit(optarg, "w");
29123fb4f7cdSSrinivas Pandruvada 			break;
2913a85a35fcSSrinivas Pandruvada 		case 'p':
2914a85a35fcSSrinivas Pandruvada 			ret = strtol(optarg, &ptr, 10);
2915a85a35fcSSrinivas Pandruvada 			if (!ret)
2916a85a35fcSSrinivas Pandruvada 				fprintf(stderr, "Invalid pause interval, ignore\n");
2917a85a35fcSSrinivas Pandruvada 			else
2918a85a35fcSSrinivas Pandruvada 				mbox_delay = ret;
2919a85a35fcSSrinivas Pandruvada 			break;
292032279be7SSrinivas Pandruvada 		case 'r':
292132279be7SSrinivas Pandruvada 			ret = strtol(optarg, &ptr, 10);
292232279be7SSrinivas Pandruvada 			if (!ret)
292332279be7SSrinivas Pandruvada 				fprintf(stderr, "Invalid retry count, ignore\n");
292432279be7SSrinivas Pandruvada 			else
292532279be7SSrinivas Pandruvada 				mbox_retries = ret;
292632279be7SSrinivas Pandruvada 			break;
29273fb4f7cdSSrinivas Pandruvada 		case 'v':
29283fb4f7cdSSrinivas Pandruvada 			print_version();
29293fb4f7cdSSrinivas Pandruvada 			break;
29307fd786dfSSrinivas Pandruvada 		case 'b':
29317fd786dfSSrinivas Pandruvada 			oob_mode = 1;
29327fd786dfSSrinivas Pandruvada 			break;
29337fd786dfSSrinivas Pandruvada 		case 'n':
29347fd786dfSSrinivas Pandruvada 			no_daemon = 1;
29357fd786dfSSrinivas Pandruvada 			break;
29367fd786dfSSrinivas Pandruvada 		case 'w':
29377fd786dfSSrinivas Pandruvada 			ret = strtol(optarg, &ptr, 10);
29387fd786dfSSrinivas Pandruvada 			if (!ret) {
29397fd786dfSSrinivas Pandruvada 				fprintf(stderr, "Invalid poll interval count\n");
29407fd786dfSSrinivas Pandruvada 				exit(0);
29417fd786dfSSrinivas Pandruvada 			}
29427fd786dfSSrinivas Pandruvada 			poll_interval = ret;
29437fd786dfSSrinivas Pandruvada 			break;
29443fb4f7cdSSrinivas Pandruvada 		default:
29453fb4f7cdSSrinivas Pandruvada 			usage();
29463fb4f7cdSSrinivas Pandruvada 		}
29473fb4f7cdSSrinivas Pandruvada 	}
29483fb4f7cdSSrinivas Pandruvada 
29497fd786dfSSrinivas Pandruvada 	if (optind > (argc - 2) && !oob_mode) {
2950addd116dSSrinivas Pandruvada 		usage();
29513fb4f7cdSSrinivas Pandruvada 		exit(0);
29523fb4f7cdSSrinivas Pandruvada 	}
29533fb4f7cdSSrinivas Pandruvada 	set_max_cpu_num();
29540d3dfd75SSrinivas Pandruvada 	if (force_cpus_online)
29550d3dfd75SSrinivas Pandruvada 		force_all_cpus_online();
2956fb186158SSrinivas Pandruvada 	store_cpu_topology();
29573fb4f7cdSSrinivas Pandruvada 	set_cpu_present_cpu_mask();
29583fb4f7cdSSrinivas Pandruvada 	set_cpu_target_cpu_mask();
2959c829f0efSPrarit Bhargava 
29607fd786dfSSrinivas Pandruvada 	if (oob_mode) {
29617fd786dfSSrinivas Pandruvada 		create_cpu_map();
29627fd786dfSSrinivas Pandruvada 		if (debug_flag)
29637fd786dfSSrinivas Pandruvada 			fprintf(stderr, "OOB mode is enabled in debug mode\n");
29647fd786dfSSrinivas Pandruvada 
29657fd786dfSSrinivas Pandruvada 		ret = isst_daemon(debug_flag, poll_interval, no_daemon);
29667fd786dfSSrinivas Pandruvada 		if (ret)
29677fd786dfSSrinivas Pandruvada 			fprintf(stderr, "OOB mode enable failed\n");
29687fd786dfSSrinivas Pandruvada 		goto out;
29697fd786dfSSrinivas Pandruvada 	}
29707fd786dfSSrinivas Pandruvada 
2971c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
29723bc3d30cSPrarit Bhargava 		ret = isst_fill_platform_info();
29733bc3d30cSPrarit Bhargava 		if (ret)
29743bc3d30cSPrarit Bhargava 			goto out;
2975210369dcSPrarit Bhargava 		process_command(argc, argv, isst_help_cmds, isst_cmds);
2976c829f0efSPrarit Bhargava 	} else {
2977c829f0efSPrarit Bhargava 		process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
2978c829f0efSPrarit Bhargava 	}
29793bc3d30cSPrarit Bhargava out:
29803bc3d30cSPrarit Bhargava 	free_cpu_set(present_cpumask);
29813bc3d30cSPrarit Bhargava 	free_cpu_set(target_cpumask);
29823fb4f7cdSSrinivas Pandruvada }
29833fb4f7cdSSrinivas Pandruvada 
29843fb4f7cdSSrinivas Pandruvada int main(int argc, char **argv)
29853fb4f7cdSSrinivas Pandruvada {
29863fb4f7cdSSrinivas Pandruvada 	outf = stderr;
29873fb4f7cdSSrinivas Pandruvada 	cmdline(argc, argv);
29883fb4f7cdSSrinivas Pandruvada 	return 0;
29893fb4f7cdSSrinivas Pandruvada }
2990