xref: /linux/tools/power/x86/intel-speed-select/isst-config.c (revision a9fd6ae739ef9624f9017df08edf0970c7d170e2)
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 
981c1d935cSPrarit Bhargava static int update_cpu_model(void)
993fb4f7cdSSrinivas Pandruvada {
1003fb4f7cdSSrinivas Pandruvada 	unsigned int ebx, ecx, edx;
1013fb4f7cdSSrinivas Pandruvada 	unsigned int fms, family;
1023fb4f7cdSSrinivas Pandruvada 
1033fb4f7cdSSrinivas Pandruvada 	__cpuid(1, fms, ebx, ecx, edx);
1043fb4f7cdSSrinivas Pandruvada 	family = (fms >> 8) & 0xf;
1053fb4f7cdSSrinivas Pandruvada 	cpu_model = (fms >> 4) & 0xf;
1063fb4f7cdSSrinivas Pandruvada 	if (family == 6 || family == 0xf)
1073fb4f7cdSSrinivas Pandruvada 		cpu_model += ((fms >> 16) & 0xf) << 4;
1081c1d935cSPrarit Bhargava 
1091c1d935cSPrarit Bhargava 	cpu_stepping = fms & 0xf;
1101c1d935cSPrarit Bhargava 	/* only three CascadeLake-N models are supported */
1111c1d935cSPrarit Bhargava 	if (is_clx_n_platform()) {
1121c1d935cSPrarit Bhargava 		FILE *fp;
1131c1d935cSPrarit Bhargava 		size_t n = 0;
1141c1d935cSPrarit Bhargava 		char *line = NULL;
1151c1d935cSPrarit Bhargava 		int ret = 1;
1161c1d935cSPrarit Bhargava 
1171c1d935cSPrarit Bhargava 		fp = fopen("/proc/cpuinfo", "r");
1181c1d935cSPrarit Bhargava 		if (!fp)
1191c1d935cSPrarit Bhargava 			err(-1, "cannot open /proc/cpuinfo\n");
1201c1d935cSPrarit Bhargava 
1211c1d935cSPrarit Bhargava 		while (getline(&line, &n, fp) > 0) {
1221c1d935cSPrarit Bhargava 			if (strstr(line, "model name")) {
1231c1d935cSPrarit Bhargava 				if (strstr(line, "6252N") ||
1241c1d935cSPrarit Bhargava 				    strstr(line, "6230N") ||
1251c1d935cSPrarit Bhargava 				    strstr(line, "5218N"))
1261c1d935cSPrarit Bhargava 					ret = 0;
1271c1d935cSPrarit Bhargava 				break;
1281c1d935cSPrarit Bhargava 			}
1291c1d935cSPrarit Bhargava 		}
1301c1d935cSPrarit Bhargava 		free(line);
1311c1d935cSPrarit Bhargava 		fclose(fp);
1321c1d935cSPrarit Bhargava 		return ret;
1331c1d935cSPrarit Bhargava 	}
1341c1d935cSPrarit Bhargava 	return 0;
1353fb4f7cdSSrinivas Pandruvada }
1363fb4f7cdSSrinivas Pandruvada 
1373fb4f7cdSSrinivas Pandruvada /* Open a file, and exit on failure */
1383fb4f7cdSSrinivas Pandruvada static FILE *fopen_or_exit(const char *path, const char *mode)
1393fb4f7cdSSrinivas Pandruvada {
1403fb4f7cdSSrinivas Pandruvada 	FILE *filep = fopen(path, mode);
1413fb4f7cdSSrinivas Pandruvada 
1423fb4f7cdSSrinivas Pandruvada 	if (!filep)
1433fb4f7cdSSrinivas Pandruvada 		err(1, "%s: open failed", path);
1443fb4f7cdSSrinivas Pandruvada 
1453fb4f7cdSSrinivas Pandruvada 	return filep;
1463fb4f7cdSSrinivas Pandruvada }
1473fb4f7cdSSrinivas Pandruvada 
1483fb4f7cdSSrinivas Pandruvada /* Parse a file containing a single int */
1493fb4f7cdSSrinivas Pandruvada static int parse_int_file(int fatal, const char *fmt, ...)
1503fb4f7cdSSrinivas Pandruvada {
1513fb4f7cdSSrinivas Pandruvada 	va_list args;
1523fb4f7cdSSrinivas Pandruvada 	char path[PATH_MAX];
1533fb4f7cdSSrinivas Pandruvada 	FILE *filep;
1543fb4f7cdSSrinivas Pandruvada 	int value;
1553fb4f7cdSSrinivas Pandruvada 
1563fb4f7cdSSrinivas Pandruvada 	va_start(args, fmt);
1573fb4f7cdSSrinivas Pandruvada 	vsnprintf(path, sizeof(path), fmt, args);
1583fb4f7cdSSrinivas Pandruvada 	va_end(args);
1593fb4f7cdSSrinivas Pandruvada 	if (fatal) {
1603fb4f7cdSSrinivas Pandruvada 		filep = fopen_or_exit(path, "r");
1613fb4f7cdSSrinivas Pandruvada 	} else {
1623fb4f7cdSSrinivas Pandruvada 		filep = fopen(path, "r");
1633fb4f7cdSSrinivas Pandruvada 		if (!filep)
1643fb4f7cdSSrinivas Pandruvada 			return -1;
1653fb4f7cdSSrinivas Pandruvada 	}
1663fb4f7cdSSrinivas Pandruvada 	if (fscanf(filep, "%d", &value) != 1)
1673fb4f7cdSSrinivas Pandruvada 		err(1, "%s: failed to parse number from file", path);
1683fb4f7cdSSrinivas Pandruvada 	fclose(filep);
1693fb4f7cdSSrinivas Pandruvada 
1703fb4f7cdSSrinivas Pandruvada 	return value;
1713fb4f7cdSSrinivas Pandruvada }
1723fb4f7cdSSrinivas Pandruvada 
1733fb4f7cdSSrinivas Pandruvada int cpufreq_sysfs_present(void)
1743fb4f7cdSSrinivas Pandruvada {
1753fb4f7cdSSrinivas Pandruvada 	DIR *dir;
1763fb4f7cdSSrinivas Pandruvada 
1773fb4f7cdSSrinivas Pandruvada 	dir = opendir("/sys/devices/system/cpu/cpu0/cpufreq");
1783fb4f7cdSSrinivas Pandruvada 	if (dir) {
1793fb4f7cdSSrinivas Pandruvada 		closedir(dir);
1803fb4f7cdSSrinivas Pandruvada 		return 1;
1813fb4f7cdSSrinivas Pandruvada 	}
1823fb4f7cdSSrinivas Pandruvada 
1833fb4f7cdSSrinivas Pandruvada 	return 0;
1843fb4f7cdSSrinivas Pandruvada }
1853fb4f7cdSSrinivas Pandruvada 
1863fb4f7cdSSrinivas Pandruvada int out_format_is_json(void)
1873fb4f7cdSSrinivas Pandruvada {
1883fb4f7cdSSrinivas Pandruvada 	return out_format_json;
1893fb4f7cdSSrinivas Pandruvada }
1903fb4f7cdSSrinivas Pandruvada 
191fb186158SSrinivas Pandruvada static int get_stored_topology_info(int cpu, int *core_id, int *pkg_id, int *die_id)
192fb186158SSrinivas Pandruvada {
193fb186158SSrinivas Pandruvada 	const char *pathname = "/tmp/isst_cpu_topology.dat";
194fb186158SSrinivas Pandruvada 	struct cpu_topology cpu_top;
195fb186158SSrinivas Pandruvada 	FILE *fp;
196fb186158SSrinivas Pandruvada 	int ret;
197fb186158SSrinivas Pandruvada 
198fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "rb");
199fb186158SSrinivas Pandruvada 	if (!fp)
200fb186158SSrinivas Pandruvada 		return -1;
201fb186158SSrinivas Pandruvada 
202fb186158SSrinivas Pandruvada 	ret = fseek(fp, cpu * sizeof(cpu_top), SEEK_SET);
203fb186158SSrinivas Pandruvada 	if (ret)
204fb186158SSrinivas Pandruvada 		goto err_ret;
205fb186158SSrinivas Pandruvada 
206fb186158SSrinivas Pandruvada 	ret = fread(&cpu_top, sizeof(cpu_top), 1, fp);
207fb186158SSrinivas Pandruvada 	if (ret != 1) {
208fb186158SSrinivas Pandruvada 		ret = -1;
209fb186158SSrinivas Pandruvada 		goto err_ret;
210fb186158SSrinivas Pandruvada 	}
211fb186158SSrinivas Pandruvada 
212fb186158SSrinivas Pandruvada 	*pkg_id = cpu_top.pkg_id;
213fb186158SSrinivas Pandruvada 	*core_id = cpu_top.core_id;
214fb186158SSrinivas Pandruvada 	*die_id = cpu_top.die_id;
215fb186158SSrinivas Pandruvada 	ret = 0;
216fb186158SSrinivas Pandruvada 
217fb186158SSrinivas Pandruvada err_ret:
218fb186158SSrinivas Pandruvada 	fclose(fp);
219fb186158SSrinivas Pandruvada 
220fb186158SSrinivas Pandruvada 	return ret;
221fb186158SSrinivas Pandruvada }
222fb186158SSrinivas Pandruvada 
223fb186158SSrinivas Pandruvada static void store_cpu_topology(void)
224fb186158SSrinivas Pandruvada {
225fb186158SSrinivas Pandruvada 	const char *pathname = "/tmp/isst_cpu_topology.dat";
226fb186158SSrinivas Pandruvada 	FILE *fp;
227fb186158SSrinivas Pandruvada 	int i;
228fb186158SSrinivas Pandruvada 
229fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "rb");
230fb186158SSrinivas Pandruvada 	if (fp) {
231fb186158SSrinivas Pandruvada 		/* Mapping already exists */
232fb186158SSrinivas Pandruvada 		fclose(fp);
233fb186158SSrinivas Pandruvada 		return;
234fb186158SSrinivas Pandruvada 	}
235fb186158SSrinivas Pandruvada 
236fb186158SSrinivas Pandruvada 	fp = fopen(pathname, "wb");
237fb186158SSrinivas Pandruvada 	if (!fp) {
238fb186158SSrinivas Pandruvada 		fprintf(stderr, "Can't create file:%s\n", pathname);
239fb186158SSrinivas Pandruvada 		return;
240fb186158SSrinivas Pandruvada 	}
241fb186158SSrinivas Pandruvada 
242fb186158SSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
243fb186158SSrinivas Pandruvada 		struct cpu_topology cpu_top;
244fb186158SSrinivas Pandruvada 
245fb186158SSrinivas Pandruvada 		cpu_top.core_id = parse_int_file(0,
246fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/core_id", i);
247fb186158SSrinivas Pandruvada 		if (cpu_top.core_id < 0)
248fb186158SSrinivas Pandruvada 			cpu_top.core_id = -1;
249fb186158SSrinivas Pandruvada 
250fb186158SSrinivas Pandruvada 		cpu_top.pkg_id = parse_int_file(0,
251fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
252fb186158SSrinivas Pandruvada 		if (cpu_top.pkg_id < 0)
253fb186158SSrinivas Pandruvada 			cpu_top.pkg_id = -1;
254fb186158SSrinivas Pandruvada 
255fb186158SSrinivas Pandruvada 		cpu_top.die_id = parse_int_file(0,
256fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/die_id", i);
257fb186158SSrinivas Pandruvada 		if (cpu_top.die_id < 0)
258fb186158SSrinivas Pandruvada 			cpu_top.die_id = -1;
259fb186158SSrinivas Pandruvada 
260fb186158SSrinivas Pandruvada 		cpu_top.cpu = i;
261fb186158SSrinivas Pandruvada 
262fb186158SSrinivas Pandruvada 		if (fwrite(&cpu_top, sizeof(cpu_top), 1, fp) != 1) {
263fb186158SSrinivas Pandruvada 			fprintf(stderr, "Can't write to:%s\n", pathname);
264fb186158SSrinivas Pandruvada 			break;
265fb186158SSrinivas Pandruvada 		}
266fb186158SSrinivas Pandruvada 	}
267fb186158SSrinivas Pandruvada 
268fb186158SSrinivas Pandruvada 	fclose(fp);
269fb186158SSrinivas Pandruvada }
270fb186158SSrinivas Pandruvada 
2713fb4f7cdSSrinivas Pandruvada int get_physical_package_id(int cpu)
2723fb4f7cdSSrinivas Pandruvada {
273fb186158SSrinivas Pandruvada 	int ret;
274fb186158SSrinivas Pandruvada 
275fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
276fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id",
2773fb4f7cdSSrinivas Pandruvada 			cpu);
278fb186158SSrinivas Pandruvada 	if (ret < 0) {
279fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
280fb186158SSrinivas Pandruvada 
281fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
282fb186158SSrinivas Pandruvada 		if (!ret)
283fb186158SSrinivas Pandruvada 			return pkg_id;
284fb186158SSrinivas Pandruvada 	}
285fb186158SSrinivas Pandruvada 
286fb186158SSrinivas Pandruvada 	return ret;
2873fb4f7cdSSrinivas Pandruvada }
2883fb4f7cdSSrinivas Pandruvada 
2893fb4f7cdSSrinivas Pandruvada int get_physical_core_id(int cpu)
2903fb4f7cdSSrinivas Pandruvada {
291fb186158SSrinivas Pandruvada 	int ret;
292fb186158SSrinivas Pandruvada 
293fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
294fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/core_id",
295fb186158SSrinivas Pandruvada 			cpu);
296fb186158SSrinivas Pandruvada 	if (ret < 0) {
297fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
298fb186158SSrinivas Pandruvada 
299fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
300fb186158SSrinivas Pandruvada 		if (!ret)
301fb186158SSrinivas Pandruvada 			return core_id;
302fb186158SSrinivas Pandruvada 	}
303fb186158SSrinivas Pandruvada 
304fb186158SSrinivas Pandruvada 	return ret;
3053fb4f7cdSSrinivas Pandruvada }
3063fb4f7cdSSrinivas Pandruvada 
3073fb4f7cdSSrinivas Pandruvada int get_physical_die_id(int cpu)
3083fb4f7cdSSrinivas Pandruvada {
3093fb4f7cdSSrinivas Pandruvada 	int ret;
3103fb4f7cdSSrinivas Pandruvada 
311fb186158SSrinivas Pandruvada 	ret = parse_int_file(0,
312fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/die_id",
3133fb4f7cdSSrinivas Pandruvada 			cpu);
314fb186158SSrinivas Pandruvada 	if (ret < 0) {
315fb186158SSrinivas Pandruvada 		int core_id, pkg_id, die_id;
316fb186158SSrinivas Pandruvada 
317fb186158SSrinivas Pandruvada 		ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
318fb186158SSrinivas Pandruvada 		if (!ret)
319fb186158SSrinivas Pandruvada 			return die_id;
320fb186158SSrinivas Pandruvada 	}
321fb186158SSrinivas Pandruvada 
3223fb4f7cdSSrinivas Pandruvada 	if (ret < 0)
3233fb4f7cdSSrinivas Pandruvada 		ret = 0;
3243fb4f7cdSSrinivas Pandruvada 
3253fb4f7cdSSrinivas Pandruvada 	return ret;
3263fb4f7cdSSrinivas Pandruvada }
3273fb4f7cdSSrinivas Pandruvada 
3287af5a95bSSrinivas Pandruvada int get_cpufreq_base_freq(int cpu)
3297af5a95bSSrinivas Pandruvada {
3307af5a95bSSrinivas Pandruvada 	return parse_int_file(0, "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency", cpu);
3317af5a95bSSrinivas Pandruvada }
3327af5a95bSSrinivas Pandruvada 
3333fb4f7cdSSrinivas Pandruvada int get_topo_max_cpus(void)
3343fb4f7cdSSrinivas Pandruvada {
3353fb4f7cdSSrinivas Pandruvada 	return topo_max_cpus;
3363fb4f7cdSSrinivas Pandruvada }
3373fb4f7cdSSrinivas Pandruvada 
3383c64c81aSSrinivas Pandruvada static void set_cpu_online_offline(int cpu, int state)
3393c64c81aSSrinivas Pandruvada {
3403c64c81aSSrinivas Pandruvada 	char buffer[128];
341abd120e3SSrinivas Pandruvada 	int fd, ret;
3423c64c81aSSrinivas Pandruvada 
3433c64c81aSSrinivas Pandruvada 	snprintf(buffer, sizeof(buffer),
3443c64c81aSSrinivas Pandruvada 		 "/sys/devices/system/cpu/cpu%d/online", cpu);
3453c64c81aSSrinivas Pandruvada 
3463c64c81aSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
34769669198SSrinivas Pandruvada 	if (fd < 0) {
34869669198SSrinivas Pandruvada 		if (!cpu && state) {
34969669198SSrinivas Pandruvada 			fprintf(stderr, "This system is not configured for CPU 0 online/offline\n");
35069669198SSrinivas Pandruvada 			fprintf(stderr, "Ignoring online request for CPU 0 as this is already online\n");
35169669198SSrinivas Pandruvada 			return;
35269669198SSrinivas Pandruvada 		}
3533c64c81aSSrinivas Pandruvada 		err(-1, "%s open failed", buffer);
35469669198SSrinivas Pandruvada 	}
3553c64c81aSSrinivas Pandruvada 
3563c64c81aSSrinivas Pandruvada 	if (state)
357abd120e3SSrinivas Pandruvada 		ret = write(fd, "1\n", 2);
3583c64c81aSSrinivas Pandruvada 	else
359abd120e3SSrinivas Pandruvada 		ret = write(fd, "0\n", 2);
360abd120e3SSrinivas Pandruvada 
361abd120e3SSrinivas Pandruvada 	if (ret == -1)
362abd120e3SSrinivas Pandruvada 		perror("Online/Offline: Operation failed\n");
3633c64c81aSSrinivas Pandruvada 
3643c64c81aSSrinivas Pandruvada 	close(fd);
3653c64c81aSSrinivas Pandruvada }
3663c64c81aSSrinivas Pandruvada 
3673fb4f7cdSSrinivas Pandruvada #define MAX_PACKAGE_COUNT 8
3683fb4f7cdSSrinivas Pandruvada #define MAX_DIE_PER_PACKAGE 2
3693fb4f7cdSSrinivas Pandruvada static void for_each_online_package_in_set(void (*callback)(int, void *, void *,
3703fb4f7cdSSrinivas Pandruvada 							    void *, void *),
3713fb4f7cdSSrinivas Pandruvada 					   void *arg1, void *arg2, void *arg3,
3723fb4f7cdSSrinivas Pandruvada 					   void *arg4)
3733fb4f7cdSSrinivas Pandruvada {
3743fb4f7cdSSrinivas Pandruvada 	int max_packages[MAX_PACKAGE_COUNT * MAX_PACKAGE_COUNT];
3753fb4f7cdSSrinivas Pandruvada 	int pkg_index = 0, i;
3763fb4f7cdSSrinivas Pandruvada 
3773fb4f7cdSSrinivas Pandruvada 	memset(max_packages, 0xff, sizeof(max_packages));
3783fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
3793fb4f7cdSSrinivas Pandruvada 		int j, online, pkg_id, die_id = 0, skip = 0;
3803fb4f7cdSSrinivas Pandruvada 
3813fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
3823fb4f7cdSSrinivas Pandruvada 			continue;
3833fb4f7cdSSrinivas Pandruvada 		if (i)
3843fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
3853fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
3863fb4f7cdSSrinivas Pandruvada 		else
3873fb4f7cdSSrinivas Pandruvada 			online =
3883fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
3893fb4f7cdSSrinivas Pandruvada 
3903fb4f7cdSSrinivas Pandruvada 		die_id = get_physical_die_id(i);
3913fb4f7cdSSrinivas Pandruvada 		if (die_id < 0)
3923fb4f7cdSSrinivas Pandruvada 			die_id = 0;
393fb186158SSrinivas Pandruvada 
394fb186158SSrinivas Pandruvada 		pkg_id = parse_int_file(0,
395fb186158SSrinivas Pandruvada 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
396fb186158SSrinivas Pandruvada 		if (pkg_id < 0)
397f0e0b4d1SSrinivas Pandruvada 			continue;
398fb186158SSrinivas Pandruvada 
3993fb4f7cdSSrinivas Pandruvada 		/* Create an unique id for package, die combination to store */
4003fb4f7cdSSrinivas Pandruvada 		pkg_id = (MAX_PACKAGE_COUNT * pkg_id + die_id);
4013fb4f7cdSSrinivas Pandruvada 
4023fb4f7cdSSrinivas Pandruvada 		for (j = 0; j < pkg_index; ++j) {
4033fb4f7cdSSrinivas Pandruvada 			if (max_packages[j] == pkg_id) {
4043fb4f7cdSSrinivas Pandruvada 				skip = 1;
4053fb4f7cdSSrinivas Pandruvada 				break;
4063fb4f7cdSSrinivas Pandruvada 			}
4073fb4f7cdSSrinivas Pandruvada 		}
4083fb4f7cdSSrinivas Pandruvada 
4093fb4f7cdSSrinivas Pandruvada 		if (!skip && online && callback) {
4103fb4f7cdSSrinivas Pandruvada 			callback(i, arg1, arg2, arg3, arg4);
4113fb4f7cdSSrinivas Pandruvada 			max_packages[pkg_index++] = pkg_id;
4123fb4f7cdSSrinivas Pandruvada 		}
4133fb4f7cdSSrinivas Pandruvada 	}
4143fb4f7cdSSrinivas Pandruvada }
4153fb4f7cdSSrinivas Pandruvada 
4163fb4f7cdSSrinivas Pandruvada static void for_each_online_target_cpu_in_set(
4173fb4f7cdSSrinivas Pandruvada 	void (*callback)(int, void *, void *, void *, void *), void *arg1,
4183fb4f7cdSSrinivas Pandruvada 	void *arg2, void *arg3, void *arg4)
4193fb4f7cdSSrinivas Pandruvada {
4203fb4f7cdSSrinivas Pandruvada 	int i;
4213fb4f7cdSSrinivas Pandruvada 
4223fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
4233fb4f7cdSSrinivas Pandruvada 		int online;
4243fb4f7cdSSrinivas Pandruvada 
4253fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
4263fb4f7cdSSrinivas Pandruvada 			continue;
4273fb4f7cdSSrinivas Pandruvada 		if (i)
4283fb4f7cdSSrinivas Pandruvada 			online = parse_int_file(
4293fb4f7cdSSrinivas Pandruvada 				1, "/sys/devices/system/cpu/cpu%d/online", i);
4303fb4f7cdSSrinivas Pandruvada 		else
4313fb4f7cdSSrinivas Pandruvada 			online =
4323fb4f7cdSSrinivas Pandruvada 				1; /* online entry for CPU 0 needs some special configs */
4333fb4f7cdSSrinivas Pandruvada 
4343fb4f7cdSSrinivas Pandruvada 		if (online && callback)
4353fb4f7cdSSrinivas Pandruvada 			callback(i, arg1, arg2, arg3, arg4);
4363fb4f7cdSSrinivas Pandruvada 	}
4373fb4f7cdSSrinivas Pandruvada }
4383fb4f7cdSSrinivas Pandruvada 
4393fb4f7cdSSrinivas Pandruvada #define BITMASK_SIZE 32
4403fb4f7cdSSrinivas Pandruvada static void set_max_cpu_num(void)
4413fb4f7cdSSrinivas Pandruvada {
4423fb4f7cdSSrinivas Pandruvada 	FILE *filep;
4433fb4f7cdSSrinivas Pandruvada 	unsigned long dummy;
444864dc09eSSrinivas Pandruvada 	int i;
4453fb4f7cdSSrinivas Pandruvada 
4463fb4f7cdSSrinivas Pandruvada 	topo_max_cpus = 0;
447864dc09eSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
448864dc09eSSrinivas Pandruvada 		char path[256];
449864dc09eSSrinivas Pandruvada 
450864dc09eSSrinivas Pandruvada 		snprintf(path, sizeof(path),
451864dc09eSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
452864dc09eSSrinivas Pandruvada 		filep = fopen(path, "r");
453864dc09eSSrinivas Pandruvada 		if (filep)
454864dc09eSSrinivas Pandruvada 			break;
455864dc09eSSrinivas Pandruvada 	}
456864dc09eSSrinivas Pandruvada 
457864dc09eSSrinivas Pandruvada 	if (!filep) {
458864dc09eSSrinivas Pandruvada 		fprintf(stderr, "Can't get max cpu number\n");
459864dc09eSSrinivas Pandruvada 		exit(0);
460864dc09eSSrinivas Pandruvada 	}
461864dc09eSSrinivas Pandruvada 
4623fb4f7cdSSrinivas Pandruvada 	while (fscanf(filep, "%lx,", &dummy) == 1)
4633fb4f7cdSSrinivas Pandruvada 		topo_max_cpus += BITMASK_SIZE;
4643fb4f7cdSSrinivas Pandruvada 	fclose(filep);
4653fb4f7cdSSrinivas Pandruvada 
4663fb4f7cdSSrinivas Pandruvada 	debug_printf("max cpus %d\n", topo_max_cpus);
4673fb4f7cdSSrinivas Pandruvada }
4683fb4f7cdSSrinivas Pandruvada 
4693fb4f7cdSSrinivas Pandruvada size_t alloc_cpu_set(cpu_set_t **cpu_set)
4703fb4f7cdSSrinivas Pandruvada {
4713fb4f7cdSSrinivas Pandruvada 	cpu_set_t *_cpu_set;
4723fb4f7cdSSrinivas Pandruvada 	size_t size;
4733fb4f7cdSSrinivas Pandruvada 
4743fb4f7cdSSrinivas Pandruvada 	_cpu_set = CPU_ALLOC((topo_max_cpus + 1));
4753fb4f7cdSSrinivas Pandruvada 	if (_cpu_set == NULL)
4763fb4f7cdSSrinivas Pandruvada 		err(3, "CPU_ALLOC");
4773fb4f7cdSSrinivas Pandruvada 	size = CPU_ALLOC_SIZE((topo_max_cpus + 1));
4783fb4f7cdSSrinivas Pandruvada 	CPU_ZERO_S(size, _cpu_set);
4793fb4f7cdSSrinivas Pandruvada 
4803fb4f7cdSSrinivas Pandruvada 	*cpu_set = _cpu_set;
4813fb4f7cdSSrinivas Pandruvada 	return size;
4823fb4f7cdSSrinivas Pandruvada }
4833fb4f7cdSSrinivas Pandruvada 
4843fb4f7cdSSrinivas Pandruvada void free_cpu_set(cpu_set_t *cpu_set)
4853fb4f7cdSSrinivas Pandruvada {
4863fb4f7cdSSrinivas Pandruvada 	CPU_FREE(cpu_set);
4873fb4f7cdSSrinivas Pandruvada }
4883fb4f7cdSSrinivas Pandruvada 
4893fb4f7cdSSrinivas Pandruvada static int cpu_cnt[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
490de7f9d3dSSrinivas Pandruvada static long long core_mask[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE];
4913fb4f7cdSSrinivas Pandruvada static void set_cpu_present_cpu_mask(void)
4923fb4f7cdSSrinivas Pandruvada {
4933fb4f7cdSSrinivas Pandruvada 	size_t size;
4943fb4f7cdSSrinivas Pandruvada 	DIR *dir;
4953fb4f7cdSSrinivas Pandruvada 	int i;
4963fb4f7cdSSrinivas Pandruvada 
4973fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&present_cpumask);
4983fb4f7cdSSrinivas Pandruvada 	present_cpumask_size = size;
4993fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
5003fb4f7cdSSrinivas Pandruvada 		char buffer[256];
5013fb4f7cdSSrinivas Pandruvada 
5023fb4f7cdSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
5033fb4f7cdSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d", i);
5043fb4f7cdSSrinivas Pandruvada 		dir = opendir(buffer);
5053fb4f7cdSSrinivas Pandruvada 		if (dir) {
5063fb4f7cdSSrinivas Pandruvada 			int pkg_id, die_id;
5073fb4f7cdSSrinivas Pandruvada 
5083fb4f7cdSSrinivas Pandruvada 			CPU_SET_S(i, size, present_cpumask);
5093fb4f7cdSSrinivas Pandruvada 			die_id = get_physical_die_id(i);
5103fb4f7cdSSrinivas Pandruvada 			if (die_id < 0)
5113fb4f7cdSSrinivas Pandruvada 				die_id = 0;
5123fb4f7cdSSrinivas Pandruvada 
5133fb4f7cdSSrinivas Pandruvada 			pkg_id = get_physical_package_id(i);
514f0e0b4d1SSrinivas Pandruvada 			if (pkg_id < 0) {
515f0e0b4d1SSrinivas Pandruvada 				fprintf(stderr, "Failed to get package id, CPU %d may be offline\n", i);
516f0e0b4d1SSrinivas Pandruvada 				continue;
517f0e0b4d1SSrinivas Pandruvada 			}
5183fb4f7cdSSrinivas Pandruvada 			if (pkg_id < MAX_PACKAGE_COUNT &&
519de7f9d3dSSrinivas Pandruvada 			    die_id < MAX_DIE_PER_PACKAGE) {
520de7f9d3dSSrinivas Pandruvada 				int core_id = get_physical_core_id(i);
521de7f9d3dSSrinivas Pandruvada 
5223fb4f7cdSSrinivas Pandruvada 				cpu_cnt[pkg_id][die_id]++;
523de7f9d3dSSrinivas Pandruvada 				core_mask[pkg_id][die_id] |= (1ULL << core_id);
524de7f9d3dSSrinivas Pandruvada 			}
5253fb4f7cdSSrinivas Pandruvada 		}
5263fb4f7cdSSrinivas Pandruvada 		closedir(dir);
5273fb4f7cdSSrinivas Pandruvada 	}
5283fb4f7cdSSrinivas Pandruvada }
5293fb4f7cdSSrinivas Pandruvada 
530de7f9d3dSSrinivas Pandruvada int get_core_count(int pkg_id, int die_id)
531de7f9d3dSSrinivas Pandruvada {
532de7f9d3dSSrinivas Pandruvada 	int cnt = 0;
533de7f9d3dSSrinivas Pandruvada 
534de7f9d3dSSrinivas Pandruvada 	if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE) {
535de7f9d3dSSrinivas Pandruvada 		int i;
536de7f9d3dSSrinivas Pandruvada 
537de7f9d3dSSrinivas Pandruvada 		for (i = 0; i < sizeof(long long) * 8; ++i) {
538de7f9d3dSSrinivas Pandruvada 			if (core_mask[pkg_id][die_id] & (1ULL << i))
539de7f9d3dSSrinivas Pandruvada 				cnt++;
540de7f9d3dSSrinivas Pandruvada 		}
541de7f9d3dSSrinivas Pandruvada 	}
542de7f9d3dSSrinivas Pandruvada 
543de7f9d3dSSrinivas Pandruvada 	return cnt;
544de7f9d3dSSrinivas Pandruvada }
545de7f9d3dSSrinivas Pandruvada 
5463fb4f7cdSSrinivas Pandruvada int get_cpu_count(int pkg_id, int die_id)
5473fb4f7cdSSrinivas Pandruvada {
5483fb4f7cdSSrinivas Pandruvada 	if (pkg_id < MAX_PACKAGE_COUNT && die_id < MAX_DIE_PER_PACKAGE)
5493ec2aef1SPrarit Bhargava 		return cpu_cnt[pkg_id][die_id];
5503fb4f7cdSSrinivas Pandruvada 
5513fb4f7cdSSrinivas Pandruvada 	return 0;
5523fb4f7cdSSrinivas Pandruvada }
5533fb4f7cdSSrinivas Pandruvada 
5543fb4f7cdSSrinivas Pandruvada static void set_cpu_target_cpu_mask(void)
5553fb4f7cdSSrinivas Pandruvada {
5563fb4f7cdSSrinivas Pandruvada 	size_t size;
5573fb4f7cdSSrinivas Pandruvada 	int i;
5583fb4f7cdSSrinivas Pandruvada 
5593fb4f7cdSSrinivas Pandruvada 	size = alloc_cpu_set(&target_cpumask);
5603fb4f7cdSSrinivas Pandruvada 	target_cpumask_size = size;
5613fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < max_target_cpus; ++i) {
5623fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(target_cpus[i], present_cpumask_size,
5633fb4f7cdSSrinivas Pandruvada 				 present_cpumask))
5643fb4f7cdSSrinivas Pandruvada 			continue;
5653fb4f7cdSSrinivas Pandruvada 
5663fb4f7cdSSrinivas Pandruvada 		CPU_SET_S(target_cpus[i], size, target_cpumask);
5673fb4f7cdSSrinivas Pandruvada 	}
5683fb4f7cdSSrinivas Pandruvada }
5693fb4f7cdSSrinivas Pandruvada 
5703fb4f7cdSSrinivas Pandruvada static void create_cpu_map(void)
5713fb4f7cdSSrinivas Pandruvada {
5723fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
5733fb4f7cdSSrinivas Pandruvada 	int i, fd = 0;
5743fb4f7cdSSrinivas Pandruvada 	struct isst_if_cpu_maps map;
5753fb4f7cdSSrinivas Pandruvada 
5763fb4f7cdSSrinivas Pandruvada 	cpu_map = malloc(sizeof(*cpu_map) * topo_max_cpus);
5773fb4f7cdSSrinivas Pandruvada 	if (!cpu_map)
5783fb4f7cdSSrinivas Pandruvada 		err(3, "cpumap");
5793fb4f7cdSSrinivas Pandruvada 
5803fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
5813fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
5823fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
5833fb4f7cdSSrinivas Pandruvada 
5843fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
5853fb4f7cdSSrinivas Pandruvada 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
5863fb4f7cdSSrinivas Pandruvada 			continue;
5873fb4f7cdSSrinivas Pandruvada 
5883fb4f7cdSSrinivas Pandruvada 		map.cmd_count = 1;
5893fb4f7cdSSrinivas Pandruvada 		map.cpu_map[0].logical_cpu = i;
5903fb4f7cdSSrinivas Pandruvada 
5913fb4f7cdSSrinivas Pandruvada 		debug_printf(" map logical_cpu:%d\n",
5923fb4f7cdSSrinivas Pandruvada 			     map.cpu_map[0].logical_cpu);
5933fb4f7cdSSrinivas Pandruvada 		if (ioctl(fd, ISST_IF_GET_PHY_ID, &map) == -1) {
5943fb4f7cdSSrinivas Pandruvada 			perror("ISST_IF_GET_PHY_ID");
5953fb4f7cdSSrinivas Pandruvada 			fprintf(outf, "Error: map logical_cpu:%d\n",
5963fb4f7cdSSrinivas Pandruvada 				map.cpu_map[0].logical_cpu);
5973fb4f7cdSSrinivas Pandruvada 			continue;
5983fb4f7cdSSrinivas Pandruvada 		}
5993fb4f7cdSSrinivas Pandruvada 		cpu_map[i].core_id = get_physical_core_id(i);
6003fb4f7cdSSrinivas Pandruvada 		cpu_map[i].pkg_id = get_physical_package_id(i);
6013fb4f7cdSSrinivas Pandruvada 		cpu_map[i].die_id = get_physical_die_id(i);
6023fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu = map.cpu_map[0].physical_cpu;
6033fb4f7cdSSrinivas Pandruvada 		cpu_map[i].punit_cpu_core = (map.cpu_map[0].physical_cpu >>
6043fb4f7cdSSrinivas Pandruvada 					     1); // shift to get core id
6053fb4f7cdSSrinivas Pandruvada 
6063fb4f7cdSSrinivas Pandruvada 		debug_printf(
6073fb4f7cdSSrinivas Pandruvada 			"map logical_cpu:%d core: %d die:%d pkg:%d punit_cpu:%d punit_core:%d\n",
6083fb4f7cdSSrinivas Pandruvada 			i, cpu_map[i].core_id, cpu_map[i].die_id,
6093fb4f7cdSSrinivas Pandruvada 			cpu_map[i].pkg_id, cpu_map[i].punit_cpu,
6103fb4f7cdSSrinivas Pandruvada 			cpu_map[i].punit_cpu_core);
6113fb4f7cdSSrinivas Pandruvada 	}
6123fb4f7cdSSrinivas Pandruvada 
6133fb4f7cdSSrinivas Pandruvada 	if (fd)
6143fb4f7cdSSrinivas Pandruvada 		close(fd);
6153fb4f7cdSSrinivas Pandruvada }
6163fb4f7cdSSrinivas Pandruvada 
6173fb4f7cdSSrinivas Pandruvada int find_logical_cpu(int pkg_id, int die_id, int punit_core_id)
6183fb4f7cdSSrinivas Pandruvada {
6193fb4f7cdSSrinivas Pandruvada 	int i;
6203fb4f7cdSSrinivas Pandruvada 
6213fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < topo_max_cpus; ++i) {
6223fb4f7cdSSrinivas Pandruvada 		if (cpu_map[i].pkg_id == pkg_id &&
6233fb4f7cdSSrinivas Pandruvada 		    cpu_map[i].die_id == die_id &&
6243fb4f7cdSSrinivas Pandruvada 		    cpu_map[i].punit_cpu_core == punit_core_id)
6253fb4f7cdSSrinivas Pandruvada 			return i;
6263fb4f7cdSSrinivas Pandruvada 	}
6273fb4f7cdSSrinivas Pandruvada 
6283fb4f7cdSSrinivas Pandruvada 	return -EINVAL;
6293fb4f7cdSSrinivas Pandruvada }
6303fb4f7cdSSrinivas Pandruvada 
6313fb4f7cdSSrinivas Pandruvada void set_cpu_mask_from_punit_coremask(int cpu, unsigned long long core_mask,
6323fb4f7cdSSrinivas Pandruvada 				      size_t core_cpumask_size,
6333fb4f7cdSSrinivas Pandruvada 				      cpu_set_t *core_cpumask, int *cpu_cnt)
6343fb4f7cdSSrinivas Pandruvada {
6353fb4f7cdSSrinivas Pandruvada 	int i, cnt = 0;
6363fb4f7cdSSrinivas Pandruvada 	int die_id, pkg_id;
6373fb4f7cdSSrinivas Pandruvada 
6383fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = 0;
6393fb4f7cdSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
6403fb4f7cdSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
6413fb4f7cdSSrinivas Pandruvada 
6423fb4f7cdSSrinivas Pandruvada 	for (i = 0; i < 64; ++i) {
6433fb4f7cdSSrinivas Pandruvada 		if (core_mask & BIT(i)) {
6443fb4f7cdSSrinivas Pandruvada 			int j;
6453fb4f7cdSSrinivas Pandruvada 
6463fb4f7cdSSrinivas Pandruvada 			for (j = 0; j < topo_max_cpus; ++j) {
64744460efeSYouquan Song 				if (!CPU_ISSET_S(j, present_cpumask_size, present_cpumask))
64844460efeSYouquan Song 					continue;
64944460efeSYouquan Song 
6503fb4f7cdSSrinivas Pandruvada 				if (cpu_map[j].pkg_id == pkg_id &&
6513fb4f7cdSSrinivas Pandruvada 				    cpu_map[j].die_id == die_id &&
6523fb4f7cdSSrinivas Pandruvada 				    cpu_map[j].punit_cpu_core == i) {
6533fb4f7cdSSrinivas Pandruvada 					CPU_SET_S(j, core_cpumask_size,
6543fb4f7cdSSrinivas Pandruvada 						  core_cpumask);
6553fb4f7cdSSrinivas Pandruvada 					++cnt;
6563fb4f7cdSSrinivas Pandruvada 				}
6573fb4f7cdSSrinivas Pandruvada 			}
6583fb4f7cdSSrinivas Pandruvada 		}
6593fb4f7cdSSrinivas Pandruvada 	}
6603fb4f7cdSSrinivas Pandruvada 
6613fb4f7cdSSrinivas Pandruvada 	*cpu_cnt = cnt;
6623fb4f7cdSSrinivas Pandruvada }
6633fb4f7cdSSrinivas Pandruvada 
6643fb4f7cdSSrinivas Pandruvada int find_phy_core_num(int logical_cpu)
6653fb4f7cdSSrinivas Pandruvada {
6663fb4f7cdSSrinivas Pandruvada 	if (logical_cpu < topo_max_cpus)
6673fb4f7cdSSrinivas Pandruvada 		return cpu_map[logical_cpu].punit_cpu_core;
6683fb4f7cdSSrinivas Pandruvada 
6693fb4f7cdSSrinivas Pandruvada 	return -EINVAL;
6703fb4f7cdSSrinivas Pandruvada }
6713fb4f7cdSSrinivas Pandruvada 
6723fb4f7cdSSrinivas Pandruvada static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
6733fb4f7cdSSrinivas Pandruvada 				  unsigned int *value)
6743fb4f7cdSSrinivas Pandruvada {
6753fb4f7cdSSrinivas Pandruvada 	struct isst_if_io_regs io_regs;
6763fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
6773fb4f7cdSSrinivas Pandruvada 	int cmd;
6783fb4f7cdSSrinivas Pandruvada 	int fd;
6793fb4f7cdSSrinivas Pandruvada 
6803fb4f7cdSSrinivas Pandruvada 	debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
6813fb4f7cdSSrinivas Pandruvada 
6823fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
6833fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
6843fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
6853fb4f7cdSSrinivas Pandruvada 
6863fb4f7cdSSrinivas Pandruvada 	io_regs.req_count = 1;
6873fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].logical_cpu = cpu;
6883fb4f7cdSSrinivas Pandruvada 	io_regs.io_reg[0].reg = reg;
6893fb4f7cdSSrinivas Pandruvada 	cmd = ISST_IF_IO_CMD;
6903fb4f7cdSSrinivas Pandruvada 	if (write) {
6913fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 1;
6923fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].value = *value;
6933fb4f7cdSSrinivas Pandruvada 	} else {
6943fb4f7cdSSrinivas Pandruvada 		io_regs.io_reg[0].read_write = 0;
6953fb4f7cdSSrinivas Pandruvada 	}
6963fb4f7cdSSrinivas Pandruvada 
6973fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, cmd, &io_regs) == -1) {
6983fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_IO_CMD");
6993fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
7003fb4f7cdSSrinivas Pandruvada 			cpu, reg, write);
7013fb4f7cdSSrinivas Pandruvada 	} else {
7023fb4f7cdSSrinivas Pandruvada 		if (!write)
7033fb4f7cdSSrinivas Pandruvada 			*value = io_regs.io_reg[0].value;
7043fb4f7cdSSrinivas Pandruvada 
7053fb4f7cdSSrinivas Pandruvada 		debug_printf(
7063fb4f7cdSSrinivas Pandruvada 			"mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
7073fb4f7cdSSrinivas Pandruvada 			cpu, reg, write, *value);
7083fb4f7cdSSrinivas Pandruvada 	}
7093fb4f7cdSSrinivas Pandruvada 
7103fb4f7cdSSrinivas Pandruvada 	close(fd);
7113fb4f7cdSSrinivas Pandruvada 
7123fb4f7cdSSrinivas Pandruvada 	return 0;
7133fb4f7cdSSrinivas Pandruvada }
7143fb4f7cdSSrinivas Pandruvada 
7153fb4f7cdSSrinivas Pandruvada int isst_send_mbox_command(unsigned int cpu, unsigned char command,
7163fb4f7cdSSrinivas Pandruvada 			   unsigned char sub_command, unsigned int parameter,
7173fb4f7cdSSrinivas Pandruvada 			   unsigned int req_data, unsigned int *resp)
7183fb4f7cdSSrinivas Pandruvada {
7193fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
7203fb4f7cdSSrinivas Pandruvada 	int fd;
7213fb4f7cdSSrinivas Pandruvada 	struct isst_if_mbox_cmds mbox_cmds = { 0 };
7223fb4f7cdSSrinivas Pandruvada 
7233fb4f7cdSSrinivas Pandruvada 	debug_printf(
7243fb4f7cdSSrinivas Pandruvada 		"mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
7253fb4f7cdSSrinivas Pandruvada 		cpu, command, sub_command, parameter, req_data);
7263fb4f7cdSSrinivas Pandruvada 
7278ddbda76SSrinivas Pandruvada 	if (isst_platform_info.mmio_supported && command == CONFIG_CLOS &&
7288ddbda76SSrinivas Pandruvada 	    sub_command != CLOS_PM_QOS_CONFIG) {
7293fb4f7cdSSrinivas Pandruvada 		unsigned int value;
7303fb4f7cdSSrinivas Pandruvada 		int write = 0;
7313fb4f7cdSSrinivas Pandruvada 		int clos_id, core_id, ret = 0;
7323fb4f7cdSSrinivas Pandruvada 
733d2d1f304SSrinivas Pandruvada 		debug_printf("CPU %d\n", cpu);
7343fb4f7cdSSrinivas Pandruvada 
7353fb4f7cdSSrinivas Pandruvada 		if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
7363fb4f7cdSSrinivas Pandruvada 			value = req_data;
7373fb4f7cdSSrinivas Pandruvada 			write = 1;
7383fb4f7cdSSrinivas Pandruvada 		}
7393fb4f7cdSSrinivas Pandruvada 
7403fb4f7cdSSrinivas Pandruvada 		switch (sub_command) {
7413fb4f7cdSSrinivas Pandruvada 		case CLOS_PQR_ASSOC:
7423fb4f7cdSSrinivas Pandruvada 			core_id = parameter & 0xff;
7433fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
7443fb4f7cdSSrinivas Pandruvada 				cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
7453fb4f7cdSSrinivas Pandruvada 				&value);
7463fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
7473fb4f7cdSSrinivas Pandruvada 				*resp = value;
7483fb4f7cdSSrinivas Pandruvada 			break;
7493fb4f7cdSSrinivas Pandruvada 		case CLOS_PM_CLOS:
7503fb4f7cdSSrinivas Pandruvada 			clos_id = parameter & 0x03;
7513fb4f7cdSSrinivas Pandruvada 			ret = isst_send_mmio_command(
7523fb4f7cdSSrinivas Pandruvada 				cpu, PM_CLOS_OFFSET + clos_id * 4, write,
7533fb4f7cdSSrinivas Pandruvada 				&value);
7543fb4f7cdSSrinivas Pandruvada 			if (!ret && !write)
7553fb4f7cdSSrinivas Pandruvada 				*resp = value;
7563fb4f7cdSSrinivas Pandruvada 			break;
7573fb4f7cdSSrinivas Pandruvada 		case CLOS_STATUS:
7583fb4f7cdSSrinivas Pandruvada 			break;
7593fb4f7cdSSrinivas Pandruvada 		default:
7603fb4f7cdSSrinivas Pandruvada 			break;
7613fb4f7cdSSrinivas Pandruvada 		}
7623fb4f7cdSSrinivas Pandruvada 		return ret;
7633fb4f7cdSSrinivas Pandruvada 	}
7643fb4f7cdSSrinivas Pandruvada 
7653fb4f7cdSSrinivas Pandruvada 	mbox_cmds.cmd_count = 1;
7663fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
7673fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].command = command;
7683fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].sub_command = sub_command;
7693fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].parameter = parameter;
7703fb4f7cdSSrinivas Pandruvada 	mbox_cmds.mbox_cmd[0].req_data = req_data;
7713fb4f7cdSSrinivas Pandruvada 
7723fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
7733fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
7743fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
7753fb4f7cdSSrinivas Pandruvada 
7763fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
7773fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_MBOX_COMMAND");
7783fb4f7cdSSrinivas Pandruvada 		fprintf(outf,
7793fb4f7cdSSrinivas Pandruvada 			"Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
7803fb4f7cdSSrinivas Pandruvada 			cpu, command, sub_command, parameter, req_data);
7817af5a95bSSrinivas Pandruvada 		return -1;
7823fb4f7cdSSrinivas Pandruvada 	} else {
7833fb4f7cdSSrinivas Pandruvada 		*resp = mbox_cmds.mbox_cmd[0].resp_data;
7843fb4f7cdSSrinivas Pandruvada 		debug_printf(
7853fb4f7cdSSrinivas Pandruvada 			"mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
7863fb4f7cdSSrinivas Pandruvada 			cpu, command, sub_command, parameter, req_data, *resp);
7873fb4f7cdSSrinivas Pandruvada 	}
7883fb4f7cdSSrinivas Pandruvada 
7893fb4f7cdSSrinivas Pandruvada 	close(fd);
7903fb4f7cdSSrinivas Pandruvada 
7913fb4f7cdSSrinivas Pandruvada 	return 0;
7923fb4f7cdSSrinivas Pandruvada }
7933fb4f7cdSSrinivas Pandruvada 
7943fb4f7cdSSrinivas Pandruvada int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
7953fb4f7cdSSrinivas Pandruvada 			  unsigned long long *req_resp)
7963fb4f7cdSSrinivas Pandruvada {
7973fb4f7cdSSrinivas Pandruvada 	struct isst_if_msr_cmds msr_cmds;
7983fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
7993fb4f7cdSSrinivas Pandruvada 	int fd;
8003fb4f7cdSSrinivas Pandruvada 
8013fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8023fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8033fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
8043fb4f7cdSSrinivas Pandruvada 
8053fb4f7cdSSrinivas Pandruvada 	msr_cmds.cmd_count = 1;
8063fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].logical_cpu = cpu;
8073fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].msr = msr;
8083fb4f7cdSSrinivas Pandruvada 	msr_cmds.msr_cmd[0].read_write = write;
8093fb4f7cdSSrinivas Pandruvada 	if (write)
8103fb4f7cdSSrinivas Pandruvada 		msr_cmds.msr_cmd[0].data = *req_resp;
8113fb4f7cdSSrinivas Pandruvada 
8123fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
8133fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_MSR_COMMAD");
8143fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
8153fb4f7cdSSrinivas Pandruvada 			cpu, msr, write);
8163fb4f7cdSSrinivas Pandruvada 	} else {
8173fb4f7cdSSrinivas Pandruvada 		if (!write)
8183fb4f7cdSSrinivas Pandruvada 			*req_resp = msr_cmds.msr_cmd[0].data;
8193fb4f7cdSSrinivas Pandruvada 
8203fb4f7cdSSrinivas Pandruvada 		debug_printf(
8213fb4f7cdSSrinivas Pandruvada 			"msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
8223fb4f7cdSSrinivas Pandruvada 			cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
8233fb4f7cdSSrinivas Pandruvada 	}
8243fb4f7cdSSrinivas Pandruvada 
8253fb4f7cdSSrinivas Pandruvada 	close(fd);
8263fb4f7cdSSrinivas Pandruvada 
8273fb4f7cdSSrinivas Pandruvada 	return 0;
8283fb4f7cdSSrinivas Pandruvada }
8293fb4f7cdSSrinivas Pandruvada 
8303fb4f7cdSSrinivas Pandruvada static int isst_fill_platform_info(void)
8313fb4f7cdSSrinivas Pandruvada {
8323fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
8333fb4f7cdSSrinivas Pandruvada 	int fd;
8343fb4f7cdSSrinivas Pandruvada 
8353fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
8363fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
8373fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
8383fb4f7cdSSrinivas Pandruvada 
8393fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &isst_platform_info) == -1) {
8403fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
8413fb4f7cdSSrinivas Pandruvada 		close(fd);
8423fb4f7cdSSrinivas Pandruvada 		return -1;
8433fb4f7cdSSrinivas Pandruvada 	}
8443fb4f7cdSSrinivas Pandruvada 
8453fb4f7cdSSrinivas Pandruvada 	close(fd);
8463fb4f7cdSSrinivas Pandruvada 
8473bc3d30cSPrarit Bhargava 	if (isst_platform_info.api_version > supported_api_ver) {
8483bc3d30cSPrarit Bhargava 		printf("Incompatible API versions; Upgrade of tool is required\n");
8493bc3d30cSPrarit Bhargava 		return -1;
8503bc3d30cSPrarit Bhargava 	}
8513fb4f7cdSSrinivas Pandruvada 	return 0;
8523fb4f7cdSSrinivas Pandruvada }
8533fb4f7cdSSrinivas Pandruvada 
8541ba148aeSSrinivas Pandruvada static void isst_print_extended_platform_info(void)
8551ba148aeSSrinivas Pandruvada {
8561ba148aeSSrinivas Pandruvada 	int cp_state, cp_cap, fact_support = 0, pbf_support = 0;
8571ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info ctdp_level;
8581ba148aeSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
8591ba148aeSSrinivas Pandruvada 	int ret, i, j;
8601ba148aeSSrinivas Pandruvada 	FILE *filep;
8611ba148aeSSrinivas Pandruvada 
8621ba148aeSSrinivas Pandruvada 	for (i = 0; i < 256; ++i) {
8631ba148aeSSrinivas Pandruvada 		char path[256];
8641ba148aeSSrinivas Pandruvada 
8651ba148aeSSrinivas Pandruvada 		snprintf(path, sizeof(path),
8661ba148aeSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);
8671ba148aeSSrinivas Pandruvada 		filep = fopen(path, "r");
8681ba148aeSSrinivas Pandruvada 		if (filep)
8691ba148aeSSrinivas Pandruvada 			break;
8701ba148aeSSrinivas Pandruvada 	}
8711ba148aeSSrinivas Pandruvada 
8721ba148aeSSrinivas Pandruvada 	if (!filep)
8731ba148aeSSrinivas Pandruvada 		return;
8741ba148aeSSrinivas Pandruvada 
8751ba148aeSSrinivas Pandruvada 	fclose(filep);
8761ba148aeSSrinivas Pandruvada 
8771ba148aeSSrinivas Pandruvada 	ret = isst_get_ctdp_levels(i, &pkg_dev);
8781ba148aeSSrinivas Pandruvada 	if (ret)
8791ba148aeSSrinivas Pandruvada 		return;
8801ba148aeSSrinivas Pandruvada 
8811ba148aeSSrinivas Pandruvada 	if (pkg_dev.enabled) {
8821ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is supported\n");
8831ba148aeSSrinivas Pandruvada 	} else {
8841ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-PP (feature perf-profile) is not supported\n");
8851ba148aeSSrinivas Pandruvada 		fprintf(outf, "Only performance level 0 (base level) is present\n");
8861ba148aeSSrinivas Pandruvada 	}
8871ba148aeSSrinivas Pandruvada 
8881ba148aeSSrinivas Pandruvada 	if (pkg_dev.locked)
8891ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is locked\n");
8901ba148aeSSrinivas Pandruvada 	else
8911ba148aeSSrinivas Pandruvada 		fprintf(outf, "TDP level change control is unlocked, max level: %d \n", pkg_dev.levels);
8921ba148aeSSrinivas Pandruvada 
8931ba148aeSSrinivas Pandruvada 	for (j = 0; j <= pkg_dev.levels; ++j) {
8941ba148aeSSrinivas Pandruvada 		ret = isst_get_ctdp_control(i, j, &ctdp_level);
8951ba148aeSSrinivas Pandruvada 		if (ret)
8961ba148aeSSrinivas Pandruvada 			continue;
8971ba148aeSSrinivas Pandruvada 
8981ba148aeSSrinivas Pandruvada 		if (!fact_support && ctdp_level.fact_support)
8991ba148aeSSrinivas Pandruvada 			fact_support = 1;
9001ba148aeSSrinivas Pandruvada 
9011ba148aeSSrinivas Pandruvada 		if (!pbf_support && ctdp_level.pbf_support)
9021ba148aeSSrinivas Pandruvada 			pbf_support = 1;
9031ba148aeSSrinivas Pandruvada 	}
9041ba148aeSSrinivas Pandruvada 
9051ba148aeSSrinivas Pandruvada 	if (fact_support)
9061ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is supported\n");
9071ba148aeSSrinivas Pandruvada 	else
9081ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-TF (feature turbo-freq) is not supported\n");
9091ba148aeSSrinivas Pandruvada 
9101ba148aeSSrinivas Pandruvada 	if (pbf_support)
9111ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is supported\n");
9121ba148aeSSrinivas Pandruvada 	else
9131ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-BF (feature base-freq) is not supported\n");
9141ba148aeSSrinivas Pandruvada 
9151ba148aeSSrinivas Pandruvada 	ret = isst_read_pm_config(i, &cp_state, &cp_cap);
9161ba148aeSSrinivas Pandruvada 	if (cp_cap)
9171ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is supported\n");
9181ba148aeSSrinivas Pandruvada 	else
9191ba148aeSSrinivas Pandruvada 		fprintf(outf, "Intel(R) SST-CP (feature core-power) is not supported\n");
9201ba148aeSSrinivas Pandruvada }
9211ba148aeSSrinivas Pandruvada 
9223fb4f7cdSSrinivas Pandruvada static void isst_print_platform_information(void)
9233fb4f7cdSSrinivas Pandruvada {
9243fb4f7cdSSrinivas Pandruvada 	struct isst_if_platform_info platform_info;
9253fb4f7cdSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
9263fb4f7cdSSrinivas Pandruvada 	int fd;
9273fb4f7cdSSrinivas Pandruvada 
9281ba148aeSSrinivas Pandruvada 	if (is_clx_n_platform()) {
9291ba148aeSSrinivas Pandruvada 		fprintf(stderr, "\nThis option in not supported on this platform\n");
9301ba148aeSSrinivas Pandruvada 		exit(0);
9311ba148aeSSrinivas Pandruvada 	}
9321ba148aeSSrinivas Pandruvada 
9333fb4f7cdSSrinivas Pandruvada 	fd = open(pathname, O_RDWR);
9343fb4f7cdSSrinivas Pandruvada 	if (fd < 0)
9353fb4f7cdSSrinivas Pandruvada 		err(-1, "%s open failed", pathname);
9363fb4f7cdSSrinivas Pandruvada 
9373fb4f7cdSSrinivas Pandruvada 	if (ioctl(fd, ISST_IF_GET_PLATFORM_INFO, &platform_info) == -1) {
9383fb4f7cdSSrinivas Pandruvada 		perror("ISST_IF_GET_PLATFORM_INFO");
9393fb4f7cdSSrinivas Pandruvada 	} else {
9403fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: API version : %d\n",
9413fb4f7cdSSrinivas Pandruvada 			platform_info.api_version);
9423fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: Driver version : %d\n",
9433fb4f7cdSSrinivas Pandruvada 			platform_info.driver_version);
9443fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mbox supported : %d\n",
9453fb4f7cdSSrinivas Pandruvada 			platform_info.mbox_supported);
9463fb4f7cdSSrinivas Pandruvada 		fprintf(outf, "Platform: mmio supported : %d\n",
9473fb4f7cdSSrinivas Pandruvada 			platform_info.mmio_supported);
9481ba148aeSSrinivas Pandruvada 		isst_print_extended_platform_info();
9493fb4f7cdSSrinivas Pandruvada 	}
9503fb4f7cdSSrinivas Pandruvada 
9513fb4f7cdSSrinivas Pandruvada 	close(fd);
9523fb4f7cdSSrinivas Pandruvada 
9533fb4f7cdSSrinivas Pandruvada 	exit(0);
9543fb4f7cdSSrinivas Pandruvada }
9553fb4f7cdSSrinivas Pandruvada 
9563d1a8579SSrinivas Pandruvada static char *local_str0, *local_str1;
9573fb4f7cdSSrinivas Pandruvada static void exec_on_get_ctdp_cpu(int cpu, void *arg1, void *arg2, void *arg3,
9583fb4f7cdSSrinivas Pandruvada 				 void *arg4)
9593fb4f7cdSSrinivas Pandruvada {
9603fb4f7cdSSrinivas Pandruvada 	int (*fn_ptr)(int cpu, void *arg);
9613fb4f7cdSSrinivas Pandruvada 	int ret;
9623fb4f7cdSSrinivas Pandruvada 
9633fb4f7cdSSrinivas Pandruvada 	fn_ptr = arg1;
9643fb4f7cdSSrinivas Pandruvada 	ret = fn_ptr(cpu, arg2);
9653fb4f7cdSSrinivas Pandruvada 	if (ret)
9663d1a8579SSrinivas Pandruvada 		isst_display_error_info_message(1, "get_tdp_* failed", 0, 0);
9673fb4f7cdSSrinivas Pandruvada 	else
968b3abfd77SSrinivas Pandruvada 		isst_ctdp_display_core_info(cpu, outf, arg3,
9693d1a8579SSrinivas Pandruvada 					    *(unsigned int *)arg4,
9703d1a8579SSrinivas Pandruvada 					    local_str0, local_str1);
9713fb4f7cdSSrinivas Pandruvada }
9723fb4f7cdSSrinivas Pandruvada 
9733d1a8579SSrinivas Pandruvada #define _get_tdp_level(desc, suffix, object, help, str0, str1)			\
974ce1326a2SPrarit Bhargava 	static void get_tdp_##object(int arg)                                    \
9753fb4f7cdSSrinivas Pandruvada 	{                                                                         \
9763fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp ctdp;                                        \
9773fb4f7cdSSrinivas Pandruvada \
9783fb4f7cdSSrinivas Pandruvada 		if (cmd_help) {                                                   \
9793fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,                                           \
9803fb4f7cdSSrinivas Pandruvada 				"Print %s [No command arguments are required]\n", \
9813fb4f7cdSSrinivas Pandruvada 				help);                                            \
9823fb4f7cdSSrinivas Pandruvada 			exit(0);                                                  \
9833fb4f7cdSSrinivas Pandruvada 		}                                                                 \
9843d1a8579SSrinivas Pandruvada 		local_str0 = str0;						  \
9853d1a8579SSrinivas Pandruvada 		local_str1 = str1;						  \
9863fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_start(outf);                        \
9873fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus)                                              \
9883fb4f7cdSSrinivas Pandruvada 			for_each_online_target_cpu_in_set(                        \
9893fb4f7cdSSrinivas Pandruvada 				exec_on_get_ctdp_cpu, isst_get_ctdp_##suffix,     \
9903fb4f7cdSSrinivas Pandruvada 				&ctdp, desc, &ctdp.object);                       \
9913fb4f7cdSSrinivas Pandruvada 		else                                                              \
9923fb4f7cdSSrinivas Pandruvada 			for_each_online_package_in_set(exec_on_get_ctdp_cpu,      \
9933fb4f7cdSSrinivas Pandruvada 						       isst_get_ctdp_##suffix,    \
9943fb4f7cdSSrinivas Pandruvada 						       &ctdp, desc,               \
9953fb4f7cdSSrinivas Pandruvada 						       &ctdp.object);             \
9963fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);                          \
9973fb4f7cdSSrinivas Pandruvada 	}
9983fb4f7cdSSrinivas Pandruvada 
9993d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-levels", levels, levels, "Max TDP level", NULL, NULL);
10003d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-version", levels, version, "TDP version", NULL, NULL);
10013d1a8579SSrinivas Pandruvada _get_tdp_level("get-config-enabled", levels, enabled, "perf-profile enable status", "disabled", "enabled");
10023fb4f7cdSSrinivas Pandruvada _get_tdp_level("get-config-current_level", levels, current_level,
10033d1a8579SSrinivas Pandruvada 	       "Current TDP Level", NULL, NULL);
10043d1a8579SSrinivas Pandruvada _get_tdp_level("get-lock-status", levels, locked, "TDP lock status", "unlocked", "locked");
10053fb4f7cdSSrinivas Pandruvada 
1006062e4aacSPrarit Bhargava struct isst_pkg_ctdp clx_n_pkg_dev;
1007062e4aacSPrarit Bhargava 
1008062e4aacSPrarit Bhargava static int clx_n_get_base_ratio(void)
1009062e4aacSPrarit Bhargava {
1010062e4aacSPrarit Bhargava 	FILE *fp;
1011062e4aacSPrarit Bhargava 	char *begin, *end, *line = NULL;
1012062e4aacSPrarit Bhargava 	char number[5];
1013062e4aacSPrarit Bhargava 	float value = 0;
1014062e4aacSPrarit Bhargava 	size_t n = 0;
1015062e4aacSPrarit Bhargava 
1016062e4aacSPrarit Bhargava 	fp = fopen("/proc/cpuinfo", "r");
1017062e4aacSPrarit Bhargava 	if (!fp)
1018062e4aacSPrarit Bhargava 		err(-1, "cannot open /proc/cpuinfo\n");
1019062e4aacSPrarit Bhargava 
1020062e4aacSPrarit Bhargava 	while (getline(&line, &n, fp) > 0) {
1021062e4aacSPrarit Bhargava 		if (strstr(line, "model name")) {
1022062e4aacSPrarit Bhargava 			/* this is true for CascadeLake-N */
1023062e4aacSPrarit Bhargava 			begin = strstr(line, "@ ") + 2;
1024062e4aacSPrarit Bhargava 			end = strstr(line, "GHz");
1025062e4aacSPrarit Bhargava 			strncpy(number, begin, end - begin);
1026062e4aacSPrarit Bhargava 			value = atof(number) * 10;
1027062e4aacSPrarit Bhargava 			break;
1028062e4aacSPrarit Bhargava 		}
1029062e4aacSPrarit Bhargava 	}
1030062e4aacSPrarit Bhargava 	free(line);
1031062e4aacSPrarit Bhargava 	fclose(fp);
1032062e4aacSPrarit Bhargava 
1033062e4aacSPrarit Bhargava 	return (int)(value);
1034062e4aacSPrarit Bhargava }
1035062e4aacSPrarit Bhargava 
1036062e4aacSPrarit Bhargava static int clx_n_config(int cpu)
1037062e4aacSPrarit Bhargava {
1038062e4aacSPrarit Bhargava 	int i, ret, pkg_id, die_id;
1039062e4aacSPrarit Bhargava 	unsigned long cpu_bf;
1040062e4aacSPrarit Bhargava 	struct isst_pkg_ctdp_level_info *ctdp_level;
1041062e4aacSPrarit Bhargava 	struct isst_pbf_info *pbf_info;
1042062e4aacSPrarit Bhargava 
1043062e4aacSPrarit Bhargava 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1044062e4aacSPrarit Bhargava 	pbf_info = &ctdp_level->pbf_info;
1045062e4aacSPrarit Bhargava 	ctdp_level->core_cpumask_size =
1046062e4aacSPrarit Bhargava 			alloc_cpu_set(&ctdp_level->core_cpumask);
1047062e4aacSPrarit Bhargava 
1048062e4aacSPrarit Bhargava 	/* find the frequency base ratio */
1049062e4aacSPrarit Bhargava 	ctdp_level->tdp_ratio = clx_n_get_base_ratio();
1050062e4aacSPrarit Bhargava 	if (ctdp_level->tdp_ratio == 0) {
1051062e4aacSPrarit Bhargava 		debug_printf("CLX: cn base ratio is zero\n");
1052062e4aacSPrarit Bhargava 		ret = -1;
1053062e4aacSPrarit Bhargava 		goto error_ret;
1054062e4aacSPrarit Bhargava 	}
1055062e4aacSPrarit Bhargava 
1056062e4aacSPrarit Bhargava 	/* find the high and low priority frequencies */
1057062e4aacSPrarit Bhargava 	pbf_info->p1_high = 0;
1058062e4aacSPrarit Bhargava 	pbf_info->p1_low = ~0;
1059062e4aacSPrarit Bhargava 
1060062e4aacSPrarit Bhargava 	pkg_id = get_physical_package_id(cpu);
1061062e4aacSPrarit Bhargava 	die_id = get_physical_die_id(cpu);
1062062e4aacSPrarit Bhargava 
1063062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1064062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1065062e4aacSPrarit Bhargava 			continue;
1066062e4aacSPrarit Bhargava 
1067062e4aacSPrarit Bhargava 		if (pkg_id != get_physical_package_id(i) ||
1068062e4aacSPrarit Bhargava 		    die_id != get_physical_die_id(i))
1069062e4aacSPrarit Bhargava 			continue;
1070062e4aacSPrarit Bhargava 
1071062e4aacSPrarit Bhargava 		CPU_SET_S(i, ctdp_level->core_cpumask_size,
1072062e4aacSPrarit Bhargava 			  ctdp_level->core_cpumask);
1073062e4aacSPrarit Bhargava 
1074062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1075062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1076062e4aacSPrarit Bhargava 					i);
1077062e4aacSPrarit Bhargava 		if (cpu_bf > pbf_info->p1_high)
1078062e4aacSPrarit Bhargava 			pbf_info->p1_high = cpu_bf;
1079062e4aacSPrarit Bhargava 		if (cpu_bf < pbf_info->p1_low)
1080062e4aacSPrarit Bhargava 			pbf_info->p1_low = cpu_bf;
1081062e4aacSPrarit Bhargava 	}
1082062e4aacSPrarit Bhargava 
1083062e4aacSPrarit Bhargava 	if (pbf_info->p1_high == ~0UL) {
1084062e4aacSPrarit Bhargava 		debug_printf("CLX: maximum base frequency not set\n");
1085062e4aacSPrarit Bhargava 		ret = -1;
1086062e4aacSPrarit Bhargava 		goto error_ret;
1087062e4aacSPrarit Bhargava 	}
1088062e4aacSPrarit Bhargava 
1089062e4aacSPrarit Bhargava 	if (pbf_info->p1_low == 0) {
1090062e4aacSPrarit Bhargava 		debug_printf("CLX: minimum base frequency not set\n");
1091062e4aacSPrarit Bhargava 		ret = -1;
1092062e4aacSPrarit Bhargava 		goto error_ret;
1093062e4aacSPrarit Bhargava 	}
1094062e4aacSPrarit Bhargava 
1095062e4aacSPrarit Bhargava 	/* convert frequencies back to ratios */
109691d92814SSrinivas Pandruvada 	pbf_info->p1_high = pbf_info->p1_high / 100000;
109791d92814SSrinivas Pandruvada 	pbf_info->p1_low = pbf_info->p1_low / 100000;
1098062e4aacSPrarit Bhargava 
1099062e4aacSPrarit Bhargava 	/* create high priority cpu mask */
1100062e4aacSPrarit Bhargava 	pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
1101062e4aacSPrarit Bhargava 	for (i = 0; i < topo_max_cpus; i++) {
1102062e4aacSPrarit Bhargava 		if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1103062e4aacSPrarit Bhargava 			continue;
1104062e4aacSPrarit Bhargava 
1105062e4aacSPrarit Bhargava 		if (pkg_id != get_physical_package_id(i) ||
1106062e4aacSPrarit Bhargava 		    die_id != get_physical_die_id(i))
1107062e4aacSPrarit Bhargava 			continue;
1108062e4aacSPrarit Bhargava 
1109062e4aacSPrarit Bhargava 		cpu_bf = parse_int_file(1,
1110062e4aacSPrarit Bhargava 			"/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency",
1111062e4aacSPrarit Bhargava 					i);
111291d92814SSrinivas Pandruvada 		cpu_bf = cpu_bf / 100000;
1113062e4aacSPrarit Bhargava 		if (cpu_bf == pbf_info->p1_high)
1114062e4aacSPrarit Bhargava 			CPU_SET_S(i, pbf_info->core_cpumask_size,
1115062e4aacSPrarit Bhargava 				  pbf_info->core_cpumask);
1116062e4aacSPrarit Bhargava 	}
1117062e4aacSPrarit Bhargava 
1118062e4aacSPrarit Bhargava 	/* extra ctdp & pbf struct parameters */
1119062e4aacSPrarit Bhargava 	ctdp_level->processed = 1;
1120062e4aacSPrarit Bhargava 	ctdp_level->pbf_support = 1; /* PBF is always supported and enabled */
1121062e4aacSPrarit Bhargava 	ctdp_level->pbf_enabled = 1;
1122062e4aacSPrarit Bhargava 	ctdp_level->fact_support = 0; /* FACT is never supported */
1123062e4aacSPrarit Bhargava 	ctdp_level->fact_enabled = 0;
1124062e4aacSPrarit Bhargava 
1125062e4aacSPrarit Bhargava 	return 0;
1126062e4aacSPrarit Bhargava 
1127062e4aacSPrarit Bhargava error_ret:
1128062e4aacSPrarit Bhargava 	free_cpu_set(ctdp_level->core_cpumask);
1129062e4aacSPrarit Bhargava 	return ret;
1130062e4aacSPrarit Bhargava }
1131062e4aacSPrarit Bhargava 
1132062e4aacSPrarit Bhargava static void dump_clx_n_config_for_cpu(int cpu, void *arg1, void *arg2,
1133062e4aacSPrarit Bhargava 				   void *arg3, void *arg4)
1134062e4aacSPrarit Bhargava {
1135062e4aacSPrarit Bhargava 	int ret;
1136062e4aacSPrarit Bhargava 
1137ac9d05eaSSrinivas Pandruvada 	if (tdp_level != 0xff && tdp_level != 0) {
1138ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid level", 1, tdp_level);
1139ac9d05eaSSrinivas Pandruvada 		exit(0);
1140ac9d05eaSSrinivas Pandruvada 	}
1141ac9d05eaSSrinivas Pandruvada 
1142062e4aacSPrarit Bhargava 	ret = clx_n_config(cpu);
1143062e4aacSPrarit Bhargava 	if (ret) {
1144062e4aacSPrarit Bhargava 		perror("isst_get_process_ctdp");
1145062e4aacSPrarit Bhargava 	} else {
1146062e4aacSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
1147062e4aacSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
1148062e4aacSPrarit Bhargava 
1149062e4aacSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1150062e4aacSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
1151062e4aacSPrarit Bhargava 		isst_ctdp_display_information(cpu, outf, tdp_level, &clx_n_pkg_dev);
1152062e4aacSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
1153062e4aacSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
1154062e4aacSPrarit Bhargava 	}
1155062e4aacSPrarit Bhargava }
1156062e4aacSPrarit Bhargava 
11573fb4f7cdSSrinivas Pandruvada static void dump_isst_config_for_cpu(int cpu, void *arg1, void *arg2,
11583fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
11593fb4f7cdSSrinivas Pandruvada {
11603fb4f7cdSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
11613fb4f7cdSSrinivas Pandruvada 	int ret;
11623fb4f7cdSSrinivas Pandruvada 
11633fb4f7cdSSrinivas Pandruvada 	memset(&pkg_dev, 0, sizeof(pkg_dev));
11643fb4f7cdSSrinivas Pandruvada 	ret = isst_get_process_ctdp(cpu, tdp_level, &pkg_dev);
11653fb4f7cdSSrinivas Pandruvada 	if (ret) {
1166ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get perf-profile info on cpu", 1, cpu);
1167ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1168ac9d05eaSSrinivas Pandruvada 		exit(1);
11693fb4f7cdSSrinivas Pandruvada 	} else {
11703fb4f7cdSSrinivas Pandruvada 		isst_ctdp_display_information(cpu, outf, tdp_level, &pkg_dev);
11713fb4f7cdSSrinivas Pandruvada 		isst_get_process_ctdp_complete(cpu, &pkg_dev);
11723fb4f7cdSSrinivas Pandruvada 	}
11733fb4f7cdSSrinivas Pandruvada }
11743fb4f7cdSSrinivas Pandruvada 
1175ce1326a2SPrarit Bhargava static void dump_isst_config(int arg)
11763fb4f7cdSSrinivas Pandruvada {
1177062e4aacSPrarit Bhargava 	void *fn;
1178062e4aacSPrarit Bhargava 
11793fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
11803fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
11813fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology Performance profile configuration\n");
11823fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
11833fb4f7cdSSrinivas Pandruvada 			"including base frequency and turbo frequency configurations\n");
11843fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Optional: -l|--level : Specify tdp level\n");
11853fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
11863fb4f7cdSSrinivas Pandruvada 			"\tIf no arguments, dump information for all TDP levels\n");
11873fb4f7cdSSrinivas Pandruvada 		exit(0);
11883fb4f7cdSSrinivas Pandruvada 	}
11893fb4f7cdSSrinivas Pandruvada 
1190062e4aacSPrarit Bhargava 	if (!is_clx_n_platform())
1191062e4aacSPrarit Bhargava 		fn = dump_isst_config_for_cpu;
1192062e4aacSPrarit Bhargava 	else
1193062e4aacSPrarit Bhargava 		fn = dump_clx_n_config_for_cpu;
1194062e4aacSPrarit Bhargava 
11953fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
11963fb4f7cdSSrinivas Pandruvada 
11973fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
1198062e4aacSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
11993fb4f7cdSSrinivas Pandruvada 	else
1200062e4aacSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
12013fb4f7cdSSrinivas Pandruvada 
12023fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
12033fb4f7cdSSrinivas Pandruvada }
12043fb4f7cdSSrinivas Pandruvada 
12053fb4f7cdSSrinivas Pandruvada static void set_tdp_level_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
12063fb4f7cdSSrinivas Pandruvada 				  void *arg4)
12073fb4f7cdSSrinivas Pandruvada {
12083fb4f7cdSSrinivas Pandruvada 	int ret;
12093fb4f7cdSSrinivas Pandruvada 
12103fb4f7cdSSrinivas Pandruvada 	ret = isst_set_tdp_level(cpu, tdp_level);
1211ac9d05eaSSrinivas Pandruvada 	if (ret) {
1212ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Set TDP level failed", 0, 0);
1213ac9d05eaSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1214ac9d05eaSSrinivas Pandruvada 		exit(1);
1215ac9d05eaSSrinivas Pandruvada 	} else {
12163fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "perf-profile", "set_tdp_level",
12173fb4f7cdSSrinivas Pandruvada 				    ret);
12183c64c81aSSrinivas Pandruvada 		if (force_online_offline) {
12193c64c81aSSrinivas Pandruvada 			struct isst_pkg_ctdp_level_info ctdp_level;
12203c64c81aSSrinivas Pandruvada 			int pkg_id = get_physical_package_id(cpu);
12213c64c81aSSrinivas Pandruvada 			int die_id = get_physical_die_id(cpu);
12223c64c81aSSrinivas Pandruvada 
12233c64c81aSSrinivas Pandruvada 			fprintf(stderr, "Option is set to online/offline\n");
12243c64c81aSSrinivas Pandruvada 			ctdp_level.core_cpumask_size =
12253c64c81aSSrinivas Pandruvada 				alloc_cpu_set(&ctdp_level.core_cpumask);
12263c64c81aSSrinivas Pandruvada 			isst_get_coremask_info(cpu, tdp_level, &ctdp_level);
12273c64c81aSSrinivas Pandruvada 			if (ctdp_level.cpu_count) {
12283c64c81aSSrinivas Pandruvada 				int i, max_cpus = get_topo_max_cpus();
12293c64c81aSSrinivas Pandruvada 				for (i = 0; i < max_cpus; ++i) {
12303c64c81aSSrinivas Pandruvada 					if (pkg_id != get_physical_package_id(i) || die_id != get_physical_die_id(i))
12313c64c81aSSrinivas Pandruvada 						continue;
12323c64c81aSSrinivas Pandruvada 					if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) {
12333c64c81aSSrinivas Pandruvada 						fprintf(stderr, "online cpu %d\n", i);
12343c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 1);
12353c64c81aSSrinivas Pandruvada 					} else {
12363c64c81aSSrinivas Pandruvada 						fprintf(stderr, "offline cpu %d\n", i);
12373c64c81aSSrinivas Pandruvada 						set_cpu_online_offline(i, 0);
12383c64c81aSSrinivas Pandruvada 					}
12393c64c81aSSrinivas Pandruvada 				}
12403c64c81aSSrinivas Pandruvada 			}
12413c64c81aSSrinivas Pandruvada 		}
12423c64c81aSSrinivas Pandruvada 	}
12433fb4f7cdSSrinivas Pandruvada }
12443fb4f7cdSSrinivas Pandruvada 
1245ce1326a2SPrarit Bhargava static void set_tdp_level(int arg)
12463fb4f7cdSSrinivas Pandruvada {
12473fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
12483fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Set Config TDP level\n");
12493fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
12503fb4f7cdSSrinivas Pandruvada 			"\t Arguments: -l|--level : Specify tdp level\n");
12513c64c81aSSrinivas Pandruvada 		fprintf(stderr,
12523c64c81aSSrinivas Pandruvada 			"\t Optional Arguments: -o | online : online/offline for the tdp level\n");
1253ac9d05eaSSrinivas Pandruvada 		fprintf(stderr,
1254ac9d05eaSSrinivas Pandruvada 			"\t  online/offline operation has limitations, refer to Linux hotplug documentation\n");
12553fb4f7cdSSrinivas Pandruvada 		exit(0);
12563fb4f7cdSSrinivas Pandruvada 	}
12573fb4f7cdSSrinivas Pandruvada 
12583fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1259ac9d05eaSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
12603fb4f7cdSSrinivas Pandruvada 		exit(1);
12613fb4f7cdSSrinivas Pandruvada 	}
12623fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
12633fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
12643fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_tdp_level_for_cpu, NULL,
12653fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
12663fb4f7cdSSrinivas Pandruvada 	else
12673fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_tdp_level_for_cpu, NULL,
12683fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
12693fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
12703fb4f7cdSSrinivas Pandruvada }
12713fb4f7cdSSrinivas Pandruvada 
12721aa7177cSPrarit Bhargava static void clx_n_dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2,
12731aa7177cSPrarit Bhargava 				       void *arg3, void *arg4)
12741aa7177cSPrarit Bhargava {
12751aa7177cSPrarit Bhargava 	int ret;
12761aa7177cSPrarit Bhargava 
12771aa7177cSPrarit Bhargava 	ret = clx_n_config(cpu);
12781aa7177cSPrarit Bhargava 	if (ret) {
127939bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "clx_n_config failed", 0, 0);
12801aa7177cSPrarit Bhargava 	} else {
12811aa7177cSPrarit Bhargava 		struct isst_pkg_ctdp_level_info *ctdp_level;
12821aa7177cSPrarit Bhargava 		struct isst_pbf_info *pbf_info;
12831aa7177cSPrarit Bhargava 
12841aa7177cSPrarit Bhargava 		ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
12851aa7177cSPrarit Bhargava 		pbf_info = &ctdp_level->pbf_info;
12861aa7177cSPrarit Bhargava 		isst_pbf_display_information(cpu, outf, tdp_level, pbf_info);
12871aa7177cSPrarit Bhargava 		free_cpu_set(ctdp_level->core_cpumask);
12881aa7177cSPrarit Bhargava 		free_cpu_set(pbf_info->core_cpumask);
12891aa7177cSPrarit Bhargava 	}
12901aa7177cSPrarit Bhargava }
12911aa7177cSPrarit Bhargava 
12923fb4f7cdSSrinivas Pandruvada static void dump_pbf_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
12933fb4f7cdSSrinivas Pandruvada 				    void *arg4)
12943fb4f7cdSSrinivas Pandruvada {
12953fb4f7cdSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
12963fb4f7cdSSrinivas Pandruvada 	int ret;
12973fb4f7cdSSrinivas Pandruvada 
12983fb4f7cdSSrinivas Pandruvada 	ret = isst_get_pbf_info(cpu, tdp_level, &pbf_info);
12993fb4f7cdSSrinivas Pandruvada 	if (ret) {
130039bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get base-freq info at this level", 1, tdp_level);
130139bae0fcSSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
130239bae0fcSSrinivas Pandruvada 		exit(1);
13033fb4f7cdSSrinivas Pandruvada 	} else {
13043fb4f7cdSSrinivas Pandruvada 		isst_pbf_display_information(cpu, outf, tdp_level, &pbf_info);
13053fb4f7cdSSrinivas Pandruvada 		isst_get_pbf_info_complete(&pbf_info);
13063fb4f7cdSSrinivas Pandruvada 	}
13073fb4f7cdSSrinivas Pandruvada }
13083fb4f7cdSSrinivas Pandruvada 
1309ce1326a2SPrarit Bhargava static void dump_pbf_config(int arg)
13103fb4f7cdSSrinivas Pandruvada {
13111aa7177cSPrarit Bhargava 	void *fn;
13121aa7177cSPrarit Bhargava 
13133fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
13143fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
13153fb4f7cdSSrinivas Pandruvada 			"Print Intel(R) Speed Select Technology base frequency configuration for a TDP level\n");
13163fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
13173fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
13183fb4f7cdSSrinivas Pandruvada 		exit(0);
13193fb4f7cdSSrinivas Pandruvada 	}
13203fb4f7cdSSrinivas Pandruvada 
13213fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
132239bae0fcSSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level", 0, 0);
13233fb4f7cdSSrinivas Pandruvada 		exit(1);
13243fb4f7cdSSrinivas Pandruvada 	}
13253fb4f7cdSSrinivas Pandruvada 
13261aa7177cSPrarit Bhargava 	if (!is_clx_n_platform())
13271aa7177cSPrarit Bhargava 		fn = dump_pbf_config_for_cpu;
13283fb4f7cdSSrinivas Pandruvada 	else
13291aa7177cSPrarit Bhargava 		fn = clx_n_dump_pbf_config_for_cpu;
13301aa7177cSPrarit Bhargava 
13311aa7177cSPrarit Bhargava 	isst_ctdp_display_information_start(outf);
13321aa7177cSPrarit Bhargava 
13331aa7177cSPrarit Bhargava 	if (max_target_cpus)
13341aa7177cSPrarit Bhargava 		for_each_online_target_cpu_in_set(fn, NULL, NULL, NULL, NULL);
13351aa7177cSPrarit Bhargava 	else
13361aa7177cSPrarit Bhargava 		for_each_online_package_in_set(fn, NULL, NULL, NULL, NULL);
13371aa7177cSPrarit Bhargava 
13383fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
13393fb4f7cdSSrinivas Pandruvada }
13403fb4f7cdSSrinivas Pandruvada 
1341354bd06fSSrinivas Pandruvada static int set_clos_param(int cpu, int clos, int epp, int wt, int min, int max)
1342354bd06fSSrinivas Pandruvada {
1343354bd06fSSrinivas Pandruvada 	struct isst_clos_config clos_config;
1344354bd06fSSrinivas Pandruvada 	int ret;
1345354bd06fSSrinivas Pandruvada 
1346354bd06fSSrinivas Pandruvada 	ret = isst_pm_get_clos(cpu, clos, &clos_config);
1347354bd06fSSrinivas Pandruvada 	if (ret) {
1348354bd06fSSrinivas Pandruvada 		perror("isst_pm_get_clos");
1349354bd06fSSrinivas Pandruvada 		return ret;
1350354bd06fSSrinivas Pandruvada 	}
1351354bd06fSSrinivas Pandruvada 	clos_config.clos_min = min;
1352354bd06fSSrinivas Pandruvada 	clos_config.clos_max = max;
1353354bd06fSSrinivas Pandruvada 	clos_config.epp = epp;
1354354bd06fSSrinivas Pandruvada 	clos_config.clos_prop_prio = wt;
1355354bd06fSSrinivas Pandruvada 	ret = isst_set_clos(cpu, clos, &clos_config);
1356354bd06fSSrinivas Pandruvada 	if (ret) {
1357354bd06fSSrinivas Pandruvada 		perror("isst_pm_set_clos");
1358354bd06fSSrinivas Pandruvada 		return ret;
1359354bd06fSSrinivas Pandruvada 	}
1360354bd06fSSrinivas Pandruvada 
1361354bd06fSSrinivas Pandruvada 	return 0;
1362354bd06fSSrinivas Pandruvada }
1363354bd06fSSrinivas Pandruvada 
1364a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max(int cpu, int max, int freq)
1365a9b2f8e2SSrinivas Pandruvada {
1366a9b2f8e2SSrinivas Pandruvada 	char buffer[128], freq_str[16];
1367a9b2f8e2SSrinivas Pandruvada 	int fd, ret, len;
1368a9b2f8e2SSrinivas Pandruvada 
1369a9b2f8e2SSrinivas Pandruvada 	if (max)
1370a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1371a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1372a9b2f8e2SSrinivas Pandruvada 	else
1373a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1374a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1375a9b2f8e2SSrinivas Pandruvada 
1376a9b2f8e2SSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1377a9b2f8e2SSrinivas Pandruvada 	if (fd < 0)
1378a9b2f8e2SSrinivas Pandruvada 		return fd;
1379a9b2f8e2SSrinivas Pandruvada 
1380a9b2f8e2SSrinivas Pandruvada 	snprintf(freq_str, sizeof(freq_str), "%d", freq);
1381a9b2f8e2SSrinivas Pandruvada 	len = strlen(freq_str);
1382a9b2f8e2SSrinivas Pandruvada 	ret = write(fd, freq_str, len);
1383a9b2f8e2SSrinivas Pandruvada 	if (ret == -1) {
1384a9b2f8e2SSrinivas Pandruvada 		close(fd);
1385a9b2f8e2SSrinivas Pandruvada 		return ret;
1386a9b2f8e2SSrinivas Pandruvada 	}
1387a9b2f8e2SSrinivas Pandruvada 	close(fd);
1388a9b2f8e2SSrinivas Pandruvada 
1389a9b2f8e2SSrinivas Pandruvada 	return 0;
1390a9b2f8e2SSrinivas Pandruvada }
1391a9b2f8e2SSrinivas Pandruvada 
1392a9b2f8e2SSrinivas Pandruvada static int set_clx_pbf_cpufreq_scaling_min_max(int cpu)
1393a9b2f8e2SSrinivas Pandruvada {
1394a9b2f8e2SSrinivas Pandruvada 	struct isst_pkg_ctdp_level_info *ctdp_level;
1395a9b2f8e2SSrinivas Pandruvada 	struct isst_pbf_info *pbf_info;
1396a9b2f8e2SSrinivas Pandruvada 	int i, pkg_id, die_id, freq, freq_high, freq_low;
1397a9b2f8e2SSrinivas Pandruvada 	int ret;
1398a9b2f8e2SSrinivas Pandruvada 
1399a9b2f8e2SSrinivas Pandruvada 	ret = clx_n_config(cpu);
1400a9b2f8e2SSrinivas Pandruvada 	if (ret) {
1401a9b2f8e2SSrinivas Pandruvada 		perror("set_clx_pbf_cpufreq_scaling_min_max");
1402a9b2f8e2SSrinivas Pandruvada 		return ret;
1403a9b2f8e2SSrinivas Pandruvada 	}
1404a9b2f8e2SSrinivas Pandruvada 
1405a9b2f8e2SSrinivas Pandruvada 	ctdp_level = &clx_n_pkg_dev.ctdp_level[0];
1406a9b2f8e2SSrinivas Pandruvada 	pbf_info = &ctdp_level->pbf_info;
1407a9b2f8e2SSrinivas Pandruvada 	freq_high = pbf_info->p1_high * 100000;
1408a9b2f8e2SSrinivas Pandruvada 	freq_low = pbf_info->p1_low * 100000;
1409a9b2f8e2SSrinivas Pandruvada 
1410a9b2f8e2SSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1411a9b2f8e2SSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1412a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1413a9b2f8e2SSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1414a9b2f8e2SSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1415a9b2f8e2SSrinivas Pandruvada 			continue;
1416a9b2f8e2SSrinivas Pandruvada 
1417a9b2f8e2SSrinivas Pandruvada 		if (CPU_ISSET_S(i, pbf_info->core_cpumask_size,
1418a9b2f8e2SSrinivas Pandruvada 				  pbf_info->core_cpumask))
1419a9b2f8e2SSrinivas Pandruvada 			freq = freq_high;
1420a9b2f8e2SSrinivas Pandruvada 		else
1421a9b2f8e2SSrinivas Pandruvada 			freq = freq_low;
1422a9b2f8e2SSrinivas Pandruvada 
1423a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 1, freq);
1424a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max(i, 0, freq);
1425a9b2f8e2SSrinivas Pandruvada 	}
1426a9b2f8e2SSrinivas Pandruvada 
1427a9b2f8e2SSrinivas Pandruvada 	return 0;
1428a9b2f8e2SSrinivas Pandruvada }
1429a9b2f8e2SSrinivas Pandruvada 
1430a9b2f8e2SSrinivas Pandruvada static int set_cpufreq_scaling_min_max_from_cpuinfo(int cpu, int cpuinfo_max, int scaling_max)
1431354bd06fSSrinivas Pandruvada {
1432354bd06fSSrinivas Pandruvada 	char buffer[128], min_freq[16];
1433354bd06fSSrinivas Pandruvada 	int fd, ret, len;
1434354bd06fSSrinivas Pandruvada 
1435354bd06fSSrinivas Pandruvada 	if (!CPU_ISSET_S(cpu, present_cpumask_size, present_cpumask))
1436354bd06fSSrinivas Pandruvada 		return -1;
1437354bd06fSSrinivas Pandruvada 
1438a9b2f8e2SSrinivas Pandruvada 	if (cpuinfo_max)
1439354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1440354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
1441354bd06fSSrinivas Pandruvada 	else
1442354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1443354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_min_freq", cpu);
1444354bd06fSSrinivas Pandruvada 
1445354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_RDONLY);
1446354bd06fSSrinivas Pandruvada 	if (fd < 0)
1447354bd06fSSrinivas Pandruvada 		return fd;
1448354bd06fSSrinivas Pandruvada 
1449354bd06fSSrinivas Pandruvada 	len = read(fd, min_freq, sizeof(min_freq));
1450354bd06fSSrinivas Pandruvada 	close(fd);
1451354bd06fSSrinivas Pandruvada 
1452354bd06fSSrinivas Pandruvada 	if (len < 0)
1453354bd06fSSrinivas Pandruvada 		return len;
1454354bd06fSSrinivas Pandruvada 
1455a9b2f8e2SSrinivas Pandruvada 	if (scaling_max)
1456a9b2f8e2SSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1457a9b2f8e2SSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
1458a9b2f8e2SSrinivas Pandruvada 	else
1459354bd06fSSrinivas Pandruvada 		snprintf(buffer, sizeof(buffer),
1460354bd06fSSrinivas Pandruvada 			 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
1461354bd06fSSrinivas Pandruvada 
1462354bd06fSSrinivas Pandruvada 	fd = open(buffer, O_WRONLY);
1463354bd06fSSrinivas Pandruvada 	if (fd < 0)
1464354bd06fSSrinivas Pandruvada 		return fd;
1465354bd06fSSrinivas Pandruvada 
1466354bd06fSSrinivas Pandruvada 	len = strlen(min_freq);
1467354bd06fSSrinivas Pandruvada 	ret = write(fd, min_freq, len);
1468354bd06fSSrinivas Pandruvada 	if (ret == -1) {
1469354bd06fSSrinivas Pandruvada 		close(fd);
1470354bd06fSSrinivas Pandruvada 		return ret;
1471354bd06fSSrinivas Pandruvada 	}
1472354bd06fSSrinivas Pandruvada 	close(fd);
1473354bd06fSSrinivas Pandruvada 
1474354bd06fSSrinivas Pandruvada 	return 0;
1475354bd06fSSrinivas Pandruvada }
1476354bd06fSSrinivas Pandruvada 
1477354bd06fSSrinivas Pandruvada static void set_scaling_min_to_cpuinfo_max(int cpu)
1478354bd06fSSrinivas Pandruvada {
1479354bd06fSSrinivas Pandruvada 	int i, pkg_id, die_id;
1480354bd06fSSrinivas Pandruvada 
1481354bd06fSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1482354bd06fSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1483354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1484354bd06fSSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1485354bd06fSSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1486354bd06fSSrinivas Pandruvada 			continue;
1487354bd06fSSrinivas Pandruvada 
1488a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 0);
1489354bd06fSSrinivas Pandruvada 	}
1490354bd06fSSrinivas Pandruvada }
1491354bd06fSSrinivas Pandruvada 
1492354bd06fSSrinivas Pandruvada static void set_scaling_min_to_cpuinfo_min(int cpu)
1493354bd06fSSrinivas Pandruvada {
1494354bd06fSSrinivas Pandruvada 	int i, pkg_id, die_id;
1495354bd06fSSrinivas Pandruvada 
1496354bd06fSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1497354bd06fSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1498354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1499354bd06fSSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1500354bd06fSSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1501354bd06fSSrinivas Pandruvada 			continue;
1502354bd06fSSrinivas Pandruvada 
1503a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 0, 0);
1504a9b2f8e2SSrinivas Pandruvada 	}
1505a9b2f8e2SSrinivas Pandruvada }
1506a9b2f8e2SSrinivas Pandruvada 
1507a9b2f8e2SSrinivas Pandruvada static void set_scaling_max_to_cpuinfo_max(int cpu)
1508a9b2f8e2SSrinivas Pandruvada {
1509a9b2f8e2SSrinivas Pandruvada 	int i, pkg_id, die_id;
1510a9b2f8e2SSrinivas Pandruvada 
1511a9b2f8e2SSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1512a9b2f8e2SSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1513a9b2f8e2SSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1514a9b2f8e2SSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1515a9b2f8e2SSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1516a9b2f8e2SSrinivas Pandruvada 			continue;
1517a9b2f8e2SSrinivas Pandruvada 
1518a9b2f8e2SSrinivas Pandruvada 		set_cpufreq_scaling_min_max_from_cpuinfo(i, 1, 1);
1519354bd06fSSrinivas Pandruvada 	}
1520354bd06fSSrinivas Pandruvada }
1521354bd06fSSrinivas Pandruvada 
1522354bd06fSSrinivas Pandruvada static int set_core_priority_and_min(int cpu, int mask_size,
1523354bd06fSSrinivas Pandruvada 				     cpu_set_t *cpu_mask, int min_high,
1524354bd06fSSrinivas Pandruvada 				     int min_low)
1525354bd06fSSrinivas Pandruvada {
1526354bd06fSSrinivas Pandruvada 	int pkg_id, die_id, ret, i;
1527354bd06fSSrinivas Pandruvada 
1528354bd06fSSrinivas Pandruvada 	if (!CPU_COUNT_S(mask_size, cpu_mask))
1529354bd06fSSrinivas Pandruvada 		return -1;
1530354bd06fSSrinivas Pandruvada 
1531354bd06fSSrinivas Pandruvada 	ret = set_clos_param(cpu, 0, 0, 0, min_high, 0xff);
1532354bd06fSSrinivas Pandruvada 	if (ret)
1533354bd06fSSrinivas Pandruvada 		return ret;
1534354bd06fSSrinivas Pandruvada 
153521c3390dSSrinivas Pandruvada 	ret = set_clos_param(cpu, 1, 15, 15, min_low, 0xff);
1536354bd06fSSrinivas Pandruvada 	if (ret)
1537354bd06fSSrinivas Pandruvada 		return ret;
1538354bd06fSSrinivas Pandruvada 
153921c3390dSSrinivas Pandruvada 	ret = set_clos_param(cpu, 2, 15, 15, min_low, 0xff);
1540354bd06fSSrinivas Pandruvada 	if (ret)
1541354bd06fSSrinivas Pandruvada 		return ret;
1542354bd06fSSrinivas Pandruvada 
154321c3390dSSrinivas Pandruvada 	ret = set_clos_param(cpu, 3, 15, 15, min_low, 0xff);
1544354bd06fSSrinivas Pandruvada 	if (ret)
1545354bd06fSSrinivas Pandruvada 		return ret;
1546354bd06fSSrinivas Pandruvada 
1547354bd06fSSrinivas Pandruvada 	pkg_id = get_physical_package_id(cpu);
1548354bd06fSSrinivas Pandruvada 	die_id = get_physical_die_id(cpu);
1549354bd06fSSrinivas Pandruvada 	for (i = 0; i < get_topo_max_cpus(); ++i) {
1550354bd06fSSrinivas Pandruvada 		int clos;
1551354bd06fSSrinivas Pandruvada 
1552354bd06fSSrinivas Pandruvada 		if (pkg_id != get_physical_package_id(i) ||
1553354bd06fSSrinivas Pandruvada 		    die_id != get_physical_die_id(i))
1554354bd06fSSrinivas Pandruvada 			continue;
1555354bd06fSSrinivas Pandruvada 
1556354bd06fSSrinivas Pandruvada 		if (CPU_ISSET_S(i, mask_size, cpu_mask))
1557354bd06fSSrinivas Pandruvada 			clos = 0;
1558354bd06fSSrinivas Pandruvada 		else
1559354bd06fSSrinivas Pandruvada 			clos = 3;
1560354bd06fSSrinivas Pandruvada 
1561354bd06fSSrinivas Pandruvada 		debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1562354bd06fSSrinivas Pandruvada 		ret = isst_clos_associate(i, clos);
1563354bd06fSSrinivas Pandruvada 		if (ret) {
1564354bd06fSSrinivas Pandruvada 			perror("isst_clos_associate");
1565354bd06fSSrinivas Pandruvada 			return ret;
1566354bd06fSSrinivas Pandruvada 		}
1567354bd06fSSrinivas Pandruvada 	}
1568354bd06fSSrinivas Pandruvada 
1569354bd06fSSrinivas Pandruvada 	return 0;
1570354bd06fSSrinivas Pandruvada }
1571354bd06fSSrinivas Pandruvada 
1572354bd06fSSrinivas Pandruvada static int set_pbf_core_power(int cpu)
1573354bd06fSSrinivas Pandruvada {
1574354bd06fSSrinivas Pandruvada 	struct isst_pbf_info pbf_info;
1575354bd06fSSrinivas Pandruvada 	struct isst_pkg_ctdp pkg_dev;
1576354bd06fSSrinivas Pandruvada 	int ret;
1577354bd06fSSrinivas Pandruvada 
1578354bd06fSSrinivas Pandruvada 	ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1579354bd06fSSrinivas Pandruvada 	if (ret) {
1580354bd06fSSrinivas Pandruvada 		perror("isst_get_ctdp_levels");
1581354bd06fSSrinivas Pandruvada 		return ret;
1582354bd06fSSrinivas Pandruvada 	}
1583354bd06fSSrinivas Pandruvada 	debug_printf("Current_level: %d\n", pkg_dev.current_level);
1584354bd06fSSrinivas Pandruvada 
1585354bd06fSSrinivas Pandruvada 	ret = isst_get_pbf_info(cpu, pkg_dev.current_level, &pbf_info);
1586354bd06fSSrinivas Pandruvada 	if (ret) {
1587354bd06fSSrinivas Pandruvada 		perror("isst_get_pbf_info");
1588354bd06fSSrinivas Pandruvada 		return ret;
1589354bd06fSSrinivas Pandruvada 	}
1590354bd06fSSrinivas Pandruvada 	debug_printf("p1_high: %d p1_low: %d\n", pbf_info.p1_high,
1591354bd06fSSrinivas Pandruvada 		     pbf_info.p1_low);
1592354bd06fSSrinivas Pandruvada 
1593354bd06fSSrinivas Pandruvada 	ret = set_core_priority_and_min(cpu, pbf_info.core_cpumask_size,
1594354bd06fSSrinivas Pandruvada 					pbf_info.core_cpumask,
1595354bd06fSSrinivas Pandruvada 					pbf_info.p1_high, pbf_info.p1_low);
1596354bd06fSSrinivas Pandruvada 	if (ret) {
1597354bd06fSSrinivas Pandruvada 		perror("set_core_priority_and_min");
1598354bd06fSSrinivas Pandruvada 		return ret;
1599354bd06fSSrinivas Pandruvada 	}
1600354bd06fSSrinivas Pandruvada 
1601354bd06fSSrinivas Pandruvada 	ret = isst_pm_qos_config(cpu, 1, 1);
1602354bd06fSSrinivas Pandruvada 	if (ret) {
1603354bd06fSSrinivas Pandruvada 		perror("isst_pm_qos_config");
1604354bd06fSSrinivas Pandruvada 		return ret;
1605354bd06fSSrinivas Pandruvada 	}
1606354bd06fSSrinivas Pandruvada 
1607354bd06fSSrinivas Pandruvada 	return 0;
1608354bd06fSSrinivas Pandruvada }
1609354bd06fSSrinivas Pandruvada 
16103fb4f7cdSSrinivas Pandruvada static void set_pbf_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
16113fb4f7cdSSrinivas Pandruvada 			    void *arg4)
16123fb4f7cdSSrinivas Pandruvada {
16133fb4f7cdSSrinivas Pandruvada 	int ret;
16143fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
16153fb4f7cdSSrinivas Pandruvada 
16161aa7177cSPrarit Bhargava 	if (is_clx_n_platform()) {
1617a9b2f8e2SSrinivas Pandruvada 		if (status) {
16181aa7177cSPrarit Bhargava 			ret = 0;
16191aa7177cSPrarit Bhargava 			if (auto_mode)
1620a9b2f8e2SSrinivas Pandruvada 				set_clx_pbf_cpufreq_scaling_min_max(cpu);
1621a9b2f8e2SSrinivas Pandruvada 
1622a9b2f8e2SSrinivas Pandruvada 		} else {
1623a9b2f8e2SSrinivas Pandruvada 			ret = -1;
1624a9b2f8e2SSrinivas Pandruvada 			if (auto_mode) {
1625a9b2f8e2SSrinivas Pandruvada 				set_scaling_max_to_cpuinfo_max(cpu);
1626a9b2f8e2SSrinivas Pandruvada 				set_scaling_min_to_cpuinfo_min(cpu);
1627a9b2f8e2SSrinivas Pandruvada 			}
16281aa7177cSPrarit Bhargava 		}
16291aa7177cSPrarit Bhargava 		goto disp_result;
16301aa7177cSPrarit Bhargava 	}
16311aa7177cSPrarit Bhargava 
1632097a5222SSrinivas Pandruvada 	if (auto_mode && status) {
1633354bd06fSSrinivas Pandruvada 		ret = set_pbf_core_power(cpu);
1634354bd06fSSrinivas Pandruvada 		if (ret)
1635354bd06fSSrinivas Pandruvada 			goto disp_result;
1636354bd06fSSrinivas Pandruvada 	}
1637354bd06fSSrinivas Pandruvada 
16383fb4f7cdSSrinivas Pandruvada 	ret = isst_set_pbf_fact_status(cpu, 1, status);
16393fb4f7cdSSrinivas Pandruvada 	if (ret) {
164039bae0fcSSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1641354bd06fSSrinivas Pandruvada 		if (auto_mode)
1642354bd06fSSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
16433fb4f7cdSSrinivas Pandruvada 	} else {
1644354bd06fSSrinivas Pandruvada 		if (auto_mode) {
1645354bd06fSSrinivas Pandruvada 			if (status)
1646354bd06fSSrinivas Pandruvada 				set_scaling_min_to_cpuinfo_max(cpu);
1647354bd06fSSrinivas Pandruvada 			else
1648354bd06fSSrinivas Pandruvada 				set_scaling_min_to_cpuinfo_min(cpu);
1649354bd06fSSrinivas Pandruvada 		}
1650354bd06fSSrinivas Pandruvada 	}
1651354bd06fSSrinivas Pandruvada 
1652097a5222SSrinivas Pandruvada 	if (auto_mode && !status)
1653097a5222SSrinivas Pandruvada 		isst_pm_qos_config(cpu, 0, 0);
1654097a5222SSrinivas Pandruvada 
1655354bd06fSSrinivas Pandruvada disp_result:
16563fb4f7cdSSrinivas Pandruvada 	if (status)
16573fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "base-freq", "enable",
16583fb4f7cdSSrinivas Pandruvada 				    ret);
16593fb4f7cdSSrinivas Pandruvada 	else
16603fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "base-freq", "disable",
16613fb4f7cdSSrinivas Pandruvada 				    ret);
16623fb4f7cdSSrinivas Pandruvada }
16633fb4f7cdSSrinivas Pandruvada 
1664ce1326a2SPrarit Bhargava static void set_pbf_enable(int arg)
16653fb4f7cdSSrinivas Pandruvada {
1666ce1326a2SPrarit Bhargava 	int enable = arg;
16673fb4f7cdSSrinivas Pandruvada 
16683fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1669ce1326a2SPrarit Bhargava 		if (enable) {
16703fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1671354bd06fSSrinivas Pandruvada 				"Enable Intel Speed Select Technology base frequency feature\n");
167239bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
167339bae0fcSSrinivas Pandruvada 				fprintf(stderr,
167439bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't enable feature in the hardware.\n");
167539bae0fcSSrinivas Pandruvada 				fprintf(stderr,
167639bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpufreq base_frequency.\n");
167739bae0fcSSrinivas Pandruvada 				exit(0);
167839bae0fcSSrinivas Pandruvada 
167939bae0fcSSrinivas Pandruvada 			}
1680354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1681354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Use priority of cores to set core-power associations\n");
1682ce1326a2SPrarit Bhargava 		} else {
1683354bd06fSSrinivas Pandruvada 
168439bae0fcSSrinivas Pandruvada 			if (is_clx_n_platform()) {
168539bae0fcSSrinivas Pandruvada 				fprintf(stderr,
168639bae0fcSSrinivas Pandruvada 					"\tOn this platform this command doesn't disable feature in the hardware.\n");
168739bae0fcSSrinivas Pandruvada 				fprintf(stderr,
168839bae0fcSSrinivas Pandruvada 					"\tIt updates the cpufreq scaling_min_freq to match cpuinfo_min_freq\n");
168939bae0fcSSrinivas Pandruvada 				exit(0);
169039bae0fcSSrinivas Pandruvada 			}
16913fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
1692354bd06fSSrinivas Pandruvada 				"Disable Intel Speed Select Technology base frequency feature\n");
1693354bd06fSSrinivas Pandruvada 			fprintf(stderr,
1694354bd06fSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1695ce1326a2SPrarit Bhargava 		}
16963fb4f7cdSSrinivas Pandruvada 		exit(0);
16973fb4f7cdSSrinivas Pandruvada 	}
16983fb4f7cdSSrinivas Pandruvada 
16993fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
17003fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
17013fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_pbf_for_cpu, NULL, NULL,
1702ce1326a2SPrarit Bhargava 						  NULL, &enable);
17033fb4f7cdSSrinivas Pandruvada 	else
17043fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_pbf_for_cpu, NULL, NULL,
1705ce1326a2SPrarit Bhargava 					       NULL, &enable);
17063fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
17073fb4f7cdSSrinivas Pandruvada }
17083fb4f7cdSSrinivas Pandruvada 
17093fb4f7cdSSrinivas Pandruvada static void dump_fact_config_for_cpu(int cpu, void *arg1, void *arg2,
17103fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
17113fb4f7cdSSrinivas Pandruvada {
17123fb4f7cdSSrinivas Pandruvada 	struct isst_fact_info fact_info;
17133fb4f7cdSSrinivas Pandruvada 	int ret;
17143fb4f7cdSSrinivas Pandruvada 
1715*a9fd6ae7SSrinivas Pandruvada 	ret = isst_get_fact_info(cpu, tdp_level, fact_bucket, &fact_info);
1716*a9fd6ae7SSrinivas Pandruvada 	if (ret) {
1717*a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Failed to get turbo-freq info at this level", 1, tdp_level);
1718*a9fd6ae7SSrinivas Pandruvada 		isst_ctdp_display_information_end(outf);
1719*a9fd6ae7SSrinivas Pandruvada 		exit(1);
1720*a9fd6ae7SSrinivas Pandruvada 	} else {
17213fb4f7cdSSrinivas Pandruvada 		isst_fact_display_information(cpu, outf, tdp_level, fact_bucket,
17223fb4f7cdSSrinivas Pandruvada 					      fact_avx, &fact_info);
17233fb4f7cdSSrinivas Pandruvada 	}
1724*a9fd6ae7SSrinivas Pandruvada }
17253fb4f7cdSSrinivas Pandruvada 
1726ce1326a2SPrarit Bhargava static void dump_fact_config(int arg)
17273fb4f7cdSSrinivas Pandruvada {
17283fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
17293fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
17303fb4f7cdSSrinivas Pandruvada 			"Print complete Intel Speed Select Technology turbo frequency configuration for a TDP level. Other arguments are optional.\n");
17313fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
17323fb4f7cdSSrinivas Pandruvada 			"\tArguments: -l|--level : Specify tdp level\n");
17333fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
17343fb4f7cdSSrinivas Pandruvada 			"\tArguments: -b|--bucket : Bucket index to dump\n");
17353fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
17363fb4f7cdSSrinivas Pandruvada 			"\tArguments: -r|--trl-type : Specify trl type: sse|avx2|avx512\n");
17373fb4f7cdSSrinivas Pandruvada 		exit(0);
17383fb4f7cdSSrinivas Pandruvada 	}
17393fb4f7cdSSrinivas Pandruvada 
17403fb4f7cdSSrinivas Pandruvada 	if (tdp_level == 0xff) {
1741*a9fd6ae7SSrinivas Pandruvada 		isst_display_error_info_message(1, "Invalid command: specify tdp_level\n", 0, 0);
17423fb4f7cdSSrinivas Pandruvada 		exit(1);
17433fb4f7cdSSrinivas Pandruvada 	}
17443fb4f7cdSSrinivas Pandruvada 
17453fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
17463fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
17473fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_fact_config_for_cpu,
17483fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
17493fb4f7cdSSrinivas Pandruvada 	else
17503fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_fact_config_for_cpu, NULL,
17513fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
17523fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
17533fb4f7cdSSrinivas Pandruvada }
17543fb4f7cdSSrinivas Pandruvada 
17553fb4f7cdSSrinivas Pandruvada static void set_fact_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
17563fb4f7cdSSrinivas Pandruvada 			     void *arg4)
17573fb4f7cdSSrinivas Pandruvada {
17583fb4f7cdSSrinivas Pandruvada 	int ret;
17593fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
17603fb4f7cdSSrinivas Pandruvada 
1761097a5222SSrinivas Pandruvada 	if (auto_mode && status) {
1762a6a82f9bSSrinivas Pandruvada 		ret = isst_pm_qos_config(cpu, 1, 1);
17633fb4f7cdSSrinivas Pandruvada 		if (ret)
1764a6a82f9bSSrinivas Pandruvada 			goto disp_results;
1765a6a82f9bSSrinivas Pandruvada 	}
1766a6a82f9bSSrinivas Pandruvada 
1767a6a82f9bSSrinivas Pandruvada 	ret = isst_set_pbf_fact_status(cpu, 0, status);
1768a6a82f9bSSrinivas Pandruvada 	if (ret) {
1769*a9fd6ae7SSrinivas Pandruvada 		debug_printf("isst_set_pbf_fact_status failed");
1770a6a82f9bSSrinivas Pandruvada 		if (auto_mode)
1771a6a82f9bSSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
1772a6a82f9bSSrinivas Pandruvada 
1773a6a82f9bSSrinivas Pandruvada 		goto disp_results;
1774a6a82f9bSSrinivas Pandruvada 	}
1775a6a82f9bSSrinivas Pandruvada 
1776a6a82f9bSSrinivas Pandruvada 	/* Set TRL */
17773fb4f7cdSSrinivas Pandruvada 	if (status) {
17783fb4f7cdSSrinivas Pandruvada 		struct isst_pkg_ctdp pkg_dev;
17793fb4f7cdSSrinivas Pandruvada 
17803fb4f7cdSSrinivas Pandruvada 		ret = isst_get_ctdp_levels(cpu, &pkg_dev);
1781a6a82f9bSSrinivas Pandruvada 		if (!ret)
17823fb4f7cdSSrinivas Pandruvada 			ret = isst_set_trl(cpu, fact_trl);
1783a6a82f9bSSrinivas Pandruvada 		if (ret && auto_mode)
1784a6a82f9bSSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
1785097a5222SSrinivas Pandruvada 	} else {
1786097a5222SSrinivas Pandruvada 		if (auto_mode)
1787097a5222SSrinivas Pandruvada 			isst_pm_qos_config(cpu, 0, 0);
1788a6a82f9bSSrinivas Pandruvada 	}
1789a6a82f9bSSrinivas Pandruvada 
1790a6a82f9bSSrinivas Pandruvada disp_results:
1791a6a82f9bSSrinivas Pandruvada 	if (status) {
1792a6a82f9bSSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-freq", "enable", ret);
179314a8aa49SSrinivas Pandruvada 		if (ret)
179414a8aa49SSrinivas Pandruvada 			fact_enable_fail = ret;
17953fb4f7cdSSrinivas Pandruvada 	} else {
17963fb4f7cdSSrinivas Pandruvada 		/* Since we modified TRL during Fact enable, restore it */
17973fb4f7cdSSrinivas Pandruvada 		isst_set_trl_from_current_tdp(cpu, fact_trl);
1798a6a82f9bSSrinivas Pandruvada 		isst_display_result(cpu, outf, "turbo-freq", "disable", ret);
17993fb4f7cdSSrinivas Pandruvada 	}
18003fb4f7cdSSrinivas Pandruvada }
18013fb4f7cdSSrinivas Pandruvada 
1802ce1326a2SPrarit Bhargava static void set_fact_enable(int arg)
18033fb4f7cdSSrinivas Pandruvada {
1804ce1326a2SPrarit Bhargava 	int i, ret, enable = arg;
18053fb4f7cdSSrinivas Pandruvada 
18063fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1807ce1326a2SPrarit Bhargava 		if (enable) {
18083fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
18093fb4f7cdSSrinivas Pandruvada 				"Enable Intel Speed Select Technology Turbo frequency feature\n");
18103fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
18113fb4f7cdSSrinivas Pandruvada 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1812a6a82f9bSSrinivas Pandruvada 			fprintf(stderr,
1813a6a82f9bSSrinivas Pandruvada 				"\tOptional Arguments: -a|--auto : Designate specified target CPUs with");
1814ce1326a2SPrarit Bhargava 			fprintf(stderr,
1815ce1326a2SPrarit Bhargava 				"-C|--cpu option as as high priority using core-power feature\n");
1816ce1326a2SPrarit Bhargava 		} else {
1817ce1326a2SPrarit Bhargava 			fprintf(stderr,
1818ce1326a2SPrarit Bhargava 				"Disable Intel Speed Select Technology turbo frequency feature\n");
1819ce1326a2SPrarit Bhargava 			fprintf(stderr,
1820ce1326a2SPrarit Bhargava 				"Optional: -t|--trl : Specify turbo ratio limit\n");
1821ce1326a2SPrarit Bhargava 			fprintf(stderr,
1822ce1326a2SPrarit Bhargava 				"\tOptional Arguments: -a|--auto : Also disable core-power associations\n");
1823ce1326a2SPrarit Bhargava 		}
18243fb4f7cdSSrinivas Pandruvada 		exit(0);
18253fb4f7cdSSrinivas Pandruvada 	}
18263fb4f7cdSSrinivas Pandruvada 
18273fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
18283fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
18293fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_fact_for_cpu, NULL, NULL,
1830ce1326a2SPrarit Bhargava 						  NULL, &enable);
18313fb4f7cdSSrinivas Pandruvada 	else
18323fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_fact_for_cpu, NULL, NULL,
1833ce1326a2SPrarit Bhargava 					       NULL, &enable);
18343fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
1835a6a82f9bSSrinivas Pandruvada 
183614a8aa49SSrinivas Pandruvada 	if (!fact_enable_fail && enable && auto_mode) {
1837a6a82f9bSSrinivas Pandruvada 		/*
1838a6a82f9bSSrinivas Pandruvada 		 * When we adjust CLOS param, we have to set for siblings also.
1839a6a82f9bSSrinivas Pandruvada 		 * So for the each user specified CPU, also add the sibling
1840a6a82f9bSSrinivas Pandruvada 		 * in the present_cpu_mask.
1841a6a82f9bSSrinivas Pandruvada 		 */
1842a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
1843a6a82f9bSSrinivas Pandruvada 			char buffer[128], sibling_list[128], *cpu_str;
1844a6a82f9bSSrinivas Pandruvada 			int fd, len;
1845a6a82f9bSSrinivas Pandruvada 
1846a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1847a6a82f9bSSrinivas Pandruvada 				continue;
1848a6a82f9bSSrinivas Pandruvada 
1849a6a82f9bSSrinivas Pandruvada 			snprintf(buffer, sizeof(buffer),
1850a6a82f9bSSrinivas Pandruvada 				 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1851a6a82f9bSSrinivas Pandruvada 
1852a6a82f9bSSrinivas Pandruvada 			fd = open(buffer, O_RDONLY);
1853a6a82f9bSSrinivas Pandruvada 			if (fd < 0)
1854a6a82f9bSSrinivas Pandruvada 				continue;
1855a6a82f9bSSrinivas Pandruvada 
1856a6a82f9bSSrinivas Pandruvada 			len = read(fd, sibling_list, sizeof(sibling_list));
1857a6a82f9bSSrinivas Pandruvada 			close(fd);
1858a6a82f9bSSrinivas Pandruvada 
1859a6a82f9bSSrinivas Pandruvada 			if (len < 0)
1860a6a82f9bSSrinivas Pandruvada 				continue;
1861a6a82f9bSSrinivas Pandruvada 
1862a6a82f9bSSrinivas Pandruvada 			cpu_str = strtok(sibling_list, ",");
1863a6a82f9bSSrinivas Pandruvada 			while (cpu_str != NULL) {
1864a6a82f9bSSrinivas Pandruvada 				int cpu;
1865a6a82f9bSSrinivas Pandruvada 
1866a6a82f9bSSrinivas Pandruvada 				sscanf(cpu_str, "%d", &cpu);
1867a6a82f9bSSrinivas Pandruvada 				CPU_SET_S(cpu, target_cpumask_size, target_cpumask);
1868a6a82f9bSSrinivas Pandruvada 				cpu_str = strtok(NULL, ",");
1869a6a82f9bSSrinivas Pandruvada 			}
1870a6a82f9bSSrinivas Pandruvada 		}
1871a6a82f9bSSrinivas Pandruvada 
1872a6a82f9bSSrinivas Pandruvada 		for (i = 0; i < get_topo_max_cpus(); ++i) {
1873a6a82f9bSSrinivas Pandruvada 			int clos;
1874a6a82f9bSSrinivas Pandruvada 
1875a6a82f9bSSrinivas Pandruvada 			if (!CPU_ISSET_S(i, present_cpumask_size, present_cpumask))
1876a6a82f9bSSrinivas Pandruvada 				continue;
1877a6a82f9bSSrinivas Pandruvada 
1878a6a82f9bSSrinivas Pandruvada 			ret = set_clos_param(i, 0, 0, 0, 0, 0xff);
1879a6a82f9bSSrinivas Pandruvada 			if (ret)
1880a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1881a6a82f9bSSrinivas Pandruvada 
188221c3390dSSrinivas Pandruvada 			ret = set_clos_param(i, 1, 15, 15, 0, 0xff);
1883a6a82f9bSSrinivas Pandruvada 			if (ret)
1884a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1885a6a82f9bSSrinivas Pandruvada 
188621c3390dSSrinivas Pandruvada 			ret = set_clos_param(i, 2, 15, 15, 0, 0xff);
1887a6a82f9bSSrinivas Pandruvada 			if (ret)
1888a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1889a6a82f9bSSrinivas Pandruvada 
189021c3390dSSrinivas Pandruvada 			ret = set_clos_param(i, 3, 15, 15, 0, 0xff);
1891a6a82f9bSSrinivas Pandruvada 			if (ret)
1892a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1893a6a82f9bSSrinivas Pandruvada 
1894a6a82f9bSSrinivas Pandruvada 			if (CPU_ISSET_S(i, target_cpumask_size, target_cpumask))
1895a6a82f9bSSrinivas Pandruvada 				clos = 0;
1896a6a82f9bSSrinivas Pandruvada 			else
1897a6a82f9bSSrinivas Pandruvada 				clos = 3;
1898a6a82f9bSSrinivas Pandruvada 
1899a6a82f9bSSrinivas Pandruvada 			debug_printf("Associate cpu: %d clos: %d\n", i, clos);
1900a6a82f9bSSrinivas Pandruvada 			ret = isst_clos_associate(i, clos);
1901a6a82f9bSSrinivas Pandruvada 			if (ret)
1902a6a82f9bSSrinivas Pandruvada 				goto error_disp;
1903a6a82f9bSSrinivas Pandruvada 		}
190478e77b7dSSrinivas Pandruvada 		isst_display_result(-1, outf, "turbo-freq --auto", "enable", 0);
1905a6a82f9bSSrinivas Pandruvada 	}
1906a6a82f9bSSrinivas Pandruvada 
1907a6a82f9bSSrinivas Pandruvada 	return;
1908a6a82f9bSSrinivas Pandruvada 
1909a6a82f9bSSrinivas Pandruvada error_disp:
1910a6a82f9bSSrinivas Pandruvada 	isst_display_result(i, outf, "turbo-freq --auto", "enable", ret);
1911a6a82f9bSSrinivas Pandruvada 
19123fb4f7cdSSrinivas Pandruvada }
19133fb4f7cdSSrinivas Pandruvada 
19143fb4f7cdSSrinivas Pandruvada static void enable_clos_qos_config(int cpu, void *arg1, void *arg2, void *arg3,
19153fb4f7cdSSrinivas Pandruvada 				   void *arg4)
19163fb4f7cdSSrinivas Pandruvada {
19173fb4f7cdSSrinivas Pandruvada 	int ret;
19183fb4f7cdSSrinivas Pandruvada 	int status = *(int *)arg4;
19193fb4f7cdSSrinivas Pandruvada 
19203fb4f7cdSSrinivas Pandruvada 	ret = isst_pm_qos_config(cpu, status, clos_priority_type);
1921a6a82f9bSSrinivas Pandruvada 	if (ret)
19223fb4f7cdSSrinivas Pandruvada 		perror("isst_pm_qos_config");
1923a6a82f9bSSrinivas Pandruvada 
19243fb4f7cdSSrinivas Pandruvada 	if (status)
19253fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "enable",
19263fb4f7cdSSrinivas Pandruvada 				    ret);
19273fb4f7cdSSrinivas Pandruvada 	else
19283fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "disable",
19293fb4f7cdSSrinivas Pandruvada 				    ret);
19303fb4f7cdSSrinivas Pandruvada }
19313fb4f7cdSSrinivas Pandruvada 
1932ce1326a2SPrarit Bhargava static void set_clos_enable(int arg)
19333fb4f7cdSSrinivas Pandruvada {
1934ce1326a2SPrarit Bhargava 	int enable = arg;
19353fb4f7cdSSrinivas Pandruvada 
19363fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
1937ce1326a2SPrarit Bhargava 		if (enable) {
1938ce1326a2SPrarit Bhargava 			fprintf(stderr,
1939ce1326a2SPrarit Bhargava 				"Enable core-power for a package/die\n");
19403fb4f7cdSSrinivas Pandruvada 			fprintf(stderr,
19413fb4f7cdSSrinivas Pandruvada 				"\tClos Enable: Specify priority type with [--priority|-p]\n");
19423fb4f7cdSSrinivas Pandruvada 			fprintf(stderr, "\t\t 0: Proportional, 1: Ordered\n");
1943ce1326a2SPrarit Bhargava 		} else {
1944ce1326a2SPrarit Bhargava 			fprintf(stderr,
1945ce1326a2SPrarit Bhargava 				"Disable core-power: [No command arguments are required]\n");
1946ce1326a2SPrarit Bhargava 		}
19473fb4f7cdSSrinivas Pandruvada 		exit(0);
19483fb4f7cdSSrinivas Pandruvada 	}
19493fb4f7cdSSrinivas Pandruvada 
1950ce1326a2SPrarit Bhargava 	if (enable && cpufreq_sysfs_present()) {
19513fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
19523fb4f7cdSSrinivas Pandruvada 			"cpufreq subsystem and core-power enable will interfere with each other!\n");
19533fb4f7cdSSrinivas Pandruvada 	}
19543fb4f7cdSSrinivas Pandruvada 
19553fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
19563fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
19573fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(enable_clos_qos_config, NULL,
1958ce1326a2SPrarit Bhargava 						  NULL, NULL, &enable);
19593fb4f7cdSSrinivas Pandruvada 	else
19603fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(enable_clos_qos_config, NULL,
1961ce1326a2SPrarit Bhargava 					       NULL, NULL, &enable);
19623fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
19633fb4f7cdSSrinivas Pandruvada }
19643fb4f7cdSSrinivas Pandruvada 
19653fb4f7cdSSrinivas Pandruvada static void dump_clos_config_for_cpu(int cpu, void *arg1, void *arg2,
19663fb4f7cdSSrinivas Pandruvada 				     void *arg3, void *arg4)
19673fb4f7cdSSrinivas Pandruvada {
19683fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
19693fb4f7cdSSrinivas Pandruvada 	int ret;
19703fb4f7cdSSrinivas Pandruvada 
19713fb4f7cdSSrinivas Pandruvada 	ret = isst_pm_get_clos(cpu, current_clos, &clos_config);
19723fb4f7cdSSrinivas Pandruvada 	if (ret)
19733fb4f7cdSSrinivas Pandruvada 		perror("isst_pm_get_clos");
19743fb4f7cdSSrinivas Pandruvada 	else
19753fb4f7cdSSrinivas Pandruvada 		isst_clos_display_information(cpu, outf, current_clos,
19763fb4f7cdSSrinivas Pandruvada 					      &clos_config);
19773fb4f7cdSSrinivas Pandruvada }
19783fb4f7cdSSrinivas Pandruvada 
1979ce1326a2SPrarit Bhargava static void dump_clos_config(int arg)
19803fb4f7cdSSrinivas Pandruvada {
19813fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
19823fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
19833fb4f7cdSSrinivas Pandruvada 			"Print Intel Speed Select Technology core power configuration\n");
19843fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
19853fb4f7cdSSrinivas Pandruvada 			"\tArguments: [-c | --clos]: Specify clos id\n");
19863fb4f7cdSSrinivas Pandruvada 		exit(0);
19873fb4f7cdSSrinivas Pandruvada 	}
19883fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
19893fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Invalid clos id\n");
19903fb4f7cdSSrinivas Pandruvada 		exit(0);
19913fb4f7cdSSrinivas Pandruvada 	}
19923fb4f7cdSSrinivas Pandruvada 
19933fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
19943fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
19953fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(dump_clos_config_for_cpu,
19963fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL, NULL);
19973fb4f7cdSSrinivas Pandruvada 	else
19983fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(dump_clos_config_for_cpu, NULL,
19993fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
20003fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
20013fb4f7cdSSrinivas Pandruvada }
20023fb4f7cdSSrinivas Pandruvada 
2003188afed9SSrinivas Pandruvada static void get_clos_info_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
2004188afed9SSrinivas Pandruvada 				  void *arg4)
2005188afed9SSrinivas Pandruvada {
2006188afed9SSrinivas Pandruvada 	int enable, ret, prio_type;
2007188afed9SSrinivas Pandruvada 
2008188afed9SSrinivas Pandruvada 	ret = isst_clos_get_clos_information(cpu, &enable, &prio_type);
2009188afed9SSrinivas Pandruvada 	if (ret)
2010188afed9SSrinivas Pandruvada 		perror("isst_clos_get_info");
2011143ad322SSrinivas Pandruvada 	else {
2012143ad322SSrinivas Pandruvada 		int cp_state, cp_cap;
2013143ad322SSrinivas Pandruvada 
2014143ad322SSrinivas Pandruvada 		isst_read_pm_config(cpu, &cp_state, &cp_cap);
2015143ad322SSrinivas Pandruvada 		isst_clos_display_clos_information(cpu, outf, enable, prio_type,
2016143ad322SSrinivas Pandruvada 						   cp_state, cp_cap);
2017143ad322SSrinivas Pandruvada 	}
2018188afed9SSrinivas Pandruvada }
2019188afed9SSrinivas Pandruvada 
2020ce1326a2SPrarit Bhargava static void dump_clos_info(int arg)
2021188afed9SSrinivas Pandruvada {
2022188afed9SSrinivas Pandruvada 	if (cmd_help) {
2023188afed9SSrinivas Pandruvada 		fprintf(stderr,
2024188afed9SSrinivas Pandruvada 			"Print Intel Speed Select Technology core power information\n");
2025f5205f49SSrinivas Pandruvada 		fprintf(stderr, "\t Optionally specify targeted cpu id with [--cpu|-c]\n");
2026188afed9SSrinivas Pandruvada 		exit(0);
2027188afed9SSrinivas Pandruvada 	}
2028188afed9SSrinivas Pandruvada 
2029188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2030f5205f49SSrinivas Pandruvada 	if (max_target_cpus)
2031188afed9SSrinivas Pandruvada 		for_each_online_target_cpu_in_set(get_clos_info_for_cpu, NULL,
2032188afed9SSrinivas Pandruvada 						  NULL, NULL, NULL);
2033f5205f49SSrinivas Pandruvada 	else
2034f5205f49SSrinivas Pandruvada 		for_each_online_package_in_set(get_clos_info_for_cpu, NULL,
2035f5205f49SSrinivas Pandruvada 					       NULL, NULL, NULL);
2036188afed9SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
2037188afed9SSrinivas Pandruvada 
2038188afed9SSrinivas Pandruvada }
2039188afed9SSrinivas Pandruvada 
20403fb4f7cdSSrinivas Pandruvada static void set_clos_config_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
20413fb4f7cdSSrinivas Pandruvada 				    void *arg4)
20423fb4f7cdSSrinivas Pandruvada {
20433fb4f7cdSSrinivas Pandruvada 	struct isst_clos_config clos_config;
20443fb4f7cdSSrinivas Pandruvada 	int ret;
20453fb4f7cdSSrinivas Pandruvada 
20463fb4f7cdSSrinivas Pandruvada 	clos_config.pkg_id = get_physical_package_id(cpu);
20473fb4f7cdSSrinivas Pandruvada 	clos_config.die_id = get_physical_die_id(cpu);
20483fb4f7cdSSrinivas Pandruvada 
20493fb4f7cdSSrinivas Pandruvada 	clos_config.epp = clos_epp;
20503fb4f7cdSSrinivas Pandruvada 	clos_config.clos_prop_prio = clos_prop_prio;
20513fb4f7cdSSrinivas Pandruvada 	clos_config.clos_min = clos_min;
20523fb4f7cdSSrinivas Pandruvada 	clos_config.clos_max = clos_max;
20533fb4f7cdSSrinivas Pandruvada 	clos_config.clos_desired = clos_desired;
20543fb4f7cdSSrinivas Pandruvada 	ret = isst_set_clos(cpu, current_clos, &clos_config);
20553fb4f7cdSSrinivas Pandruvada 	if (ret)
20563fb4f7cdSSrinivas Pandruvada 		perror("isst_set_clos");
20573fb4f7cdSSrinivas Pandruvada 	else
20583fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "config", ret);
20593fb4f7cdSSrinivas Pandruvada }
20603fb4f7cdSSrinivas Pandruvada 
2061ce1326a2SPrarit Bhargava static void set_clos_config(int arg)
20623fb4f7cdSSrinivas Pandruvada {
20633fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
20643fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
20653fb4f7cdSSrinivas Pandruvada 			"Set core-power configuration for one of the four clos ids\n");
20663fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
20673fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
20683fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos EPP with [--epp|-e]\n");
20693fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
20703fb4f7cdSSrinivas Pandruvada 			"\tSpecify clos Proportional Priority [--weight|-w]\n");
207140dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos min in MHz with [--min|-n]\n");
207240dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos max in MHz with [--max|-m]\n");
207340dee9ddSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify clos desired in MHz with [--desired|-d]\n");
20743fb4f7cdSSrinivas Pandruvada 		exit(0);
20753fb4f7cdSSrinivas Pandruvada 	}
20763fb4f7cdSSrinivas Pandruvada 
20773fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
20783fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Invalid clos id\n");
20793fb4f7cdSSrinivas Pandruvada 		exit(0);
20803fb4f7cdSSrinivas Pandruvada 	}
20813fb4f7cdSSrinivas Pandruvada 	if (clos_epp < 0 || clos_epp > 0x0F) {
20823fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "clos epp is not specified, default: 0\n");
20833fb4f7cdSSrinivas Pandruvada 		clos_epp = 0;
20843fb4f7cdSSrinivas Pandruvada 	}
20853fb4f7cdSSrinivas Pandruvada 	if (clos_prop_prio < 0 || clos_prop_prio > 0x0F) {
20863fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
20873fb4f7cdSSrinivas Pandruvada 			"clos frequency weight is not specified, default: 0\n");
20883fb4f7cdSSrinivas Pandruvada 		clos_prop_prio = 0;
20893fb4f7cdSSrinivas Pandruvada 	}
20903fb4f7cdSSrinivas Pandruvada 	if (clos_min < 0) {
20913fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "clos min is not specified, default: 0\n");
20923fb4f7cdSSrinivas Pandruvada 		clos_min = 0;
20933fb4f7cdSSrinivas Pandruvada 	}
20943fb4f7cdSSrinivas Pandruvada 	if (clos_max < 0) {
209540dee9ddSSrinivas Pandruvada 		fprintf(stderr, "clos max is not specified, default: 25500 MHz\n");
20963fb4f7cdSSrinivas Pandruvada 		clos_max = 0xff;
20973fb4f7cdSSrinivas Pandruvada 	}
20983fb4f7cdSSrinivas Pandruvada 	if (clos_desired < 0) {
20993fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "clos desired is not specified, default: 0\n");
21003fb4f7cdSSrinivas Pandruvada 		clos_desired = 0x00;
21013fb4f7cdSSrinivas Pandruvada 	}
21023fb4f7cdSSrinivas Pandruvada 
21033fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
21043fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21053fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_config_for_cpu, NULL,
21063fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
21073fb4f7cdSSrinivas Pandruvada 	else
21083fb4f7cdSSrinivas Pandruvada 		for_each_online_package_in_set(set_clos_config_for_cpu, NULL,
21093fb4f7cdSSrinivas Pandruvada 					       NULL, NULL, NULL);
21103fb4f7cdSSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
21113fb4f7cdSSrinivas Pandruvada }
21123fb4f7cdSSrinivas Pandruvada 
21133fb4f7cdSSrinivas Pandruvada static void set_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
21143fb4f7cdSSrinivas Pandruvada 				   void *arg4)
21153fb4f7cdSSrinivas Pandruvada {
21163fb4f7cdSSrinivas Pandruvada 	int ret;
21173fb4f7cdSSrinivas Pandruvada 
21183fb4f7cdSSrinivas Pandruvada 	ret = isst_clos_associate(cpu, current_clos);
21193fb4f7cdSSrinivas Pandruvada 	if (ret)
21203fb4f7cdSSrinivas Pandruvada 		perror("isst_clos_associate");
21213fb4f7cdSSrinivas Pandruvada 	else
21223fb4f7cdSSrinivas Pandruvada 		isst_display_result(cpu, outf, "core-power", "assoc", ret);
21233fb4f7cdSSrinivas Pandruvada }
21243fb4f7cdSSrinivas Pandruvada 
2125ce1326a2SPrarit Bhargava static void set_clos_assoc(int arg)
21263fb4f7cdSSrinivas Pandruvada {
21273fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
21283fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Associate a clos id to a CPU\n");
21293fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21303fb4f7cdSSrinivas Pandruvada 			"\tSpecify targeted clos id with [--clos|-c]\n");
213168e2f109SSrinivas Pandruvada 		fprintf(stderr,
213268e2f109SSrinivas Pandruvada 			"\tFor example to associate clos 1 to CPU 0: issue\n");
213368e2f109SSrinivas Pandruvada 		fprintf(stderr,
213468e2f109SSrinivas Pandruvada 			"\tintel-speed-select --cpu 0 core-power assoc --clos 1\n");
21353fb4f7cdSSrinivas Pandruvada 		exit(0);
21363fb4f7cdSSrinivas Pandruvada 	}
21373fb4f7cdSSrinivas Pandruvada 
21383fb4f7cdSSrinivas Pandruvada 	if (current_clos < 0 || current_clos > 3) {
21393fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Invalid clos id\n");
21403fb4f7cdSSrinivas Pandruvada 		exit(0);
21413fb4f7cdSSrinivas Pandruvada 	}
21423fb4f7cdSSrinivas Pandruvada 	if (max_target_cpus)
21433fb4f7cdSSrinivas Pandruvada 		for_each_online_target_cpu_in_set(set_clos_assoc_for_cpu, NULL,
21443fb4f7cdSSrinivas Pandruvada 						  NULL, NULL, NULL);
21453fb4f7cdSSrinivas Pandruvada 	else {
21463fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21473fb4f7cdSSrinivas Pandruvada 			"Invalid target cpu. Specify with [-c|--cpu]\n");
21483fb4f7cdSSrinivas Pandruvada 	}
21493fb4f7cdSSrinivas Pandruvada }
21503fb4f7cdSSrinivas Pandruvada 
21513fb4f7cdSSrinivas Pandruvada static void get_clos_assoc_for_cpu(int cpu, void *arg1, void *arg2, void *arg3,
21523fb4f7cdSSrinivas Pandruvada 				   void *arg4)
21533fb4f7cdSSrinivas Pandruvada {
21543fb4f7cdSSrinivas Pandruvada 	int clos, ret;
21553fb4f7cdSSrinivas Pandruvada 
21563fb4f7cdSSrinivas Pandruvada 	ret = isst_clos_get_assoc_status(cpu, &clos);
21573fb4f7cdSSrinivas Pandruvada 	if (ret)
21583fb4f7cdSSrinivas Pandruvada 		perror("isst_clos_get_assoc_status");
21593fb4f7cdSSrinivas Pandruvada 	else
2160e118fbe3SSrinivas Pandruvada 		isst_clos_display_assoc_information(cpu, outf, clos);
21613fb4f7cdSSrinivas Pandruvada }
21623fb4f7cdSSrinivas Pandruvada 
2163ce1326a2SPrarit Bhargava static void get_clos_assoc(int arg)
21643fb4f7cdSSrinivas Pandruvada {
21653fb4f7cdSSrinivas Pandruvada 	if (cmd_help) {
21663fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Get associate clos id to a CPU\n");
21673fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "\tSpecify targeted cpu id with [--cpu|-c]\n");
21683fb4f7cdSSrinivas Pandruvada 		exit(0);
21693fb4f7cdSSrinivas Pandruvada 	}
2170e118fbe3SSrinivas Pandruvada 
2171e118fbe3SSrinivas Pandruvada 	if (!max_target_cpus) {
21723fb4f7cdSSrinivas Pandruvada 		fprintf(stderr,
21733fb4f7cdSSrinivas Pandruvada 			"Invalid target cpu. Specify with [-c|--cpu]\n");
2174e118fbe3SSrinivas Pandruvada 		exit(0);
21753fb4f7cdSSrinivas Pandruvada 	}
2176e118fbe3SSrinivas Pandruvada 
2177e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_start(outf);
2178e118fbe3SSrinivas Pandruvada 	for_each_online_target_cpu_in_set(get_clos_assoc_for_cpu, NULL,
2179e118fbe3SSrinivas Pandruvada 					  NULL, NULL, NULL);
2180e118fbe3SSrinivas Pandruvada 	isst_ctdp_display_information_end(outf);
21813fb4f7cdSSrinivas Pandruvada }
21823fb4f7cdSSrinivas Pandruvada 
2183c829f0efSPrarit Bhargava static struct process_cmd_struct clx_n_cmds[] = {
2184062e4aacSPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
21851aa7177cSPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
21861aa7177cSPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
21871aa7177cSPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2188c829f0efSPrarit Bhargava 	{ NULL, NULL, NULL, 0 }
2189c829f0efSPrarit Bhargava };
2190c829f0efSPrarit Bhargava 
21913fb4f7cdSSrinivas Pandruvada static struct process_cmd_struct isst_cmds[] = {
2192ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-lock-status", get_tdp_locked, 0 },
2193ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-levels", get_tdp_levels, 0 },
2194ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-version", get_tdp_version, 0 },
2195ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-enabled", get_tdp_enabled, 0 },
2196ce1326a2SPrarit Bhargava 	{ "perf-profile", "get-config-current-level", get_tdp_current_level,
2197ce1326a2SPrarit Bhargava 	 0 },
2198ce1326a2SPrarit Bhargava 	{ "perf-profile", "set-config-level", set_tdp_level, 0 },
2199ce1326a2SPrarit Bhargava 	{ "perf-profile", "info", dump_isst_config, 0 },
2200ce1326a2SPrarit Bhargava 	{ "base-freq", "info", dump_pbf_config, 0 },
2201ce1326a2SPrarit Bhargava 	{ "base-freq", "enable", set_pbf_enable, 1 },
2202ce1326a2SPrarit Bhargava 	{ "base-freq", "disable", set_pbf_enable, 0 },
2203ce1326a2SPrarit Bhargava 	{ "turbo-freq", "info", dump_fact_config, 0 },
2204ce1326a2SPrarit Bhargava 	{ "turbo-freq", "enable", set_fact_enable, 1 },
2205ce1326a2SPrarit Bhargava 	{ "turbo-freq", "disable", set_fact_enable, 0 },
2206ce1326a2SPrarit Bhargava 	{ "core-power", "info", dump_clos_info, 0 },
2207ce1326a2SPrarit Bhargava 	{ "core-power", "enable", set_clos_enable, 1 },
2208ce1326a2SPrarit Bhargava 	{ "core-power", "disable", set_clos_enable, 0 },
2209ce1326a2SPrarit Bhargava 	{ "core-power", "config", set_clos_config, 0 },
2210ce1326a2SPrarit Bhargava 	{ "core-power", "get-config", dump_clos_config, 0 },
2211ce1326a2SPrarit Bhargava 	{ "core-power", "assoc", set_clos_assoc, 0 },
2212ce1326a2SPrarit Bhargava 	{ "core-power", "get-assoc", get_clos_assoc, 0 },
22133fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL, NULL }
22143fb4f7cdSSrinivas Pandruvada };
22153fb4f7cdSSrinivas Pandruvada 
22163fb4f7cdSSrinivas Pandruvada /*
22173fb4f7cdSSrinivas Pandruvada  * parse cpuset with following syntax
22183fb4f7cdSSrinivas Pandruvada  * 1,2,4..6,8-10 and set bits in cpu_subset
22193fb4f7cdSSrinivas Pandruvada  */
22203fb4f7cdSSrinivas Pandruvada void parse_cpu_command(char *optarg)
22213fb4f7cdSSrinivas Pandruvada {
22223fb4f7cdSSrinivas Pandruvada 	unsigned int start, end;
22233fb4f7cdSSrinivas Pandruvada 	char *next;
22243fb4f7cdSSrinivas Pandruvada 
22253fb4f7cdSSrinivas Pandruvada 	next = optarg;
22263fb4f7cdSSrinivas Pandruvada 
22273fb4f7cdSSrinivas Pandruvada 	while (next && *next) {
22283fb4f7cdSSrinivas Pandruvada 		if (*next == '-') /* no negative cpu numbers */
22293fb4f7cdSSrinivas Pandruvada 			goto error;
22303fb4f7cdSSrinivas Pandruvada 
22313fb4f7cdSSrinivas Pandruvada 		start = strtoul(next, &next, 10);
22323fb4f7cdSSrinivas Pandruvada 
22333fb4f7cdSSrinivas Pandruvada 		if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
22343fb4f7cdSSrinivas Pandruvada 			target_cpus[max_target_cpus++] = start;
22353fb4f7cdSSrinivas Pandruvada 
22363fb4f7cdSSrinivas Pandruvada 		if (*next == '\0')
22373fb4f7cdSSrinivas Pandruvada 			break;
22383fb4f7cdSSrinivas Pandruvada 
22393fb4f7cdSSrinivas Pandruvada 		if (*next == ',') {
22403fb4f7cdSSrinivas Pandruvada 			next += 1;
22413fb4f7cdSSrinivas Pandruvada 			continue;
22423fb4f7cdSSrinivas Pandruvada 		}
22433fb4f7cdSSrinivas Pandruvada 
22443fb4f7cdSSrinivas Pandruvada 		if (*next == '-') {
22453fb4f7cdSSrinivas Pandruvada 			next += 1; /* start range */
22463fb4f7cdSSrinivas Pandruvada 		} else if (*next == '.') {
22473fb4f7cdSSrinivas Pandruvada 			next += 1;
22483fb4f7cdSSrinivas Pandruvada 			if (*next == '.')
22493fb4f7cdSSrinivas Pandruvada 				next += 1; /* start range */
22503fb4f7cdSSrinivas Pandruvada 			else
22513fb4f7cdSSrinivas Pandruvada 				goto error;
22523fb4f7cdSSrinivas Pandruvada 		}
22533fb4f7cdSSrinivas Pandruvada 
22543fb4f7cdSSrinivas Pandruvada 		end = strtoul(next, &next, 10);
22553fb4f7cdSSrinivas Pandruvada 		if (end <= start)
22563fb4f7cdSSrinivas Pandruvada 			goto error;
22573fb4f7cdSSrinivas Pandruvada 
22583fb4f7cdSSrinivas Pandruvada 		while (++start <= end) {
22593fb4f7cdSSrinivas Pandruvada 			if (max_target_cpus < MAX_CPUS_IN_ONE_REQ)
22603fb4f7cdSSrinivas Pandruvada 				target_cpus[max_target_cpus++] = start;
22613fb4f7cdSSrinivas Pandruvada 		}
22623fb4f7cdSSrinivas Pandruvada 
22633fb4f7cdSSrinivas Pandruvada 		if (*next == ',')
22643fb4f7cdSSrinivas Pandruvada 			next += 1;
22653fb4f7cdSSrinivas Pandruvada 		else if (*next != '\0')
22663fb4f7cdSSrinivas Pandruvada 			goto error;
22673fb4f7cdSSrinivas Pandruvada 	}
22683fb4f7cdSSrinivas Pandruvada 
22693fb4f7cdSSrinivas Pandruvada #ifdef DEBUG
22703fb4f7cdSSrinivas Pandruvada 	{
22713fb4f7cdSSrinivas Pandruvada 		int i;
22723fb4f7cdSSrinivas Pandruvada 
22733fb4f7cdSSrinivas Pandruvada 		for (i = 0; i < max_target_cpus; ++i)
22743fb4f7cdSSrinivas Pandruvada 			printf("cpu [%d] in arg\n", target_cpus[i]);
22753fb4f7cdSSrinivas Pandruvada 	}
22763fb4f7cdSSrinivas Pandruvada #endif
22773fb4f7cdSSrinivas Pandruvada 	return;
22783fb4f7cdSSrinivas Pandruvada 
22793fb4f7cdSSrinivas Pandruvada error:
22803fb4f7cdSSrinivas Pandruvada 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
22813fb4f7cdSSrinivas Pandruvada 	exit(-1);
22823fb4f7cdSSrinivas Pandruvada }
22833fb4f7cdSSrinivas Pandruvada 
22843fb4f7cdSSrinivas Pandruvada static void parse_cmd_args(int argc, int start, char **argv)
22853fb4f7cdSSrinivas Pandruvada {
22863fb4f7cdSSrinivas Pandruvada 	int opt;
22873fb4f7cdSSrinivas Pandruvada 	int option_index;
22883fb4f7cdSSrinivas Pandruvada 
22893fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
22903fb4f7cdSSrinivas Pandruvada 		{ "bucket", required_argument, 0, 'b' },
22913fb4f7cdSSrinivas Pandruvada 		{ "level", required_argument, 0, 'l' },
22923c64c81aSSrinivas Pandruvada 		{ "online", required_argument, 0, 'o' },
22933fb4f7cdSSrinivas Pandruvada 		{ "trl-type", required_argument, 0, 'r' },
22943fb4f7cdSSrinivas Pandruvada 		{ "trl", required_argument, 0, 't' },
22953fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
22963fb4f7cdSSrinivas Pandruvada 		{ "clos", required_argument, 0, 'c' },
22973fb4f7cdSSrinivas Pandruvada 		{ "desired", required_argument, 0, 'd' },
22983fb4f7cdSSrinivas Pandruvada 		{ "epp", required_argument, 0, 'e' },
22993fb4f7cdSSrinivas Pandruvada 		{ "min", required_argument, 0, 'n' },
23003fb4f7cdSSrinivas Pandruvada 		{ "max", required_argument, 0, 'm' },
23013fb4f7cdSSrinivas Pandruvada 		{ "priority", required_argument, 0, 'p' },
23023fb4f7cdSSrinivas Pandruvada 		{ "weight", required_argument, 0, 'w' },
2303354bd06fSSrinivas Pandruvada 		{ "auto", no_argument, 0, 'a' },
23043fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
23053fb4f7cdSSrinivas Pandruvada 	};
23063fb4f7cdSSrinivas Pandruvada 
23073fb4f7cdSSrinivas Pandruvada 	option_index = start;
23083fb4f7cdSSrinivas Pandruvada 
23093fb4f7cdSSrinivas Pandruvada 	optind = start + 1;
2310354bd06fSSrinivas Pandruvada 	while ((opt = getopt_long(argc, argv, "b:l:t:c:d:e:n:m:p:w:hoa",
23113fb4f7cdSSrinivas Pandruvada 				  long_options, &option_index)) != -1) {
23123fb4f7cdSSrinivas Pandruvada 		switch (opt) {
2313354bd06fSSrinivas Pandruvada 		case 'a':
2314354bd06fSSrinivas Pandruvada 			auto_mode = 1;
2315354bd06fSSrinivas Pandruvada 			break;
23163fb4f7cdSSrinivas Pandruvada 		case 'b':
23173fb4f7cdSSrinivas Pandruvada 			fact_bucket = atoi(optarg);
23183fb4f7cdSSrinivas Pandruvada 			break;
23193fb4f7cdSSrinivas Pandruvada 		case 'h':
23203fb4f7cdSSrinivas Pandruvada 			cmd_help = 1;
23213fb4f7cdSSrinivas Pandruvada 			break;
23223fb4f7cdSSrinivas Pandruvada 		case 'l':
23233fb4f7cdSSrinivas Pandruvada 			tdp_level = atoi(optarg);
23243fb4f7cdSSrinivas Pandruvada 			break;
23253c64c81aSSrinivas Pandruvada 		case 'o':
23263c64c81aSSrinivas Pandruvada 			force_online_offline = 1;
23273c64c81aSSrinivas Pandruvada 			break;
23283fb4f7cdSSrinivas Pandruvada 		case 't':
23293fb4f7cdSSrinivas Pandruvada 			sscanf(optarg, "0x%llx", &fact_trl);
23303fb4f7cdSSrinivas Pandruvada 			break;
23313fb4f7cdSSrinivas Pandruvada 		case 'r':
23323fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "sse", 3)) {
23333fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x01;
23343fb4f7cdSSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx2", 4)) {
23353fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x02;
23363fb4f7cdSSrinivas Pandruvada 			} else if (!strncmp(optarg, "avx512", 4)) {
23373fb4f7cdSSrinivas Pandruvada 				fact_avx = 0x04;
23383fb4f7cdSSrinivas Pandruvada 			} else {
23393fb4f7cdSSrinivas Pandruvada 				fprintf(outf, "Invalid sse,avx options\n");
23403fb4f7cdSSrinivas Pandruvada 				exit(1);
23413fb4f7cdSSrinivas Pandruvada 			}
23423fb4f7cdSSrinivas Pandruvada 			break;
23433fb4f7cdSSrinivas Pandruvada 		/* CLOS related */
23443fb4f7cdSSrinivas Pandruvada 		case 'c':
23453fb4f7cdSSrinivas Pandruvada 			current_clos = atoi(optarg);
23463fb4f7cdSSrinivas Pandruvada 			break;
23473fb4f7cdSSrinivas Pandruvada 		case 'd':
23483fb4f7cdSSrinivas Pandruvada 			clos_desired = atoi(optarg);
234940dee9ddSSrinivas Pandruvada 			clos_desired /= DISP_FREQ_MULTIPLIER;
23503fb4f7cdSSrinivas Pandruvada 			break;
23513fb4f7cdSSrinivas Pandruvada 		case 'e':
23523fb4f7cdSSrinivas Pandruvada 			clos_epp = atoi(optarg);
23533fb4f7cdSSrinivas Pandruvada 			break;
23543fb4f7cdSSrinivas Pandruvada 		case 'n':
23553fb4f7cdSSrinivas Pandruvada 			clos_min = atoi(optarg);
235640dee9ddSSrinivas Pandruvada 			clos_min /= DISP_FREQ_MULTIPLIER;
23573fb4f7cdSSrinivas Pandruvada 			break;
23583fb4f7cdSSrinivas Pandruvada 		case 'm':
23593fb4f7cdSSrinivas Pandruvada 			clos_max = atoi(optarg);
236040dee9ddSSrinivas Pandruvada 			clos_max /= DISP_FREQ_MULTIPLIER;
23613fb4f7cdSSrinivas Pandruvada 			break;
23623fb4f7cdSSrinivas Pandruvada 		case 'p':
23633fb4f7cdSSrinivas Pandruvada 			clos_priority_type = atoi(optarg);
23643fb4f7cdSSrinivas Pandruvada 			break;
23653fb4f7cdSSrinivas Pandruvada 		case 'w':
23663fb4f7cdSSrinivas Pandruvada 			clos_prop_prio = atoi(optarg);
23673fb4f7cdSSrinivas Pandruvada 			break;
23683fb4f7cdSSrinivas Pandruvada 		default:
23693fb4f7cdSSrinivas Pandruvada 			printf("no match\n");
23703fb4f7cdSSrinivas Pandruvada 		}
23713fb4f7cdSSrinivas Pandruvada 	}
23723fb4f7cdSSrinivas Pandruvada }
23733fb4f7cdSSrinivas Pandruvada 
23743fb4f7cdSSrinivas Pandruvada static void isst_help(void)
23753fb4f7cdSSrinivas Pandruvada {
23763fb4f7cdSSrinivas Pandruvada 	printf("perf-profile:\tAn architectural mechanism that allows multiple optimized \n\
23773fb4f7cdSSrinivas Pandruvada 		performance profiles per system via static and/or dynamic\n\
23783fb4f7cdSSrinivas Pandruvada 		adjustment of core count, workload, Tjmax, and\n\
23793fb4f7cdSSrinivas Pandruvada 		TDP, etc.\n");
23803fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=perf-profile\n");
23813fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
2382c829f0efSPrarit Bhargava 
2383c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
23843fb4f7cdSSrinivas Pandruvada 		printf("\tget-lock-status\n");
23853fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-levels\n");
23863fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-version\n");
23873fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-enabled\n");
23883fb4f7cdSSrinivas Pandruvada 		printf("\tget-config-current-level\n");
23893fb4f7cdSSrinivas Pandruvada 		printf("\tset-config-level\n");
23903fb4f7cdSSrinivas Pandruvada 	}
2391c829f0efSPrarit Bhargava }
23923fb4f7cdSSrinivas Pandruvada 
23933fb4f7cdSSrinivas Pandruvada static void pbf_help(void)
23943fb4f7cdSSrinivas Pandruvada {
23953fb4f7cdSSrinivas Pandruvada 	printf("base-freq:\tEnables users to increase guaranteed base frequency\n\
23963fb4f7cdSSrinivas Pandruvada 		on certain cores (high priority cores) in exchange for lower\n\
23973fb4f7cdSSrinivas Pandruvada 		base frequency on remaining cores (low priority cores).\n");
23983fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
23993fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
24003fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
24013fb4f7cdSSrinivas Pandruvada }
24023fb4f7cdSSrinivas Pandruvada 
24033fb4f7cdSSrinivas Pandruvada static void fact_help(void)
24043fb4f7cdSSrinivas Pandruvada {
24053fb4f7cdSSrinivas Pandruvada 	printf("turbo-freq:\tEnables the ability to set different turbo ratio\n\
24063fb4f7cdSSrinivas Pandruvada 		limits to cores based on priority.\n");
24073fb4f7cdSSrinivas Pandruvada 	printf("\nCommand: For feature=turbo-freq\n");
24083fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : info\n");
24093fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : enable\n");
24103fb4f7cdSSrinivas Pandruvada 	printf("\tcommand : disable\n");
24113fb4f7cdSSrinivas Pandruvada }
24123fb4f7cdSSrinivas Pandruvada 
24133fb4f7cdSSrinivas Pandruvada static void core_power_help(void)
24143fb4f7cdSSrinivas Pandruvada {
24153fb4f7cdSSrinivas Pandruvada 	printf("core-power:\tInterface that allows user to define per core/tile\n\
24163fb4f7cdSSrinivas Pandruvada 		priority.\n");
24173fb4f7cdSSrinivas Pandruvada 	printf("\nCommands : For feature=core-power\n");
24183fb4f7cdSSrinivas Pandruvada 	printf("\tinfo\n");
24193fb4f7cdSSrinivas Pandruvada 	printf("\tenable\n");
24203fb4f7cdSSrinivas Pandruvada 	printf("\tdisable\n");
24213fb4f7cdSSrinivas Pandruvada 	printf("\tconfig\n");
2422188afed9SSrinivas Pandruvada 	printf("\tget-config\n");
24233fb4f7cdSSrinivas Pandruvada 	printf("\tassoc\n");
24243fb4f7cdSSrinivas Pandruvada 	printf("\tget-assoc\n");
24253fb4f7cdSSrinivas Pandruvada }
24263fb4f7cdSSrinivas Pandruvada 
24273fb4f7cdSSrinivas Pandruvada struct process_cmd_help_struct {
24283fb4f7cdSSrinivas Pandruvada 	char *feature;
24293fb4f7cdSSrinivas Pandruvada 	void (*process_fn)(void);
24303fb4f7cdSSrinivas Pandruvada };
24313fb4f7cdSSrinivas Pandruvada 
24323fb4f7cdSSrinivas Pandruvada static struct process_cmd_help_struct isst_help_cmds[] = {
24333fb4f7cdSSrinivas Pandruvada 	{ "perf-profile", isst_help },
24343fb4f7cdSSrinivas Pandruvada 	{ "base-freq", pbf_help },
24353fb4f7cdSSrinivas Pandruvada 	{ "turbo-freq", fact_help },
24363fb4f7cdSSrinivas Pandruvada 	{ "core-power", core_power_help },
24373fb4f7cdSSrinivas Pandruvada 	{ NULL, NULL }
24383fb4f7cdSSrinivas Pandruvada };
24393fb4f7cdSSrinivas Pandruvada 
2440c829f0efSPrarit Bhargava static struct process_cmd_help_struct clx_n_help_cmds[] = {
2441c829f0efSPrarit Bhargava 	{ "perf-profile", isst_help },
2442c829f0efSPrarit Bhargava 	{ "base-freq", pbf_help },
2443c829f0efSPrarit Bhargava 	{ NULL, NULL }
2444c829f0efSPrarit Bhargava };
2445c829f0efSPrarit Bhargava 
2446210369dcSPrarit Bhargava void process_command(int argc, char **argv,
2447210369dcSPrarit Bhargava 		     struct process_cmd_help_struct *help_cmds,
2448210369dcSPrarit Bhargava 		     struct process_cmd_struct *cmds)
24493fb4f7cdSSrinivas Pandruvada {
24503fb4f7cdSSrinivas Pandruvada 	int i = 0, matched = 0;
24513fb4f7cdSSrinivas Pandruvada 	char *feature = argv[optind];
24523fb4f7cdSSrinivas Pandruvada 	char *cmd = argv[optind + 1];
24533fb4f7cdSSrinivas Pandruvada 
24543fb4f7cdSSrinivas Pandruvada 	if (!feature || !cmd)
24553fb4f7cdSSrinivas Pandruvada 		return;
24563fb4f7cdSSrinivas Pandruvada 
24573fb4f7cdSSrinivas Pandruvada 	debug_printf("feature name [%s] command [%s]\n", feature, cmd);
24583fb4f7cdSSrinivas Pandruvada 	if (!strcmp(cmd, "-h") || !strcmp(cmd, "--help")) {
2459210369dcSPrarit Bhargava 		while (help_cmds[i].feature) {
2460210369dcSPrarit Bhargava 			if (!strcmp(help_cmds[i].feature, feature)) {
2461210369dcSPrarit Bhargava 				help_cmds[i].process_fn();
24623fb4f7cdSSrinivas Pandruvada 				exit(0);
24633fb4f7cdSSrinivas Pandruvada 			}
24643fb4f7cdSSrinivas Pandruvada 			++i;
24653fb4f7cdSSrinivas Pandruvada 		}
24663fb4f7cdSSrinivas Pandruvada 	}
24673fb4f7cdSSrinivas Pandruvada 
2468062e4aacSPrarit Bhargava 	if (!is_clx_n_platform())
24693fb4f7cdSSrinivas Pandruvada 		create_cpu_map();
24703fb4f7cdSSrinivas Pandruvada 
24713fb4f7cdSSrinivas Pandruvada 	i = 0;
2472210369dcSPrarit Bhargava 	while (cmds[i].feature) {
2473210369dcSPrarit Bhargava 		if (!strcmp(cmds[i].feature, feature) &&
2474210369dcSPrarit Bhargava 		    !strcmp(cmds[i].command, cmd)) {
24753fb4f7cdSSrinivas Pandruvada 			parse_cmd_args(argc, optind + 1, argv);
2476210369dcSPrarit Bhargava 			cmds[i].process_fn(cmds[i].arg);
24773fb4f7cdSSrinivas Pandruvada 			matched = 1;
24783fb4f7cdSSrinivas Pandruvada 			break;
24793fb4f7cdSSrinivas Pandruvada 		}
24803fb4f7cdSSrinivas Pandruvada 		++i;
24813fb4f7cdSSrinivas Pandruvada 	}
24823fb4f7cdSSrinivas Pandruvada 
24833fb4f7cdSSrinivas Pandruvada 	if (!matched)
24843fb4f7cdSSrinivas Pandruvada 		fprintf(stderr, "Invalid command\n");
24853fb4f7cdSSrinivas Pandruvada }
24863fb4f7cdSSrinivas Pandruvada 
24873fb4f7cdSSrinivas Pandruvada static void usage(void)
24883fb4f7cdSSrinivas Pandruvada {
2489addd116dSSrinivas Pandruvada 	if (is_clx_n_platform()) {
2490addd116dSSrinivas Pandruvada 		fprintf(stderr, "\nThere is limited support of Intel Speed Select features on this platform.\n");
2491addd116dSSrinivas Pandruvada 		fprintf(stderr, "Everything is pre-configured using BIOS options, this tool can't enable any feature in the hardware.\n\n");
2492addd116dSSrinivas Pandruvada 	}
2493addd116dSSrinivas Pandruvada 
24943fb4f7cdSSrinivas Pandruvada 	printf("\nUsage:\n");
24953fb4f7cdSSrinivas Pandruvada 	printf("intel-speed-select [OPTIONS] FEATURE COMMAND COMMAND_ARGUMENTS\n");
2496addd116dSSrinivas Pandruvada 	printf("\nUse this tool to enumerate and control the Intel Speed Select Technology features:\n");
2497addd116dSSrinivas Pandruvada 	if (is_clx_n_platform())
2498addd116dSSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq]\n");
2499addd116dSSrinivas Pandruvada 	else
25003fb4f7cdSSrinivas Pandruvada 		printf("\nFEATURE : [perf-profile|base-freq|turbo-freq|core-power]\n");
250143774c0dSPrarit Bhargava 	printf("\nFor help on each feature, use -h|--help\n");
25023fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile -h\n");
25033fb4f7cdSSrinivas Pandruvada 
25043fb4f7cdSSrinivas Pandruvada 	printf("\nFor additional help on each command for a feature, use --h|--help\n");
25053fb4f7cdSSrinivas Pandruvada 	printf("\tFor example:  intel-speed-select perf-profile get-lock-status -h\n");
25063fb4f7cdSSrinivas Pandruvada 	printf("\t\t This will print help for the command \"get-lock-status\" for the feature \"perf-profile\"\n");
25073fb4f7cdSSrinivas Pandruvada 
25083fb4f7cdSSrinivas Pandruvada 	printf("\nOPTIONS\n");
25093fb4f7cdSSrinivas Pandruvada 	printf("\t[-c|--cpu] : logical cpu number\n");
25103fb4f7cdSSrinivas Pandruvada 	printf("\t\tDefault: Die scoped for all dies in the system with multiple dies/package\n");
25113fb4f7cdSSrinivas Pandruvada 	printf("\t\t\t Or Package scoped for all Packages when each package contains one die\n");
25123fb4f7cdSSrinivas Pandruvada 	printf("\t[-d|--debug] : Debug mode\n");
2513addd116dSSrinivas Pandruvada 	printf("\t[-f|--format] : output format [json|text]. Default: text\n");
25143fb4f7cdSSrinivas Pandruvada 	printf("\t[-h|--help] : Print help\n");
25153fb4f7cdSSrinivas Pandruvada 	printf("\t[-i|--info] : Print platform information\n");
25163fb4f7cdSSrinivas Pandruvada 	printf("\t[-o|--out] : Output file\n");
25173fb4f7cdSSrinivas Pandruvada 	printf("\t\t\tDefault : stderr\n");
25183fb4f7cdSSrinivas Pandruvada 	printf("\t[-v|--version] : Print version\n");
25193fb4f7cdSSrinivas Pandruvada 
25203fb4f7cdSSrinivas Pandruvada 	printf("\nResult format\n");
25213fb4f7cdSSrinivas Pandruvada 	printf("\tResult display uses a common format for each command:\n");
25223fb4f7cdSSrinivas Pandruvada 	printf("\tResults are formatted in text/JSON with\n");
25233fb4f7cdSSrinivas Pandruvada 	printf("\t\tPackage, Die, CPU, and command specific results.\n");
2524addd116dSSrinivas Pandruvada 
2525addd116dSSrinivas Pandruvada 	printf("\nExamples\n");
2526addd116dSSrinivas Pandruvada 	printf("\tTo get platform information:\n");
2527addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select --info\n");
2528addd116dSSrinivas Pandruvada 	printf("\tTo get full perf-profile information dump:\n");
2529addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select perf-profile info\n");
2530addd116dSSrinivas Pandruvada 	printf("\tTo get full base-freq information dump:\n");
2531addd116dSSrinivas Pandruvada 	printf("\t\tintel-speed-select base-freq info -l 0\n");
2532addd116dSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2533addd116dSSrinivas Pandruvada 		printf("\tTo get full turbo-freq information dump:\n");
2534addd116dSSrinivas Pandruvada 		printf("\t\tintel-speed-select turbo-freq info -l 0\n");
2535addd116dSSrinivas Pandruvada 	}
25363fb4f7cdSSrinivas Pandruvada 	exit(1);
25373fb4f7cdSSrinivas Pandruvada }
25383fb4f7cdSSrinivas Pandruvada 
25393fb4f7cdSSrinivas Pandruvada static void print_version(void)
25403fb4f7cdSSrinivas Pandruvada {
25413fb4f7cdSSrinivas Pandruvada 	fprintf(outf, "Version %s\n", version_str);
25423fb4f7cdSSrinivas Pandruvada 	fprintf(outf, "Build date %s time %s\n", __DATE__, __TIME__);
25433fb4f7cdSSrinivas Pandruvada 	exit(0);
25443fb4f7cdSSrinivas Pandruvada }
25453fb4f7cdSSrinivas Pandruvada 
25463fb4f7cdSSrinivas Pandruvada static void cmdline(int argc, char **argv)
25473fb4f7cdSSrinivas Pandruvada {
2548f362cdccSSrinivas Pandruvada 	const char *pathname = "/dev/isst_interface";
2549f362cdccSSrinivas Pandruvada 	FILE *fp;
25503fb4f7cdSSrinivas Pandruvada 	int opt;
25513fb4f7cdSSrinivas Pandruvada 	int option_index = 0;
25523bc3d30cSPrarit Bhargava 	int ret;
25533fb4f7cdSSrinivas Pandruvada 
25543fb4f7cdSSrinivas Pandruvada 	static struct option long_options[] = {
25553fb4f7cdSSrinivas Pandruvada 		{ "cpu", required_argument, 0, 'c' },
25563fb4f7cdSSrinivas Pandruvada 		{ "debug", no_argument, 0, 'd' },
25573fb4f7cdSSrinivas Pandruvada 		{ "format", required_argument, 0, 'f' },
25583fb4f7cdSSrinivas Pandruvada 		{ "help", no_argument, 0, 'h' },
25593fb4f7cdSSrinivas Pandruvada 		{ "info", no_argument, 0, 'i' },
25603fb4f7cdSSrinivas Pandruvada 		{ "out", required_argument, 0, 'o' },
25613fb4f7cdSSrinivas Pandruvada 		{ "version", no_argument, 0, 'v' },
25623fb4f7cdSSrinivas Pandruvada 		{ 0, 0, 0, 0 }
25633fb4f7cdSSrinivas Pandruvada 	};
25643fb4f7cdSSrinivas Pandruvada 
2565f362cdccSSrinivas Pandruvada 	if (geteuid() != 0) {
2566f362cdccSSrinivas Pandruvada 		fprintf(stderr, "Must run as root\n");
2567f362cdccSSrinivas Pandruvada 		exit(0);
2568f362cdccSSrinivas Pandruvada 	}
2569f362cdccSSrinivas Pandruvada 
2570f362cdccSSrinivas Pandruvada 	ret = update_cpu_model();
2571f362cdccSSrinivas Pandruvada 	if (ret)
2572f362cdccSSrinivas Pandruvada 		err(-1, "Invalid CPU model (%d)\n", cpu_model);
2573f362cdccSSrinivas Pandruvada 	printf("Intel(R) Speed Select Technology\n");
2574f362cdccSSrinivas Pandruvada 	printf("Executing on CPU model:%d[0x%x]\n", cpu_model, cpu_model);
2575f362cdccSSrinivas Pandruvada 
2576f362cdccSSrinivas Pandruvada 	if (!is_clx_n_platform()) {
2577f362cdccSSrinivas Pandruvada 		fp = fopen(pathname, "rb");
2578f362cdccSSrinivas Pandruvada 		if (!fp) {
2579f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Intel speed select drivers are not loaded on this system.\n");
2580f362cdccSSrinivas Pandruvada 			fprintf(stderr, "Verify that kernel config includes CONFIG_INTEL_SPEED_SELECT_INTERFACE.\n");
2581f362cdccSSrinivas Pandruvada 			fprintf(stderr, "If the config is included then this is not a supported platform.\n");
2582f362cdccSSrinivas Pandruvada 			exit(0);
2583f362cdccSSrinivas Pandruvada 		}
2584f362cdccSSrinivas Pandruvada 		fclose(fp);
2585f362cdccSSrinivas Pandruvada 	}
2586f362cdccSSrinivas Pandruvada 
25873fb4f7cdSSrinivas Pandruvada 	progname = argv[0];
25883fb4f7cdSSrinivas Pandruvada 	while ((opt = getopt_long_only(argc, argv, "+c:df:hio:v", long_options,
25893fb4f7cdSSrinivas Pandruvada 				       &option_index)) != -1) {
25903fb4f7cdSSrinivas Pandruvada 		switch (opt) {
25913fb4f7cdSSrinivas Pandruvada 		case 'c':
25923fb4f7cdSSrinivas Pandruvada 			parse_cpu_command(optarg);
25933fb4f7cdSSrinivas Pandruvada 			break;
25943fb4f7cdSSrinivas Pandruvada 		case 'd':
25953fb4f7cdSSrinivas Pandruvada 			debug_flag = 1;
25963fb4f7cdSSrinivas Pandruvada 			printf("Debug Mode ON\n");
25973fb4f7cdSSrinivas Pandruvada 			break;
25983fb4f7cdSSrinivas Pandruvada 		case 'f':
25993fb4f7cdSSrinivas Pandruvada 			if (!strncmp(optarg, "json", 4))
26003fb4f7cdSSrinivas Pandruvada 				out_format_json = 1;
26013fb4f7cdSSrinivas Pandruvada 			break;
26023fb4f7cdSSrinivas Pandruvada 		case 'h':
26033fb4f7cdSSrinivas Pandruvada 			usage();
26043fb4f7cdSSrinivas Pandruvada 			break;
26053fb4f7cdSSrinivas Pandruvada 		case 'i':
26063fb4f7cdSSrinivas Pandruvada 			isst_print_platform_information();
26073fb4f7cdSSrinivas Pandruvada 			break;
26083fb4f7cdSSrinivas Pandruvada 		case 'o':
26093fb4f7cdSSrinivas Pandruvada 			if (outf)
26103fb4f7cdSSrinivas Pandruvada 				fclose(outf);
26113fb4f7cdSSrinivas Pandruvada 			outf = fopen_or_exit(optarg, "w");
26123fb4f7cdSSrinivas Pandruvada 			break;
26133fb4f7cdSSrinivas Pandruvada 		case 'v':
26143fb4f7cdSSrinivas Pandruvada 			print_version();
26153fb4f7cdSSrinivas Pandruvada 			break;
26163fb4f7cdSSrinivas Pandruvada 		default:
26173fb4f7cdSSrinivas Pandruvada 			usage();
26183fb4f7cdSSrinivas Pandruvada 		}
26193fb4f7cdSSrinivas Pandruvada 	}
26203fb4f7cdSSrinivas Pandruvada 
26213fb4f7cdSSrinivas Pandruvada 	if (optind > (argc - 2)) {
2622addd116dSSrinivas Pandruvada 		usage();
26233fb4f7cdSSrinivas Pandruvada 		exit(0);
26243fb4f7cdSSrinivas Pandruvada 	}
26253fb4f7cdSSrinivas Pandruvada 	set_max_cpu_num();
2626fb186158SSrinivas Pandruvada 	store_cpu_topology();
26273fb4f7cdSSrinivas Pandruvada 	set_cpu_present_cpu_mask();
26283fb4f7cdSSrinivas Pandruvada 	set_cpu_target_cpu_mask();
2629c829f0efSPrarit Bhargava 
2630c829f0efSPrarit Bhargava 	if (!is_clx_n_platform()) {
26313bc3d30cSPrarit Bhargava 		ret = isst_fill_platform_info();
26323bc3d30cSPrarit Bhargava 		if (ret)
26333bc3d30cSPrarit Bhargava 			goto out;
2634210369dcSPrarit Bhargava 		process_command(argc, argv, isst_help_cmds, isst_cmds);
2635c829f0efSPrarit Bhargava 	} else {
2636c829f0efSPrarit Bhargava 		process_command(argc, argv, clx_n_help_cmds, clx_n_cmds);
2637c829f0efSPrarit Bhargava 	}
26383bc3d30cSPrarit Bhargava out:
26393bc3d30cSPrarit Bhargava 	free_cpu_set(present_cpumask);
26403bc3d30cSPrarit Bhargava 	free_cpu_set(target_cpumask);
26413fb4f7cdSSrinivas Pandruvada }
26423fb4f7cdSSrinivas Pandruvada 
26433fb4f7cdSSrinivas Pandruvada int main(int argc, char **argv)
26443fb4f7cdSSrinivas Pandruvada {
26453fb4f7cdSSrinivas Pandruvada 	outf = stderr;
26463fb4f7cdSSrinivas Pandruvada 	cmdline(argc, argv);
26473fb4f7cdSSrinivas Pandruvada 	return 0;
26483fb4f7cdSSrinivas Pandruvada }
2649