xref: /linux/tools/power/x86/intel-speed-select/isst-config.c (revision 070fdea13d4ba3ba6ae5a239fe334947921a1b9f)
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 
1819be0b2aSSrinivas Pandruvada static const char *version_str = "v1.2";
193fb4f7cdSSrinivas Pandruvada static const int supported_api_ver = 1;
203fb4f7cdSSrinivas Pandruvada static struct isst_if_platform_info isst_platform_info;
213fb4f7cdSSrinivas Pandruvada static char *progname;
223fb4f7cdSSrinivas Pandruvada static int debug_flag;
233fb4f7cdSSrinivas Pandruvada static FILE *outf;
243fb4f7cdSSrinivas Pandruvada 
253fb4f7cdSSrinivas Pandruvada static int cpu_model;
261c1d935cSPrarit Bhargava static int cpu_stepping;
273fb4f7cdSSrinivas Pandruvada 
283fb4f7cdSSrinivas Pandruvada #define MAX_CPUS_IN_ONE_REQ 64
293fb4f7cdSSrinivas Pandruvada static short max_target_cpus;
303fb4f7cdSSrinivas Pandruvada static unsigned short target_cpus[MAX_CPUS_IN_ONE_REQ];
313fb4f7cdSSrinivas Pandruvada 
323fb4f7cdSSrinivas Pandruvada static int topo_max_cpus;
333fb4f7cdSSrinivas Pandruvada static size_t present_cpumask_size;
343fb4f7cdSSrinivas Pandruvada static cpu_set_t *present_cpumask;
353fb4f7cdSSrinivas Pandruvada static size_t target_cpumask_size;
363fb4f7cdSSrinivas Pandruvada static cpu_set_t *target_cpumask;
373fb4f7cdSSrinivas Pandruvada static int tdp_level = 0xFF;
383fb4f7cdSSrinivas Pandruvada static int fact_bucket = 0xFF;
393fb4f7cdSSrinivas Pandruvada static int fact_avx = 0xFF;
403fb4f7cdSSrinivas Pandruvada static unsigned long long fact_trl;
413fb4f7cdSSrinivas Pandruvada static int out_format_json;
423fb4f7cdSSrinivas Pandruvada static int cmd_help;
433c64c81aSSrinivas Pandruvada static int force_online_offline;
44354bd06fSSrinivas Pandruvada static int auto_mode;
4514a8aa49SSrinivas Pandruvada static int fact_enable_fail;
463fb4f7cdSSrinivas Pandruvada 
473fb4f7cdSSrinivas Pandruvada /* clos related */
483fb4f7cdSSrinivas Pandruvada static int current_clos = -1;
493fb4f7cdSSrinivas Pandruvada static int clos_epp = -1;
503fb4f7cdSSrinivas Pandruvada static int clos_prop_prio = -1;
513fb4f7cdSSrinivas Pandruvada static int clos_min = -1;
523fb4f7cdSSrinivas Pandruvada static int clos_max = -1;
533fb4f7cdSSrinivas Pandruvada static int clos_desired = -1;
543fb4f7cdSSrinivas Pandruvada static int clos_priority_type;
553fb4f7cdSSrinivas Pandruvada 
563fb4f7cdSSrinivas Pandruvada struct _cpu_map {
573fb4f7cdSSrinivas Pandruvada 	unsigned short core_id;
583fb4f7cdSSrinivas Pandruvada 	unsigned short pkg_id;
593fb4f7cdSSrinivas Pandruvada 	unsigned short die_id;
603fb4f7cdSSrinivas Pandruvada 	unsigned short punit_cpu;
613fb4f7cdSSrinivas Pandruvada 	unsigned short punit_cpu_core;
623fb4f7cdSSrinivas Pandruvada };
633fb4f7cdSSrinivas Pandruvada struct _cpu_map *cpu_map;
643fb4f7cdSSrinivas Pandruvada 
65fb186158SSrinivas Pandruvada struct cpu_topology {
66fb186158SSrinivas Pandruvada 	short cpu;
67fb186158SSrinivas Pandruvada 	short core_id;
68fb186158SSrinivas Pandruvada 	short pkg_id;
69fb186158SSrinivas Pandruvada 	short die_id;
70fb186158SSrinivas Pandruvada };
71fb186158SSrinivas Pandruvada 
7287e115b3SSrinivas Pandruvada FILE *get_output_file(void)
7387e115b3SSrinivas Pandruvada {
7487e115b3SSrinivas Pandruvada 	return outf;
7587e115b3SSrinivas Pandruvada }
7687e115b3SSrinivas Pandruvada 
773fb4f7cdSSrinivas Pandruvada void debug_printf(const char *format, ...)
783fb4f7cdSSrinivas Pandruvada {
793fb4f7cdSSrinivas Pandruvada 	va_list args;
803fb4f7cdSSrinivas Pandruvada 
813fb4f7cdSSrinivas Pandruvada 	va_start(args, format);
823fb4f7cdSSrinivas Pandruvada 
833fb4f7cdSSrinivas Pandruvada 	if (debug_flag)
843fb4f7cdSSrinivas Pandruvada 		vprintf(format, args);
853fb4f7cdSSrinivas Pandruvada 
863fb4f7cdSSrinivas Pandruvada 	va_end(args);
873fb4f7cdSSrinivas Pandruvada }
883fb4f7cdSSrinivas Pandruvada 
891c1d935cSPrarit Bhargava 
901c1d935cSPrarit Bhargava int is_clx_n_platform(void)
911c1d935cSPrarit Bhargava {
921c1d935cSPrarit Bhargava 	if (cpu_model == 0x55)
931c1d935cSPrarit Bhargava 		if (cpu_stepping == 0x6 || cpu_stepping == 0x7)
941c1d935cSPrarit Bhargava 			return 1;
951c1d935cSPrarit Bhargava 	return 0;
961c1d935cSPrarit Bhargava }
971c1d935cSPrarit Bhargava 
9895f8e569SSrinivas Pandruvada int is_skx_based_platform(void)
9995f8e569SSrinivas Pandruvada {
10095f8e569SSrinivas Pandruvada 	if (cpu_model == 0x55)
10195f8e569SSrinivas Pandruvada 		return 1;
10295f8e569SSrinivas Pandruvada 
10395f8e569SSrinivas Pandruvada 	return 0;
10495f8e569SSrinivas Pandruvada }
10595f8e569SSrinivas Pandruvada 
1061c1d935cSPrarit Bhargava static int update_cpu_model(void)
1073fb4f7cdSSrinivas Pandruvada {
1083fb4f7cdSSrinivas Pandruvada 	unsigned int ebx, ecx, edx;
1093fb4f7cdSSrinivas Pandruvada 	unsigned int fms, family;
1103fb4f7cdSSrinivas Pandruvada 
1113fb4f7cdSSrinivas Pandruvada 	__cpuid(1, fms, ebx, ecx, edx);
1123fb4f7cdSSrinivas Pandruvada 	family = (fms >> 8) & 0xf;
1133fb4f7cdSSrinivas Pandruvada 	cpu_model = (fms >> 4) & 0xf;
1143fb4f7cdSSrinivas Pandruvada 	if (family == 6 || family == 0xf)
1153fb4f7cdSSrinivas Pandruvada 		cpu_model += ((fms >> 16) & 0xf) << 4;
1161c1d935cSPrarit Bhargava 
1171c1d935cSPrarit Bhargava 	cpu_stepping = fms & 0xf;
1181c1d935cSPrarit Bhargava 	/* only three CascadeLake-N models are supported */
1191c1d935cSPrarit Bhargava 	if (is_clx_n_platform()) {
1201c1d935cSPrarit Bhargava 		FILE *fp;
1211c1d935cSPrarit Bhargava 		size_t n = 0;
1221c1d935cSPrarit Bhargava 		char *line = NULL;
1231c1d935cSPrarit Bhargava 		int ret = 1;
1241c1d935cSPrarit Bhargava 
1251c1d935cSPrarit Bhargava 		fp = fopen("/proc/cpuinfo", "r");
1261c1d935cSPrarit Bhargava 		if (!fp)
1271c1d935cSPrarit Bhargava 			err(-1, "cannot open /proc/cpuinfo\n");
1281c1d935cSPrarit Bhargava 
1291c1d935cSPrarit Bhargava 		while (getline(&line, &n, fp) > 0) {
1301c1d935cSPrarit Bhargava 			if (strstr(line, "model name")) {
1311c1d935cSPrarit Bhargava 				if (strstr(line, "6252N") ||
1321c1d935cSPrarit Bhargava 				    strstr(line, "6230N") ||
1331c1d935cSPrarit Bhargava 				    strstr(line, "5218N"))
1341c1d935cSPrarit Bhargava 					ret = 0;
1351c1d935cSPrarit Bhargava 				break;
1361c1d935cSPrarit Bhargava 			}
1371c1d935cSPrarit Bhargava 		}
1381c1d935cSPrarit Bhargava 		free(line);
1391c1d935cSPrarit Bhargava 		fclose(fp);
1401c1d935cSPrarit Bhargava 		return ret;
1411c1d935cSPrarit Bhargava 	}
1421c1d935cSPrarit Bhargava 	return 0;
1433fb4f7cdSSrinivas Pandruvada }
1443fb4f7cdSSrinivas Pandruvada 
1453fb4f7cdSSrinivas Pandruvada /* Open a file, and exit on failure */
1463fb4f7cdSSrinivas Pandruvada static FILE *fopen_or_exit(const char *path, const char *mode)
1473fb4f7cdSSrinivas Pandruvada {
1483fb4f7cdSSrinivas Pandruvada 	FILE *filep = fopen(path, mode);
1493fb4f7cdSSrinivas Pandruvada 
1503fb4f7cdSSrinivas Pandruvada 	if (!filep)
1513fb4f7cdSSrinivas Pandruvada 		err(1, "%s: open failed", path);
1523fb4f7cdSSrinivas Pandruvada 
1533fb4f7cdSSrinivas Pandruvada 	return filep;
1543fb4f7cdSSrinivas Pandruvada }
1553fb4f7cdSSrinivas Pandruvada 
1563fb4f7cdSSrinivas Pandruvada /* Parse a file containing a single int */
1573fb4f7cdSSrinivas Pandruvada static int parse_int_file(int fatal, const char *fmt, ...)
1583fb4f7cdSSrinivas Pandruvada {
1593fb4f7cdSSrinivas Pandruvada 	va_list args;
1603fb4f7cdSSrinivas Pandruvada 	char path[PATH_MAX];
1613fb4f7cdSSrinivas Pandruvada 	FILE *filep;
1623fb4f7cdSSrinivas Pandruvada 	int value;
1633fb4f7cdSSrinivas Pandruvada 
1643fb4f7cdSSrinivas Pandruvada 	va_start(args, fmt);
1653fb4f7cdSSrinivas Pandruvada 	vsnprintf(path, sizeof(path), fmt, args);
1663fb4f7cdSSrinivas Pandruvada 	va_end(args);
1673fb4f7cdSSrinivas Pandruvada 	if (fatal) {
1683fb4f7cdSSrinivas Pandruvada 		filep = fopen_or_exit(path, "r");
1693fb4f7cdSSrinivas Pandruvada 	} else {
1703fb4f7cdSSrinivas Pandruvada 		filep = fopen(path, "r");
1713fb4f7cdSSrinivas Pandruvada 		if (!filep)
1723fb4f7cdSSrinivas Pandruvada 			return -1;
1733fb4f7cdSSrinivas Pandruvada 	}
1743fb4f7cdSSrinivas Pandruvada 	if (fscanf(filep, "%d", &value) != 1)
1753fb4f7cdSSrinivas Pandruvada 		err(1, "%s: failed to parse number from file", path);
1763fb4f7cdSSrinivas Pandruvada 	fclose(filep);
1773fb4f7cdSSrinivas Pandruvada 
1783fb4f7cdSSrinivas Pandruvada 	return value;
1793fb4f7cdSSrinivas Pandruvada }
1803fb4f7cdSSrinivas Pandruvada 
1813fb4f7cdSSrinivas Pandruvada int cpufreq_sysfs_present(void)
1823fb4f7cdSSrinivas Pandruvada {
1833fb4f7cdSSrinivas Pandruvada 	DIR *dir;
1843fb4f7cdSSrinivas Pandruvada 
1853fb4f7cdSSrinivas Pandruvada 	dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
1863fb4f7cdSSrinivas Pandruvada 	if (dir) {
1873fb4f7cdSSrinivas Pandruvada 		closedir(dir);
1883fb4f7cdSSrinivas Pandruvada 		return 1;
1893fb4f7cdSSrinivas Pandruvada 	}
1903fb4f7cdSSrinivas Pandruvada 
1913fb4f7cdSSrinivas Pandruvada 	return 0;
1923fb4f7cdSSrinivas Pandruvada }
1933fb4f7cdSSrinivas Pandruvada 
1943fb4f7cdSSrinivas Pandruvada int out_format_is_json(void)
1953fb4f7cdSSrinivas Pandruvada {
1963fb4f7cdSSrinivas Pandruvada 	return out_format_json;
1973fb4f7cdSSrinivas Pandruvada }
1983fb4f7cdSSrinivas Pandruvada 
199fb186158SSrinivas Pandruvada static int get_stored_topology_info(int cpu, int *core_id, int *pkg_id, int *die_id)
200fb186158SSrinivas Pandruvada {
201fb186158SSrinivas Pandruvada 	const char *pathname = "/tmp/isst_cpu_topology.dat";
202fb186158SSrinivas Pandruvada 	struct cpu_topology cpu_top;
203fb186158SSrinivas Pandruvada 	FILE *fp;
204fb186158SSrinivas Pandruvada 	int ret;
205fb186158SSrinivas Pandruvada 
206fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "rb");
207fb186158SSrinivas Pandruvada 	if (!fp)
208fb186158SSrinivas Pandruvada 		return -1;
209fb186158SSrinivas Pandruvada 
210fb186158SSrinivas Pandruvada 	ret = fseek(fp, cpu * sizeof(cpu_top), SEEK_SET);
211fb186158SSrinivas Pandruvada 	if (ret)
212fb186158SSrinivas Pandruvada 		goto err_ret;
213fb186158SSrinivas Pandruvada 
214fb186158SSrinivas Pandruvada 	ret = fread(&cpu_top, sizeof(cpu_top), 1, fp);
215fb186158SSrinivas Pandruvada 	if (ret != 1) {
216fb186158SSrinivas Pandruvada 		ret = -1;
217fb186158SSrinivas Pandruvada 		goto err_ret;
218fb186158SSrinivas Pandruvada 	}
219fb186158SSrinivas Pandruvada 
220fb186158SSrinivas Pandruvada 	*pkg_id = cpu_top.pkg_id;
221fb186158SSrinivas Pandruvada 	*core_id = cpu_top.core_id;
222fb186158SSrinivas Pandruvada 	*die_id = cpu_top.die_id;
223fb186158SSrinivas Pandruvada 	ret = 0;
224fb186158SSrinivas Pandruvada 
225fb186158SSrinivas Pandruvada err_ret:
226fb186158SSrinivas Pandruvada 	fclose(fp);
227fb186158SSrinivas Pandruvada 
228fb186158SSrinivas Pandruvada 	return ret;
229fb186158SSrinivas Pandruvada }
230fb186158SSrinivas Pandruvada 
231fb186158SSrinivas Pandruvada static void store_cpu_topology(void)
232fb186158SSrinivas Pandruvada {
233fb186158SSrinivas Pandruvada 	const char *pathname = "/tmp/isst_cpu_topology.dat";
234fb186158SSrinivas Pandruvada 	FILE *fp;
235fb186158SSrinivas Pandruvada 	int i;
236fb186158SSrinivas Pandruvada 
237fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "rb");
238fb186158SSrinivas Pandruvada 	if (fp) {
239fb186158SSrinivas Pandruvada 		/* Mapping already exists */
240fb186158SSrinivas Pandruvada 		fclose(fp);
241fb186158SSrinivas Pandruvada 		return;
242fb186158SSrinivas Pandruvada 	}
243fb186158SSrinivas Pandruvada 
244fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "wb");
245fb186158SSrinivas Pandruvada 	if (!fp) {
246fb186158SSrinivas Pandruvada 		fprintf(stderr, "Can't create file:%s\n", pathname);
247fb186158SSrinivas Pandruvada 		return;
248fb186158SSrinivas Pandruvada 	}
249fb186158SSrinivas Pandruvada 
250fb186158SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
251fb186158SSrinivas Pandruvada 		struct cpu_topology cpu_top;
252fb186158SSrinivas Pandruvada 
253fb186158SSrinivas Pandruvada 		cpu_top.core_id = parse_int_file(0,
254fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/core_id", i);
255fb186158SSrinivas Pandruvada 		if (cpu_top.core_id < 0)
256fb186158SSrinivas Pandruvada 			cpu_top.core_id = -1;
257fb186158SSrinivas Pandruvada 
258fb186158SSrinivas Pandruvada 		cpu_top.pkg_id = parse_int_file(0,
259fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
260fb186158SSrinivas Pandruvada 		if (cpu_top.pkg_id < 0)
261fb186158SSrinivas Pandruvada 			cpu_top.pkg_id = -1;
262fb186158SSrinivas Pandruvada 
263fb186158SSrinivas Pandruvada 		cpu_top.die_id = parse_int_file(0,
264fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/die_id", i);
265fb186158SSrinivas Pandruvada 		if (cpu_top.die_id < 0)
266fb186158SSrinivas Pandruvada 			cpu_top.die_id = -1;
267fb186158SSrinivas Pandruvada 
268fb186158SSrinivas Pandruvada 		cpu_top.cpu = i;
269fb186158SSrinivas Pandruvada 
270fb186158SSrinivas Pandruvada 		if (fwrite(&cpu_top, sizeof(cpu_top), 1, fp) != 1) {
271fb186158SSrinivas Pandruvada 			fprintf(stderr, "Can't write to:%s\n", pathname);
272fb186158SSrinivas Pandruvada 			break;
273fb186158SSrinivas Pandruvada 		}
274fb186158SSrinivas Pandruvada 	}
275fb186158SSrinivas Pandruvada 
276fb186158SSrinivas Pandruvada 	fclose(fp);
277fb186158SSrinivas Pandruvada }
278fb186158SSrinivas Pandruvada 
2793fb4f7cdSSrinivas Pandruvada int get_physical_package_id(int cpu)
2803fb4f7cdSSrinivas Pandruvada {
281fb186158SSrinivas Pandruvada 	int ret;
282fb186158SSrinivas Pandruvada 
283fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
284fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
2853fb4f7cdSSrinivas Pandruvada 			cpu);
286fb186158SSrinivas Pandruvada 	if (ret < 0) {
287fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
288fb186158SSrinivas Pandruvada 
289fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
290fb186158SSrinivas Pandruvada 		if (!ret)
291fb186158SSrinivas Pandruvada 			return pkg_id;
292fb186158SSrinivas Pandruvada 	}
293fb186158SSrinivas Pandruvada 
294fb186158SSrinivas Pandruvada 	return ret;
2953fb4f7cdSSrinivas Pandruvada }
2963fb4f7cdSSrinivas Pandruvada 
2973fb4f7cdSSrinivas Pandruvada int get_physical_core_id(int cpu)
2983fb4f7cdSSrinivas Pandruvada {
299fb186158SSrinivas Pandruvada 	int ret;
300fb186158SSrinivas Pandruvada 
301fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
302fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/core_id",
303fb186158SSrinivas Pandruvada 			cpu);
304fb186158SSrinivas Pandruvada 	if (ret < 0) {
305fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
306fb186158SSrinivas Pandruvada 
307fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
308fb186158SSrinivas Pandruvada 		if (!ret)
309fb186158SSrinivas Pandruvada 			return core_id;
310fb186158SSrinivas Pandruvada 	}
311fb186158SSrinivas Pandruvada 
312fb186158SSrinivas Pandruvada 	return ret;
3133fb4f7cdSSrinivas Pandruvada }
3143fb4f7cdSSrinivas Pandruvada 
3153fb4f7cdSSrinivas Pandruvada int get_physical_die_id(int cpu)
3163fb4f7cdSSrinivas Pandruvada {
3173fb4f7cdSSrinivas Pandruvada 	int ret;
3183fb4f7cdSSrinivas Pandruvada 
319fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
320fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/die_id",
3213fb4f7cdSSrinivas Pandruvada 			cpu);
322fb186158SSrinivas Pandruvada 	if (ret < 0) {
323fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
324fb186158SSrinivas Pandruvada 
325fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
326fb186158SSrinivas Pandruvada 		if (!ret)
327fb186158SSrinivas Pandruvada 			return die_id;
328fb186158SSrinivas Pandruvada 	}
329fb186158SSrinivas Pandruvada 
3303fb4f7cdSSrinivas Pandruvada 	if (ret < 0)
3313fb4f7cdSSrinivas Pandruvada 		ret = 0;
3323fb4f7cdSSrinivas Pandruvada 
3333fb4f7cdSSrinivas Pandruvada 	return ret;
3343fb4f7cdSSrinivas Pandruvada }
3353fb4f7cdSSrinivas Pandruvada 
3367af5a95bSSrinivas Pandruvada int get_cpufreq_base_freq(int cpu)
3377af5a95bSSrinivas Pandruvada {
3387af5a95bSSrinivas Pandruvada 	return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
3397af5a95bSSrinivas Pandruvada }
3407af5a95bSSrinivas Pandruvada 
3413fb4f7cdSSrinivas Pandruvada int get_topo_max_cpus(void)
3423fb4f7cdSSrinivas Pandruvada {
3433fb4f7cdSSrinivas Pandruvada 	return topo_max_cpus;
3443fb4f7cdSSrinivas Pandruvada }
3453fb4f7cdSSrinivas Pandruvada 
3463c64c81aSSrinivas Pandruvada static void set_cpu_online_offline(int cpu, int state)
3473c64c81aSSrinivas Pandruvada {
3483c64c81aSSrinivas Pandruvada 	char buffer[128];
349abd120e3SSrinivas Pandruvada 	int fd, ret;
3503c64c81aSSrinivas Pandruvada 
3513c64c81aSSrinivas Pandruvada 	snprintf(buffer, sizeof(buffer),
3523c64c81aSSrinivas Pandruvada 		 "/sys/devices/system/cpu/cpu%d/online", cpu);
3533c64c81aSSrinivas Pandruvada 
3543c64c81aSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
35569669198SSrinivas Pandruvada 	if (fd < 0) {
35669669198SSrinivas Pandruvada 		if (!cpu && state) {
35769669198SSrinivas Pandruvada 			fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
35869669198SSrinivas Pandruvada 			fprintf(stderr, "Ignoring online request for CPU 0 as this is already online\n");
35969669198SSrinivas Pandruvada 			return;
36069669198SSrinivas Pandruvada 		}
3613c64c81aSSrinivas Pandruvada 		err(-1, "%s open failed", buffer);
36269669198SSrinivas Pandruvada 	}
3633c64c81aSSrinivas Pandruvada 
3643c64c81aSSrinivas Pandruvada 	if (state)
365abd120e3SSrinivas Pandruvada 		ret = write(fd, "1\n", 2);
3663c64c81aSSrinivas Pandruvada 	else
367abd120e3SSrinivas Pandruvada 		ret = write(fd, "0\n", 2);
368abd120e3SSrinivas Pandruvada 
369abd120e3SSrinivas Pandruvada 	if (ret == -1)
370abd120e3SSrinivas Pandruvada 		perror("Online/Offline: Operation failed\n");
3713c64c81aSSrinivas Pandruvada 
3723c64c81aSSrinivas Pandruvada 	close(fd);
3733c64c81aSSrinivas Pandruvada }
3743c64c81aSSrinivas Pandruvada 
3753fb4f7cdSSrinivas Pandruvada #define MAX_PACKAGE_COUNT 8
3763fb4f7cdSSrinivas Pandruvada #define MAX_DIE_PER_PACKAGE 2
3773fb4f7cdSSrinivas Pandruvada static void for_each_online_package_in_set(void (*callback)(int, void *, void *,
3783fb4f7cdSSrinivas Pandruvada 							    void *, void *),
3793fb4f7cdSSrinivas Pandruvada 					   void *arg1, void *arg2, void *arg3,
3803fb4f7cdSSrinivas Pandruvada 					   void *arg4)
3813fb4f7cdSSrinivas Pandruvada {
3823fb4f7cdSSrinivas Pandruvada 	int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
3833fb4f7cdSSrinivas Pandruvada 	int pkg_index = 0, i;
3843fb4f7cdSSrinivas Pandruvada 
3853fb4f7cdSSrinivas Pandruvada 	memset(max_packages, 0xff, sizeof(max_packages));
3863fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
3873fb4f7cdSSrinivas Pandruvada 		int j, online, pkg_id, die_id = 0, skip = 0;
3883fb4f7cdSSrinivas Pandruvada 
3893fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
3903fb4f7cdSSrinivas Pandruvada 			continue;
3913fb4f7cdSSrinivas Pandruvada 		if (i)
3923fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
3933fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
3943fb4f7cdSSrinivas Pandruvada 		else
3953fb4f7cdSSrinivas Pandruvada 			online =
3963fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
3973fb4f7cdSSrinivas Pandruvada 
3983fb4f7cdSSrinivas Pandruvada 		die_id = get_physical_die_id(i);
3993fb4f7cdSSrinivas Pandruvada 		if (die_id < 0)
4003fb4f7cdSSrinivas Pandruvada 			die_id = 0;
401fb186158SSrinivas Pandruvada 
402fb186158SSrinivas Pandruvada 		pkg_id = parse_int_file(0,
403fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
404fb186158SSrinivas Pandruvada 		if (pkg_id < 0)
405f0e0b4d1SSrinivas Pandruvada 			continue;
406fb186158SSrinivas Pandruvada 
4073fb4f7cdSSrinivas Pandruvada 		/* Create an unique id for package, die combination to store */
4083fb4f7cdSSrinivas Pandruvada 		pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
4093fb4f7cdSSrinivas Pandruvada 
4103fb4f7cdSSrinivas Pandruvada 		for (j = 0; j < pkg_index; ++j) {
4113fb4f7cdSSrinivas Pandruvada 			if (max_packages[j] == pkg_id) {
4123fb4f7cdSSrinivas Pandruvada 				skip = 1;
4133fb4f7cdSSrinivas Pandruvada 				break;
4143fb4f7cdSSrinivas Pandruvada 			}
4153fb4f7cdSSrinivas Pandruvada 		}
4163fb4f7cdSSrinivas Pandruvada 
4173fb4f7cdSSrinivas Pandruvada 		if (!skip && online && callback) {
4183fb4f7cdSSrinivas Pandruvada 			callback(i, arg1, arg2, arg3, arg4);
4193fb4f7cdSSrinivas Pandruvada 			max_packages[pkg_index++] = pkg_id;
4203fb4f7cdSSrinivas Pandruvada 		}
4213fb4f7cdSSrinivas Pandruvada 	}
4223fb4f7cdSSrinivas Pandruvada }
4233fb4f7cdSSrinivas Pandruvada 
4243fb4f7cdSSrinivas Pandruvada static void for_each_online_target_cpu_in_set(
4253fb4f7cdSSrinivas Pandruvada 	void (*callback)(int, void *, void *, void *, void *), void *arg1,
4263fb4f7cdSSrinivas Pandruvada 	void *arg2, void *arg3, void *arg4)
4273fb4f7cdSSrinivas Pandruvada {
428*070fdea1SSrinivas Pandruvada 	int i, found = 0;
4293fb4f7cdSSrinivas Pandruvada 
4303fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
4313fb4f7cdSSrinivas Pandruvada 		int online;
4323fb4f7cdSSrinivas Pandruvada 
4333fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
4343fb4f7cdSSrinivas Pandruvada 			continue;
4353fb4f7cdSSrinivas Pandruvada 		if (i)
4363fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
4373fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
4383fb4f7cdSSrinivas Pandruvada 		else
4393fb4f7cdSSrinivas Pandruvada 			online =
4403fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
4413fb4f7cdSSrinivas Pandruvada 
442*070fdea1SSrinivas Pandruvada 		if (online && callback) {
4433fb4f7cdSSrinivas Pandruvada 			callback(i, arg1, arg2, arg3, arg4);
444*070fdea1SSrinivas Pandruvada 			found = 1;
4453fb4f7cdSSrinivas Pandruvada 		}
4463fb4f7cdSSrinivas Pandruvada 	}
4473fb4f7cdSSrinivas Pandruvada 
448*070fdea1SSrinivas Pandruvada 	if (!found)
449*070fdea1SSrinivas Pandruvada 		fprintf(stderr, "No valid CPU in the list\n");
450*070fdea1SSrinivas Pandruvada }
451*070fdea1SSrinivas Pandruvada 
4523fb4f7cdSSrinivas Pandruvada #define BITMASK_SIZE 32
4533fb4f7cdSSrinivas Pandruvada static void set_max_cpu_num(void)
4543fb4f7cdSSrinivas Pandruvada {
4553fb4f7cdSSrinivas Pandruvada 	FILE *filep;
4563fb4f7cdSSrinivas Pandruvada 	unsigned long dummy;
457864dc09eSSrinivas Pandruvada 	int i;
4583fb4f7cdSSrinivas Pandruvada 
4593fb4f7cdSSrinivas Pandruvada 	topo_max_cpus = 0;
460864dc09eSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
461864dc09eSSrinivas Pandruvada 		char path[256];
462864dc09eSSrinivas Pandruvada 
463864dc09eSSrinivas Pandruvada 		snprintf(path, sizeof(path),
464864dc09eSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
465864dc09eSSrinivas Pandruvada 		filep = fopen(path, "r");
466864dc09eSSrinivas Pandruvada 		if (filep)
467864dc09eSSrinivas Pandruvada 			break;
468864dc09eSSrinivas Pandruvada 	}
469864dc09eSSrinivas Pandruvada 
470864dc09eSSrinivas Pandruvada 	if (!filep) {
471864dc09eSSrinivas Pandruvada 		fprintf(stderr, "Can't get max cpu number\n");
472864dc09eSSrinivas Pandruvada 		exit(0);
473864dc09eSSrinivas Pandruvada 	}
474864dc09eSSrinivas Pandruvada 
4753fb4f7cdSSrinivas Pandruvada 	while (fscanf(filep, "%lx,", &dummy) == 1)
4763fb4f7cdSSrinivas Pandruvada 		topo_max_cpus += BITMASK_SIZE;
4773fb4f7cdSSrinivas Pandruvada 	fclose(filep);
4783fb4f7cdSSrinivas Pandruvada 
4793fb4f7cdSSrinivas Pandruvada 	debug_printf("max cpus %d\n", topo_max_cpus);
4803fb4f7cdSSrinivas Pandruvada }
4813fb4f7cdSSrinivas Pandruvada 
4823fb4f7cdSSrinivas Pandruvada size_t alloc_cpu_set(cpu_set_t **cpu_set)
4833fb4f7cdSSrinivas Pandruvada {
4843fb4f7cdSSrinivas Pandruvada 	cpu_set_t *_cpu_set;
4853fb4f7cdSSrinivas Pandruvada 	size_t size;
4863fb4f7cdSSrinivas Pandruvada 
4873fb4f7cdSSrinivas Pandruvada 	_cpu_set = CPU_ALLOC((topo_max_cpus + 1));
4883fb4f7cdSSrinivas Pandruvada 	if (_cpu_set == NULL)
4893fb4f7cdSSrinivas Pandruvada 		err(3, "CPU_ALLOC");
4903fb4f7cdSSrinivas Pandruvada 	size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
4913fb4f7cdSSrinivas Pandruvada 	CPU_ZERO_S(size, _cpu_set);
4923fb4f7cdSSrinivas Pandruvada 
4933fb4f7cdSSrinivas Pandruvada 	*cpu_set = _cpu_set;
4943fb4f7cdSSrinivas Pandruvada 	return size;
4953fb4f7cdSSrinivas Pandruvada }
4963fb4f7cdSSrinivas Pandruvada 
4973fb4f7cdSSrinivas Pandruvada void free_cpu_set(cpu_set_t *cpu_set)
4983fb4f7cdSSrinivas Pandruvada {
4993fb4f7cdSSrinivas Pandruvada 	CPU_FREE(cpu_set);
5003fb4f7cdSSrinivas Pandruvada }
5013fb4f7cdSSrinivas Pandruvada 
5023fb4f7cdSSrinivas Pandruvada static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
503de7f9d3dSSrinivas Pandruvada static long long core_mask[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
5043fb4f7cdSSrinivas Pandruvada static void set_cpu_present_cpu_mask(void)
5053fb4f7cdSSrinivas Pandruvada {
5063fb4f7cdSSrinivas Pandruvada 	size_t size;
5073fb4f7cdSSrinivas Pandruvada 	DIR *dir;
5083fb4f7cdSSrinivas Pandruvada 	int i;
5093fb4f7cdSSrinivas Pandruvada 
5103fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&present_cpumask);
5113fb4f7cdSSrinivas Pandruvada 	present_cpumask_size = size;
5123fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
5133fb4f7cdSSrinivas Pandruvada 		char buffer[256];
5143fb4f7cdSSrinivas Pandruvada 
5153fb4f7cdSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
5163fb4f7cdSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d", i);
5173fb4f7cdSSrinivas Pandruvada 		dir = opendir(buffer);
5183fb4f7cdSSrinivas Pandruvada 		if (dir) {
5193fb4f7cdSSrinivas Pandruvada 			int pkg_id, die_id;
5203fb4f7cdSSrinivas Pandruvada 
5213fb4f7cdSSrinivas Pandruvada 			CPU_SET_S(i, size, present_cpumask);
5223fb4f7cdSSrinivas Pandruvada 			die_id = get_physical_die_id(i);
5233fb4f7cdSSrinivas Pandruvada 			if (die_id < 0)
5243fb4f7cdSSrinivas Pandruvada 				die_id = 0;
5253fb4f7cdSSrinivas Pandruvada 
5263fb4f7cdSSrinivas Pandruvada 			pkg_id = get_physical_package_id(i);
527f0e0b4d1SSrinivas Pandruvada 			if (pkg_id < 0) {
528f0e0b4d1SSrinivas Pandruvada 				fprintf(stderr, "Failed to get package id, CPU %d may be offline\n", i);
529f0e0b4d1SSrinivas Pandruvada 				continue;
530f0e0b4d1SSrinivas Pandruvada 			}
5313fb4f7cdSSrinivas Pandruvada 			if (pkg_id < MAX_PACKAGE_COUNT &&
532de7f9d3dSSrinivas Pandruvada 			    die_id < MAX_DIE_PER_PACKAGE) {
533de7f9d3dSSrinivas Pandruvada 				int core_id = get_physical_core_id(i);
534de7f9d3dSSrinivas Pandruvada 
5353fb4f7cdSSrinivas Pandruvada 				cpu_cnt[pkg_id][die_id]++;
536de7f9d3dSSrinivas Pandruvada 				core_mask[pkg_id][die_id] |= (1ULL << core_id);
537de7f9d3dSSrinivas Pandruvada 			}
5383fb4f7cdSSrinivas Pandruvada 		}
5393fb4f7cdSSrinivas Pandruvada 		closedir(dir);
5403fb4f7cdSSrinivas Pandruvada 	}
5413fb4f7cdSSrinivas Pandruvada }
5423fb4f7cdSSrinivas Pandruvada 
543de7f9d3dSSrinivas Pandruvada int get_core_count(int pkg_id, int die_id)
544de7f9d3dSSrinivas Pandruvada {
545de7f9d3dSSrinivas Pandruvada 	int cnt = 0;
546de7f9d3dSSrinivas Pandruvada 
547de7f9d3dSSrinivas Pandruvada 	if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE) {
548de7f9d3dSSrinivas Pandruvada 		int i;
549de7f9d3dSSrinivas Pandruvada 
550de7f9d3dSSrinivas Pandruvada 		for (i = 0; i < sizeof(long long) * 8; ++i) {
551de7f9d3dSSrinivas Pandruvada 			if (core_mask[pkg_id][die_id] & (1ULL << i))
552de7f9d3dSSrinivas Pandruvada 				cnt++;
553de7f9d3dSSrinivas Pandruvada 		}
554de7f9d3dSSrinivas Pandruvada 	}
555de7f9d3dSSrinivas Pandruvada 
556de7f9d3dSSrinivas Pandruvada 	return cnt;
557de7f9d3dSSrinivas Pandruvada }
558de7f9d3dSSrinivas Pandruvada 
5593fb4f7cdSSrinivas Pandruvada int get_cpu_count(int pkg_id, int die_id)
5603fb4f7cdSSrinivas Pandruvada {
5613fb4f7cdSSrinivas Pandruvada 	if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
5623ec2aef1SPrarit Bhargava 		return cpu_cnt[pkg_id][die_id];
5633fb4f7cdSSrinivas Pandruvada 
5643fb4f7cdSSrinivas Pandruvada 	return 0;
5653fb4f7cdSSrinivas Pandruvada }
5663fb4f7cdSSrinivas Pandruvada 
5673fb4f7cdSSrinivas Pandruvada static void set_cpu_target_cpu_mask(void)
5683fb4f7cdSSrinivas Pandruvada {
5693fb4f7cdSSrinivas Pandruvada 	size_t size;
5703fb4f7cdSSrinivas Pandruvada 	int i;
5713fb4f7cdSSrinivas Pandruvada 
5723fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&target_cpumask);
5733fb4f7cdSSrinivas Pandruvada 	target_cpumask_size = size;
5743fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < max_target_cpus; ++i) {
5753fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
5763fb4f7cdSSrinivas Pandruvada 				 present_cpumask))
5773fb4f7cdSSrinivas Pandruvada 			continue;
5783fb4f7cdSSrinivas Pandruvada 
5793fb4f7cdSSrinivas Pandruvada 		CPU_SET_S(target_cpus[i], size, target_cpumask);
5803fb4f7cdSSrinivas Pandruvada 	}
5813fb4f7cdSSrinivas Pandruvada }
5823fb4f7cdSSrinivas Pandruvada 
5833fb4f7cdSSrinivas Pandruvada static void create_cpu_map(void)
5843fb4f7cdSSrinivas Pandruvada {
5853fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
5863fb4f7cdSSrinivas Pandruvada 	int i, fd = 0;
5873fb4f7cdSSrinivas Pandruvada 	struct isst_if_cpu_maps map;
5883fb4f7cdSSrinivas Pandruvada 
5893fb4f7cdSSrinivas Pandruvada 	cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
5903fb4f7cdSSrinivas Pandruvada 	if (!cpu_map)
5913fb4f7cdSSrinivas Pandruvada 		err(3, "cpumap");
5923fb4f7cdSSrinivas Pandruvada 
5933fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
5943fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
5953fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
5963fb4f7cdSSrinivas Pandruvada 
5973fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
5983fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
5993fb4f7cdSSrinivas Pandruvada 			continue;
6003fb4f7cdSSrinivas Pandruvada 
6013fb4f7cdSSrinivas Pandruvada 		map.cmd_count = 1;
6023fb4f7cdSSrinivas Pandruvada 		map.cpu_map[0].logical_cpu = i;
6033fb4f7cdSSrinivas Pandruvada 
6043fb4f7cdSSrinivas Pandruvada 		debug_printf(" map logical_cpu:%d\n",
6053fb4f7cdSSrinivas Pandruvada 			     map.cpu_map[0].logical_cpu);
6063fb4f7cdSSrinivas Pandruvada 		if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
6073fb4f7cdSSrinivas Pandruvada 			perror("ISST_IF_GET_PHY_ID");
6083fb4f7cdSSrinivas Pandruvada 			fprintf(outf, "Error: map logical_cpu:%d\n",
6093fb4f7cdSSrinivas Pandruvada 				map.cpu_map[0].logical_cpu);
6103fb4f7cdSSrinivas Pandruvada 			continue;
6113fb4f7cdSSrinivas Pandruvada 		}
6123fb4f7cdSSrinivas Pandruvada 		cpu_map[i].core_id = get_physical_core_id(i);
6133fb4f7cdSSrinivas Pandruvada 		cpu_map[i].pkg_id = get_physical_package_id(i);
6143fb4f7cdSSrinivas Pandruvada 		cpu_map[i].die_id = get_physical_die_id(i);
6153fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
6163fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
6173fb4f7cdSSrinivas Pandruvada 					     1); // shift to get core id
6183fb4f7cdSSrinivas Pandruvada 
6193fb4f7cdSSrinivas Pandruvada 		debug_printf(
6203fb4f7cdSSrinivas Pandruvada 			"map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
6213fb4f7cdSSrinivas Pandruvada 			i, cpu_map[i].core_id, cpu_map[i].die_id,
6223fb4f7cdSSrinivas Pandruvada 			cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
6233fb4f7cdSSrinivas Pandruvada 			cpu_map[i].punit_cpu_core);
6243fb4f7cdSSrinivas Pandruvada 	}
6253fb4f7cdSSrinivas Pandruvada 
6263fb4f7cdSSrinivas Pandruvada 	if (fd)
6273fb4f7cdSSrinivas Pandruvada 		close(fd);
6283fb4f7cdSSrinivas Pandruvada }
6293fb4f7cdSSrinivas Pandruvada 
6303fb4f7cdSSrinivas Pandruvada int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
6313fb4f7cdSSrinivas Pandruvada {
6323fb4f7cdSSrinivas Pandruvada 	int i;
6333fb4f7cdSSrinivas Pandruvada 
6343fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
6353fb4f7cdSSrinivas Pandruvada 		if (cpu_map[i].pkg_id == pkg_id &&
6363fb4f7cdSSrinivas Pandruvada 		    cpu_map[i].die_id == die_id &&
6373fb4f7cdSSrinivas Pandruvada 		    cpu_map[i].punit_cpu_core == punit_core_id)
6383fb4f7cdSSrinivas Pandruvada 			return i;
6393fb4f7cdSSrinivas Pandruvada 	}
6403fb4f7cdSSrinivas Pandruvada 
6413fb4f7cdSSrinivas Pandruvada 	return -EINVAL;
6423fb4f7cdSSrinivas Pandruvada }
6433fb4f7cdSSrinivas Pandruvada 
6443fb4f7cdSSrinivas Pandruvada void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
6453fb4f7cdSSrinivas Pandruvada 				      size_t core_cpumask_size,
6463fb4f7cdSSrinivas Pandruvada 				      cpu_set_t *core_cpumask, int *cpu_cnt)
6473fb4f7cdSSrinivas Pandruvada {
6483fb4f7cdSSrinivas Pandruvada 	int i, cnt = 0;
6493fb4f7cdSSrinivas Pandruvada 	int die_id, pkg_id;
6503fb4f7cdSSrinivas Pandruvada 
6513fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = 0;
6523fb4f7cdSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
6533fb4f7cdSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
6543fb4f7cdSSrinivas Pandruvada 
6553fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < 64; ++i) {
6563fb4f7cdSSrinivas Pandruvada 		if (core_mask & BIT(i)) {
6573fb4f7cdSSrinivas Pandruvada 			int j;
6583fb4f7cdSSrinivas Pandruvada 
6593fb4f7cdSSrinivas Pandruvada 			for (j = 0; j < topo_max_cpus; ++j) {
66044460efeSYouquan Song 				if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
66144460efeSYouquan Song 					continue;
66244460efeSYouquan Song 
6633fb4f7cdSSrinivas Pandruvada 				if (cpu_map[j].pkg_id == pkg_id &&
6643fb4f7cdSSrinivas Pandruvada 				    cpu_map[j].die_id == die_id &&
6653fb4f7cdSSrinivas Pandruvada 				    cpu_map[j].punit_cpu_core == i) {
6663fb4f7cdSSrinivas Pandruvada 					CPU_SET_S(j, core_cpumask_size,
6673fb4f7cdSSrinivas Pandruvada 						  core_cpumask);
6683fb4f7cdSSrinivas Pandruvada 					++cnt;
6693fb4f7cdSSrinivas Pandruvada 				}
6703fb4f7cdSSrinivas Pandruvada 			}
6713fb4f7cdSSrinivas Pandruvada 		}
6723fb4f7cdSSrinivas Pandruvada 	}
6733fb4f7cdSSrinivas Pandruvada 
6743fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = cnt;
6753fb4f7cdSSrinivas Pandruvada }
6763fb4f7cdSSrinivas Pandruvada 
6773fb4f7cdSSrinivas Pandruvada int find_phy_core_num(int logical_cpu)
6783fb4f7cdSSrinivas Pandruvada {
6793fb4f7cdSSrinivas Pandruvada 	if (logical_cpu < topo_max_cpus)
6803fb4f7cdSSrinivas Pandruvada 		return cpu_map[logical_cpu].punit_cpu_core;
6813fb4f7cdSSrinivas Pandruvada 
6823fb4f7cdSSrinivas Pandruvada 	return -EINVAL;
6833fb4f7cdSSrinivas Pandruvada }
6843fb4f7cdSSrinivas Pandruvada 
6853fb4f7cdSSrinivas Pandruvada static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
6863fb4f7cdSSrinivas Pandruvada 				  unsigned int *value)
6873fb4f7cdSSrinivas Pandruvada {
6883fb4f7cdSSrinivas Pandruvada 	struct isst_if_io_regs io_regs;
6893fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
6903fb4f7cdSSrinivas Pandruvada 	int cmd;
6913fb4f7cdSSrinivas Pandruvada 	int fd;
6923fb4f7cdSSrinivas Pandruvada 
6933fb4f7cdSSrinivas Pandruvada 	debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
6943fb4f7cdSSrinivas Pandruvada 
6953fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
6963fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
6973fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
6983fb4f7cdSSrinivas Pandruvada 
6993fb4f7cdSSrinivas Pandruvada 	io_regs.req_count = 1;
7003fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].logical_cpu = cpu;
7013fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].reg = reg;
7023fb4f7cdSSrinivas Pandruvada 	cmd = ISST_IF_IO_CMD;
7033fb4f7cdSSrinivas Pandruvada 	if (write) {
7043fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 1;
7053fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].value = *value;
7063fb4f7cdSSrinivas Pandruvada 	} else {
7073fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 0;
7083fb4f7cdSSrinivas Pandruvada 	}
7093fb4f7cdSSrinivas Pandruvada 
7103fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, cmd, &io_regs) == -1) {
71195f8e569SSrinivas Pandruvada 		if (errno == ENOTTY) {
71295f8e569SSrinivas Pandruvada 			perror("ISST_IF_IO_COMMAND\n");
71395f8e569SSrinivas Pandruvada 			fprintf(stderr, "Check presence of kernel modules: isst_if_mmio\n");
71495f8e569SSrinivas Pandruvada 			exit(0);
71595f8e569SSrinivas Pandruvada 		}
7163fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
7173fb4f7cdSSrinivas Pandruvada 			cpu, reg, write);
7183fb4f7cdSSrinivas Pandruvada 	} else {
7193fb4f7cdSSrinivas Pandruvada 		if (!write)
7203fb4f7cdSSrinivas Pandruvada 			*value = io_regs.io_reg[0].value;
7213fb4f7cdSSrinivas Pandruvada 
7223fb4f7cdSSrinivas Pandruvada 		debug_printf(
7233fb4f7cdSSrinivas Pandruvada 			"mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
7243fb4f7cdSSrinivas Pandruvada 			cpu, reg, write, *value);
7253fb4f7cdSSrinivas Pandruvada 	}
7263fb4f7cdSSrinivas Pandruvada 
7273fb4f7cdSSrinivas Pandruvada 	close(fd);
7283fb4f7cdSSrinivas Pandruvada 
7293fb4f7cdSSrinivas Pandruvada 	return 0;
7303fb4f7cdSSrinivas Pandruvada }
7313fb4f7cdSSrinivas Pandruvada 
7323fb4f7cdSSrinivas Pandruvada int isst_send_mbox_command(unsigned int cpu, unsigned char command,
7333fb4f7cdSSrinivas Pandruvada 			   unsigned char sub_command, unsigned int parameter,
7343fb4f7cdSSrinivas Pandruvada 			   unsigned int req_data, unsigned int *resp)
7353fb4f7cdSSrinivas Pandruvada {
7363fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
7373fb4f7cdSSrinivas Pandruvada 	int fd;
7383fb4f7cdSSrinivas Pandruvada 	struct isst_if_mbox_cmds mbox_cmds = { 0 };
7393fb4f7cdSSrinivas Pandruvada 
7403fb4f7cdSSrinivas Pandruvada 	debug_printf(
7413fb4f7cdSSrinivas Pandruvada 		"mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
7423fb4f7cdSSrinivas Pandruvada 		cpu, command, sub_command, parameter, req_data);
7433fb4f7cdSSrinivas Pandruvada 
74495f8e569SSrinivas Pandruvada 	if (!is_skx_based_platform() && command == CONFIG_CLOS &&
7458ddbda76SSrinivas Pandruvada 	    sub_command != CLOS_PM_QOS_CONFIG) {
7463fb4f7cdSSrinivas Pandruvada 		unsigned int value;
7473fb4f7cdSSrinivas Pandruvada 		int write = 0;
7483fb4f7cdSSrinivas Pandruvada 		int clos_id, core_id, ret = 0;
7493fb4f7cdSSrinivas Pandruvada 
750d2d1f304SSrinivas Pandruvada 		debug_printf("CPU %d\n", cpu);
7513fb4f7cdSSrinivas Pandruvada 
7523fb4f7cdSSrinivas Pandruvada 		if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
7533fb4f7cdSSrinivas Pandruvada 			value = req_data;
7543fb4f7cdSSrinivas Pandruvada 			write = 1;
7553fb4f7cdSSrinivas Pandruvada 		}
7563fb4f7cdSSrinivas Pandruvada 
7573fb4f7cdSSrinivas Pandruvada 		switch (sub_command) {
7583fb4f7cdSSrinivas Pandruvada 		case CLOS_PQR_ASSOC:
7593fb4f7cdSSrinivas Pandruvada 			core_id = parameter & 0xff;
7603fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
7613fb4f7cdSSrinivas Pandruvada 				cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
7623fb4f7cdSSrinivas Pandruvada 				&value);
7633fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
7643fb4f7cdSSrinivas Pandruvada 				*resp = value;
7653fb4f7cdSSrinivas Pandruvada 			break;
7663fb4f7cdSSrinivas Pandruvada 		case CLOS_PM_CLOS:
7673fb4f7cdSSrinivas Pandruvada 			clos_id = parameter & 0x03;
7683fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
7693fb4f7cdSSrinivas Pandruvada 				cpu, PM_CLOS_OFFSET + clos_id * 4, write,
7703fb4f7cdSSrinivas Pandruvada 				&value);
7713fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
7723fb4f7cdSSrinivas Pandruvada 				*resp = value;
7733fb4f7cdSSrinivas Pandruvada 			break;
7743fb4f7cdSSrinivas Pandruvada 		case CLOS_STATUS:
7753fb4f7cdSSrinivas Pandruvada 			break;
7763fb4f7cdSSrinivas Pandruvada 		default:
7773fb4f7cdSSrinivas Pandruvada 			break;
7783fb4f7cdSSrinivas Pandruvada 		}
7793fb4f7cdSSrinivas Pandruvada 		return ret;
7803fb4f7cdSSrinivas Pandruvada 	}
7813fb4f7cdSSrinivas Pandruvada 
7823fb4f7cdSSrinivas Pandruvada 	mbox_cmds.cmd_count = 1;
7833fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
7843fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].command = command;
7853fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].sub_command = sub_command;
7863fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].parameter = parameter;
7873fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].req_data = req_data;
7883fb4f7cdSSrinivas Pandruvada 
7893fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
7903fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
7913fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
7923fb4f7cdSSrinivas Pandruvada 
7933fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
79495f8e569SSrinivas Pandruvada 		if (errno == ENOTTY) {
79595f8e569SSrinivas Pandruvada 			perror("ISST_IF_MBOX_COMMAND\n");
79695f8e569SSrinivas Pandruvada 			fprintf(stderr, "Check presence of kernel modules: isst_if_mbox_pci or isst_if_mbox_msr\n");
79795f8e569SSrinivas Pandruvada 			exit(0);
79895f8e569SSrinivas Pandruvada 		}
79995f8e569SSrinivas Pandruvada 		debug_printf(
80095f8e569SSrinivas Pandruvada 			"Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x errorno:%d\n",
80195f8e569SSrinivas Pandruvada 			cpu, command, sub_command, parameter, req_data, errno);
8027af5a95bSSrinivas Pandruvada 		return -1;
8033fb4f7cdSSrinivas Pandruvada 	} else {
8043fb4f7cdSSrinivas Pandruvada 		*resp = mbox_cmds.mbox_cmd[0].resp_data;
8053fb4f7cdSSrinivas Pandruvada 		debug_printf(
8063fb4f7cdSSrinivas Pandruvada 			"mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
8073fb4f7cdSSrinivas Pandruvada 			cpu, command, sub_command, parameter, req_data, *resp);
8083fb4f7cdSSrinivas Pandruvada 	}
8093fb4f7cdSSrinivas Pandruvada 
8103fb4f7cdSSrinivas Pandruvada 	close(fd);
8113fb4f7cdSSrinivas Pandruvada 
8123fb4f7cdSSrinivas Pandruvada 	return 0;
8133fb4f7cdSSrinivas Pandruvada }
8143fb4f7cdSSrinivas Pandruvada 
8153fb4f7cdSSrinivas Pandruvada int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
8163fb4f7cdSSrinivas Pandruvada 			  unsigned long long *req_resp)
8173fb4f7cdSSrinivas Pandruvada {
8183fb4f7cdSSrinivas Pandruvada 	struct isst_if_msr_cmds msr_cmds;
8193fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
8203fb4f7cdSSrinivas Pandruvada 	int fd;
8213fb4f7cdSSrinivas Pandruvada 
8223fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8233fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8243fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
8253fb4f7cdSSrinivas Pandruvada 
8263fb4f7cdSSrinivas Pandruvada 	msr_cmds.cmd_count = 1;
8273fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].logical_cpu = cpu;
8283fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].msr = msr;
8293fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].read_write = write;
8303fb4f7cdSSrinivas Pandruvada 	if (write)
8313fb4f7cdSSrinivas Pandruvada 		msr_cmds.msr_cmd[0].data = *req_resp;
8323fb4f7cdSSrinivas Pandruvada 
8333fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
8343fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_MSR_COMMAD");
8353fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
8363fb4f7cdSSrinivas Pandruvada 			cpu, msr, write);
8373fb4f7cdSSrinivas Pandruvada 	} else {
8383fb4f7cdSSrinivas Pandruvada 		if (!write)
8393fb4f7cdSSrinivas Pandruvada 			*req_resp = msr_cmds.msr_cmd[0].data;
8403fb4f7cdSSrinivas Pandruvada 
8413fb4f7cdSSrinivas Pandruvada 		debug_printf(
8423fb4f7cdSSrinivas Pandruvada 			"msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
8433fb4f7cdSSrinivas Pandruvada 			cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
8443fb4f7cdSSrinivas Pandruvada 	}
8453fb4f7cdSSrinivas Pandruvada 
8463fb4f7cdSSrinivas Pandruvada 	close(fd);
8473fb4f7cdSSrinivas Pandruvada 
8483fb4f7cdSSrinivas Pandruvada 	return 0;
8493fb4f7cdSSrinivas Pandruvada }
8503fb4f7cdSSrinivas Pandruvada 
8513fb4f7cdSSrinivas Pandruvada static int isst_fill_platform_info(void)
8523fb4f7cdSSrinivas Pandruvada {
8533fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
8543fb4f7cdSSrinivas Pandruvada 	int fd;
8553fb4f7cdSSrinivas Pandruvada 
8563fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8573fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8583fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
8593fb4f7cdSSrinivas Pandruvada 
8603fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
8613fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
8623fb4f7cdSSrinivas Pandruvada 		close(fd);
8633fb4f7cdSSrinivas Pandruvada 		return -1;
8643fb4f7cdSSrinivas Pandruvada 	}
8653fb4f7cdSSrinivas Pandruvada 
8663fb4f7cdSSrinivas Pandruvada 	close(fd);
8673fb4f7cdSSrinivas Pandruvada 
8683bc3d30cSPrarit Bhargava 	if (isst_platform_info.api_version > supported_api_ver) {
8693bc3d30cSPrarit Bhargava 		printf("Incompatible API versions; Upgrade of tool is required\n");
8703bc3d30cSPrarit Bhargava 		return -1;
8713bc3d30cSPrarit Bhargava 	}
8723fb4f7cdSSrinivas Pandruvada 	return 0;
8733fb4f7cdSSrinivas Pandruvada }
8743fb4f7cdSSrinivas Pandruvada 
8751ba148aeSSrinivas Pandruvada static void isst_print_extended_platform_info(void)
8761ba148aeSSrinivas Pandruvada {
8771ba148aeSSrinivas Pandruvada 	int cp_state, cp_cap, fact_support = 0, pbf_support = 0;
8781ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
8791ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
8801ba148aeSSrinivas Pandruvada 	int ret, i, j;
8811ba148aeSSrinivas Pandruvada 	FILE *filep;
8821ba148aeSSrinivas Pandruvada 
8831ba148aeSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
8841ba148aeSSrinivas Pandruvada 		char path[256];
8851ba148aeSSrinivas Pandruvada 
8861ba148aeSSrinivas Pandruvada 		snprintf(path, sizeof(path),
8871ba148aeSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
8881ba148aeSSrinivas Pandruvada 		filep = fopen(path, "r");
8891ba148aeSSrinivas Pandruvada 		if (filep)
8901ba148aeSSrinivas Pandruvada 			break;
8911ba148aeSSrinivas Pandruvada 	}
8921ba148aeSSrinivas Pandruvada 
8931ba148aeSSrinivas Pandruvada 	if (!filep)
8941ba148aeSSrinivas Pandruvada 		return;
8951ba148aeSSrinivas Pandruvada 
8961ba148aeSSrinivas Pandruvada 	fclose(filep);
8971ba148aeSSrinivas Pandruvada 
8981ba148aeSSrinivas Pandruvada 	ret = isst_get_ctdp_levels(i, &pkg_dev);
8991ba148aeSSrinivas Pandruvada 	if (ret)
9001ba148aeSSrinivas Pandruvada 		return;
9011ba148aeSSrinivas Pandruvada 
9021ba148aeSSrinivas Pandruvada 	if (pkg_dev.enabled) {
9031ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
9041ba148aeSSrinivas Pandruvada 	} else {
9051ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
9061ba148aeSSrinivas Pandruvada 		fprintf(outf, "Only performance level 0 (base level) is present\n");
9071ba148aeSSrinivas Pandruvada 	}
9081ba148aeSSrinivas Pandruvada 
9091ba148aeSSrinivas Pandruvada 	if (pkg_dev.locked)
9101ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is locked\n");
9111ba148aeSSrinivas Pandruvada 	else
9121ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is unlocked, max level: %d \n", pkg_dev.levels);
9131ba148aeSSrinivas Pandruvada 
9141ba148aeSSrinivas Pandruvada 	for (j = 0; j <= pkg_dev.levels; ++j) {
9151ba148aeSSrinivas Pandruvada 		ret = isst_get_ctdp_control(i, j, &ctdp_level);
9161ba148aeSSrinivas Pandruvada 		if (ret)
9171ba148aeSSrinivas Pandruvada 			continue;
9181ba148aeSSrinivas Pandruvada 
9191ba148aeSSrinivas Pandruvada 		if (!fact_support && ctdp_level.fact_support)
9201ba148aeSSrinivas Pandruvada 			fact_support = 1;
9211ba148aeSSrinivas Pandruvada 
9221ba148aeSSrinivas Pandruvada 		if (!pbf_support && ctdp_level.pbf_support)
9231ba148aeSSrinivas Pandruvada 			pbf_support = 1;
9241ba148aeSSrinivas Pandruvada 	}
9251ba148aeSSrinivas Pandruvada 
9261ba148aeSSrinivas Pandruvada 	if (fact_support)
9271ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
9281ba148aeSSrinivas Pandruvada 	else
9291ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
9301ba148aeSSrinivas Pandruvada 
9311ba148aeSSrinivas Pandruvada 	if (pbf_support)
9321ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
9331ba148aeSSrinivas Pandruvada 	else
9341ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
9351ba148aeSSrinivas Pandruvada 
9361ba148aeSSrinivas Pandruvada 	ret = isst_read_pm_config(i, &cp_state, &cp_cap);
9371ba148aeSSrinivas Pandruvada 	if (cp_cap)
9381ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
9391ba148aeSSrinivas Pandruvada 	else
9401ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
9411ba148aeSSrinivas Pandruvada }
9421ba148aeSSrinivas Pandruvada 
9433fb4f7cdSSrinivas Pandruvada static void isst_print_platform_information(void)
9443fb4f7cdSSrinivas Pandruvada {
9453fb4f7cdSSrinivas Pandruvada 	struct isst_if_platform_info platform_info;
9463fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
9473fb4f7cdSSrinivas Pandruvada 	int fd;
9483fb4f7cdSSrinivas Pandruvada 
9491ba148aeSSrinivas Pandruvada 	if (is_clx_n_platform()) {
9501ba148aeSSrinivas Pandruvada 		fprintf(stderr, "\nThis option in not supported on this platform\n");
9511ba148aeSSrinivas Pandruvada 		exit(0);
9521ba148aeSSrinivas Pandruvada 	}
9531ba148aeSSrinivas Pandruvada 
9543fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
9553fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
9563fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
9573fb4f7cdSSrinivas Pandruvada 
9583fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
9593fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
9603fb4f7cdSSrinivas Pandruvada 	} else {
9613fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: API version : %d\n",
9623fb4f7cdSSrinivas Pandruvada 			platform_info.api_version);
9633fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: Driver version : %d\n",
9643fb4f7cdSSrinivas Pandruvada 			platform_info.driver_version);
9653fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mbox supported : %d\n",
9663fb4f7cdSSrinivas Pandruvada 			platform_info.mbox_supported);
9673fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mmio supported : %d\n",
9683fb4f7cdSSrinivas Pandruvada 			platform_info.mmio_supported);
9691ba148aeSSrinivas Pandruvada 		isst_print_extended_platform_info();
9703fb4f7cdSSrinivas Pandruvada 	}
9713fb4f7cdSSrinivas Pandruvada 
9723fb4f7cdSSrinivas Pandruvada 	close(fd);
9733fb4f7cdSSrinivas Pandruvada 
9743fb4f7cdSSrinivas Pandruvada 	exit(0);
9753fb4f7cdSSrinivas Pandruvada }
9763fb4f7cdSSrinivas Pandruvada 
9773d1a8579SSrinivas Pandruvada static char *local_str0, *local_str1;
9783fb4f7cdSSrinivas Pandruvada static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
9793fb4f7cdSSrinivas Pandruvada 				 void *arg4)
9803fb4f7cdSSrinivas Pandruvada {
9813fb4f7cdSSrinivas Pandruvada 	int (*fn_ptr)(int cpu, void *arg);
9823fb4f7cdSSrinivas Pandruvada 	int ret;
9833fb4f7cdSSrinivas Pandruvada 
9843fb4f7cdSSrinivas Pandruvada 	fn_ptr = arg1;
9853fb4f7cdSSrinivas Pandruvada 	ret = fn_ptr(cpu, arg2);
9863fb4f7cdSSrinivas Pandruvada 	if (ret)
9873d1a8579SSrinivas Pandruvada 		isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
9883fb4f7cdSSrinivas Pandruvada 	else
989b3abfd77SSrinivas Pandruvada 		isst_ctdp_display_core_info(cpu, outf, arg3,
9903d1a8579SSrinivas Pandruvada 					    *(unsigned int *)arg4,
9913d1a8579SSrinivas Pandruvada 					    local_str0, local_str1);
9923fb4f7cdSSrinivas Pandruvada }
9933fb4f7cdSSrinivas Pandruvada 
9943d1a8579SSrinivas Pandruvada #define _get_tdp_level(desc, suffix, object, help, str0, str1)			\
995ce1326a2SPrarit Bhargava 	static void get_tdp_##object(int arg)                                    \
9963fb4f7cdSSrinivas Pandruvada 	{                                                                         \
9973fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp ctdp;                                        \
9983fb4f7cdSSrinivas Pandruvada \
9993fb4f7cdSSrinivas Pandruvada 		if (cmd_help) {                                                   \
10003fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,                                           \
10013fb4f7cdSSrinivas Pandruvada 				"Print %s [No command arguments are required]\n", \
10023fb4f7cdSSrinivas Pandruvada 				help);                                            \
10033fb4f7cdSSrinivas Pandruvada 			exit(0);                                                  \
10043fb4f7cdSSrinivas Pandruvada 		}                                                                 \
10053d1a8579SSrinivas Pandruvada 		local_str0 = str0;						  \
10063d1a8579SSrinivas Pandruvada 		local_str1 = str1;						  \
10073fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_start(outf);                        \
10083fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus)                                              \
10093fb4f7cdSSrinivas Pandruvada 			for_each_online_target_cpu_in_set(                        \
10103fb4f7cdSSrinivas Pandruvada 				exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
10113fb4f7cdSSrinivas Pandruvada 				&ctdp, desc, &ctdp.object);                       \
10123fb4f7cdSSrinivas Pandruvada 		else                                                              \
10133fb4f7cdSSrinivas Pandruvada 			for_each_online_package_in_set(exec_on_get_ctdp_cpu,      \
10143fb4f7cdSSrinivas Pandruvada 						       isst_get_ctdp_##suffix,    \
10153fb4f7cdSSrinivas Pandruvada 						       &ctdp, desc,               \
10163fb4f7cdSSrinivas Pandruvada 						       &ctdp.object);             \
10173fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);                          \
10183fb4f7cdSSrinivas Pandruvada 	}
10193fb4f7cdSSrinivas Pandruvada 
10203d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
10213d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
10223d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
10233fb4f7cdSSrinivas Pandruvada _get_tdp_level("get-config-current_level", levels, current_level,
10243d1a8579SSrinivas Pandruvada 	       "Current TDP Level", NULL, NULL);
10253d1a8579SSrinivas Pandruvada _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
10263fb4f7cdSSrinivas Pandruvada 
1027062e4aacSPrarit Bhargava struct isst_pkg_ctdp clx_n_pkg_dev;
1028062e4aacSPrarit Bhargava 
1029062e4aacSPrarit Bhargava static int clx_n_get_base_ratio(void)
1030062e4aacSPrarit Bhargava {
1031062e4aacSPrarit Bhargava 	FILE *fp;
1032062e4aacSPrarit Bhargava 	char *begin, *end, *line = NULL;
1033062e4aacSPrarit Bhargava 	char number[5];
1034062e4aacSPrarit Bhargava 	float value = 0;
1035062e4aacSPrarit Bhargava 	size_t n = 0;
1036062e4aacSPrarit Bhargava 
1037062e4aacSPrarit Bhargava 	fp = fopen("/proc/cpuinfo", "r");
1038062e4aacSPrarit Bhargava 	if (!fp)
1039062e4aacSPrarit Bhargava 		err(-1, "cannot open /proc/cpuinfo\n");
1040062e4aacSPrarit Bhargava 
1041062e4aacSPrarit Bhargava 	while (getline(&line, &n, fp) > 0) {
1042062e4aacSPrarit Bhargava 		if (strstr(line, "model name")) {
1043062e4aacSPrarit Bhargava 			/* this is true for CascadeLake-N */
1044062e4aacSPrarit Bhargava 			begin = strstr(line, "@ ") + 2;
1045062e4aacSPrarit Bhargava 			end = strstr(line, "GHz");
1046062e4aacSPrarit Bhargava 			strncpy(number, begin, end - begin);
1047062e4aacSPrarit Bhargava 			value = atof(number) * 10;
1048062e4aacSPrarit Bhargava 			break;
1049062e4aacSPrarit Bhargava 		}
1050062e4aacSPrarit Bhargava 	}
1051062e4aacSPrarit Bhargava 	free(line);
1052062e4aacSPrarit Bhargava 	fclose(fp);
1053062e4aacSPrarit Bhargava 
1054062e4aacSPrarit Bhargava 	return (int)(value);
1055062e4aacSPrarit Bhargava }
1056062e4aacSPrarit Bhargava 
1057062e4aacSPrarit Bhargava static int clx_n_config(int cpu)
1058062e4aacSPrarit Bhargava {
1059062e4aacSPrarit Bhargava 	int i, ret, pkg_id, die_id;
1060062e4aacSPrarit Bhargava 	unsigned long cpu_bf;
1061062e4aacSPrarit Bhargava 	struct isst_pkg_ctdp_level_info *ctdp_level;
1062062e4aacSPrarit Bhargava 	struct isst_pbf_info *pbf_info;
1063062e4aacSPrarit Bhargava 
1064062e4aacSPrarit Bhargava 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1065062e4aacSPrarit Bhargava 	pbf_info = &ctdp_level->pbf_info;
1066062e4aacSPrarit Bhargava 	ctdp_level->core_cpumask_size =
1067062e4aacSPrarit Bhargava 			alloc_cpu_set(&ctdp_level->core_cpumask);
1068062e4aacSPrarit Bhargava 
1069062e4aacSPrarit Bhargava 	/* find the frequency base ratio */
1070062e4aacSPrarit Bhargava 	ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1071062e4aacSPrarit Bhargava 	if (ctdp_level->tdp_ratio == 0) {
1072062e4aacSPrarit Bhargava 		debug_printf("CLX: cn base ratio is zero\n");
1073062e4aacSPrarit Bhargava 		ret = -1;
1074062e4aacSPrarit Bhargava 		goto error_ret;
1075062e4aacSPrarit Bhargava 	}
1076062e4aacSPrarit Bhargava 
1077062e4aacSPrarit Bhargava 	/* find the high and low priority frequencies */
1078062e4aacSPrarit Bhargava 	pbf_info->p1_high = 0;
1079062e4aacSPrarit Bhargava 	pbf_info->p1_low = ~0;
1080062e4aacSPrarit Bhargava 
1081062e4aacSPrarit Bhargava 	pkg_id = get_physical_package_id(cpu);
1082062e4aacSPrarit Bhargava 	die_id = get_physical_die_id(cpu);
1083062e4aacSPrarit Bhargava 
1084062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1085062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1086062e4aacSPrarit Bhargava 			continue;
1087062e4aacSPrarit Bhargava 
1088062e4aacSPrarit Bhargava 		if (pkg_id != get_physical_package_id(i) ||
1089062e4aacSPrarit Bhargava 		    die_id != get_physical_die_id(i))
1090062e4aacSPrarit Bhargava 			continue;
1091062e4aacSPrarit Bhargava 
1092062e4aacSPrarit Bhargava 		CPU_SET_S(i, ctdp_level->core_cpumask_size,
1093062e4aacSPrarit Bhargava 			  ctdp_level->core_cpumask);
1094062e4aacSPrarit Bhargava 
1095062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1096062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1097062e4aacSPrarit Bhargava 					i);
1098062e4aacSPrarit Bhargava 		if (cpu_bf > pbf_info->p1_high)
1099062e4aacSPrarit Bhargava 			pbf_info->p1_high = cpu_bf;
1100062e4aacSPrarit Bhargava 		if (cpu_bf < pbf_info->p1_low)
1101062e4aacSPrarit Bhargava 			pbf_info->p1_low = cpu_bf;
1102062e4aacSPrarit Bhargava 	}
1103062e4aacSPrarit Bhargava 
1104062e4aacSPrarit Bhargava 	if (pbf_info->p1_high == ~0UL) {
1105062e4aacSPrarit Bhargava 		debug_printf("CLX: maximum base frequency not set\n");
1106062e4aacSPrarit Bhargava 		ret = -1;
1107062e4aacSPrarit Bhargava 		goto error_ret;
1108062e4aacSPrarit Bhargava 	}
1109062e4aacSPrarit Bhargava 
1110062e4aacSPrarit Bhargava 	if (pbf_info->p1_low == 0) {
1111062e4aacSPrarit Bhargava 		debug_printf("CLX: minimum base frequency not set\n");
1112062e4aacSPrarit Bhargava 		ret = -1;
1113062e4aacSPrarit Bhargava 		goto error_ret;
1114062e4aacSPrarit Bhargava 	}
1115062e4aacSPrarit Bhargava 
1116062e4aacSPrarit Bhargava 	/* convert frequencies back to ratios */
111791d92814SSrinivas Pandruvada 	pbf_info->p1_high = pbf_info->p1_high / 100000;
111891d92814SSrinivas Pandruvada 	pbf_info->p1_low = pbf_info->p1_low / 100000;
1119062e4aacSPrarit Bhargava 
1120062e4aacSPrarit Bhargava 	/* create high priority cpu mask */
1121062e4aacSPrarit Bhargava 	pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1122062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1123062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1124062e4aacSPrarit Bhargava 			continue;
1125062e4aacSPrarit Bhargava 
1126062e4aacSPrarit Bhargava 		if (pkg_id != get_physical_package_id(i) ||
1127062e4aacSPrarit Bhargava 		    die_id != get_physical_die_id(i))
1128062e4aacSPrarit Bhargava 			continue;
1129062e4aacSPrarit Bhargava 
1130062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1131062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1132062e4aacSPrarit Bhargava 					i);
113391d92814SSrinivas Pandruvada 		cpu_bf = cpu_bf / 100000;
1134062e4aacSPrarit Bhargava 		if (cpu_bf == pbf_info->p1_high)
1135062e4aacSPrarit Bhargava 			CPU_SET_S(i, pbf_info->core_cpumask_size,
1136062e4aacSPrarit Bhargava 				  pbf_info->core_cpumask);
1137062e4aacSPrarit Bhargava 	}
1138062e4aacSPrarit Bhargava 
1139062e4aacSPrarit Bhargava 	/* extra ctdp & pbf struct parameters */
1140062e4aacSPrarit Bhargava 	ctdp_level->processed = 1;
1141062e4aacSPrarit Bhargava 	ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1142062e4aacSPrarit Bhargava 	ctdp_level->pbf_enabled = 1;
1143062e4aacSPrarit Bhargava 	ctdp_level->fact_support = 0; /* FACT is never supported */
1144062e4aacSPrarit Bhargava 	ctdp_level->fact_enabled = 0;
1145062e4aacSPrarit Bhargava 
1146062e4aacSPrarit Bhargava 	return 0;
1147062e4aacSPrarit Bhargava 
1148062e4aacSPrarit Bhargava error_ret:
1149062e4aacSPrarit Bhargava 	free_cpu_set(ctdp_level->core_cpumask);
1150062e4aacSPrarit Bhargava 	return ret;
1151062e4aacSPrarit Bhargava }
1152062e4aacSPrarit Bhargava 
1153062e4aacSPrarit Bhargava static void dump_clx_n_config_for_cpu(int cpu, void *arg1, void *arg2,
1154062e4aacSPrarit Bhargava 				   void *arg3, void *arg4)
1155062e4aacSPrarit Bhargava {
1156062e4aacSPrarit Bhargava 	int ret;
1157062e4aacSPrarit Bhargava 
1158ac9d05eaSSrinivas Pandruvada 	if (tdp_level != 0xff && tdp_level != 0) {
1159ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1160ac9d05eaSSrinivas Pandruvada 		exit(0);
1161ac9d05eaSSrinivas Pandruvada 	}
1162ac9d05eaSSrinivas Pandruvada 
1163062e4aacSPrarit Bhargava 	ret = clx_n_config(cpu);
1164062e4aacSPrarit Bhargava 	if (ret) {
1165062e4aacSPrarit Bhargava 		perror("isst_get_process_ctdp");
1166062e4aacSPrarit Bhargava 	} else {
1167062e4aacSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
1168062e4aacSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
1169062e4aacSPrarit Bhargava 
1170062e4aacSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1171062e4aacSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
1172062e4aacSPrarit Bhargava 		isst_ctdp_display_information(cpu, outf, tdp_level, &clx_n_pkg_dev);
1173062e4aacSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
1174062e4aacSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
1175062e4aacSPrarit Bhargava 	}
1176062e4aacSPrarit Bhargava }
1177062e4aacSPrarit Bhargava 
11783fb4f7cdSSrinivas Pandruvada static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
11793fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
11803fb4f7cdSSrinivas Pandruvada {
11813fb4f7cdSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
11823fb4f7cdSSrinivas Pandruvada 	int ret;
11833fb4f7cdSSrinivas Pandruvada 
11843fb4f7cdSSrinivas Pandruvada 	memset(&pkg_dev, 0, sizeof(pkg_dev));
11853fb4f7cdSSrinivas Pandruvada 	ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
11863fb4f7cdSSrinivas Pandruvada 	if (ret) {
1187ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, cpu);
1188ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1189ac9d05eaSSrinivas Pandruvada 		exit(1);
11903fb4f7cdSSrinivas Pandruvada 	} else {
11913fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
11923fb4f7cdSSrinivas Pandruvada 		isst_get_process_ctdp_complete(cpu, &pkg_dev);
11933fb4f7cdSSrinivas Pandruvada 	}
11943fb4f7cdSSrinivas Pandruvada }
11953fb4f7cdSSrinivas Pandruvada 
1196ce1326a2SPrarit Bhargava static void dump_isst_config(int arg)
11973fb4f7cdSSrinivas Pandruvada {
1198062e4aacSPrarit Bhargava 	void *fn;
1199062e4aacSPrarit Bhargava 
12003fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
12013fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12023fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology Performance profile configuration\n");
12033fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12043fb4f7cdSSrinivas Pandruvada 			"including base frequency and turbo frequency configurations\n");
12053fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
12063fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12073fb4f7cdSSrinivas Pandruvada 			"\tIf no arguments, dump information for all TDP levels\n");
12083fb4f7cdSSrinivas Pandruvada 		exit(0);
12093fb4f7cdSSrinivas Pandruvada 	}
12103fb4f7cdSSrinivas Pandruvada 
1211062e4aacSPrarit Bhargava 	if (!is_clx_n_platform())
1212062e4aacSPrarit Bhargava 		fn = dump_isst_config_for_cpu;
1213062e4aacSPrarit Bhargava 	else
1214062e4aacSPrarit Bhargava 		fn = dump_clx_n_config_for_cpu;
1215062e4aacSPrarit Bhargava 
12163fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
12173fb4f7cdSSrinivas Pandruvada 
12183fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
1219062e4aacSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
12203fb4f7cdSSrinivas Pandruvada 	else
1221062e4aacSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
12223fb4f7cdSSrinivas Pandruvada 
12233fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
12243fb4f7cdSSrinivas Pandruvada }
12253fb4f7cdSSrinivas Pandruvada 
12263fb4f7cdSSrinivas Pandruvada static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
12273fb4f7cdSSrinivas Pandruvada 				  void *arg4)
12283fb4f7cdSSrinivas Pandruvada {
12293fb4f7cdSSrinivas Pandruvada 	int ret;
12303fb4f7cdSSrinivas Pandruvada 
12313fb4f7cdSSrinivas Pandruvada 	ret = isst_set_tdp_level(cpu, tdp_level);
1232ac9d05eaSSrinivas Pandruvada 	if (ret) {
1233ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1234ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1235ac9d05eaSSrinivas Pandruvada 		exit(1);
1236ac9d05eaSSrinivas Pandruvada 	} else {
12373fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
12383fb4f7cdSSrinivas Pandruvada 				    ret);
12393c64c81aSSrinivas Pandruvada 		if (force_online_offline) {
12403c64c81aSSrinivas Pandruvada 			struct isst_pkg_ctdp_level_info ctdp_level;
12413c64c81aSSrinivas Pandruvada 			int pkg_id = get_physical_package_id(cpu);
12423c64c81aSSrinivas Pandruvada 			int die_id = get_physical_die_id(cpu);
12433c64c81aSSrinivas Pandruvada 
12443c64c81aSSrinivas Pandruvada 			fprintf(stderr, "Option is set to online/offline\n");
12453c64c81aSSrinivas Pandruvada 			ctdp_level.core_cpumask_size =
12463c64c81aSSrinivas Pandruvada 				alloc_cpu_set(&ctdp_level.core_cpumask);
12473c64c81aSSrinivas Pandruvada 			isst_get_coremask_info(cpu, tdp_level, &ctdp_level);
12483c64c81aSSrinivas Pandruvada 			if (ctdp_level.cpu_count) {
12493c64c81aSSrinivas Pandruvada 				int i, max_cpus = get_topo_max_cpus();
12503c64c81aSSrinivas Pandruvada 				for (i = 0; i < max_cpus; ++i) {
12513c64c81aSSrinivas Pandruvada 					if (pkg_id != get_physical_package_id(i) || die_id != get_physical_die_id(i))
12523c64c81aSSrinivas Pandruvada 						continue;
12533c64c81aSSrinivas Pandruvada 					if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
12543c64c81aSSrinivas Pandruvada 						fprintf(stderr, "online cpu %d\n", i);
12553c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 1);
12563c64c81aSSrinivas Pandruvada 					} else {
12573c64c81aSSrinivas Pandruvada 						fprintf(stderr, "offline cpu %d\n", i);
12583c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 0);
12593c64c81aSSrinivas Pandruvada 					}
12603c64c81aSSrinivas Pandruvada 				}
12613c64c81aSSrinivas Pandruvada 			}
12623c64c81aSSrinivas Pandruvada 		}
12633c64c81aSSrinivas Pandruvada 	}
12643fb4f7cdSSrinivas Pandruvada }
12653fb4f7cdSSrinivas Pandruvada 
1266ce1326a2SPrarit Bhargava static void set_tdp_level(int arg)
12673fb4f7cdSSrinivas Pandruvada {
12683fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
12693fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Set Config TDP level\n");
12703fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12713fb4f7cdSSrinivas Pandruvada 			"\t Arguments: -l|--level : Specify tdp level\n");
12723c64c81aSSrinivas Pandruvada 		fprintf(stderr,
12733c64c81aSSrinivas Pandruvada 			"\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1274ac9d05eaSSrinivas Pandruvada 		fprintf(stderr,
1275ac9d05eaSSrinivas Pandruvada 			"\t  online/offline operation has limitations, refer to Linux hotplug documentation\n");
12763fb4f7cdSSrinivas Pandruvada 		exit(0);
12773fb4f7cdSSrinivas Pandruvada 	}
12783fb4f7cdSSrinivas Pandruvada 
12793fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1280ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
12813fb4f7cdSSrinivas Pandruvada 		exit(1);
12823fb4f7cdSSrinivas Pandruvada 	}
12833fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
12843fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
12853fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
12863fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
12873fb4f7cdSSrinivas Pandruvada 	else
12883fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
12893fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
12903fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
12913fb4f7cdSSrinivas Pandruvada }
12923fb4f7cdSSrinivas Pandruvada 
12931aa7177cSPrarit Bhargava static void clx_n_dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2,
12941aa7177cSPrarit Bhargava 				       void *arg3, void *arg4)
12951aa7177cSPrarit Bhargava {
12961aa7177cSPrarit Bhargava 	int ret;
12971aa7177cSPrarit Bhargava 
12981aa7177cSPrarit Bhargava 	ret = clx_n_config(cpu);
12991aa7177cSPrarit Bhargava 	if (ret) {
130039bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "clx_n_config failed", 0, 0);
13011aa7177cSPrarit Bhargava 	} else {
13021aa7177cSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
13031aa7177cSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
13041aa7177cSPrarit Bhargava 
13051aa7177cSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
13061aa7177cSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
13071aa7177cSPrarit Bhargava 		isst_pbf_display_information(cpu, outf, tdp_level, pbf_info);
13081aa7177cSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
13091aa7177cSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
13101aa7177cSPrarit Bhargava 	}
13111aa7177cSPrarit Bhargava }
13121aa7177cSPrarit Bhargava 
13133fb4f7cdSSrinivas Pandruvada static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
13143fb4f7cdSSrinivas Pandruvada 				    void *arg4)
13153fb4f7cdSSrinivas Pandruvada {
13163fb4f7cdSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
13173fb4f7cdSSrinivas Pandruvada 	int ret;
13183fb4f7cdSSrinivas Pandruvada 
13193fb4f7cdSSrinivas Pandruvada 	ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
13203fb4f7cdSSrinivas Pandruvada 	if (ret) {
132139bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get base-freq info at this level", 1, tdp_level);
132239bae0fcSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
132339bae0fcSSrinivas Pandruvada 		exit(1);
13243fb4f7cdSSrinivas Pandruvada 	} else {
13253fb4f7cdSSrinivas Pandruvada 		isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
13263fb4f7cdSSrinivas Pandruvada 		isst_get_pbf_info_complete(&pbf_info);
13273fb4f7cdSSrinivas Pandruvada 	}
13283fb4f7cdSSrinivas Pandruvada }
13293fb4f7cdSSrinivas Pandruvada 
1330ce1326a2SPrarit Bhargava static void dump_pbf_config(int arg)
13313fb4f7cdSSrinivas Pandruvada {
13321aa7177cSPrarit Bhargava 	void *fn;
13331aa7177cSPrarit Bhargava 
13343fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
13353fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
13363fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
13373fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
13383fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
13393fb4f7cdSSrinivas Pandruvada 		exit(0);
13403fb4f7cdSSrinivas Pandruvada 	}
13413fb4f7cdSSrinivas Pandruvada 
13423fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
134339bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
13443fb4f7cdSSrinivas Pandruvada 		exit(1);
13453fb4f7cdSSrinivas Pandruvada 	}
13463fb4f7cdSSrinivas Pandruvada 
13471aa7177cSPrarit Bhargava 	if (!is_clx_n_platform())
13481aa7177cSPrarit Bhargava 		fn = dump_pbf_config_for_cpu;
13493fb4f7cdSSrinivas Pandruvada 	else
13501aa7177cSPrarit Bhargava 		fn = clx_n_dump_pbf_config_for_cpu;
13511aa7177cSPrarit Bhargava 
13521aa7177cSPrarit Bhargava 	isst_ctdp_display_information_start(outf);
13531aa7177cSPrarit Bhargava 
13541aa7177cSPrarit Bhargava 	if (max_target_cpus)
13551aa7177cSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
13561aa7177cSPrarit Bhargava 	else
13571aa7177cSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
13581aa7177cSPrarit Bhargava 
13593fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
13603fb4f7cdSSrinivas Pandruvada }
13613fb4f7cdSSrinivas Pandruvada 
1362354bd06fSSrinivas Pandruvada static int set_clos_param(int cpu, int clos, int epp, int wt, int min, int max)
1363354bd06fSSrinivas Pandruvada {
1364354bd06fSSrinivas Pandruvada 	struct isst_clos_config clos_config;
1365354bd06fSSrinivas Pandruvada 	int ret;
1366354bd06fSSrinivas Pandruvada 
1367354bd06fSSrinivas Pandruvada 	ret = isst_pm_get_clos(cpu, clos, &clos_config);
1368354bd06fSSrinivas Pandruvada 	if (ret) {
1369fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
1370354bd06fSSrinivas Pandruvada 		return ret;
1371354bd06fSSrinivas Pandruvada 	}
1372354bd06fSSrinivas Pandruvada 	clos_config.clos_min = min;
1373354bd06fSSrinivas Pandruvada 	clos_config.clos_max = max;
1374354bd06fSSrinivas Pandruvada 	clos_config.epp = epp;
1375354bd06fSSrinivas Pandruvada 	clos_config.clos_prop_prio = wt;
1376354bd06fSSrinivas Pandruvada 	ret = isst_set_clos(cpu, clos, &clos_config);
1377354bd06fSSrinivas Pandruvada 	if (ret) {
1378fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
1379354bd06fSSrinivas Pandruvada 		return ret;
1380354bd06fSSrinivas Pandruvada 	}
1381354bd06fSSrinivas Pandruvada 
1382354bd06fSSrinivas Pandruvada 	return 0;
1383354bd06fSSrinivas Pandruvada }
1384354bd06fSSrinivas Pandruvada 
1385a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1386a9b2f8e2SSrinivas Pandruvada {
1387a9b2f8e2SSrinivas Pandruvada 	char buffer[128], freq_str[16];
1388a9b2f8e2SSrinivas Pandruvada 	int fd, ret, len;
1389a9b2f8e2SSrinivas Pandruvada 
1390a9b2f8e2SSrinivas Pandruvada 	if (max)
1391a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1392a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1393a9b2f8e2SSrinivas Pandruvada 	else
1394a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1395a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1396a9b2f8e2SSrinivas Pandruvada 
1397a9b2f8e2SSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1398a9b2f8e2SSrinivas Pandruvada 	if (fd < 0)
1399a9b2f8e2SSrinivas Pandruvada 		return fd;
1400a9b2f8e2SSrinivas Pandruvada 
1401a9b2f8e2SSrinivas Pandruvada 	snprintf(freq_str, sizeof(freq_str), "%d", freq);
1402a9b2f8e2SSrinivas Pandruvada 	len = strlen(freq_str);
1403a9b2f8e2SSrinivas Pandruvada 	ret = write(fd, freq_str, len);
1404a9b2f8e2SSrinivas Pandruvada 	if (ret == -1) {
1405a9b2f8e2SSrinivas Pandruvada 		close(fd);
1406a9b2f8e2SSrinivas Pandruvada 		return ret;
1407a9b2f8e2SSrinivas Pandruvada 	}
1408a9b2f8e2SSrinivas Pandruvada 	close(fd);
1409a9b2f8e2SSrinivas Pandruvada 
1410a9b2f8e2SSrinivas Pandruvada 	return 0;
1411a9b2f8e2SSrinivas Pandruvada }
1412a9b2f8e2SSrinivas Pandruvada 
1413a9b2f8e2SSrinivas Pandruvada static int set_clx_pbf_cpufreq_scaling_min_max(int cpu)
1414a9b2f8e2SSrinivas Pandruvada {
1415a9b2f8e2SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info *ctdp_level;
1416a9b2f8e2SSrinivas Pandruvada 	struct isst_pbf_info *pbf_info;
1417a9b2f8e2SSrinivas Pandruvada 	int i, pkg_id, die_id, freq, freq_high, freq_low;
1418a9b2f8e2SSrinivas Pandruvada 	int ret;
1419a9b2f8e2SSrinivas Pandruvada 
1420a9b2f8e2SSrinivas Pandruvada 	ret = clx_n_config(cpu);
1421a9b2f8e2SSrinivas Pandruvada 	if (ret) {
1422a9b2f8e2SSrinivas Pandruvada 		perror("set_clx_pbf_cpufreq_scaling_min_max");
1423a9b2f8e2SSrinivas Pandruvada 		return ret;
1424a9b2f8e2SSrinivas Pandruvada 	}
1425a9b2f8e2SSrinivas Pandruvada 
1426a9b2f8e2SSrinivas Pandruvada 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1427a9b2f8e2SSrinivas Pandruvada 	pbf_info = &ctdp_level->pbf_info;
1428a9b2f8e2SSrinivas Pandruvada 	freq_high = pbf_info->p1_high * 100000;
1429a9b2f8e2SSrinivas Pandruvada 	freq_low = pbf_info->p1_low * 100000;
1430a9b2f8e2SSrinivas Pandruvada 
1431a9b2f8e2SSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1432a9b2f8e2SSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1433a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1434a9b2f8e2SSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1435a9b2f8e2SSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1436a9b2f8e2SSrinivas Pandruvada 			continue;
1437a9b2f8e2SSrinivas Pandruvada 
1438a9b2f8e2SSrinivas Pandruvada 		if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1439a9b2f8e2SSrinivas Pandruvada 				  pbf_info->core_cpumask))
1440a9b2f8e2SSrinivas Pandruvada 			freq = freq_high;
1441a9b2f8e2SSrinivas Pandruvada 		else
1442a9b2f8e2SSrinivas Pandruvada 			freq = freq_low;
1443a9b2f8e2SSrinivas Pandruvada 
1444a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 1, freq);
1445a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 0, freq);
1446a9b2f8e2SSrinivas Pandruvada 	}
1447a9b2f8e2SSrinivas Pandruvada 
1448a9b2f8e2SSrinivas Pandruvada 	return 0;
1449a9b2f8e2SSrinivas Pandruvada }
1450a9b2f8e2SSrinivas Pandruvada 
1451a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1452354bd06fSSrinivas Pandruvada {
1453354bd06fSSrinivas Pandruvada 	char buffer[128], min_freq[16];
1454354bd06fSSrinivas Pandruvada 	int fd, ret, len;
1455354bd06fSSrinivas Pandruvada 
1456354bd06fSSrinivas Pandruvada 	if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1457354bd06fSSrinivas Pandruvada 		return -1;
1458354bd06fSSrinivas Pandruvada 
1459a9b2f8e2SSrinivas Pandruvada 	if (cpuinfo_max)
1460354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1461354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1462354bd06fSSrinivas Pandruvada 	else
1463354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1464354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1465354bd06fSSrinivas Pandruvada 
1466354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_RDONLY);
1467354bd06fSSrinivas Pandruvada 	if (fd < 0)
1468354bd06fSSrinivas Pandruvada 		return fd;
1469354bd06fSSrinivas Pandruvada 
1470354bd06fSSrinivas Pandruvada 	len = read(fd, min_freq, sizeof(min_freq));
1471354bd06fSSrinivas Pandruvada 	close(fd);
1472354bd06fSSrinivas Pandruvada 
1473354bd06fSSrinivas Pandruvada 	if (len < 0)
1474354bd06fSSrinivas Pandruvada 		return len;
1475354bd06fSSrinivas Pandruvada 
1476a9b2f8e2SSrinivas Pandruvada 	if (scaling_max)
1477a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1478a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1479a9b2f8e2SSrinivas Pandruvada 	else
1480354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1481354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1482354bd06fSSrinivas Pandruvada 
1483354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1484354bd06fSSrinivas Pandruvada 	if (fd < 0)
1485354bd06fSSrinivas Pandruvada 		return fd;
1486354bd06fSSrinivas Pandruvada 
1487354bd06fSSrinivas Pandruvada 	len = strlen(min_freq);
1488354bd06fSSrinivas Pandruvada 	ret = write(fd, min_freq, len);
1489354bd06fSSrinivas Pandruvada 	if (ret == -1) {
1490354bd06fSSrinivas Pandruvada 		close(fd);
1491354bd06fSSrinivas Pandruvada 		return ret;
1492354bd06fSSrinivas Pandruvada 	}
1493354bd06fSSrinivas Pandruvada 	close(fd);
1494354bd06fSSrinivas Pandruvada 
1495354bd06fSSrinivas Pandruvada 	return 0;
1496354bd06fSSrinivas Pandruvada }
1497354bd06fSSrinivas Pandruvada 
1498354bd06fSSrinivas Pandruvada static void set_scaling_min_to_cpuinfo_max(int cpu)
1499354bd06fSSrinivas Pandruvada {
1500354bd06fSSrinivas Pandruvada 	int i, pkg_id, die_id;
1501354bd06fSSrinivas Pandruvada 
1502354bd06fSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1503354bd06fSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1504354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1505354bd06fSSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1506354bd06fSSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1507354bd06fSSrinivas Pandruvada 			continue;
1508354bd06fSSrinivas Pandruvada 
1509a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1510354bd06fSSrinivas Pandruvada 	}
1511354bd06fSSrinivas Pandruvada }
1512354bd06fSSrinivas Pandruvada 
1513354bd06fSSrinivas Pandruvada static void set_scaling_min_to_cpuinfo_min(int cpu)
1514354bd06fSSrinivas Pandruvada {
1515354bd06fSSrinivas Pandruvada 	int i, pkg_id, die_id;
1516354bd06fSSrinivas Pandruvada 
1517354bd06fSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1518354bd06fSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1519354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1520354bd06fSSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1521354bd06fSSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1522354bd06fSSrinivas Pandruvada 			continue;
1523354bd06fSSrinivas Pandruvada 
1524a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1525a9b2f8e2SSrinivas Pandruvada 	}
1526a9b2f8e2SSrinivas Pandruvada }
1527a9b2f8e2SSrinivas Pandruvada 
1528a9b2f8e2SSrinivas Pandruvada static void set_scaling_max_to_cpuinfo_max(int cpu)
1529a9b2f8e2SSrinivas Pandruvada {
1530a9b2f8e2SSrinivas Pandruvada 	int i, pkg_id, die_id;
1531a9b2f8e2SSrinivas Pandruvada 
1532a9b2f8e2SSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1533a9b2f8e2SSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1534a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1535a9b2f8e2SSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1536a9b2f8e2SSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1537a9b2f8e2SSrinivas Pandruvada 			continue;
1538a9b2f8e2SSrinivas Pandruvada 
1539a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1540354bd06fSSrinivas Pandruvada 	}
1541354bd06fSSrinivas Pandruvada }
1542354bd06fSSrinivas Pandruvada 
1543354bd06fSSrinivas Pandruvada static int set_core_priority_and_min(int cpu, int mask_size,
1544354bd06fSSrinivas Pandruvada 				     cpu_set_t *cpu_mask, int min_high,
1545354bd06fSSrinivas Pandruvada 				     int min_low)
1546354bd06fSSrinivas Pandruvada {
1547354bd06fSSrinivas Pandruvada 	int pkg_id, die_id, ret, i;
1548354bd06fSSrinivas Pandruvada 
1549354bd06fSSrinivas Pandruvada 	if (!CPU_COUNT_S(mask_size, cpu_mask))
1550354bd06fSSrinivas Pandruvada 		return -1;
1551354bd06fSSrinivas Pandruvada 
1552354bd06fSSrinivas Pandruvada 	ret = set_clos_param(cpu, 0, 0, 0, min_high, 0xff);
1553354bd06fSSrinivas Pandruvada 	if (ret)
1554354bd06fSSrinivas Pandruvada 		return ret;
1555354bd06fSSrinivas Pandruvada 
155621c3390dSSrinivas Pandruvada 	ret = set_clos_param(cpu, 1, 15, 15, min_low, 0xff);
1557354bd06fSSrinivas Pandruvada 	if (ret)
1558354bd06fSSrinivas Pandruvada 		return ret;
1559354bd06fSSrinivas Pandruvada 
156021c3390dSSrinivas Pandruvada 	ret = set_clos_param(cpu, 2, 15, 15, min_low, 0xff);
1561354bd06fSSrinivas Pandruvada 	if (ret)
1562354bd06fSSrinivas Pandruvada 		return ret;
1563354bd06fSSrinivas Pandruvada 
156421c3390dSSrinivas Pandruvada 	ret = set_clos_param(cpu, 3, 15, 15, min_low, 0xff);
1565354bd06fSSrinivas Pandruvada 	if (ret)
1566354bd06fSSrinivas Pandruvada 		return ret;
1567354bd06fSSrinivas Pandruvada 
1568354bd06fSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1569354bd06fSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1570354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1571354bd06fSSrinivas Pandruvada 		int clos;
1572354bd06fSSrinivas Pandruvada 
1573354bd06fSSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1574354bd06fSSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1575354bd06fSSrinivas Pandruvada 			continue;
1576354bd06fSSrinivas Pandruvada 
1577354bd06fSSrinivas Pandruvada 		if (CPU_ISSET_S(i, mask_size, cpu_mask))
1578354bd06fSSrinivas Pandruvada 			clos = 0;
1579354bd06fSSrinivas Pandruvada 		else
1580354bd06fSSrinivas Pandruvada 			clos = 3;
1581354bd06fSSrinivas Pandruvada 
1582354bd06fSSrinivas Pandruvada 		debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1583354bd06fSSrinivas Pandruvada 		ret = isst_clos_associate(i, clos);
1584354bd06fSSrinivas Pandruvada 		if (ret) {
1585fe6fb216SSrinivas Pandruvada 			isst_display_error_info_message(1, "isst_clos_associate failed", 0, 0);
1586354bd06fSSrinivas Pandruvada 			return ret;
1587354bd06fSSrinivas Pandruvada 		}
1588354bd06fSSrinivas Pandruvada 	}
1589354bd06fSSrinivas Pandruvada 
1590354bd06fSSrinivas Pandruvada 	return 0;
1591354bd06fSSrinivas Pandruvada }
1592354bd06fSSrinivas Pandruvada 
1593354bd06fSSrinivas Pandruvada static int set_pbf_core_power(int cpu)
1594354bd06fSSrinivas Pandruvada {
1595354bd06fSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
1596354bd06fSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
1597354bd06fSSrinivas Pandruvada 	int ret;
1598354bd06fSSrinivas Pandruvada 
1599354bd06fSSrinivas Pandruvada 	ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1600354bd06fSSrinivas Pandruvada 	if (ret) {
1601fe6fb216SSrinivas Pandruvada 		debug_printf("isst_get_ctdp_levels failed");
1602354bd06fSSrinivas Pandruvada 		return ret;
1603354bd06fSSrinivas Pandruvada 	}
1604354bd06fSSrinivas Pandruvada 	debug_printf("Current_level: %d\n", pkg_dev.current_level);
1605354bd06fSSrinivas Pandruvada 
1606354bd06fSSrinivas Pandruvada 	ret = isst_get_pbf_info(cpu, pkg_dev.current_level, &pbf_info);
1607354bd06fSSrinivas Pandruvada 	if (ret) {
1608fe6fb216SSrinivas Pandruvada 		debug_printf("isst_get_pbf_info failed");
1609354bd06fSSrinivas Pandruvada 		return ret;
1610354bd06fSSrinivas Pandruvada 	}
1611354bd06fSSrinivas Pandruvada 	debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1612354bd06fSSrinivas Pandruvada 		     pbf_info.p1_low);
1613354bd06fSSrinivas Pandruvada 
1614354bd06fSSrinivas Pandruvada 	ret = set_core_priority_and_min(cpu, pbf_info.core_cpumask_size,
1615354bd06fSSrinivas Pandruvada 					pbf_info.core_cpumask,
1616354bd06fSSrinivas Pandruvada 					pbf_info.p1_high, pbf_info.p1_low);
1617354bd06fSSrinivas Pandruvada 	if (ret) {
1618fe6fb216SSrinivas Pandruvada 		debug_printf("set_core_priority_and_min failed");
1619354bd06fSSrinivas Pandruvada 		return ret;
1620354bd06fSSrinivas Pandruvada 	}
1621354bd06fSSrinivas Pandruvada 
1622354bd06fSSrinivas Pandruvada 	ret = isst_pm_qos_config(cpu, 1, 1);
1623354bd06fSSrinivas Pandruvada 	if (ret) {
1624fe6fb216SSrinivas Pandruvada 		debug_printf("isst_pm_qos_config failed");
1625354bd06fSSrinivas Pandruvada 		return ret;
1626354bd06fSSrinivas Pandruvada 	}
1627354bd06fSSrinivas Pandruvada 
1628354bd06fSSrinivas Pandruvada 	return 0;
1629354bd06fSSrinivas Pandruvada }
1630354bd06fSSrinivas Pandruvada 
16313fb4f7cdSSrinivas Pandruvada static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
16323fb4f7cdSSrinivas Pandruvada 			    void *arg4)
16333fb4f7cdSSrinivas Pandruvada {
16343fb4f7cdSSrinivas Pandruvada 	int ret;
16353fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
16363fb4f7cdSSrinivas Pandruvada 
16371aa7177cSPrarit Bhargava 	if (is_clx_n_platform()) {
1638a9b2f8e2SSrinivas Pandruvada 		if (status) {
16391aa7177cSPrarit Bhargava 			ret = 0;
16401aa7177cSPrarit Bhargava 			if (auto_mode)
1641a9b2f8e2SSrinivas Pandruvada 				set_clx_pbf_cpufreq_scaling_min_max(cpu);
1642a9b2f8e2SSrinivas Pandruvada 
1643a9b2f8e2SSrinivas Pandruvada 		} else {
1644a9b2f8e2SSrinivas Pandruvada 			ret = -1;
1645a9b2f8e2SSrinivas Pandruvada 			if (auto_mode) {
1646a9b2f8e2SSrinivas Pandruvada 				set_scaling_max_to_cpuinfo_max(cpu);
1647a9b2f8e2SSrinivas Pandruvada 				set_scaling_min_to_cpuinfo_min(cpu);
1648a9b2f8e2SSrinivas Pandruvada 			}
16491aa7177cSPrarit Bhargava 		}
16501aa7177cSPrarit Bhargava 		goto disp_result;
16511aa7177cSPrarit Bhargava 	}
16521aa7177cSPrarit Bhargava 
1653097a5222SSrinivas Pandruvada 	if (auto_mode && status) {
1654354bd06fSSrinivas Pandruvada 		ret = set_pbf_core_power(cpu);
1655354bd06fSSrinivas Pandruvada 		if (ret)
1656354bd06fSSrinivas Pandruvada 			goto disp_result;
1657354bd06fSSrinivas Pandruvada 	}
1658354bd06fSSrinivas Pandruvada 
16593fb4f7cdSSrinivas Pandruvada 	ret = isst_set_pbf_fact_status(cpu, 1, status);
16603fb4f7cdSSrinivas Pandruvada 	if (ret) {
166139bae0fcSSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1662354bd06fSSrinivas Pandruvada 		if (auto_mode)
1663354bd06fSSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
16643fb4f7cdSSrinivas Pandruvada 	} else {
1665354bd06fSSrinivas Pandruvada 		if (auto_mode) {
1666354bd06fSSrinivas Pandruvada 			if (status)
1667354bd06fSSrinivas Pandruvada 				set_scaling_min_to_cpuinfo_max(cpu);
1668354bd06fSSrinivas Pandruvada 			else
1669354bd06fSSrinivas Pandruvada 				set_scaling_min_to_cpuinfo_min(cpu);
1670354bd06fSSrinivas Pandruvada 		}
1671354bd06fSSrinivas Pandruvada 	}
1672354bd06fSSrinivas Pandruvada 
1673097a5222SSrinivas Pandruvada 	if (auto_mode && !status)
1674fe6fb216SSrinivas Pandruvada 		isst_pm_qos_config(cpu, 0, 1);
1675097a5222SSrinivas Pandruvada 
1676354bd06fSSrinivas Pandruvada disp_result:
16773fb4f7cdSSrinivas Pandruvada 	if (status)
16783fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "base-freq", "enable",
16793fb4f7cdSSrinivas Pandruvada 				    ret);
16803fb4f7cdSSrinivas Pandruvada 	else
16813fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "base-freq", "disable",
16823fb4f7cdSSrinivas Pandruvada 				    ret);
16833fb4f7cdSSrinivas Pandruvada }
16843fb4f7cdSSrinivas Pandruvada 
1685ce1326a2SPrarit Bhargava static void set_pbf_enable(int arg)
16863fb4f7cdSSrinivas Pandruvada {
1687ce1326a2SPrarit Bhargava 	int enable = arg;
16883fb4f7cdSSrinivas Pandruvada 
16893fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1690ce1326a2SPrarit Bhargava 		if (enable) {
16913fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1692354bd06fSSrinivas Pandruvada 				"Enable Intel Speed Select Technology base frequency feature\n");
169339bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
169439bae0fcSSrinivas Pandruvada 				fprintf(stderr,
169539bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't enable feature in the hardware.\n");
169639bae0fcSSrinivas Pandruvada 				fprintf(stderr,
169739bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpufreq base_frequency.\n");
169839bae0fcSSrinivas Pandruvada 				exit(0);
169939bae0fcSSrinivas Pandruvada 
170039bae0fcSSrinivas Pandruvada 			}
1701354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1702354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1703ce1326a2SPrarit Bhargava 		} else {
1704354bd06fSSrinivas Pandruvada 
170539bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
170639bae0fcSSrinivas Pandruvada 				fprintf(stderr,
170739bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't disable feature in the hardware.\n");
170839bae0fcSSrinivas Pandruvada 				fprintf(stderr,
170939bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpuinfo_min_freq\n");
171039bae0fcSSrinivas Pandruvada 				exit(0);
171139bae0fcSSrinivas Pandruvada 			}
17123fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1713354bd06fSSrinivas Pandruvada 				"Disable Intel Speed Select Technology base frequency feature\n");
1714354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1715354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1716ce1326a2SPrarit Bhargava 		}
17173fb4f7cdSSrinivas Pandruvada 		exit(0);
17183fb4f7cdSSrinivas Pandruvada 	}
17193fb4f7cdSSrinivas Pandruvada 
17203fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
17213fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
17223fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1723ce1326a2SPrarit Bhargava 						  NULL, &enable);
17243fb4f7cdSSrinivas Pandruvada 	else
17253fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1726ce1326a2SPrarit Bhargava 					       NULL, &enable);
17273fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
17283fb4f7cdSSrinivas Pandruvada }
17293fb4f7cdSSrinivas Pandruvada 
17303fb4f7cdSSrinivas Pandruvada static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
17313fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
17323fb4f7cdSSrinivas Pandruvada {
17333fb4f7cdSSrinivas Pandruvada 	struct isst_fact_info fact_info;
17343fb4f7cdSSrinivas Pandruvada 	int ret;
17353fb4f7cdSSrinivas Pandruvada 
1736a9fd6ae7SSrinivas Pandruvada 	ret = isst_get_fact_info(cpu, tdp_level, fact_bucket, &fact_info);
1737a9fd6ae7SSrinivas Pandruvada 	if (ret) {
1738a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get turbo-freq info at this level", 1, tdp_level);
1739a9fd6ae7SSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1740a9fd6ae7SSrinivas Pandruvada 		exit(1);
1741a9fd6ae7SSrinivas Pandruvada 	} else {
17423fb4f7cdSSrinivas Pandruvada 		isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
17433fb4f7cdSSrinivas Pandruvada 					      fact_avx, &fact_info);
17443fb4f7cdSSrinivas Pandruvada 	}
1745a9fd6ae7SSrinivas Pandruvada }
17463fb4f7cdSSrinivas Pandruvada 
1747ce1326a2SPrarit Bhargava static void dump_fact_config(int arg)
17483fb4f7cdSSrinivas Pandruvada {
17493fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
17503fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
17513fb4f7cdSSrinivas Pandruvada 			"Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
17523fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
17533fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
17543fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
17553fb4f7cdSSrinivas Pandruvada 			"\tArguments: -b|--bucket : Bucket index to dump\n");
17563fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
17573fb4f7cdSSrinivas Pandruvada 			"\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
17583fb4f7cdSSrinivas Pandruvada 		exit(0);
17593fb4f7cdSSrinivas Pandruvada 	}
17603fb4f7cdSSrinivas Pandruvada 
17613fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1762a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level\n", 0, 0);
17633fb4f7cdSSrinivas Pandruvada 		exit(1);
17643fb4f7cdSSrinivas Pandruvada 	}
17653fb4f7cdSSrinivas Pandruvada 
17663fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
17673fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
17683fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
17693fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
17703fb4f7cdSSrinivas Pandruvada 	else
17713fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
17723fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
17733fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
17743fb4f7cdSSrinivas Pandruvada }
17753fb4f7cdSSrinivas Pandruvada 
17763fb4f7cdSSrinivas Pandruvada static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
17773fb4f7cdSSrinivas Pandruvada 			     void *arg4)
17783fb4f7cdSSrinivas Pandruvada {
17793fb4f7cdSSrinivas Pandruvada 	int ret;
17803fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
17813fb4f7cdSSrinivas Pandruvada 
1782097a5222SSrinivas Pandruvada 	if (auto_mode && status) {
1783a6a82f9bSSrinivas Pandruvada 		ret = isst_pm_qos_config(cpu, 1, 1);
17843fb4f7cdSSrinivas Pandruvada 		if (ret)
1785a6a82f9bSSrinivas Pandruvada 			goto disp_results;
1786a6a82f9bSSrinivas Pandruvada 	}
1787a6a82f9bSSrinivas Pandruvada 
1788a6a82f9bSSrinivas Pandruvada 	ret = isst_set_pbf_fact_status(cpu, 0, status);
1789a6a82f9bSSrinivas Pandruvada 	if (ret) {
1790a9fd6ae7SSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1791a6a82f9bSSrinivas Pandruvada 		if (auto_mode)
1792a6a82f9bSSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
1793a6a82f9bSSrinivas Pandruvada 
1794a6a82f9bSSrinivas Pandruvada 		goto disp_results;
1795a6a82f9bSSrinivas Pandruvada 	}
1796a6a82f9bSSrinivas Pandruvada 
1797a6a82f9bSSrinivas Pandruvada 	/* Set TRL */
17983fb4f7cdSSrinivas Pandruvada 	if (status) {
17993fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp pkg_dev;
18003fb4f7cdSSrinivas Pandruvada 
18013fb4f7cdSSrinivas Pandruvada 		ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1802a6a82f9bSSrinivas Pandruvada 		if (!ret)
18033fb4f7cdSSrinivas Pandruvada 			ret = isst_set_trl(cpu, fact_trl);
1804a6a82f9bSSrinivas Pandruvada 		if (ret && auto_mode)
1805a6a82f9bSSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
1806097a5222SSrinivas Pandruvada 	} else {
1807097a5222SSrinivas Pandruvada 		if (auto_mode)
1808097a5222SSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
1809a6a82f9bSSrinivas Pandruvada 	}
1810a6a82f9bSSrinivas Pandruvada 
1811a6a82f9bSSrinivas Pandruvada disp_results:
1812a6a82f9bSSrinivas Pandruvada 	if (status) {
1813a6a82f9bSSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-freq", "enable", ret);
181414a8aa49SSrinivas Pandruvada 		if (ret)
181514a8aa49SSrinivas Pandruvada 			fact_enable_fail = ret;
18163fb4f7cdSSrinivas Pandruvada 	} else {
18173fb4f7cdSSrinivas Pandruvada 		/* Since we modified TRL during Fact enable, restore it */
18183fb4f7cdSSrinivas Pandruvada 		isst_set_trl_from_current_tdp(cpu, fact_trl);
1819a6a82f9bSSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-freq", "disable", ret);
18203fb4f7cdSSrinivas Pandruvada 	}
18213fb4f7cdSSrinivas Pandruvada }
18223fb4f7cdSSrinivas Pandruvada 
1823ce1326a2SPrarit Bhargava static void set_fact_enable(int arg)
18243fb4f7cdSSrinivas Pandruvada {
1825ce1326a2SPrarit Bhargava 	int i, ret, enable = arg;
18263fb4f7cdSSrinivas Pandruvada 
18273fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1828ce1326a2SPrarit Bhargava 		if (enable) {
18293fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
18303fb4f7cdSSrinivas Pandruvada 				"Enable Intel Speed Select Technology Turbo frequency feature\n");
18313fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
18323fb4f7cdSSrinivas Pandruvada 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1833a6a82f9bSSrinivas Pandruvada 			fprintf(stderr,
1834a6a82f9bSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1835ce1326a2SPrarit Bhargava 			fprintf(stderr,
1836ce1326a2SPrarit Bhargava 				"-C|--cpu option as as high priority using core-power feature\n");
1837ce1326a2SPrarit Bhargava 		} else {
1838ce1326a2SPrarit Bhargava 			fprintf(stderr,
1839ce1326a2SPrarit Bhargava 				"Disable Intel Speed Select Technology turbo frequency feature\n");
1840ce1326a2SPrarit Bhargava 			fprintf(stderr,
1841ce1326a2SPrarit Bhargava 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1842ce1326a2SPrarit Bhargava 			fprintf(stderr,
1843ce1326a2SPrarit Bhargava 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1844ce1326a2SPrarit Bhargava 		}
18453fb4f7cdSSrinivas Pandruvada 		exit(0);
18463fb4f7cdSSrinivas Pandruvada 	}
18473fb4f7cdSSrinivas Pandruvada 
18483fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
18493fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
18503fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1851ce1326a2SPrarit Bhargava 						  NULL, &enable);
18523fb4f7cdSSrinivas Pandruvada 	else
18533fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
1854ce1326a2SPrarit Bhargava 					       NULL, &enable);
18553fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
1856a6a82f9bSSrinivas Pandruvada 
185714a8aa49SSrinivas Pandruvada 	if (!fact_enable_fail && enable && auto_mode) {
1858a6a82f9bSSrinivas Pandruvada 		/*
1859a6a82f9bSSrinivas Pandruvada 		 * When we adjust CLOS param, we have to set for siblings also.
1860a6a82f9bSSrinivas Pandruvada 		 * So for the each user specified CPU, also add the sibling
1861a6a82f9bSSrinivas Pandruvada 		 * in the present_cpu_mask.
1862a6a82f9bSSrinivas Pandruvada 		 */
1863a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
1864a6a82f9bSSrinivas Pandruvada 			char buffer[128], sibling_list[128], *cpu_str;
1865a6a82f9bSSrinivas Pandruvada 			int fd, len;
1866a6a82f9bSSrinivas Pandruvada 
1867a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1868a6a82f9bSSrinivas Pandruvada 				continue;
1869a6a82f9bSSrinivas Pandruvada 
1870a6a82f9bSSrinivas Pandruvada 			snprintf(buffer, sizeof(buffer),
1871a6a82f9bSSrinivas Pandruvada 				 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1872a6a82f9bSSrinivas Pandruvada 
1873a6a82f9bSSrinivas Pandruvada 			fd = open(buffer, O_RDONLY);
1874a6a82f9bSSrinivas Pandruvada 			if (fd < 0)
1875a6a82f9bSSrinivas Pandruvada 				continue;
1876a6a82f9bSSrinivas Pandruvada 
1877a6a82f9bSSrinivas Pandruvada 			len = read(fd, sibling_list, sizeof(sibling_list));
1878a6a82f9bSSrinivas Pandruvada 			close(fd);
1879a6a82f9bSSrinivas Pandruvada 
1880a6a82f9bSSrinivas Pandruvada 			if (len < 0)
1881a6a82f9bSSrinivas Pandruvada 				continue;
1882a6a82f9bSSrinivas Pandruvada 
1883a6a82f9bSSrinivas Pandruvada 			cpu_str = strtok(sibling_list, ",");
1884a6a82f9bSSrinivas Pandruvada 			while (cpu_str != NULL) {
1885a6a82f9bSSrinivas Pandruvada 				int cpu;
1886a6a82f9bSSrinivas Pandruvada 
1887a6a82f9bSSrinivas Pandruvada 				sscanf(cpu_str, "%d", &cpu);
1888a6a82f9bSSrinivas Pandruvada 				CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
1889a6a82f9bSSrinivas Pandruvada 				cpu_str = strtok(NULL, ",");
1890a6a82f9bSSrinivas Pandruvada 			}
1891a6a82f9bSSrinivas Pandruvada 		}
1892a6a82f9bSSrinivas Pandruvada 
1893a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
1894a6a82f9bSSrinivas Pandruvada 			int clos;
1895a6a82f9bSSrinivas Pandruvada 
1896a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1897a6a82f9bSSrinivas Pandruvada 				continue;
1898a6a82f9bSSrinivas Pandruvada 
1899a6a82f9bSSrinivas Pandruvada 			ret = set_clos_param(i, 0, 0, 0, 0, 0xff);
1900a6a82f9bSSrinivas Pandruvada 			if (ret)
1901a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1902a6a82f9bSSrinivas Pandruvada 
190321c3390dSSrinivas Pandruvada 			ret = set_clos_param(i, 1, 15, 15, 0, 0xff);
1904a6a82f9bSSrinivas Pandruvada 			if (ret)
1905a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1906a6a82f9bSSrinivas Pandruvada 
190721c3390dSSrinivas Pandruvada 			ret = set_clos_param(i, 2, 15, 15, 0, 0xff);
1908a6a82f9bSSrinivas Pandruvada 			if (ret)
1909a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1910a6a82f9bSSrinivas Pandruvada 
191121c3390dSSrinivas Pandruvada 			ret = set_clos_param(i, 3, 15, 15, 0, 0xff);
1912a6a82f9bSSrinivas Pandruvada 			if (ret)
1913a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1914a6a82f9bSSrinivas Pandruvada 
1915a6a82f9bSSrinivas Pandruvada 			if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1916a6a82f9bSSrinivas Pandruvada 				clos = 0;
1917a6a82f9bSSrinivas Pandruvada 			else
1918a6a82f9bSSrinivas Pandruvada 				clos = 3;
1919a6a82f9bSSrinivas Pandruvada 
1920a6a82f9bSSrinivas Pandruvada 			debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1921a6a82f9bSSrinivas Pandruvada 			ret = isst_clos_associate(i, clos);
1922a6a82f9bSSrinivas Pandruvada 			if (ret)
1923a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1924a6a82f9bSSrinivas Pandruvada 		}
192578e77b7dSSrinivas Pandruvada 		isst_display_result(-1, outf, "turbo-freq --auto", "enable", 0);
1926a6a82f9bSSrinivas Pandruvada 	}
1927a6a82f9bSSrinivas Pandruvada 
1928a6a82f9bSSrinivas Pandruvada 	return;
1929a6a82f9bSSrinivas Pandruvada 
1930a6a82f9bSSrinivas Pandruvada error_disp:
1931a6a82f9bSSrinivas Pandruvada 	isst_display_result(i, outf, "turbo-freq --auto", "enable", ret);
1932a6a82f9bSSrinivas Pandruvada 
19333fb4f7cdSSrinivas Pandruvada }
19343fb4f7cdSSrinivas Pandruvada 
19353fb4f7cdSSrinivas Pandruvada static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
19363fb4f7cdSSrinivas Pandruvada 				   void *arg4)
19373fb4f7cdSSrinivas Pandruvada {
19383fb4f7cdSSrinivas Pandruvada 	int ret;
19393fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
19403fb4f7cdSSrinivas Pandruvada 
1941fe6fb216SSrinivas Pandruvada 	if (is_skx_based_platform())
1942fe6fb216SSrinivas Pandruvada 		clos_priority_type = 1;
1943fe6fb216SSrinivas Pandruvada 
19443fb4f7cdSSrinivas Pandruvada 	ret = isst_pm_qos_config(cpu, status, clos_priority_type);
1945a6a82f9bSSrinivas Pandruvada 	if (ret)
1946fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_qos_config failed", 0, 0);
1947a6a82f9bSSrinivas Pandruvada 
19483fb4f7cdSSrinivas Pandruvada 	if (status)
19493fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "enable",
19503fb4f7cdSSrinivas Pandruvada 				    ret);
19513fb4f7cdSSrinivas Pandruvada 	else
19523fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "disable",
19533fb4f7cdSSrinivas Pandruvada 				    ret);
19543fb4f7cdSSrinivas Pandruvada }
19553fb4f7cdSSrinivas Pandruvada 
1956ce1326a2SPrarit Bhargava static void set_clos_enable(int arg)
19573fb4f7cdSSrinivas Pandruvada {
1958ce1326a2SPrarit Bhargava 	int enable = arg;
19593fb4f7cdSSrinivas Pandruvada 
19603fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1961ce1326a2SPrarit Bhargava 		if (enable) {
1962ce1326a2SPrarit Bhargava 			fprintf(stderr,
1963ce1326a2SPrarit Bhargava 				"Enable core-power for a package/die\n");
1964fe6fb216SSrinivas Pandruvada 			if (!is_skx_based_platform()) {
19653fb4f7cdSSrinivas Pandruvada 				fprintf(stderr,
19663fb4f7cdSSrinivas Pandruvada 					"\tClos Enable: Specify priority type with [--priority|-p]\n");
19673fb4f7cdSSrinivas Pandruvada 				fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
1968fe6fb216SSrinivas Pandruvada 			}
1969ce1326a2SPrarit Bhargava 		} else {
1970ce1326a2SPrarit Bhargava 			fprintf(stderr,
1971ce1326a2SPrarit Bhargava 				"Disable core-power: [No command arguments are required]\n");
1972ce1326a2SPrarit Bhargava 		}
19733fb4f7cdSSrinivas Pandruvada 		exit(0);
19743fb4f7cdSSrinivas Pandruvada 	}
19753fb4f7cdSSrinivas Pandruvada 
1976ce1326a2SPrarit Bhargava 	if (enable && cpufreq_sysfs_present()) {
19773fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
19783fb4f7cdSSrinivas Pandruvada 			"cpufreq subsystem and core-power enable will interfere with each other!\n");
19793fb4f7cdSSrinivas Pandruvada 	}
19803fb4f7cdSSrinivas Pandruvada 
19813fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
19823fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
19833fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1984ce1326a2SPrarit Bhargava 						  NULL, NULL, &enable);
19853fb4f7cdSSrinivas Pandruvada 	else
19863fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(enable_clos_qos_config, NULL,
1987ce1326a2SPrarit Bhargava 					       NULL, NULL, &enable);
19883fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
19893fb4f7cdSSrinivas Pandruvada }
19903fb4f7cdSSrinivas Pandruvada 
19913fb4f7cdSSrinivas Pandruvada static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
19923fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
19933fb4f7cdSSrinivas Pandruvada {
19943fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
19953fb4f7cdSSrinivas Pandruvada 	int ret;
19963fb4f7cdSSrinivas Pandruvada 
19973fb4f7cdSSrinivas Pandruvada 	ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
19983fb4f7cdSSrinivas Pandruvada 	if (ret)
1999fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_pm_get_clos failed", 0, 0);
20003fb4f7cdSSrinivas Pandruvada 	else
20013fb4f7cdSSrinivas Pandruvada 		isst_clos_display_information(cpu, outf, current_clos,
20023fb4f7cdSSrinivas Pandruvada 					      &clos_config);
20033fb4f7cdSSrinivas Pandruvada }
20043fb4f7cdSSrinivas Pandruvada 
2005ce1326a2SPrarit Bhargava static void dump_clos_config(int arg)
20063fb4f7cdSSrinivas Pandruvada {
20073fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
20083fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
20093fb4f7cdSSrinivas Pandruvada 			"Print Intel Speed Select Technology core power configuration\n");
20103fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
20113fb4f7cdSSrinivas Pandruvada 			"\tArguments: [-c | --clos]: Specify clos id\n");
20123fb4f7cdSSrinivas Pandruvada 		exit(0);
20133fb4f7cdSSrinivas Pandruvada 	}
20143fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2015fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
2016fe6fb216SSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
20173fb4f7cdSSrinivas Pandruvada 		exit(0);
20183fb4f7cdSSrinivas Pandruvada 	}
20193fb4f7cdSSrinivas Pandruvada 
20203fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
20213fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
20223fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
20233fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
20243fb4f7cdSSrinivas Pandruvada 	else
20253fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
20263fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
20273fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
20283fb4f7cdSSrinivas Pandruvada }
20293fb4f7cdSSrinivas Pandruvada 
2030188afed9SSrinivas Pandruvada static void get_clos_info_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2031188afed9SSrinivas Pandruvada 				  void *arg4)
2032188afed9SSrinivas Pandruvada {
2033188afed9SSrinivas Pandruvada 	int enable, ret, prio_type;
2034188afed9SSrinivas Pandruvada 
2035188afed9SSrinivas Pandruvada 	ret = isst_clos_get_clos_information(cpu, &enable, &prio_type);
2036188afed9SSrinivas Pandruvada 	if (ret)
2037fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_clos_get_info failed", 0, 0);
2038143ad322SSrinivas Pandruvada 	else {
2039143ad322SSrinivas Pandruvada 		int cp_state, cp_cap;
2040143ad322SSrinivas Pandruvada 
2041143ad322SSrinivas Pandruvada 		isst_read_pm_config(cpu, &cp_state, &cp_cap);
2042143ad322SSrinivas Pandruvada 		isst_clos_display_clos_information(cpu, outf, enable, prio_type,
2043143ad322SSrinivas Pandruvada 						   cp_state, cp_cap);
2044143ad322SSrinivas Pandruvada 	}
2045188afed9SSrinivas Pandruvada }
2046188afed9SSrinivas Pandruvada 
2047ce1326a2SPrarit Bhargava static void dump_clos_info(int arg)
2048188afed9SSrinivas Pandruvada {
2049188afed9SSrinivas Pandruvada 	if (cmd_help) {
2050188afed9SSrinivas Pandruvada 		fprintf(stderr,
2051188afed9SSrinivas Pandruvada 			"Print Intel Speed Select Technology core power information\n");
2052f5205f49SSrinivas Pandruvada 		fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2053188afed9SSrinivas Pandruvada 		exit(0);
2054188afed9SSrinivas Pandruvada 	}
2055188afed9SSrinivas Pandruvada 
2056188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2057f5205f49SSrinivas Pandruvada 	if (max_target_cpus)
2058188afed9SSrinivas Pandruvada 		for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2059188afed9SSrinivas Pandruvada 						  NULL, NULL, NULL);
2060f5205f49SSrinivas Pandruvada 	else
2061f5205f49SSrinivas Pandruvada 		for_each_online_package_in_set(get_clos_info_for_cpu, NULL,
2062f5205f49SSrinivas Pandruvada 					       NULL, NULL, NULL);
2063188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
2064188afed9SSrinivas Pandruvada 
2065188afed9SSrinivas Pandruvada }
2066188afed9SSrinivas Pandruvada 
20673fb4f7cdSSrinivas Pandruvada static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
20683fb4f7cdSSrinivas Pandruvada 				    void *arg4)
20693fb4f7cdSSrinivas Pandruvada {
20703fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
20713fb4f7cdSSrinivas Pandruvada 	int ret;
20723fb4f7cdSSrinivas Pandruvada 
20733fb4f7cdSSrinivas Pandruvada 	clos_config.pkg_id = get_physical_package_id(cpu);
20743fb4f7cdSSrinivas Pandruvada 	clos_config.die_id = get_physical_die_id(cpu);
20753fb4f7cdSSrinivas Pandruvada 
20763fb4f7cdSSrinivas Pandruvada 	clos_config.epp = clos_epp;
20773fb4f7cdSSrinivas Pandruvada 	clos_config.clos_prop_prio = clos_prop_prio;
20783fb4f7cdSSrinivas Pandruvada 	clos_config.clos_min = clos_min;
20793fb4f7cdSSrinivas Pandruvada 	clos_config.clos_max = clos_max;
20803fb4f7cdSSrinivas Pandruvada 	clos_config.clos_desired = clos_desired;
20813fb4f7cdSSrinivas Pandruvada 	ret = isst_set_clos(cpu, current_clos, &clos_config);
20823fb4f7cdSSrinivas Pandruvada 	if (ret)
2083fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_set_clos failed", 0, 0);
20843fb4f7cdSSrinivas Pandruvada 	else
20853fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "config", ret);
20863fb4f7cdSSrinivas Pandruvada }
20873fb4f7cdSSrinivas Pandruvada 
2088ce1326a2SPrarit Bhargava static void set_clos_config(int arg)
20893fb4f7cdSSrinivas Pandruvada {
20903fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
20913fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
20923fb4f7cdSSrinivas Pandruvada 			"Set core-power configuration for one of the four clos ids\n");
20933fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
20943fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
2095fe6fb216SSrinivas Pandruvada 		if (!is_skx_based_platform()) {
20963fb4f7cdSSrinivas Pandruvada 			fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
20973fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
20983fb4f7cdSSrinivas Pandruvada 				"\tSpecify clos Proportional Priority [--weight|-w]\n");
2099fe6fb216SSrinivas Pandruvada 		}
210040dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
210140dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
21023fb4f7cdSSrinivas Pandruvada 		exit(0);
21033fb4f7cdSSrinivas Pandruvada 	}
21043fb4f7cdSSrinivas Pandruvada 
21053fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2106fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
21073fb4f7cdSSrinivas Pandruvada 		exit(0);
21083fb4f7cdSSrinivas Pandruvada 	}
2109fe6fb216SSrinivas Pandruvada 	if (!is_skx_based_platform() && (clos_epp < 0 || clos_epp > 0x0F)) {
2110fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos epp is not specified or invalid, default: 0\n");
21113fb4f7cdSSrinivas Pandruvada 		clos_epp = 0;
21123fb4f7cdSSrinivas Pandruvada 	}
2113fe6fb216SSrinivas Pandruvada 	if (!is_skx_based_platform() && (clos_prop_prio < 0 || clos_prop_prio > 0x0F)) {
21143fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
2115fe6fb216SSrinivas Pandruvada 			"clos frequency weight is not specified or invalid, default: 0\n");
21163fb4f7cdSSrinivas Pandruvada 		clos_prop_prio = 0;
21173fb4f7cdSSrinivas Pandruvada 	}
21183fb4f7cdSSrinivas Pandruvada 	if (clos_min < 0) {
21193fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "clos min is not specified, default: 0\n");
21203fb4f7cdSSrinivas Pandruvada 		clos_min = 0;
21213fb4f7cdSSrinivas Pandruvada 	}
21223fb4f7cdSSrinivas Pandruvada 	if (clos_max < 0) {
2123fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos max is not specified, default: Max frequency (ratio 0xff)\n");
21243fb4f7cdSSrinivas Pandruvada 		clos_max = 0xff;
21253fb4f7cdSSrinivas Pandruvada 	}
2126fe6fb216SSrinivas Pandruvada 	if (clos_desired) {
2127fe6fb216SSrinivas Pandruvada 		fprintf(stderr, "clos desired is not supported on this platform\n");
21283fb4f7cdSSrinivas Pandruvada 		clos_desired = 0x00;
21293fb4f7cdSSrinivas Pandruvada 	}
21303fb4f7cdSSrinivas Pandruvada 
21313fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
21323fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21333fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
21343fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
21353fb4f7cdSSrinivas Pandruvada 	else
21363fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
21373fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
21383fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
21393fb4f7cdSSrinivas Pandruvada }
21403fb4f7cdSSrinivas Pandruvada 
21413fb4f7cdSSrinivas Pandruvada static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
21423fb4f7cdSSrinivas Pandruvada 				   void *arg4)
21433fb4f7cdSSrinivas Pandruvada {
21443fb4f7cdSSrinivas Pandruvada 	int ret;
21453fb4f7cdSSrinivas Pandruvada 
21463fb4f7cdSSrinivas Pandruvada 	ret = isst_clos_associate(cpu, current_clos);
21473fb4f7cdSSrinivas Pandruvada 	if (ret)
2148fe6fb216SSrinivas Pandruvada 		debug_printf("isst_clos_associate failed");
21493fb4f7cdSSrinivas Pandruvada 	else
21503fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "assoc", ret);
21513fb4f7cdSSrinivas Pandruvada }
21523fb4f7cdSSrinivas Pandruvada 
2153ce1326a2SPrarit Bhargava static void set_clos_assoc(int arg)
21543fb4f7cdSSrinivas Pandruvada {
21553fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
21563fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Associate a clos id to a CPU\n");
21573fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21583fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
215968e2f109SSrinivas Pandruvada 		fprintf(stderr,
216068e2f109SSrinivas Pandruvada 			"\tFor example to associate clos 1 to CPU 0: issue\n");
216168e2f109SSrinivas Pandruvada 		fprintf(stderr,
216268e2f109SSrinivas Pandruvada 			"\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
21633fb4f7cdSSrinivas Pandruvada 		exit(0);
21643fb4f7cdSSrinivas Pandruvada 	}
21653fb4f7cdSSrinivas Pandruvada 
21663fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
2167fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid clos id\n", 0, 0);
21683fb4f7cdSSrinivas Pandruvada 		exit(0);
21693fb4f7cdSSrinivas Pandruvada 	}
21703fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21713fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
21723fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
21733fb4f7cdSSrinivas Pandruvada 	else {
2174fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
21753fb4f7cdSSrinivas Pandruvada 	}
21763fb4f7cdSSrinivas Pandruvada }
21773fb4f7cdSSrinivas Pandruvada 
21783fb4f7cdSSrinivas Pandruvada static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
21793fb4f7cdSSrinivas Pandruvada 				   void *arg4)
21803fb4f7cdSSrinivas Pandruvada {
21813fb4f7cdSSrinivas Pandruvada 	int clos, ret;
21823fb4f7cdSSrinivas Pandruvada 
21833fb4f7cdSSrinivas Pandruvada 	ret = isst_clos_get_assoc_status(cpu, &clos);
21843fb4f7cdSSrinivas Pandruvada 	if (ret)
2185fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "isst_clos_get_assoc_status failed", 0, 0);
21863fb4f7cdSSrinivas Pandruvada 	else
2187e118fbe3SSrinivas Pandruvada 		isst_clos_display_assoc_information(cpu, outf, clos);
21883fb4f7cdSSrinivas Pandruvada }
21893fb4f7cdSSrinivas Pandruvada 
2190ce1326a2SPrarit Bhargava static void get_clos_assoc(int arg)
21913fb4f7cdSSrinivas Pandruvada {
21923fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
21933fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Get associate clos id to a CPU\n");
21943fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
21953fb4f7cdSSrinivas Pandruvada 		exit(0);
21963fb4f7cdSSrinivas Pandruvada 	}
2197e118fbe3SSrinivas Pandruvada 
2198e118fbe3SSrinivas Pandruvada 	if (!max_target_cpus) {
2199fe6fb216SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid target cpu. Specify with [-c|--cpu]", 0, 0);
2200e118fbe3SSrinivas Pandruvada 		exit(0);
22013fb4f7cdSSrinivas Pandruvada 	}
2202e118fbe3SSrinivas Pandruvada 
2203e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2204e118fbe3SSrinivas Pandruvada 	for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2205e118fbe3SSrinivas Pandruvada 					  NULL, NULL, NULL);
2206e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
22073fb4f7cdSSrinivas Pandruvada }
22083fb4f7cdSSrinivas Pandruvada 
2209c829f0efSPrarit Bhargava static struct process_cmd_struct clx_n_cmds[] = {
2210062e4aacSPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
22111aa7177cSPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
22121aa7177cSPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
22131aa7177cSPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2214c829f0efSPrarit Bhargava 	{ NULL, NULL, NULL, 0 }
2215c829f0efSPrarit Bhargava };
2216c829f0efSPrarit Bhargava 
22173fb4f7cdSSrinivas Pandruvada static struct process_cmd_struct isst_cmds[] = {
2218ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2219ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2220ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-version", get_tdp_version, 0 },
2221ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2222ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-current-level", get_tdp_current_level,
2223ce1326a2SPrarit Bhargava 	 0 },
2224ce1326a2SPrarit Bhargava 	{ "perf-profile", "set-config-level", set_tdp_level, 0 },
2225ce1326a2SPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
2226ce1326a2SPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
2227ce1326a2SPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
2228ce1326a2SPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2229ce1326a2SPrarit Bhargava 	{ "turbo-freq", "info", dump_fact_config, 0 },
2230ce1326a2SPrarit Bhargava 	{ "turbo-freq", "enable", set_fact_enable, 1 },
2231ce1326a2SPrarit Bhargava 	{ "turbo-freq", "disable", set_fact_enable, 0 },
2232ce1326a2SPrarit Bhargava 	{ "core-power", "info", dump_clos_info, 0 },
2233ce1326a2SPrarit Bhargava 	{ "core-power", "enable", set_clos_enable, 1 },
2234ce1326a2SPrarit Bhargava 	{ "core-power", "disable", set_clos_enable, 0 },
2235ce1326a2SPrarit Bhargava 	{ "core-power", "config", set_clos_config, 0 },
2236ce1326a2SPrarit Bhargava 	{ "core-power", "get-config", dump_clos_config, 0 },
2237ce1326a2SPrarit Bhargava 	{ "core-power", "assoc", set_clos_assoc, 0 },
2238ce1326a2SPrarit Bhargava 	{ "core-power", "get-assoc", get_clos_assoc, 0 },
22393fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL, NULL }
22403fb4f7cdSSrinivas Pandruvada };
22413fb4f7cdSSrinivas Pandruvada 
22423fb4f7cdSSrinivas Pandruvada /*
22433fb4f7cdSSrinivas Pandruvada  * parse cpuset with following syntax
22443fb4f7cdSSrinivas Pandruvada  * 1,2,4..6,8-10 and set bits in cpu_subset
22453fb4f7cdSSrinivas Pandruvada  */
22463fb4f7cdSSrinivas Pandruvada void parse_cpu_command(char *optarg)
22473fb4f7cdSSrinivas Pandruvada {
22483fb4f7cdSSrinivas Pandruvada 	unsigned int start, end;
22493fb4f7cdSSrinivas Pandruvada 	char *next;
22503fb4f7cdSSrinivas Pandruvada 
22513fb4f7cdSSrinivas Pandruvada 	next = optarg;
22523fb4f7cdSSrinivas Pandruvada 
22533fb4f7cdSSrinivas Pandruvada 	while (next && *next) {
22543fb4f7cdSSrinivas Pandruvada 		if (*next == '-') /* no negative cpu numbers */
22553fb4f7cdSSrinivas Pandruvada 			goto error;
22563fb4f7cdSSrinivas Pandruvada 
22573fb4f7cdSSrinivas Pandruvada 		start = strtoul(next, &next, 10);
22583fb4f7cdSSrinivas Pandruvada 
22593fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
22603fb4f7cdSSrinivas Pandruvada 			target_cpus[max_target_cpus++] = start;
22613fb4f7cdSSrinivas Pandruvada 
22623fb4f7cdSSrinivas Pandruvada 		if (*next == '\0')
22633fb4f7cdSSrinivas Pandruvada 			break;
22643fb4f7cdSSrinivas Pandruvada 
22653fb4f7cdSSrinivas Pandruvada 		if (*next == ',') {
22663fb4f7cdSSrinivas Pandruvada 			next += 1;
22673fb4f7cdSSrinivas Pandruvada 			continue;
22683fb4f7cdSSrinivas Pandruvada 		}
22693fb4f7cdSSrinivas Pandruvada 
22703fb4f7cdSSrinivas Pandruvada 		if (*next == '-') {
22713fb4f7cdSSrinivas Pandruvada 			next += 1; /* start range */
22723fb4f7cdSSrinivas Pandruvada 		} else if (*next == '.') {
22733fb4f7cdSSrinivas Pandruvada 			next += 1;
22743fb4f7cdSSrinivas Pandruvada 			if (*next == '.')
22753fb4f7cdSSrinivas Pandruvada 				next += 1; /* start range */
22763fb4f7cdSSrinivas Pandruvada 			else
22773fb4f7cdSSrinivas Pandruvada 				goto error;
22783fb4f7cdSSrinivas Pandruvada 		}
22793fb4f7cdSSrinivas Pandruvada 
22803fb4f7cdSSrinivas Pandruvada 		end = strtoul(next, &next, 10);
22813fb4f7cdSSrinivas Pandruvada 		if (end <= start)
22823fb4f7cdSSrinivas Pandruvada 			goto error;
22833fb4f7cdSSrinivas Pandruvada 
22843fb4f7cdSSrinivas Pandruvada 		while (++start <= end) {
22853fb4f7cdSSrinivas Pandruvada 			if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
22863fb4f7cdSSrinivas Pandruvada 				target_cpus[max_target_cpus++] = start;
22873fb4f7cdSSrinivas Pandruvada 		}
22883fb4f7cdSSrinivas Pandruvada 
22893fb4f7cdSSrinivas Pandruvada 		if (*next == ',')
22903fb4f7cdSSrinivas Pandruvada 			next += 1;
22913fb4f7cdSSrinivas Pandruvada 		else if (*next != '\0')
22923fb4f7cdSSrinivas Pandruvada 			goto error;
22933fb4f7cdSSrinivas Pandruvada 	}
22943fb4f7cdSSrinivas Pandruvada 
22953fb4f7cdSSrinivas Pandruvada #ifdef DEBUG
22963fb4f7cdSSrinivas Pandruvada 	{
22973fb4f7cdSSrinivas Pandruvada 		int i;
22983fb4f7cdSSrinivas Pandruvada 
22993fb4f7cdSSrinivas Pandruvada 		for (i = 0; i < max_target_cpus; ++i)
23003fb4f7cdSSrinivas Pandruvada 			printf("cpu [%d] in arg\n", target_cpus[i]);
23013fb4f7cdSSrinivas Pandruvada 	}
23023fb4f7cdSSrinivas Pandruvada #endif
23033fb4f7cdSSrinivas Pandruvada 	return;
23043fb4f7cdSSrinivas Pandruvada 
23053fb4f7cdSSrinivas Pandruvada error:
23063fb4f7cdSSrinivas Pandruvada 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
23073fb4f7cdSSrinivas Pandruvada 	exit(-1);
23083fb4f7cdSSrinivas Pandruvada }
23093fb4f7cdSSrinivas Pandruvada 
23103fb4f7cdSSrinivas Pandruvada static void parse_cmd_args(int argc, int start, char **argv)
23113fb4f7cdSSrinivas Pandruvada {
23123fb4f7cdSSrinivas Pandruvada 	int opt;
23133fb4f7cdSSrinivas Pandruvada 	int option_index;
23143fb4f7cdSSrinivas Pandruvada 
23153fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
23163fb4f7cdSSrinivas Pandruvada 		{ "bucket", required_argument, 0, 'b' },
23173fb4f7cdSSrinivas Pandruvada 		{ "level", required_argument, 0, 'l' },
23183c64c81aSSrinivas Pandruvada 		{ "online", required_argument, 0, 'o' },
23193fb4f7cdSSrinivas Pandruvada 		{ "trl-type", required_argument, 0, 'r' },
23203fb4f7cdSSrinivas Pandruvada 		{ "trl", required_argument, 0, 't' },
23213fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
23223fb4f7cdSSrinivas Pandruvada 		{ "clos", required_argument, 0, 'c' },
23233fb4f7cdSSrinivas Pandruvada 		{ "desired", required_argument, 0, 'd' },
23243fb4f7cdSSrinivas Pandruvada 		{ "epp", required_argument, 0, 'e' },
23253fb4f7cdSSrinivas Pandruvada 		{ "min", required_argument, 0, 'n' },
23263fb4f7cdSSrinivas Pandruvada 		{ "max", required_argument, 0, 'm' },
23273fb4f7cdSSrinivas Pandruvada 		{ "priority", required_argument, 0, 'p' },
23283fb4f7cdSSrinivas Pandruvada 		{ "weight", required_argument, 0, 'w' },
2329354bd06fSSrinivas Pandruvada 		{ "auto", no_argument, 0, 'a' },
23303fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
23313fb4f7cdSSrinivas Pandruvada 	};
23323fb4f7cdSSrinivas Pandruvada 
23333fb4f7cdSSrinivas Pandruvada 	option_index = start;
23343fb4f7cdSSrinivas Pandruvada 
23353fb4f7cdSSrinivas Pandruvada 	optind = start + 1;
2336354bd06fSSrinivas Pandruvada 	while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:hoa",
23373fb4f7cdSSrinivas Pandruvada 				  long_options, &option_index)) != -1) {
23383fb4f7cdSSrinivas Pandruvada 		switch (opt) {
2339354bd06fSSrinivas Pandruvada 		case 'a':
2340354bd06fSSrinivas Pandruvada 			auto_mode = 1;
2341354bd06fSSrinivas Pandruvada 			break;
23423fb4f7cdSSrinivas Pandruvada 		case 'b':
23433fb4f7cdSSrinivas Pandruvada 			fact_bucket = atoi(optarg);
23443fb4f7cdSSrinivas Pandruvada 			break;
23453fb4f7cdSSrinivas Pandruvada 		case 'h':
23463fb4f7cdSSrinivas Pandruvada 			cmd_help = 1;
23473fb4f7cdSSrinivas Pandruvada 			break;
23483fb4f7cdSSrinivas Pandruvada 		case 'l':
23493fb4f7cdSSrinivas Pandruvada 			tdp_level = atoi(optarg);
23503fb4f7cdSSrinivas Pandruvada 			break;
23513c64c81aSSrinivas Pandruvada 		case 'o':
23523c64c81aSSrinivas Pandruvada 			force_online_offline = 1;
23533c64c81aSSrinivas Pandruvada 			break;
23543fb4f7cdSSrinivas Pandruvada 		case 't':
23553fb4f7cdSSrinivas Pandruvada 			sscanf(optarg, "0x%llx", &fact_trl);
23563fb4f7cdSSrinivas Pandruvada 			break;
23573fb4f7cdSSrinivas Pandruvada 		case 'r':
23583fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "sse", 3)) {
23593fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x01;
23603fb4f7cdSSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx2", 4)) {
23613fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x02;
23623fb4f7cdSSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx512", 4)) {
23633fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x04;
23643fb4f7cdSSrinivas Pandruvada 			} else {
23653fb4f7cdSSrinivas Pandruvada 				fprintf(outf, "Invalid sse,avx options\n");
23663fb4f7cdSSrinivas Pandruvada 				exit(1);
23673fb4f7cdSSrinivas Pandruvada 			}
23683fb4f7cdSSrinivas Pandruvada 			break;
23693fb4f7cdSSrinivas Pandruvada 		/* CLOS related */
23703fb4f7cdSSrinivas Pandruvada 		case 'c':
23713fb4f7cdSSrinivas Pandruvada 			current_clos = atoi(optarg);
23723fb4f7cdSSrinivas Pandruvada 			break;
23733fb4f7cdSSrinivas Pandruvada 		case 'd':
23743fb4f7cdSSrinivas Pandruvada 			clos_desired = atoi(optarg);
237540dee9ddSSrinivas Pandruvada 			clos_desired /= DISP_FREQ_MULTIPLIER;
23763fb4f7cdSSrinivas Pandruvada 			break;
23773fb4f7cdSSrinivas Pandruvada 		case 'e':
23783fb4f7cdSSrinivas Pandruvada 			clos_epp = atoi(optarg);
2379fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform()) {
2380fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "epp can't be specified on this platform", 0, 0);
2381fe6fb216SSrinivas Pandruvada 				exit(0);
2382fe6fb216SSrinivas Pandruvada 			}
23833fb4f7cdSSrinivas Pandruvada 			break;
23843fb4f7cdSSrinivas Pandruvada 		case 'n':
23853fb4f7cdSSrinivas Pandruvada 			clos_min = atoi(optarg);
238640dee9ddSSrinivas Pandruvada 			clos_min /= DISP_FREQ_MULTIPLIER;
23873fb4f7cdSSrinivas Pandruvada 			break;
23883fb4f7cdSSrinivas Pandruvada 		case 'm':
23893fb4f7cdSSrinivas Pandruvada 			clos_max = atoi(optarg);
239040dee9ddSSrinivas Pandruvada 			clos_max /= DISP_FREQ_MULTIPLIER;
23913fb4f7cdSSrinivas Pandruvada 			break;
23923fb4f7cdSSrinivas Pandruvada 		case 'p':
23933fb4f7cdSSrinivas Pandruvada 			clos_priority_type = atoi(optarg);
2394fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform() && !clos_priority_type) {
2395fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "Invalid clos priority type: proportional for this platform", 0, 0);
2396fe6fb216SSrinivas Pandruvada 				exit(0);
2397fe6fb216SSrinivas Pandruvada 			}
23983fb4f7cdSSrinivas Pandruvada 			break;
23993fb4f7cdSSrinivas Pandruvada 		case 'w':
24003fb4f7cdSSrinivas Pandruvada 			clos_prop_prio = atoi(optarg);
2401fe6fb216SSrinivas Pandruvada 			if (is_skx_based_platform()) {
2402fe6fb216SSrinivas Pandruvada 				isst_display_error_info_message(1, "weight can't be specified on this platform", 0, 0);
2403fe6fb216SSrinivas Pandruvada 				exit(0);
2404fe6fb216SSrinivas Pandruvada 			}
24053fb4f7cdSSrinivas Pandruvada 			break;
24063fb4f7cdSSrinivas Pandruvada 		default:
24073fb4f7cdSSrinivas Pandruvada 			printf("no match\n");
24083fb4f7cdSSrinivas Pandruvada 		}
24093fb4f7cdSSrinivas Pandruvada 	}
24103fb4f7cdSSrinivas Pandruvada }
24113fb4f7cdSSrinivas Pandruvada 
24123fb4f7cdSSrinivas Pandruvada static void isst_help(void)
24133fb4f7cdSSrinivas Pandruvada {
24143fb4f7cdSSrinivas Pandruvada 	printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
24153fb4f7cdSSrinivas Pandruvada 		performance profiles per system via static and/or dynamic\n\
24163fb4f7cdSSrinivas Pandruvada 		adjustment of core count, workload, Tjmax, and\n\
24173fb4f7cdSSrinivas Pandruvada 		TDP, etc.\n");
24183fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=perf-profile\n");
24193fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
2420c829f0efSPrarit Bhargava 
2421c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
24223fb4f7cdSSrinivas Pandruvada 		printf("\tget-lock-status\n");
24233fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-levels\n");
24243fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-version\n");
24253fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-enabled\n");
24263fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-current-level\n");
24273fb4f7cdSSrinivas Pandruvada 		printf("\tset-config-level\n");
24283fb4f7cdSSrinivas Pandruvada 	}
2429c829f0efSPrarit Bhargava }
24303fb4f7cdSSrinivas Pandruvada 
24313fb4f7cdSSrinivas Pandruvada static void pbf_help(void)
24323fb4f7cdSSrinivas Pandruvada {
24333fb4f7cdSSrinivas Pandruvada 	printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
24343fb4f7cdSSrinivas Pandruvada 		on certain cores (high priority cores) in exchange for lower\n\
24353fb4f7cdSSrinivas Pandruvada 		base frequency on remaining cores (low priority cores).\n");
24363fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
24373fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
24383fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
24393fb4f7cdSSrinivas Pandruvada }
24403fb4f7cdSSrinivas Pandruvada 
24413fb4f7cdSSrinivas Pandruvada static void fact_help(void)
24423fb4f7cdSSrinivas Pandruvada {
24433fb4f7cdSSrinivas Pandruvada 	printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
24443fb4f7cdSSrinivas Pandruvada 		limits to cores based on priority.\n");
24453fb4f7cdSSrinivas Pandruvada 	printf("\nCommand: For feature=turbo-freq\n");
24463fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
24473fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
24483fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
24493fb4f7cdSSrinivas Pandruvada }
24503fb4f7cdSSrinivas Pandruvada 
24513fb4f7cdSSrinivas Pandruvada static void core_power_help(void)
24523fb4f7cdSSrinivas Pandruvada {
24533fb4f7cdSSrinivas Pandruvada 	printf("core-power:\tInterface that allows user to define per core/tile\n\
24543fb4f7cdSSrinivas Pandruvada 		priority.\n");
24553fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=core-power\n");
24563fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
24573fb4f7cdSSrinivas Pandruvada 	printf("\tenable\n");
24583fb4f7cdSSrinivas Pandruvada 	printf("\tdisable\n");
24593fb4f7cdSSrinivas Pandruvada 	printf("\tconfig\n");
2460188afed9SSrinivas Pandruvada 	printf("\tget-config\n");
24613fb4f7cdSSrinivas Pandruvada 	printf("\tassoc\n");
24623fb4f7cdSSrinivas Pandruvada 	printf("\tget-assoc\n");
24633fb4f7cdSSrinivas Pandruvada }
24643fb4f7cdSSrinivas Pandruvada 
24653fb4f7cdSSrinivas Pandruvada struct process_cmd_help_struct {
24663fb4f7cdSSrinivas Pandruvada 	char *feature;
24673fb4f7cdSSrinivas Pandruvada 	void (*process_fn)(void);
24683fb4f7cdSSrinivas Pandruvada };
24693fb4f7cdSSrinivas Pandruvada 
24703fb4f7cdSSrinivas Pandruvada static struct process_cmd_help_struct isst_help_cmds[] = {
24713fb4f7cdSSrinivas Pandruvada 	{ "perf-profile", isst_help },
24723fb4f7cdSSrinivas Pandruvada 	{ "base-freq", pbf_help },
24733fb4f7cdSSrinivas Pandruvada 	{ "turbo-freq", fact_help },
24743fb4f7cdSSrinivas Pandruvada 	{ "core-power", core_power_help },
24753fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL }
24763fb4f7cdSSrinivas Pandruvada };
24773fb4f7cdSSrinivas Pandruvada 
2478c829f0efSPrarit Bhargava static struct process_cmd_help_struct clx_n_help_cmds[] = {
2479c829f0efSPrarit Bhargava 	{ "perf-profile", isst_help },
2480c829f0efSPrarit Bhargava 	{ "base-freq", pbf_help },
2481c829f0efSPrarit Bhargava 	{ NULL, NULL }
2482c829f0efSPrarit Bhargava };
2483c829f0efSPrarit Bhargava 
2484210369dcSPrarit Bhargava void process_command(int argc, char **argv,
2485210369dcSPrarit Bhargava 		     struct process_cmd_help_struct *help_cmds,
2486210369dcSPrarit Bhargava 		     struct process_cmd_struct *cmds)
24873fb4f7cdSSrinivas Pandruvada {
24883fb4f7cdSSrinivas Pandruvada 	int i = 0, matched = 0;
24893fb4f7cdSSrinivas Pandruvada 	char *feature = argv[optind];
24903fb4f7cdSSrinivas Pandruvada 	char *cmd = argv[optind + 1];
24913fb4f7cdSSrinivas Pandruvada 
24923fb4f7cdSSrinivas Pandruvada 	if (!feature || !cmd)
24933fb4f7cdSSrinivas Pandruvada 		return;
24943fb4f7cdSSrinivas Pandruvada 
24953fb4f7cdSSrinivas Pandruvada 	debug_printf("feature name [%s] command [%s]\n", feature, cmd);
24963fb4f7cdSSrinivas Pandruvada 	if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
2497210369dcSPrarit Bhargava 		while (help_cmds[i].feature) {
2498210369dcSPrarit Bhargava 			if (!strcmp(help_cmds[i].feature, feature)) {
2499210369dcSPrarit Bhargava 				help_cmds[i].process_fn();
25003fb4f7cdSSrinivas Pandruvada 				exit(0);
25013fb4f7cdSSrinivas Pandruvada 			}
25023fb4f7cdSSrinivas Pandruvada 			++i;
25033fb4f7cdSSrinivas Pandruvada 		}
25043fb4f7cdSSrinivas Pandruvada 	}
25053fb4f7cdSSrinivas Pandruvada 
2506062e4aacSPrarit Bhargava 	if (!is_clx_n_platform())
25073fb4f7cdSSrinivas Pandruvada 		create_cpu_map();
25083fb4f7cdSSrinivas Pandruvada 
25093fb4f7cdSSrinivas Pandruvada 	i = 0;
2510210369dcSPrarit Bhargava 	while (cmds[i].feature) {
2511210369dcSPrarit Bhargava 		if (!strcmp(cmds[i].feature, feature) &&
2512210369dcSPrarit Bhargava 		    !strcmp(cmds[i].command, cmd)) {
25133fb4f7cdSSrinivas Pandruvada 			parse_cmd_args(argc, optind + 1, argv);
2514210369dcSPrarit Bhargava 			cmds[i].process_fn(cmds[i].arg);
25153fb4f7cdSSrinivas Pandruvada 			matched = 1;
25163fb4f7cdSSrinivas Pandruvada 			break;
25173fb4f7cdSSrinivas Pandruvada 		}
25183fb4f7cdSSrinivas Pandruvada 		++i;
25193fb4f7cdSSrinivas Pandruvada 	}
25203fb4f7cdSSrinivas Pandruvada 
25213fb4f7cdSSrinivas Pandruvada 	if (!matched)
25223fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Invalid command\n");
25233fb4f7cdSSrinivas Pandruvada }
25243fb4f7cdSSrinivas Pandruvada 
25253fb4f7cdSSrinivas Pandruvada static void usage(void)
25263fb4f7cdSSrinivas Pandruvada {
2527addd116dSSrinivas Pandruvada 	if (is_clx_n_platform()) {
2528addd116dSSrinivas Pandruvada 		fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
2529addd116dSSrinivas Pandruvada 		fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
2530addd116dSSrinivas Pandruvada 	}
2531addd116dSSrinivas Pandruvada 
25323fb4f7cdSSrinivas Pandruvada 	printf("\nUsage:\n");
25333fb4f7cdSSrinivas Pandruvada 	printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
2534addd116dSSrinivas Pandruvada 	printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
2535addd116dSSrinivas Pandruvada 	if (is_clx_n_platform())
2536addd116dSSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq]\n");
2537addd116dSSrinivas Pandruvada 	else
25383fb4f7cdSSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power]\n");
253943774c0dSPrarit Bhargava 	printf("\nFor help on each feature, use -h|--help\n");
25403fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile -h\n");
25413fb4f7cdSSrinivas Pandruvada 
25423fb4f7cdSSrinivas Pandruvada 	printf("\nFor additional help on each command for a feature, use --h|--help\n");
25433fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
25443fb4f7cdSSrinivas Pandruvada 	printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
25453fb4f7cdSSrinivas Pandruvada 
25463fb4f7cdSSrinivas Pandruvada 	printf("\nOPTIONS\n");
25473fb4f7cdSSrinivas Pandruvada 	printf("\t[-c|--cpu] : logical cpu number\n");
25483fb4f7cdSSrinivas Pandruvada 	printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
25493fb4f7cdSSrinivas Pandruvada 	printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
25503fb4f7cdSSrinivas Pandruvada 	printf("\t[-d|--debug] : Debug mode\n");
2551addd116dSSrinivas Pandruvada 	printf("\t[-f|--format] : output format [json|text]. Default: text\n");
25523fb4f7cdSSrinivas Pandruvada 	printf("\t[-h|--help] : Print help\n");
25533fb4f7cdSSrinivas Pandruvada 	printf("\t[-i|--info] : Print platform information\n");
25543fb4f7cdSSrinivas Pandruvada 	printf("\t[-o|--out] : Output file\n");
25553fb4f7cdSSrinivas Pandruvada 	printf("\t\t\tDefault : stderr\n");
25563fb4f7cdSSrinivas Pandruvada 	printf("\t[-v|--version] : Print version\n");
25573fb4f7cdSSrinivas Pandruvada 
25583fb4f7cdSSrinivas Pandruvada 	printf("\nResult format\n");
25593fb4f7cdSSrinivas Pandruvada 	printf("\tResult display uses a common format for each command:\n");
25603fb4f7cdSSrinivas Pandruvada 	printf("\tResults are formatted in text/JSON with\n");
25613fb4f7cdSSrinivas Pandruvada 	printf("\t\tPackage, Die, CPU, and command specific results.\n");
2562addd116dSSrinivas Pandruvada 
2563addd116dSSrinivas Pandruvada 	printf("\nExamples\n");
2564addd116dSSrinivas Pandruvada 	printf("\tTo get platform information:\n");
2565addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select --info\n");
2566addd116dSSrinivas Pandruvada 	printf("\tTo get full perf-profile information dump:\n");
2567addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select perf-profile info\n");
2568addd116dSSrinivas Pandruvada 	printf("\tTo get full base-freq information dump:\n");
2569addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select base-freq info -l 0\n");
2570addd116dSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2571addd116dSSrinivas Pandruvada 		printf("\tTo get full turbo-freq information dump:\n");
2572addd116dSSrinivas Pandruvada 		printf("\t\tintel-speed-select turbo-freq info -l 0\n");
2573addd116dSSrinivas Pandruvada 	}
25743fb4f7cdSSrinivas Pandruvada 	exit(1);
25753fb4f7cdSSrinivas Pandruvada }
25763fb4f7cdSSrinivas Pandruvada 
25773fb4f7cdSSrinivas Pandruvada static void print_version(void)
25783fb4f7cdSSrinivas Pandruvada {
25793fb4f7cdSSrinivas Pandruvada 	fprintf(outf, "Version %s\n", version_str);
25803fb4f7cdSSrinivas Pandruvada 	fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
25813fb4f7cdSSrinivas Pandruvada 	exit(0);
25823fb4f7cdSSrinivas Pandruvada }
25833fb4f7cdSSrinivas Pandruvada 
25843fb4f7cdSSrinivas Pandruvada static void cmdline(int argc, char **argv)
25853fb4f7cdSSrinivas Pandruvada {
2586f362cdccSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
2587f362cdccSSrinivas Pandruvada 	FILE *fp;
25883fb4f7cdSSrinivas Pandruvada 	int opt;
25893fb4f7cdSSrinivas Pandruvada 	int option_index = 0;
25903bc3d30cSPrarit Bhargava 	int ret;
25913fb4f7cdSSrinivas Pandruvada 
25923fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
25933fb4f7cdSSrinivas Pandruvada 		{ "cpu", required_argument, 0, 'c' },
25943fb4f7cdSSrinivas Pandruvada 		{ "debug", no_argument, 0, 'd' },
25953fb4f7cdSSrinivas Pandruvada 		{ "format", required_argument, 0, 'f' },
25963fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
25973fb4f7cdSSrinivas Pandruvada 		{ "info", no_argument, 0, 'i' },
25983fb4f7cdSSrinivas Pandruvada 		{ "out", required_argument, 0, 'o' },
25993fb4f7cdSSrinivas Pandruvada 		{ "version", no_argument, 0, 'v' },
26003fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
26013fb4f7cdSSrinivas Pandruvada 	};
26023fb4f7cdSSrinivas Pandruvada 
2603f362cdccSSrinivas Pandruvada 	if (geteuid() != 0) {
2604f362cdccSSrinivas Pandruvada 		fprintf(stderr, "Must run as root\n");
2605f362cdccSSrinivas Pandruvada 		exit(0);
2606f362cdccSSrinivas Pandruvada 	}
2607f362cdccSSrinivas Pandruvada 
2608f362cdccSSrinivas Pandruvada 	ret = update_cpu_model();
2609f362cdccSSrinivas Pandruvada 	if (ret)
2610f362cdccSSrinivas Pandruvada 		err(-1, "Invalid CPU model (%d)\n", cpu_model);
2611f362cdccSSrinivas Pandruvada 	printf("Intel(R) Speed Select Technology\n");
2612f362cdccSSrinivas Pandruvada 	printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2613f362cdccSSrinivas Pandruvada 
2614f362cdccSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2615f362cdccSSrinivas Pandruvada 		fp = fopen(pathname, "rb");
2616f362cdccSSrinivas Pandruvada 		if (!fp) {
2617f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
2618f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
2619f362cdccSSrinivas Pandruvada 			fprintf(stderr, "If the config is included then this is not a supported platform.\n");
2620f362cdccSSrinivas Pandruvada 			exit(0);
2621f362cdccSSrinivas Pandruvada 		}
2622f362cdccSSrinivas Pandruvada 		fclose(fp);
2623f362cdccSSrinivas Pandruvada 	}
2624f362cdccSSrinivas Pandruvada 
26253fb4f7cdSSrinivas Pandruvada 	progname = argv[0];
26263fb4f7cdSSrinivas Pandruvada 	while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v", long_options,
26273fb4f7cdSSrinivas Pandruvada 				       &option_index)) != -1) {
26283fb4f7cdSSrinivas Pandruvada 		switch (opt) {
26293fb4f7cdSSrinivas Pandruvada 		case 'c':
26303fb4f7cdSSrinivas Pandruvada 			parse_cpu_command(optarg);
26313fb4f7cdSSrinivas Pandruvada 			break;
26323fb4f7cdSSrinivas Pandruvada 		case 'd':
26333fb4f7cdSSrinivas Pandruvada 			debug_flag = 1;
26343fb4f7cdSSrinivas Pandruvada 			printf("Debug Mode ON\n");
26353fb4f7cdSSrinivas Pandruvada 			break;
26363fb4f7cdSSrinivas Pandruvada 		case 'f':
26373fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "json", 4))
26383fb4f7cdSSrinivas Pandruvada 				out_format_json = 1;
26393fb4f7cdSSrinivas Pandruvada 			break;
26403fb4f7cdSSrinivas Pandruvada 		case 'h':
26413fb4f7cdSSrinivas Pandruvada 			usage();
26423fb4f7cdSSrinivas Pandruvada 			break;
26433fb4f7cdSSrinivas Pandruvada 		case 'i':
26443fb4f7cdSSrinivas Pandruvada 			isst_print_platform_information();
26453fb4f7cdSSrinivas Pandruvada 			break;
26463fb4f7cdSSrinivas Pandruvada 		case 'o':
26473fb4f7cdSSrinivas Pandruvada 			if (outf)
26483fb4f7cdSSrinivas Pandruvada 				fclose(outf);
26493fb4f7cdSSrinivas Pandruvada 			outf = fopen_or_exit(optarg, "w");
26503fb4f7cdSSrinivas Pandruvada 			break;
26513fb4f7cdSSrinivas Pandruvada 		case 'v':
26523fb4f7cdSSrinivas Pandruvada 			print_version();
26533fb4f7cdSSrinivas Pandruvada 			break;
26543fb4f7cdSSrinivas Pandruvada 		default:
26553fb4f7cdSSrinivas Pandruvada 			usage();
26563fb4f7cdSSrinivas Pandruvada 		}
26573fb4f7cdSSrinivas Pandruvada 	}
26583fb4f7cdSSrinivas Pandruvada 
26593fb4f7cdSSrinivas Pandruvada 	if (optind > (argc - 2)) {
2660addd116dSSrinivas Pandruvada 		usage();
26613fb4f7cdSSrinivas Pandruvada 		exit(0);
26623fb4f7cdSSrinivas Pandruvada 	}
26633fb4f7cdSSrinivas Pandruvada 	set_max_cpu_num();
2664fb186158SSrinivas Pandruvada 	store_cpu_topology();
26653fb4f7cdSSrinivas Pandruvada 	set_cpu_present_cpu_mask();
26663fb4f7cdSSrinivas Pandruvada 	set_cpu_target_cpu_mask();
2667c829f0efSPrarit Bhargava 
2668c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
26693bc3d30cSPrarit Bhargava 		ret = isst_fill_platform_info();
26703bc3d30cSPrarit Bhargava 		if (ret)
26713bc3d30cSPrarit Bhargava 			goto out;
2672210369dcSPrarit Bhargava 		process_command(argc, argv, isst_help_cmds, isst_cmds);
2673c829f0efSPrarit Bhargava 	} else {
2674c829f0efSPrarit Bhargava 		process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
2675c829f0efSPrarit Bhargava 	}
26763bc3d30cSPrarit Bhargava out:
26773bc3d30cSPrarit Bhargava 	free_cpu_set(present_cpumask);
26783bc3d30cSPrarit Bhargava 	free_cpu_set(target_cpumask);
26793fb4f7cdSSrinivas Pandruvada }
26803fb4f7cdSSrinivas Pandruvada 
26813fb4f7cdSSrinivas Pandruvada int main(int argc, char **argv)
26823fb4f7cdSSrinivas Pandruvada {
26833fb4f7cdSSrinivas Pandruvada 	outf = stderr;
26843fb4f7cdSSrinivas Pandruvada 	cmdline(argc, argv);
26853fb4f7cdSSrinivas Pandruvada 	return 0;
26863fb4f7cdSSrinivas Pandruvada }
2687