xref: /linux/tools/power/x86/intel-speed-select/isst-config.c (revision 56d6469291f8b1e1ab2f78e401c564163d3ee0f0)
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 
18f3874e96SSrinivas Pandruvada static const char *version_str = "v1.12";
19f3874e96SSrinivas 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 
362850337ecSZhang Rui void set_isst_id(struct isst_id *id, int cpu)
363850337ecSZhang Rui {
364850337ecSZhang Rui 	id->cpu = cpu;
36532d6ab45SZhang Rui 	id->pkg = get_physical_package_id(cpu);
36632d6ab45SZhang Rui 	id->die = get_physical_die_id(cpu);
367850337ecSZhang Rui }
368850337ecSZhang Rui 
3697af5a95bSSrinivas Pandruvada int get_cpufreq_base_freq(int cpu)
3707af5a95bSSrinivas Pandruvada {
3717af5a95bSSrinivas Pandruvada 	return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
3727af5a95bSSrinivas Pandruvada }
3737af5a95bSSrinivas Pandruvada 
3743fb4f7cdSSrinivas Pandruvada int get_topo_max_cpus(void)
3753fb4f7cdSSrinivas Pandruvada {
3763fb4f7cdSSrinivas Pandruvada 	return topo_max_cpus;
3773fb4f7cdSSrinivas Pandruvada }
3783fb4f7cdSSrinivas Pandruvada 
3797fd786dfSSrinivas Pandruvada void set_cpu_online_offline(int cpu, int state)
3803c64c81aSSrinivas Pandruvada {
3813c64c81aSSrinivas Pandruvada 	char buffer[128];
382abd120e3SSrinivas Pandruvada 	int fd, ret;
3833c64c81aSSrinivas Pandruvada 
3843c64c81aSSrinivas Pandruvada 	snprintf(buffer, sizeof(buffer),
3853c64c81aSSrinivas Pandruvada 		 "/sys/devices/system/cpu/cpu%d/online", cpu);
3863c64c81aSSrinivas Pandruvada 
3873c64c81aSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
38869669198SSrinivas Pandruvada 	if (fd < 0) {
38969669198SSrinivas Pandruvada 		if (!cpu && state) {
39069669198SSrinivas Pandruvada 			fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
39169669198SSrinivas Pandruvada 			fprintf(stderr, "Ignoring online request for CPU 0 as this is already online\n");
39269669198SSrinivas Pandruvada 			return;
39369669198SSrinivas Pandruvada 		}
3943c64c81aSSrinivas Pandruvada 		err(-1, "%s open failed", buffer);
39569669198SSrinivas Pandruvada 	}
3963c64c81aSSrinivas Pandruvada 
3973c64c81aSSrinivas Pandruvada 	if (state)
398abd120e3SSrinivas Pandruvada 		ret = write(fd, "1\n", 2);
3993c64c81aSSrinivas Pandruvada 	else
400abd120e3SSrinivas Pandruvada 		ret = write(fd, "0\n", 2);
401abd120e3SSrinivas Pandruvada 
402abd120e3SSrinivas Pandruvada 	if (ret == -1)
403abd120e3SSrinivas Pandruvada 		perror("Online/Offline: Operation failed\n");
4043c64c81aSSrinivas Pandruvada 
4053c64c81aSSrinivas Pandruvada 	close(fd);
4063c64c81aSSrinivas Pandruvada }
4073c64c81aSSrinivas Pandruvada 
4080d3dfd75SSrinivas Pandruvada static void force_all_cpus_online(void)
4090d3dfd75SSrinivas Pandruvada {
4100d3dfd75SSrinivas Pandruvada 	int i;
4110d3dfd75SSrinivas Pandruvada 
4120d3dfd75SSrinivas Pandruvada 	fprintf(stderr, "Forcing all CPUs online\n");
4130d3dfd75SSrinivas Pandruvada 
4140d3dfd75SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i)
4150d3dfd75SSrinivas Pandruvada 		set_cpu_online_offline(i, 1);
4160d3dfd75SSrinivas Pandruvada 
4170d3dfd75SSrinivas Pandruvada 	unlink("/var/run/isst_cpu_topology.dat");
4180d3dfd75SSrinivas Pandruvada }
4190d3dfd75SSrinivas Pandruvada 
420850337ecSZhang Rui void for_each_online_package_in_set(void (*callback)(struct isst_id *, void *, void *,
4213fb4f7cdSSrinivas Pandruvada 						     void *, void *),
4223fb4f7cdSSrinivas Pandruvada 				    void *arg1, void *arg2, void *arg3,
4233fb4f7cdSSrinivas Pandruvada 				    void *arg4)
4243fb4f7cdSSrinivas Pandruvada {
4253fb4f7cdSSrinivas Pandruvada 	int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
4263fb4f7cdSSrinivas Pandruvada 	int pkg_index = 0, i;
427850337ecSZhang Rui 	struct isst_id id;
4283fb4f7cdSSrinivas Pandruvada 
4293fb4f7cdSSrinivas Pandruvada 	memset(max_packages, 0xff, sizeof(max_packages));
4303fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
4313fb4f7cdSSrinivas Pandruvada 		int j, online, pkg_id, die_id = 0, skip = 0;
4323fb4f7cdSSrinivas Pandruvada 
4333fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
4343fb4f7cdSSrinivas Pandruvada 			continue;
4353fb4f7cdSSrinivas Pandruvada 		if (i)
4363fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
4373fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
4383fb4f7cdSSrinivas Pandruvada 		else
4393fb4f7cdSSrinivas Pandruvada 			online =
4403fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
4413fb4f7cdSSrinivas Pandruvada 
4423fb4f7cdSSrinivas Pandruvada 		die_id = get_physical_die_id(i);
4433fb4f7cdSSrinivas Pandruvada 		if (die_id < 0)
4443fb4f7cdSSrinivas Pandruvada 			die_id = 0;
445fb186158SSrinivas Pandruvada 
446fb186158SSrinivas Pandruvada 		pkg_id = parse_int_file(0,
447fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
448fb186158SSrinivas Pandruvada 		if (pkg_id < 0)
449f0e0b4d1SSrinivas Pandruvada 			continue;
450fb186158SSrinivas Pandruvada 
4513fb4f7cdSSrinivas Pandruvada 		/* Create an unique id for package, die combination to store */
4523fb4f7cdSSrinivas Pandruvada 		pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
4533fb4f7cdSSrinivas Pandruvada 
4543fb4f7cdSSrinivas Pandruvada 		for (j = 0; j < pkg_index; ++j) {
4553fb4f7cdSSrinivas Pandruvada 			if (max_packages[j] == pkg_id) {
4563fb4f7cdSSrinivas Pandruvada 				skip = 1;
4573fb4f7cdSSrinivas Pandruvada 				break;
4583fb4f7cdSSrinivas Pandruvada 			}
4593fb4f7cdSSrinivas Pandruvada 		}
4603fb4f7cdSSrinivas Pandruvada 
461850337ecSZhang Rui 		set_isst_id(&id, i);
4623fb4f7cdSSrinivas Pandruvada 		if (!skip && online && callback) {
463850337ecSZhang Rui 			callback(&id, arg1, arg2, arg3, arg4);
4643fb4f7cdSSrinivas Pandruvada 			max_packages[pkg_index++] = pkg_id;
4653fb4f7cdSSrinivas Pandruvada 		}
4663fb4f7cdSSrinivas Pandruvada 	}
4673fb4f7cdSSrinivas Pandruvada }
4683fb4f7cdSSrinivas Pandruvada 
4693fb4f7cdSSrinivas Pandruvada static void for_each_online_target_cpu_in_set(
470850337ecSZhang Rui 	void (*callback)(struct isst_id *, void *, void *, void *, void *), void *arg1,
4713fb4f7cdSSrinivas Pandruvada 	void *arg2, void *arg3, void *arg4)
4723fb4f7cdSSrinivas Pandruvada {
473070fdea1SSrinivas Pandruvada 	int i, found = 0;
474850337ecSZhang Rui 	struct isst_id id;
4753fb4f7cdSSrinivas Pandruvada 
4763fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
4773fb4f7cdSSrinivas Pandruvada 		int online;
4783fb4f7cdSSrinivas Pandruvada 
4793fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
4803fb4f7cdSSrinivas Pandruvada 			continue;
4813fb4f7cdSSrinivas Pandruvada 		if (i)
4823fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
4833fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
4843fb4f7cdSSrinivas Pandruvada 		else
4853fb4f7cdSSrinivas Pandruvada 			online =
4863fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
4873fb4f7cdSSrinivas Pandruvada 
488850337ecSZhang Rui 		set_isst_id(&id, i);
489070fdea1SSrinivas Pandruvada 		if (online && callback) {
490850337ecSZhang Rui 			callback(&id, arg1, arg2, arg3, arg4);
491070fdea1SSrinivas Pandruvada 			found = 1;
4923fb4f7cdSSrinivas Pandruvada 		}
4933fb4f7cdSSrinivas Pandruvada 	}
4943fb4f7cdSSrinivas Pandruvada 
495070fdea1SSrinivas Pandruvada 	if (!found)
496070fdea1SSrinivas Pandruvada 		fprintf(stderr, "No valid CPU in the list\n");
497070fdea1SSrinivas Pandruvada }
498070fdea1SSrinivas Pandruvada 
4993fb4f7cdSSrinivas Pandruvada #define BITMASK_SIZE 32
5003fb4f7cdSSrinivas Pandruvada static void set_max_cpu_num(void)
5013fb4f7cdSSrinivas Pandruvada {
5023fb4f7cdSSrinivas Pandruvada 	FILE *filep;
5033fb4f7cdSSrinivas Pandruvada 	unsigned long dummy;
504864dc09eSSrinivas Pandruvada 	int i;
5053fb4f7cdSSrinivas Pandruvada 
5063fb4f7cdSSrinivas Pandruvada 	topo_max_cpus = 0;
507864dc09eSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
508864dc09eSSrinivas Pandruvada 		char path[256];
509864dc09eSSrinivas Pandruvada 
510864dc09eSSrinivas Pandruvada 		snprintf(path, sizeof(path),
511864dc09eSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
512864dc09eSSrinivas Pandruvada 		filep = fopen(path, "r");
513864dc09eSSrinivas Pandruvada 		if (filep)
514864dc09eSSrinivas Pandruvada 			break;
515864dc09eSSrinivas Pandruvada 	}
516864dc09eSSrinivas Pandruvada 
517864dc09eSSrinivas Pandruvada 	if (!filep) {
518864dc09eSSrinivas Pandruvada 		fprintf(stderr, "Can't get max cpu number\n");
519864dc09eSSrinivas Pandruvada 		exit(0);
520864dc09eSSrinivas Pandruvada 	}
521864dc09eSSrinivas Pandruvada 
5223fb4f7cdSSrinivas Pandruvada 	while (fscanf(filep, "%lx,", &dummy) == 1)
5233fb4f7cdSSrinivas Pandruvada 		topo_max_cpus += BITMASK_SIZE;
5243fb4f7cdSSrinivas Pandruvada 	fclose(filep);
5253fb4f7cdSSrinivas Pandruvada 
5263fb4f7cdSSrinivas Pandruvada 	debug_printf("max cpus %d\n", topo_max_cpus);
5273fb4f7cdSSrinivas Pandruvada }
5283fb4f7cdSSrinivas Pandruvada 
5293fb4f7cdSSrinivas Pandruvada size_t alloc_cpu_set(cpu_set_t **cpu_set)
5303fb4f7cdSSrinivas Pandruvada {
5313fb4f7cdSSrinivas Pandruvada 	cpu_set_t *_cpu_set;
5323fb4f7cdSSrinivas Pandruvada 	size_t size;
5333fb4f7cdSSrinivas Pandruvada 
5343fb4f7cdSSrinivas Pandruvada 	_cpu_set = CPU_ALLOC((topo_max_cpus + 1));
5353fb4f7cdSSrinivas Pandruvada 	if (_cpu_set == NULL)
5363fb4f7cdSSrinivas Pandruvada 		err(3, "CPU_ALLOC");
5373fb4f7cdSSrinivas Pandruvada 	size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
5383fb4f7cdSSrinivas Pandruvada 	CPU_ZERO_S(size, _cpu_set);
5393fb4f7cdSSrinivas Pandruvada 
5403fb4f7cdSSrinivas Pandruvada 	*cpu_set = _cpu_set;
5413fb4f7cdSSrinivas Pandruvada 	return size;
5423fb4f7cdSSrinivas Pandruvada }
5433fb4f7cdSSrinivas Pandruvada 
5443fb4f7cdSSrinivas Pandruvada void free_cpu_set(cpu_set_t *cpu_set)
5453fb4f7cdSSrinivas Pandruvada {
5463fb4f7cdSSrinivas Pandruvada 	CPU_FREE(cpu_set);
5473fb4f7cdSSrinivas Pandruvada }
5483fb4f7cdSSrinivas Pandruvada 
5493fb4f7cdSSrinivas Pandruvada static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
5503fb4f7cdSSrinivas Pandruvada static void set_cpu_present_cpu_mask(void)
5513fb4f7cdSSrinivas Pandruvada {
5523fb4f7cdSSrinivas Pandruvada 	size_t size;
5533fb4f7cdSSrinivas Pandruvada 	DIR *dir;
5543fb4f7cdSSrinivas Pandruvada 	int i;
5553fb4f7cdSSrinivas Pandruvada 
5563fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&present_cpumask);
5573fb4f7cdSSrinivas Pandruvada 	present_cpumask_size = size;
5583fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
5593fb4f7cdSSrinivas Pandruvada 		char buffer[256];
5603fb4f7cdSSrinivas Pandruvada 
5613fb4f7cdSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
5623fb4f7cdSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d", i);
5633fb4f7cdSSrinivas Pandruvada 		dir = opendir(buffer);
5643fb4f7cdSSrinivas Pandruvada 		if (dir) {
5653fb4f7cdSSrinivas Pandruvada 			int pkg_id, die_id;
5663fb4f7cdSSrinivas Pandruvada 
5673fb4f7cdSSrinivas Pandruvada 			CPU_SET_S(i, size, present_cpumask);
5683fb4f7cdSSrinivas Pandruvada 			die_id = get_physical_die_id(i);
5693fb4f7cdSSrinivas Pandruvada 			if (die_id < 0)
5703fb4f7cdSSrinivas Pandruvada 				die_id = 0;
5713fb4f7cdSSrinivas Pandruvada 
5723fb4f7cdSSrinivas Pandruvada 			pkg_id = get_physical_package_id(i);
573f0e0b4d1SSrinivas Pandruvada 			if (pkg_id < 0) {
574f0e0b4d1SSrinivas Pandruvada 				fprintf(stderr, "Failed to get package id, CPU %d may be offline\n", i);
575f0e0b4d1SSrinivas Pandruvada 				continue;
576f0e0b4d1SSrinivas Pandruvada 			}
5773fb4f7cdSSrinivas Pandruvada 			if (pkg_id < MAX_PACKAGE_COUNT &&
578e2783369SZhang Rui 			    die_id < MAX_DIE_PER_PACKAGE)
5793fb4f7cdSSrinivas Pandruvada 				cpu_cnt[pkg_id][die_id]++;
5803fb4f7cdSSrinivas Pandruvada 		}
5813fb4f7cdSSrinivas Pandruvada 		closedir(dir);
5823fb4f7cdSSrinivas Pandruvada 	}
5833fb4f7cdSSrinivas Pandruvada }
5843fb4f7cdSSrinivas Pandruvada 
58530e0600eSZhang Rui int get_max_punit_core_id(struct isst_id *id)
586de7f9d3dSSrinivas Pandruvada {
5877566616fSJonathan Doman 	int max_id = 0;
588de7f9d3dSSrinivas Pandruvada 	int i;
589de7f9d3dSSrinivas Pandruvada 
5907566616fSJonathan Doman 	for (i = 0; i < topo_max_cpus; ++i)
5917566616fSJonathan Doman 	{
5927566616fSJonathan Doman 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
5937566616fSJonathan Doman 			continue;
5947566616fSJonathan Doman 
59530e0600eSZhang Rui 		if (cpu_map[i].pkg_id == id->pkg &&
59630e0600eSZhang Rui 			cpu_map[i].die_id == id->die &&
5977566616fSJonathan Doman 			cpu_map[i].punit_cpu_core > max_id)
5987566616fSJonathan Doman 			max_id = cpu_map[i].punit_cpu_core;
599de7f9d3dSSrinivas Pandruvada 	}
600de7f9d3dSSrinivas Pandruvada 
6017566616fSJonathan Doman 	return max_id;
602de7f9d3dSSrinivas Pandruvada }
603de7f9d3dSSrinivas Pandruvada 
60430e0600eSZhang Rui int get_cpu_count(struct isst_id *id)
6053fb4f7cdSSrinivas Pandruvada {
60630e0600eSZhang Rui 	if (id->pkg < MAX_PACKAGE_COUNT && id->die < MAX_DIE_PER_PACKAGE)
60730e0600eSZhang Rui 		return cpu_cnt[id->pkg][id->die];
6083fb4f7cdSSrinivas Pandruvada 
6093fb4f7cdSSrinivas Pandruvada 	return 0;
6103fb4f7cdSSrinivas Pandruvada }
6113fb4f7cdSSrinivas Pandruvada 
6123fb4f7cdSSrinivas Pandruvada static void set_cpu_target_cpu_mask(void)
6133fb4f7cdSSrinivas Pandruvada {
6143fb4f7cdSSrinivas Pandruvada 	size_t size;
6153fb4f7cdSSrinivas Pandruvada 	int i;
6163fb4f7cdSSrinivas Pandruvada 
6173fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&target_cpumask);
6183fb4f7cdSSrinivas Pandruvada 	target_cpumask_size = size;
6193fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < max_target_cpus; ++i) {
6203fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
6213fb4f7cdSSrinivas Pandruvada 				 present_cpumask))
6223fb4f7cdSSrinivas Pandruvada 			continue;
6233fb4f7cdSSrinivas Pandruvada 
6243fb4f7cdSSrinivas Pandruvada 		CPU_SET_S(target_cpus[i], size, target_cpumask);
6253fb4f7cdSSrinivas Pandruvada 	}
6263fb4f7cdSSrinivas Pandruvada }
6273fb4f7cdSSrinivas Pandruvada 
6283fb4f7cdSSrinivas Pandruvada static void create_cpu_map(void)
6293fb4f7cdSSrinivas Pandruvada {
6303fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
6313fb4f7cdSSrinivas Pandruvada 	int i, fd = 0;
6323fb4f7cdSSrinivas Pandruvada 	struct isst_if_cpu_maps map;
6333fb4f7cdSSrinivas Pandruvada 
6343fb4f7cdSSrinivas Pandruvada 	cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
6353fb4f7cdSSrinivas Pandruvada 	if (!cpu_map)
6363fb4f7cdSSrinivas Pandruvada 		err(3, "cpumap");
6373fb4f7cdSSrinivas Pandruvada 
6383fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
6393fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
6403fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
6413fb4f7cdSSrinivas Pandruvada 
6423fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
6433fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
6443fb4f7cdSSrinivas Pandruvada 			continue;
6453fb4f7cdSSrinivas Pandruvada 
6463fb4f7cdSSrinivas Pandruvada 		map.cmd_count = 1;
6473fb4f7cdSSrinivas Pandruvada 		map.cpu_map[0].logical_cpu = i;
6483fb4f7cdSSrinivas Pandruvada 
6493fb4f7cdSSrinivas Pandruvada 		debug_printf(" map logical_cpu:%d\n",
6503fb4f7cdSSrinivas Pandruvada 			     map.cpu_map[0].logical_cpu);
6513fb4f7cdSSrinivas Pandruvada 		if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
6523fb4f7cdSSrinivas Pandruvada 			perror("ISST_IF_GET_PHY_ID");
6533fb4f7cdSSrinivas Pandruvada 			fprintf(outf, "Error: map logical_cpu:%d\n",
6543fb4f7cdSSrinivas Pandruvada 				map.cpu_map[0].logical_cpu);
6553fb4f7cdSSrinivas Pandruvada 			continue;
6563fb4f7cdSSrinivas Pandruvada 		}
6573fb4f7cdSSrinivas Pandruvada 		cpu_map[i].core_id = get_physical_core_id(i);
6583fb4f7cdSSrinivas Pandruvada 		cpu_map[i].pkg_id = get_physical_package_id(i);
6593fb4f7cdSSrinivas Pandruvada 		cpu_map[i].die_id = get_physical_die_id(i);
6603fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
6613fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
6623fb4f7cdSSrinivas Pandruvada 					     1); // shift to get core id
6633fb4f7cdSSrinivas Pandruvada 
6643fb4f7cdSSrinivas Pandruvada 		debug_printf(
6653fb4f7cdSSrinivas Pandruvada 			"map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
6663fb4f7cdSSrinivas Pandruvada 			i, cpu_map[i].core_id, cpu_map[i].die_id,
6673fb4f7cdSSrinivas Pandruvada 			cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
6683fb4f7cdSSrinivas Pandruvada 			cpu_map[i].punit_cpu_core);
6693fb4f7cdSSrinivas Pandruvada 	}
6703fb4f7cdSSrinivas Pandruvada 
6713fb4f7cdSSrinivas Pandruvada 	if (fd)
6723fb4f7cdSSrinivas Pandruvada 		close(fd);
6733fb4f7cdSSrinivas Pandruvada }
6743fb4f7cdSSrinivas Pandruvada 
675850337ecSZhang Rui void set_cpu_mask_from_punit_coremask(struct isst_id *id, unsigned long long core_mask,
6763fb4f7cdSSrinivas Pandruvada 				      size_t core_cpumask_size,
6773fb4f7cdSSrinivas Pandruvada 				      cpu_set_t *core_cpumask, int *cpu_cnt)
6783fb4f7cdSSrinivas Pandruvada {
6793fb4f7cdSSrinivas Pandruvada 	int i, cnt = 0;
6803fb4f7cdSSrinivas Pandruvada 
6813fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = 0;
6823fb4f7cdSSrinivas Pandruvada 
6833fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < 64; ++i) {
684873e391fSSrinivas Pandruvada 		if (core_mask & BIT_ULL(i)) {
6853fb4f7cdSSrinivas Pandruvada 			int j;
6863fb4f7cdSSrinivas Pandruvada 
6873fb4f7cdSSrinivas Pandruvada 			for (j = 0; j < topo_max_cpus; ++j) {
68844460efeSYouquan Song 				if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
68944460efeSYouquan Song 					continue;
69044460efeSYouquan Song 
691*56d64692SZhang Rui 				if (cpu_map[j].pkg_id == id->pkg &&
692*56d64692SZhang Rui 				    cpu_map[j].die_id == id->die &&
6933fb4f7cdSSrinivas Pandruvada 				    cpu_map[j].punit_cpu_core == i) {
6943fb4f7cdSSrinivas Pandruvada 					CPU_SET_S(j, core_cpumask_size,
6953fb4f7cdSSrinivas Pandruvada 						  core_cpumask);
6963fb4f7cdSSrinivas Pandruvada 					++cnt;
6973fb4f7cdSSrinivas Pandruvada 				}
6983fb4f7cdSSrinivas Pandruvada 			}
6993fb4f7cdSSrinivas Pandruvada 		}
7003fb4f7cdSSrinivas Pandruvada 	}
7013fb4f7cdSSrinivas Pandruvada 
7023fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = cnt;
7033fb4f7cdSSrinivas Pandruvada }
7043fb4f7cdSSrinivas Pandruvada 
7053fb4f7cdSSrinivas Pandruvada int find_phy_core_num(int logical_cpu)
7063fb4f7cdSSrinivas Pandruvada {
7073fb4f7cdSSrinivas Pandruvada 	if (logical_cpu < topo_max_cpus)
7083fb4f7cdSSrinivas Pandruvada 		return cpu_map[logical_cpu].punit_cpu_core;
7093fb4f7cdSSrinivas Pandruvada 
7103fb4f7cdSSrinivas Pandruvada 	return -EINVAL;
7113fb4f7cdSSrinivas Pandruvada }
7123fb4f7cdSSrinivas Pandruvada 
7133fb4f7cdSSrinivas Pandruvada static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
7143fb4f7cdSSrinivas Pandruvada 				  unsigned int *value)
7153fb4f7cdSSrinivas Pandruvada {
7163fb4f7cdSSrinivas Pandruvada 	struct isst_if_io_regs io_regs;
7173fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
7183fb4f7cdSSrinivas Pandruvada 	int cmd;
7193fb4f7cdSSrinivas Pandruvada 	int fd;
7203fb4f7cdSSrinivas Pandruvada 
7213fb4f7cdSSrinivas Pandruvada 	debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
7223fb4f7cdSSrinivas Pandruvada 
7233fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
7243fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
7253fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
7263fb4f7cdSSrinivas Pandruvada 
7273fb4f7cdSSrinivas Pandruvada 	io_regs.req_count = 1;
7283fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].logical_cpu = cpu;
7293fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].reg = reg;
7303fb4f7cdSSrinivas Pandruvada 	cmd = ISST_IF_IO_CMD;
7313fb4f7cdSSrinivas Pandruvada 	if (write) {
7323fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 1;
7333fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].value = *value;
7343fb4f7cdSSrinivas Pandruvada 	} else {
7353fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 0;
7363fb4f7cdSSrinivas Pandruvada 	}
7373fb4f7cdSSrinivas Pandruvada 
7383fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, cmd, &io_regs) == -1) {
73995f8e569SSrinivas Pandruvada 		if (errno == ENOTTY) {
74095f8e569SSrinivas Pandruvada 			perror("ISST_IF_IO_COMMAND\n");
74195f8e569SSrinivas Pandruvada 			fprintf(stderr, "Check presence of kernel modules: isst_if_mmio\n");
74295f8e569SSrinivas Pandruvada 			exit(0);
74395f8e569SSrinivas Pandruvada 		}
7443fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
7453fb4f7cdSSrinivas Pandruvada 			cpu, reg, write);
7463fb4f7cdSSrinivas Pandruvada 	} else {
7473fb4f7cdSSrinivas Pandruvada 		if (!write)
7483fb4f7cdSSrinivas Pandruvada 			*value = io_regs.io_reg[0].value;
7493fb4f7cdSSrinivas Pandruvada 
7503fb4f7cdSSrinivas Pandruvada 		debug_printf(
7513fb4f7cdSSrinivas Pandruvada 			"mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
7523fb4f7cdSSrinivas Pandruvada 			cpu, reg, write, *value);
7533fb4f7cdSSrinivas Pandruvada 	}
7543fb4f7cdSSrinivas Pandruvada 
7553fb4f7cdSSrinivas Pandruvada 	close(fd);
7563fb4f7cdSSrinivas Pandruvada 
7573fb4f7cdSSrinivas Pandruvada 	return 0;
7583fb4f7cdSSrinivas Pandruvada }
7593fb4f7cdSSrinivas Pandruvada 
7603fb4f7cdSSrinivas Pandruvada int isst_send_mbox_command(unsigned int cpu, unsigned char command,
7613fb4f7cdSSrinivas Pandruvada 			   unsigned char sub_command, unsigned int parameter,
7623fb4f7cdSSrinivas Pandruvada 			   unsigned int req_data, unsigned int *resp)
7633fb4f7cdSSrinivas Pandruvada {
7643fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
76532279be7SSrinivas Pandruvada 	int fd, retry;
7663fb4f7cdSSrinivas Pandruvada 	struct isst_if_mbox_cmds mbox_cmds = { 0 };
7673fb4f7cdSSrinivas Pandruvada 
7683fb4f7cdSSrinivas Pandruvada 	debug_printf(
7693fb4f7cdSSrinivas Pandruvada 		"mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
7703fb4f7cdSSrinivas Pandruvada 		cpu, command, sub_command, parameter, req_data);
7713fb4f7cdSSrinivas Pandruvada 
77295f8e569SSrinivas Pandruvada 	if (!is_skx_based_platform() && command == CONFIG_CLOS &&
7738ddbda76SSrinivas Pandruvada 	    sub_command != CLOS_PM_QOS_CONFIG) {
7743fb4f7cdSSrinivas Pandruvada 		unsigned int value;
7753fb4f7cdSSrinivas Pandruvada 		int write = 0;
7763fb4f7cdSSrinivas Pandruvada 		int clos_id, core_id, ret = 0;
7773fb4f7cdSSrinivas Pandruvada 
778d2d1f304SSrinivas Pandruvada 		debug_printf("CPU %d\n", cpu);
7793fb4f7cdSSrinivas Pandruvada 
7803fb4f7cdSSrinivas Pandruvada 		if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
7813fb4f7cdSSrinivas Pandruvada 			value = req_data;
7823fb4f7cdSSrinivas Pandruvada 			write = 1;
7833fb4f7cdSSrinivas Pandruvada 		}
7843fb4f7cdSSrinivas Pandruvada 
7853fb4f7cdSSrinivas Pandruvada 		switch (sub_command) {
7863fb4f7cdSSrinivas Pandruvada 		case CLOS_PQR_ASSOC:
7873fb4f7cdSSrinivas Pandruvada 			core_id = parameter & 0xff;
7883fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
7893fb4f7cdSSrinivas Pandruvada 				cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
7903fb4f7cdSSrinivas Pandruvada 				&value);
7913fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
7923fb4f7cdSSrinivas Pandruvada 				*resp = value;
7933fb4f7cdSSrinivas Pandruvada 			break;
7943fb4f7cdSSrinivas Pandruvada 		case CLOS_PM_CLOS:
7953fb4f7cdSSrinivas Pandruvada 			clos_id = parameter & 0x03;
7963fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
7973fb4f7cdSSrinivas Pandruvada 				cpu, PM_CLOS_OFFSET + clos_id * 4, write,
7983fb4f7cdSSrinivas Pandruvada 				&value);
7993fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
8003fb4f7cdSSrinivas Pandruvada 				*resp = value;
8013fb4f7cdSSrinivas Pandruvada 			break;
8023fb4f7cdSSrinivas Pandruvada 		case CLOS_STATUS:
8033fb4f7cdSSrinivas Pandruvada 			break;
8043fb4f7cdSSrinivas Pandruvada 		default:
8053fb4f7cdSSrinivas Pandruvada 			break;
8063fb4f7cdSSrinivas Pandruvada 		}
8073fb4f7cdSSrinivas Pandruvada 		return ret;
8083fb4f7cdSSrinivas Pandruvada 	}
8093fb4f7cdSSrinivas Pandruvada 
8103fb4f7cdSSrinivas Pandruvada 	mbox_cmds.cmd_count = 1;
8113fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
8123fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].command = command;
8133fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].sub_command = sub_command;
8143fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].parameter = parameter;
8153fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].req_data = req_data;
8163fb4f7cdSSrinivas Pandruvada 
817a85a35fcSSrinivas Pandruvada 	if (mbox_delay)
818a85a35fcSSrinivas Pandruvada 		usleep(mbox_delay * 1000);
819a85a35fcSSrinivas Pandruvada 
8203fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8213fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8223fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
8233fb4f7cdSSrinivas Pandruvada 
82432279be7SSrinivas Pandruvada 	retry = mbox_retries;
82532279be7SSrinivas Pandruvada 
82632279be7SSrinivas Pandruvada 	do {
8273fb4f7cdSSrinivas Pandruvada 		if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
82895f8e569SSrinivas Pandruvada 			if (errno == ENOTTY) {
82995f8e569SSrinivas Pandruvada 				perror("ISST_IF_MBOX_COMMAND\n");
83095f8e569SSrinivas Pandruvada 				fprintf(stderr, "Check presence of kernel modules: isst_if_mbox_pci or isst_if_mbox_msr\n");
83195f8e569SSrinivas Pandruvada 				exit(0);
83295f8e569SSrinivas Pandruvada 			}
83395f8e569SSrinivas Pandruvada 			debug_printf(
83495f8e569SSrinivas Pandruvada 				"Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x errorno:%d\n",
83595f8e569SSrinivas Pandruvada 				cpu, command, sub_command, parameter, req_data, errno);
83632279be7SSrinivas Pandruvada 			--retry;
8373fb4f7cdSSrinivas Pandruvada 		} else {
8383fb4f7cdSSrinivas Pandruvada 			*resp = mbox_cmds.mbox_cmd[0].resp_data;
8393fb4f7cdSSrinivas Pandruvada 			debug_printf(
8403fb4f7cdSSrinivas Pandruvada 				"mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
8413fb4f7cdSSrinivas Pandruvada 				cpu, command, sub_command, parameter, req_data, *resp);
84232279be7SSrinivas Pandruvada 			break;
8433fb4f7cdSSrinivas Pandruvada 		}
84432279be7SSrinivas Pandruvada 	} while (retry);
8453fb4f7cdSSrinivas Pandruvada 
8463fb4f7cdSSrinivas Pandruvada 	close(fd);
8473fb4f7cdSSrinivas Pandruvada 
84832279be7SSrinivas Pandruvada 	if (!retry) {
84932279be7SSrinivas Pandruvada 		debug_printf("Failed mbox command even after retries\n");
85032279be7SSrinivas Pandruvada 		return -1;
85132279be7SSrinivas Pandruvada 
85232279be7SSrinivas Pandruvada 	}
8533fb4f7cdSSrinivas Pandruvada 	return 0;
8543fb4f7cdSSrinivas Pandruvada }
8553fb4f7cdSSrinivas Pandruvada 
8563fb4f7cdSSrinivas Pandruvada int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
8573fb4f7cdSSrinivas Pandruvada 			  unsigned long long *req_resp)
8583fb4f7cdSSrinivas Pandruvada {
8593fb4f7cdSSrinivas Pandruvada 	struct isst_if_msr_cmds msr_cmds;
8603fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
8613fb4f7cdSSrinivas Pandruvada 	int fd;
8623fb4f7cdSSrinivas Pandruvada 
8633fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8643fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8653fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
8663fb4f7cdSSrinivas Pandruvada 
8673fb4f7cdSSrinivas Pandruvada 	msr_cmds.cmd_count = 1;
8683fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].logical_cpu = cpu;
8693fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].msr = msr;
8703fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].read_write = write;
8713fb4f7cdSSrinivas Pandruvada 	if (write)
8723fb4f7cdSSrinivas Pandruvada 		msr_cmds.msr_cmd[0].data = *req_resp;
8733fb4f7cdSSrinivas Pandruvada 
8743fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
8759945a247SMasanari Iida 		perror("ISST_IF_MSR_COMMAND");
8763fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
8773fb4f7cdSSrinivas Pandruvada 			cpu, msr, write);
8783fb4f7cdSSrinivas Pandruvada 	} else {
8793fb4f7cdSSrinivas Pandruvada 		if (!write)
8803fb4f7cdSSrinivas Pandruvada 			*req_resp = msr_cmds.msr_cmd[0].data;
8813fb4f7cdSSrinivas Pandruvada 
8823fb4f7cdSSrinivas Pandruvada 		debug_printf(
8833fb4f7cdSSrinivas Pandruvada 			"msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
8843fb4f7cdSSrinivas Pandruvada 			cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
8853fb4f7cdSSrinivas Pandruvada 	}
8863fb4f7cdSSrinivas Pandruvada 
8873fb4f7cdSSrinivas Pandruvada 	close(fd);
8883fb4f7cdSSrinivas Pandruvada 
8893fb4f7cdSSrinivas Pandruvada 	return 0;
8903fb4f7cdSSrinivas Pandruvada }
8913fb4f7cdSSrinivas Pandruvada 
8923fb4f7cdSSrinivas Pandruvada static int isst_fill_platform_info(void)
8933fb4f7cdSSrinivas Pandruvada {
8943fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
8953fb4f7cdSSrinivas Pandruvada 	int fd;
8963fb4f7cdSSrinivas Pandruvada 
8973fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8983fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8993fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
9003fb4f7cdSSrinivas Pandruvada 
9013fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
9023fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
9033fb4f7cdSSrinivas Pandruvada 		close(fd);
9043fb4f7cdSSrinivas Pandruvada 		return -1;
9053fb4f7cdSSrinivas Pandruvada 	}
9063fb4f7cdSSrinivas Pandruvada 
9073fb4f7cdSSrinivas Pandruvada 	close(fd);
9083fb4f7cdSSrinivas Pandruvada 
9093bc3d30cSPrarit Bhargava 	if (isst_platform_info.api_version > supported_api_ver) {
9103bc3d30cSPrarit Bhargava 		printf("Incompatible API versions; Upgrade of tool is required\n");
9113bc3d30cSPrarit Bhargava 		return -1;
9123bc3d30cSPrarit Bhargava 	}
9133fb4f7cdSSrinivas Pandruvada 	return 0;
9143fb4f7cdSSrinivas Pandruvada }
9153fb4f7cdSSrinivas Pandruvada 
9161ba148aeSSrinivas Pandruvada static void isst_print_extended_platform_info(void)
9171ba148aeSSrinivas Pandruvada {
9181ba148aeSSrinivas Pandruvada 	int cp_state, cp_cap, fact_support = 0, pbf_support = 0;
9191ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
9201ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
9211ba148aeSSrinivas Pandruvada 	int ret, i, j;
9221ba148aeSSrinivas Pandruvada 	FILE *filep;
923850337ecSZhang Rui 	struct isst_id id;
9241ba148aeSSrinivas Pandruvada 
9251ba148aeSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
9261ba148aeSSrinivas Pandruvada 		char path[256];
9271ba148aeSSrinivas Pandruvada 
9281ba148aeSSrinivas Pandruvada 		snprintf(path, sizeof(path),
9291ba148aeSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
9301ba148aeSSrinivas Pandruvada 		filep = fopen(path, "r");
9311ba148aeSSrinivas Pandruvada 		if (filep)
9321ba148aeSSrinivas Pandruvada 			break;
9331ba148aeSSrinivas Pandruvada 	}
9341ba148aeSSrinivas Pandruvada 
9351ba148aeSSrinivas Pandruvada 	if (!filep)
9361ba148aeSSrinivas Pandruvada 		return;
9371ba148aeSSrinivas Pandruvada 
9381ba148aeSSrinivas Pandruvada 	fclose(filep);
9391ba148aeSSrinivas Pandruvada 
940850337ecSZhang Rui 	set_isst_id(&id, i);
941850337ecSZhang Rui 	ret = isst_get_ctdp_levels(&id, &pkg_dev);
9421ba148aeSSrinivas Pandruvada 	if (ret)
9431ba148aeSSrinivas Pandruvada 		return;
9441ba148aeSSrinivas Pandruvada 
9451ba148aeSSrinivas Pandruvada 	if (pkg_dev.enabled) {
9461ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
9471ba148aeSSrinivas Pandruvada 	} else {
9481ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
9491ba148aeSSrinivas Pandruvada 		fprintf(outf, "Only performance level 0 (base level) is present\n");
9501ba148aeSSrinivas Pandruvada 	}
9511ba148aeSSrinivas Pandruvada 
9521ba148aeSSrinivas Pandruvada 	if (pkg_dev.locked)
9531ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is locked\n");
9541ba148aeSSrinivas Pandruvada 	else
9551ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is unlocked, max level: %d \n", pkg_dev.levels);
9561ba148aeSSrinivas Pandruvada 
9571ba148aeSSrinivas Pandruvada 	for (j = 0; j <= pkg_dev.levels; ++j) {
958850337ecSZhang Rui 		ret = isst_get_ctdp_control(&id, j, &ctdp_level);
9591ba148aeSSrinivas Pandruvada 		if (ret)
9601ba148aeSSrinivas Pandruvada 			continue;
9611ba148aeSSrinivas Pandruvada 
9621ba148aeSSrinivas Pandruvada 		if (!fact_support && ctdp_level.fact_support)
9631ba148aeSSrinivas Pandruvada 			fact_support = 1;
9641ba148aeSSrinivas Pandruvada 
9651ba148aeSSrinivas Pandruvada 		if (!pbf_support && ctdp_level.pbf_support)
9661ba148aeSSrinivas Pandruvada 			pbf_support = 1;
9671ba148aeSSrinivas Pandruvada 	}
9681ba148aeSSrinivas Pandruvada 
9691ba148aeSSrinivas Pandruvada 	if (fact_support)
9701ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
9711ba148aeSSrinivas Pandruvada 	else
9721ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
9731ba148aeSSrinivas Pandruvada 
9741ba148aeSSrinivas Pandruvada 	if (pbf_support)
9751ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
9761ba148aeSSrinivas Pandruvada 	else
9771ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
9781ba148aeSSrinivas Pandruvada 
979850337ecSZhang Rui 	ret = isst_read_pm_config(&id, &cp_state, &cp_cap);
980b84733a1SSrinivas Pandruvada 	if (ret) {
981b84733a1SSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) status is unknown\n");
982b84733a1SSrinivas Pandruvada 		return;
983b84733a1SSrinivas Pandruvada 	}
9841ba148aeSSrinivas Pandruvada 	if (cp_cap)
9851ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
9861ba148aeSSrinivas Pandruvada 	else
9871ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
9881ba148aeSSrinivas Pandruvada }
9891ba148aeSSrinivas Pandruvada 
9903fb4f7cdSSrinivas Pandruvada static void isst_print_platform_information(void)
9913fb4f7cdSSrinivas Pandruvada {
9923fb4f7cdSSrinivas Pandruvada 	struct isst_if_platform_info platform_info;
9933fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
9943fb4f7cdSSrinivas Pandruvada 	int fd;
9953fb4f7cdSSrinivas Pandruvada 
9961ba148aeSSrinivas Pandruvada 	if (is_clx_n_platform()) {
9971ba148aeSSrinivas Pandruvada 		fprintf(stderr, "\nThis option in not supported on this platform\n");
9981ba148aeSSrinivas Pandruvada 		exit(0);
9991ba148aeSSrinivas Pandruvada 	}
10001ba148aeSSrinivas Pandruvada 
10013fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
10023fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
10033fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
10043fb4f7cdSSrinivas Pandruvada 
10053fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
10063fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
10073fb4f7cdSSrinivas Pandruvada 	} else {
10083fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: API version : %d\n",
10093fb4f7cdSSrinivas Pandruvada 			platform_info.api_version);
10103fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: Driver version : %d\n",
10113fb4f7cdSSrinivas Pandruvada 			platform_info.driver_version);
10123fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mbox supported : %d\n",
10133fb4f7cdSSrinivas Pandruvada 			platform_info.mbox_supported);
10143fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mmio supported : %d\n",
10153fb4f7cdSSrinivas Pandruvada 			platform_info.mmio_supported);
10161ba148aeSSrinivas Pandruvada 		isst_print_extended_platform_info();
10173fb4f7cdSSrinivas Pandruvada 	}
10183fb4f7cdSSrinivas Pandruvada 
10193fb4f7cdSSrinivas Pandruvada 	close(fd);
10203fb4f7cdSSrinivas Pandruvada 
10213fb4f7cdSSrinivas Pandruvada 	exit(0);
10223fb4f7cdSSrinivas Pandruvada }
10233fb4f7cdSSrinivas Pandruvada 
10243d1a8579SSrinivas Pandruvada static char *local_str0, *local_str1;
1025850337ecSZhang Rui static void exec_on_get_ctdp_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
10263fb4f7cdSSrinivas Pandruvada 				 void *arg4)
10273fb4f7cdSSrinivas Pandruvada {
1028850337ecSZhang Rui 	int (*fn_ptr)(struct isst_id *id, void *arg);
10293fb4f7cdSSrinivas Pandruvada 	int ret;
10303fb4f7cdSSrinivas Pandruvada 
10313fb4f7cdSSrinivas Pandruvada 	fn_ptr = arg1;
1032850337ecSZhang Rui 	ret = fn_ptr(id, arg2);
10333fb4f7cdSSrinivas Pandruvada 	if (ret)
10343d1a8579SSrinivas Pandruvada 		isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
10353fb4f7cdSSrinivas Pandruvada 	else
1036850337ecSZhang Rui 		isst_ctdp_display_core_info(id, outf, arg3,
10373d1a8579SSrinivas Pandruvada 					    *(unsigned int *)arg4,
10383d1a8579SSrinivas Pandruvada 					    local_str0, local_str1);
10393fb4f7cdSSrinivas Pandruvada }
10403fb4f7cdSSrinivas Pandruvada 
10413d1a8579SSrinivas Pandruvada #define _get_tdp_level(desc, suffix, object, help, str0, str1)			\
1042ce1326a2SPrarit Bhargava 	static void get_tdp_##object(int arg)                                    \
10433fb4f7cdSSrinivas Pandruvada 	{                                                                         \
10443fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp ctdp;                                        \
10453fb4f7cdSSrinivas Pandruvada \
10463fb4f7cdSSrinivas Pandruvada 		if (cmd_help) {                                                   \
10473fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,                                           \
10483fb4f7cdSSrinivas Pandruvada 				"Print %s [No command arguments are required]\n", \
10493fb4f7cdSSrinivas Pandruvada 				help);                                            \
10503fb4f7cdSSrinivas Pandruvada 			exit(0);                                                  \
10513fb4f7cdSSrinivas Pandruvada 		}                                                                 \
10523d1a8579SSrinivas Pandruvada 		local_str0 = str0;						  \
10533d1a8579SSrinivas Pandruvada 		local_str1 = str1;						  \
10543fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_start(outf);                        \
10553fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus)                                              \
10563fb4f7cdSSrinivas Pandruvada 			for_each_online_target_cpu_in_set(                        \
10573fb4f7cdSSrinivas Pandruvada 				exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
10583fb4f7cdSSrinivas Pandruvada 				&ctdp, desc, &ctdp.object);                       \
10593fb4f7cdSSrinivas Pandruvada 		else                                                              \
10603fb4f7cdSSrinivas Pandruvada 			for_each_online_package_in_set(exec_on_get_ctdp_cpu,      \
10613fb4f7cdSSrinivas Pandruvada 						       isst_get_ctdp_##suffix,    \
10623fb4f7cdSSrinivas Pandruvada 						       &ctdp, desc,               \
10633fb4f7cdSSrinivas Pandruvada 						       &ctdp.object);             \
10643fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);                          \
10653fb4f7cdSSrinivas Pandruvada 	}
10663fb4f7cdSSrinivas Pandruvada 
10673d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
10683d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
10693d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
10703fb4f7cdSSrinivas Pandruvada _get_tdp_level("get-config-current_level", levels, current_level,
10713d1a8579SSrinivas Pandruvada 	       "Current TDP Level", NULL, NULL);
10723d1a8579SSrinivas Pandruvada _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
10733fb4f7cdSSrinivas Pandruvada 
1074062e4aacSPrarit Bhargava struct isst_pkg_ctdp clx_n_pkg_dev;
1075062e4aacSPrarit Bhargava 
1076062e4aacSPrarit Bhargava static int clx_n_get_base_ratio(void)
1077062e4aacSPrarit Bhargava {
1078062e4aacSPrarit Bhargava 	FILE *fp;
1079062e4aacSPrarit Bhargava 	char *begin, *end, *line = NULL;
1080062e4aacSPrarit Bhargava 	char number[5];
1081062e4aacSPrarit Bhargava 	float value = 0;
1082062e4aacSPrarit Bhargava 	size_t n = 0;
1083062e4aacSPrarit Bhargava 
1084062e4aacSPrarit Bhargava 	fp = fopen("/proc/cpuinfo", "r");
1085062e4aacSPrarit Bhargava 	if (!fp)
1086062e4aacSPrarit Bhargava 		err(-1, "cannot open /proc/cpuinfo\n");
1087062e4aacSPrarit Bhargava 
1088062e4aacSPrarit Bhargava 	while (getline(&line, &n, fp) > 0) {
1089062e4aacSPrarit Bhargava 		if (strstr(line, "model name")) {
1090062e4aacSPrarit Bhargava 			/* this is true for CascadeLake-N */
1091062e4aacSPrarit Bhargava 			begin = strstr(line, "@ ") + 2;
1092062e4aacSPrarit Bhargava 			end = strstr(line, "GHz");
1093062e4aacSPrarit Bhargava 			strncpy(number, begin, end - begin);
1094062e4aacSPrarit Bhargava 			value = atof(number) * 10;
1095062e4aacSPrarit Bhargava 			break;
1096062e4aacSPrarit Bhargava 		}
1097062e4aacSPrarit Bhargava 	}
1098062e4aacSPrarit Bhargava 	free(line);
1099062e4aacSPrarit Bhargava 	fclose(fp);
1100062e4aacSPrarit Bhargava 
1101062e4aacSPrarit Bhargava 	return (int)(value);
1102062e4aacSPrarit Bhargava }
1103062e4aacSPrarit Bhargava 
1104850337ecSZhang Rui static int clx_n_config(struct isst_id *id)
1105062e4aacSPrarit Bhargava {
1106*56d64692SZhang Rui 	int i, ret;
1107062e4aacSPrarit Bhargava 	unsigned long cpu_bf;
1108062e4aacSPrarit Bhargava 	struct isst_pkg_ctdp_level_info *ctdp_level;
1109062e4aacSPrarit Bhargava 	struct isst_pbf_info *pbf_info;
1110062e4aacSPrarit Bhargava 
1111062e4aacSPrarit Bhargava 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1112062e4aacSPrarit Bhargava 	pbf_info = &ctdp_level->pbf_info;
1113062e4aacSPrarit Bhargava 	ctdp_level->core_cpumask_size =
1114062e4aacSPrarit Bhargava 			alloc_cpu_set(&ctdp_level->core_cpumask);
1115062e4aacSPrarit Bhargava 
1116062e4aacSPrarit Bhargava 	/* find the frequency base ratio */
1117062e4aacSPrarit Bhargava 	ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1118062e4aacSPrarit Bhargava 	if (ctdp_level->tdp_ratio == 0) {
1119062e4aacSPrarit Bhargava 		debug_printf("CLX: cn base ratio is zero\n");
1120062e4aacSPrarit Bhargava 		ret = -1;
1121062e4aacSPrarit Bhargava 		goto error_ret;
1122062e4aacSPrarit Bhargava 	}
1123062e4aacSPrarit Bhargava 
1124062e4aacSPrarit Bhargava 	/* find the high and low priority frequencies */
1125062e4aacSPrarit Bhargava 	pbf_info->p1_high = 0;
1126062e4aacSPrarit Bhargava 	pbf_info->p1_low = ~0;
1127062e4aacSPrarit Bhargava 
1128062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1129062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1130062e4aacSPrarit Bhargava 			continue;
1131062e4aacSPrarit Bhargava 
1132*56d64692SZhang Rui 		if (id->pkg != get_physical_package_id(i) ||
1133*56d64692SZhang Rui 		    id->die != get_physical_die_id(i))
1134062e4aacSPrarit Bhargava 			continue;
1135062e4aacSPrarit Bhargava 
1136062e4aacSPrarit Bhargava 		CPU_SET_S(i, ctdp_level->core_cpumask_size,
1137062e4aacSPrarit Bhargava 			  ctdp_level->core_cpumask);
1138062e4aacSPrarit Bhargava 
1139062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1140062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1141062e4aacSPrarit Bhargava 					i);
1142062e4aacSPrarit Bhargava 		if (cpu_bf > pbf_info->p1_high)
1143062e4aacSPrarit Bhargava 			pbf_info->p1_high = cpu_bf;
1144062e4aacSPrarit Bhargava 		if (cpu_bf < pbf_info->p1_low)
1145062e4aacSPrarit Bhargava 			pbf_info->p1_low = cpu_bf;
1146062e4aacSPrarit Bhargava 	}
1147062e4aacSPrarit Bhargava 
1148062e4aacSPrarit Bhargava 	if (pbf_info->p1_high == ~0UL) {
1149062e4aacSPrarit Bhargava 		debug_printf("CLX: maximum base frequency not set\n");
1150062e4aacSPrarit Bhargava 		ret = -1;
1151062e4aacSPrarit Bhargava 		goto error_ret;
1152062e4aacSPrarit Bhargava 	}
1153062e4aacSPrarit Bhargava 
1154062e4aacSPrarit Bhargava 	if (pbf_info->p1_low == 0) {
1155062e4aacSPrarit Bhargava 		debug_printf("CLX: minimum base frequency not set\n");
1156062e4aacSPrarit Bhargava 		ret = -1;
1157062e4aacSPrarit Bhargava 		goto error_ret;
1158062e4aacSPrarit Bhargava 	}
1159062e4aacSPrarit Bhargava 
1160062e4aacSPrarit Bhargava 	/* convert frequencies back to ratios */
116191d92814SSrinivas Pandruvada 	pbf_info->p1_high = pbf_info->p1_high / 100000;
116291d92814SSrinivas Pandruvada 	pbf_info->p1_low = pbf_info->p1_low / 100000;
1163062e4aacSPrarit Bhargava 
1164062e4aacSPrarit Bhargava 	/* create high priority cpu mask */
1165062e4aacSPrarit Bhargava 	pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1166062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1167062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1168062e4aacSPrarit Bhargava 			continue;
1169062e4aacSPrarit Bhargava 
1170*56d64692SZhang Rui 		if (id->pkg != get_physical_package_id(i) ||
1171*56d64692SZhang Rui 		    id->die != get_physical_die_id(i))
1172062e4aacSPrarit Bhargava 			continue;
1173062e4aacSPrarit Bhargava 
1174062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1175062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1176062e4aacSPrarit Bhargava 					i);
117791d92814SSrinivas Pandruvada 		cpu_bf = cpu_bf / 100000;
1178062e4aacSPrarit Bhargava 		if (cpu_bf == pbf_info->p1_high)
1179062e4aacSPrarit Bhargava 			CPU_SET_S(i, pbf_info->core_cpumask_size,
1180062e4aacSPrarit Bhargava 				  pbf_info->core_cpumask);
1181062e4aacSPrarit Bhargava 	}
1182062e4aacSPrarit Bhargava 
1183062e4aacSPrarit Bhargava 	/* extra ctdp & pbf struct parameters */
1184062e4aacSPrarit Bhargava 	ctdp_level->processed = 1;
1185062e4aacSPrarit Bhargava 	ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1186062e4aacSPrarit Bhargava 	ctdp_level->pbf_enabled = 1;
1187062e4aacSPrarit Bhargava 	ctdp_level->fact_support = 0; /* FACT is never supported */
1188062e4aacSPrarit Bhargava 	ctdp_level->fact_enabled = 0;
1189062e4aacSPrarit Bhargava 
1190062e4aacSPrarit Bhargava 	return 0;
1191062e4aacSPrarit Bhargava 
1192062e4aacSPrarit Bhargava error_ret:
1193062e4aacSPrarit Bhargava 	free_cpu_set(ctdp_level->core_cpumask);
1194062e4aacSPrarit Bhargava 	return ret;
1195062e4aacSPrarit Bhargava }
1196062e4aacSPrarit Bhargava 
1197850337ecSZhang Rui static void dump_clx_n_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
1198062e4aacSPrarit Bhargava 				   void *arg3, void *arg4)
1199062e4aacSPrarit Bhargava {
1200062e4aacSPrarit Bhargava 	int ret;
1201062e4aacSPrarit Bhargava 
1202ac9d05eaSSrinivas Pandruvada 	if (tdp_level != 0xff && tdp_level != 0) {
1203ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1204ac9d05eaSSrinivas Pandruvada 		exit(0);
1205ac9d05eaSSrinivas Pandruvada 	}
1206ac9d05eaSSrinivas Pandruvada 
1207850337ecSZhang Rui 	ret = clx_n_config(id);
1208062e4aacSPrarit Bhargava 	if (ret) {
12097fc9fefdSSrinivas Pandruvada 		debug_printf("clx_n_config failed");
1210062e4aacSPrarit Bhargava 	} else {
1211062e4aacSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
1212062e4aacSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
1213062e4aacSPrarit Bhargava 
1214062e4aacSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1215062e4aacSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
121628c59ae6SPrarit Bhargava 		clx_n_pkg_dev.processed = 1;
1217850337ecSZhang Rui 		isst_ctdp_display_information(id, outf, tdp_level, &clx_n_pkg_dev);
1218062e4aacSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
1219062e4aacSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
1220062e4aacSPrarit Bhargava 	}
1221062e4aacSPrarit Bhargava }
1222062e4aacSPrarit Bhargava 
1223850337ecSZhang Rui static void dump_isst_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
12243fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
12253fb4f7cdSSrinivas Pandruvada {
12263fb4f7cdSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
12273fb4f7cdSSrinivas Pandruvada 	int ret;
12283fb4f7cdSSrinivas Pandruvada 
12293fb4f7cdSSrinivas Pandruvada 	memset(&pkg_dev, 0, sizeof(pkg_dev));
1230850337ecSZhang Rui 	ret = isst_get_process_ctdp(id, tdp_level, &pkg_dev);
12313fb4f7cdSSrinivas Pandruvada 	if (ret) {
1232850337ecSZhang Rui 		isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, id->cpu);
1233ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1234ac9d05eaSSrinivas Pandruvada 		exit(1);
12353fb4f7cdSSrinivas Pandruvada 	} else {
1236850337ecSZhang Rui 		isst_ctdp_display_information(id, outf, tdp_level, &pkg_dev);
1237850337ecSZhang Rui 		isst_get_process_ctdp_complete(id, &pkg_dev);
12383fb4f7cdSSrinivas Pandruvada 	}
12393fb4f7cdSSrinivas Pandruvada }
12403fb4f7cdSSrinivas Pandruvada 
1241ce1326a2SPrarit Bhargava static void dump_isst_config(int arg)
12423fb4f7cdSSrinivas Pandruvada {
1243062e4aacSPrarit Bhargava 	void *fn;
1244062e4aacSPrarit Bhargava 
12453fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
12463fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12473fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology Performance profile configuration\n");
12483fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12493fb4f7cdSSrinivas Pandruvada 			"including base frequency and turbo frequency configurations\n");
12503fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
12513fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12523fb4f7cdSSrinivas Pandruvada 			"\tIf no arguments, dump information for all TDP levels\n");
12533fb4f7cdSSrinivas Pandruvada 		exit(0);
12543fb4f7cdSSrinivas Pandruvada 	}
12553fb4f7cdSSrinivas Pandruvada 
1256062e4aacSPrarit Bhargava 	if (!is_clx_n_platform())
1257062e4aacSPrarit Bhargava 		fn = dump_isst_config_for_cpu;
1258062e4aacSPrarit Bhargava 	else
1259062e4aacSPrarit Bhargava 		fn = dump_clx_n_config_for_cpu;
1260062e4aacSPrarit Bhargava 
12613fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
12623fb4f7cdSSrinivas Pandruvada 
12633fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
1264062e4aacSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
12653fb4f7cdSSrinivas Pandruvada 	else
1266062e4aacSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
12673fb4f7cdSSrinivas Pandruvada 
12683fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
12693fb4f7cdSSrinivas Pandruvada }
12703fb4f7cdSSrinivas Pandruvada 
1271f981dc17SSrinivas Pandruvada static void adjust_scaling_max_from_base_freq(int cpu);
1272f981dc17SSrinivas Pandruvada 
1273850337ecSZhang Rui static void set_tdp_level_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
12743fb4f7cdSSrinivas Pandruvada 				  void *arg4)
12753fb4f7cdSSrinivas Pandruvada {
12763fb4f7cdSSrinivas Pandruvada 	int ret;
12773fb4f7cdSSrinivas Pandruvada 
1278850337ecSZhang Rui 	ret = isst_set_tdp_level(id, tdp_level);
1279ac9d05eaSSrinivas Pandruvada 	if (ret) {
1280ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1281ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1282ac9d05eaSSrinivas Pandruvada 		exit(1);
1283ac9d05eaSSrinivas Pandruvada 	} else {
1284850337ecSZhang Rui 		isst_display_result(id, outf, "perf-profile", "set_tdp_level",
12853fb4f7cdSSrinivas Pandruvada 				    ret);
12863c64c81aSSrinivas Pandruvada 		if (force_online_offline) {
12873c64c81aSSrinivas Pandruvada 			struct isst_pkg_ctdp_level_info ctdp_level;
12883c64c81aSSrinivas Pandruvada 
1289f981dc17SSrinivas Pandruvada 			/* Wait for updated base frequencies */
1290f981dc17SSrinivas Pandruvada 			usleep(2000);
1291f981dc17SSrinivas Pandruvada 
12923c64c81aSSrinivas Pandruvada 			fprintf(stderr, "Option is set to online/offline\n");
12933c64c81aSSrinivas Pandruvada 			ctdp_level.core_cpumask_size =
12943c64c81aSSrinivas Pandruvada 				alloc_cpu_set(&ctdp_level.core_cpumask);
1295850337ecSZhang Rui 			ret = isst_get_coremask_info(id, tdp_level, &ctdp_level);
12966374de84SSrinivas Pandruvada 			if (ret) {
12976374de84SSrinivas Pandruvada 				isst_display_error_info_message(1, "Can't get coremask, online/offline option is ignored", 0, 0);
12986374de84SSrinivas Pandruvada 				return;
12996374de84SSrinivas Pandruvada 			}
13003c64c81aSSrinivas Pandruvada 			if (ctdp_level.cpu_count) {
13013c64c81aSSrinivas Pandruvada 				int i, max_cpus = get_topo_max_cpus();
13023c64c81aSSrinivas Pandruvada 				for (i = 0; i < max_cpus; ++i) {
1303*56d64692SZhang Rui 					if (id->pkg != get_physical_package_id(i) || id->die != get_physical_die_id(i))
13043c64c81aSSrinivas Pandruvada 						continue;
13053c64c81aSSrinivas Pandruvada 					if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
13063c64c81aSSrinivas Pandruvada 						fprintf(stderr, "online cpu %d\n", i);
13073c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 1);
1308f981dc17SSrinivas Pandruvada 						adjust_scaling_max_from_base_freq(i);
13093c64c81aSSrinivas Pandruvada 					} else {
13103c64c81aSSrinivas Pandruvada 						fprintf(stderr, "offline cpu %d\n", i);
13113c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 0);
13123c64c81aSSrinivas Pandruvada 					}
13133c64c81aSSrinivas Pandruvada 				}
13143c64c81aSSrinivas Pandruvada 			}
13153c64c81aSSrinivas Pandruvada 		}
13163c64c81aSSrinivas Pandruvada 	}
13173fb4f7cdSSrinivas Pandruvada }
13183fb4f7cdSSrinivas Pandruvada 
1319ce1326a2SPrarit Bhargava static void set_tdp_level(int arg)
13203fb4f7cdSSrinivas Pandruvada {
13213fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
13223fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Set Config TDP level\n");
13233fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
13243fb4f7cdSSrinivas Pandruvada 			"\t Arguments: -l|--level : Specify tdp level\n");
13253c64c81aSSrinivas Pandruvada 		fprintf(stderr,
13263c64c81aSSrinivas Pandruvada 			"\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1327ac9d05eaSSrinivas Pandruvada 		fprintf(stderr,
1328ac9d05eaSSrinivas Pandruvada 			"\t  online/offline operation has limitations, refer to Linux hotplug documentation\n");
13293fb4f7cdSSrinivas Pandruvada 		exit(0);
13303fb4f7cdSSrinivas Pandruvada 	}
13313fb4f7cdSSrinivas Pandruvada 
13323fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1333ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
13343fb4f7cdSSrinivas Pandruvada 		exit(1);
13353fb4f7cdSSrinivas Pandruvada 	}
13363fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
13373fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
13383fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
13393fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
13403fb4f7cdSSrinivas Pandruvada 	else
13413fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
13423fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
13433fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
13443fb4f7cdSSrinivas Pandruvada }
13453fb4f7cdSSrinivas Pandruvada 
1346850337ecSZhang Rui static void clx_n_dump_pbf_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
13471aa7177cSPrarit Bhargava 				       void *arg3, void *arg4)
13481aa7177cSPrarit Bhargava {
13491aa7177cSPrarit Bhargava 	int ret;
13501aa7177cSPrarit Bhargava 
1351850337ecSZhang Rui 	ret = clx_n_config(id);
13521aa7177cSPrarit Bhargava 	if (ret) {
135339bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "clx_n_config failed", 0, 0);
13541aa7177cSPrarit Bhargava 	} else {
13551aa7177cSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
13561aa7177cSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
13571aa7177cSPrarit Bhargava 
13581aa7177cSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
13591aa7177cSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
1360850337ecSZhang Rui 		isst_pbf_display_information(id, outf, tdp_level, pbf_info);
13611aa7177cSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
13621aa7177cSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
13631aa7177cSPrarit Bhargava 	}
13641aa7177cSPrarit Bhargava }
13651aa7177cSPrarit Bhargava 
1366850337ecSZhang Rui static void dump_pbf_config_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
13673fb4f7cdSSrinivas Pandruvada 				    void *arg4)
13683fb4f7cdSSrinivas Pandruvada {
13693fb4f7cdSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
13703fb4f7cdSSrinivas Pandruvada 	int ret;
13713fb4f7cdSSrinivas Pandruvada 
1372850337ecSZhang Rui 	ret = isst_get_pbf_info(id, tdp_level, &pbf_info);
13733fb4f7cdSSrinivas Pandruvada 	if (ret) {
137439bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get base-freq info at this level", 1, tdp_level);
137539bae0fcSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
137639bae0fcSSrinivas Pandruvada 		exit(1);
13773fb4f7cdSSrinivas Pandruvada 	} else {
1378850337ecSZhang Rui 		isst_pbf_display_information(id, outf, tdp_level, &pbf_info);
13793fb4f7cdSSrinivas Pandruvada 		isst_get_pbf_info_complete(&pbf_info);
13803fb4f7cdSSrinivas Pandruvada 	}
13813fb4f7cdSSrinivas Pandruvada }
13823fb4f7cdSSrinivas Pandruvada 
1383ce1326a2SPrarit Bhargava static void dump_pbf_config(int arg)
13843fb4f7cdSSrinivas Pandruvada {
13851aa7177cSPrarit Bhargava 	void *fn;
13861aa7177cSPrarit Bhargava 
13873fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
13883fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
13893fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
13903fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
13913fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
13923fb4f7cdSSrinivas Pandruvada 		exit(0);
13933fb4f7cdSSrinivas Pandruvada 	}
13943fb4f7cdSSrinivas Pandruvada 
13953fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
139639bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
13973fb4f7cdSSrinivas Pandruvada 		exit(1);
13983fb4f7cdSSrinivas Pandruvada 	}
13993fb4f7cdSSrinivas Pandruvada 
14001aa7177cSPrarit Bhargava 	if (!is_clx_n_platform())
14011aa7177cSPrarit Bhargava 		fn = dump_pbf_config_for_cpu;
14023fb4f7cdSSrinivas Pandruvada 	else
14031aa7177cSPrarit Bhargava 		fn = clx_n_dump_pbf_config_for_cpu;
14041aa7177cSPrarit Bhargava 
14051aa7177cSPrarit Bhargava 	isst_ctdp_display_information_start(outf);
14061aa7177cSPrarit Bhargava 
14071aa7177cSPrarit Bhargava 	if (max_target_cpus)
14081aa7177cSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
14091aa7177cSPrarit Bhargava 	else
14101aa7177cSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
14111aa7177cSPrarit Bhargava 
14123fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
14133fb4f7cdSSrinivas Pandruvada }
14143fb4f7cdSSrinivas Pandruvada 
1415850337ecSZhang Rui static int set_clos_param(struct isst_id *id, int clos, int epp, int wt, int min, int max)
1416354bd06fSSrinivas Pandruvada {
1417354bd06fSSrinivas Pandruvada 	struct isst_clos_config clos_config;
1418354bd06fSSrinivas Pandruvada 	int ret;
1419354bd06fSSrinivas Pandruvada 
1420850337ecSZhang Rui 	ret = isst_pm_get_clos(id, clos, &clos_config);
1421354bd06fSSrinivas Pandruvada 	if (ret) {
1422fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
1423354bd06fSSrinivas Pandruvada 		return ret;
1424354bd06fSSrinivas Pandruvada 	}
1425354bd06fSSrinivas Pandruvada 	clos_config.clos_min = min;
1426354bd06fSSrinivas Pandruvada 	clos_config.clos_max = max;
1427354bd06fSSrinivas Pandruvada 	clos_config.epp = epp;
1428354bd06fSSrinivas Pandruvada 	clos_config.clos_prop_prio = wt;
1429850337ecSZhang Rui 	ret = isst_set_clos(id, clos, &clos_config);
1430354bd06fSSrinivas Pandruvada 	if (ret) {
1431fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
1432354bd06fSSrinivas Pandruvada 		return ret;
1433354bd06fSSrinivas Pandruvada 	}
1434354bd06fSSrinivas Pandruvada 
1435354bd06fSSrinivas Pandruvada 	return 0;
1436354bd06fSSrinivas Pandruvada }
1437354bd06fSSrinivas Pandruvada 
1438a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1439a9b2f8e2SSrinivas Pandruvada {
1440a9b2f8e2SSrinivas Pandruvada 	char buffer[128], freq_str[16];
1441a9b2f8e2SSrinivas Pandruvada 	int fd, ret, len;
1442a9b2f8e2SSrinivas Pandruvada 
1443a9b2f8e2SSrinivas Pandruvada 	if (max)
1444a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1445a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1446a9b2f8e2SSrinivas Pandruvada 	else
1447a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1448a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1449a9b2f8e2SSrinivas Pandruvada 
1450a9b2f8e2SSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1451a9b2f8e2SSrinivas Pandruvada 	if (fd < 0)
1452a9b2f8e2SSrinivas Pandruvada 		return fd;
1453a9b2f8e2SSrinivas Pandruvada 
1454a9b2f8e2SSrinivas Pandruvada 	snprintf(freq_str, sizeof(freq_str), "%d", freq);
1455a9b2f8e2SSrinivas Pandruvada 	len = strlen(freq_str);
1456a9b2f8e2SSrinivas Pandruvada 	ret = write(fd, freq_str, len);
1457a9b2f8e2SSrinivas Pandruvada 	if (ret == -1) {
1458a9b2f8e2SSrinivas Pandruvada 		close(fd);
1459a9b2f8e2SSrinivas Pandruvada 		return ret;
1460a9b2f8e2SSrinivas Pandruvada 	}
1461a9b2f8e2SSrinivas Pandruvada 	close(fd);
1462a9b2f8e2SSrinivas Pandruvada 
1463a9b2f8e2SSrinivas Pandruvada 	return 0;
1464a9b2f8e2SSrinivas Pandruvada }
1465a9b2f8e2SSrinivas Pandruvada 
1466f981dc17SSrinivas Pandruvada static int no_turbo(void)
1467f981dc17SSrinivas Pandruvada {
1468f981dc17SSrinivas Pandruvada 	return parse_int_file(0, "/sys/devices/system/cpu/intel_pstate/no_turbo");
1469f981dc17SSrinivas Pandruvada }
1470f981dc17SSrinivas Pandruvada 
1471f981dc17SSrinivas Pandruvada static void adjust_scaling_max_from_base_freq(int cpu)
1472f981dc17SSrinivas Pandruvada {
1473f981dc17SSrinivas Pandruvada 	int base_freq, scaling_max_freq;
1474f981dc17SSrinivas Pandruvada 
1475f981dc17SSrinivas Pandruvada 	scaling_max_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1476f981dc17SSrinivas Pandruvada 	base_freq = get_cpufreq_base_freq(cpu);
1477f981dc17SSrinivas Pandruvada 	if (scaling_max_freq < base_freq || no_turbo())
1478f981dc17SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(cpu, 1, base_freq);
1479f981dc17SSrinivas Pandruvada }
1480f981dc17SSrinivas Pandruvada 
1481bbaa2e95SSrinivas Pandruvada static void adjust_scaling_min_from_base_freq(int cpu)
1482bbaa2e95SSrinivas Pandruvada {
1483bbaa2e95SSrinivas Pandruvada 	int base_freq, scaling_min_freq;
1484bbaa2e95SSrinivas Pandruvada 
1485bbaa2e95SSrinivas Pandruvada 	scaling_min_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1486bbaa2e95SSrinivas Pandruvada 	base_freq = get_cpufreq_base_freq(cpu);
1487bbaa2e95SSrinivas Pandruvada 	if (scaling_min_freq < base_freq)
1488bbaa2e95SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(cpu, 0, base_freq);
1489bbaa2e95SSrinivas Pandruvada }
1490bbaa2e95SSrinivas Pandruvada 
1491850337ecSZhang Rui static int set_clx_pbf_cpufreq_scaling_min_max(struct isst_id *id)
1492a9b2f8e2SSrinivas Pandruvada {
1493a9b2f8e2SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info *ctdp_level;
1494a9b2f8e2SSrinivas Pandruvada 	struct isst_pbf_info *pbf_info;
1495*56d64692SZhang Rui 	int i, freq, freq_high, freq_low;
1496a9b2f8e2SSrinivas Pandruvada 	int ret;
1497a9b2f8e2SSrinivas Pandruvada 
1498850337ecSZhang Rui 	ret = clx_n_config(id);
1499a9b2f8e2SSrinivas Pandruvada 	if (ret) {
15007fc9fefdSSrinivas Pandruvada 		debug_printf("cpufreq_scaling_min_max failed for CLX");
1501a9b2f8e2SSrinivas Pandruvada 		return ret;
1502a9b2f8e2SSrinivas Pandruvada 	}
1503a9b2f8e2SSrinivas Pandruvada 
1504a9b2f8e2SSrinivas Pandruvada 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1505a9b2f8e2SSrinivas Pandruvada 	pbf_info = &ctdp_level->pbf_info;
1506a9b2f8e2SSrinivas Pandruvada 	freq_high = pbf_info->p1_high * 100000;
1507a9b2f8e2SSrinivas Pandruvada 	freq_low = pbf_info->p1_low * 100000;
1508a9b2f8e2SSrinivas Pandruvada 
1509a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1510*56d64692SZhang Rui 		if (id->pkg != get_physical_package_id(i) ||
1511*56d64692SZhang Rui 		    id->die != get_physical_die_id(i))
1512a9b2f8e2SSrinivas Pandruvada 			continue;
1513a9b2f8e2SSrinivas Pandruvada 
1514a9b2f8e2SSrinivas Pandruvada 		if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1515a9b2f8e2SSrinivas Pandruvada 				  pbf_info->core_cpumask))
1516a9b2f8e2SSrinivas Pandruvada 			freq = freq_high;
1517a9b2f8e2SSrinivas Pandruvada 		else
1518a9b2f8e2SSrinivas Pandruvada 			freq = freq_low;
1519a9b2f8e2SSrinivas Pandruvada 
1520a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 1, freq);
1521a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 0, freq);
1522a9b2f8e2SSrinivas Pandruvada 	}
1523a9b2f8e2SSrinivas Pandruvada 
1524a9b2f8e2SSrinivas Pandruvada 	return 0;
1525a9b2f8e2SSrinivas Pandruvada }
1526a9b2f8e2SSrinivas Pandruvada 
1527a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1528354bd06fSSrinivas Pandruvada {
1529354bd06fSSrinivas Pandruvada 	char buffer[128], min_freq[16];
1530354bd06fSSrinivas Pandruvada 	int fd, ret, len;
1531354bd06fSSrinivas Pandruvada 
1532354bd06fSSrinivas Pandruvada 	if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1533354bd06fSSrinivas Pandruvada 		return -1;
1534354bd06fSSrinivas Pandruvada 
1535a9b2f8e2SSrinivas Pandruvada 	if (cpuinfo_max)
1536354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1537354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1538354bd06fSSrinivas Pandruvada 	else
1539354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1540354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1541354bd06fSSrinivas Pandruvada 
1542354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_RDONLY);
1543354bd06fSSrinivas Pandruvada 	if (fd < 0)
1544354bd06fSSrinivas Pandruvada 		return fd;
1545354bd06fSSrinivas Pandruvada 
1546354bd06fSSrinivas Pandruvada 	len = read(fd, min_freq, sizeof(min_freq));
1547354bd06fSSrinivas Pandruvada 	close(fd);
1548354bd06fSSrinivas Pandruvada 
1549354bd06fSSrinivas Pandruvada 	if (len < 0)
1550354bd06fSSrinivas Pandruvada 		return len;
1551354bd06fSSrinivas Pandruvada 
1552a9b2f8e2SSrinivas Pandruvada 	if (scaling_max)
1553a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1554a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1555a9b2f8e2SSrinivas Pandruvada 	else
1556354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1557354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1558354bd06fSSrinivas Pandruvada 
1559354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1560354bd06fSSrinivas Pandruvada 	if (fd < 0)
1561354bd06fSSrinivas Pandruvada 		return fd;
1562354bd06fSSrinivas Pandruvada 
1563354bd06fSSrinivas Pandruvada 	len = strlen(min_freq);
1564354bd06fSSrinivas Pandruvada 	ret = write(fd, min_freq, len);
1565354bd06fSSrinivas Pandruvada 	if (ret == -1) {
1566354bd06fSSrinivas Pandruvada 		close(fd);
1567354bd06fSSrinivas Pandruvada 		return ret;
1568354bd06fSSrinivas Pandruvada 	}
1569354bd06fSSrinivas Pandruvada 	close(fd);
1570354bd06fSSrinivas Pandruvada 
1571354bd06fSSrinivas Pandruvada 	return 0;
1572354bd06fSSrinivas Pandruvada }
1573354bd06fSSrinivas Pandruvada 
1574850337ecSZhang Rui static void set_scaling_min_to_cpuinfo_max(struct isst_id *id)
1575354bd06fSSrinivas Pandruvada {
1576*56d64692SZhang Rui 	int i;
1577354bd06fSSrinivas Pandruvada 
1578354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1579*56d64692SZhang Rui 		if (id->pkg != get_physical_package_id(i) ||
1580*56d64692SZhang Rui 		    id->die != get_physical_die_id(i))
1581354bd06fSSrinivas Pandruvada 			continue;
1582354bd06fSSrinivas Pandruvada 
15839734213eSSrinivas Pandruvada 		adjust_scaling_max_from_base_freq(i);
1584a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1585bbaa2e95SSrinivas Pandruvada 		adjust_scaling_min_from_base_freq(i);
1586354bd06fSSrinivas Pandruvada 	}
1587354bd06fSSrinivas Pandruvada }
1588354bd06fSSrinivas Pandruvada 
1589850337ecSZhang Rui static void set_scaling_min_to_cpuinfo_min(struct isst_id *id)
1590354bd06fSSrinivas Pandruvada {
1591*56d64692SZhang Rui 	int i;
1592354bd06fSSrinivas Pandruvada 
1593354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1594*56d64692SZhang Rui 		if (id->pkg != get_physical_package_id(i) ||
1595*56d64692SZhang Rui 		    id->die != get_physical_die_id(i))
1596354bd06fSSrinivas Pandruvada 			continue;
1597354bd06fSSrinivas Pandruvada 
15989734213eSSrinivas Pandruvada 		adjust_scaling_max_from_base_freq(i);
1599a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1600a9b2f8e2SSrinivas Pandruvada 	}
1601a9b2f8e2SSrinivas Pandruvada }
1602a9b2f8e2SSrinivas Pandruvada 
1603850337ecSZhang Rui static void set_scaling_max_to_cpuinfo_max(struct isst_id *id)
1604a9b2f8e2SSrinivas Pandruvada {
1605*56d64692SZhang Rui 	int i;
1606a9b2f8e2SSrinivas Pandruvada 
1607a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1608*56d64692SZhang Rui 		if (id->pkg != get_physical_package_id(i) ||
1609*56d64692SZhang Rui 		    id->die != get_physical_die_id(i))
1610a9b2f8e2SSrinivas Pandruvada 			continue;
1611a9b2f8e2SSrinivas Pandruvada 
1612a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1613354bd06fSSrinivas Pandruvada 	}
1614354bd06fSSrinivas Pandruvada }
1615354bd06fSSrinivas Pandruvada 
1616850337ecSZhang Rui static int set_core_priority_and_min(struct isst_id *id, int mask_size,
1617354bd06fSSrinivas Pandruvada 				     cpu_set_t *cpu_mask, int min_high,
1618354bd06fSSrinivas Pandruvada 				     int min_low)
1619354bd06fSSrinivas Pandruvada {
1620*56d64692SZhang Rui 	int ret, i;
1621354bd06fSSrinivas Pandruvada 
1622354bd06fSSrinivas Pandruvada 	if (!CPU_COUNT_S(mask_size, cpu_mask))
1623354bd06fSSrinivas Pandruvada 		return -1;
1624354bd06fSSrinivas Pandruvada 
1625850337ecSZhang Rui 	ret = set_clos_param(id, 0, 0, 0, min_high, 0xff);
1626354bd06fSSrinivas Pandruvada 	if (ret)
1627354bd06fSSrinivas Pandruvada 		return ret;
1628354bd06fSSrinivas Pandruvada 
1629850337ecSZhang Rui 	ret = set_clos_param(id, 1, 15, 15, min_low, 0xff);
1630354bd06fSSrinivas Pandruvada 	if (ret)
1631354bd06fSSrinivas Pandruvada 		return ret;
1632354bd06fSSrinivas Pandruvada 
1633850337ecSZhang Rui 	ret = set_clos_param(id, 2, 15, 15, min_low, 0xff);
1634354bd06fSSrinivas Pandruvada 	if (ret)
1635354bd06fSSrinivas Pandruvada 		return ret;
1636354bd06fSSrinivas Pandruvada 
1637850337ecSZhang Rui 	ret = set_clos_param(id, 3, 15, 15, min_low, 0xff);
1638354bd06fSSrinivas Pandruvada 	if (ret)
1639354bd06fSSrinivas Pandruvada 		return ret;
1640354bd06fSSrinivas Pandruvada 
1641354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1642354bd06fSSrinivas Pandruvada 		int clos;
1643850337ecSZhang Rui 		struct isst_id tid;
1644354bd06fSSrinivas Pandruvada 
1645*56d64692SZhang Rui 		if (id->pkg != get_physical_package_id(i) ||
1646*56d64692SZhang Rui 		    id->die != get_physical_die_id(i))
1647354bd06fSSrinivas Pandruvada 			continue;
1648354bd06fSSrinivas Pandruvada 
1649354bd06fSSrinivas Pandruvada 		if (CPU_ISSET_S(i, mask_size, cpu_mask))
1650354bd06fSSrinivas Pandruvada 			clos = 0;
1651354bd06fSSrinivas Pandruvada 		else
1652354bd06fSSrinivas Pandruvada 			clos = 3;
1653354bd06fSSrinivas Pandruvada 
1654354bd06fSSrinivas Pandruvada 		debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1655850337ecSZhang Rui 		set_isst_id(&tid, i);
1656850337ecSZhang Rui 		ret = isst_clos_associate(&tid, clos);
1657354bd06fSSrinivas Pandruvada 		if (ret) {
1658fe6fb216SSrinivas Pandruvada 			isst_display_error_info_message(1, "isst_clos_associate failed", 0, 0);
1659354bd06fSSrinivas Pandruvada 			return ret;
1660354bd06fSSrinivas Pandruvada 		}
1661354bd06fSSrinivas Pandruvada 	}
1662354bd06fSSrinivas Pandruvada 
1663354bd06fSSrinivas Pandruvada 	return 0;
1664354bd06fSSrinivas Pandruvada }
1665354bd06fSSrinivas Pandruvada 
1666850337ecSZhang Rui static int set_pbf_core_power(struct isst_id *id)
1667354bd06fSSrinivas Pandruvada {
1668354bd06fSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
1669354bd06fSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
1670354bd06fSSrinivas Pandruvada 	int ret;
1671354bd06fSSrinivas Pandruvada 
1672850337ecSZhang Rui 	ret = isst_get_ctdp_levels(id, &pkg_dev);
1673354bd06fSSrinivas Pandruvada 	if (ret) {
1674fe6fb216SSrinivas Pandruvada 		debug_printf("isst_get_ctdp_levels failed");
1675354bd06fSSrinivas Pandruvada 		return ret;
1676354bd06fSSrinivas Pandruvada 	}
1677354bd06fSSrinivas Pandruvada 	debug_printf("Current_level: %d\n", pkg_dev.current_level);
1678354bd06fSSrinivas Pandruvada 
1679850337ecSZhang Rui 	ret = isst_get_pbf_info(id, pkg_dev.current_level, &pbf_info);
1680354bd06fSSrinivas Pandruvada 	if (ret) {
1681fe6fb216SSrinivas Pandruvada 		debug_printf("isst_get_pbf_info failed");
1682354bd06fSSrinivas Pandruvada 		return ret;
1683354bd06fSSrinivas Pandruvada 	}
1684354bd06fSSrinivas Pandruvada 	debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1685354bd06fSSrinivas Pandruvada 		     pbf_info.p1_low);
1686354bd06fSSrinivas Pandruvada 
1687850337ecSZhang Rui 	ret = set_core_priority_and_min(id, pbf_info.core_cpumask_size,
1688354bd06fSSrinivas Pandruvada 					pbf_info.core_cpumask,
1689354bd06fSSrinivas Pandruvada 					pbf_info.p1_high, pbf_info.p1_low);
1690354bd06fSSrinivas Pandruvada 	if (ret) {
1691fe6fb216SSrinivas Pandruvada 		debug_printf("set_core_priority_and_min failed");
1692354bd06fSSrinivas Pandruvada 		return ret;
1693354bd06fSSrinivas Pandruvada 	}
1694354bd06fSSrinivas Pandruvada 
1695850337ecSZhang Rui 	ret = isst_pm_qos_config(id, 1, 1);
1696354bd06fSSrinivas Pandruvada 	if (ret) {
1697fe6fb216SSrinivas Pandruvada 		debug_printf("isst_pm_qos_config failed");
1698354bd06fSSrinivas Pandruvada 		return ret;
1699354bd06fSSrinivas Pandruvada 	}
1700354bd06fSSrinivas Pandruvada 
1701354bd06fSSrinivas Pandruvada 	return 0;
1702354bd06fSSrinivas Pandruvada }
1703354bd06fSSrinivas Pandruvada 
1704850337ecSZhang Rui static void set_pbf_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
17053fb4f7cdSSrinivas Pandruvada 			    void *arg4)
17063fb4f7cdSSrinivas Pandruvada {
17073d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
17083d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
17093fb4f7cdSSrinivas Pandruvada 	int ret;
17103fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
17113fb4f7cdSSrinivas Pandruvada 
17121aa7177cSPrarit Bhargava 	if (is_clx_n_platform()) {
17131aa7177cSPrarit Bhargava 		ret = 0;
17147fc9fefdSSrinivas Pandruvada 		if (status) {
1715850337ecSZhang Rui 			set_clx_pbf_cpufreq_scaling_min_max(id);
1716a9b2f8e2SSrinivas Pandruvada 
1717a9b2f8e2SSrinivas Pandruvada 		} else {
1718850337ecSZhang Rui 			set_scaling_max_to_cpuinfo_max(id);
1719850337ecSZhang Rui 			set_scaling_min_to_cpuinfo_min(id);
1720a9b2f8e2SSrinivas Pandruvada 		}
17211aa7177cSPrarit Bhargava 		goto disp_result;
17221aa7177cSPrarit Bhargava 	}
17231aa7177cSPrarit Bhargava 
1724850337ecSZhang Rui 	ret = isst_get_ctdp_levels(id, &pkg_dev);
17253d904f06SSrinivas Pandruvada 	if (ret) {
17263d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
17273d904f06SSrinivas Pandruvada 		goto disp_result;
17283d904f06SSrinivas Pandruvada 	}
17293d904f06SSrinivas Pandruvada 
1730850337ecSZhang Rui 	ret = isst_get_ctdp_control(id, pkg_dev.current_level, &ctdp_level);
17313d904f06SSrinivas Pandruvada 	if (ret) {
17323d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get current level", 0, 0);
17333d904f06SSrinivas Pandruvada 		goto disp_result;
17343d904f06SSrinivas Pandruvada 	}
17353d904f06SSrinivas Pandruvada 
17363d904f06SSrinivas Pandruvada 	if (!ctdp_level.pbf_support) {
17373d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "base-freq feature is not present at this level", 1, pkg_dev.current_level);
17383d904f06SSrinivas Pandruvada 		ret = -1;
17393d904f06SSrinivas Pandruvada 		goto disp_result;
17403d904f06SSrinivas Pandruvada 	}
17413d904f06SSrinivas Pandruvada 
1742097a5222SSrinivas Pandruvada 	if (auto_mode && status) {
1743850337ecSZhang Rui 		ret = set_pbf_core_power(id);
1744354bd06fSSrinivas Pandruvada 		if (ret)
1745354bd06fSSrinivas Pandruvada 			goto disp_result;
1746354bd06fSSrinivas Pandruvada 	}
1747354bd06fSSrinivas Pandruvada 
1748850337ecSZhang Rui 	ret = isst_set_pbf_fact_status(id, 1, status);
17493fb4f7cdSSrinivas Pandruvada 	if (ret) {
175039bae0fcSSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1751354bd06fSSrinivas Pandruvada 		if (auto_mode)
1752850337ecSZhang Rui 			isst_pm_qos_config(id, 0, 0);
17533fb4f7cdSSrinivas Pandruvada 	} else {
1754354bd06fSSrinivas Pandruvada 		if (auto_mode) {
1755354bd06fSSrinivas Pandruvada 			if (status)
1756850337ecSZhang Rui 				set_scaling_min_to_cpuinfo_max(id);
1757354bd06fSSrinivas Pandruvada 			else
1758850337ecSZhang Rui 				set_scaling_min_to_cpuinfo_min(id);
1759354bd06fSSrinivas Pandruvada 		}
1760354bd06fSSrinivas Pandruvada 	}
1761354bd06fSSrinivas Pandruvada 
1762097a5222SSrinivas Pandruvada 	if (auto_mode && !status)
1763850337ecSZhang Rui 		isst_pm_qos_config(id, 0, 1);
1764097a5222SSrinivas Pandruvada 
1765354bd06fSSrinivas Pandruvada disp_result:
17663fb4f7cdSSrinivas Pandruvada 	if (status)
1767850337ecSZhang Rui 		isst_display_result(id, outf, "base-freq", "enable",
17683fb4f7cdSSrinivas Pandruvada 				    ret);
17693fb4f7cdSSrinivas Pandruvada 	else
1770850337ecSZhang Rui 		isst_display_result(id, outf, "base-freq", "disable",
17713fb4f7cdSSrinivas Pandruvada 				    ret);
17723fb4f7cdSSrinivas Pandruvada }
17733fb4f7cdSSrinivas Pandruvada 
1774ce1326a2SPrarit Bhargava static void set_pbf_enable(int arg)
17753fb4f7cdSSrinivas Pandruvada {
1776ce1326a2SPrarit Bhargava 	int enable = arg;
17773fb4f7cdSSrinivas Pandruvada 
17783fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1779ce1326a2SPrarit Bhargava 		if (enable) {
17803fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1781354bd06fSSrinivas Pandruvada 				"Enable Intel Speed Select Technology base frequency feature\n");
178239bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
178339bae0fcSSrinivas Pandruvada 				fprintf(stderr,
178439bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't enable feature in the hardware.\n");
178539bae0fcSSrinivas Pandruvada 				fprintf(stderr,
178639bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpufreq base_frequency.\n");
178739bae0fcSSrinivas Pandruvada 				exit(0);
178839bae0fcSSrinivas Pandruvada 
178939bae0fcSSrinivas Pandruvada 			}
1790354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1791354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1792ce1326a2SPrarit Bhargava 		} else {
1793354bd06fSSrinivas Pandruvada 
179439bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
179539bae0fcSSrinivas Pandruvada 				fprintf(stderr,
179639bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't disable feature in the hardware.\n");
179739bae0fcSSrinivas Pandruvada 				fprintf(stderr,
179839bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpuinfo_min_freq\n");
179939bae0fcSSrinivas Pandruvada 				exit(0);
180039bae0fcSSrinivas Pandruvada 			}
18013fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1802354bd06fSSrinivas Pandruvada 				"Disable Intel Speed Select Technology base frequency feature\n");
1803354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1804354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1805ce1326a2SPrarit Bhargava 		}
18063fb4f7cdSSrinivas Pandruvada 		exit(0);
18073fb4f7cdSSrinivas Pandruvada 	}
18083fb4f7cdSSrinivas Pandruvada 
18093fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
18103fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
18113fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1812ce1326a2SPrarit Bhargava 						  NULL, &enable);
18133fb4f7cdSSrinivas Pandruvada 	else
18143fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1815ce1326a2SPrarit Bhargava 					       NULL, &enable);
18163fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
18173fb4f7cdSSrinivas Pandruvada }
18183fb4f7cdSSrinivas Pandruvada 
1819850337ecSZhang Rui static void dump_fact_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
18203fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
18213fb4f7cdSSrinivas Pandruvada {
18223fb4f7cdSSrinivas Pandruvada 	struct isst_fact_info fact_info;
18233fb4f7cdSSrinivas Pandruvada 	int ret;
18243fb4f7cdSSrinivas Pandruvada 
1825850337ecSZhang Rui 	ret = isst_get_fact_info(id, tdp_level, fact_bucket, &fact_info);
1826a9fd6ae7SSrinivas Pandruvada 	if (ret) {
1827a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get turbo-freq info at this level", 1, tdp_level);
1828a9fd6ae7SSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1829a9fd6ae7SSrinivas Pandruvada 		exit(1);
1830a9fd6ae7SSrinivas Pandruvada 	} else {
1831850337ecSZhang Rui 		isst_fact_display_information(id, outf, tdp_level, fact_bucket,
18323fb4f7cdSSrinivas Pandruvada 					      fact_avx, &fact_info);
18333fb4f7cdSSrinivas Pandruvada 	}
1834a9fd6ae7SSrinivas Pandruvada }
18353fb4f7cdSSrinivas Pandruvada 
1836ce1326a2SPrarit Bhargava static void dump_fact_config(int arg)
18373fb4f7cdSSrinivas Pandruvada {
18383fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
18393fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18403fb4f7cdSSrinivas Pandruvada 			"Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
18413fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18423fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
18433fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18443fb4f7cdSSrinivas Pandruvada 			"\tArguments: -b|--bucket : Bucket index to dump\n");
18453fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18463fb4f7cdSSrinivas Pandruvada 			"\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
18473fb4f7cdSSrinivas Pandruvada 		exit(0);
18483fb4f7cdSSrinivas Pandruvada 	}
18493fb4f7cdSSrinivas Pandruvada 
18503fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1851a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level\n", 0, 0);
18523fb4f7cdSSrinivas Pandruvada 		exit(1);
18533fb4f7cdSSrinivas Pandruvada 	}
18543fb4f7cdSSrinivas Pandruvada 
18553fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
18563fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
18573fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
18583fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
18593fb4f7cdSSrinivas Pandruvada 	else
18603fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
18613fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
18623fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
18633fb4f7cdSSrinivas Pandruvada }
18643fb4f7cdSSrinivas Pandruvada 
1865850337ecSZhang Rui static void set_fact_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
18663fb4f7cdSSrinivas Pandruvada 			     void *arg4)
18673fb4f7cdSSrinivas Pandruvada {
18683d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
18693d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
18703fb4f7cdSSrinivas Pandruvada 	int ret;
18713fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
18723fb4f7cdSSrinivas Pandruvada 
18732da6391dSSrinivas Pandruvada 	if (status && no_turbo()) {
18742da6391dSSrinivas Pandruvada 		isst_display_error_info_message(1, "Turbo mode is disabled", 0, 0);
18752da6391dSSrinivas Pandruvada 		ret = -1;
18762da6391dSSrinivas Pandruvada 		goto disp_results;
18772da6391dSSrinivas Pandruvada 	}
18782da6391dSSrinivas Pandruvada 
1879850337ecSZhang Rui 	ret = isst_get_ctdp_levels(id, &pkg_dev);
18803d904f06SSrinivas Pandruvada 	if (ret) {
18813d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
18823d904f06SSrinivas Pandruvada 		goto disp_results;
18833d904f06SSrinivas Pandruvada 	}
18843d904f06SSrinivas Pandruvada 
1885850337ecSZhang Rui 	ret = isst_get_ctdp_control(id, pkg_dev.current_level, &ctdp_level);
18863d904f06SSrinivas Pandruvada 	if (ret) {
18873d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get current level", 0, 0);
18883d904f06SSrinivas Pandruvada 		goto disp_results;
18893d904f06SSrinivas Pandruvada 	}
18903d904f06SSrinivas Pandruvada 
18913d904f06SSrinivas Pandruvada 	if (!ctdp_level.fact_support) {
18923d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "turbo-freq feature is not present at this level", 1, pkg_dev.current_level);
18933d904f06SSrinivas Pandruvada 		ret = -1;
18943d904f06SSrinivas Pandruvada 		goto disp_results;
18953d904f06SSrinivas Pandruvada 	}
18963d904f06SSrinivas Pandruvada 
18977983ed6fSSrinivas Pandruvada 	if (status) {
1898850337ecSZhang Rui 		ret = isst_pm_qos_config(id, 1, 1);
18993fb4f7cdSSrinivas Pandruvada 		if (ret)
1900a6a82f9bSSrinivas Pandruvada 			goto disp_results;
1901a6a82f9bSSrinivas Pandruvada 	}
1902a6a82f9bSSrinivas Pandruvada 
1903850337ecSZhang Rui 	ret = isst_set_pbf_fact_status(id, 0, status);
1904a6a82f9bSSrinivas Pandruvada 	if (ret) {
1905a9fd6ae7SSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1906a6a82f9bSSrinivas Pandruvada 		if (auto_mode)
1907850337ecSZhang Rui 			isst_pm_qos_config(id, 0, 0);
1908a6a82f9bSSrinivas Pandruvada 
1909a6a82f9bSSrinivas Pandruvada 		goto disp_results;
1910a6a82f9bSSrinivas Pandruvada 	}
1911a6a82f9bSSrinivas Pandruvada 
1912a6a82f9bSSrinivas Pandruvada 	/* Set TRL */
19133fb4f7cdSSrinivas Pandruvada 	if (status) {
19143fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp pkg_dev;
19153fb4f7cdSSrinivas Pandruvada 
1916850337ecSZhang Rui 		ret = isst_get_ctdp_levels(id, &pkg_dev);
1917a6a82f9bSSrinivas Pandruvada 		if (!ret)
1918850337ecSZhang Rui 			ret = isst_set_trl(id, fact_trl);
1919a6a82f9bSSrinivas Pandruvada 		if (ret && auto_mode)
1920850337ecSZhang Rui 			isst_pm_qos_config(id, 0, 0);
1921097a5222SSrinivas Pandruvada 	} else {
1922097a5222SSrinivas Pandruvada 		if (auto_mode)
1923850337ecSZhang Rui 			isst_pm_qos_config(id, 0, 0);
1924a6a82f9bSSrinivas Pandruvada 	}
1925a6a82f9bSSrinivas Pandruvada 
1926a6a82f9bSSrinivas Pandruvada disp_results:
1927a6a82f9bSSrinivas Pandruvada 	if (status) {
1928850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-freq", "enable", ret);
192914a8aa49SSrinivas Pandruvada 		if (ret)
193014a8aa49SSrinivas Pandruvada 			fact_enable_fail = ret;
19313fb4f7cdSSrinivas Pandruvada 	} else {
19323fb4f7cdSSrinivas Pandruvada 		/* Since we modified TRL during Fact enable, restore it */
1933850337ecSZhang Rui 		isst_set_trl_from_current_tdp(id, fact_trl);
1934850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-freq", "disable", ret);
19353fb4f7cdSSrinivas Pandruvada 	}
19363fb4f7cdSSrinivas Pandruvada }
19373fb4f7cdSSrinivas Pandruvada 
1938ce1326a2SPrarit Bhargava static void set_fact_enable(int arg)
19393fb4f7cdSSrinivas Pandruvada {
1940ce1326a2SPrarit Bhargava 	int i, ret, enable = arg;
1941850337ecSZhang Rui 	struct isst_id id;
19423fb4f7cdSSrinivas Pandruvada 
19433fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1944ce1326a2SPrarit Bhargava 		if (enable) {
19453fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
19463fb4f7cdSSrinivas Pandruvada 				"Enable Intel Speed Select Technology Turbo frequency feature\n");
19473fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
19483fb4f7cdSSrinivas Pandruvada 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1949a6a82f9bSSrinivas Pandruvada 			fprintf(stderr,
1950a6a82f9bSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1951ce1326a2SPrarit Bhargava 			fprintf(stderr,
1952ce1326a2SPrarit Bhargava 				"-C|--cpu option as as high priority using core-power feature\n");
1953ce1326a2SPrarit Bhargava 		} else {
1954ce1326a2SPrarit Bhargava 			fprintf(stderr,
1955ce1326a2SPrarit Bhargava 				"Disable Intel Speed Select Technology turbo frequency feature\n");
1956ce1326a2SPrarit Bhargava 			fprintf(stderr,
1957ce1326a2SPrarit Bhargava 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1958ce1326a2SPrarit Bhargava 			fprintf(stderr,
1959ce1326a2SPrarit Bhargava 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1960ce1326a2SPrarit Bhargava 		}
19613fb4f7cdSSrinivas Pandruvada 		exit(0);
19623fb4f7cdSSrinivas Pandruvada 	}
19633fb4f7cdSSrinivas Pandruvada 
19643fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
19653fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
19663fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1967ce1326a2SPrarit Bhargava 						  NULL, &enable);
19683fb4f7cdSSrinivas Pandruvada 	else
19693fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
1970ce1326a2SPrarit Bhargava 					       NULL, &enable);
19713fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
1972a6a82f9bSSrinivas Pandruvada 
197314a8aa49SSrinivas Pandruvada 	if (!fact_enable_fail && enable && auto_mode) {
1974a6a82f9bSSrinivas Pandruvada 		/*
1975a6a82f9bSSrinivas Pandruvada 		 * When we adjust CLOS param, we have to set for siblings also.
1976a6a82f9bSSrinivas Pandruvada 		 * So for the each user specified CPU, also add the sibling
1977a6a82f9bSSrinivas Pandruvada 		 * in the present_cpu_mask.
1978a6a82f9bSSrinivas Pandruvada 		 */
1979a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
1980a6a82f9bSSrinivas Pandruvada 			char buffer[128], sibling_list[128], *cpu_str;
1981a6a82f9bSSrinivas Pandruvada 			int fd, len;
1982a6a82f9bSSrinivas Pandruvada 
1983a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1984a6a82f9bSSrinivas Pandruvada 				continue;
1985a6a82f9bSSrinivas Pandruvada 
1986a6a82f9bSSrinivas Pandruvada 			snprintf(buffer, sizeof(buffer),
1987a6a82f9bSSrinivas Pandruvada 				 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1988a6a82f9bSSrinivas Pandruvada 
1989a6a82f9bSSrinivas Pandruvada 			fd = open(buffer, O_RDONLY);
1990a6a82f9bSSrinivas Pandruvada 			if (fd < 0)
1991a6a82f9bSSrinivas Pandruvada 				continue;
1992a6a82f9bSSrinivas Pandruvada 
1993a6a82f9bSSrinivas Pandruvada 			len = read(fd, sibling_list, sizeof(sibling_list));
1994a6a82f9bSSrinivas Pandruvada 			close(fd);
1995a6a82f9bSSrinivas Pandruvada 
1996a6a82f9bSSrinivas Pandruvada 			if (len < 0)
1997a6a82f9bSSrinivas Pandruvada 				continue;
1998a6a82f9bSSrinivas Pandruvada 
1999a6a82f9bSSrinivas Pandruvada 			cpu_str = strtok(sibling_list, ",");
2000a6a82f9bSSrinivas Pandruvada 			while (cpu_str != NULL) {
2001a6a82f9bSSrinivas Pandruvada 				int cpu;
2002a6a82f9bSSrinivas Pandruvada 
2003a6a82f9bSSrinivas Pandruvada 				sscanf(cpu_str, "%d", &cpu);
2004a6a82f9bSSrinivas Pandruvada 				CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
2005a6a82f9bSSrinivas Pandruvada 				cpu_str = strtok(NULL, ",");
2006a6a82f9bSSrinivas Pandruvada 			}
2007a6a82f9bSSrinivas Pandruvada 		}
2008a6a82f9bSSrinivas Pandruvada 
2009a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
2010a6a82f9bSSrinivas Pandruvada 			int clos;
2011a6a82f9bSSrinivas Pandruvada 
2012a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
2013a6a82f9bSSrinivas Pandruvada 				continue;
2014a6a82f9bSSrinivas Pandruvada 
2015850337ecSZhang Rui 			set_isst_id(&id, i);
2016850337ecSZhang Rui 			ret = set_clos_param(&id, 0, 0, 0, 0, 0xff);
2017a6a82f9bSSrinivas Pandruvada 			if (ret)
2018a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2019a6a82f9bSSrinivas Pandruvada 
2020850337ecSZhang Rui 			ret = set_clos_param(&id, 1, 15, 15, 0, 0xff);
2021a6a82f9bSSrinivas Pandruvada 			if (ret)
2022a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2023a6a82f9bSSrinivas Pandruvada 
2024850337ecSZhang Rui 			ret = set_clos_param(&id, 2, 15, 15, 0, 0xff);
2025a6a82f9bSSrinivas Pandruvada 			if (ret)
2026a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2027a6a82f9bSSrinivas Pandruvada 
2028850337ecSZhang Rui 			ret = set_clos_param(&id, 3, 15, 15, 0, 0xff);
2029a6a82f9bSSrinivas Pandruvada 			if (ret)
2030a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2031a6a82f9bSSrinivas Pandruvada 
2032a6a82f9bSSrinivas Pandruvada 			if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
2033a6a82f9bSSrinivas Pandruvada 				clos = 0;
2034a6a82f9bSSrinivas Pandruvada 			else
2035a6a82f9bSSrinivas Pandruvada 				clos = 3;
2036a6a82f9bSSrinivas Pandruvada 
2037a6a82f9bSSrinivas Pandruvada 			debug_printf("Associate cpu: %d clos: %d\n", i, clos);
2038850337ecSZhang Rui 			ret = isst_clos_associate(&id, clos);
2039a6a82f9bSSrinivas Pandruvada 			if (ret)
2040a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2041a6a82f9bSSrinivas Pandruvada 		}
2042850337ecSZhang Rui 		set_isst_id(&id, -1);
2043850337ecSZhang Rui 		isst_display_result(&id, outf, "turbo-freq --auto", "enable", 0);
2044a6a82f9bSSrinivas Pandruvada 	}
2045a6a82f9bSSrinivas Pandruvada 
2046a6a82f9bSSrinivas Pandruvada 	return;
2047a6a82f9bSSrinivas Pandruvada 
2048a6a82f9bSSrinivas Pandruvada error_disp:
2049850337ecSZhang Rui 	isst_display_result(&id, outf, "turbo-freq --auto", "enable", ret);
2050a6a82f9bSSrinivas Pandruvada 
20513fb4f7cdSSrinivas Pandruvada }
20523fb4f7cdSSrinivas Pandruvada 
2053850337ecSZhang Rui static void enable_clos_qos_config(struct isst_id *id, void *arg1, void *arg2, void *arg3,
20543fb4f7cdSSrinivas Pandruvada 				   void *arg4)
20553fb4f7cdSSrinivas Pandruvada {
20563fb4f7cdSSrinivas Pandruvada 	int ret;
20573fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
20583fb4f7cdSSrinivas Pandruvada 
2059fe6fb216SSrinivas Pandruvada 	if (is_skx_based_platform())
2060fe6fb216SSrinivas Pandruvada 		clos_priority_type = 1;
2061fe6fb216SSrinivas Pandruvada 
2062850337ecSZhang Rui 	ret = isst_pm_qos_config(id, status, clos_priority_type);
2063a6a82f9bSSrinivas Pandruvada 	if (ret)
2064fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_qos_config failed", 0, 0);
2065a6a82f9bSSrinivas Pandruvada 
20663fb4f7cdSSrinivas Pandruvada 	if (status)
2067850337ecSZhang Rui 		isst_display_result(id, outf, "core-power", "enable",
20683fb4f7cdSSrinivas Pandruvada 				    ret);
20693fb4f7cdSSrinivas Pandruvada 	else
2070850337ecSZhang Rui 		isst_display_result(id, outf, "core-power", "disable",
20713fb4f7cdSSrinivas Pandruvada 				    ret);
20723fb4f7cdSSrinivas Pandruvada }
20733fb4f7cdSSrinivas Pandruvada 
2074ce1326a2SPrarit Bhargava static void set_clos_enable(int arg)
20753fb4f7cdSSrinivas Pandruvada {
2076ce1326a2SPrarit Bhargava 	int enable = arg;
20773fb4f7cdSSrinivas Pandruvada 
20783fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
2079ce1326a2SPrarit Bhargava 		if (enable) {
2080ce1326a2SPrarit Bhargava 			fprintf(stderr,
2081ce1326a2SPrarit Bhargava 				"Enable core-power for a package/die\n");
2082fe6fb216SSrinivas Pandruvada 			if (!is_skx_based_platform()) {
20833fb4f7cdSSrinivas Pandruvada 				fprintf(stderr,
20843fb4f7cdSSrinivas Pandruvada 					"\tClos Enable: Specify priority type with [--priority|-p]\n");
20853fb4f7cdSSrinivas Pandruvada 				fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
2086fe6fb216SSrinivas Pandruvada 			}
2087ce1326a2SPrarit Bhargava 		} else {
2088ce1326a2SPrarit Bhargava 			fprintf(stderr,
2089ce1326a2SPrarit Bhargava 				"Disable core-power: [No command arguments are required]\n");
2090ce1326a2SPrarit Bhargava 		}
20913fb4f7cdSSrinivas Pandruvada 		exit(0);
20923fb4f7cdSSrinivas Pandruvada 	}
20933fb4f7cdSSrinivas Pandruvada 
2094ce1326a2SPrarit Bhargava 	if (enable && cpufreq_sysfs_present()) {
20953fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
20963fb4f7cdSSrinivas Pandruvada 			"cpufreq subsystem and core-power enable will interfere with each other!\n");
20973fb4f7cdSSrinivas Pandruvada 	}
20983fb4f7cdSSrinivas Pandruvada 
20993fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
21003fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21013fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
2102ce1326a2SPrarit Bhargava 						  NULL, NULL, &enable);
21033fb4f7cdSSrinivas Pandruvada 	else
21043fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(enable_clos_qos_config, NULL,
2105ce1326a2SPrarit Bhargava 					       NULL, NULL, &enable);
21063fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
21073fb4f7cdSSrinivas Pandruvada }
21083fb4f7cdSSrinivas Pandruvada 
2109850337ecSZhang Rui static void dump_clos_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
21103fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
21113fb4f7cdSSrinivas Pandruvada {
21123fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
21133fb4f7cdSSrinivas Pandruvada 	int ret;
21143fb4f7cdSSrinivas Pandruvada 
2115850337ecSZhang Rui 	ret = isst_pm_get_clos(id, current_clos, &clos_config);
21163fb4f7cdSSrinivas Pandruvada 	if (ret)
2117fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
21183fb4f7cdSSrinivas Pandruvada 	else
2119850337ecSZhang Rui 		isst_clos_display_information(id, outf, current_clos,
21203fb4f7cdSSrinivas Pandruvada 					      &clos_config);
21213fb4f7cdSSrinivas Pandruvada }
21223fb4f7cdSSrinivas Pandruvada 
2123ce1326a2SPrarit Bhargava static void dump_clos_config(int arg)
21243fb4f7cdSSrinivas Pandruvada {
21253fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
21263fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21273fb4f7cdSSrinivas Pandruvada 			"Print Intel Speed Select Technology core power configuration\n");
21283fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21293fb4f7cdSSrinivas Pandruvada 			"\tArguments: [-c | --clos]: Specify clos id\n");
21303fb4f7cdSSrinivas Pandruvada 		exit(0);
21313fb4f7cdSSrinivas Pandruvada 	}
21323fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2133fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2134fe6fb216SSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
21353fb4f7cdSSrinivas Pandruvada 		exit(0);
21363fb4f7cdSSrinivas Pandruvada 	}
21373fb4f7cdSSrinivas Pandruvada 
21383fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
21393fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21403fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
21413fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
21423fb4f7cdSSrinivas Pandruvada 	else
21433fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
21443fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
21453fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
21463fb4f7cdSSrinivas Pandruvada }
21473fb4f7cdSSrinivas Pandruvada 
2148850337ecSZhang Rui static void get_clos_info_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2149188afed9SSrinivas Pandruvada 				  void *arg4)
2150188afed9SSrinivas Pandruvada {
2151188afed9SSrinivas Pandruvada 	int enable, ret, prio_type;
2152188afed9SSrinivas Pandruvada 
2153850337ecSZhang Rui 	ret = isst_clos_get_clos_information(id, &enable, &prio_type);
2154188afed9SSrinivas Pandruvada 	if (ret)
2155fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_clos_get_info failed", 0, 0);
2156143ad322SSrinivas Pandruvada 	else {
2157143ad322SSrinivas Pandruvada 		int cp_state, cp_cap;
2158143ad322SSrinivas Pandruvada 
2159850337ecSZhang Rui 		isst_read_pm_config(id, &cp_state, &cp_cap);
2160850337ecSZhang Rui 		isst_clos_display_clos_information(id, outf, enable, prio_type,
2161143ad322SSrinivas Pandruvada 						   cp_state, cp_cap);
2162143ad322SSrinivas Pandruvada 	}
2163188afed9SSrinivas Pandruvada }
2164188afed9SSrinivas Pandruvada 
2165ce1326a2SPrarit Bhargava static void dump_clos_info(int arg)
2166188afed9SSrinivas Pandruvada {
2167188afed9SSrinivas Pandruvada 	if (cmd_help) {
2168188afed9SSrinivas Pandruvada 		fprintf(stderr,
2169188afed9SSrinivas Pandruvada 			"Print Intel Speed Select Technology core power information\n");
2170f5205f49SSrinivas Pandruvada 		fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2171188afed9SSrinivas Pandruvada 		exit(0);
2172188afed9SSrinivas Pandruvada 	}
2173188afed9SSrinivas Pandruvada 
2174188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2175f5205f49SSrinivas Pandruvada 	if (max_target_cpus)
2176188afed9SSrinivas Pandruvada 		for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2177188afed9SSrinivas Pandruvada 						  NULL, NULL, NULL);
2178f5205f49SSrinivas Pandruvada 	else
2179f5205f49SSrinivas Pandruvada 		for_each_online_package_in_set(get_clos_info_for_cpu, NULL,
2180f5205f49SSrinivas Pandruvada 					       NULL, NULL, NULL);
2181188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
2182188afed9SSrinivas Pandruvada 
2183188afed9SSrinivas Pandruvada }
2184188afed9SSrinivas Pandruvada 
2185850337ecSZhang Rui static void set_clos_config_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
21863fb4f7cdSSrinivas Pandruvada 				    void *arg4)
21873fb4f7cdSSrinivas Pandruvada {
21883fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
21893fb4f7cdSSrinivas Pandruvada 	int ret;
21903fb4f7cdSSrinivas Pandruvada 
2191*56d64692SZhang Rui 	clos_config.pkg_id = id->pkg;
2192*56d64692SZhang Rui 	clos_config.die_id = id->die;
21933fb4f7cdSSrinivas Pandruvada 
21943fb4f7cdSSrinivas Pandruvada 	clos_config.epp = clos_epp;
21953fb4f7cdSSrinivas Pandruvada 	clos_config.clos_prop_prio = clos_prop_prio;
21963fb4f7cdSSrinivas Pandruvada 	clos_config.clos_min = clos_min;
21973fb4f7cdSSrinivas Pandruvada 	clos_config.clos_max = clos_max;
21983fb4f7cdSSrinivas Pandruvada 	clos_config.clos_desired = clos_desired;
2199850337ecSZhang Rui 	ret = isst_set_clos(id, current_clos, &clos_config);
22003fb4f7cdSSrinivas Pandruvada 	if (ret)
2201fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
22023fb4f7cdSSrinivas Pandruvada 	else
2203850337ecSZhang Rui 		isst_display_result(id, outf, "core-power", "config", ret);
22043fb4f7cdSSrinivas Pandruvada }
22053fb4f7cdSSrinivas Pandruvada 
2206ce1326a2SPrarit Bhargava static void set_clos_config(int arg)
22073fb4f7cdSSrinivas Pandruvada {
22083fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
22093fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
22103fb4f7cdSSrinivas Pandruvada 			"Set core-power configuration for one of the four clos ids\n");
22113fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
22123fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
2213fe6fb216SSrinivas Pandruvada 		if (!is_skx_based_platform()) {
22143fb4f7cdSSrinivas Pandruvada 			fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
22153fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
22163fb4f7cdSSrinivas Pandruvada 				"\tSpecify clos Proportional Priority [--weight|-w]\n");
2217fe6fb216SSrinivas Pandruvada 		}
221840dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
221940dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
22203fb4f7cdSSrinivas Pandruvada 		exit(0);
22213fb4f7cdSSrinivas Pandruvada 	}
22223fb4f7cdSSrinivas Pandruvada 
22233fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2224fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
22253fb4f7cdSSrinivas Pandruvada 		exit(0);
22263fb4f7cdSSrinivas Pandruvada 	}
2227fe6fb216SSrinivas Pandruvada 	if (!is_skx_based_platform() && (clos_epp < 0 || clos_epp > 0x0F)) {
2228fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos epp is not specified or invalid, default: 0\n");
22293fb4f7cdSSrinivas Pandruvada 		clos_epp = 0;
22303fb4f7cdSSrinivas Pandruvada 	}
2231fe6fb216SSrinivas Pandruvada 	if (!is_skx_based_platform() && (clos_prop_prio < 0 || clos_prop_prio > 0x0F)) {
22323fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
2233fe6fb216SSrinivas Pandruvada 			"clos frequency weight is not specified or invalid, default: 0\n");
22343fb4f7cdSSrinivas Pandruvada 		clos_prop_prio = 0;
22353fb4f7cdSSrinivas Pandruvada 	}
22363fb4f7cdSSrinivas Pandruvada 	if (clos_min < 0) {
22373fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "clos min is not specified, default: 0\n");
22383fb4f7cdSSrinivas Pandruvada 		clos_min = 0;
22393fb4f7cdSSrinivas Pandruvada 	}
22403fb4f7cdSSrinivas Pandruvada 	if (clos_max < 0) {
2241fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos max is not specified, default: Max frequency (ratio 0xff)\n");
22423fb4f7cdSSrinivas Pandruvada 		clos_max = 0xff;
22433fb4f7cdSSrinivas Pandruvada 	}
2244fe6fb216SSrinivas Pandruvada 	if (clos_desired) {
2245fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos desired is not supported on this platform\n");
22463fb4f7cdSSrinivas Pandruvada 		clos_desired = 0x00;
22473fb4f7cdSSrinivas Pandruvada 	}
22483fb4f7cdSSrinivas Pandruvada 
22493fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
22503fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
22513fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
22523fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
22533fb4f7cdSSrinivas Pandruvada 	else
22543fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
22553fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
22563fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
22573fb4f7cdSSrinivas Pandruvada }
22583fb4f7cdSSrinivas Pandruvada 
2259850337ecSZhang Rui static void set_clos_assoc_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
22603fb4f7cdSSrinivas Pandruvada 				   void *arg4)
22613fb4f7cdSSrinivas Pandruvada {
22623fb4f7cdSSrinivas Pandruvada 	int ret;
22633fb4f7cdSSrinivas Pandruvada 
2264850337ecSZhang Rui 	ret = isst_clos_associate(id, current_clos);
22653fb4f7cdSSrinivas Pandruvada 	if (ret)
2266fe6fb216SSrinivas Pandruvada 		debug_printf("isst_clos_associate failed");
22673fb4f7cdSSrinivas Pandruvada 	else
2268850337ecSZhang Rui 		isst_display_result(id, outf, "core-power", "assoc", ret);
22693fb4f7cdSSrinivas Pandruvada }
22703fb4f7cdSSrinivas Pandruvada 
2271ce1326a2SPrarit Bhargava static void set_clos_assoc(int arg)
22723fb4f7cdSSrinivas Pandruvada {
22733fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
22743fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Associate a clos id to a CPU\n");
22753fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
22763fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
227768e2f109SSrinivas Pandruvada 		fprintf(stderr,
227868e2f109SSrinivas Pandruvada 			"\tFor example to associate clos 1 to CPU 0: issue\n");
227968e2f109SSrinivas Pandruvada 		fprintf(stderr,
228068e2f109SSrinivas Pandruvada 			"\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
22813fb4f7cdSSrinivas Pandruvada 		exit(0);
22823fb4f7cdSSrinivas Pandruvada 	}
22833fb4f7cdSSrinivas Pandruvada 
22843fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2285fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
22863fb4f7cdSSrinivas Pandruvada 		exit(0);
22873fb4f7cdSSrinivas Pandruvada 	}
22883fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
22893fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
22903fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
22913fb4f7cdSSrinivas Pandruvada 	else {
2292fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
22933fb4f7cdSSrinivas Pandruvada 	}
22943fb4f7cdSSrinivas Pandruvada }
22953fb4f7cdSSrinivas Pandruvada 
2296850337ecSZhang Rui static void get_clos_assoc_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
22973fb4f7cdSSrinivas Pandruvada 				   void *arg4)
22983fb4f7cdSSrinivas Pandruvada {
22993fb4f7cdSSrinivas Pandruvada 	int clos, ret;
23003fb4f7cdSSrinivas Pandruvada 
2301850337ecSZhang Rui 	ret = isst_clos_get_assoc_status(id, &clos);
23023fb4f7cdSSrinivas Pandruvada 	if (ret)
2303fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_clos_get_assoc_status failed", 0, 0);
23043fb4f7cdSSrinivas Pandruvada 	else
2305850337ecSZhang Rui 		isst_clos_display_assoc_information(id, outf, clos);
23063fb4f7cdSSrinivas Pandruvada }
23073fb4f7cdSSrinivas Pandruvada 
2308ce1326a2SPrarit Bhargava static void get_clos_assoc(int arg)
23093fb4f7cdSSrinivas Pandruvada {
23103fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
23113fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Get associate clos id to a CPU\n");
23123fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
23133fb4f7cdSSrinivas Pandruvada 		exit(0);
23143fb4f7cdSSrinivas Pandruvada 	}
2315e118fbe3SSrinivas Pandruvada 
2316e118fbe3SSrinivas Pandruvada 	if (!max_target_cpus) {
2317fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2318e118fbe3SSrinivas Pandruvada 		exit(0);
23193fb4f7cdSSrinivas Pandruvada 	}
2320e118fbe3SSrinivas Pandruvada 
2321e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2322e118fbe3SSrinivas Pandruvada 	for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2323e118fbe3SSrinivas Pandruvada 					  NULL, NULL, NULL);
2324e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
23253fb4f7cdSSrinivas Pandruvada }
23263fb4f7cdSSrinivas Pandruvada 
2327850337ecSZhang Rui static void set_turbo_mode_for_cpu(struct isst_id *id, int status)
2328006050a6SSrinivas Pandruvada {
2329006050a6SSrinivas Pandruvada 	int base_freq;
2330006050a6SSrinivas Pandruvada 
2331006050a6SSrinivas Pandruvada 	if (status) {
2332850337ecSZhang Rui 		base_freq = get_cpufreq_base_freq(id->cpu);
2333850337ecSZhang Rui 		set_cpufreq_scaling_min_max(id->cpu, 1, base_freq);
2334006050a6SSrinivas Pandruvada 	} else {
2335850337ecSZhang Rui 		set_scaling_max_to_cpuinfo_max(id);
2336006050a6SSrinivas Pandruvada 	}
2337006050a6SSrinivas Pandruvada 
2338006050a6SSrinivas Pandruvada 	if (status) {
2339850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-mode", "enable", 0);
2340006050a6SSrinivas Pandruvada 	} else {
2341850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-mode", "disable", 0);
2342006050a6SSrinivas Pandruvada 	}
2343006050a6SSrinivas Pandruvada }
2344006050a6SSrinivas Pandruvada 
2345006050a6SSrinivas Pandruvada static void set_turbo_mode(int arg)
2346006050a6SSrinivas Pandruvada {
2347006050a6SSrinivas Pandruvada 	int i, enable = arg;
2348850337ecSZhang Rui 	struct isst_id id;
2349006050a6SSrinivas Pandruvada 
2350006050a6SSrinivas Pandruvada 	if (cmd_help) {
2351006050a6SSrinivas Pandruvada 		if (enable)
2352006050a6SSrinivas Pandruvada 			fprintf(stderr, "Set turbo mode enable\n");
2353006050a6SSrinivas Pandruvada 		else
2354006050a6SSrinivas Pandruvada 			fprintf(stderr, "Set turbo mode disable\n");
2355006050a6SSrinivas Pandruvada 		exit(0);
2356006050a6SSrinivas Pandruvada 	}
2357006050a6SSrinivas Pandruvada 
2358006050a6SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2359006050a6SSrinivas Pandruvada 
2360006050a6SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
2361006050a6SSrinivas Pandruvada 		int online;
2362006050a6SSrinivas Pandruvada 
2363006050a6SSrinivas Pandruvada 		if (i)
2364006050a6SSrinivas Pandruvada 			online = parse_int_file(
2365006050a6SSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
2366006050a6SSrinivas Pandruvada 		else
2367006050a6SSrinivas Pandruvada 			online =
2368006050a6SSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
2369006050a6SSrinivas Pandruvada 
2370850337ecSZhang Rui 		if (online) {
2371850337ecSZhang Rui 			set_isst_id(&id, i);
2372850337ecSZhang Rui 			set_turbo_mode_for_cpu(&id, enable);
2373850337ecSZhang Rui 		}
2374006050a6SSrinivas Pandruvada 
2375006050a6SSrinivas Pandruvada 	}
2376006050a6SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
2377006050a6SSrinivas Pandruvada }
2378006050a6SSrinivas Pandruvada 
2379850337ecSZhang Rui static void get_set_trl(struct isst_id *id, void *arg1, void *arg2, void *arg3,
23802c7dc57eSSrinivas Pandruvada 			void *arg4)
23812c7dc57eSSrinivas Pandruvada {
23822c7dc57eSSrinivas Pandruvada 	unsigned long long trl;
23832c7dc57eSSrinivas Pandruvada 	int set = *(int *)arg4;
23842c7dc57eSSrinivas Pandruvada 	int ret;
23852c7dc57eSSrinivas Pandruvada 
23862c7dc57eSSrinivas Pandruvada 	if (set && !fact_trl) {
23872c7dc57eSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid TRL. Specify with [-t|--trl]", 0, 0);
23882c7dc57eSSrinivas Pandruvada 		exit(0);
23892c7dc57eSSrinivas Pandruvada 	}
23902c7dc57eSSrinivas Pandruvada 
23912c7dc57eSSrinivas Pandruvada 	if (set) {
2392850337ecSZhang Rui 		ret = isst_set_trl(id, fact_trl);
2393850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-mode", "set-trl", ret);
23942c7dc57eSSrinivas Pandruvada 		return;
23952c7dc57eSSrinivas Pandruvada 	}
23962c7dc57eSSrinivas Pandruvada 
2397850337ecSZhang Rui 	ret = isst_get_trl(id, &trl);
23982c7dc57eSSrinivas Pandruvada 	if (ret)
2399850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-mode", "get-trl", ret);
24002c7dc57eSSrinivas Pandruvada 	else
2401850337ecSZhang Rui 		isst_trl_display_information(id, outf, trl);
24022c7dc57eSSrinivas Pandruvada }
24032c7dc57eSSrinivas Pandruvada 
24042c7dc57eSSrinivas Pandruvada static void process_trl(int arg)
24052c7dc57eSSrinivas Pandruvada {
24062c7dc57eSSrinivas Pandruvada 	if (cmd_help) {
24072c7dc57eSSrinivas Pandruvada 		if (arg) {
24082c7dc57eSSrinivas Pandruvada 			fprintf(stderr, "Set TRL (turbo ratio limits)\n");
24092c7dc57eSSrinivas Pandruvada 			fprintf(stderr, "\t t|--trl: Specify turbo ratio limit for setting TRL\n");
24102c7dc57eSSrinivas Pandruvada 		} else {
24112c7dc57eSSrinivas Pandruvada 			fprintf(stderr, "Get TRL (turbo ratio limits)\n");
24122c7dc57eSSrinivas Pandruvada 		}
24132c7dc57eSSrinivas Pandruvada 		exit(0);
24142c7dc57eSSrinivas Pandruvada 	}
24152c7dc57eSSrinivas Pandruvada 
24162c7dc57eSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
24172c7dc57eSSrinivas Pandruvada 	if (max_target_cpus)
24182c7dc57eSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(get_set_trl, NULL,
24192c7dc57eSSrinivas Pandruvada 						  NULL, NULL, &arg);
24202c7dc57eSSrinivas Pandruvada 	else
24212c7dc57eSSrinivas Pandruvada 		for_each_online_package_in_set(get_set_trl, NULL,
24222c7dc57eSSrinivas Pandruvada 					       NULL, NULL, &arg);
24232c7dc57eSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
24242c7dc57eSSrinivas Pandruvada }
24252c7dc57eSSrinivas Pandruvada 
2426c829f0efSPrarit Bhargava static struct process_cmd_struct clx_n_cmds[] = {
2427062e4aacSPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
24281aa7177cSPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
24291aa7177cSPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
24301aa7177cSPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2431c829f0efSPrarit Bhargava 	{ NULL, NULL, NULL, 0 }
2432c829f0efSPrarit Bhargava };
2433c829f0efSPrarit Bhargava 
24343fb4f7cdSSrinivas Pandruvada static struct process_cmd_struct isst_cmds[] = {
2435ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2436ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2437ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-version", get_tdp_version, 0 },
2438ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2439ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-current-level", get_tdp_current_level,
2440ce1326a2SPrarit Bhargava 	 0 },
2441ce1326a2SPrarit Bhargava 	{ "perf-profile", "set-config-level", set_tdp_level, 0 },
2442ce1326a2SPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
2443ce1326a2SPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
2444ce1326a2SPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
2445ce1326a2SPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2446ce1326a2SPrarit Bhargava 	{ "turbo-freq", "info", dump_fact_config, 0 },
2447ce1326a2SPrarit Bhargava 	{ "turbo-freq", "enable", set_fact_enable, 1 },
2448ce1326a2SPrarit Bhargava 	{ "turbo-freq", "disable", set_fact_enable, 0 },
2449ce1326a2SPrarit Bhargava 	{ "core-power", "info", dump_clos_info, 0 },
2450ce1326a2SPrarit Bhargava 	{ "core-power", "enable", set_clos_enable, 1 },
2451ce1326a2SPrarit Bhargava 	{ "core-power", "disable", set_clos_enable, 0 },
2452ce1326a2SPrarit Bhargava 	{ "core-power", "config", set_clos_config, 0 },
2453ce1326a2SPrarit Bhargava 	{ "core-power", "get-config", dump_clos_config, 0 },
2454ce1326a2SPrarit Bhargava 	{ "core-power", "assoc", set_clos_assoc, 0 },
2455ce1326a2SPrarit Bhargava 	{ "core-power", "get-assoc", get_clos_assoc, 0 },
2456006050a6SSrinivas Pandruvada 	{ "turbo-mode", "enable", set_turbo_mode, 0 },
2457006050a6SSrinivas Pandruvada 	{ "turbo-mode", "disable", set_turbo_mode, 1 },
24582c7dc57eSSrinivas Pandruvada 	{ "turbo-mode", "get-trl", process_trl, 0 },
24592c7dc57eSSrinivas Pandruvada 	{ "turbo-mode", "set-trl", process_trl, 1 },
24603fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL, NULL }
24613fb4f7cdSSrinivas Pandruvada };
24623fb4f7cdSSrinivas Pandruvada 
24633fb4f7cdSSrinivas Pandruvada /*
24643fb4f7cdSSrinivas Pandruvada  * parse cpuset with following syntax
24653fb4f7cdSSrinivas Pandruvada  * 1,2,4..6,8-10 and set bits in cpu_subset
24663fb4f7cdSSrinivas Pandruvada  */
24673fb4f7cdSSrinivas Pandruvada void parse_cpu_command(char *optarg)
24683fb4f7cdSSrinivas Pandruvada {
24693fb4f7cdSSrinivas Pandruvada 	unsigned int start, end;
24703fb4f7cdSSrinivas Pandruvada 	char *next;
24713fb4f7cdSSrinivas Pandruvada 
24723fb4f7cdSSrinivas Pandruvada 	next = optarg;
24733fb4f7cdSSrinivas Pandruvada 
24743fb4f7cdSSrinivas Pandruvada 	while (next && *next) {
24753fb4f7cdSSrinivas Pandruvada 		if (*next == '-') /* no negative cpu numbers */
24763fb4f7cdSSrinivas Pandruvada 			goto error;
24773fb4f7cdSSrinivas Pandruvada 
24783fb4f7cdSSrinivas Pandruvada 		start = strtoul(next, &next, 10);
24793fb4f7cdSSrinivas Pandruvada 
24803fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
24813fb4f7cdSSrinivas Pandruvada 			target_cpus[max_target_cpus++] = start;
24823fb4f7cdSSrinivas Pandruvada 
24833fb4f7cdSSrinivas Pandruvada 		if (*next == '\0')
24843fb4f7cdSSrinivas Pandruvada 			break;
24853fb4f7cdSSrinivas Pandruvada 
24863fb4f7cdSSrinivas Pandruvada 		if (*next == ',') {
24873fb4f7cdSSrinivas Pandruvada 			next += 1;
24883fb4f7cdSSrinivas Pandruvada 			continue;
24893fb4f7cdSSrinivas Pandruvada 		}
24903fb4f7cdSSrinivas Pandruvada 
24913fb4f7cdSSrinivas Pandruvada 		if (*next == '-') {
24923fb4f7cdSSrinivas Pandruvada 			next += 1; /* start range */
24933fb4f7cdSSrinivas Pandruvada 		} else if (*next == '.') {
24943fb4f7cdSSrinivas Pandruvada 			next += 1;
24953fb4f7cdSSrinivas Pandruvada 			if (*next == '.')
24963fb4f7cdSSrinivas Pandruvada 				next += 1; /* start range */
24973fb4f7cdSSrinivas Pandruvada 			else
24983fb4f7cdSSrinivas Pandruvada 				goto error;
24993fb4f7cdSSrinivas Pandruvada 		}
25003fb4f7cdSSrinivas Pandruvada 
25013fb4f7cdSSrinivas Pandruvada 		end = strtoul(next, &next, 10);
25023fb4f7cdSSrinivas Pandruvada 		if (end <= start)
25033fb4f7cdSSrinivas Pandruvada 			goto error;
25043fb4f7cdSSrinivas Pandruvada 
25053fb4f7cdSSrinivas Pandruvada 		while (++start <= end) {
25063fb4f7cdSSrinivas Pandruvada 			if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
25073fb4f7cdSSrinivas Pandruvada 				target_cpus[max_target_cpus++] = start;
25083fb4f7cdSSrinivas Pandruvada 		}
25093fb4f7cdSSrinivas Pandruvada 
25103fb4f7cdSSrinivas Pandruvada 		if (*next == ',')
25113fb4f7cdSSrinivas Pandruvada 			next += 1;
25123fb4f7cdSSrinivas Pandruvada 		else if (*next != '\0')
25133fb4f7cdSSrinivas Pandruvada 			goto error;
25143fb4f7cdSSrinivas Pandruvada 	}
25153fb4f7cdSSrinivas Pandruvada 
25163fb4f7cdSSrinivas Pandruvada #ifdef DEBUG
25173fb4f7cdSSrinivas Pandruvada 	{
25183fb4f7cdSSrinivas Pandruvada 		int i;
25193fb4f7cdSSrinivas Pandruvada 
25203fb4f7cdSSrinivas Pandruvada 		for (i = 0; i < max_target_cpus; ++i)
25213fb4f7cdSSrinivas Pandruvada 			printf("cpu [%d] in arg\n", target_cpus[i]);
25223fb4f7cdSSrinivas Pandruvada 	}
25233fb4f7cdSSrinivas Pandruvada #endif
25243fb4f7cdSSrinivas Pandruvada 	return;
25253fb4f7cdSSrinivas Pandruvada 
25263fb4f7cdSSrinivas Pandruvada error:
25273fb4f7cdSSrinivas Pandruvada 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
25283fb4f7cdSSrinivas Pandruvada 	exit(-1);
25293fb4f7cdSSrinivas Pandruvada }
25303fb4f7cdSSrinivas Pandruvada 
25313fb4f7cdSSrinivas Pandruvada static void parse_cmd_args(int argc, int start, char **argv)
25323fb4f7cdSSrinivas Pandruvada {
25333fb4f7cdSSrinivas Pandruvada 	int opt;
25343fb4f7cdSSrinivas Pandruvada 	int option_index;
25353fb4f7cdSSrinivas Pandruvada 
25363fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
25373fb4f7cdSSrinivas Pandruvada 		{ "bucket", required_argument, 0, 'b' },
25383fb4f7cdSSrinivas Pandruvada 		{ "level", required_argument, 0, 'l' },
25393c64c81aSSrinivas Pandruvada 		{ "online", required_argument, 0, 'o' },
25403fb4f7cdSSrinivas Pandruvada 		{ "trl-type", required_argument, 0, 'r' },
25413fb4f7cdSSrinivas Pandruvada 		{ "trl", required_argument, 0, 't' },
25423fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
25433fb4f7cdSSrinivas Pandruvada 		{ "clos", required_argument, 0, 'c' },
25443fb4f7cdSSrinivas Pandruvada 		{ "desired", required_argument, 0, 'd' },
25453fb4f7cdSSrinivas Pandruvada 		{ "epp", required_argument, 0, 'e' },
25463fb4f7cdSSrinivas Pandruvada 		{ "min", required_argument, 0, 'n' },
25473fb4f7cdSSrinivas Pandruvada 		{ "max", required_argument, 0, 'm' },
25483fb4f7cdSSrinivas Pandruvada 		{ "priority", required_argument, 0, 'p' },
25493fb4f7cdSSrinivas Pandruvada 		{ "weight", required_argument, 0, 'w' },
2550354bd06fSSrinivas Pandruvada 		{ "auto", no_argument, 0, 'a' },
25513fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
25523fb4f7cdSSrinivas Pandruvada 	};
25533fb4f7cdSSrinivas Pandruvada 
25543fb4f7cdSSrinivas Pandruvada 	option_index = start;
25553fb4f7cdSSrinivas Pandruvada 
25563fb4f7cdSSrinivas Pandruvada 	optind = start + 1;
2557b86639e1SSrinivas Pandruvada 	while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:r:hoa",
25583fb4f7cdSSrinivas Pandruvada 				  long_options, &option_index)) != -1) {
25593fb4f7cdSSrinivas Pandruvada 		switch (opt) {
2560354bd06fSSrinivas Pandruvada 		case 'a':
2561354bd06fSSrinivas Pandruvada 			auto_mode = 1;
2562354bd06fSSrinivas Pandruvada 			break;
25633fb4f7cdSSrinivas Pandruvada 		case 'b':
25643fb4f7cdSSrinivas Pandruvada 			fact_bucket = atoi(optarg);
25653fb4f7cdSSrinivas Pandruvada 			break;
25663fb4f7cdSSrinivas Pandruvada 		case 'h':
25673fb4f7cdSSrinivas Pandruvada 			cmd_help = 1;
25683fb4f7cdSSrinivas Pandruvada 			break;
25693fb4f7cdSSrinivas Pandruvada 		case 'l':
25703fb4f7cdSSrinivas Pandruvada 			tdp_level = atoi(optarg);
25713fb4f7cdSSrinivas Pandruvada 			break;
25723c64c81aSSrinivas Pandruvada 		case 'o':
25733c64c81aSSrinivas Pandruvada 			force_online_offline = 1;
25743c64c81aSSrinivas Pandruvada 			break;
25753fb4f7cdSSrinivas Pandruvada 		case 't':
25763fb4f7cdSSrinivas Pandruvada 			sscanf(optarg, "0x%llx", &fact_trl);
25773fb4f7cdSSrinivas Pandruvada 			break;
25783fb4f7cdSSrinivas Pandruvada 		case 'r':
25793fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "sse", 3)) {
25803fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x01;
25813fb4f7cdSSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx2", 4)) {
25823fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x02;
2583b86639e1SSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx512", 6)) {
25843fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x04;
25853fb4f7cdSSrinivas Pandruvada 			} else {
25863fb4f7cdSSrinivas Pandruvada 				fprintf(outf, "Invalid sse,avx options\n");
25873fb4f7cdSSrinivas Pandruvada 				exit(1);
25883fb4f7cdSSrinivas Pandruvada 			}
25893fb4f7cdSSrinivas Pandruvada 			break;
25903fb4f7cdSSrinivas Pandruvada 		/* CLOS related */
25913fb4f7cdSSrinivas Pandruvada 		case 'c':
25923fb4f7cdSSrinivas Pandruvada 			current_clos = atoi(optarg);
25933fb4f7cdSSrinivas Pandruvada 			break;
25943fb4f7cdSSrinivas Pandruvada 		case 'd':
25953fb4f7cdSSrinivas Pandruvada 			clos_desired = atoi(optarg);
259640dee9ddSSrinivas Pandruvada 			clos_desired /= DISP_FREQ_MULTIPLIER;
25973fb4f7cdSSrinivas Pandruvada 			break;
25983fb4f7cdSSrinivas Pandruvada 		case 'e':
25993fb4f7cdSSrinivas Pandruvada 			clos_epp = atoi(optarg);
2600fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform()) {
2601fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "epp can't be specified on this platform", 0, 0);
2602fe6fb216SSrinivas Pandruvada 				exit(0);
2603fe6fb216SSrinivas Pandruvada 			}
26043fb4f7cdSSrinivas Pandruvada 			break;
26053fb4f7cdSSrinivas Pandruvada 		case 'n':
26063fb4f7cdSSrinivas Pandruvada 			clos_min = atoi(optarg);
260740dee9ddSSrinivas Pandruvada 			clos_min /= DISP_FREQ_MULTIPLIER;
26083fb4f7cdSSrinivas Pandruvada 			break;
26093fb4f7cdSSrinivas Pandruvada 		case 'm':
26103fb4f7cdSSrinivas Pandruvada 			clos_max = atoi(optarg);
261140dee9ddSSrinivas Pandruvada 			clos_max /= DISP_FREQ_MULTIPLIER;
26123fb4f7cdSSrinivas Pandruvada 			break;
26133fb4f7cdSSrinivas Pandruvada 		case 'p':
26143fb4f7cdSSrinivas Pandruvada 			clos_priority_type = atoi(optarg);
2615fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform() && !clos_priority_type) {
2616fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "Invalid clos priority type: proportional for this platform", 0, 0);
2617fe6fb216SSrinivas Pandruvada 				exit(0);
2618fe6fb216SSrinivas Pandruvada 			}
26193fb4f7cdSSrinivas Pandruvada 			break;
26203fb4f7cdSSrinivas Pandruvada 		case 'w':
26213fb4f7cdSSrinivas Pandruvada 			clos_prop_prio = atoi(optarg);
2622fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform()) {
2623fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "weight can't be specified on this platform", 0, 0);
2624fe6fb216SSrinivas Pandruvada 				exit(0);
2625fe6fb216SSrinivas Pandruvada 			}
26263fb4f7cdSSrinivas Pandruvada 			break;
26273fb4f7cdSSrinivas Pandruvada 		default:
26284a960353SSrinivas Pandruvada 			printf("Unknown option: ignore\n");
26293fb4f7cdSSrinivas Pandruvada 		}
26303fb4f7cdSSrinivas Pandruvada 	}
26314a960353SSrinivas Pandruvada 
26324a960353SSrinivas Pandruvada 	if (argv[optind])
26334a960353SSrinivas Pandruvada 		printf("Garbage at the end of command: ignore\n");
26343fb4f7cdSSrinivas Pandruvada }
26353fb4f7cdSSrinivas Pandruvada 
26363fb4f7cdSSrinivas Pandruvada static void isst_help(void)
26373fb4f7cdSSrinivas Pandruvada {
26383fb4f7cdSSrinivas Pandruvada 	printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
26393fb4f7cdSSrinivas Pandruvada 		performance profiles per system via static and/or dynamic\n\
26403fb4f7cdSSrinivas Pandruvada 		adjustment of core count, workload, Tjmax, and\n\
26413fb4f7cdSSrinivas Pandruvada 		TDP, etc.\n");
26423fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=perf-profile\n");
26433fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
2644c829f0efSPrarit Bhargava 
2645c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
26463fb4f7cdSSrinivas Pandruvada 		printf("\tget-lock-status\n");
26473fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-levels\n");
26483fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-version\n");
26493fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-enabled\n");
26503fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-current-level\n");
26513fb4f7cdSSrinivas Pandruvada 		printf("\tset-config-level\n");
26523fb4f7cdSSrinivas Pandruvada 	}
2653c829f0efSPrarit Bhargava }
26543fb4f7cdSSrinivas Pandruvada 
26553fb4f7cdSSrinivas Pandruvada static void pbf_help(void)
26563fb4f7cdSSrinivas Pandruvada {
26573fb4f7cdSSrinivas Pandruvada 	printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
26583fb4f7cdSSrinivas Pandruvada 		on certain cores (high priority cores) in exchange for lower\n\
26593fb4f7cdSSrinivas Pandruvada 		base frequency on remaining cores (low priority cores).\n");
26603fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
26613fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
26623fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
26633fb4f7cdSSrinivas Pandruvada }
26643fb4f7cdSSrinivas Pandruvada 
26653fb4f7cdSSrinivas Pandruvada static void fact_help(void)
26663fb4f7cdSSrinivas Pandruvada {
26673fb4f7cdSSrinivas Pandruvada 	printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
26683fb4f7cdSSrinivas Pandruvada 		limits to cores based on priority.\n");
26693fb4f7cdSSrinivas Pandruvada 	printf("\nCommand: For feature=turbo-freq\n");
26703fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
26713fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
26723fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
26733fb4f7cdSSrinivas Pandruvada }
26743fb4f7cdSSrinivas Pandruvada 
2675006050a6SSrinivas Pandruvada static void turbo_mode_help(void)
2676006050a6SSrinivas Pandruvada {
26772c7dc57eSSrinivas 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");
2678006050a6SSrinivas Pandruvada 	printf("\tcommand : enable\n");
2679006050a6SSrinivas Pandruvada 	printf("\tcommand : disable\n");
26802c7dc57eSSrinivas Pandruvada 	printf("\tcommand : get-trl\n");
26812c7dc57eSSrinivas Pandruvada 	printf("\tcommand : set-trl\n");
2682006050a6SSrinivas Pandruvada }
2683006050a6SSrinivas Pandruvada 
2684006050a6SSrinivas Pandruvada 
26853fb4f7cdSSrinivas Pandruvada static void core_power_help(void)
26863fb4f7cdSSrinivas Pandruvada {
26873fb4f7cdSSrinivas Pandruvada 	printf("core-power:\tInterface that allows user to define per core/tile\n\
26883fb4f7cdSSrinivas Pandruvada 		priority.\n");
26893fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=core-power\n");
26903fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
26913fb4f7cdSSrinivas Pandruvada 	printf("\tenable\n");
26923fb4f7cdSSrinivas Pandruvada 	printf("\tdisable\n");
26933fb4f7cdSSrinivas Pandruvada 	printf("\tconfig\n");
2694188afed9SSrinivas Pandruvada 	printf("\tget-config\n");
26953fb4f7cdSSrinivas Pandruvada 	printf("\tassoc\n");
26963fb4f7cdSSrinivas Pandruvada 	printf("\tget-assoc\n");
26973fb4f7cdSSrinivas Pandruvada }
26983fb4f7cdSSrinivas Pandruvada 
26993fb4f7cdSSrinivas Pandruvada struct process_cmd_help_struct {
27003fb4f7cdSSrinivas Pandruvada 	char *feature;
27013fb4f7cdSSrinivas Pandruvada 	void (*process_fn)(void);
27023fb4f7cdSSrinivas Pandruvada };
27033fb4f7cdSSrinivas Pandruvada 
27043fb4f7cdSSrinivas Pandruvada static struct process_cmd_help_struct isst_help_cmds[] = {
27053fb4f7cdSSrinivas Pandruvada 	{ "perf-profile", isst_help },
27063fb4f7cdSSrinivas Pandruvada 	{ "base-freq", pbf_help },
27073fb4f7cdSSrinivas Pandruvada 	{ "turbo-freq", fact_help },
27083fb4f7cdSSrinivas Pandruvada 	{ "core-power", core_power_help },
2709006050a6SSrinivas Pandruvada 	{ "turbo-mode", turbo_mode_help },
27103fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL }
27113fb4f7cdSSrinivas Pandruvada };
27123fb4f7cdSSrinivas Pandruvada 
2713c829f0efSPrarit Bhargava static struct process_cmd_help_struct clx_n_help_cmds[] = {
2714c829f0efSPrarit Bhargava 	{ "perf-profile", isst_help },
2715c829f0efSPrarit Bhargava 	{ "base-freq", pbf_help },
2716c829f0efSPrarit Bhargava 	{ NULL, NULL }
2717c829f0efSPrarit Bhargava };
2718c829f0efSPrarit Bhargava 
2719210369dcSPrarit Bhargava void process_command(int argc, char **argv,
2720210369dcSPrarit Bhargava 		     struct process_cmd_help_struct *help_cmds,
2721210369dcSPrarit Bhargava 		     struct process_cmd_struct *cmds)
27223fb4f7cdSSrinivas Pandruvada {
27233fb4f7cdSSrinivas Pandruvada 	int i = 0, matched = 0;
27243fb4f7cdSSrinivas Pandruvada 	char *feature = argv[optind];
27253fb4f7cdSSrinivas Pandruvada 	char *cmd = argv[optind + 1];
27263fb4f7cdSSrinivas Pandruvada 
27273fb4f7cdSSrinivas Pandruvada 	if (!feature || !cmd)
27283fb4f7cdSSrinivas Pandruvada 		return;
27293fb4f7cdSSrinivas Pandruvada 
27303fb4f7cdSSrinivas Pandruvada 	debug_printf("feature name [%s] command [%s]\n", feature, cmd);
27313fb4f7cdSSrinivas Pandruvada 	if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
2732210369dcSPrarit Bhargava 		while (help_cmds[i].feature) {
2733210369dcSPrarit Bhargava 			if (!strcmp(help_cmds[i].feature, feature)) {
2734210369dcSPrarit Bhargava 				help_cmds[i].process_fn();
27353fb4f7cdSSrinivas Pandruvada 				exit(0);
27363fb4f7cdSSrinivas Pandruvada 			}
27373fb4f7cdSSrinivas Pandruvada 			++i;
27383fb4f7cdSSrinivas Pandruvada 		}
27393fb4f7cdSSrinivas Pandruvada 	}
27403fb4f7cdSSrinivas Pandruvada 
2741062e4aacSPrarit Bhargava 	if (!is_clx_n_platform())
27423fb4f7cdSSrinivas Pandruvada 		create_cpu_map();
27433fb4f7cdSSrinivas Pandruvada 
27443fb4f7cdSSrinivas Pandruvada 	i = 0;
2745210369dcSPrarit Bhargava 	while (cmds[i].feature) {
2746210369dcSPrarit Bhargava 		if (!strcmp(cmds[i].feature, feature) &&
2747210369dcSPrarit Bhargava 		    !strcmp(cmds[i].command, cmd)) {
27483fb4f7cdSSrinivas Pandruvada 			parse_cmd_args(argc, optind + 1, argv);
2749210369dcSPrarit Bhargava 			cmds[i].process_fn(cmds[i].arg);
27503fb4f7cdSSrinivas Pandruvada 			matched = 1;
27513fb4f7cdSSrinivas Pandruvada 			break;
27523fb4f7cdSSrinivas Pandruvada 		}
27533fb4f7cdSSrinivas Pandruvada 		++i;
27543fb4f7cdSSrinivas Pandruvada 	}
27553fb4f7cdSSrinivas Pandruvada 
27563fb4f7cdSSrinivas Pandruvada 	if (!matched)
27573fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Invalid command\n");
27583fb4f7cdSSrinivas Pandruvada }
27593fb4f7cdSSrinivas Pandruvada 
27603fb4f7cdSSrinivas Pandruvada static void usage(void)
27613fb4f7cdSSrinivas Pandruvada {
2762addd116dSSrinivas Pandruvada 	if (is_clx_n_platform()) {
2763addd116dSSrinivas Pandruvada 		fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
2764addd116dSSrinivas Pandruvada 		fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
2765addd116dSSrinivas Pandruvada 	}
2766addd116dSSrinivas Pandruvada 
27673fb4f7cdSSrinivas Pandruvada 	printf("\nUsage:\n");
27683fb4f7cdSSrinivas Pandruvada 	printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
2769addd116dSSrinivas Pandruvada 	printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
2770addd116dSSrinivas Pandruvada 	if (is_clx_n_platform())
2771addd116dSSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq]\n");
2772addd116dSSrinivas Pandruvada 	else
2773006050a6SSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power|turbo-mode]\n");
277443774c0dSPrarit Bhargava 	printf("\nFor help on each feature, use -h|--help\n");
27753fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile -h\n");
27763fb4f7cdSSrinivas Pandruvada 
27773fb4f7cdSSrinivas Pandruvada 	printf("\nFor additional help on each command for a feature, use --h|--help\n");
27783fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
27793fb4f7cdSSrinivas Pandruvada 	printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
27803fb4f7cdSSrinivas Pandruvada 
27813fb4f7cdSSrinivas Pandruvada 	printf("\nOPTIONS\n");
27823fb4f7cdSSrinivas Pandruvada 	printf("\t[-c|--cpu] : logical cpu number\n");
27833fb4f7cdSSrinivas Pandruvada 	printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
27843fb4f7cdSSrinivas Pandruvada 	printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
27853fb4f7cdSSrinivas Pandruvada 	printf("\t[-d|--debug] : Debug mode\n");
2786addd116dSSrinivas Pandruvada 	printf("\t[-f|--format] : output format [json|text]. Default: text\n");
27873fb4f7cdSSrinivas Pandruvada 	printf("\t[-h|--help] : Print help\n");
27883fb4f7cdSSrinivas Pandruvada 	printf("\t[-i|--info] : Print platform information\n");
27890d3dfd75SSrinivas Pandruvada 	printf("\t[-a|--all-cpus-online] : Force online every CPU in the system\n");
27903fb4f7cdSSrinivas Pandruvada 	printf("\t[-o|--out] : Output file\n");
27913fb4f7cdSSrinivas Pandruvada 	printf("\t\t\tDefault : stderr\n");
2792a85a35fcSSrinivas Pandruvada 	printf("\t[-p|--pause] : Delay between two mail box commands in milliseconds\n");
279332279be7SSrinivas Pandruvada 	printf("\t[-r|--retry] : Retry count for mail box commands on failure, default 3\n");
27943fb4f7cdSSrinivas Pandruvada 	printf("\t[-v|--version] : Print version\n");
27957fd786dfSSrinivas Pandruvada 	printf("\t[-b|--oob : Start a daemon to process HFI events for perf profile change from Out of Band agent.\n");
27967fd786dfSSrinivas Pandruvada 	printf("\t[-n|--no-daemon : Don't run as daemon. By default --oob will turn on daemon mode\n");
27977fd786dfSSrinivas Pandruvada 	printf("\t[-w|--delay : Delay for reading config level state change in OOB poll mode.\n");
27983fb4f7cdSSrinivas Pandruvada 	printf("\nResult format\n");
27993fb4f7cdSSrinivas Pandruvada 	printf("\tResult display uses a common format for each command:\n");
28003fb4f7cdSSrinivas Pandruvada 	printf("\tResults are formatted in text/JSON with\n");
28013fb4f7cdSSrinivas Pandruvada 	printf("\t\tPackage, Die, CPU, and command specific results.\n");
2802addd116dSSrinivas Pandruvada 
2803addd116dSSrinivas Pandruvada 	printf("\nExamples\n");
2804addd116dSSrinivas Pandruvada 	printf("\tTo get platform information:\n");
2805addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select --info\n");
2806addd116dSSrinivas Pandruvada 	printf("\tTo get full perf-profile information dump:\n");
2807addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select perf-profile info\n");
2808addd116dSSrinivas Pandruvada 	printf("\tTo get full base-freq information dump:\n");
2809addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select base-freq info -l 0\n");
2810addd116dSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2811addd116dSSrinivas Pandruvada 		printf("\tTo get full turbo-freq information dump:\n");
2812addd116dSSrinivas Pandruvada 		printf("\t\tintel-speed-select turbo-freq info -l 0\n");
2813addd116dSSrinivas Pandruvada 	}
28143fb4f7cdSSrinivas Pandruvada 	exit(1);
28153fb4f7cdSSrinivas Pandruvada }
28163fb4f7cdSSrinivas Pandruvada 
28173fb4f7cdSSrinivas Pandruvada static void print_version(void)
28183fb4f7cdSSrinivas Pandruvada {
28193fb4f7cdSSrinivas Pandruvada 	fprintf(outf, "Version %s\n", version_str);
28203fb4f7cdSSrinivas Pandruvada 	exit(0);
28213fb4f7cdSSrinivas Pandruvada }
28223fb4f7cdSSrinivas Pandruvada 
28233fb4f7cdSSrinivas Pandruvada static void cmdline(int argc, char **argv)
28243fb4f7cdSSrinivas Pandruvada {
2825f362cdccSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
2826a85a35fcSSrinivas Pandruvada 	char *ptr;
2827f362cdccSSrinivas Pandruvada 	FILE *fp;
28280d3dfd75SSrinivas Pandruvada 	int opt, force_cpus_online = 0;
28293fb4f7cdSSrinivas Pandruvada 	int option_index = 0;
28303bc3d30cSPrarit Bhargava 	int ret;
28317fd786dfSSrinivas Pandruvada 	int oob_mode = 0;
28327fd786dfSSrinivas Pandruvada 	int poll_interval = -1;
28337fd786dfSSrinivas Pandruvada 	int no_daemon = 0;
28343fb4f7cdSSrinivas Pandruvada 
28353fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
28360d3dfd75SSrinivas Pandruvada 		{ "all-cpus-online", no_argument, 0, 'a' },
28373fb4f7cdSSrinivas Pandruvada 		{ "cpu", required_argument, 0, 'c' },
28383fb4f7cdSSrinivas Pandruvada 		{ "debug", no_argument, 0, 'd' },
28393fb4f7cdSSrinivas Pandruvada 		{ "format", required_argument, 0, 'f' },
28403fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
28413fb4f7cdSSrinivas Pandruvada 		{ "info", no_argument, 0, 'i' },
2842a85a35fcSSrinivas Pandruvada 		{ "pause", required_argument, 0, 'p' },
28433fb4f7cdSSrinivas Pandruvada 		{ "out", required_argument, 0, 'o' },
284432279be7SSrinivas Pandruvada 		{ "retry", required_argument, 0, 'r' },
28453fb4f7cdSSrinivas Pandruvada 		{ "version", no_argument, 0, 'v' },
28467fd786dfSSrinivas Pandruvada 		{ "oob", no_argument, 0, 'b' },
28477fd786dfSSrinivas Pandruvada 		{ "no-daemon", no_argument, 0, 'n' },
28487fd786dfSSrinivas Pandruvada 		{ "poll-interval", required_argument, 0, 'w' },
28493fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
28503fb4f7cdSSrinivas Pandruvada 	};
28513fb4f7cdSSrinivas Pandruvada 
2852f362cdccSSrinivas Pandruvada 	if (geteuid() != 0) {
2853f362cdccSSrinivas Pandruvada 		fprintf(stderr, "Must run as root\n");
2854f362cdccSSrinivas Pandruvada 		exit(0);
2855f362cdccSSrinivas Pandruvada 	}
2856f362cdccSSrinivas Pandruvada 
2857f362cdccSSrinivas Pandruvada 	ret = update_cpu_model();
2858f362cdccSSrinivas Pandruvada 	if (ret)
2859f362cdccSSrinivas Pandruvada 		err(-1, "Invalid CPU model (%d)\n", cpu_model);
2860f362cdccSSrinivas Pandruvada 	printf("Intel(R) Speed Select Technology\n");
2861f362cdccSSrinivas Pandruvada 	printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2862f362cdccSSrinivas Pandruvada 
2863f362cdccSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2864f362cdccSSrinivas Pandruvada 		fp = fopen(pathname, "rb");
2865f362cdccSSrinivas Pandruvada 		if (!fp) {
2866f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
2867f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
2868f362cdccSSrinivas Pandruvada 			fprintf(stderr, "If the config is included then this is not a supported platform.\n");
2869f362cdccSSrinivas Pandruvada 			exit(0);
2870f362cdccSSrinivas Pandruvada 		}
2871f362cdccSSrinivas Pandruvada 		fclose(fp);
2872f362cdccSSrinivas Pandruvada 	}
2873f362cdccSSrinivas Pandruvada 
28743fb4f7cdSSrinivas Pandruvada 	progname = argv[0];
28757fd786dfSSrinivas Pandruvada 	while ((opt = getopt_long_only(argc, argv, "+c:df:hio:vabw:n", long_options,
28763fb4f7cdSSrinivas Pandruvada 				       &option_index)) != -1) {
28773fb4f7cdSSrinivas Pandruvada 		switch (opt) {
28780d3dfd75SSrinivas Pandruvada 		case 'a':
28790d3dfd75SSrinivas Pandruvada 			force_cpus_online = 1;
28800d3dfd75SSrinivas Pandruvada 			break;
28813fb4f7cdSSrinivas Pandruvada 		case 'c':
28823fb4f7cdSSrinivas Pandruvada 			parse_cpu_command(optarg);
28833fb4f7cdSSrinivas Pandruvada 			break;
28843fb4f7cdSSrinivas Pandruvada 		case 'd':
28853fb4f7cdSSrinivas Pandruvada 			debug_flag = 1;
28863fb4f7cdSSrinivas Pandruvada 			printf("Debug Mode ON\n");
28873fb4f7cdSSrinivas Pandruvada 			break;
28883fb4f7cdSSrinivas Pandruvada 		case 'f':
28893fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "json", 4))
28903fb4f7cdSSrinivas Pandruvada 				out_format_json = 1;
28913fb4f7cdSSrinivas Pandruvada 			break;
28923fb4f7cdSSrinivas Pandruvada 		case 'h':
28933fb4f7cdSSrinivas Pandruvada 			usage();
28943fb4f7cdSSrinivas Pandruvada 			break;
28953fb4f7cdSSrinivas Pandruvada 		case 'i':
28963fb4f7cdSSrinivas Pandruvada 			isst_print_platform_information();
28973fb4f7cdSSrinivas Pandruvada 			break;
28983fb4f7cdSSrinivas Pandruvada 		case 'o':
28993fb4f7cdSSrinivas Pandruvada 			if (outf)
29003fb4f7cdSSrinivas Pandruvada 				fclose(outf);
29013fb4f7cdSSrinivas Pandruvada 			outf = fopen_or_exit(optarg, "w");
29023fb4f7cdSSrinivas Pandruvada 			break;
2903a85a35fcSSrinivas Pandruvada 		case 'p':
2904a85a35fcSSrinivas Pandruvada 			ret = strtol(optarg, &ptr, 10);
2905a85a35fcSSrinivas Pandruvada 			if (!ret)
2906a85a35fcSSrinivas Pandruvada 				fprintf(stderr, "Invalid pause interval, ignore\n");
2907a85a35fcSSrinivas Pandruvada 			else
2908a85a35fcSSrinivas Pandruvada 				mbox_delay = ret;
2909a85a35fcSSrinivas Pandruvada 			break;
291032279be7SSrinivas Pandruvada 		case 'r':
291132279be7SSrinivas Pandruvada 			ret = strtol(optarg, &ptr, 10);
291232279be7SSrinivas Pandruvada 			if (!ret)
291332279be7SSrinivas Pandruvada 				fprintf(stderr, "Invalid retry count, ignore\n");
291432279be7SSrinivas Pandruvada 			else
291532279be7SSrinivas Pandruvada 				mbox_retries = ret;
291632279be7SSrinivas Pandruvada 			break;
29173fb4f7cdSSrinivas Pandruvada 		case 'v':
29183fb4f7cdSSrinivas Pandruvada 			print_version();
29193fb4f7cdSSrinivas Pandruvada 			break;
29207fd786dfSSrinivas Pandruvada 		case 'b':
29217fd786dfSSrinivas Pandruvada 			oob_mode = 1;
29227fd786dfSSrinivas Pandruvada 			break;
29237fd786dfSSrinivas Pandruvada 		case 'n':
29247fd786dfSSrinivas Pandruvada 			no_daemon = 1;
29257fd786dfSSrinivas Pandruvada 			break;
29267fd786dfSSrinivas Pandruvada 		case 'w':
29277fd786dfSSrinivas Pandruvada 			ret = strtol(optarg, &ptr, 10);
29287fd786dfSSrinivas Pandruvada 			if (!ret) {
29297fd786dfSSrinivas Pandruvada 				fprintf(stderr, "Invalid poll interval count\n");
29307fd786dfSSrinivas Pandruvada 				exit(0);
29317fd786dfSSrinivas Pandruvada 			}
29327fd786dfSSrinivas Pandruvada 			poll_interval = ret;
29337fd786dfSSrinivas Pandruvada 			break;
29343fb4f7cdSSrinivas Pandruvada 		default:
29353fb4f7cdSSrinivas Pandruvada 			usage();
29363fb4f7cdSSrinivas Pandruvada 		}
29373fb4f7cdSSrinivas Pandruvada 	}
29383fb4f7cdSSrinivas Pandruvada 
29397fd786dfSSrinivas Pandruvada 	if (optind > (argc - 2) && !oob_mode) {
2940addd116dSSrinivas Pandruvada 		usage();
29413fb4f7cdSSrinivas Pandruvada 		exit(0);
29423fb4f7cdSSrinivas Pandruvada 	}
29433fb4f7cdSSrinivas Pandruvada 	set_max_cpu_num();
29440d3dfd75SSrinivas Pandruvada 	if (force_cpus_online)
29450d3dfd75SSrinivas Pandruvada 		force_all_cpus_online();
2946fb186158SSrinivas Pandruvada 	store_cpu_topology();
29473fb4f7cdSSrinivas Pandruvada 	set_cpu_present_cpu_mask();
29483fb4f7cdSSrinivas Pandruvada 	set_cpu_target_cpu_mask();
2949c829f0efSPrarit Bhargava 
29507fd786dfSSrinivas Pandruvada 	if (oob_mode) {
29517fd786dfSSrinivas Pandruvada 		create_cpu_map();
29527fd786dfSSrinivas Pandruvada 		if (debug_flag)
29537fd786dfSSrinivas Pandruvada 			fprintf(stderr, "OOB mode is enabled in debug mode\n");
29547fd786dfSSrinivas Pandruvada 
29557fd786dfSSrinivas Pandruvada 		ret = isst_daemon(debug_flag, poll_interval, no_daemon);
29567fd786dfSSrinivas Pandruvada 		if (ret)
29577fd786dfSSrinivas Pandruvada 			fprintf(stderr, "OOB mode enable failed\n");
29587fd786dfSSrinivas Pandruvada 		goto out;
29597fd786dfSSrinivas Pandruvada 	}
29607fd786dfSSrinivas Pandruvada 
2961c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
29623bc3d30cSPrarit Bhargava 		ret = isst_fill_platform_info();
29633bc3d30cSPrarit Bhargava 		if (ret)
29643bc3d30cSPrarit Bhargava 			goto out;
2965210369dcSPrarit Bhargava 		process_command(argc, argv, isst_help_cmds, isst_cmds);
2966c829f0efSPrarit Bhargava 	} else {
2967c829f0efSPrarit Bhargava 		process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
2968c829f0efSPrarit Bhargava 	}
29693bc3d30cSPrarit Bhargava out:
29703bc3d30cSPrarit Bhargava 	free_cpu_set(present_cpumask);
29713bc3d30cSPrarit Bhargava 	free_cpu_set(target_cpumask);
29723fb4f7cdSSrinivas Pandruvada }
29733fb4f7cdSSrinivas Pandruvada 
29743fb4f7cdSSrinivas Pandruvada int main(int argc, char **argv)
29753fb4f7cdSSrinivas Pandruvada {
29763fb4f7cdSSrinivas Pandruvada 	outf = stderr;
29773fb4f7cdSSrinivas Pandruvada 	cmdline(argc, argv);
29783fb4f7cdSSrinivas Pandruvada 	return 0;
29793fb4f7cdSSrinivas Pandruvada }
2980