xref: /linux/tools/power/x86/intel-speed-select/isst-config.c (revision ca56725d78c5241f65cc565e5a865f45d943dc04)
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;
66*ca56725dSZhang Rui 	unsigned short initialized;
673fb4f7cdSSrinivas Pandruvada };
683fb4f7cdSSrinivas Pandruvada struct _cpu_map *cpu_map;
693fb4f7cdSSrinivas Pandruvada 
70fb186158SSrinivas Pandruvada struct cpu_topology {
71fb186158SSrinivas Pandruvada 	short cpu;
72fb186158SSrinivas Pandruvada 	short core_id;
73fb186158SSrinivas Pandruvada 	short pkg_id;
74fb186158SSrinivas Pandruvada 	short die_id;
75fb186158SSrinivas Pandruvada };
76fb186158SSrinivas Pandruvada 
7787e115b3SSrinivas Pandruvada FILE *get_output_file(void)
7887e115b3SSrinivas Pandruvada {
7987e115b3SSrinivas Pandruvada 	return outf;
8087e115b3SSrinivas Pandruvada }
8187e115b3SSrinivas Pandruvada 
823fb4f7cdSSrinivas Pandruvada void debug_printf(const char *format, ...)
833fb4f7cdSSrinivas Pandruvada {
843fb4f7cdSSrinivas Pandruvada 	va_list args;
853fb4f7cdSSrinivas Pandruvada 
863fb4f7cdSSrinivas Pandruvada 	va_start(args, format);
873fb4f7cdSSrinivas Pandruvada 
883fb4f7cdSSrinivas Pandruvada 	if (debug_flag)
893fb4f7cdSSrinivas Pandruvada 		vprintf(format, args);
903fb4f7cdSSrinivas Pandruvada 
913fb4f7cdSSrinivas Pandruvada 	va_end(args);
923fb4f7cdSSrinivas Pandruvada }
933fb4f7cdSSrinivas Pandruvada 
941c1d935cSPrarit Bhargava 
951c1d935cSPrarit Bhargava int is_clx_n_platform(void)
961c1d935cSPrarit Bhargava {
971c1d935cSPrarit Bhargava 	if (cpu_model == 0x55)
981c1d935cSPrarit Bhargava 		if (cpu_stepping == 0x6 || cpu_stepping == 0x7)
991c1d935cSPrarit Bhargava 			return 1;
1001c1d935cSPrarit Bhargava 	return 0;
1011c1d935cSPrarit Bhargava }
1021c1d935cSPrarit Bhargava 
10395f8e569SSrinivas Pandruvada int is_skx_based_platform(void)
10495f8e569SSrinivas Pandruvada {
10595f8e569SSrinivas Pandruvada 	if (cpu_model == 0x55)
10695f8e569SSrinivas Pandruvada 		return 1;
10795f8e569SSrinivas Pandruvada 
10895f8e569SSrinivas Pandruvada 	return 0;
10995f8e569SSrinivas Pandruvada }
11095f8e569SSrinivas Pandruvada 
111159f130fSSrinivas Pandruvada int is_spr_platform(void)
112159f130fSSrinivas Pandruvada {
113159f130fSSrinivas Pandruvada 	if (cpu_model == 0x8F)
114159f130fSSrinivas Pandruvada 		return 1;
115159f130fSSrinivas Pandruvada 
116159f130fSSrinivas Pandruvada 	return 0;
117159f130fSSrinivas Pandruvada }
118159f130fSSrinivas Pandruvada 
119159f130fSSrinivas Pandruvada int is_icx_platform(void)
120159f130fSSrinivas Pandruvada {
121159f130fSSrinivas Pandruvada 	if (cpu_model == 0x6A || cpu_model == 0x6C)
122159f130fSSrinivas Pandruvada 		return 1;
123159f130fSSrinivas Pandruvada 
124159f130fSSrinivas Pandruvada 	return 0;
125159f130fSSrinivas Pandruvada }
126159f130fSSrinivas Pandruvada 
1271c1d935cSPrarit Bhargava static int update_cpu_model(void)
1283fb4f7cdSSrinivas Pandruvada {
1293fb4f7cdSSrinivas Pandruvada 	unsigned int ebx, ecx, edx;
1303fb4f7cdSSrinivas Pandruvada 	unsigned int fms, family;
1313fb4f7cdSSrinivas Pandruvada 
1323fb4f7cdSSrinivas Pandruvada 	__cpuid(1, fms, ebx, ecx, edx);
1333fb4f7cdSSrinivas Pandruvada 	family = (fms >> 8) & 0xf;
1343fb4f7cdSSrinivas Pandruvada 	cpu_model = (fms >> 4) & 0xf;
1353fb4f7cdSSrinivas Pandruvada 	if (family == 6 || family == 0xf)
1363fb4f7cdSSrinivas Pandruvada 		cpu_model += ((fms >> 16) & 0xf) << 4;
1371c1d935cSPrarit Bhargava 
1381c1d935cSPrarit Bhargava 	cpu_stepping = fms & 0xf;
1391c1d935cSPrarit Bhargava 	/* only three CascadeLake-N models are supported */
1401c1d935cSPrarit Bhargava 	if (is_clx_n_platform()) {
1411c1d935cSPrarit Bhargava 		FILE *fp;
1421c1d935cSPrarit Bhargava 		size_t n = 0;
1431c1d935cSPrarit Bhargava 		char *line = NULL;
1441c1d935cSPrarit Bhargava 		int ret = 1;
1451c1d935cSPrarit Bhargava 
1461c1d935cSPrarit Bhargava 		fp = fopen("/proc/cpuinfo", "r");
1471c1d935cSPrarit Bhargava 		if (!fp)
1481c1d935cSPrarit Bhargava 			err(-1, "cannot open /proc/cpuinfo\n");
1491c1d935cSPrarit Bhargava 
1501c1d935cSPrarit Bhargava 		while (getline(&line, &n, fp) > 0) {
1511c1d935cSPrarit Bhargava 			if (strstr(line, "model name")) {
1521c1d935cSPrarit Bhargava 				if (strstr(line, "6252N") ||
1531c1d935cSPrarit Bhargava 				    strstr(line, "6230N") ||
1541c1d935cSPrarit Bhargava 				    strstr(line, "5218N"))
1551c1d935cSPrarit Bhargava 					ret = 0;
1561c1d935cSPrarit Bhargava 				break;
1571c1d935cSPrarit Bhargava 			}
1581c1d935cSPrarit Bhargava 		}
1591c1d935cSPrarit Bhargava 		free(line);
1601c1d935cSPrarit Bhargava 		fclose(fp);
1611c1d935cSPrarit Bhargava 		return ret;
1621c1d935cSPrarit Bhargava 	}
1631c1d935cSPrarit Bhargava 	return 0;
1643fb4f7cdSSrinivas Pandruvada }
1653fb4f7cdSSrinivas Pandruvada 
1663fb4f7cdSSrinivas Pandruvada /* Open a file, and exit on failure */
1673fb4f7cdSSrinivas Pandruvada static FILE *fopen_or_exit(const char *path, const char *mode)
1683fb4f7cdSSrinivas Pandruvada {
1693fb4f7cdSSrinivas Pandruvada 	FILE *filep = fopen(path, mode);
1703fb4f7cdSSrinivas Pandruvada 
1713fb4f7cdSSrinivas Pandruvada 	if (!filep)
1723fb4f7cdSSrinivas Pandruvada 		err(1, "%s: open failed", path);
1733fb4f7cdSSrinivas Pandruvada 
1743fb4f7cdSSrinivas Pandruvada 	return filep;
1753fb4f7cdSSrinivas Pandruvada }
1763fb4f7cdSSrinivas Pandruvada 
1773fb4f7cdSSrinivas Pandruvada /* Parse a file containing a single int */
1783fb4f7cdSSrinivas Pandruvada static int parse_int_file(int fatal, const char *fmt, ...)
1793fb4f7cdSSrinivas Pandruvada {
1803fb4f7cdSSrinivas Pandruvada 	va_list args;
1813fb4f7cdSSrinivas Pandruvada 	char path[PATH_MAX];
1823fb4f7cdSSrinivas Pandruvada 	FILE *filep;
1833fb4f7cdSSrinivas Pandruvada 	int value;
1843fb4f7cdSSrinivas Pandruvada 
1853fb4f7cdSSrinivas Pandruvada 	va_start(args, fmt);
1863fb4f7cdSSrinivas Pandruvada 	vsnprintf(path, sizeof(path), fmt, args);
1873fb4f7cdSSrinivas Pandruvada 	va_end(args);
1883fb4f7cdSSrinivas Pandruvada 	if (fatal) {
1893fb4f7cdSSrinivas Pandruvada 		filep = fopen_or_exit(path, "r");
1903fb4f7cdSSrinivas Pandruvada 	} else {
1913fb4f7cdSSrinivas Pandruvada 		filep = fopen(path, "r");
1923fb4f7cdSSrinivas Pandruvada 		if (!filep)
1933fb4f7cdSSrinivas Pandruvada 			return -1;
1943fb4f7cdSSrinivas Pandruvada 	}
1953fb4f7cdSSrinivas Pandruvada 	if (fscanf(filep, "%d", &value) != 1)
1963fb4f7cdSSrinivas Pandruvada 		err(1, "%s: failed to parse number from file", path);
1973fb4f7cdSSrinivas Pandruvada 	fclose(filep);
1983fb4f7cdSSrinivas Pandruvada 
1993fb4f7cdSSrinivas Pandruvada 	return value;
2003fb4f7cdSSrinivas Pandruvada }
2013fb4f7cdSSrinivas Pandruvada 
2023fb4f7cdSSrinivas Pandruvada int cpufreq_sysfs_present(void)
2033fb4f7cdSSrinivas Pandruvada {
2043fb4f7cdSSrinivas Pandruvada 	DIR *dir;
2053fb4f7cdSSrinivas Pandruvada 
2063fb4f7cdSSrinivas Pandruvada 	dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
2073fb4f7cdSSrinivas Pandruvada 	if (dir) {
2083fb4f7cdSSrinivas Pandruvada 		closedir(dir);
2093fb4f7cdSSrinivas Pandruvada 		return 1;
2103fb4f7cdSSrinivas Pandruvada 	}
2113fb4f7cdSSrinivas Pandruvada 
2123fb4f7cdSSrinivas Pandruvada 	return 0;
2133fb4f7cdSSrinivas Pandruvada }
2143fb4f7cdSSrinivas Pandruvada 
2153fb4f7cdSSrinivas Pandruvada int out_format_is_json(void)
2163fb4f7cdSSrinivas Pandruvada {
2173fb4f7cdSSrinivas Pandruvada 	return out_format_json;
2183fb4f7cdSSrinivas Pandruvada }
2193fb4f7cdSSrinivas Pandruvada 
220fb186158SSrinivas Pandruvada static int get_stored_topology_info(int cpu, int *core_id, int *pkg_id, int *die_id)
221fb186158SSrinivas Pandruvada {
222b1d12cefSSrinivas Pandruvada 	const char *pathname = "/var/run/isst_cpu_topology.dat";
223fb186158SSrinivas Pandruvada 	struct cpu_topology cpu_top;
224fb186158SSrinivas Pandruvada 	FILE *fp;
225fb186158SSrinivas Pandruvada 	int ret;
226fb186158SSrinivas Pandruvada 
227fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "rb");
228fb186158SSrinivas Pandruvada 	if (!fp)
229fb186158SSrinivas Pandruvada 		return -1;
230fb186158SSrinivas Pandruvada 
231fb186158SSrinivas Pandruvada 	ret = fseek(fp, cpu * sizeof(cpu_top), SEEK_SET);
232fb186158SSrinivas Pandruvada 	if (ret)
233fb186158SSrinivas Pandruvada 		goto err_ret;
234fb186158SSrinivas Pandruvada 
235fb186158SSrinivas Pandruvada 	ret = fread(&cpu_top, sizeof(cpu_top), 1, fp);
236fb186158SSrinivas Pandruvada 	if (ret != 1) {
237fb186158SSrinivas Pandruvada 		ret = -1;
238fb186158SSrinivas Pandruvada 		goto err_ret;
239fb186158SSrinivas Pandruvada 	}
240fb186158SSrinivas Pandruvada 
241fb186158SSrinivas Pandruvada 	*pkg_id = cpu_top.pkg_id;
242fb186158SSrinivas Pandruvada 	*core_id = cpu_top.core_id;
243fb186158SSrinivas Pandruvada 	*die_id = cpu_top.die_id;
244fb186158SSrinivas Pandruvada 	ret = 0;
245fb186158SSrinivas Pandruvada 
246fb186158SSrinivas Pandruvada err_ret:
247fb186158SSrinivas Pandruvada 	fclose(fp);
248fb186158SSrinivas Pandruvada 
249fb186158SSrinivas Pandruvada 	return ret;
250fb186158SSrinivas Pandruvada }
251fb186158SSrinivas Pandruvada 
252fb186158SSrinivas Pandruvada static void store_cpu_topology(void)
253fb186158SSrinivas Pandruvada {
254b1d12cefSSrinivas Pandruvada 	const char *pathname = "/var/run/isst_cpu_topology.dat";
255fb186158SSrinivas Pandruvada 	FILE *fp;
256fb186158SSrinivas Pandruvada 	int i;
257fb186158SSrinivas Pandruvada 
258fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "rb");
259fb186158SSrinivas Pandruvada 	if (fp) {
260fb186158SSrinivas Pandruvada 		/* Mapping already exists */
261fb186158SSrinivas Pandruvada 		fclose(fp);
262fb186158SSrinivas Pandruvada 		return;
263fb186158SSrinivas Pandruvada 	}
264fb186158SSrinivas Pandruvada 
265fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "wb");
266fb186158SSrinivas Pandruvada 	if (!fp) {
267fb186158SSrinivas Pandruvada 		fprintf(stderr, "Can't create file:%s\n", pathname);
268fb186158SSrinivas Pandruvada 		return;
269fb186158SSrinivas Pandruvada 	}
270fb186158SSrinivas Pandruvada 
271b1d12cefSSrinivas Pandruvada 	fprintf(stderr, "Caching topology information\n");
272b1d12cefSSrinivas Pandruvada 
273fb186158SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
274fb186158SSrinivas Pandruvada 		struct cpu_topology cpu_top;
275fb186158SSrinivas Pandruvada 
276fb186158SSrinivas Pandruvada 		cpu_top.core_id = parse_int_file(0,
277fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/core_id", i);
278fb186158SSrinivas Pandruvada 		if (cpu_top.core_id < 0)
279fb186158SSrinivas Pandruvada 			cpu_top.core_id = -1;
280fb186158SSrinivas Pandruvada 
281fb186158SSrinivas Pandruvada 		cpu_top.pkg_id = parse_int_file(0,
282fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
283fb186158SSrinivas Pandruvada 		if (cpu_top.pkg_id < 0)
284fb186158SSrinivas Pandruvada 			cpu_top.pkg_id = -1;
285fb186158SSrinivas Pandruvada 
286fb186158SSrinivas Pandruvada 		cpu_top.die_id = parse_int_file(0,
287fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/die_id", i);
288fb186158SSrinivas Pandruvada 		if (cpu_top.die_id < 0)
289fb186158SSrinivas Pandruvada 			cpu_top.die_id = -1;
290fb186158SSrinivas Pandruvada 
291fb186158SSrinivas Pandruvada 		cpu_top.cpu = i;
292fb186158SSrinivas Pandruvada 
293fb186158SSrinivas Pandruvada 		if (fwrite(&cpu_top, sizeof(cpu_top), 1, fp) != 1) {
294fb186158SSrinivas Pandruvada 			fprintf(stderr, "Can't write to:%s\n", pathname);
295fb186158SSrinivas Pandruvada 			break;
296fb186158SSrinivas Pandruvada 		}
297fb186158SSrinivas Pandruvada 	}
298fb186158SSrinivas Pandruvada 
299fb186158SSrinivas Pandruvada 	fclose(fp);
300fb186158SSrinivas Pandruvada }
301fb186158SSrinivas Pandruvada 
302e616059eSZhang Rui static int get_physical_package_id(int cpu)
3033fb4f7cdSSrinivas Pandruvada {
304fb186158SSrinivas Pandruvada 	int ret;
305fb186158SSrinivas Pandruvada 
306*ca56725dSZhang Rui 	if (cpu < 0)
307*ca56725dSZhang Rui 		return -1;
308*ca56725dSZhang Rui 
309*ca56725dSZhang Rui 	if (cpu_map && cpu_map[cpu].initialized)
310*ca56725dSZhang Rui 		return cpu_map[cpu].pkg_id;
311*ca56725dSZhang Rui 
312fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
313fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
3143fb4f7cdSSrinivas Pandruvada 			cpu);
315fb186158SSrinivas Pandruvada 	if (ret < 0) {
316fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
317fb186158SSrinivas Pandruvada 
318fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
319fb186158SSrinivas Pandruvada 		if (!ret)
320fb186158SSrinivas Pandruvada 			return pkg_id;
321fb186158SSrinivas Pandruvada 	}
322fb186158SSrinivas Pandruvada 
323fb186158SSrinivas Pandruvada 	return ret;
3243fb4f7cdSSrinivas Pandruvada }
3253fb4f7cdSSrinivas Pandruvada 
326e616059eSZhang Rui static int get_physical_core_id(int cpu)
3273fb4f7cdSSrinivas Pandruvada {
328fb186158SSrinivas Pandruvada 	int ret;
329fb186158SSrinivas Pandruvada 
330*ca56725dSZhang Rui 	if (cpu < 0)
331*ca56725dSZhang Rui 		return -1;
332*ca56725dSZhang Rui 
333*ca56725dSZhang Rui 	if (cpu_map && cpu_map[cpu].initialized)
334*ca56725dSZhang Rui 		return cpu_map[cpu].core_id;
335*ca56725dSZhang Rui 
336fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
337fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/core_id",
338fb186158SSrinivas Pandruvada 			cpu);
339fb186158SSrinivas Pandruvada 	if (ret < 0) {
340fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
341fb186158SSrinivas Pandruvada 
342fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
343fb186158SSrinivas Pandruvada 		if (!ret)
344fb186158SSrinivas Pandruvada 			return core_id;
345fb186158SSrinivas Pandruvada 	}
346fb186158SSrinivas Pandruvada 
347fb186158SSrinivas Pandruvada 	return ret;
3483fb4f7cdSSrinivas Pandruvada }
3493fb4f7cdSSrinivas Pandruvada 
350e616059eSZhang Rui static int get_physical_die_id(int cpu)
3513fb4f7cdSSrinivas Pandruvada {
3523fb4f7cdSSrinivas Pandruvada 	int ret;
3533fb4f7cdSSrinivas Pandruvada 
354*ca56725dSZhang Rui 	if (cpu < 0)
355*ca56725dSZhang Rui 		return -1;
356*ca56725dSZhang Rui 
357*ca56725dSZhang Rui 	if (cpu_map && cpu_map[cpu].initialized)
358*ca56725dSZhang Rui 		return cpu_map[cpu].die_id;
359*ca56725dSZhang Rui 
360fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
361fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/die_id",
3623fb4f7cdSSrinivas Pandruvada 			cpu);
363fb186158SSrinivas Pandruvada 	if (ret < 0) {
364fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
365fb186158SSrinivas Pandruvada 
366fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
3676c483225SSrinivas Pandruvada 		if (!ret) {
3686c483225SSrinivas Pandruvada 			if (die_id < 0)
3696c483225SSrinivas Pandruvada 				die_id = 0;
3706c483225SSrinivas Pandruvada 
371fb186158SSrinivas Pandruvada 			return die_id;
372fb186158SSrinivas Pandruvada 		}
3736c483225SSrinivas Pandruvada 	}
374fb186158SSrinivas Pandruvada 
3753fb4f7cdSSrinivas Pandruvada 	if (ret < 0)
3763fb4f7cdSSrinivas Pandruvada 		ret = 0;
3773fb4f7cdSSrinivas Pandruvada 
3783fb4f7cdSSrinivas Pandruvada 	return ret;
3793fb4f7cdSSrinivas Pandruvada }
3803fb4f7cdSSrinivas Pandruvada 
381850337ecSZhang Rui void set_isst_id(struct isst_id *id, int cpu)
382850337ecSZhang Rui {
383850337ecSZhang Rui 	id->cpu = cpu;
3843ba6a275SZhang Rui 
38532d6ab45SZhang Rui 	id->pkg = get_physical_package_id(cpu);
3863ba6a275SZhang Rui 	if (id < 0 || id->pkg >= MAX_PACKAGE_COUNT)
3873ba6a275SZhang Rui 		id->pkg = -1;
3883ba6a275SZhang Rui 
38932d6ab45SZhang Rui 	id->die = get_physical_die_id(cpu);
3903ba6a275SZhang Rui 	if (id < 0 || id->die >= MAX_DIE_PER_PACKAGE)
3913ba6a275SZhang Rui 		id->die = -1;
392850337ecSZhang Rui }
393850337ecSZhang Rui 
39400bb07dbSZhang Rui int is_cpu_in_power_domain(int cpu, struct isst_id *id)
39500bb07dbSZhang Rui {
39600bb07dbSZhang Rui 	struct isst_id tid;
39700bb07dbSZhang Rui 
39800bb07dbSZhang Rui 	set_isst_id(&tid, cpu);
39900bb07dbSZhang Rui 
40000bb07dbSZhang Rui 	if (id->pkg == tid.pkg && id->die == tid.die)
40100bb07dbSZhang Rui 		return 1;
40200bb07dbSZhang Rui 
40300bb07dbSZhang Rui 	return 0;
40400bb07dbSZhang Rui }
40500bb07dbSZhang Rui 
4067af5a95bSSrinivas Pandruvada int get_cpufreq_base_freq(int cpu)
4077af5a95bSSrinivas Pandruvada {
4087af5a95bSSrinivas Pandruvada 	return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
4097af5a95bSSrinivas Pandruvada }
4107af5a95bSSrinivas Pandruvada 
4113fb4f7cdSSrinivas Pandruvada int get_topo_max_cpus(void)
4123fb4f7cdSSrinivas Pandruvada {
4133fb4f7cdSSrinivas Pandruvada 	return topo_max_cpus;
4143fb4f7cdSSrinivas Pandruvada }
4153fb4f7cdSSrinivas Pandruvada 
4167fd786dfSSrinivas Pandruvada void set_cpu_online_offline(int cpu, int state)
4173c64c81aSSrinivas Pandruvada {
4183c64c81aSSrinivas Pandruvada 	char buffer[128];
419abd120e3SSrinivas Pandruvada 	int fd, ret;
4203c64c81aSSrinivas Pandruvada 
4213c64c81aSSrinivas Pandruvada 	snprintf(buffer, sizeof(buffer),
4223c64c81aSSrinivas Pandruvada 		 "/sys/devices/system/cpu/cpu%d/online", cpu);
4233c64c81aSSrinivas Pandruvada 
4243c64c81aSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
42569669198SSrinivas Pandruvada 	if (fd < 0) {
42669669198SSrinivas Pandruvada 		if (!cpu && state) {
42769669198SSrinivas Pandruvada 			fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
42869669198SSrinivas Pandruvada 			fprintf(stderr, "Ignoring online request for CPU 0 as this is already online\n");
42969669198SSrinivas Pandruvada 			return;
43069669198SSrinivas Pandruvada 		}
4313c64c81aSSrinivas Pandruvada 		err(-1, "%s open failed", buffer);
43269669198SSrinivas Pandruvada 	}
4333c64c81aSSrinivas Pandruvada 
4343c64c81aSSrinivas Pandruvada 	if (state)
435abd120e3SSrinivas Pandruvada 		ret = write(fd, "1\n", 2);
4363c64c81aSSrinivas Pandruvada 	else
437abd120e3SSrinivas Pandruvada 		ret = write(fd, "0\n", 2);
438abd120e3SSrinivas Pandruvada 
439abd120e3SSrinivas Pandruvada 	if (ret == -1)
440abd120e3SSrinivas Pandruvada 		perror("Online/Offline: Operation failed\n");
4413c64c81aSSrinivas Pandruvada 
4423c64c81aSSrinivas Pandruvada 	close(fd);
4433c64c81aSSrinivas Pandruvada }
4443c64c81aSSrinivas Pandruvada 
4450d3dfd75SSrinivas Pandruvada static void force_all_cpus_online(void)
4460d3dfd75SSrinivas Pandruvada {
4470d3dfd75SSrinivas Pandruvada 	int i;
4480d3dfd75SSrinivas Pandruvada 
4490d3dfd75SSrinivas Pandruvada 	fprintf(stderr, "Forcing all CPUs online\n");
4500d3dfd75SSrinivas Pandruvada 
4510d3dfd75SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i)
4520d3dfd75SSrinivas Pandruvada 		set_cpu_online_offline(i, 1);
4530d3dfd75SSrinivas Pandruvada 
4540d3dfd75SSrinivas Pandruvada 	unlink("/var/run/isst_cpu_topology.dat");
4550d3dfd75SSrinivas Pandruvada }
4560d3dfd75SSrinivas Pandruvada 
457850337ecSZhang Rui void for_each_online_package_in_set(void (*callback)(struct isst_id *, void *, void *,
4583fb4f7cdSSrinivas Pandruvada 						     void *, void *),
4593fb4f7cdSSrinivas Pandruvada 				    void *arg1, void *arg2, void *arg3,
4603fb4f7cdSSrinivas Pandruvada 				    void *arg4)
4613fb4f7cdSSrinivas Pandruvada {
4623fb4f7cdSSrinivas Pandruvada 	int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
4633fb4f7cdSSrinivas Pandruvada 	int pkg_index = 0, i;
464850337ecSZhang Rui 	struct isst_id id;
4653fb4f7cdSSrinivas Pandruvada 
4663fb4f7cdSSrinivas Pandruvada 	memset(max_packages, 0xff, sizeof(max_packages));
4673fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
4683fb4f7cdSSrinivas Pandruvada 		int j, online, pkg_id, die_id = 0, skip = 0;
4693fb4f7cdSSrinivas Pandruvada 
4703fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
4713fb4f7cdSSrinivas Pandruvada 			continue;
4723fb4f7cdSSrinivas Pandruvada 		if (i)
4733fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
4743fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
4753fb4f7cdSSrinivas Pandruvada 		else
4763fb4f7cdSSrinivas Pandruvada 			online =
4773fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
4783fb4f7cdSSrinivas Pandruvada 
4793fb4f7cdSSrinivas Pandruvada 		die_id = get_physical_die_id(i);
4803fb4f7cdSSrinivas Pandruvada 		if (die_id < 0)
4813fb4f7cdSSrinivas Pandruvada 			die_id = 0;
482fb186158SSrinivas Pandruvada 
483fb186158SSrinivas Pandruvada 		pkg_id = parse_int_file(0,
484fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
485fb186158SSrinivas Pandruvada 		if (pkg_id < 0)
486f0e0b4d1SSrinivas Pandruvada 			continue;
487fb186158SSrinivas Pandruvada 
4883fb4f7cdSSrinivas Pandruvada 		/* Create an unique id for package, die combination to store */
4893fb4f7cdSSrinivas Pandruvada 		pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
4903fb4f7cdSSrinivas Pandruvada 
4913fb4f7cdSSrinivas Pandruvada 		for (j = 0; j < pkg_index; ++j) {
4923fb4f7cdSSrinivas Pandruvada 			if (max_packages[j] == pkg_id) {
4933fb4f7cdSSrinivas Pandruvada 				skip = 1;
4943fb4f7cdSSrinivas Pandruvada 				break;
4953fb4f7cdSSrinivas Pandruvada 			}
4963fb4f7cdSSrinivas Pandruvada 		}
4973fb4f7cdSSrinivas Pandruvada 
498850337ecSZhang Rui 		set_isst_id(&id, i);
4993fb4f7cdSSrinivas Pandruvada 		if (!skip && online && callback) {
500850337ecSZhang Rui 			callback(&id, arg1, arg2, arg3, arg4);
5013fb4f7cdSSrinivas Pandruvada 			max_packages[pkg_index++] = pkg_id;
5023fb4f7cdSSrinivas Pandruvada 		}
5033fb4f7cdSSrinivas Pandruvada 	}
5043fb4f7cdSSrinivas Pandruvada }
5053fb4f7cdSSrinivas Pandruvada 
5063fb4f7cdSSrinivas Pandruvada static void for_each_online_target_cpu_in_set(
507850337ecSZhang Rui 	void (*callback)(struct isst_id *, void *, void *, void *, void *), void *arg1,
5083fb4f7cdSSrinivas Pandruvada 	void *arg2, void *arg3, void *arg4)
5093fb4f7cdSSrinivas Pandruvada {
510070fdea1SSrinivas Pandruvada 	int i, found = 0;
511850337ecSZhang Rui 	struct isst_id id;
5123fb4f7cdSSrinivas Pandruvada 
5133fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
5143fb4f7cdSSrinivas Pandruvada 		int online;
5153fb4f7cdSSrinivas Pandruvada 
5163fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
5173fb4f7cdSSrinivas Pandruvada 			continue;
5183fb4f7cdSSrinivas Pandruvada 		if (i)
5193fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
5203fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
5213fb4f7cdSSrinivas Pandruvada 		else
5223fb4f7cdSSrinivas Pandruvada 			online =
5233fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
5243fb4f7cdSSrinivas Pandruvada 
525850337ecSZhang Rui 		set_isst_id(&id, i);
526070fdea1SSrinivas Pandruvada 		if (online && callback) {
527850337ecSZhang Rui 			callback(&id, arg1, arg2, arg3, arg4);
528070fdea1SSrinivas Pandruvada 			found = 1;
5293fb4f7cdSSrinivas Pandruvada 		}
5303fb4f7cdSSrinivas Pandruvada 	}
5313fb4f7cdSSrinivas Pandruvada 
532070fdea1SSrinivas Pandruvada 	if (!found)
533070fdea1SSrinivas Pandruvada 		fprintf(stderr, "No valid CPU in the list\n");
534070fdea1SSrinivas Pandruvada }
535070fdea1SSrinivas Pandruvada 
5363fb4f7cdSSrinivas Pandruvada #define BITMASK_SIZE 32
5373fb4f7cdSSrinivas Pandruvada static void set_max_cpu_num(void)
5383fb4f7cdSSrinivas Pandruvada {
5393fb4f7cdSSrinivas Pandruvada 	FILE *filep;
5403fb4f7cdSSrinivas Pandruvada 	unsigned long dummy;
541864dc09eSSrinivas Pandruvada 	int i;
5423fb4f7cdSSrinivas Pandruvada 
5433fb4f7cdSSrinivas Pandruvada 	topo_max_cpus = 0;
544864dc09eSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
545864dc09eSSrinivas Pandruvada 		char path[256];
546864dc09eSSrinivas Pandruvada 
547864dc09eSSrinivas Pandruvada 		snprintf(path, sizeof(path),
548864dc09eSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
549864dc09eSSrinivas Pandruvada 		filep = fopen(path, "r");
550864dc09eSSrinivas Pandruvada 		if (filep)
551864dc09eSSrinivas Pandruvada 			break;
552864dc09eSSrinivas Pandruvada 	}
553864dc09eSSrinivas Pandruvada 
554864dc09eSSrinivas Pandruvada 	if (!filep) {
555864dc09eSSrinivas Pandruvada 		fprintf(stderr, "Can't get max cpu number\n");
556864dc09eSSrinivas Pandruvada 		exit(0);
557864dc09eSSrinivas Pandruvada 	}
558864dc09eSSrinivas Pandruvada 
5593fb4f7cdSSrinivas Pandruvada 	while (fscanf(filep, "%lx,", &dummy) == 1)
5603fb4f7cdSSrinivas Pandruvada 		topo_max_cpus += BITMASK_SIZE;
5613fb4f7cdSSrinivas Pandruvada 	fclose(filep);
5623fb4f7cdSSrinivas Pandruvada 
5633fb4f7cdSSrinivas Pandruvada 	debug_printf("max cpus %d\n", topo_max_cpus);
5643fb4f7cdSSrinivas Pandruvada }
5653fb4f7cdSSrinivas Pandruvada 
5663fb4f7cdSSrinivas Pandruvada size_t alloc_cpu_set(cpu_set_t **cpu_set)
5673fb4f7cdSSrinivas Pandruvada {
5683fb4f7cdSSrinivas Pandruvada 	cpu_set_t *_cpu_set;
5693fb4f7cdSSrinivas Pandruvada 	size_t size;
5703fb4f7cdSSrinivas Pandruvada 
5713fb4f7cdSSrinivas Pandruvada 	_cpu_set = CPU_ALLOC((topo_max_cpus + 1));
5723fb4f7cdSSrinivas Pandruvada 	if (_cpu_set == NULL)
5733fb4f7cdSSrinivas Pandruvada 		err(3, "CPU_ALLOC");
5743fb4f7cdSSrinivas Pandruvada 	size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
5753fb4f7cdSSrinivas Pandruvada 	CPU_ZERO_S(size, _cpu_set);
5763fb4f7cdSSrinivas Pandruvada 
5773fb4f7cdSSrinivas Pandruvada 	*cpu_set = _cpu_set;
5783fb4f7cdSSrinivas Pandruvada 	return size;
5793fb4f7cdSSrinivas Pandruvada }
5803fb4f7cdSSrinivas Pandruvada 
5813fb4f7cdSSrinivas Pandruvada void free_cpu_set(cpu_set_t *cpu_set)
5823fb4f7cdSSrinivas Pandruvada {
5833fb4f7cdSSrinivas Pandruvada 	CPU_FREE(cpu_set);
5843fb4f7cdSSrinivas Pandruvada }
5853fb4f7cdSSrinivas Pandruvada 
5863fb4f7cdSSrinivas Pandruvada static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
5873fb4f7cdSSrinivas Pandruvada static void set_cpu_present_cpu_mask(void)
5883fb4f7cdSSrinivas Pandruvada {
5893fb4f7cdSSrinivas Pandruvada 	size_t size;
5903fb4f7cdSSrinivas Pandruvada 	DIR *dir;
5913fb4f7cdSSrinivas Pandruvada 	int i;
5923fb4f7cdSSrinivas Pandruvada 
5933fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&present_cpumask);
5943fb4f7cdSSrinivas Pandruvada 	present_cpumask_size = size;
5953fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
5963fb4f7cdSSrinivas Pandruvada 		char buffer[256];
5973fb4f7cdSSrinivas Pandruvada 
5983fb4f7cdSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
5993fb4f7cdSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d", i);
6003fb4f7cdSSrinivas Pandruvada 		dir = opendir(buffer);
6013fb4f7cdSSrinivas Pandruvada 		if (dir) {
6023fb4f7cdSSrinivas Pandruvada 			int pkg_id, die_id;
6033fb4f7cdSSrinivas Pandruvada 
6043fb4f7cdSSrinivas Pandruvada 			CPU_SET_S(i, size, present_cpumask);
6053fb4f7cdSSrinivas Pandruvada 			die_id = get_physical_die_id(i);
6063fb4f7cdSSrinivas Pandruvada 			if (die_id < 0)
6073fb4f7cdSSrinivas Pandruvada 				die_id = 0;
6083fb4f7cdSSrinivas Pandruvada 
6093fb4f7cdSSrinivas Pandruvada 			pkg_id = get_physical_package_id(i);
610f0e0b4d1SSrinivas Pandruvada 			if (pkg_id < 0) {
611f0e0b4d1SSrinivas Pandruvada 				fprintf(stderr, "Failed to get package id, CPU %d may be offline\n", i);
612f0e0b4d1SSrinivas Pandruvada 				continue;
613f0e0b4d1SSrinivas Pandruvada 			}
6143fb4f7cdSSrinivas Pandruvada 			if (pkg_id < MAX_PACKAGE_COUNT &&
615e2783369SZhang Rui 			    die_id < MAX_DIE_PER_PACKAGE)
6163fb4f7cdSSrinivas Pandruvada 				cpu_cnt[pkg_id][die_id]++;
6173fb4f7cdSSrinivas Pandruvada 		}
6183fb4f7cdSSrinivas Pandruvada 		closedir(dir);
6193fb4f7cdSSrinivas Pandruvada 	}
6203fb4f7cdSSrinivas Pandruvada }
6213fb4f7cdSSrinivas Pandruvada 
62230e0600eSZhang Rui int get_max_punit_core_id(struct isst_id *id)
623de7f9d3dSSrinivas Pandruvada {
6247566616fSJonathan Doman 	int max_id = 0;
625de7f9d3dSSrinivas Pandruvada 	int i;
626de7f9d3dSSrinivas Pandruvada 
6277566616fSJonathan Doman 	for (i = 0; i < topo_max_cpus; ++i)
6287566616fSJonathan Doman 	{
6297566616fSJonathan Doman 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
6307566616fSJonathan Doman 			continue;
6317566616fSJonathan Doman 
63200bb07dbSZhang Rui 		if (is_cpu_in_power_domain(i, id) &&
6337566616fSJonathan Doman 		    cpu_map[i].punit_cpu_core > max_id)
6347566616fSJonathan Doman 			max_id = cpu_map[i].punit_cpu_core;
635de7f9d3dSSrinivas Pandruvada 	}
636de7f9d3dSSrinivas Pandruvada 
6377566616fSJonathan Doman 	return max_id;
638de7f9d3dSSrinivas Pandruvada }
639de7f9d3dSSrinivas Pandruvada 
64030e0600eSZhang Rui int get_cpu_count(struct isst_id *id)
6413fb4f7cdSSrinivas Pandruvada {
6423ba6a275SZhang Rui 	if (id->pkg < 0 || id->die < 0)
6433fb4f7cdSSrinivas Pandruvada 		return 0;
6443ba6a275SZhang Rui 
6453ba6a275SZhang Rui 	return cpu_cnt[id->pkg][id->die];
6463fb4f7cdSSrinivas Pandruvada }
6473fb4f7cdSSrinivas Pandruvada 
6483fb4f7cdSSrinivas Pandruvada static void set_cpu_target_cpu_mask(void)
6493fb4f7cdSSrinivas Pandruvada {
6503fb4f7cdSSrinivas Pandruvada 	size_t size;
6513fb4f7cdSSrinivas Pandruvada 	int i;
6523fb4f7cdSSrinivas Pandruvada 
6533fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&target_cpumask);
6543fb4f7cdSSrinivas Pandruvada 	target_cpumask_size = size;
6553fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < max_target_cpus; ++i) {
6563fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
6573fb4f7cdSSrinivas Pandruvada 				 present_cpumask))
6583fb4f7cdSSrinivas Pandruvada 			continue;
6593fb4f7cdSSrinivas Pandruvada 
6603fb4f7cdSSrinivas Pandruvada 		CPU_SET_S(target_cpus[i], size, target_cpumask);
6613fb4f7cdSSrinivas Pandruvada 	}
6623fb4f7cdSSrinivas Pandruvada }
6633fb4f7cdSSrinivas Pandruvada 
6643fb4f7cdSSrinivas Pandruvada static void create_cpu_map(void)
6653fb4f7cdSSrinivas Pandruvada {
6663fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
6673fb4f7cdSSrinivas Pandruvada 	int i, fd = 0;
6683fb4f7cdSSrinivas Pandruvada 	struct isst_if_cpu_maps map;
6693fb4f7cdSSrinivas Pandruvada 
670*ca56725dSZhang Rui 	/* Use calloc to make sure the memory is initialized to Zero */
671*ca56725dSZhang Rui 	cpu_map = calloc(topo_max_cpus, sizeof(*cpu_map));
6723fb4f7cdSSrinivas Pandruvada 	if (!cpu_map)
6733fb4f7cdSSrinivas Pandruvada 		err(3, "cpumap");
6743fb4f7cdSSrinivas Pandruvada 
6753fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
676*ca56725dSZhang Rui 	if (fd < 0 && !is_clx_n_platform())
6773fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
6783fb4f7cdSSrinivas Pandruvada 
6793fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
6803fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
6813fb4f7cdSSrinivas Pandruvada 			continue;
6823fb4f7cdSSrinivas Pandruvada 
683*ca56725dSZhang Rui 		cpu_map[i].core_id = get_physical_core_id(i);
684*ca56725dSZhang Rui 		cpu_map[i].pkg_id = get_physical_package_id(i);
685*ca56725dSZhang Rui 		cpu_map[i].die_id = get_physical_die_id(i);
686*ca56725dSZhang Rui 		cpu_map[i].initialized = 1;
687*ca56725dSZhang Rui 
688*ca56725dSZhang Rui 		if (fd < 0)
689*ca56725dSZhang Rui 			continue;
6903fb4f7cdSSrinivas Pandruvada 		map.cmd_count = 1;
6913fb4f7cdSSrinivas Pandruvada 		map.cpu_map[0].logical_cpu = i;
6923fb4f7cdSSrinivas Pandruvada 		debug_printf(" map logical_cpu:%d\n",
6933fb4f7cdSSrinivas Pandruvada 			     map.cpu_map[0].logical_cpu);
6943fb4f7cdSSrinivas Pandruvada 		if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
6953fb4f7cdSSrinivas Pandruvada 			perror("ISST_IF_GET_PHY_ID");
6963fb4f7cdSSrinivas Pandruvada 			fprintf(outf, "Error: map logical_cpu:%d\n",
6973fb4f7cdSSrinivas Pandruvada 				map.cpu_map[0].logical_cpu);
6983fb4f7cdSSrinivas Pandruvada 			continue;
6993fb4f7cdSSrinivas Pandruvada 		}
7003fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
7013fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
7023fb4f7cdSSrinivas Pandruvada 					     1); // shift to get core id
7033fb4f7cdSSrinivas Pandruvada 
7043fb4f7cdSSrinivas Pandruvada 		debug_printf(
7053fb4f7cdSSrinivas Pandruvada 			"map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
7063fb4f7cdSSrinivas Pandruvada 			i, cpu_map[i].core_id, cpu_map[i].die_id,
7073fb4f7cdSSrinivas Pandruvada 			cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
7083fb4f7cdSSrinivas Pandruvada 			cpu_map[i].punit_cpu_core);
7093fb4f7cdSSrinivas Pandruvada 	}
7103fb4f7cdSSrinivas Pandruvada 
711*ca56725dSZhang Rui 	if (fd >= 0)
7123fb4f7cdSSrinivas Pandruvada 		close(fd);
7133fb4f7cdSSrinivas Pandruvada }
7143fb4f7cdSSrinivas Pandruvada 
715850337ecSZhang Rui void set_cpu_mask_from_punit_coremask(struct isst_id *id, unsigned long long core_mask,
7163fb4f7cdSSrinivas Pandruvada 				      size_t core_cpumask_size,
7173fb4f7cdSSrinivas Pandruvada 				      cpu_set_t *core_cpumask, int *cpu_cnt)
7183fb4f7cdSSrinivas Pandruvada {
7193fb4f7cdSSrinivas Pandruvada 	int i, cnt = 0;
7203fb4f7cdSSrinivas Pandruvada 
7213fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = 0;
7223fb4f7cdSSrinivas Pandruvada 
7233fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < 64; ++i) {
724873e391fSSrinivas Pandruvada 		if (core_mask & BIT_ULL(i)) {
7253fb4f7cdSSrinivas Pandruvada 			int j;
7263fb4f7cdSSrinivas Pandruvada 
7273fb4f7cdSSrinivas Pandruvada 			for (j = 0; j < topo_max_cpus; ++j) {
72844460efeSYouquan Song 				if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
72944460efeSYouquan Song 					continue;
73044460efeSYouquan Song 
73100bb07dbSZhang Rui 				if (is_cpu_in_power_domain(j, id) &&
7323fb4f7cdSSrinivas Pandruvada 				    cpu_map[j].punit_cpu_core == i) {
7333fb4f7cdSSrinivas Pandruvada 					CPU_SET_S(j, core_cpumask_size,
7343fb4f7cdSSrinivas Pandruvada 						  core_cpumask);
7353fb4f7cdSSrinivas Pandruvada 					++cnt;
7363fb4f7cdSSrinivas Pandruvada 				}
7373fb4f7cdSSrinivas Pandruvada 			}
7383fb4f7cdSSrinivas Pandruvada 		}
7393fb4f7cdSSrinivas Pandruvada 	}
7403fb4f7cdSSrinivas Pandruvada 
7413fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = cnt;
7423fb4f7cdSSrinivas Pandruvada }
7433fb4f7cdSSrinivas Pandruvada 
7443fb4f7cdSSrinivas Pandruvada int find_phy_core_num(int logical_cpu)
7453fb4f7cdSSrinivas Pandruvada {
7463fb4f7cdSSrinivas Pandruvada 	if (logical_cpu < topo_max_cpus)
7473fb4f7cdSSrinivas Pandruvada 		return cpu_map[logical_cpu].punit_cpu_core;
7483fb4f7cdSSrinivas Pandruvada 
7493fb4f7cdSSrinivas Pandruvada 	return -EINVAL;
7503fb4f7cdSSrinivas Pandruvada }
7513fb4f7cdSSrinivas Pandruvada 
7523fb4f7cdSSrinivas Pandruvada static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
7533fb4f7cdSSrinivas Pandruvada 				  unsigned int *value)
7543fb4f7cdSSrinivas Pandruvada {
7553fb4f7cdSSrinivas Pandruvada 	struct isst_if_io_regs io_regs;
7563fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
7573fb4f7cdSSrinivas Pandruvada 	int cmd;
7583fb4f7cdSSrinivas Pandruvada 	int fd;
7593fb4f7cdSSrinivas Pandruvada 
7603fb4f7cdSSrinivas Pandruvada 	debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
7613fb4f7cdSSrinivas Pandruvada 
7623fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
7633fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
7643fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
7653fb4f7cdSSrinivas Pandruvada 
7663fb4f7cdSSrinivas Pandruvada 	io_regs.req_count = 1;
7673fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].logical_cpu = cpu;
7683fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].reg = reg;
7693fb4f7cdSSrinivas Pandruvada 	cmd = ISST_IF_IO_CMD;
7703fb4f7cdSSrinivas Pandruvada 	if (write) {
7713fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 1;
7723fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].value = *value;
7733fb4f7cdSSrinivas Pandruvada 	} else {
7743fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 0;
7753fb4f7cdSSrinivas Pandruvada 	}
7763fb4f7cdSSrinivas Pandruvada 
7773fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, cmd, &io_regs) == -1) {
77895f8e569SSrinivas Pandruvada 		if (errno == ENOTTY) {
77995f8e569SSrinivas Pandruvada 			perror("ISST_IF_IO_COMMAND\n");
78095f8e569SSrinivas Pandruvada 			fprintf(stderr, "Check presence of kernel modules: isst_if_mmio\n");
78195f8e569SSrinivas Pandruvada 			exit(0);
78295f8e569SSrinivas Pandruvada 		}
7833fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
7843fb4f7cdSSrinivas Pandruvada 			cpu, reg, write);
7853fb4f7cdSSrinivas Pandruvada 	} else {
7863fb4f7cdSSrinivas Pandruvada 		if (!write)
7873fb4f7cdSSrinivas Pandruvada 			*value = io_regs.io_reg[0].value;
7883fb4f7cdSSrinivas Pandruvada 
7893fb4f7cdSSrinivas Pandruvada 		debug_printf(
7903fb4f7cdSSrinivas Pandruvada 			"mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
7913fb4f7cdSSrinivas Pandruvada 			cpu, reg, write, *value);
7923fb4f7cdSSrinivas Pandruvada 	}
7933fb4f7cdSSrinivas Pandruvada 
7943fb4f7cdSSrinivas Pandruvada 	close(fd);
7953fb4f7cdSSrinivas Pandruvada 
7963fb4f7cdSSrinivas Pandruvada 	return 0;
7973fb4f7cdSSrinivas Pandruvada }
7983fb4f7cdSSrinivas Pandruvada 
7993fb4f7cdSSrinivas Pandruvada int isst_send_mbox_command(unsigned int cpu, unsigned char command,
8003fb4f7cdSSrinivas Pandruvada 			   unsigned char sub_command, unsigned int parameter,
8013fb4f7cdSSrinivas Pandruvada 			   unsigned int req_data, unsigned int *resp)
8023fb4f7cdSSrinivas Pandruvada {
8033fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
80432279be7SSrinivas Pandruvada 	int fd, retry;
8053fb4f7cdSSrinivas Pandruvada 	struct isst_if_mbox_cmds mbox_cmds = { 0 };
8063fb4f7cdSSrinivas Pandruvada 
8073fb4f7cdSSrinivas Pandruvada 	debug_printf(
8083fb4f7cdSSrinivas Pandruvada 		"mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
8093fb4f7cdSSrinivas Pandruvada 		cpu, command, sub_command, parameter, req_data);
8103fb4f7cdSSrinivas Pandruvada 
81195f8e569SSrinivas Pandruvada 	if (!is_skx_based_platform() && command == CONFIG_CLOS &&
8128ddbda76SSrinivas Pandruvada 	    sub_command != CLOS_PM_QOS_CONFIG) {
8133fb4f7cdSSrinivas Pandruvada 		unsigned int value;
8143fb4f7cdSSrinivas Pandruvada 		int write = 0;
8153fb4f7cdSSrinivas Pandruvada 		int clos_id, core_id, ret = 0;
8163fb4f7cdSSrinivas Pandruvada 
817d2d1f304SSrinivas Pandruvada 		debug_printf("CPU %d\n", cpu);
8183fb4f7cdSSrinivas Pandruvada 
8193fb4f7cdSSrinivas Pandruvada 		if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
8203fb4f7cdSSrinivas Pandruvada 			value = req_data;
8213fb4f7cdSSrinivas Pandruvada 			write = 1;
8223fb4f7cdSSrinivas Pandruvada 		}
8233fb4f7cdSSrinivas Pandruvada 
8243fb4f7cdSSrinivas Pandruvada 		switch (sub_command) {
8253fb4f7cdSSrinivas Pandruvada 		case CLOS_PQR_ASSOC:
8263fb4f7cdSSrinivas Pandruvada 			core_id = parameter & 0xff;
8273fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
8283fb4f7cdSSrinivas Pandruvada 				cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
8293fb4f7cdSSrinivas Pandruvada 				&value);
8303fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
8313fb4f7cdSSrinivas Pandruvada 				*resp = value;
8323fb4f7cdSSrinivas Pandruvada 			break;
8333fb4f7cdSSrinivas Pandruvada 		case CLOS_PM_CLOS:
8343fb4f7cdSSrinivas Pandruvada 			clos_id = parameter & 0x03;
8353fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
8363fb4f7cdSSrinivas Pandruvada 				cpu, PM_CLOS_OFFSET + clos_id * 4, write,
8373fb4f7cdSSrinivas Pandruvada 				&value);
8383fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
8393fb4f7cdSSrinivas Pandruvada 				*resp = value;
8403fb4f7cdSSrinivas Pandruvada 			break;
8413fb4f7cdSSrinivas Pandruvada 		case CLOS_STATUS:
8423fb4f7cdSSrinivas Pandruvada 			break;
8433fb4f7cdSSrinivas Pandruvada 		default:
8443fb4f7cdSSrinivas Pandruvada 			break;
8453fb4f7cdSSrinivas Pandruvada 		}
8463fb4f7cdSSrinivas Pandruvada 		return ret;
8473fb4f7cdSSrinivas Pandruvada 	}
8483fb4f7cdSSrinivas Pandruvada 
8493fb4f7cdSSrinivas Pandruvada 	mbox_cmds.cmd_count = 1;
8503fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
8513fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].command = command;
8523fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].sub_command = sub_command;
8533fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].parameter = parameter;
8543fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].req_data = req_data;
8553fb4f7cdSSrinivas Pandruvada 
856a85a35fcSSrinivas Pandruvada 	if (mbox_delay)
857a85a35fcSSrinivas Pandruvada 		usleep(mbox_delay * 1000);
858a85a35fcSSrinivas Pandruvada 
8593fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8603fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8613fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
8623fb4f7cdSSrinivas Pandruvada 
86332279be7SSrinivas Pandruvada 	retry = mbox_retries;
86432279be7SSrinivas Pandruvada 
86532279be7SSrinivas Pandruvada 	do {
8663fb4f7cdSSrinivas Pandruvada 		if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
86795f8e569SSrinivas Pandruvada 			if (errno == ENOTTY) {
86895f8e569SSrinivas Pandruvada 				perror("ISST_IF_MBOX_COMMAND\n");
86995f8e569SSrinivas Pandruvada 				fprintf(stderr, "Check presence of kernel modules: isst_if_mbox_pci or isst_if_mbox_msr\n");
87095f8e569SSrinivas Pandruvada 				exit(0);
87195f8e569SSrinivas Pandruvada 			}
87295f8e569SSrinivas Pandruvada 			debug_printf(
87395f8e569SSrinivas Pandruvada 				"Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x errorno:%d\n",
87495f8e569SSrinivas Pandruvada 				cpu, command, sub_command, parameter, req_data, errno);
87532279be7SSrinivas Pandruvada 			--retry;
8763fb4f7cdSSrinivas Pandruvada 		} else {
8773fb4f7cdSSrinivas Pandruvada 			*resp = mbox_cmds.mbox_cmd[0].resp_data;
8783fb4f7cdSSrinivas Pandruvada 			debug_printf(
8793fb4f7cdSSrinivas Pandruvada 				"mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
8803fb4f7cdSSrinivas Pandruvada 				cpu, command, sub_command, parameter, req_data, *resp);
88132279be7SSrinivas Pandruvada 			break;
8823fb4f7cdSSrinivas Pandruvada 		}
88332279be7SSrinivas Pandruvada 	} while (retry);
8843fb4f7cdSSrinivas Pandruvada 
8853fb4f7cdSSrinivas Pandruvada 	close(fd);
8863fb4f7cdSSrinivas Pandruvada 
88732279be7SSrinivas Pandruvada 	if (!retry) {
88832279be7SSrinivas Pandruvada 		debug_printf("Failed mbox command even after retries\n");
88932279be7SSrinivas Pandruvada 		return -1;
89032279be7SSrinivas Pandruvada 
89132279be7SSrinivas Pandruvada 	}
8923fb4f7cdSSrinivas Pandruvada 	return 0;
8933fb4f7cdSSrinivas Pandruvada }
8943fb4f7cdSSrinivas Pandruvada 
8953fb4f7cdSSrinivas Pandruvada int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
8963fb4f7cdSSrinivas Pandruvada 			  unsigned long long *req_resp)
8973fb4f7cdSSrinivas Pandruvada {
8983fb4f7cdSSrinivas Pandruvada 	struct isst_if_msr_cmds msr_cmds;
8993fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
9003fb4f7cdSSrinivas Pandruvada 	int fd;
9013fb4f7cdSSrinivas Pandruvada 
9023fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
9033fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
9043fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
9053fb4f7cdSSrinivas Pandruvada 
9063fb4f7cdSSrinivas Pandruvada 	msr_cmds.cmd_count = 1;
9073fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].logical_cpu = cpu;
9083fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].msr = msr;
9093fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].read_write = write;
9103fb4f7cdSSrinivas Pandruvada 	if (write)
9113fb4f7cdSSrinivas Pandruvada 		msr_cmds.msr_cmd[0].data = *req_resp;
9123fb4f7cdSSrinivas Pandruvada 
9133fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
9149945a247SMasanari Iida 		perror("ISST_IF_MSR_COMMAND");
9153fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
9163fb4f7cdSSrinivas Pandruvada 			cpu, msr, write);
9173fb4f7cdSSrinivas Pandruvada 	} else {
9183fb4f7cdSSrinivas Pandruvada 		if (!write)
9193fb4f7cdSSrinivas Pandruvada 			*req_resp = msr_cmds.msr_cmd[0].data;
9203fb4f7cdSSrinivas Pandruvada 
9213fb4f7cdSSrinivas Pandruvada 		debug_printf(
9223fb4f7cdSSrinivas Pandruvada 			"msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
9233fb4f7cdSSrinivas Pandruvada 			cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
9243fb4f7cdSSrinivas Pandruvada 	}
9253fb4f7cdSSrinivas Pandruvada 
9263fb4f7cdSSrinivas Pandruvada 	close(fd);
9273fb4f7cdSSrinivas Pandruvada 
9283fb4f7cdSSrinivas Pandruvada 	return 0;
9293fb4f7cdSSrinivas Pandruvada }
9303fb4f7cdSSrinivas Pandruvada 
9313fb4f7cdSSrinivas Pandruvada static int isst_fill_platform_info(void)
9323fb4f7cdSSrinivas Pandruvada {
9333fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
9343fb4f7cdSSrinivas Pandruvada 	int fd;
9353fb4f7cdSSrinivas Pandruvada 
9363fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
9373fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
9383fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
9393fb4f7cdSSrinivas Pandruvada 
9403fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
9413fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
9423fb4f7cdSSrinivas Pandruvada 		close(fd);
9433fb4f7cdSSrinivas Pandruvada 		return -1;
9443fb4f7cdSSrinivas Pandruvada 	}
9453fb4f7cdSSrinivas Pandruvada 
9463fb4f7cdSSrinivas Pandruvada 	close(fd);
9473fb4f7cdSSrinivas Pandruvada 
9483bc3d30cSPrarit Bhargava 	if (isst_platform_info.api_version > supported_api_ver) {
9493bc3d30cSPrarit Bhargava 		printf("Incompatible API versions; Upgrade of tool is required\n");
9503bc3d30cSPrarit Bhargava 		return -1;
9513bc3d30cSPrarit Bhargava 	}
9523fb4f7cdSSrinivas Pandruvada 	return 0;
9533fb4f7cdSSrinivas Pandruvada }
9543fb4f7cdSSrinivas Pandruvada 
9551ba148aeSSrinivas Pandruvada static void isst_print_extended_platform_info(void)
9561ba148aeSSrinivas Pandruvada {
9571ba148aeSSrinivas Pandruvada 	int cp_state, cp_cap, fact_support = 0, pbf_support = 0;
9581ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
9591ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
9601ba148aeSSrinivas Pandruvada 	int ret, i, j;
9611ba148aeSSrinivas Pandruvada 	FILE *filep;
962850337ecSZhang Rui 	struct isst_id id;
9631ba148aeSSrinivas Pandruvada 
9641ba148aeSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
9651ba148aeSSrinivas Pandruvada 		char path[256];
9661ba148aeSSrinivas Pandruvada 
9671ba148aeSSrinivas Pandruvada 		snprintf(path, sizeof(path),
9681ba148aeSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
9691ba148aeSSrinivas Pandruvada 		filep = fopen(path, "r");
9701ba148aeSSrinivas Pandruvada 		if (filep)
9711ba148aeSSrinivas Pandruvada 			break;
9721ba148aeSSrinivas Pandruvada 	}
9731ba148aeSSrinivas Pandruvada 
9741ba148aeSSrinivas Pandruvada 	if (!filep)
9751ba148aeSSrinivas Pandruvada 		return;
9761ba148aeSSrinivas Pandruvada 
9771ba148aeSSrinivas Pandruvada 	fclose(filep);
9781ba148aeSSrinivas Pandruvada 
979850337ecSZhang Rui 	set_isst_id(&id, i);
980850337ecSZhang Rui 	ret = isst_get_ctdp_levels(&id, &pkg_dev);
9811ba148aeSSrinivas Pandruvada 	if (ret)
9821ba148aeSSrinivas Pandruvada 		return;
9831ba148aeSSrinivas Pandruvada 
9841ba148aeSSrinivas Pandruvada 	if (pkg_dev.enabled) {
9851ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
9861ba148aeSSrinivas Pandruvada 	} else {
9871ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
9881ba148aeSSrinivas Pandruvada 		fprintf(outf, "Only performance level 0 (base level) is present\n");
9891ba148aeSSrinivas Pandruvada 	}
9901ba148aeSSrinivas Pandruvada 
9911ba148aeSSrinivas Pandruvada 	if (pkg_dev.locked)
9921ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is locked\n");
9931ba148aeSSrinivas Pandruvada 	else
9941ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is unlocked, max level: %d \n", pkg_dev.levels);
9951ba148aeSSrinivas Pandruvada 
9961ba148aeSSrinivas Pandruvada 	for (j = 0; j <= pkg_dev.levels; ++j) {
997850337ecSZhang Rui 		ret = isst_get_ctdp_control(&id, j, &ctdp_level);
9981ba148aeSSrinivas Pandruvada 		if (ret)
9991ba148aeSSrinivas Pandruvada 			continue;
10001ba148aeSSrinivas Pandruvada 
10011ba148aeSSrinivas Pandruvada 		if (!fact_support && ctdp_level.fact_support)
10021ba148aeSSrinivas Pandruvada 			fact_support = 1;
10031ba148aeSSrinivas Pandruvada 
10041ba148aeSSrinivas Pandruvada 		if (!pbf_support && ctdp_level.pbf_support)
10051ba148aeSSrinivas Pandruvada 			pbf_support = 1;
10061ba148aeSSrinivas Pandruvada 	}
10071ba148aeSSrinivas Pandruvada 
10081ba148aeSSrinivas Pandruvada 	if (fact_support)
10091ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
10101ba148aeSSrinivas Pandruvada 	else
10111ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
10121ba148aeSSrinivas Pandruvada 
10131ba148aeSSrinivas Pandruvada 	if (pbf_support)
10141ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
10151ba148aeSSrinivas Pandruvada 	else
10161ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
10171ba148aeSSrinivas Pandruvada 
1018850337ecSZhang Rui 	ret = isst_read_pm_config(&id, &cp_state, &cp_cap);
1019b84733a1SSrinivas Pandruvada 	if (ret) {
1020b84733a1SSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) status is unknown\n");
1021b84733a1SSrinivas Pandruvada 		return;
1022b84733a1SSrinivas Pandruvada 	}
10231ba148aeSSrinivas Pandruvada 	if (cp_cap)
10241ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
10251ba148aeSSrinivas Pandruvada 	else
10261ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
10271ba148aeSSrinivas Pandruvada }
10281ba148aeSSrinivas Pandruvada 
10293fb4f7cdSSrinivas Pandruvada static void isst_print_platform_information(void)
10303fb4f7cdSSrinivas Pandruvada {
10313fb4f7cdSSrinivas Pandruvada 	struct isst_if_platform_info platform_info;
10323fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
10333fb4f7cdSSrinivas Pandruvada 	int fd;
10343fb4f7cdSSrinivas Pandruvada 
10351ba148aeSSrinivas Pandruvada 	if (is_clx_n_platform()) {
10361ba148aeSSrinivas Pandruvada 		fprintf(stderr, "\nThis option in not supported on this platform\n");
10371ba148aeSSrinivas Pandruvada 		exit(0);
10381ba148aeSSrinivas Pandruvada 	}
10391ba148aeSSrinivas Pandruvada 
1040*ca56725dSZhang Rui 	/* Early initialization to create working cpu_map */
1041*ca56725dSZhang Rui 	set_max_cpu_num();
1042*ca56725dSZhang Rui 	set_cpu_present_cpu_mask();
1043*ca56725dSZhang Rui 	create_cpu_map();
1044*ca56725dSZhang Rui 
10453fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
10463fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
10473fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
10483fb4f7cdSSrinivas Pandruvada 
10493fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
10503fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
10513fb4f7cdSSrinivas Pandruvada 	} else {
10523fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: API version : %d\n",
10533fb4f7cdSSrinivas Pandruvada 			platform_info.api_version);
10543fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: Driver version : %d\n",
10553fb4f7cdSSrinivas Pandruvada 			platform_info.driver_version);
10563fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mbox supported : %d\n",
10573fb4f7cdSSrinivas Pandruvada 			platform_info.mbox_supported);
10583fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mmio supported : %d\n",
10593fb4f7cdSSrinivas Pandruvada 			platform_info.mmio_supported);
10601ba148aeSSrinivas Pandruvada 		isst_print_extended_platform_info();
10613fb4f7cdSSrinivas Pandruvada 	}
10623fb4f7cdSSrinivas Pandruvada 
10633fb4f7cdSSrinivas Pandruvada 	close(fd);
10643fb4f7cdSSrinivas Pandruvada 
10653fb4f7cdSSrinivas Pandruvada 	exit(0);
10663fb4f7cdSSrinivas Pandruvada }
10673fb4f7cdSSrinivas Pandruvada 
10683d1a8579SSrinivas Pandruvada static char *local_str0, *local_str1;
1069850337ecSZhang Rui static void exec_on_get_ctdp_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
10703fb4f7cdSSrinivas Pandruvada 				 void *arg4)
10713fb4f7cdSSrinivas Pandruvada {
1072850337ecSZhang Rui 	int (*fn_ptr)(struct isst_id *id, void *arg);
10733fb4f7cdSSrinivas Pandruvada 	int ret;
10743fb4f7cdSSrinivas Pandruvada 
10753fb4f7cdSSrinivas Pandruvada 	fn_ptr = arg1;
1076850337ecSZhang Rui 	ret = fn_ptr(id, arg2);
10773fb4f7cdSSrinivas Pandruvada 	if (ret)
10783d1a8579SSrinivas Pandruvada 		isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
10793fb4f7cdSSrinivas Pandruvada 	else
1080850337ecSZhang Rui 		isst_ctdp_display_core_info(id, outf, arg3,
10813d1a8579SSrinivas Pandruvada 					    *(unsigned int *)arg4,
10823d1a8579SSrinivas Pandruvada 					    local_str0, local_str1);
10833fb4f7cdSSrinivas Pandruvada }
10843fb4f7cdSSrinivas Pandruvada 
10853d1a8579SSrinivas Pandruvada #define _get_tdp_level(desc, suffix, object, help, str0, str1)			\
1086ce1326a2SPrarit Bhargava 	static void get_tdp_##object(int arg)                                    \
10873fb4f7cdSSrinivas Pandruvada 	{                                                                         \
10883fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp ctdp;                                        \
10893fb4f7cdSSrinivas Pandruvada \
10903fb4f7cdSSrinivas Pandruvada 		if (cmd_help) {                                                   \
10913fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,                                           \
10923fb4f7cdSSrinivas Pandruvada 				"Print %s [No command arguments are required]\n", \
10933fb4f7cdSSrinivas Pandruvada 				help);                                            \
10943fb4f7cdSSrinivas Pandruvada 			exit(0);                                                  \
10953fb4f7cdSSrinivas Pandruvada 		}                                                                 \
10963d1a8579SSrinivas Pandruvada 		local_str0 = str0;						  \
10973d1a8579SSrinivas Pandruvada 		local_str1 = str1;						  \
10983fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_start(outf);                        \
10993fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus)                                              \
11003fb4f7cdSSrinivas Pandruvada 			for_each_online_target_cpu_in_set(                        \
11013fb4f7cdSSrinivas Pandruvada 				exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
11023fb4f7cdSSrinivas Pandruvada 				&ctdp, desc, &ctdp.object);                       \
11033fb4f7cdSSrinivas Pandruvada 		else                                                              \
11043fb4f7cdSSrinivas Pandruvada 			for_each_online_package_in_set(exec_on_get_ctdp_cpu,      \
11053fb4f7cdSSrinivas Pandruvada 						       isst_get_ctdp_##suffix,    \
11063fb4f7cdSSrinivas Pandruvada 						       &ctdp, desc,               \
11073fb4f7cdSSrinivas Pandruvada 						       &ctdp.object);             \
11083fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);                          \
11093fb4f7cdSSrinivas Pandruvada 	}
11103fb4f7cdSSrinivas Pandruvada 
11113d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
11123d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
11133d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
11143fb4f7cdSSrinivas Pandruvada _get_tdp_level("get-config-current_level", levels, current_level,
11153d1a8579SSrinivas Pandruvada 	       "Current TDP Level", NULL, NULL);
11163d1a8579SSrinivas Pandruvada _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
11173fb4f7cdSSrinivas Pandruvada 
1118062e4aacSPrarit Bhargava struct isst_pkg_ctdp clx_n_pkg_dev;
1119062e4aacSPrarit Bhargava 
1120062e4aacSPrarit Bhargava static int clx_n_get_base_ratio(void)
1121062e4aacSPrarit Bhargava {
1122062e4aacSPrarit Bhargava 	FILE *fp;
1123062e4aacSPrarit Bhargava 	char *begin, *end, *line = NULL;
1124062e4aacSPrarit Bhargava 	char number[5];
1125062e4aacSPrarit Bhargava 	float value = 0;
1126062e4aacSPrarit Bhargava 	size_t n = 0;
1127062e4aacSPrarit Bhargava 
1128062e4aacSPrarit Bhargava 	fp = fopen("/proc/cpuinfo", "r");
1129062e4aacSPrarit Bhargava 	if (!fp)
1130062e4aacSPrarit Bhargava 		err(-1, "cannot open /proc/cpuinfo\n");
1131062e4aacSPrarit Bhargava 
1132062e4aacSPrarit Bhargava 	while (getline(&line, &n, fp) > 0) {
1133062e4aacSPrarit Bhargava 		if (strstr(line, "model name")) {
1134062e4aacSPrarit Bhargava 			/* this is true for CascadeLake-N */
1135062e4aacSPrarit Bhargava 			begin = strstr(line, "@ ") + 2;
1136062e4aacSPrarit Bhargava 			end = strstr(line, "GHz");
1137062e4aacSPrarit Bhargava 			strncpy(number, begin, end - begin);
1138062e4aacSPrarit Bhargava 			value = atof(number) * 10;
1139062e4aacSPrarit Bhargava 			break;
1140062e4aacSPrarit Bhargava 		}
1141062e4aacSPrarit Bhargava 	}
1142062e4aacSPrarit Bhargava 	free(line);
1143062e4aacSPrarit Bhargava 	fclose(fp);
1144062e4aacSPrarit Bhargava 
1145062e4aacSPrarit Bhargava 	return (int)(value);
1146062e4aacSPrarit Bhargava }
1147062e4aacSPrarit Bhargava 
1148850337ecSZhang Rui static int clx_n_config(struct isst_id *id)
1149062e4aacSPrarit Bhargava {
115056d64692SZhang Rui 	int i, ret;
1151062e4aacSPrarit Bhargava 	unsigned long cpu_bf;
1152062e4aacSPrarit Bhargava 	struct isst_pkg_ctdp_level_info *ctdp_level;
1153062e4aacSPrarit Bhargava 	struct isst_pbf_info *pbf_info;
1154062e4aacSPrarit Bhargava 
1155062e4aacSPrarit Bhargava 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1156062e4aacSPrarit Bhargava 	pbf_info = &ctdp_level->pbf_info;
1157062e4aacSPrarit Bhargava 	ctdp_level->core_cpumask_size =
1158062e4aacSPrarit Bhargava 			alloc_cpu_set(&ctdp_level->core_cpumask);
1159062e4aacSPrarit Bhargava 
1160062e4aacSPrarit Bhargava 	/* find the frequency base ratio */
1161062e4aacSPrarit Bhargava 	ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1162062e4aacSPrarit Bhargava 	if (ctdp_level->tdp_ratio == 0) {
1163062e4aacSPrarit Bhargava 		debug_printf("CLX: cn base ratio is zero\n");
1164062e4aacSPrarit Bhargava 		ret = -1;
1165062e4aacSPrarit Bhargava 		goto error_ret;
1166062e4aacSPrarit Bhargava 	}
1167062e4aacSPrarit Bhargava 
1168062e4aacSPrarit Bhargava 	/* find the high and low priority frequencies */
1169062e4aacSPrarit Bhargava 	pbf_info->p1_high = 0;
1170062e4aacSPrarit Bhargava 	pbf_info->p1_low = ~0;
1171062e4aacSPrarit Bhargava 
1172062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1173062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1174062e4aacSPrarit Bhargava 			continue;
1175062e4aacSPrarit Bhargava 
117600bb07dbSZhang Rui 		if (!is_cpu_in_power_domain(i, id))
1177062e4aacSPrarit Bhargava 			continue;
1178062e4aacSPrarit Bhargava 
1179062e4aacSPrarit Bhargava 		CPU_SET_S(i, ctdp_level->core_cpumask_size,
1180062e4aacSPrarit Bhargava 			  ctdp_level->core_cpumask);
1181062e4aacSPrarit Bhargava 
1182062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1183062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1184062e4aacSPrarit Bhargava 					i);
1185062e4aacSPrarit Bhargava 		if (cpu_bf > pbf_info->p1_high)
1186062e4aacSPrarit Bhargava 			pbf_info->p1_high = cpu_bf;
1187062e4aacSPrarit Bhargava 		if (cpu_bf < pbf_info->p1_low)
1188062e4aacSPrarit Bhargava 			pbf_info->p1_low = cpu_bf;
1189062e4aacSPrarit Bhargava 	}
1190062e4aacSPrarit Bhargava 
1191062e4aacSPrarit Bhargava 	if (pbf_info->p1_high == ~0UL) {
1192062e4aacSPrarit Bhargava 		debug_printf("CLX: maximum base frequency not set\n");
1193062e4aacSPrarit Bhargava 		ret = -1;
1194062e4aacSPrarit Bhargava 		goto error_ret;
1195062e4aacSPrarit Bhargava 	}
1196062e4aacSPrarit Bhargava 
1197062e4aacSPrarit Bhargava 	if (pbf_info->p1_low == 0) {
1198062e4aacSPrarit Bhargava 		debug_printf("CLX: minimum base frequency not set\n");
1199062e4aacSPrarit Bhargava 		ret = -1;
1200062e4aacSPrarit Bhargava 		goto error_ret;
1201062e4aacSPrarit Bhargava 	}
1202062e4aacSPrarit Bhargava 
1203062e4aacSPrarit Bhargava 	/* convert frequencies back to ratios */
120491d92814SSrinivas Pandruvada 	pbf_info->p1_high = pbf_info->p1_high / 100000;
120591d92814SSrinivas Pandruvada 	pbf_info->p1_low = pbf_info->p1_low / 100000;
1206062e4aacSPrarit Bhargava 
1207062e4aacSPrarit Bhargava 	/* create high priority cpu mask */
1208062e4aacSPrarit Bhargava 	pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1209062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1210062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1211062e4aacSPrarit Bhargava 			continue;
1212062e4aacSPrarit Bhargava 
121300bb07dbSZhang Rui 		if (!is_cpu_in_power_domain(i, id))
1214062e4aacSPrarit Bhargava 			continue;
1215062e4aacSPrarit Bhargava 
1216062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1217062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1218062e4aacSPrarit Bhargava 					i);
121991d92814SSrinivas Pandruvada 		cpu_bf = cpu_bf / 100000;
1220062e4aacSPrarit Bhargava 		if (cpu_bf == pbf_info->p1_high)
1221062e4aacSPrarit Bhargava 			CPU_SET_S(i, pbf_info->core_cpumask_size,
1222062e4aacSPrarit Bhargava 				  pbf_info->core_cpumask);
1223062e4aacSPrarit Bhargava 	}
1224062e4aacSPrarit Bhargava 
1225062e4aacSPrarit Bhargava 	/* extra ctdp & pbf struct parameters */
1226062e4aacSPrarit Bhargava 	ctdp_level->processed = 1;
1227062e4aacSPrarit Bhargava 	ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1228062e4aacSPrarit Bhargava 	ctdp_level->pbf_enabled = 1;
1229062e4aacSPrarit Bhargava 	ctdp_level->fact_support = 0; /* FACT is never supported */
1230062e4aacSPrarit Bhargava 	ctdp_level->fact_enabled = 0;
1231062e4aacSPrarit Bhargava 
1232062e4aacSPrarit Bhargava 	return 0;
1233062e4aacSPrarit Bhargava 
1234062e4aacSPrarit Bhargava error_ret:
1235062e4aacSPrarit Bhargava 	free_cpu_set(ctdp_level->core_cpumask);
1236062e4aacSPrarit Bhargava 	return ret;
1237062e4aacSPrarit Bhargava }
1238062e4aacSPrarit Bhargava 
1239850337ecSZhang Rui static void dump_clx_n_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
1240062e4aacSPrarit Bhargava 				   void *arg3, void *arg4)
1241062e4aacSPrarit Bhargava {
1242062e4aacSPrarit Bhargava 	int ret;
1243062e4aacSPrarit Bhargava 
1244ac9d05eaSSrinivas Pandruvada 	if (tdp_level != 0xff && tdp_level != 0) {
1245ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1246ac9d05eaSSrinivas Pandruvada 		exit(0);
1247ac9d05eaSSrinivas Pandruvada 	}
1248ac9d05eaSSrinivas Pandruvada 
1249850337ecSZhang Rui 	ret = clx_n_config(id);
1250062e4aacSPrarit Bhargava 	if (ret) {
12517fc9fefdSSrinivas Pandruvada 		debug_printf("clx_n_config failed");
1252062e4aacSPrarit Bhargava 	} else {
1253062e4aacSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
1254062e4aacSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
1255062e4aacSPrarit Bhargava 
1256062e4aacSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1257062e4aacSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
125828c59ae6SPrarit Bhargava 		clx_n_pkg_dev.processed = 1;
1259850337ecSZhang Rui 		isst_ctdp_display_information(id, outf, tdp_level, &clx_n_pkg_dev);
1260062e4aacSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
1261062e4aacSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
1262062e4aacSPrarit Bhargava 	}
1263062e4aacSPrarit Bhargava }
1264062e4aacSPrarit Bhargava 
1265850337ecSZhang Rui static void dump_isst_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
12663fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
12673fb4f7cdSSrinivas Pandruvada {
12683fb4f7cdSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
12693fb4f7cdSSrinivas Pandruvada 	int ret;
12703fb4f7cdSSrinivas Pandruvada 
12713fb4f7cdSSrinivas Pandruvada 	memset(&pkg_dev, 0, sizeof(pkg_dev));
1272850337ecSZhang Rui 	ret = isst_get_process_ctdp(id, tdp_level, &pkg_dev);
12733fb4f7cdSSrinivas Pandruvada 	if (ret) {
1274850337ecSZhang Rui 		isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, id->cpu);
1275ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1276ac9d05eaSSrinivas Pandruvada 		exit(1);
12773fb4f7cdSSrinivas Pandruvada 	} else {
1278850337ecSZhang Rui 		isst_ctdp_display_information(id, outf, tdp_level, &pkg_dev);
1279850337ecSZhang Rui 		isst_get_process_ctdp_complete(id, &pkg_dev);
12803fb4f7cdSSrinivas Pandruvada 	}
12813fb4f7cdSSrinivas Pandruvada }
12823fb4f7cdSSrinivas Pandruvada 
1283ce1326a2SPrarit Bhargava static void dump_isst_config(int arg)
12843fb4f7cdSSrinivas Pandruvada {
1285062e4aacSPrarit Bhargava 	void *fn;
1286062e4aacSPrarit Bhargava 
12873fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
12883fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12893fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology Performance profile configuration\n");
12903fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12913fb4f7cdSSrinivas Pandruvada 			"including base frequency and turbo frequency configurations\n");
12923fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
12933fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12943fb4f7cdSSrinivas Pandruvada 			"\tIf no arguments, dump information for all TDP levels\n");
12953fb4f7cdSSrinivas Pandruvada 		exit(0);
12963fb4f7cdSSrinivas Pandruvada 	}
12973fb4f7cdSSrinivas Pandruvada 
1298062e4aacSPrarit Bhargava 	if (!is_clx_n_platform())
1299062e4aacSPrarit Bhargava 		fn = dump_isst_config_for_cpu;
1300062e4aacSPrarit Bhargava 	else
1301062e4aacSPrarit Bhargava 		fn = dump_clx_n_config_for_cpu;
1302062e4aacSPrarit Bhargava 
13033fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
13043fb4f7cdSSrinivas Pandruvada 
13053fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
1306062e4aacSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
13073fb4f7cdSSrinivas Pandruvada 	else
1308062e4aacSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
13093fb4f7cdSSrinivas Pandruvada 
13103fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
13113fb4f7cdSSrinivas Pandruvada }
13123fb4f7cdSSrinivas Pandruvada 
1313f981dc17SSrinivas Pandruvada static void adjust_scaling_max_from_base_freq(int cpu);
1314f981dc17SSrinivas Pandruvada 
1315850337ecSZhang Rui static void set_tdp_level_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
13163fb4f7cdSSrinivas Pandruvada 				  void *arg4)
13173fb4f7cdSSrinivas Pandruvada {
13183fb4f7cdSSrinivas Pandruvada 	int ret;
13193fb4f7cdSSrinivas Pandruvada 
1320850337ecSZhang Rui 	ret = isst_set_tdp_level(id, tdp_level);
1321ac9d05eaSSrinivas Pandruvada 	if (ret) {
1322ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1323ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1324ac9d05eaSSrinivas Pandruvada 		exit(1);
1325ac9d05eaSSrinivas Pandruvada 	} else {
1326850337ecSZhang Rui 		isst_display_result(id, outf, "perf-profile", "set_tdp_level",
13273fb4f7cdSSrinivas Pandruvada 				    ret);
13283c64c81aSSrinivas Pandruvada 		if (force_online_offline) {
13293c64c81aSSrinivas Pandruvada 			struct isst_pkg_ctdp_level_info ctdp_level;
13303c64c81aSSrinivas Pandruvada 
1331f981dc17SSrinivas Pandruvada 			/* Wait for updated base frequencies */
1332f981dc17SSrinivas Pandruvada 			usleep(2000);
1333f981dc17SSrinivas Pandruvada 
13343c64c81aSSrinivas Pandruvada 			fprintf(stderr, "Option is set to online/offline\n");
13353c64c81aSSrinivas Pandruvada 			ctdp_level.core_cpumask_size =
13363c64c81aSSrinivas Pandruvada 				alloc_cpu_set(&ctdp_level.core_cpumask);
1337850337ecSZhang Rui 			ret = isst_get_coremask_info(id, tdp_level, &ctdp_level);
13386374de84SSrinivas Pandruvada 			if (ret) {
13396374de84SSrinivas Pandruvada 				isst_display_error_info_message(1, "Can't get coremask, online/offline option is ignored", 0, 0);
13406374de84SSrinivas Pandruvada 				return;
13416374de84SSrinivas Pandruvada 			}
13423c64c81aSSrinivas Pandruvada 			if (ctdp_level.cpu_count) {
13433c64c81aSSrinivas Pandruvada 				int i, max_cpus = get_topo_max_cpus();
13443c64c81aSSrinivas Pandruvada 				for (i = 0; i < max_cpus; ++i) {
134500bb07dbSZhang Rui 					if (!is_cpu_in_power_domain(i, id))
13463c64c81aSSrinivas Pandruvada 						continue;
13473c64c81aSSrinivas Pandruvada 					if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
13483c64c81aSSrinivas Pandruvada 						fprintf(stderr, "online cpu %d\n", i);
13493c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 1);
1350f981dc17SSrinivas Pandruvada 						adjust_scaling_max_from_base_freq(i);
13513c64c81aSSrinivas Pandruvada 					} else {
13523c64c81aSSrinivas Pandruvada 						fprintf(stderr, "offline cpu %d\n", i);
13533c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 0);
13543c64c81aSSrinivas Pandruvada 					}
13553c64c81aSSrinivas Pandruvada 				}
13563c64c81aSSrinivas Pandruvada 			}
13573c64c81aSSrinivas Pandruvada 		}
13583c64c81aSSrinivas Pandruvada 	}
13593fb4f7cdSSrinivas Pandruvada }
13603fb4f7cdSSrinivas Pandruvada 
1361ce1326a2SPrarit Bhargava static void set_tdp_level(int arg)
13623fb4f7cdSSrinivas Pandruvada {
13633fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
13643fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Set Config TDP level\n");
13653fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
13663fb4f7cdSSrinivas Pandruvada 			"\t Arguments: -l|--level : Specify tdp level\n");
13673c64c81aSSrinivas Pandruvada 		fprintf(stderr,
13683c64c81aSSrinivas Pandruvada 			"\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1369ac9d05eaSSrinivas Pandruvada 		fprintf(stderr,
1370ac9d05eaSSrinivas Pandruvada 			"\t  online/offline operation has limitations, refer to Linux hotplug documentation\n");
13713fb4f7cdSSrinivas Pandruvada 		exit(0);
13723fb4f7cdSSrinivas Pandruvada 	}
13733fb4f7cdSSrinivas Pandruvada 
13743fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1375ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
13763fb4f7cdSSrinivas Pandruvada 		exit(1);
13773fb4f7cdSSrinivas Pandruvada 	}
13783fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
13793fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
13803fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
13813fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
13823fb4f7cdSSrinivas Pandruvada 	else
13833fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
13843fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
13853fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
13863fb4f7cdSSrinivas Pandruvada }
13873fb4f7cdSSrinivas Pandruvada 
1388850337ecSZhang Rui static void clx_n_dump_pbf_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
13891aa7177cSPrarit Bhargava 				       void *arg3, void *arg4)
13901aa7177cSPrarit Bhargava {
13911aa7177cSPrarit Bhargava 	int ret;
13921aa7177cSPrarit Bhargava 
1393850337ecSZhang Rui 	ret = clx_n_config(id);
13941aa7177cSPrarit Bhargava 	if (ret) {
139539bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "clx_n_config failed", 0, 0);
13961aa7177cSPrarit Bhargava 	} else {
13971aa7177cSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
13981aa7177cSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
13991aa7177cSPrarit Bhargava 
14001aa7177cSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
14011aa7177cSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
1402850337ecSZhang Rui 		isst_pbf_display_information(id, outf, tdp_level, pbf_info);
14031aa7177cSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
14041aa7177cSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
14051aa7177cSPrarit Bhargava 	}
14061aa7177cSPrarit Bhargava }
14071aa7177cSPrarit Bhargava 
1408850337ecSZhang Rui static void dump_pbf_config_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
14093fb4f7cdSSrinivas Pandruvada 				    void *arg4)
14103fb4f7cdSSrinivas Pandruvada {
14113fb4f7cdSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
14123fb4f7cdSSrinivas Pandruvada 	int ret;
14133fb4f7cdSSrinivas Pandruvada 
1414850337ecSZhang Rui 	ret = isst_get_pbf_info(id, tdp_level, &pbf_info);
14153fb4f7cdSSrinivas Pandruvada 	if (ret) {
141639bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get base-freq info at this level", 1, tdp_level);
141739bae0fcSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
141839bae0fcSSrinivas Pandruvada 		exit(1);
14193fb4f7cdSSrinivas Pandruvada 	} else {
1420850337ecSZhang Rui 		isst_pbf_display_information(id, outf, tdp_level, &pbf_info);
14213fb4f7cdSSrinivas Pandruvada 		isst_get_pbf_info_complete(&pbf_info);
14223fb4f7cdSSrinivas Pandruvada 	}
14233fb4f7cdSSrinivas Pandruvada }
14243fb4f7cdSSrinivas Pandruvada 
1425ce1326a2SPrarit Bhargava static void dump_pbf_config(int arg)
14263fb4f7cdSSrinivas Pandruvada {
14271aa7177cSPrarit Bhargava 	void *fn;
14281aa7177cSPrarit Bhargava 
14293fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
14303fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
14313fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
14323fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
14333fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
14343fb4f7cdSSrinivas Pandruvada 		exit(0);
14353fb4f7cdSSrinivas Pandruvada 	}
14363fb4f7cdSSrinivas Pandruvada 
14373fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
143839bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
14393fb4f7cdSSrinivas Pandruvada 		exit(1);
14403fb4f7cdSSrinivas Pandruvada 	}
14413fb4f7cdSSrinivas Pandruvada 
14421aa7177cSPrarit Bhargava 	if (!is_clx_n_platform())
14431aa7177cSPrarit Bhargava 		fn = dump_pbf_config_for_cpu;
14443fb4f7cdSSrinivas Pandruvada 	else
14451aa7177cSPrarit Bhargava 		fn = clx_n_dump_pbf_config_for_cpu;
14461aa7177cSPrarit Bhargava 
14471aa7177cSPrarit Bhargava 	isst_ctdp_display_information_start(outf);
14481aa7177cSPrarit Bhargava 
14491aa7177cSPrarit Bhargava 	if (max_target_cpus)
14501aa7177cSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
14511aa7177cSPrarit Bhargava 	else
14521aa7177cSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
14531aa7177cSPrarit Bhargava 
14543fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
14553fb4f7cdSSrinivas Pandruvada }
14563fb4f7cdSSrinivas Pandruvada 
1457850337ecSZhang Rui static int set_clos_param(struct isst_id *id, int clos, int epp, int wt, int min, int max)
1458354bd06fSSrinivas Pandruvada {
1459354bd06fSSrinivas Pandruvada 	struct isst_clos_config clos_config;
1460354bd06fSSrinivas Pandruvada 	int ret;
1461354bd06fSSrinivas Pandruvada 
1462850337ecSZhang Rui 	ret = isst_pm_get_clos(id, clos, &clos_config);
1463354bd06fSSrinivas Pandruvada 	if (ret) {
1464fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
1465354bd06fSSrinivas Pandruvada 		return ret;
1466354bd06fSSrinivas Pandruvada 	}
1467354bd06fSSrinivas Pandruvada 	clos_config.clos_min = min;
1468354bd06fSSrinivas Pandruvada 	clos_config.clos_max = max;
1469354bd06fSSrinivas Pandruvada 	clos_config.epp = epp;
1470354bd06fSSrinivas Pandruvada 	clos_config.clos_prop_prio = wt;
1471850337ecSZhang Rui 	ret = isst_set_clos(id, clos, &clos_config);
1472354bd06fSSrinivas Pandruvada 	if (ret) {
1473fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
1474354bd06fSSrinivas Pandruvada 		return ret;
1475354bd06fSSrinivas Pandruvada 	}
1476354bd06fSSrinivas Pandruvada 
1477354bd06fSSrinivas Pandruvada 	return 0;
1478354bd06fSSrinivas Pandruvada }
1479354bd06fSSrinivas Pandruvada 
1480a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1481a9b2f8e2SSrinivas Pandruvada {
1482a9b2f8e2SSrinivas Pandruvada 	char buffer[128], freq_str[16];
1483a9b2f8e2SSrinivas Pandruvada 	int fd, ret, len;
1484a9b2f8e2SSrinivas Pandruvada 
1485a9b2f8e2SSrinivas Pandruvada 	if (max)
1486a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1487a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1488a9b2f8e2SSrinivas Pandruvada 	else
1489a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1490a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1491a9b2f8e2SSrinivas Pandruvada 
1492a9b2f8e2SSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1493a9b2f8e2SSrinivas Pandruvada 	if (fd < 0)
1494a9b2f8e2SSrinivas Pandruvada 		return fd;
1495a9b2f8e2SSrinivas Pandruvada 
1496a9b2f8e2SSrinivas Pandruvada 	snprintf(freq_str, sizeof(freq_str), "%d", freq);
1497a9b2f8e2SSrinivas Pandruvada 	len = strlen(freq_str);
1498a9b2f8e2SSrinivas Pandruvada 	ret = write(fd, freq_str, len);
1499a9b2f8e2SSrinivas Pandruvada 	if (ret == -1) {
1500a9b2f8e2SSrinivas Pandruvada 		close(fd);
1501a9b2f8e2SSrinivas Pandruvada 		return ret;
1502a9b2f8e2SSrinivas Pandruvada 	}
1503a9b2f8e2SSrinivas Pandruvada 	close(fd);
1504a9b2f8e2SSrinivas Pandruvada 
1505a9b2f8e2SSrinivas Pandruvada 	return 0;
1506a9b2f8e2SSrinivas Pandruvada }
1507a9b2f8e2SSrinivas Pandruvada 
1508f981dc17SSrinivas Pandruvada static int no_turbo(void)
1509f981dc17SSrinivas Pandruvada {
1510f981dc17SSrinivas Pandruvada 	return parse_int_file(0, "/sys/devices/system/cpu/intel_pstate/no_turbo");
1511f981dc17SSrinivas Pandruvada }
1512f981dc17SSrinivas Pandruvada 
1513f981dc17SSrinivas Pandruvada static void adjust_scaling_max_from_base_freq(int cpu)
1514f981dc17SSrinivas Pandruvada {
1515f981dc17SSrinivas Pandruvada 	int base_freq, scaling_max_freq;
1516f981dc17SSrinivas Pandruvada 
1517f981dc17SSrinivas Pandruvada 	scaling_max_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1518f981dc17SSrinivas Pandruvada 	base_freq = get_cpufreq_base_freq(cpu);
1519f981dc17SSrinivas Pandruvada 	if (scaling_max_freq < base_freq || no_turbo())
1520f981dc17SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(cpu, 1, base_freq);
1521f981dc17SSrinivas Pandruvada }
1522f981dc17SSrinivas Pandruvada 
1523bbaa2e95SSrinivas Pandruvada static void adjust_scaling_min_from_base_freq(int cpu)
1524bbaa2e95SSrinivas Pandruvada {
1525bbaa2e95SSrinivas Pandruvada 	int base_freq, scaling_min_freq;
1526bbaa2e95SSrinivas Pandruvada 
1527bbaa2e95SSrinivas Pandruvada 	scaling_min_freq = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1528bbaa2e95SSrinivas Pandruvada 	base_freq = get_cpufreq_base_freq(cpu);
1529bbaa2e95SSrinivas Pandruvada 	if (scaling_min_freq < base_freq)
1530bbaa2e95SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(cpu, 0, base_freq);
1531bbaa2e95SSrinivas Pandruvada }
1532bbaa2e95SSrinivas Pandruvada 
1533850337ecSZhang Rui static int set_clx_pbf_cpufreq_scaling_min_max(struct isst_id *id)
1534a9b2f8e2SSrinivas Pandruvada {
1535a9b2f8e2SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info *ctdp_level;
1536a9b2f8e2SSrinivas Pandruvada 	struct isst_pbf_info *pbf_info;
153756d64692SZhang Rui 	int i, freq, freq_high, freq_low;
1538a9b2f8e2SSrinivas Pandruvada 	int ret;
1539a9b2f8e2SSrinivas Pandruvada 
1540850337ecSZhang Rui 	ret = clx_n_config(id);
1541a9b2f8e2SSrinivas Pandruvada 	if (ret) {
15427fc9fefdSSrinivas Pandruvada 		debug_printf("cpufreq_scaling_min_max failed for CLX");
1543a9b2f8e2SSrinivas Pandruvada 		return ret;
1544a9b2f8e2SSrinivas Pandruvada 	}
1545a9b2f8e2SSrinivas Pandruvada 
1546a9b2f8e2SSrinivas Pandruvada 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1547a9b2f8e2SSrinivas Pandruvada 	pbf_info = &ctdp_level->pbf_info;
1548a9b2f8e2SSrinivas Pandruvada 	freq_high = pbf_info->p1_high * 100000;
1549a9b2f8e2SSrinivas Pandruvada 	freq_low = pbf_info->p1_low * 100000;
1550a9b2f8e2SSrinivas Pandruvada 
1551a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
155200bb07dbSZhang Rui 		if (!is_cpu_in_power_domain(i, id))
1553a9b2f8e2SSrinivas Pandruvada 			continue;
1554a9b2f8e2SSrinivas Pandruvada 
1555a9b2f8e2SSrinivas Pandruvada 		if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1556a9b2f8e2SSrinivas Pandruvada 				  pbf_info->core_cpumask))
1557a9b2f8e2SSrinivas Pandruvada 			freq = freq_high;
1558a9b2f8e2SSrinivas Pandruvada 		else
1559a9b2f8e2SSrinivas Pandruvada 			freq = freq_low;
1560a9b2f8e2SSrinivas Pandruvada 
1561a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 1, freq);
1562a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 0, freq);
1563a9b2f8e2SSrinivas Pandruvada 	}
1564a9b2f8e2SSrinivas Pandruvada 
1565a9b2f8e2SSrinivas Pandruvada 	return 0;
1566a9b2f8e2SSrinivas Pandruvada }
1567a9b2f8e2SSrinivas Pandruvada 
1568a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1569354bd06fSSrinivas Pandruvada {
1570354bd06fSSrinivas Pandruvada 	char buffer[128], min_freq[16];
1571354bd06fSSrinivas Pandruvada 	int fd, ret, len;
1572354bd06fSSrinivas Pandruvada 
1573354bd06fSSrinivas Pandruvada 	if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1574354bd06fSSrinivas Pandruvada 		return -1;
1575354bd06fSSrinivas Pandruvada 
1576a9b2f8e2SSrinivas Pandruvada 	if (cpuinfo_max)
1577354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1578354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1579354bd06fSSrinivas Pandruvada 	else
1580354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1581354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1582354bd06fSSrinivas Pandruvada 
1583354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_RDONLY);
1584354bd06fSSrinivas Pandruvada 	if (fd < 0)
1585354bd06fSSrinivas Pandruvada 		return fd;
1586354bd06fSSrinivas Pandruvada 
1587354bd06fSSrinivas Pandruvada 	len = read(fd, min_freq, sizeof(min_freq));
1588354bd06fSSrinivas Pandruvada 	close(fd);
1589354bd06fSSrinivas Pandruvada 
1590354bd06fSSrinivas Pandruvada 	if (len < 0)
1591354bd06fSSrinivas Pandruvada 		return len;
1592354bd06fSSrinivas Pandruvada 
1593a9b2f8e2SSrinivas Pandruvada 	if (scaling_max)
1594a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1595a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1596a9b2f8e2SSrinivas Pandruvada 	else
1597354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1598354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1599354bd06fSSrinivas Pandruvada 
1600354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1601354bd06fSSrinivas Pandruvada 	if (fd < 0)
1602354bd06fSSrinivas Pandruvada 		return fd;
1603354bd06fSSrinivas Pandruvada 
1604354bd06fSSrinivas Pandruvada 	len = strlen(min_freq);
1605354bd06fSSrinivas Pandruvada 	ret = write(fd, min_freq, len);
1606354bd06fSSrinivas Pandruvada 	if (ret == -1) {
1607354bd06fSSrinivas Pandruvada 		close(fd);
1608354bd06fSSrinivas Pandruvada 		return ret;
1609354bd06fSSrinivas Pandruvada 	}
1610354bd06fSSrinivas Pandruvada 	close(fd);
1611354bd06fSSrinivas Pandruvada 
1612354bd06fSSrinivas Pandruvada 	return 0;
1613354bd06fSSrinivas Pandruvada }
1614354bd06fSSrinivas Pandruvada 
1615850337ecSZhang Rui static void set_scaling_min_to_cpuinfo_max(struct isst_id *id)
1616354bd06fSSrinivas Pandruvada {
161756d64692SZhang Rui 	int i;
1618354bd06fSSrinivas Pandruvada 
1619354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
162000bb07dbSZhang Rui 		if (!is_cpu_in_power_domain(i, id))
1621354bd06fSSrinivas Pandruvada 			continue;
1622354bd06fSSrinivas Pandruvada 
16239734213eSSrinivas Pandruvada 		adjust_scaling_max_from_base_freq(i);
1624a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1625bbaa2e95SSrinivas Pandruvada 		adjust_scaling_min_from_base_freq(i);
1626354bd06fSSrinivas Pandruvada 	}
1627354bd06fSSrinivas Pandruvada }
1628354bd06fSSrinivas Pandruvada 
1629850337ecSZhang Rui static void set_scaling_min_to_cpuinfo_min(struct isst_id *id)
1630354bd06fSSrinivas Pandruvada {
163156d64692SZhang Rui 	int i;
1632354bd06fSSrinivas Pandruvada 
1633354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
163400bb07dbSZhang Rui 		if (!is_cpu_in_power_domain(i, id))
1635354bd06fSSrinivas Pandruvada 			continue;
1636354bd06fSSrinivas Pandruvada 
16379734213eSSrinivas Pandruvada 		adjust_scaling_max_from_base_freq(i);
1638a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1639a9b2f8e2SSrinivas Pandruvada 	}
1640a9b2f8e2SSrinivas Pandruvada }
1641a9b2f8e2SSrinivas Pandruvada 
1642850337ecSZhang Rui static void set_scaling_max_to_cpuinfo_max(struct isst_id *id)
1643a9b2f8e2SSrinivas Pandruvada {
164456d64692SZhang Rui 	int i;
1645a9b2f8e2SSrinivas Pandruvada 
1646a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
164700bb07dbSZhang Rui 		if (!is_cpu_in_power_domain(i, id))
1648a9b2f8e2SSrinivas Pandruvada 			continue;
1649a9b2f8e2SSrinivas Pandruvada 
1650a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1651354bd06fSSrinivas Pandruvada 	}
1652354bd06fSSrinivas Pandruvada }
1653354bd06fSSrinivas Pandruvada 
1654850337ecSZhang Rui static int set_core_priority_and_min(struct isst_id *id, int mask_size,
1655354bd06fSSrinivas Pandruvada 				     cpu_set_t *cpu_mask, int min_high,
1656354bd06fSSrinivas Pandruvada 				     int min_low)
1657354bd06fSSrinivas Pandruvada {
165856d64692SZhang Rui 	int ret, i;
1659354bd06fSSrinivas Pandruvada 
1660354bd06fSSrinivas Pandruvada 	if (!CPU_COUNT_S(mask_size, cpu_mask))
1661354bd06fSSrinivas Pandruvada 		return -1;
1662354bd06fSSrinivas Pandruvada 
1663850337ecSZhang Rui 	ret = set_clos_param(id, 0, 0, 0, min_high, 0xff);
1664354bd06fSSrinivas Pandruvada 	if (ret)
1665354bd06fSSrinivas Pandruvada 		return ret;
1666354bd06fSSrinivas Pandruvada 
1667850337ecSZhang Rui 	ret = set_clos_param(id, 1, 15, 15, min_low, 0xff);
1668354bd06fSSrinivas Pandruvada 	if (ret)
1669354bd06fSSrinivas Pandruvada 		return ret;
1670354bd06fSSrinivas Pandruvada 
1671850337ecSZhang Rui 	ret = set_clos_param(id, 2, 15, 15, min_low, 0xff);
1672354bd06fSSrinivas Pandruvada 	if (ret)
1673354bd06fSSrinivas Pandruvada 		return ret;
1674354bd06fSSrinivas Pandruvada 
1675850337ecSZhang Rui 	ret = set_clos_param(id, 3, 15, 15, min_low, 0xff);
1676354bd06fSSrinivas Pandruvada 	if (ret)
1677354bd06fSSrinivas Pandruvada 		return ret;
1678354bd06fSSrinivas Pandruvada 
1679354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1680354bd06fSSrinivas Pandruvada 		int clos;
1681850337ecSZhang Rui 		struct isst_id tid;
1682354bd06fSSrinivas Pandruvada 
168300bb07dbSZhang Rui 		if (!is_cpu_in_power_domain(i, id))
1684354bd06fSSrinivas Pandruvada 			continue;
1685354bd06fSSrinivas Pandruvada 
1686354bd06fSSrinivas Pandruvada 		if (CPU_ISSET_S(i, mask_size, cpu_mask))
1687354bd06fSSrinivas Pandruvada 			clos = 0;
1688354bd06fSSrinivas Pandruvada 		else
1689354bd06fSSrinivas Pandruvada 			clos = 3;
1690354bd06fSSrinivas Pandruvada 
1691354bd06fSSrinivas Pandruvada 		debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1692850337ecSZhang Rui 		set_isst_id(&tid, i);
1693850337ecSZhang Rui 		ret = isst_clos_associate(&tid, clos);
1694354bd06fSSrinivas Pandruvada 		if (ret) {
1695fe6fb216SSrinivas Pandruvada 			isst_display_error_info_message(1, "isst_clos_associate failed", 0, 0);
1696354bd06fSSrinivas Pandruvada 			return ret;
1697354bd06fSSrinivas Pandruvada 		}
1698354bd06fSSrinivas Pandruvada 	}
1699354bd06fSSrinivas Pandruvada 
1700354bd06fSSrinivas Pandruvada 	return 0;
1701354bd06fSSrinivas Pandruvada }
1702354bd06fSSrinivas Pandruvada 
1703850337ecSZhang Rui static int set_pbf_core_power(struct isst_id *id)
1704354bd06fSSrinivas Pandruvada {
1705354bd06fSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
1706354bd06fSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
1707354bd06fSSrinivas Pandruvada 	int ret;
1708354bd06fSSrinivas Pandruvada 
1709850337ecSZhang Rui 	ret = isst_get_ctdp_levels(id, &pkg_dev);
1710354bd06fSSrinivas Pandruvada 	if (ret) {
1711fe6fb216SSrinivas Pandruvada 		debug_printf("isst_get_ctdp_levels failed");
1712354bd06fSSrinivas Pandruvada 		return ret;
1713354bd06fSSrinivas Pandruvada 	}
1714354bd06fSSrinivas Pandruvada 	debug_printf("Current_level: %d\n", pkg_dev.current_level);
1715354bd06fSSrinivas Pandruvada 
1716850337ecSZhang Rui 	ret = isst_get_pbf_info(id, pkg_dev.current_level, &pbf_info);
1717354bd06fSSrinivas Pandruvada 	if (ret) {
1718fe6fb216SSrinivas Pandruvada 		debug_printf("isst_get_pbf_info failed");
1719354bd06fSSrinivas Pandruvada 		return ret;
1720354bd06fSSrinivas Pandruvada 	}
1721354bd06fSSrinivas Pandruvada 	debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1722354bd06fSSrinivas Pandruvada 		     pbf_info.p1_low);
1723354bd06fSSrinivas Pandruvada 
1724850337ecSZhang Rui 	ret = set_core_priority_and_min(id, pbf_info.core_cpumask_size,
1725354bd06fSSrinivas Pandruvada 					pbf_info.core_cpumask,
1726354bd06fSSrinivas Pandruvada 					pbf_info.p1_high, pbf_info.p1_low);
1727354bd06fSSrinivas Pandruvada 	if (ret) {
1728fe6fb216SSrinivas Pandruvada 		debug_printf("set_core_priority_and_min failed");
1729354bd06fSSrinivas Pandruvada 		return ret;
1730354bd06fSSrinivas Pandruvada 	}
1731354bd06fSSrinivas Pandruvada 
1732850337ecSZhang Rui 	ret = isst_pm_qos_config(id, 1, 1);
1733354bd06fSSrinivas Pandruvada 	if (ret) {
1734fe6fb216SSrinivas Pandruvada 		debug_printf("isst_pm_qos_config failed");
1735354bd06fSSrinivas Pandruvada 		return ret;
1736354bd06fSSrinivas Pandruvada 	}
1737354bd06fSSrinivas Pandruvada 
1738354bd06fSSrinivas Pandruvada 	return 0;
1739354bd06fSSrinivas Pandruvada }
1740354bd06fSSrinivas Pandruvada 
1741850337ecSZhang Rui static void set_pbf_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
17423fb4f7cdSSrinivas Pandruvada 			    void *arg4)
17433fb4f7cdSSrinivas Pandruvada {
17443d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
17453d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
17463fb4f7cdSSrinivas Pandruvada 	int ret;
17473fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
17483fb4f7cdSSrinivas Pandruvada 
17491aa7177cSPrarit Bhargava 	if (is_clx_n_platform()) {
17501aa7177cSPrarit Bhargava 		ret = 0;
17517fc9fefdSSrinivas Pandruvada 		if (status) {
1752850337ecSZhang Rui 			set_clx_pbf_cpufreq_scaling_min_max(id);
1753a9b2f8e2SSrinivas Pandruvada 
1754a9b2f8e2SSrinivas Pandruvada 		} else {
1755850337ecSZhang Rui 			set_scaling_max_to_cpuinfo_max(id);
1756850337ecSZhang Rui 			set_scaling_min_to_cpuinfo_min(id);
1757a9b2f8e2SSrinivas Pandruvada 		}
17581aa7177cSPrarit Bhargava 		goto disp_result;
17591aa7177cSPrarit Bhargava 	}
17601aa7177cSPrarit Bhargava 
1761850337ecSZhang Rui 	ret = isst_get_ctdp_levels(id, &pkg_dev);
17623d904f06SSrinivas Pandruvada 	if (ret) {
17633d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
17643d904f06SSrinivas Pandruvada 		goto disp_result;
17653d904f06SSrinivas Pandruvada 	}
17663d904f06SSrinivas Pandruvada 
1767850337ecSZhang Rui 	ret = isst_get_ctdp_control(id, pkg_dev.current_level, &ctdp_level);
17683d904f06SSrinivas Pandruvada 	if (ret) {
17693d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get current level", 0, 0);
17703d904f06SSrinivas Pandruvada 		goto disp_result;
17713d904f06SSrinivas Pandruvada 	}
17723d904f06SSrinivas Pandruvada 
17733d904f06SSrinivas Pandruvada 	if (!ctdp_level.pbf_support) {
17743d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "base-freq feature is not present at this level", 1, pkg_dev.current_level);
17753d904f06SSrinivas Pandruvada 		ret = -1;
17763d904f06SSrinivas Pandruvada 		goto disp_result;
17773d904f06SSrinivas Pandruvada 	}
17783d904f06SSrinivas Pandruvada 
1779097a5222SSrinivas Pandruvada 	if (auto_mode && status) {
1780850337ecSZhang Rui 		ret = set_pbf_core_power(id);
1781354bd06fSSrinivas Pandruvada 		if (ret)
1782354bd06fSSrinivas Pandruvada 			goto disp_result;
1783354bd06fSSrinivas Pandruvada 	}
1784354bd06fSSrinivas Pandruvada 
1785850337ecSZhang Rui 	ret = isst_set_pbf_fact_status(id, 1, status);
17863fb4f7cdSSrinivas Pandruvada 	if (ret) {
178739bae0fcSSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1788354bd06fSSrinivas Pandruvada 		if (auto_mode)
1789850337ecSZhang Rui 			isst_pm_qos_config(id, 0, 0);
17903fb4f7cdSSrinivas Pandruvada 	} else {
1791354bd06fSSrinivas Pandruvada 		if (auto_mode) {
1792354bd06fSSrinivas Pandruvada 			if (status)
1793850337ecSZhang Rui 				set_scaling_min_to_cpuinfo_max(id);
1794354bd06fSSrinivas Pandruvada 			else
1795850337ecSZhang Rui 				set_scaling_min_to_cpuinfo_min(id);
1796354bd06fSSrinivas Pandruvada 		}
1797354bd06fSSrinivas Pandruvada 	}
1798354bd06fSSrinivas Pandruvada 
1799097a5222SSrinivas Pandruvada 	if (auto_mode && !status)
1800850337ecSZhang Rui 		isst_pm_qos_config(id, 0, 1);
1801097a5222SSrinivas Pandruvada 
1802354bd06fSSrinivas Pandruvada disp_result:
18033fb4f7cdSSrinivas Pandruvada 	if (status)
1804850337ecSZhang Rui 		isst_display_result(id, outf, "base-freq", "enable",
18053fb4f7cdSSrinivas Pandruvada 				    ret);
18063fb4f7cdSSrinivas Pandruvada 	else
1807850337ecSZhang Rui 		isst_display_result(id, outf, "base-freq", "disable",
18083fb4f7cdSSrinivas Pandruvada 				    ret);
18093fb4f7cdSSrinivas Pandruvada }
18103fb4f7cdSSrinivas Pandruvada 
1811ce1326a2SPrarit Bhargava static void set_pbf_enable(int arg)
18123fb4f7cdSSrinivas Pandruvada {
1813ce1326a2SPrarit Bhargava 	int enable = arg;
18143fb4f7cdSSrinivas Pandruvada 
18153fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1816ce1326a2SPrarit Bhargava 		if (enable) {
18173fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1818354bd06fSSrinivas Pandruvada 				"Enable Intel Speed Select Technology base frequency feature\n");
181939bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
182039bae0fcSSrinivas Pandruvada 				fprintf(stderr,
182139bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't enable feature in the hardware.\n");
182239bae0fcSSrinivas Pandruvada 				fprintf(stderr,
182339bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpufreq base_frequency.\n");
182439bae0fcSSrinivas Pandruvada 				exit(0);
182539bae0fcSSrinivas Pandruvada 
182639bae0fcSSrinivas Pandruvada 			}
1827354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1828354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1829ce1326a2SPrarit Bhargava 		} else {
1830354bd06fSSrinivas Pandruvada 
183139bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
183239bae0fcSSrinivas Pandruvada 				fprintf(stderr,
183339bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't disable feature in the hardware.\n");
183439bae0fcSSrinivas Pandruvada 				fprintf(stderr,
183539bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpuinfo_min_freq\n");
183639bae0fcSSrinivas Pandruvada 				exit(0);
183739bae0fcSSrinivas Pandruvada 			}
18383fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1839354bd06fSSrinivas Pandruvada 				"Disable Intel Speed Select Technology base frequency feature\n");
1840354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1841354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1842ce1326a2SPrarit Bhargava 		}
18433fb4f7cdSSrinivas Pandruvada 		exit(0);
18443fb4f7cdSSrinivas Pandruvada 	}
18453fb4f7cdSSrinivas Pandruvada 
18463fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
18473fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
18483fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1849ce1326a2SPrarit Bhargava 						  NULL, &enable);
18503fb4f7cdSSrinivas Pandruvada 	else
18513fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1852ce1326a2SPrarit Bhargava 					       NULL, &enable);
18533fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
18543fb4f7cdSSrinivas Pandruvada }
18553fb4f7cdSSrinivas Pandruvada 
1856850337ecSZhang Rui static void dump_fact_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
18573fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
18583fb4f7cdSSrinivas Pandruvada {
18593fb4f7cdSSrinivas Pandruvada 	struct isst_fact_info fact_info;
18603fb4f7cdSSrinivas Pandruvada 	int ret;
18613fb4f7cdSSrinivas Pandruvada 
1862850337ecSZhang Rui 	ret = isst_get_fact_info(id, tdp_level, fact_bucket, &fact_info);
1863a9fd6ae7SSrinivas Pandruvada 	if (ret) {
1864a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get turbo-freq info at this level", 1, tdp_level);
1865a9fd6ae7SSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1866a9fd6ae7SSrinivas Pandruvada 		exit(1);
1867a9fd6ae7SSrinivas Pandruvada 	} else {
1868850337ecSZhang Rui 		isst_fact_display_information(id, outf, tdp_level, fact_bucket,
18693fb4f7cdSSrinivas Pandruvada 					      fact_avx, &fact_info);
18703fb4f7cdSSrinivas Pandruvada 	}
1871a9fd6ae7SSrinivas Pandruvada }
18723fb4f7cdSSrinivas Pandruvada 
1873ce1326a2SPrarit Bhargava static void dump_fact_config(int arg)
18743fb4f7cdSSrinivas Pandruvada {
18753fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
18763fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18773fb4f7cdSSrinivas Pandruvada 			"Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
18783fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18793fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
18803fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18813fb4f7cdSSrinivas Pandruvada 			"\tArguments: -b|--bucket : Bucket index to dump\n");
18823fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
18833fb4f7cdSSrinivas Pandruvada 			"\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
18843fb4f7cdSSrinivas Pandruvada 		exit(0);
18853fb4f7cdSSrinivas Pandruvada 	}
18863fb4f7cdSSrinivas Pandruvada 
18873fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1888a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level\n", 0, 0);
18893fb4f7cdSSrinivas Pandruvada 		exit(1);
18903fb4f7cdSSrinivas Pandruvada 	}
18913fb4f7cdSSrinivas Pandruvada 
18923fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
18933fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
18943fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
18953fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
18963fb4f7cdSSrinivas Pandruvada 	else
18973fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
18983fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
18993fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
19003fb4f7cdSSrinivas Pandruvada }
19013fb4f7cdSSrinivas Pandruvada 
1902850337ecSZhang Rui static void set_fact_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
19033fb4f7cdSSrinivas Pandruvada 			     void *arg4)
19043fb4f7cdSSrinivas Pandruvada {
19053d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
19063d904f06SSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
19073fb4f7cdSSrinivas Pandruvada 	int ret;
19083fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
19093fb4f7cdSSrinivas Pandruvada 
19102da6391dSSrinivas Pandruvada 	if (status && no_turbo()) {
19112da6391dSSrinivas Pandruvada 		isst_display_error_info_message(1, "Turbo mode is disabled", 0, 0);
19122da6391dSSrinivas Pandruvada 		ret = -1;
19132da6391dSSrinivas Pandruvada 		goto disp_results;
19142da6391dSSrinivas Pandruvada 	}
19152da6391dSSrinivas Pandruvada 
1916850337ecSZhang Rui 	ret = isst_get_ctdp_levels(id, &pkg_dev);
19173d904f06SSrinivas Pandruvada 	if (ret) {
19183d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
19193d904f06SSrinivas Pandruvada 		goto disp_results;
19203d904f06SSrinivas Pandruvada 	}
19213d904f06SSrinivas Pandruvada 
1922850337ecSZhang Rui 	ret = isst_get_ctdp_control(id, pkg_dev.current_level, &ctdp_level);
19233d904f06SSrinivas Pandruvada 	if (ret) {
19243d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get current level", 0, 0);
19253d904f06SSrinivas Pandruvada 		goto disp_results;
19263d904f06SSrinivas Pandruvada 	}
19273d904f06SSrinivas Pandruvada 
19283d904f06SSrinivas Pandruvada 	if (!ctdp_level.fact_support) {
19293d904f06SSrinivas Pandruvada 		isst_display_error_info_message(1, "turbo-freq feature is not present at this level", 1, pkg_dev.current_level);
19303d904f06SSrinivas Pandruvada 		ret = -1;
19313d904f06SSrinivas Pandruvada 		goto disp_results;
19323d904f06SSrinivas Pandruvada 	}
19333d904f06SSrinivas Pandruvada 
19347983ed6fSSrinivas Pandruvada 	if (status) {
1935850337ecSZhang Rui 		ret = isst_pm_qos_config(id, 1, 1);
19363fb4f7cdSSrinivas Pandruvada 		if (ret)
1937a6a82f9bSSrinivas Pandruvada 			goto disp_results;
1938a6a82f9bSSrinivas Pandruvada 	}
1939a6a82f9bSSrinivas Pandruvada 
1940850337ecSZhang Rui 	ret = isst_set_pbf_fact_status(id, 0, status);
1941a6a82f9bSSrinivas Pandruvada 	if (ret) {
1942a9fd6ae7SSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1943a6a82f9bSSrinivas Pandruvada 		if (auto_mode)
1944850337ecSZhang Rui 			isst_pm_qos_config(id, 0, 0);
1945a6a82f9bSSrinivas Pandruvada 
1946a6a82f9bSSrinivas Pandruvada 		goto disp_results;
1947a6a82f9bSSrinivas Pandruvada 	}
1948a6a82f9bSSrinivas Pandruvada 
1949a6a82f9bSSrinivas Pandruvada 	/* Set TRL */
19503fb4f7cdSSrinivas Pandruvada 	if (status) {
19513fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp pkg_dev;
19523fb4f7cdSSrinivas Pandruvada 
1953850337ecSZhang Rui 		ret = isst_get_ctdp_levels(id, &pkg_dev);
1954a6a82f9bSSrinivas Pandruvada 		if (!ret)
1955850337ecSZhang Rui 			ret = isst_set_trl(id, fact_trl);
1956a6a82f9bSSrinivas Pandruvada 		if (ret && auto_mode)
1957850337ecSZhang Rui 			isst_pm_qos_config(id, 0, 0);
1958097a5222SSrinivas Pandruvada 	} else {
1959097a5222SSrinivas Pandruvada 		if (auto_mode)
1960850337ecSZhang Rui 			isst_pm_qos_config(id, 0, 0);
1961a6a82f9bSSrinivas Pandruvada 	}
1962a6a82f9bSSrinivas Pandruvada 
1963a6a82f9bSSrinivas Pandruvada disp_results:
1964a6a82f9bSSrinivas Pandruvada 	if (status) {
1965850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-freq", "enable", ret);
196614a8aa49SSrinivas Pandruvada 		if (ret)
196714a8aa49SSrinivas Pandruvada 			fact_enable_fail = ret;
19683fb4f7cdSSrinivas Pandruvada 	} else {
19693fb4f7cdSSrinivas Pandruvada 		/* Since we modified TRL during Fact enable, restore it */
1970850337ecSZhang Rui 		isst_set_trl_from_current_tdp(id, fact_trl);
1971850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-freq", "disable", ret);
19723fb4f7cdSSrinivas Pandruvada 	}
19733fb4f7cdSSrinivas Pandruvada }
19743fb4f7cdSSrinivas Pandruvada 
1975ce1326a2SPrarit Bhargava static void set_fact_enable(int arg)
19763fb4f7cdSSrinivas Pandruvada {
1977ce1326a2SPrarit Bhargava 	int i, ret, enable = arg;
1978850337ecSZhang Rui 	struct isst_id id;
19793fb4f7cdSSrinivas Pandruvada 
19803fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1981ce1326a2SPrarit Bhargava 		if (enable) {
19823fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
19833fb4f7cdSSrinivas Pandruvada 				"Enable Intel Speed Select Technology Turbo frequency feature\n");
19843fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
19853fb4f7cdSSrinivas Pandruvada 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1986a6a82f9bSSrinivas Pandruvada 			fprintf(stderr,
1987a6a82f9bSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1988ce1326a2SPrarit Bhargava 			fprintf(stderr,
1989ce1326a2SPrarit Bhargava 				"-C|--cpu option as as high priority using core-power feature\n");
1990ce1326a2SPrarit Bhargava 		} else {
1991ce1326a2SPrarit Bhargava 			fprintf(stderr,
1992ce1326a2SPrarit Bhargava 				"Disable Intel Speed Select Technology turbo frequency feature\n");
1993ce1326a2SPrarit Bhargava 			fprintf(stderr,
1994ce1326a2SPrarit Bhargava 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1995ce1326a2SPrarit Bhargava 			fprintf(stderr,
1996ce1326a2SPrarit Bhargava 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1997ce1326a2SPrarit Bhargava 		}
19983fb4f7cdSSrinivas Pandruvada 		exit(0);
19993fb4f7cdSSrinivas Pandruvada 	}
20003fb4f7cdSSrinivas Pandruvada 
20013fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
20023fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
20033fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
2004ce1326a2SPrarit Bhargava 						  NULL, &enable);
20053fb4f7cdSSrinivas Pandruvada 	else
20063fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
2007ce1326a2SPrarit Bhargava 					       NULL, &enable);
20083fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
2009a6a82f9bSSrinivas Pandruvada 
201014a8aa49SSrinivas Pandruvada 	if (!fact_enable_fail && enable && auto_mode) {
2011a6a82f9bSSrinivas Pandruvada 		/*
2012a6a82f9bSSrinivas Pandruvada 		 * When we adjust CLOS param, we have to set for siblings also.
2013a6a82f9bSSrinivas Pandruvada 		 * So for the each user specified CPU, also add the sibling
2014a6a82f9bSSrinivas Pandruvada 		 * in the present_cpu_mask.
2015a6a82f9bSSrinivas Pandruvada 		 */
2016a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
2017a6a82f9bSSrinivas Pandruvada 			char buffer[128], sibling_list[128], *cpu_str;
2018a6a82f9bSSrinivas Pandruvada 			int fd, len;
2019a6a82f9bSSrinivas Pandruvada 
2020a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
2021a6a82f9bSSrinivas Pandruvada 				continue;
2022a6a82f9bSSrinivas Pandruvada 
2023a6a82f9bSSrinivas Pandruvada 			snprintf(buffer, sizeof(buffer),
2024a6a82f9bSSrinivas Pandruvada 				 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
2025a6a82f9bSSrinivas Pandruvada 
2026a6a82f9bSSrinivas Pandruvada 			fd = open(buffer, O_RDONLY);
2027a6a82f9bSSrinivas Pandruvada 			if (fd < 0)
2028a6a82f9bSSrinivas Pandruvada 				continue;
2029a6a82f9bSSrinivas Pandruvada 
2030a6a82f9bSSrinivas Pandruvada 			len = read(fd, sibling_list, sizeof(sibling_list));
2031a6a82f9bSSrinivas Pandruvada 			close(fd);
2032a6a82f9bSSrinivas Pandruvada 
2033a6a82f9bSSrinivas Pandruvada 			if (len < 0)
2034a6a82f9bSSrinivas Pandruvada 				continue;
2035a6a82f9bSSrinivas Pandruvada 
2036a6a82f9bSSrinivas Pandruvada 			cpu_str = strtok(sibling_list, ",");
2037a6a82f9bSSrinivas Pandruvada 			while (cpu_str != NULL) {
2038a6a82f9bSSrinivas Pandruvada 				int cpu;
2039a6a82f9bSSrinivas Pandruvada 
2040a6a82f9bSSrinivas Pandruvada 				sscanf(cpu_str, "%d", &cpu);
2041a6a82f9bSSrinivas Pandruvada 				CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
2042a6a82f9bSSrinivas Pandruvada 				cpu_str = strtok(NULL, ",");
2043a6a82f9bSSrinivas Pandruvada 			}
2044a6a82f9bSSrinivas Pandruvada 		}
2045a6a82f9bSSrinivas Pandruvada 
2046a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
2047a6a82f9bSSrinivas Pandruvada 			int clos;
2048a6a82f9bSSrinivas Pandruvada 
2049a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
2050a6a82f9bSSrinivas Pandruvada 				continue;
2051a6a82f9bSSrinivas Pandruvada 
2052850337ecSZhang Rui 			set_isst_id(&id, i);
2053850337ecSZhang Rui 			ret = set_clos_param(&id, 0, 0, 0, 0, 0xff);
2054a6a82f9bSSrinivas Pandruvada 			if (ret)
2055a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2056a6a82f9bSSrinivas Pandruvada 
2057850337ecSZhang Rui 			ret = set_clos_param(&id, 1, 15, 15, 0, 0xff);
2058a6a82f9bSSrinivas Pandruvada 			if (ret)
2059a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2060a6a82f9bSSrinivas Pandruvada 
2061850337ecSZhang Rui 			ret = set_clos_param(&id, 2, 15, 15, 0, 0xff);
2062a6a82f9bSSrinivas Pandruvada 			if (ret)
2063a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2064a6a82f9bSSrinivas Pandruvada 
2065850337ecSZhang Rui 			ret = set_clos_param(&id, 3, 15, 15, 0, 0xff);
2066a6a82f9bSSrinivas Pandruvada 			if (ret)
2067a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2068a6a82f9bSSrinivas Pandruvada 
2069a6a82f9bSSrinivas Pandruvada 			if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
2070a6a82f9bSSrinivas Pandruvada 				clos = 0;
2071a6a82f9bSSrinivas Pandruvada 			else
2072a6a82f9bSSrinivas Pandruvada 				clos = 3;
2073a6a82f9bSSrinivas Pandruvada 
2074a6a82f9bSSrinivas Pandruvada 			debug_printf("Associate cpu: %d clos: %d\n", i, clos);
2075850337ecSZhang Rui 			ret = isst_clos_associate(&id, clos);
2076a6a82f9bSSrinivas Pandruvada 			if (ret)
2077a6a82f9bSSrinivas Pandruvada 				goto error_disp;
2078a6a82f9bSSrinivas Pandruvada 		}
2079850337ecSZhang Rui 		set_isst_id(&id, -1);
2080850337ecSZhang Rui 		isst_display_result(&id, outf, "turbo-freq --auto", "enable", 0);
2081a6a82f9bSSrinivas Pandruvada 	}
2082a6a82f9bSSrinivas Pandruvada 
2083a6a82f9bSSrinivas Pandruvada 	return;
2084a6a82f9bSSrinivas Pandruvada 
2085a6a82f9bSSrinivas Pandruvada error_disp:
2086850337ecSZhang Rui 	isst_display_result(&id, outf, "turbo-freq --auto", "enable", ret);
2087a6a82f9bSSrinivas Pandruvada 
20883fb4f7cdSSrinivas Pandruvada }
20893fb4f7cdSSrinivas Pandruvada 
2090850337ecSZhang Rui static void enable_clos_qos_config(struct isst_id *id, void *arg1, void *arg2, void *arg3,
20913fb4f7cdSSrinivas Pandruvada 				   void *arg4)
20923fb4f7cdSSrinivas Pandruvada {
20933fb4f7cdSSrinivas Pandruvada 	int ret;
20943fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
20953fb4f7cdSSrinivas Pandruvada 
2096fe6fb216SSrinivas Pandruvada 	if (is_skx_based_platform())
2097fe6fb216SSrinivas Pandruvada 		clos_priority_type = 1;
2098fe6fb216SSrinivas Pandruvada 
2099850337ecSZhang Rui 	ret = isst_pm_qos_config(id, status, clos_priority_type);
2100a6a82f9bSSrinivas Pandruvada 	if (ret)
2101fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_qos_config failed", 0, 0);
2102a6a82f9bSSrinivas Pandruvada 
21033fb4f7cdSSrinivas Pandruvada 	if (status)
2104850337ecSZhang Rui 		isst_display_result(id, outf, "core-power", "enable",
21053fb4f7cdSSrinivas Pandruvada 				    ret);
21063fb4f7cdSSrinivas Pandruvada 	else
2107850337ecSZhang Rui 		isst_display_result(id, outf, "core-power", "disable",
21083fb4f7cdSSrinivas Pandruvada 				    ret);
21093fb4f7cdSSrinivas Pandruvada }
21103fb4f7cdSSrinivas Pandruvada 
2111ce1326a2SPrarit Bhargava static void set_clos_enable(int arg)
21123fb4f7cdSSrinivas Pandruvada {
2113ce1326a2SPrarit Bhargava 	int enable = arg;
21143fb4f7cdSSrinivas Pandruvada 
21153fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
2116ce1326a2SPrarit Bhargava 		if (enable) {
2117ce1326a2SPrarit Bhargava 			fprintf(stderr,
2118ce1326a2SPrarit Bhargava 				"Enable core-power for a package/die\n");
2119fe6fb216SSrinivas Pandruvada 			if (!is_skx_based_platform()) {
21203fb4f7cdSSrinivas Pandruvada 				fprintf(stderr,
21213fb4f7cdSSrinivas Pandruvada 					"\tClos Enable: Specify priority type with [--priority|-p]\n");
21223fb4f7cdSSrinivas Pandruvada 				fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
2123fe6fb216SSrinivas Pandruvada 			}
2124ce1326a2SPrarit Bhargava 		} else {
2125ce1326a2SPrarit Bhargava 			fprintf(stderr,
2126ce1326a2SPrarit Bhargava 				"Disable core-power: [No command arguments are required]\n");
2127ce1326a2SPrarit Bhargava 		}
21283fb4f7cdSSrinivas Pandruvada 		exit(0);
21293fb4f7cdSSrinivas Pandruvada 	}
21303fb4f7cdSSrinivas Pandruvada 
2131ce1326a2SPrarit Bhargava 	if (enable && cpufreq_sysfs_present()) {
21323fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21333fb4f7cdSSrinivas Pandruvada 			"cpufreq subsystem and core-power enable will interfere with each other!\n");
21343fb4f7cdSSrinivas Pandruvada 	}
21353fb4f7cdSSrinivas Pandruvada 
21363fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
21373fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21383fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
2139ce1326a2SPrarit Bhargava 						  NULL, NULL, &enable);
21403fb4f7cdSSrinivas Pandruvada 	else
21413fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(enable_clos_qos_config, NULL,
2142ce1326a2SPrarit Bhargava 					       NULL, NULL, &enable);
21433fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
21443fb4f7cdSSrinivas Pandruvada }
21453fb4f7cdSSrinivas Pandruvada 
2146850337ecSZhang Rui static void dump_clos_config_for_cpu(struct isst_id *id, void *arg1, void *arg2,
21473fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
21483fb4f7cdSSrinivas Pandruvada {
21493fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
21503fb4f7cdSSrinivas Pandruvada 	int ret;
21513fb4f7cdSSrinivas Pandruvada 
2152850337ecSZhang Rui 	ret = isst_pm_get_clos(id, current_clos, &clos_config);
21533fb4f7cdSSrinivas Pandruvada 	if (ret)
2154fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
21553fb4f7cdSSrinivas Pandruvada 	else
2156850337ecSZhang Rui 		isst_clos_display_information(id, outf, current_clos,
21573fb4f7cdSSrinivas Pandruvada 					      &clos_config);
21583fb4f7cdSSrinivas Pandruvada }
21593fb4f7cdSSrinivas Pandruvada 
2160ce1326a2SPrarit Bhargava static void dump_clos_config(int arg)
21613fb4f7cdSSrinivas Pandruvada {
21623fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
21633fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21643fb4f7cdSSrinivas Pandruvada 			"Print Intel Speed Select Technology core power configuration\n");
21653fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21663fb4f7cdSSrinivas Pandruvada 			"\tArguments: [-c | --clos]: Specify clos id\n");
21673fb4f7cdSSrinivas Pandruvada 		exit(0);
21683fb4f7cdSSrinivas Pandruvada 	}
21693fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2170fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2171fe6fb216SSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
21723fb4f7cdSSrinivas Pandruvada 		exit(0);
21733fb4f7cdSSrinivas Pandruvada 	}
21743fb4f7cdSSrinivas Pandruvada 
21753fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
21763fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21773fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
21783fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
21793fb4f7cdSSrinivas Pandruvada 	else
21803fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
21813fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
21823fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
21833fb4f7cdSSrinivas Pandruvada }
21843fb4f7cdSSrinivas Pandruvada 
2185850337ecSZhang Rui static void get_clos_info_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
2186188afed9SSrinivas Pandruvada 				  void *arg4)
2187188afed9SSrinivas Pandruvada {
2188188afed9SSrinivas Pandruvada 	int enable, ret, prio_type;
2189188afed9SSrinivas Pandruvada 
2190850337ecSZhang Rui 	ret = isst_clos_get_clos_information(id, &enable, &prio_type);
2191188afed9SSrinivas Pandruvada 	if (ret)
2192fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_clos_get_info failed", 0, 0);
2193143ad322SSrinivas Pandruvada 	else {
2194143ad322SSrinivas Pandruvada 		int cp_state, cp_cap;
2195143ad322SSrinivas Pandruvada 
2196850337ecSZhang Rui 		isst_read_pm_config(id, &cp_state, &cp_cap);
2197850337ecSZhang Rui 		isst_clos_display_clos_information(id, outf, enable, prio_type,
2198143ad322SSrinivas Pandruvada 						   cp_state, cp_cap);
2199143ad322SSrinivas Pandruvada 	}
2200188afed9SSrinivas Pandruvada }
2201188afed9SSrinivas Pandruvada 
2202ce1326a2SPrarit Bhargava static void dump_clos_info(int arg)
2203188afed9SSrinivas Pandruvada {
2204188afed9SSrinivas Pandruvada 	if (cmd_help) {
2205188afed9SSrinivas Pandruvada 		fprintf(stderr,
2206188afed9SSrinivas Pandruvada 			"Print Intel Speed Select Technology core power information\n");
2207f5205f49SSrinivas Pandruvada 		fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2208188afed9SSrinivas Pandruvada 		exit(0);
2209188afed9SSrinivas Pandruvada 	}
2210188afed9SSrinivas Pandruvada 
2211188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2212f5205f49SSrinivas Pandruvada 	if (max_target_cpus)
2213188afed9SSrinivas Pandruvada 		for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2214188afed9SSrinivas Pandruvada 						  NULL, NULL, NULL);
2215f5205f49SSrinivas Pandruvada 	else
2216f5205f49SSrinivas Pandruvada 		for_each_online_package_in_set(get_clos_info_for_cpu, NULL,
2217f5205f49SSrinivas Pandruvada 					       NULL, NULL, NULL);
2218188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
2219188afed9SSrinivas Pandruvada 
2220188afed9SSrinivas Pandruvada }
2221188afed9SSrinivas Pandruvada 
2222850337ecSZhang Rui static void set_clos_config_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
22233fb4f7cdSSrinivas Pandruvada 				    void *arg4)
22243fb4f7cdSSrinivas Pandruvada {
22253fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
22263fb4f7cdSSrinivas Pandruvada 	int ret;
22273fb4f7cdSSrinivas Pandruvada 
22283fb4f7cdSSrinivas Pandruvada 	clos_config.epp = clos_epp;
22293fb4f7cdSSrinivas Pandruvada 	clos_config.clos_prop_prio = clos_prop_prio;
22303fb4f7cdSSrinivas Pandruvada 	clos_config.clos_min = clos_min;
22313fb4f7cdSSrinivas Pandruvada 	clos_config.clos_max = clos_max;
22323fb4f7cdSSrinivas Pandruvada 	clos_config.clos_desired = clos_desired;
2233850337ecSZhang Rui 	ret = isst_set_clos(id, current_clos, &clos_config);
22343fb4f7cdSSrinivas Pandruvada 	if (ret)
2235fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
22363fb4f7cdSSrinivas Pandruvada 	else
2237850337ecSZhang Rui 		isst_display_result(id, outf, "core-power", "config", ret);
22383fb4f7cdSSrinivas Pandruvada }
22393fb4f7cdSSrinivas Pandruvada 
2240ce1326a2SPrarit Bhargava static void set_clos_config(int arg)
22413fb4f7cdSSrinivas Pandruvada {
22423fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
22433fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
22443fb4f7cdSSrinivas Pandruvada 			"Set core-power configuration for one of the four clos ids\n");
22453fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
22463fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
2247fe6fb216SSrinivas Pandruvada 		if (!is_skx_based_platform()) {
22483fb4f7cdSSrinivas Pandruvada 			fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
22493fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
22503fb4f7cdSSrinivas Pandruvada 				"\tSpecify clos Proportional Priority [--weight|-w]\n");
2251fe6fb216SSrinivas Pandruvada 		}
225240dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
225340dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
22543fb4f7cdSSrinivas Pandruvada 		exit(0);
22553fb4f7cdSSrinivas Pandruvada 	}
22563fb4f7cdSSrinivas Pandruvada 
22573fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2258fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
22593fb4f7cdSSrinivas Pandruvada 		exit(0);
22603fb4f7cdSSrinivas Pandruvada 	}
2261fe6fb216SSrinivas Pandruvada 	if (!is_skx_based_platform() && (clos_epp < 0 || clos_epp > 0x0F)) {
2262fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos epp is not specified or invalid, default: 0\n");
22633fb4f7cdSSrinivas Pandruvada 		clos_epp = 0;
22643fb4f7cdSSrinivas Pandruvada 	}
2265fe6fb216SSrinivas Pandruvada 	if (!is_skx_based_platform() && (clos_prop_prio < 0 || clos_prop_prio > 0x0F)) {
22663fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
2267fe6fb216SSrinivas Pandruvada 			"clos frequency weight is not specified or invalid, default: 0\n");
22683fb4f7cdSSrinivas Pandruvada 		clos_prop_prio = 0;
22693fb4f7cdSSrinivas Pandruvada 	}
22703fb4f7cdSSrinivas Pandruvada 	if (clos_min < 0) {
22713fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "clos min is not specified, default: 0\n");
22723fb4f7cdSSrinivas Pandruvada 		clos_min = 0;
22733fb4f7cdSSrinivas Pandruvada 	}
22743fb4f7cdSSrinivas Pandruvada 	if (clos_max < 0) {
2275fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos max is not specified, default: Max frequency (ratio 0xff)\n");
22763fb4f7cdSSrinivas Pandruvada 		clos_max = 0xff;
22773fb4f7cdSSrinivas Pandruvada 	}
2278fe6fb216SSrinivas Pandruvada 	if (clos_desired) {
2279fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos desired is not supported on this platform\n");
22803fb4f7cdSSrinivas Pandruvada 		clos_desired = 0x00;
22813fb4f7cdSSrinivas Pandruvada 	}
22823fb4f7cdSSrinivas Pandruvada 
22833fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
22843fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
22853fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
22863fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
22873fb4f7cdSSrinivas Pandruvada 	else
22883fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
22893fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
22903fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
22913fb4f7cdSSrinivas Pandruvada }
22923fb4f7cdSSrinivas Pandruvada 
2293850337ecSZhang Rui static void set_clos_assoc_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
22943fb4f7cdSSrinivas Pandruvada 				   void *arg4)
22953fb4f7cdSSrinivas Pandruvada {
22963fb4f7cdSSrinivas Pandruvada 	int ret;
22973fb4f7cdSSrinivas Pandruvada 
2298850337ecSZhang Rui 	ret = isst_clos_associate(id, current_clos);
22993fb4f7cdSSrinivas Pandruvada 	if (ret)
2300fe6fb216SSrinivas Pandruvada 		debug_printf("isst_clos_associate failed");
23013fb4f7cdSSrinivas Pandruvada 	else
2302850337ecSZhang Rui 		isst_display_result(id, outf, "core-power", "assoc", ret);
23033fb4f7cdSSrinivas Pandruvada }
23043fb4f7cdSSrinivas Pandruvada 
2305ce1326a2SPrarit Bhargava static void set_clos_assoc(int arg)
23063fb4f7cdSSrinivas Pandruvada {
23073fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
23083fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Associate a clos id to a CPU\n");
23093fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
23103fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
231168e2f109SSrinivas Pandruvada 		fprintf(stderr,
231268e2f109SSrinivas Pandruvada 			"\tFor example to associate clos 1 to CPU 0: issue\n");
231368e2f109SSrinivas Pandruvada 		fprintf(stderr,
231468e2f109SSrinivas Pandruvada 			"\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
23153fb4f7cdSSrinivas Pandruvada 		exit(0);
23163fb4f7cdSSrinivas Pandruvada 	}
23173fb4f7cdSSrinivas Pandruvada 
23183fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2319fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
23203fb4f7cdSSrinivas Pandruvada 		exit(0);
23213fb4f7cdSSrinivas Pandruvada 	}
23223fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
23233fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
23243fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
23253fb4f7cdSSrinivas Pandruvada 	else {
2326fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
23273fb4f7cdSSrinivas Pandruvada 	}
23283fb4f7cdSSrinivas Pandruvada }
23293fb4f7cdSSrinivas Pandruvada 
2330850337ecSZhang Rui static void get_clos_assoc_for_cpu(struct isst_id *id, void *arg1, void *arg2, void *arg3,
23313fb4f7cdSSrinivas Pandruvada 				   void *arg4)
23323fb4f7cdSSrinivas Pandruvada {
23333fb4f7cdSSrinivas Pandruvada 	int clos, ret;
23343fb4f7cdSSrinivas Pandruvada 
2335850337ecSZhang Rui 	ret = isst_clos_get_assoc_status(id, &clos);
23363fb4f7cdSSrinivas Pandruvada 	if (ret)
2337fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_clos_get_assoc_status failed", 0, 0);
23383fb4f7cdSSrinivas Pandruvada 	else
2339850337ecSZhang Rui 		isst_clos_display_assoc_information(id, outf, clos);
23403fb4f7cdSSrinivas Pandruvada }
23413fb4f7cdSSrinivas Pandruvada 
2342ce1326a2SPrarit Bhargava static void get_clos_assoc(int arg)
23433fb4f7cdSSrinivas Pandruvada {
23443fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
23453fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Get associate clos id to a CPU\n");
23463fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
23473fb4f7cdSSrinivas Pandruvada 		exit(0);
23483fb4f7cdSSrinivas Pandruvada 	}
2349e118fbe3SSrinivas Pandruvada 
2350e118fbe3SSrinivas Pandruvada 	if (!max_target_cpus) {
2351fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2352e118fbe3SSrinivas Pandruvada 		exit(0);
23533fb4f7cdSSrinivas Pandruvada 	}
2354e118fbe3SSrinivas Pandruvada 
2355e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2356e118fbe3SSrinivas Pandruvada 	for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2357e118fbe3SSrinivas Pandruvada 					  NULL, NULL, NULL);
2358e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
23593fb4f7cdSSrinivas Pandruvada }
23603fb4f7cdSSrinivas Pandruvada 
2361850337ecSZhang Rui static void set_turbo_mode_for_cpu(struct isst_id *id, int status)
2362006050a6SSrinivas Pandruvada {
2363006050a6SSrinivas Pandruvada 	int base_freq;
2364006050a6SSrinivas Pandruvada 
2365006050a6SSrinivas Pandruvada 	if (status) {
2366850337ecSZhang Rui 		base_freq = get_cpufreq_base_freq(id->cpu);
2367850337ecSZhang Rui 		set_cpufreq_scaling_min_max(id->cpu, 1, base_freq);
2368006050a6SSrinivas Pandruvada 	} else {
2369850337ecSZhang Rui 		set_scaling_max_to_cpuinfo_max(id);
2370006050a6SSrinivas Pandruvada 	}
2371006050a6SSrinivas Pandruvada 
2372006050a6SSrinivas Pandruvada 	if (status) {
2373850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-mode", "enable", 0);
2374006050a6SSrinivas Pandruvada 	} else {
2375850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-mode", "disable", 0);
2376006050a6SSrinivas Pandruvada 	}
2377006050a6SSrinivas Pandruvada }
2378006050a6SSrinivas Pandruvada 
2379006050a6SSrinivas Pandruvada static void set_turbo_mode(int arg)
2380006050a6SSrinivas Pandruvada {
2381006050a6SSrinivas Pandruvada 	int i, enable = arg;
2382850337ecSZhang Rui 	struct isst_id id;
2383006050a6SSrinivas Pandruvada 
2384006050a6SSrinivas Pandruvada 	if (cmd_help) {
2385006050a6SSrinivas Pandruvada 		if (enable)
2386006050a6SSrinivas Pandruvada 			fprintf(stderr, "Set turbo mode enable\n");
2387006050a6SSrinivas Pandruvada 		else
2388006050a6SSrinivas Pandruvada 			fprintf(stderr, "Set turbo mode disable\n");
2389006050a6SSrinivas Pandruvada 		exit(0);
2390006050a6SSrinivas Pandruvada 	}
2391006050a6SSrinivas Pandruvada 
2392006050a6SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2393006050a6SSrinivas Pandruvada 
2394006050a6SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
2395006050a6SSrinivas Pandruvada 		int online;
2396006050a6SSrinivas Pandruvada 
2397006050a6SSrinivas Pandruvada 		if (i)
2398006050a6SSrinivas Pandruvada 			online = parse_int_file(
2399006050a6SSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
2400006050a6SSrinivas Pandruvada 		else
2401006050a6SSrinivas Pandruvada 			online =
2402006050a6SSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
2403006050a6SSrinivas Pandruvada 
2404850337ecSZhang Rui 		if (online) {
2405850337ecSZhang Rui 			set_isst_id(&id, i);
2406850337ecSZhang Rui 			set_turbo_mode_for_cpu(&id, enable);
2407850337ecSZhang Rui 		}
2408006050a6SSrinivas Pandruvada 
2409006050a6SSrinivas Pandruvada 	}
2410006050a6SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
2411006050a6SSrinivas Pandruvada }
2412006050a6SSrinivas Pandruvada 
2413850337ecSZhang Rui static void get_set_trl(struct isst_id *id, void *arg1, void *arg2, void *arg3,
24142c7dc57eSSrinivas Pandruvada 			void *arg4)
24152c7dc57eSSrinivas Pandruvada {
24162c7dc57eSSrinivas Pandruvada 	unsigned long long trl;
24172c7dc57eSSrinivas Pandruvada 	int set = *(int *)arg4;
24182c7dc57eSSrinivas Pandruvada 	int ret;
24192c7dc57eSSrinivas Pandruvada 
24202c7dc57eSSrinivas Pandruvada 	if (set && !fact_trl) {
24212c7dc57eSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid TRL. Specify with [-t|--trl]", 0, 0);
24222c7dc57eSSrinivas Pandruvada 		exit(0);
24232c7dc57eSSrinivas Pandruvada 	}
24242c7dc57eSSrinivas Pandruvada 
24252c7dc57eSSrinivas Pandruvada 	if (set) {
2426850337ecSZhang Rui 		ret = isst_set_trl(id, fact_trl);
2427850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-mode", "set-trl", ret);
24282c7dc57eSSrinivas Pandruvada 		return;
24292c7dc57eSSrinivas Pandruvada 	}
24302c7dc57eSSrinivas Pandruvada 
2431850337ecSZhang Rui 	ret = isst_get_trl(id, &trl);
24322c7dc57eSSrinivas Pandruvada 	if (ret)
2433850337ecSZhang Rui 		isst_display_result(id, outf, "turbo-mode", "get-trl", ret);
24342c7dc57eSSrinivas Pandruvada 	else
2435850337ecSZhang Rui 		isst_trl_display_information(id, outf, trl);
24362c7dc57eSSrinivas Pandruvada }
24372c7dc57eSSrinivas Pandruvada 
24382c7dc57eSSrinivas Pandruvada static void process_trl(int arg)
24392c7dc57eSSrinivas Pandruvada {
24402c7dc57eSSrinivas Pandruvada 	if (cmd_help) {
24412c7dc57eSSrinivas Pandruvada 		if (arg) {
24422c7dc57eSSrinivas Pandruvada 			fprintf(stderr, "Set TRL (turbo ratio limits)\n");
24432c7dc57eSSrinivas Pandruvada 			fprintf(stderr, "\t t|--trl: Specify turbo ratio limit for setting TRL\n");
24442c7dc57eSSrinivas Pandruvada 		} else {
24452c7dc57eSSrinivas Pandruvada 			fprintf(stderr, "Get TRL (turbo ratio limits)\n");
24462c7dc57eSSrinivas Pandruvada 		}
24472c7dc57eSSrinivas Pandruvada 		exit(0);
24482c7dc57eSSrinivas Pandruvada 	}
24492c7dc57eSSrinivas Pandruvada 
24502c7dc57eSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
24512c7dc57eSSrinivas Pandruvada 	if (max_target_cpus)
24522c7dc57eSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(get_set_trl, NULL,
24532c7dc57eSSrinivas Pandruvada 						  NULL, NULL, &arg);
24542c7dc57eSSrinivas Pandruvada 	else
24552c7dc57eSSrinivas Pandruvada 		for_each_online_package_in_set(get_set_trl, NULL,
24562c7dc57eSSrinivas Pandruvada 					       NULL, NULL, &arg);
24572c7dc57eSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
24582c7dc57eSSrinivas Pandruvada }
24592c7dc57eSSrinivas Pandruvada 
2460c829f0efSPrarit Bhargava static struct process_cmd_struct clx_n_cmds[] = {
2461062e4aacSPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
24621aa7177cSPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
24631aa7177cSPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
24641aa7177cSPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2465c829f0efSPrarit Bhargava 	{ NULL, NULL, NULL, 0 }
2466c829f0efSPrarit Bhargava };
2467c829f0efSPrarit Bhargava 
24683fb4f7cdSSrinivas Pandruvada static struct process_cmd_struct isst_cmds[] = {
2469ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2470ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2471ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-version", get_tdp_version, 0 },
2472ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2473ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-current-level", get_tdp_current_level,
2474ce1326a2SPrarit Bhargava 	 0 },
2475ce1326a2SPrarit Bhargava 	{ "perf-profile", "set-config-level", set_tdp_level, 0 },
2476ce1326a2SPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
2477ce1326a2SPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
2478ce1326a2SPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
2479ce1326a2SPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2480ce1326a2SPrarit Bhargava 	{ "turbo-freq", "info", dump_fact_config, 0 },
2481ce1326a2SPrarit Bhargava 	{ "turbo-freq", "enable", set_fact_enable, 1 },
2482ce1326a2SPrarit Bhargava 	{ "turbo-freq", "disable", set_fact_enable, 0 },
2483ce1326a2SPrarit Bhargava 	{ "core-power", "info", dump_clos_info, 0 },
2484ce1326a2SPrarit Bhargava 	{ "core-power", "enable", set_clos_enable, 1 },
2485ce1326a2SPrarit Bhargava 	{ "core-power", "disable", set_clos_enable, 0 },
2486ce1326a2SPrarit Bhargava 	{ "core-power", "config", set_clos_config, 0 },
2487ce1326a2SPrarit Bhargava 	{ "core-power", "get-config", dump_clos_config, 0 },
2488ce1326a2SPrarit Bhargava 	{ "core-power", "assoc", set_clos_assoc, 0 },
2489ce1326a2SPrarit Bhargava 	{ "core-power", "get-assoc", get_clos_assoc, 0 },
2490006050a6SSrinivas Pandruvada 	{ "turbo-mode", "enable", set_turbo_mode, 0 },
2491006050a6SSrinivas Pandruvada 	{ "turbo-mode", "disable", set_turbo_mode, 1 },
24922c7dc57eSSrinivas Pandruvada 	{ "turbo-mode", "get-trl", process_trl, 0 },
24932c7dc57eSSrinivas Pandruvada 	{ "turbo-mode", "set-trl", process_trl, 1 },
24943fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL, NULL }
24953fb4f7cdSSrinivas Pandruvada };
24963fb4f7cdSSrinivas Pandruvada 
24973fb4f7cdSSrinivas Pandruvada /*
24983fb4f7cdSSrinivas Pandruvada  * parse cpuset with following syntax
24993fb4f7cdSSrinivas Pandruvada  * 1,2,4..6,8-10 and set bits in cpu_subset
25003fb4f7cdSSrinivas Pandruvada  */
25013fb4f7cdSSrinivas Pandruvada void parse_cpu_command(char *optarg)
25023fb4f7cdSSrinivas Pandruvada {
25033fb4f7cdSSrinivas Pandruvada 	unsigned int start, end;
25043fb4f7cdSSrinivas Pandruvada 	char *next;
25053fb4f7cdSSrinivas Pandruvada 
25063fb4f7cdSSrinivas Pandruvada 	next = optarg;
25073fb4f7cdSSrinivas Pandruvada 
25083fb4f7cdSSrinivas Pandruvada 	while (next && *next) {
25093fb4f7cdSSrinivas Pandruvada 		if (*next == '-') /* no negative cpu numbers */
25103fb4f7cdSSrinivas Pandruvada 			goto error;
25113fb4f7cdSSrinivas Pandruvada 
25123fb4f7cdSSrinivas Pandruvada 		start = strtoul(next, &next, 10);
25133fb4f7cdSSrinivas Pandruvada 
25143fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
25153fb4f7cdSSrinivas Pandruvada 			target_cpus[max_target_cpus++] = start;
25163fb4f7cdSSrinivas Pandruvada 
25173fb4f7cdSSrinivas Pandruvada 		if (*next == '\0')
25183fb4f7cdSSrinivas Pandruvada 			break;
25193fb4f7cdSSrinivas Pandruvada 
25203fb4f7cdSSrinivas Pandruvada 		if (*next == ',') {
25213fb4f7cdSSrinivas Pandruvada 			next += 1;
25223fb4f7cdSSrinivas Pandruvada 			continue;
25233fb4f7cdSSrinivas Pandruvada 		}
25243fb4f7cdSSrinivas Pandruvada 
25253fb4f7cdSSrinivas Pandruvada 		if (*next == '-') {
25263fb4f7cdSSrinivas Pandruvada 			next += 1; /* start range */
25273fb4f7cdSSrinivas Pandruvada 		} else if (*next == '.') {
25283fb4f7cdSSrinivas Pandruvada 			next += 1;
25293fb4f7cdSSrinivas Pandruvada 			if (*next == '.')
25303fb4f7cdSSrinivas Pandruvada 				next += 1; /* start range */
25313fb4f7cdSSrinivas Pandruvada 			else
25323fb4f7cdSSrinivas Pandruvada 				goto error;
25333fb4f7cdSSrinivas Pandruvada 		}
25343fb4f7cdSSrinivas Pandruvada 
25353fb4f7cdSSrinivas Pandruvada 		end = strtoul(next, &next, 10);
25363fb4f7cdSSrinivas Pandruvada 		if (end <= start)
25373fb4f7cdSSrinivas Pandruvada 			goto error;
25383fb4f7cdSSrinivas Pandruvada 
25393fb4f7cdSSrinivas Pandruvada 		while (++start <= end) {
25403fb4f7cdSSrinivas Pandruvada 			if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
25413fb4f7cdSSrinivas Pandruvada 				target_cpus[max_target_cpus++] = start;
25423fb4f7cdSSrinivas Pandruvada 		}
25433fb4f7cdSSrinivas Pandruvada 
25443fb4f7cdSSrinivas Pandruvada 		if (*next == ',')
25453fb4f7cdSSrinivas Pandruvada 			next += 1;
25463fb4f7cdSSrinivas Pandruvada 		else if (*next != '\0')
25473fb4f7cdSSrinivas Pandruvada 			goto error;
25483fb4f7cdSSrinivas Pandruvada 	}
25493fb4f7cdSSrinivas Pandruvada 
25503fb4f7cdSSrinivas Pandruvada #ifdef DEBUG
25513fb4f7cdSSrinivas Pandruvada 	{
25523fb4f7cdSSrinivas Pandruvada 		int i;
25533fb4f7cdSSrinivas Pandruvada 
25543fb4f7cdSSrinivas Pandruvada 		for (i = 0; i < max_target_cpus; ++i)
25553fb4f7cdSSrinivas Pandruvada 			printf("cpu [%d] in arg\n", target_cpus[i]);
25563fb4f7cdSSrinivas Pandruvada 	}
25573fb4f7cdSSrinivas Pandruvada #endif
25583fb4f7cdSSrinivas Pandruvada 	return;
25593fb4f7cdSSrinivas Pandruvada 
25603fb4f7cdSSrinivas Pandruvada error:
25613fb4f7cdSSrinivas Pandruvada 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
25623fb4f7cdSSrinivas Pandruvada 	exit(-1);
25633fb4f7cdSSrinivas Pandruvada }
25643fb4f7cdSSrinivas Pandruvada 
25653fb4f7cdSSrinivas Pandruvada static void parse_cmd_args(int argc, int start, char **argv)
25663fb4f7cdSSrinivas Pandruvada {
25673fb4f7cdSSrinivas Pandruvada 	int opt;
25683fb4f7cdSSrinivas Pandruvada 	int option_index;
25693fb4f7cdSSrinivas Pandruvada 
25703fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
25713fb4f7cdSSrinivas Pandruvada 		{ "bucket", required_argument, 0, 'b' },
25723fb4f7cdSSrinivas Pandruvada 		{ "level", required_argument, 0, 'l' },
25733c64c81aSSrinivas Pandruvada 		{ "online", required_argument, 0, 'o' },
25743fb4f7cdSSrinivas Pandruvada 		{ "trl-type", required_argument, 0, 'r' },
25753fb4f7cdSSrinivas Pandruvada 		{ "trl", required_argument, 0, 't' },
25763fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
25773fb4f7cdSSrinivas Pandruvada 		{ "clos", required_argument, 0, 'c' },
25783fb4f7cdSSrinivas Pandruvada 		{ "desired", required_argument, 0, 'd' },
25793fb4f7cdSSrinivas Pandruvada 		{ "epp", required_argument, 0, 'e' },
25803fb4f7cdSSrinivas Pandruvada 		{ "min", required_argument, 0, 'n' },
25813fb4f7cdSSrinivas Pandruvada 		{ "max", required_argument, 0, 'm' },
25823fb4f7cdSSrinivas Pandruvada 		{ "priority", required_argument, 0, 'p' },
25833fb4f7cdSSrinivas Pandruvada 		{ "weight", required_argument, 0, 'w' },
2584354bd06fSSrinivas Pandruvada 		{ "auto", no_argument, 0, 'a' },
25853fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
25863fb4f7cdSSrinivas Pandruvada 	};
25873fb4f7cdSSrinivas Pandruvada 
25883fb4f7cdSSrinivas Pandruvada 	option_index = start;
25893fb4f7cdSSrinivas Pandruvada 
25903fb4f7cdSSrinivas Pandruvada 	optind = start + 1;
2591b86639e1SSrinivas Pandruvada 	while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:r:hoa",
25923fb4f7cdSSrinivas Pandruvada 				  long_options, &option_index)) != -1) {
25933fb4f7cdSSrinivas Pandruvada 		switch (opt) {
2594354bd06fSSrinivas Pandruvada 		case 'a':
2595354bd06fSSrinivas Pandruvada 			auto_mode = 1;
2596354bd06fSSrinivas Pandruvada 			break;
25973fb4f7cdSSrinivas Pandruvada 		case 'b':
25983fb4f7cdSSrinivas Pandruvada 			fact_bucket = atoi(optarg);
25993fb4f7cdSSrinivas Pandruvada 			break;
26003fb4f7cdSSrinivas Pandruvada 		case 'h':
26013fb4f7cdSSrinivas Pandruvada 			cmd_help = 1;
26023fb4f7cdSSrinivas Pandruvada 			break;
26033fb4f7cdSSrinivas Pandruvada 		case 'l':
26043fb4f7cdSSrinivas Pandruvada 			tdp_level = atoi(optarg);
26053fb4f7cdSSrinivas Pandruvada 			break;
26063c64c81aSSrinivas Pandruvada 		case 'o':
26073c64c81aSSrinivas Pandruvada 			force_online_offline = 1;
26083c64c81aSSrinivas Pandruvada 			break;
26093fb4f7cdSSrinivas Pandruvada 		case 't':
26103fb4f7cdSSrinivas Pandruvada 			sscanf(optarg, "0x%llx", &fact_trl);
26113fb4f7cdSSrinivas Pandruvada 			break;
26123fb4f7cdSSrinivas Pandruvada 		case 'r':
26133fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "sse", 3)) {
26143fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x01;
26153fb4f7cdSSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx2", 4)) {
26163fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x02;
2617b86639e1SSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx512", 6)) {
26183fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x04;
26193fb4f7cdSSrinivas Pandruvada 			} else {
26203fb4f7cdSSrinivas Pandruvada 				fprintf(outf, "Invalid sse,avx options\n");
26213fb4f7cdSSrinivas Pandruvada 				exit(1);
26223fb4f7cdSSrinivas Pandruvada 			}
26233fb4f7cdSSrinivas Pandruvada 			break;
26243fb4f7cdSSrinivas Pandruvada 		/* CLOS related */
26253fb4f7cdSSrinivas Pandruvada 		case 'c':
26263fb4f7cdSSrinivas Pandruvada 			current_clos = atoi(optarg);
26273fb4f7cdSSrinivas Pandruvada 			break;
26283fb4f7cdSSrinivas Pandruvada 		case 'd':
26293fb4f7cdSSrinivas Pandruvada 			clos_desired = atoi(optarg);
263040dee9ddSSrinivas Pandruvada 			clos_desired /= DISP_FREQ_MULTIPLIER;
26313fb4f7cdSSrinivas Pandruvada 			break;
26323fb4f7cdSSrinivas Pandruvada 		case 'e':
26333fb4f7cdSSrinivas Pandruvada 			clos_epp = atoi(optarg);
2634fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform()) {
2635fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "epp can't be specified on this platform", 0, 0);
2636fe6fb216SSrinivas Pandruvada 				exit(0);
2637fe6fb216SSrinivas Pandruvada 			}
26383fb4f7cdSSrinivas Pandruvada 			break;
26393fb4f7cdSSrinivas Pandruvada 		case 'n':
26403fb4f7cdSSrinivas Pandruvada 			clos_min = atoi(optarg);
264140dee9ddSSrinivas Pandruvada 			clos_min /= DISP_FREQ_MULTIPLIER;
26423fb4f7cdSSrinivas Pandruvada 			break;
26433fb4f7cdSSrinivas Pandruvada 		case 'm':
26443fb4f7cdSSrinivas Pandruvada 			clos_max = atoi(optarg);
264540dee9ddSSrinivas Pandruvada 			clos_max /= DISP_FREQ_MULTIPLIER;
26463fb4f7cdSSrinivas Pandruvada 			break;
26473fb4f7cdSSrinivas Pandruvada 		case 'p':
26483fb4f7cdSSrinivas Pandruvada 			clos_priority_type = atoi(optarg);
2649fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform() && !clos_priority_type) {
2650fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "Invalid clos priority type: proportional for this platform", 0, 0);
2651fe6fb216SSrinivas Pandruvada 				exit(0);
2652fe6fb216SSrinivas Pandruvada 			}
26533fb4f7cdSSrinivas Pandruvada 			break;
26543fb4f7cdSSrinivas Pandruvada 		case 'w':
26553fb4f7cdSSrinivas Pandruvada 			clos_prop_prio = atoi(optarg);
2656fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform()) {
2657fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "weight can't be specified on this platform", 0, 0);
2658fe6fb216SSrinivas Pandruvada 				exit(0);
2659fe6fb216SSrinivas Pandruvada 			}
26603fb4f7cdSSrinivas Pandruvada 			break;
26613fb4f7cdSSrinivas Pandruvada 		default:
26624a960353SSrinivas Pandruvada 			printf("Unknown option: ignore\n");
26633fb4f7cdSSrinivas Pandruvada 		}
26643fb4f7cdSSrinivas Pandruvada 	}
26654a960353SSrinivas Pandruvada 
26664a960353SSrinivas Pandruvada 	if (argv[optind])
26674a960353SSrinivas Pandruvada 		printf("Garbage at the end of command: ignore\n");
26683fb4f7cdSSrinivas Pandruvada }
26693fb4f7cdSSrinivas Pandruvada 
26703fb4f7cdSSrinivas Pandruvada static void isst_help(void)
26713fb4f7cdSSrinivas Pandruvada {
26723fb4f7cdSSrinivas Pandruvada 	printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
26733fb4f7cdSSrinivas Pandruvada 		performance profiles per system via static and/or dynamic\n\
26743fb4f7cdSSrinivas Pandruvada 		adjustment of core count, workload, Tjmax, and\n\
26753fb4f7cdSSrinivas Pandruvada 		TDP, etc.\n");
26763fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=perf-profile\n");
26773fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
2678c829f0efSPrarit Bhargava 
2679c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
26803fb4f7cdSSrinivas Pandruvada 		printf("\tget-lock-status\n");
26813fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-levels\n");
26823fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-version\n");
26833fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-enabled\n");
26843fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-current-level\n");
26853fb4f7cdSSrinivas Pandruvada 		printf("\tset-config-level\n");
26863fb4f7cdSSrinivas Pandruvada 	}
2687c829f0efSPrarit Bhargava }
26883fb4f7cdSSrinivas Pandruvada 
26893fb4f7cdSSrinivas Pandruvada static void pbf_help(void)
26903fb4f7cdSSrinivas Pandruvada {
26913fb4f7cdSSrinivas Pandruvada 	printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
26923fb4f7cdSSrinivas Pandruvada 		on certain cores (high priority cores) in exchange for lower\n\
26933fb4f7cdSSrinivas Pandruvada 		base frequency on remaining cores (low priority cores).\n");
26943fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
26953fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
26963fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
26973fb4f7cdSSrinivas Pandruvada }
26983fb4f7cdSSrinivas Pandruvada 
26993fb4f7cdSSrinivas Pandruvada static void fact_help(void)
27003fb4f7cdSSrinivas Pandruvada {
27013fb4f7cdSSrinivas Pandruvada 	printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
27023fb4f7cdSSrinivas Pandruvada 		limits to cores based on priority.\n");
27033fb4f7cdSSrinivas Pandruvada 	printf("\nCommand: For feature=turbo-freq\n");
27043fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
27053fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
27063fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
27073fb4f7cdSSrinivas Pandruvada }
27083fb4f7cdSSrinivas Pandruvada 
2709006050a6SSrinivas Pandruvada static void turbo_mode_help(void)
2710006050a6SSrinivas Pandruvada {
27112c7dc57eSSrinivas 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");
2712006050a6SSrinivas Pandruvada 	printf("\tcommand : enable\n");
2713006050a6SSrinivas Pandruvada 	printf("\tcommand : disable\n");
27142c7dc57eSSrinivas Pandruvada 	printf("\tcommand : get-trl\n");
27152c7dc57eSSrinivas Pandruvada 	printf("\tcommand : set-trl\n");
2716006050a6SSrinivas Pandruvada }
2717006050a6SSrinivas Pandruvada 
2718006050a6SSrinivas Pandruvada 
27193fb4f7cdSSrinivas Pandruvada static void core_power_help(void)
27203fb4f7cdSSrinivas Pandruvada {
27213fb4f7cdSSrinivas Pandruvada 	printf("core-power:\tInterface that allows user to define per core/tile\n\
27223fb4f7cdSSrinivas Pandruvada 		priority.\n");
27233fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=core-power\n");
27243fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
27253fb4f7cdSSrinivas Pandruvada 	printf("\tenable\n");
27263fb4f7cdSSrinivas Pandruvada 	printf("\tdisable\n");
27273fb4f7cdSSrinivas Pandruvada 	printf("\tconfig\n");
2728188afed9SSrinivas Pandruvada 	printf("\tget-config\n");
27293fb4f7cdSSrinivas Pandruvada 	printf("\tassoc\n");
27303fb4f7cdSSrinivas Pandruvada 	printf("\tget-assoc\n");
27313fb4f7cdSSrinivas Pandruvada }
27323fb4f7cdSSrinivas Pandruvada 
27333fb4f7cdSSrinivas Pandruvada struct process_cmd_help_struct {
27343fb4f7cdSSrinivas Pandruvada 	char *feature;
27353fb4f7cdSSrinivas Pandruvada 	void (*process_fn)(void);
27363fb4f7cdSSrinivas Pandruvada };
27373fb4f7cdSSrinivas Pandruvada 
27383fb4f7cdSSrinivas Pandruvada static struct process_cmd_help_struct isst_help_cmds[] = {
27393fb4f7cdSSrinivas Pandruvada 	{ "perf-profile", isst_help },
27403fb4f7cdSSrinivas Pandruvada 	{ "base-freq", pbf_help },
27413fb4f7cdSSrinivas Pandruvada 	{ "turbo-freq", fact_help },
27423fb4f7cdSSrinivas Pandruvada 	{ "core-power", core_power_help },
2743006050a6SSrinivas Pandruvada 	{ "turbo-mode", turbo_mode_help },
27443fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL }
27453fb4f7cdSSrinivas Pandruvada };
27463fb4f7cdSSrinivas Pandruvada 
2747c829f0efSPrarit Bhargava static struct process_cmd_help_struct clx_n_help_cmds[] = {
2748c829f0efSPrarit Bhargava 	{ "perf-profile", isst_help },
2749c829f0efSPrarit Bhargava 	{ "base-freq", pbf_help },
2750c829f0efSPrarit Bhargava 	{ NULL, NULL }
2751c829f0efSPrarit Bhargava };
2752c829f0efSPrarit Bhargava 
2753210369dcSPrarit Bhargava void process_command(int argc, char **argv,
2754210369dcSPrarit Bhargava 		     struct process_cmd_help_struct *help_cmds,
2755210369dcSPrarit Bhargava 		     struct process_cmd_struct *cmds)
27563fb4f7cdSSrinivas Pandruvada {
27573fb4f7cdSSrinivas Pandruvada 	int i = 0, matched = 0;
27583fb4f7cdSSrinivas Pandruvada 	char *feature = argv[optind];
27593fb4f7cdSSrinivas Pandruvada 	char *cmd = argv[optind + 1];
27603fb4f7cdSSrinivas Pandruvada 
27613fb4f7cdSSrinivas Pandruvada 	if (!feature || !cmd)
27623fb4f7cdSSrinivas Pandruvada 		return;
27633fb4f7cdSSrinivas Pandruvada 
27643fb4f7cdSSrinivas Pandruvada 	debug_printf("feature name [%s] command [%s]\n", feature, cmd);
27653fb4f7cdSSrinivas Pandruvada 	if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
2766210369dcSPrarit Bhargava 		while (help_cmds[i].feature) {
2767210369dcSPrarit Bhargava 			if (!strcmp(help_cmds[i].feature, feature)) {
2768210369dcSPrarit Bhargava 				help_cmds[i].process_fn();
27693fb4f7cdSSrinivas Pandruvada 				exit(0);
27703fb4f7cdSSrinivas Pandruvada 			}
27713fb4f7cdSSrinivas Pandruvada 			++i;
27723fb4f7cdSSrinivas Pandruvada 		}
27733fb4f7cdSSrinivas Pandruvada 	}
27743fb4f7cdSSrinivas Pandruvada 
27753fb4f7cdSSrinivas Pandruvada 	i = 0;
2776210369dcSPrarit Bhargava 	while (cmds[i].feature) {
2777210369dcSPrarit Bhargava 		if (!strcmp(cmds[i].feature, feature) &&
2778210369dcSPrarit Bhargava 		    !strcmp(cmds[i].command, cmd)) {
27793fb4f7cdSSrinivas Pandruvada 			parse_cmd_args(argc, optind + 1, argv);
2780210369dcSPrarit Bhargava 			cmds[i].process_fn(cmds[i].arg);
27813fb4f7cdSSrinivas Pandruvada 			matched = 1;
27823fb4f7cdSSrinivas Pandruvada 			break;
27833fb4f7cdSSrinivas Pandruvada 		}
27843fb4f7cdSSrinivas Pandruvada 		++i;
27853fb4f7cdSSrinivas Pandruvada 	}
27863fb4f7cdSSrinivas Pandruvada 
27873fb4f7cdSSrinivas Pandruvada 	if (!matched)
27883fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Invalid command\n");
27893fb4f7cdSSrinivas Pandruvada }
27903fb4f7cdSSrinivas Pandruvada 
27913fb4f7cdSSrinivas Pandruvada static void usage(void)
27923fb4f7cdSSrinivas Pandruvada {
2793addd116dSSrinivas Pandruvada 	if (is_clx_n_platform()) {
2794addd116dSSrinivas Pandruvada 		fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
2795addd116dSSrinivas Pandruvada 		fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
2796addd116dSSrinivas Pandruvada 	}
2797addd116dSSrinivas Pandruvada 
27983fb4f7cdSSrinivas Pandruvada 	printf("\nUsage:\n");
27993fb4f7cdSSrinivas Pandruvada 	printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
2800addd116dSSrinivas Pandruvada 	printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
2801addd116dSSrinivas Pandruvada 	if (is_clx_n_platform())
2802addd116dSSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq]\n");
2803addd116dSSrinivas Pandruvada 	else
2804006050a6SSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power|turbo-mode]\n");
280543774c0dSPrarit Bhargava 	printf("\nFor help on each feature, use -h|--help\n");
28063fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile -h\n");
28073fb4f7cdSSrinivas Pandruvada 
28083fb4f7cdSSrinivas Pandruvada 	printf("\nFor additional help on each command for a feature, use --h|--help\n");
28093fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
28103fb4f7cdSSrinivas Pandruvada 	printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
28113fb4f7cdSSrinivas Pandruvada 
28123fb4f7cdSSrinivas Pandruvada 	printf("\nOPTIONS\n");
28133fb4f7cdSSrinivas Pandruvada 	printf("\t[-c|--cpu] : logical cpu number\n");
28143fb4f7cdSSrinivas Pandruvada 	printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
28153fb4f7cdSSrinivas Pandruvada 	printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
28163fb4f7cdSSrinivas Pandruvada 	printf("\t[-d|--debug] : Debug mode\n");
2817addd116dSSrinivas Pandruvada 	printf("\t[-f|--format] : output format [json|text]. Default: text\n");
28183fb4f7cdSSrinivas Pandruvada 	printf("\t[-h|--help] : Print help\n");
28193fb4f7cdSSrinivas Pandruvada 	printf("\t[-i|--info] : Print platform information\n");
28200d3dfd75SSrinivas Pandruvada 	printf("\t[-a|--all-cpus-online] : Force online every CPU in the system\n");
28213fb4f7cdSSrinivas Pandruvada 	printf("\t[-o|--out] : Output file\n");
28223fb4f7cdSSrinivas Pandruvada 	printf("\t\t\tDefault : stderr\n");
2823a85a35fcSSrinivas Pandruvada 	printf("\t[-p|--pause] : Delay between two mail box commands in milliseconds\n");
282432279be7SSrinivas Pandruvada 	printf("\t[-r|--retry] : Retry count for mail box commands on failure, default 3\n");
28253fb4f7cdSSrinivas Pandruvada 	printf("\t[-v|--version] : Print version\n");
28267fd786dfSSrinivas Pandruvada 	printf("\t[-b|--oob : Start a daemon to process HFI events for perf profile change from Out of Band agent.\n");
28277fd786dfSSrinivas Pandruvada 	printf("\t[-n|--no-daemon : Don't run as daemon. By default --oob will turn on daemon mode\n");
28287fd786dfSSrinivas Pandruvada 	printf("\t[-w|--delay : Delay for reading config level state change in OOB poll mode.\n");
28293fb4f7cdSSrinivas Pandruvada 	printf("\nResult format\n");
28303fb4f7cdSSrinivas Pandruvada 	printf("\tResult display uses a common format for each command:\n");
28313fb4f7cdSSrinivas Pandruvada 	printf("\tResults are formatted in text/JSON with\n");
28323fb4f7cdSSrinivas Pandruvada 	printf("\t\tPackage, Die, CPU, and command specific results.\n");
2833addd116dSSrinivas Pandruvada 
2834addd116dSSrinivas Pandruvada 	printf("\nExamples\n");
2835addd116dSSrinivas Pandruvada 	printf("\tTo get platform information:\n");
2836addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select --info\n");
2837addd116dSSrinivas Pandruvada 	printf("\tTo get full perf-profile information dump:\n");
2838addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select perf-profile info\n");
2839addd116dSSrinivas Pandruvada 	printf("\tTo get full base-freq information dump:\n");
2840addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select base-freq info -l 0\n");
2841addd116dSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2842addd116dSSrinivas Pandruvada 		printf("\tTo get full turbo-freq information dump:\n");
2843addd116dSSrinivas Pandruvada 		printf("\t\tintel-speed-select turbo-freq info -l 0\n");
2844addd116dSSrinivas Pandruvada 	}
28453fb4f7cdSSrinivas Pandruvada 	exit(1);
28463fb4f7cdSSrinivas Pandruvada }
28473fb4f7cdSSrinivas Pandruvada 
28483fb4f7cdSSrinivas Pandruvada static void print_version(void)
28493fb4f7cdSSrinivas Pandruvada {
28503fb4f7cdSSrinivas Pandruvada 	fprintf(outf, "Version %s\n", version_str);
28513fb4f7cdSSrinivas Pandruvada 	exit(0);
28523fb4f7cdSSrinivas Pandruvada }
28533fb4f7cdSSrinivas Pandruvada 
28543fb4f7cdSSrinivas Pandruvada static void cmdline(int argc, char **argv)
28553fb4f7cdSSrinivas Pandruvada {
2856f362cdccSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
2857a85a35fcSSrinivas Pandruvada 	char *ptr;
2858f362cdccSSrinivas Pandruvada 	FILE *fp;
28590d3dfd75SSrinivas Pandruvada 	int opt, force_cpus_online = 0;
28603fb4f7cdSSrinivas Pandruvada 	int option_index = 0;
28613bc3d30cSPrarit Bhargava 	int ret;
28627fd786dfSSrinivas Pandruvada 	int oob_mode = 0;
28637fd786dfSSrinivas Pandruvada 	int poll_interval = -1;
28647fd786dfSSrinivas Pandruvada 	int no_daemon = 0;
28653fb4f7cdSSrinivas Pandruvada 
28663fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
28670d3dfd75SSrinivas Pandruvada 		{ "all-cpus-online", no_argument, 0, 'a' },
28683fb4f7cdSSrinivas Pandruvada 		{ "cpu", required_argument, 0, 'c' },
28693fb4f7cdSSrinivas Pandruvada 		{ "debug", no_argument, 0, 'd' },
28703fb4f7cdSSrinivas Pandruvada 		{ "format", required_argument, 0, 'f' },
28713fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
28723fb4f7cdSSrinivas Pandruvada 		{ "info", no_argument, 0, 'i' },
2873a85a35fcSSrinivas Pandruvada 		{ "pause", required_argument, 0, 'p' },
28743fb4f7cdSSrinivas Pandruvada 		{ "out", required_argument, 0, 'o' },
287532279be7SSrinivas Pandruvada 		{ "retry", required_argument, 0, 'r' },
28763fb4f7cdSSrinivas Pandruvada 		{ "version", no_argument, 0, 'v' },
28777fd786dfSSrinivas Pandruvada 		{ "oob", no_argument, 0, 'b' },
28787fd786dfSSrinivas Pandruvada 		{ "no-daemon", no_argument, 0, 'n' },
28797fd786dfSSrinivas Pandruvada 		{ "poll-interval", required_argument, 0, 'w' },
28803fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
28813fb4f7cdSSrinivas Pandruvada 	};
28823fb4f7cdSSrinivas Pandruvada 
2883f362cdccSSrinivas Pandruvada 	if (geteuid() != 0) {
2884f362cdccSSrinivas Pandruvada 		fprintf(stderr, "Must run as root\n");
2885f362cdccSSrinivas Pandruvada 		exit(0);
2886f362cdccSSrinivas Pandruvada 	}
2887f362cdccSSrinivas Pandruvada 
2888f362cdccSSrinivas Pandruvada 	ret = update_cpu_model();
2889f362cdccSSrinivas Pandruvada 	if (ret)
2890f362cdccSSrinivas Pandruvada 		err(-1, "Invalid CPU model (%d)\n", cpu_model);
2891f362cdccSSrinivas Pandruvada 	printf("Intel(R) Speed Select Technology\n");
2892f362cdccSSrinivas Pandruvada 	printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2893f362cdccSSrinivas Pandruvada 
2894f362cdccSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2895f362cdccSSrinivas Pandruvada 		fp = fopen(pathname, "rb");
2896f362cdccSSrinivas Pandruvada 		if (!fp) {
2897f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
2898f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
2899f362cdccSSrinivas Pandruvada 			fprintf(stderr, "If the config is included then this is not a supported platform.\n");
2900f362cdccSSrinivas Pandruvada 			exit(0);
2901f362cdccSSrinivas Pandruvada 		}
2902f362cdccSSrinivas Pandruvada 		fclose(fp);
2903f362cdccSSrinivas Pandruvada 	}
2904f362cdccSSrinivas Pandruvada 
29053fb4f7cdSSrinivas Pandruvada 	progname = argv[0];
29067fd786dfSSrinivas Pandruvada 	while ((opt = getopt_long_only(argc, argv, "+c:df:hio:vabw:n", long_options,
29073fb4f7cdSSrinivas Pandruvada 				       &option_index)) != -1) {
29083fb4f7cdSSrinivas Pandruvada 		switch (opt) {
29090d3dfd75SSrinivas Pandruvada 		case 'a':
29100d3dfd75SSrinivas Pandruvada 			force_cpus_online = 1;
29110d3dfd75SSrinivas Pandruvada 			break;
29123fb4f7cdSSrinivas Pandruvada 		case 'c':
29133fb4f7cdSSrinivas Pandruvada 			parse_cpu_command(optarg);
29143fb4f7cdSSrinivas Pandruvada 			break;
29153fb4f7cdSSrinivas Pandruvada 		case 'd':
29163fb4f7cdSSrinivas Pandruvada 			debug_flag = 1;
29173fb4f7cdSSrinivas Pandruvada 			printf("Debug Mode ON\n");
29183fb4f7cdSSrinivas Pandruvada 			break;
29193fb4f7cdSSrinivas Pandruvada 		case 'f':
29203fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "json", 4))
29213fb4f7cdSSrinivas Pandruvada 				out_format_json = 1;
29223fb4f7cdSSrinivas Pandruvada 			break;
29233fb4f7cdSSrinivas Pandruvada 		case 'h':
29243fb4f7cdSSrinivas Pandruvada 			usage();
29253fb4f7cdSSrinivas Pandruvada 			break;
29263fb4f7cdSSrinivas Pandruvada 		case 'i':
29273fb4f7cdSSrinivas Pandruvada 			isst_print_platform_information();
29283fb4f7cdSSrinivas Pandruvada 			break;
29293fb4f7cdSSrinivas Pandruvada 		case 'o':
29303fb4f7cdSSrinivas Pandruvada 			if (outf)
29313fb4f7cdSSrinivas Pandruvada 				fclose(outf);
29323fb4f7cdSSrinivas Pandruvada 			outf = fopen_or_exit(optarg, "w");
29333fb4f7cdSSrinivas Pandruvada 			break;
2934a85a35fcSSrinivas Pandruvada 		case 'p':
2935a85a35fcSSrinivas Pandruvada 			ret = strtol(optarg, &ptr, 10);
2936a85a35fcSSrinivas Pandruvada 			if (!ret)
2937a85a35fcSSrinivas Pandruvada 				fprintf(stderr, "Invalid pause interval, ignore\n");
2938a85a35fcSSrinivas Pandruvada 			else
2939a85a35fcSSrinivas Pandruvada 				mbox_delay = ret;
2940a85a35fcSSrinivas Pandruvada 			break;
294132279be7SSrinivas Pandruvada 		case 'r':
294232279be7SSrinivas Pandruvada 			ret = strtol(optarg, &ptr, 10);
294332279be7SSrinivas Pandruvada 			if (!ret)
294432279be7SSrinivas Pandruvada 				fprintf(stderr, "Invalid retry count, ignore\n");
294532279be7SSrinivas Pandruvada 			else
294632279be7SSrinivas Pandruvada 				mbox_retries = ret;
294732279be7SSrinivas Pandruvada 			break;
29483fb4f7cdSSrinivas Pandruvada 		case 'v':
29493fb4f7cdSSrinivas Pandruvada 			print_version();
29503fb4f7cdSSrinivas Pandruvada 			break;
29517fd786dfSSrinivas Pandruvada 		case 'b':
29527fd786dfSSrinivas Pandruvada 			oob_mode = 1;
29537fd786dfSSrinivas Pandruvada 			break;
29547fd786dfSSrinivas Pandruvada 		case 'n':
29557fd786dfSSrinivas Pandruvada 			no_daemon = 1;
29567fd786dfSSrinivas Pandruvada 			break;
29577fd786dfSSrinivas Pandruvada 		case 'w':
29587fd786dfSSrinivas Pandruvada 			ret = strtol(optarg, &ptr, 10);
29597fd786dfSSrinivas Pandruvada 			if (!ret) {
29607fd786dfSSrinivas Pandruvada 				fprintf(stderr, "Invalid poll interval count\n");
29617fd786dfSSrinivas Pandruvada 				exit(0);
29627fd786dfSSrinivas Pandruvada 			}
29637fd786dfSSrinivas Pandruvada 			poll_interval = ret;
29647fd786dfSSrinivas Pandruvada 			break;
29653fb4f7cdSSrinivas Pandruvada 		default:
29663fb4f7cdSSrinivas Pandruvada 			usage();
29673fb4f7cdSSrinivas Pandruvada 		}
29683fb4f7cdSSrinivas Pandruvada 	}
29693fb4f7cdSSrinivas Pandruvada 
29707fd786dfSSrinivas Pandruvada 	if (optind > (argc - 2) && !oob_mode) {
2971addd116dSSrinivas Pandruvada 		usage();
29723fb4f7cdSSrinivas Pandruvada 		exit(0);
29733fb4f7cdSSrinivas Pandruvada 	}
29743fb4f7cdSSrinivas Pandruvada 	set_max_cpu_num();
29750d3dfd75SSrinivas Pandruvada 	if (force_cpus_online)
29760d3dfd75SSrinivas Pandruvada 		force_all_cpus_online();
2977fb186158SSrinivas Pandruvada 	store_cpu_topology();
29783fb4f7cdSSrinivas Pandruvada 	set_cpu_present_cpu_mask();
29793fb4f7cdSSrinivas Pandruvada 	set_cpu_target_cpu_mask();
2980*ca56725dSZhang Rui 	create_cpu_map();
2981c829f0efSPrarit Bhargava 
29827fd786dfSSrinivas Pandruvada 	if (oob_mode) {
29837fd786dfSSrinivas Pandruvada 		if (debug_flag)
29847fd786dfSSrinivas Pandruvada 			fprintf(stderr, "OOB mode is enabled in debug mode\n");
29857fd786dfSSrinivas Pandruvada 
29867fd786dfSSrinivas Pandruvada 		ret = isst_daemon(debug_flag, poll_interval, no_daemon);
29877fd786dfSSrinivas Pandruvada 		if (ret)
29887fd786dfSSrinivas Pandruvada 			fprintf(stderr, "OOB mode enable failed\n");
29897fd786dfSSrinivas Pandruvada 		goto out;
29907fd786dfSSrinivas Pandruvada 	}
29917fd786dfSSrinivas Pandruvada 
2992c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
29933bc3d30cSPrarit Bhargava 		ret = isst_fill_platform_info();
29943bc3d30cSPrarit Bhargava 		if (ret)
29953bc3d30cSPrarit Bhargava 			goto out;
2996210369dcSPrarit Bhargava 		process_command(argc, argv, isst_help_cmds, isst_cmds);
2997c829f0efSPrarit Bhargava 	} else {
2998c829f0efSPrarit Bhargava 		process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
2999c829f0efSPrarit Bhargava 	}
30003bc3d30cSPrarit Bhargava out:
30013bc3d30cSPrarit Bhargava 	free_cpu_set(present_cpumask);
30023bc3d30cSPrarit Bhargava 	free_cpu_set(target_cpumask);
30033fb4f7cdSSrinivas Pandruvada }
30043fb4f7cdSSrinivas Pandruvada 
30053fb4f7cdSSrinivas Pandruvada int main(int argc, char **argv)
30063fb4f7cdSSrinivas Pandruvada {
30073fb4f7cdSSrinivas Pandruvada 	outf = stderr;
30083fb4f7cdSSrinivas Pandruvada 	cmdline(argc, argv);
30093fb4f7cdSSrinivas Pandruvada 	return 0;
30103fb4f7cdSSrinivas Pandruvada }
3011