18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Intel Speed Select -- Enumerate and control features
48c2ecf20Sopenharmony_ci * Copyright (c) 2019 Intel Corporation.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef _ISST_H_
88c2ecf20Sopenharmony_ci#define _ISST_H_
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <stdio.h>
118c2ecf20Sopenharmony_ci#include <unistd.h>
128c2ecf20Sopenharmony_ci#include <sys/types.h>
138c2ecf20Sopenharmony_ci#include <sched.h>
148c2ecf20Sopenharmony_ci#include <sys/stat.h>
158c2ecf20Sopenharmony_ci#include <sys/resource.h>
168c2ecf20Sopenharmony_ci#include <getopt.h>
178c2ecf20Sopenharmony_ci#include <err.h>
188c2ecf20Sopenharmony_ci#include <fcntl.h>
198c2ecf20Sopenharmony_ci#include <signal.h>
208c2ecf20Sopenharmony_ci#include <sys/time.h>
218c2ecf20Sopenharmony_ci#include <limits.h>
228c2ecf20Sopenharmony_ci#include <stdlib.h>
238c2ecf20Sopenharmony_ci#include <string.h>
248c2ecf20Sopenharmony_ci#include <cpuid.h>
258c2ecf20Sopenharmony_ci#include <dirent.h>
268c2ecf20Sopenharmony_ci#include <errno.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <stdarg.h>
298c2ecf20Sopenharmony_ci#include <sys/ioctl.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define BIT(x) (1 << (x))
328c2ecf20Sopenharmony_ci#define BIT_ULL(nr) (1ULL << (nr))
338c2ecf20Sopenharmony_ci#define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (sizeof(long) * 8 - 1 - (h))))
348c2ecf20Sopenharmony_ci#define GENMASK_ULL(h, l)                                                      \
358c2ecf20Sopenharmony_ci	(((~0ULL) << (l)) & (~0ULL >> (sizeof(long long) * 8 - 1 - (h))))
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define CONFIG_TDP				0x7f
388c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_LEVELS_INFO		0x00
398c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_TDP_CONTROL		0x01
408c2ecf20Sopenharmony_ci#define CONFIG_TDP_SET_TDP_CONTROL		0x02
418c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_TDP_INFO			0x03
428c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_PWR_INFO			0x04
438c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_TJMAX_INFO		0x05
448c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_CORE_MASK		0x06
458c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_TURBO_LIMIT_RATIOS	0x07
468c2ecf20Sopenharmony_ci#define CONFIG_TDP_SET_LEVEL			0x08
478c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_UNCORE_P0_P1_INFO	0X09
488c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_P1_INFO			0x0a
498c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_MEM_FREQ			0x0b
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES	0x10
528c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS	0x11
538c2ecf20Sopenharmony_ci#define CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO		0x12
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define CONFIG_TDP_PBF_GET_CORE_MASK_INFO	0x20
568c2ecf20Sopenharmony_ci#define CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO	0x21
578c2ecf20Sopenharmony_ci#define CONFIG_TDP_PBF_GET_TJ_MAX_INFO		0x22
588c2ecf20Sopenharmony_ci#define CONFIG_TDP_PBF_GET_TDP_INFO		0X23
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define CONFIG_CLOS				0xd0
618c2ecf20Sopenharmony_ci#define CLOS_PQR_ASSOC				0x00
628c2ecf20Sopenharmony_ci#define CLOS_PM_CLOS				0x01
638c2ecf20Sopenharmony_ci#define CLOS_PM_QOS_CONFIG			0x02
648c2ecf20Sopenharmony_ci#define CLOS_STATUS				0x03
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define MBOX_CMD_WRITE_BIT			0x08
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define PM_QOS_INFO_OFFSET			0x00
698c2ecf20Sopenharmony_ci#define PM_QOS_CONFIG_OFFSET			0x04
708c2ecf20Sopenharmony_ci#define PM_CLOS_OFFSET				0x08
718c2ecf20Sopenharmony_ci#define PQR_ASSOC_OFFSET			0x20
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define READ_PM_CONFIG				0x94
748c2ecf20Sopenharmony_ci#define WRITE_PM_CONFIG				0x95
758c2ecf20Sopenharmony_ci#define PM_FEATURE				0x03
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define DISP_FREQ_MULTIPLIER 100
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistruct isst_clos_config {
808c2ecf20Sopenharmony_ci	int pkg_id;
818c2ecf20Sopenharmony_ci	int die_id;
828c2ecf20Sopenharmony_ci	unsigned char epp;
838c2ecf20Sopenharmony_ci	unsigned char clos_prop_prio;
848c2ecf20Sopenharmony_ci	unsigned char clos_min;
858c2ecf20Sopenharmony_ci	unsigned char clos_max;
868c2ecf20Sopenharmony_ci	unsigned char clos_desired;
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistruct isst_fact_bucket_info {
908c2ecf20Sopenharmony_ci	int high_priority_cores_count;
918c2ecf20Sopenharmony_ci	int sse_trl;
928c2ecf20Sopenharmony_ci	int avx_trl;
938c2ecf20Sopenharmony_ci	int avx512_trl;
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistruct isst_pbf_info {
978c2ecf20Sopenharmony_ci	int pbf_acticated;
988c2ecf20Sopenharmony_ci	int pbf_available;
998c2ecf20Sopenharmony_ci	size_t core_cpumask_size;
1008c2ecf20Sopenharmony_ci	cpu_set_t *core_cpumask;
1018c2ecf20Sopenharmony_ci	int p1_high;
1028c2ecf20Sopenharmony_ci	int p1_low;
1038c2ecf20Sopenharmony_ci	int t_control;
1048c2ecf20Sopenharmony_ci	int t_prochot;
1058c2ecf20Sopenharmony_ci	int tdp;
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define ISST_TRL_MAX_ACTIVE_CORES	8
1098c2ecf20Sopenharmony_ci#define ISST_FACT_MAX_BUCKETS		8
1108c2ecf20Sopenharmony_cistruct isst_fact_info {
1118c2ecf20Sopenharmony_ci	int lp_clipping_ratio_license_sse;
1128c2ecf20Sopenharmony_ci	int lp_clipping_ratio_license_avx2;
1138c2ecf20Sopenharmony_ci	int lp_clipping_ratio_license_avx512;
1148c2ecf20Sopenharmony_ci	struct isst_fact_bucket_info bucket_info[ISST_FACT_MAX_BUCKETS];
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistruct isst_pkg_ctdp_level_info {
1188c2ecf20Sopenharmony_ci	int processed;
1198c2ecf20Sopenharmony_ci	int control_cpu;
1208c2ecf20Sopenharmony_ci	int pkg_id;
1218c2ecf20Sopenharmony_ci	int die_id;
1228c2ecf20Sopenharmony_ci	int level;
1238c2ecf20Sopenharmony_ci	int fact_support;
1248c2ecf20Sopenharmony_ci	int pbf_support;
1258c2ecf20Sopenharmony_ci	int fact_enabled;
1268c2ecf20Sopenharmony_ci	int pbf_enabled;
1278c2ecf20Sopenharmony_ci	int sst_cp_support;
1288c2ecf20Sopenharmony_ci	int sst_cp_enabled;
1298c2ecf20Sopenharmony_ci	int tdp_ratio;
1308c2ecf20Sopenharmony_ci	int active;
1318c2ecf20Sopenharmony_ci	int tdp_control;
1328c2ecf20Sopenharmony_ci	int pkg_tdp;
1338c2ecf20Sopenharmony_ci	int pkg_min_power;
1348c2ecf20Sopenharmony_ci	int pkg_max_power;
1358c2ecf20Sopenharmony_ci	int fact;
1368c2ecf20Sopenharmony_ci	int t_proc_hot;
1378c2ecf20Sopenharmony_ci	int uncore_p0;
1388c2ecf20Sopenharmony_ci	int uncore_p1;
1398c2ecf20Sopenharmony_ci	int sse_p1;
1408c2ecf20Sopenharmony_ci	int avx2_p1;
1418c2ecf20Sopenharmony_ci	int avx512_p1;
1428c2ecf20Sopenharmony_ci	int mem_freq;
1438c2ecf20Sopenharmony_ci	size_t core_cpumask_size;
1448c2ecf20Sopenharmony_ci	cpu_set_t *core_cpumask;
1458c2ecf20Sopenharmony_ci	int cpu_count;
1468c2ecf20Sopenharmony_ci	unsigned long long buckets_info;
1478c2ecf20Sopenharmony_ci	int trl_sse_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
1488c2ecf20Sopenharmony_ci	int trl_avx_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
1498c2ecf20Sopenharmony_ci	int trl_avx_512_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
1508c2ecf20Sopenharmony_ci	int kobj_bucket_index;
1518c2ecf20Sopenharmony_ci	int active_bucket;
1528c2ecf20Sopenharmony_ci	int fact_max_index;
1538c2ecf20Sopenharmony_ci	int fact_max_config;
1548c2ecf20Sopenharmony_ci	int pbf_found;
1558c2ecf20Sopenharmony_ci	int pbf_active;
1568c2ecf20Sopenharmony_ci	struct isst_pbf_info pbf_info;
1578c2ecf20Sopenharmony_ci	struct isst_fact_info fact_info;
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define ISST_MAX_TDP_LEVELS	(4 + 1) /* +1 for base config */
1618c2ecf20Sopenharmony_cistruct isst_pkg_ctdp {
1628c2ecf20Sopenharmony_ci	int locked;
1638c2ecf20Sopenharmony_ci	int version;
1648c2ecf20Sopenharmony_ci	int processed;
1658c2ecf20Sopenharmony_ci	int levels;
1668c2ecf20Sopenharmony_ci	int current_level;
1678c2ecf20Sopenharmony_ci	int enabled;
1688c2ecf20Sopenharmony_ci	struct isst_pkg_ctdp_level_info ctdp_level[ISST_MAX_TDP_LEVELS];
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciextern int get_topo_max_cpus(void);
1728c2ecf20Sopenharmony_ciextern int get_cpu_count(int pkg_id, int die_id);
1738c2ecf20Sopenharmony_ciextern int get_max_punit_core_id(int pkg_id, int die_id);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/* Common interfaces */
1768c2ecf20Sopenharmony_ciFILE *get_output_file(void);
1778c2ecf20Sopenharmony_ciextern void debug_printf(const char *format, ...);
1788c2ecf20Sopenharmony_ciextern int out_format_is_json(void);
1798c2ecf20Sopenharmony_ciextern int get_physical_package_id(int cpu);
1808c2ecf20Sopenharmony_ciextern int get_physical_die_id(int cpu);
1818c2ecf20Sopenharmony_ciextern size_t alloc_cpu_set(cpu_set_t **cpu_set);
1828c2ecf20Sopenharmony_ciextern void free_cpu_set(cpu_set_t *cpu_set);
1838c2ecf20Sopenharmony_ciextern int find_logical_cpu(int pkg_id, int die_id, int phy_cpu);
1848c2ecf20Sopenharmony_ciextern int find_phy_cpu_num(int logical_cpu);
1858c2ecf20Sopenharmony_ciextern int find_phy_core_num(int logical_cpu);
1868c2ecf20Sopenharmony_ciextern void set_cpu_mask_from_punit_coremask(int cpu,
1878c2ecf20Sopenharmony_ci					     unsigned long long core_mask,
1888c2ecf20Sopenharmony_ci					     size_t core_cpumask_size,
1898c2ecf20Sopenharmony_ci					     cpu_set_t *core_cpumask,
1908c2ecf20Sopenharmony_ci					     int *cpu_cnt);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciextern int isst_send_mbox_command(unsigned int cpu, unsigned char command,
1938c2ecf20Sopenharmony_ci				  unsigned char sub_command,
1948c2ecf20Sopenharmony_ci				  unsigned int write,
1958c2ecf20Sopenharmony_ci				  unsigned int req_data, unsigned int *resp);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciextern int isst_send_msr_command(unsigned int cpu, unsigned int command,
1988c2ecf20Sopenharmony_ci				 int write, unsigned long long *req_resp);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ciextern int isst_get_ctdp_levels(int cpu, struct isst_pkg_ctdp *pkg_dev);
2018c2ecf20Sopenharmony_ciextern int isst_get_ctdp_control(int cpu, int config_index,
2028c2ecf20Sopenharmony_ci				 struct isst_pkg_ctdp_level_info *ctdp_level);
2038c2ecf20Sopenharmony_ciextern int isst_get_coremask_info(int cpu, int config_index,
2048c2ecf20Sopenharmony_ci			   struct isst_pkg_ctdp_level_info *ctdp_level);
2058c2ecf20Sopenharmony_ciextern int isst_get_process_ctdp(int cpu, int tdp_level,
2068c2ecf20Sopenharmony_ci				 struct isst_pkg_ctdp *pkg_dev);
2078c2ecf20Sopenharmony_ciextern void isst_get_process_ctdp_complete(int cpu,
2088c2ecf20Sopenharmony_ci					   struct isst_pkg_ctdp *pkg_dev);
2098c2ecf20Sopenharmony_ciextern void isst_ctdp_display_information(int cpu, FILE *outf, int tdp_level,
2108c2ecf20Sopenharmony_ci					  struct isst_pkg_ctdp *pkg_dev);
2118c2ecf20Sopenharmony_ciextern void isst_ctdp_display_core_info(int cpu, FILE *outf, char *prefix,
2128c2ecf20Sopenharmony_ci					unsigned int val, char *str0, char *str1);
2138c2ecf20Sopenharmony_ciextern void isst_ctdp_display_information_start(FILE *outf);
2148c2ecf20Sopenharmony_ciextern void isst_ctdp_display_information_end(FILE *outf);
2158c2ecf20Sopenharmony_ciextern void isst_pbf_display_information(int cpu, FILE *outf, int level,
2168c2ecf20Sopenharmony_ci					 struct isst_pbf_info *info);
2178c2ecf20Sopenharmony_ciextern int isst_set_tdp_level(int cpu, int tdp_level);
2188c2ecf20Sopenharmony_ciextern int isst_set_tdp_level_msr(int cpu, int tdp_level);
2198c2ecf20Sopenharmony_ciextern int isst_set_pbf_fact_status(int cpu, int pbf, int enable);
2208c2ecf20Sopenharmony_ciextern int isst_get_pbf_info(int cpu, int level,
2218c2ecf20Sopenharmony_ci			     struct isst_pbf_info *pbf_info);
2228c2ecf20Sopenharmony_ciextern void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info);
2238c2ecf20Sopenharmony_ciextern int isst_get_fact_info(int cpu, int level, int fact_bucket,
2248c2ecf20Sopenharmony_ci			      struct isst_fact_info *fact_info);
2258c2ecf20Sopenharmony_ciextern int isst_get_fact_bucket_info(int cpu, int level,
2268c2ecf20Sopenharmony_ci				     struct isst_fact_bucket_info *bucket_info);
2278c2ecf20Sopenharmony_ciextern void isst_fact_display_information(int cpu, FILE *outf, int level,
2288c2ecf20Sopenharmony_ci					  int fact_bucket, int fact_avx,
2298c2ecf20Sopenharmony_ci					  struct isst_fact_info *fact_info);
2308c2ecf20Sopenharmony_ciextern int isst_set_trl(int cpu, unsigned long long trl);
2318c2ecf20Sopenharmony_ciextern int isst_set_trl_from_current_tdp(int cpu, unsigned long long trl);
2328c2ecf20Sopenharmony_ciextern int isst_get_config_tdp_lock_status(int cpu);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ciextern int isst_pm_qos_config(int cpu, int enable_clos, int priority_type);
2358c2ecf20Sopenharmony_ciextern int isst_pm_get_clos(int cpu, int clos,
2368c2ecf20Sopenharmony_ci			    struct isst_clos_config *clos_config);
2378c2ecf20Sopenharmony_ciextern int isst_set_clos(int cpu, int clos,
2388c2ecf20Sopenharmony_ci			 struct isst_clos_config *clos_config);
2398c2ecf20Sopenharmony_ciextern int isst_clos_associate(int cpu, int clos);
2408c2ecf20Sopenharmony_ciextern int isst_clos_get_assoc_status(int cpu, int *clos_id);
2418c2ecf20Sopenharmony_ciextern void isst_clos_display_information(int cpu, FILE *outf, int clos,
2428c2ecf20Sopenharmony_ci					  struct isst_clos_config *clos_config);
2438c2ecf20Sopenharmony_ciextern void isst_clos_display_assoc_information(int cpu, FILE *outf, int clos);
2448c2ecf20Sopenharmony_ciextern int isst_read_reg(unsigned short reg, unsigned int *val);
2458c2ecf20Sopenharmony_ciextern int isst_write_reg(int reg, unsigned int val);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ciextern void isst_display_result(int cpu, FILE *outf, char *feature, char *cmd,
2488c2ecf20Sopenharmony_ci				int result);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ciextern int isst_clos_get_clos_information(int cpu, int *enable, int *type);
2518c2ecf20Sopenharmony_ciextern void isst_clos_display_clos_information(int cpu, FILE *outf,
2528c2ecf20Sopenharmony_ci					       int clos_enable, int type,
2538c2ecf20Sopenharmony_ci					       int state, int cap);
2548c2ecf20Sopenharmony_ciextern int is_clx_n_platform(void);
2558c2ecf20Sopenharmony_ciextern int get_cpufreq_base_freq(int cpu);
2568c2ecf20Sopenharmony_ciextern int isst_read_pm_config(int cpu, int *cp_state, int *cp_cap);
2578c2ecf20Sopenharmony_ciextern void isst_display_error_info_message(int error, char *msg, int arg_valid, int arg);
2588c2ecf20Sopenharmony_ci#endif
259