| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Intel Speed Select -- Enumerate and control features |
| 4 | * Copyright (c) 2019 Intel Corporation. |
| 5 | */ |
| 6 | |
| 7 | #ifndef _ISST_H_ |
| 8 | #define _ISST_H_ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <unistd.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sched.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/resource.h> |
| 16 | #include <getopt.h> |
| 17 | #include <err.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <signal.h> |
| 20 | #include <sys/time.h> |
| 21 | #include <limits.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <cpuid.h> |
| 25 | #include <dirent.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include <stdarg.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | |
| 31 | #include <linux/isst_if.h> |
| 32 | |
| 33 | #define BIT(x) (1 << (x)) |
| 34 | #define BIT_ULL(nr) (1ULL << (nr)) |
| 35 | #define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (sizeof(long) * 8 - 1 - (h)))) |
| 36 | #define GENMASK_ULL(h, l) \ |
| 37 | (((~0ULL) << (l)) & (~0ULL >> (sizeof(long long) * 8 - 1 - (h)))) |
| 38 | |
| 39 | #define CONFIG_TDP 0x7f |
| 40 | #define CONFIG_TDP_GET_LEVELS_INFO 0x00 |
| 41 | #define CONFIG_TDP_GET_TDP_CONTROL 0x01 |
| 42 | #define CONFIG_TDP_SET_TDP_CONTROL 0x02 |
| 43 | #define CONFIG_TDP_GET_TDP_INFO 0x03 |
| 44 | #define CONFIG_TDP_GET_PWR_INFO 0x04 |
| 45 | #define CONFIG_TDP_GET_TJMAX_INFO 0x05 |
| 46 | #define CONFIG_TDP_GET_CORE_MASK 0x06 |
| 47 | #define CONFIG_TDP_GET_TURBO_LIMIT_RATIOS 0x07 |
| 48 | #define CONFIG_TDP_SET_LEVEL 0x08 |
| 49 | #define CONFIG_TDP_GET_UNCORE_P0_P1_INFO 0X09 |
| 50 | #define CONFIG_TDP_GET_P1_INFO 0x0a |
| 51 | #define CONFIG_TDP_GET_MEM_FREQ 0x0b |
| 52 | #define CONFIG_TDP_GET_RATIO_INFO 0x0c |
| 53 | |
| 54 | #define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES 0x10 |
| 55 | #define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS 0x11 |
| 56 | #define CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO 0x12 |
| 57 | |
| 58 | #define CONFIG_TDP_PBF_GET_CORE_MASK_INFO 0x20 |
| 59 | #define CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO 0x21 |
| 60 | #define CONFIG_TDP_PBF_GET_TJ_MAX_INFO 0x22 |
| 61 | #define CONFIG_TDP_PBF_GET_TDP_INFO 0X23 |
| 62 | |
| 63 | #define CONFIG_CLOS 0xd0 |
| 64 | #define CLOS_PQR_ASSOC 0x00 |
| 65 | #define CLOS_PM_CLOS 0x01 |
| 66 | #define CLOS_PM_QOS_CONFIG 0x02 |
| 67 | #define CLOS_STATUS 0x03 |
| 68 | |
| 69 | #define MBOX_CMD_WRITE_BIT 0x08 |
| 70 | |
| 71 | #define PM_QOS_INFO_OFFSET 0x00 |
| 72 | #define PM_QOS_CONFIG_OFFSET 0x04 |
| 73 | #define PM_CLOS_OFFSET 0x08 |
| 74 | #define PQR_ASSOC_OFFSET 0x20 |
| 75 | |
| 76 | #define READ_PM_CONFIG 0x94 |
| 77 | #define WRITE_PM_CONFIG 0x95 |
| 78 | #define PM_FEATURE 0x03 |
| 79 | |
| 80 | #define DISP_FREQ_MULTIPLIER 100 |
| 81 | |
| 82 | #define MAX_PACKAGE_COUNT 32 |
| 83 | #define MAX_DIE_PER_PACKAGE 16 |
| 84 | #define MAX_PUNIT_PER_DIE 8 |
| 85 | |
| 86 | /* Unified structure to specific a CPU or a Power Domain */ |
| 87 | struct isst_id { |
| 88 | int cpu; |
| 89 | int pkg; |
| 90 | int die; |
| 91 | int punit; |
| 92 | }; |
| 93 | |
| 94 | struct isst_clos_config { |
| 95 | unsigned int clos_min; |
| 96 | unsigned int clos_max; |
| 97 | unsigned char epp; |
| 98 | unsigned char clos_prop_prio; |
| 99 | unsigned char clos_desired; |
| 100 | }; |
| 101 | |
| 102 | struct isst_fact_bucket_info { |
| 103 | int hp_cores; |
| 104 | int hp_ratios[TRL_MAX_LEVELS]; |
| 105 | }; |
| 106 | |
| 107 | struct isst_pbf_info { |
| 108 | int pbf_acticated; |
| 109 | int pbf_available; |
| 110 | size_t core_cpumask_size; |
| 111 | cpu_set_t *core_cpumask; |
| 112 | int p1_high; |
| 113 | int p1_low; |
| 114 | int t_control; |
| 115 | int t_prochot; |
| 116 | int tdp; |
| 117 | }; |
| 118 | |
| 119 | #define ISST_TRL_MAX_ACTIVE_CORES 8 |
| 120 | #define ISST_FACT_MAX_BUCKETS 8 |
| 121 | struct isst_fact_info { |
| 122 | int lp_ratios[TRL_MAX_LEVELS]; |
| 123 | struct isst_fact_bucket_info bucket_info[ISST_FACT_MAX_BUCKETS]; |
| 124 | }; |
| 125 | |
| 126 | struct isst_pkg_ctdp_level_info { |
| 127 | int processed; |
| 128 | int control_cpu; |
| 129 | int pkg_id; |
| 130 | int die_id; |
| 131 | int level; |
| 132 | int fact_support; |
| 133 | int pbf_support; |
| 134 | int fact_enabled; |
| 135 | int pbf_enabled; |
| 136 | int sst_cp_support; |
| 137 | int sst_cp_enabled; |
| 138 | int tdp_ratio; |
| 139 | int active; |
| 140 | int tdp_control; |
| 141 | int pkg_tdp; |
| 142 | int pkg_min_power; |
| 143 | int pkg_max_power; |
| 144 | int fact; |
| 145 | int t_proc_hot; |
| 146 | int cooling_type; |
| 147 | int uncore_p0; |
| 148 | int uncore_p1; |
| 149 | int uncore_pm; |
| 150 | int uncore1_p0; |
| 151 | int uncore1_p1; |
| 152 | int uncore1_pm; |
| 153 | int sse_p1; |
| 154 | int avx2_p1; |
| 155 | int avx512_p1; |
| 156 | int amx_p1; |
| 157 | int mem_freq; |
| 158 | size_t core_cpumask_size; |
| 159 | cpu_set_t *core_cpumask; |
| 160 | int cpu_count; |
| 161 | unsigned long long trl_cores; /* Buckets info */ |
| 162 | int trl_ratios[TRL_MAX_LEVELS][ISST_TRL_MAX_ACTIVE_CORES]; |
| 163 | int kobj_bucket_index; |
| 164 | int active_bucket; |
| 165 | int fact_max_index; |
| 166 | int fact_max_config; |
| 167 | int pbf_found; |
| 168 | int pbf_active; |
| 169 | struct isst_pbf_info pbf_info; |
| 170 | struct isst_fact_info fact_info; |
| 171 | }; |
| 172 | |
| 173 | #define ISST_MAX_TDP_LEVELS (4 + 1) /* +1 for base config */ |
| 174 | struct isst_pkg_ctdp { |
| 175 | int locked; |
| 176 | int version; |
| 177 | int processed; |
| 178 | int levels; |
| 179 | int current_level; |
| 180 | int enabled; |
| 181 | struct isst_pkg_ctdp_level_info ctdp_level[ISST_MAX_TDP_LEVELS]; |
| 182 | }; |
| 183 | |
| 184 | enum isst_platform_param { |
| 185 | ISST_PARAM_MBOX_DELAY, |
| 186 | ISST_PARAM_MBOX_RETRIES, |
| 187 | }; |
| 188 | |
| 189 | struct isst_platform_ops { |
| 190 | int (*get_disp_freq_multiplier)(void); |
| 191 | int (*get_trl_max_levels)(void); |
| 192 | char *(*get_trl_level_name)(int level); |
| 193 | void (*update_platform_param)(enum isst_platform_param param, int value); |
| 194 | int (*is_punit_valid)(struct isst_id *id); |
| 195 | int (*read_pm_config)(struct isst_id *id, int *cp_state, int *cp_cap); |
| 196 | int (*get_config_levels)(struct isst_id *id, struct isst_pkg_ctdp *pkg_ctdp); |
| 197 | int (*get_ctdp_control)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level); |
| 198 | int (*get_tdp_info)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level); |
| 199 | int (*get_pwr_info)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level); |
| 200 | int (*get_coremask_info)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level); |
| 201 | int (*get_get_trl)(struct isst_id *id, int level, int avx_level, int *trl); |
| 202 | int (*get_get_trls)(struct isst_id *id, int level, struct isst_pkg_ctdp_level_info *ctdp_level); |
| 203 | int (*get_trl_bucket_info)(struct isst_id *id, int level, unsigned long long *buckets_info); |
| 204 | int (*set_tdp_level)(struct isst_id *id, int tdp_level); |
| 205 | int (*get_pbf_info)(struct isst_id *id, int level, struct isst_pbf_info *pbf_info); |
| 206 | int (*set_pbf_fact_status)(struct isst_id *id, int pbf, int enable); |
| 207 | int (*get_fact_info)(struct isst_id *id, int level, int fact_bucket, struct isst_fact_info *fact_info); |
| 208 | void (*adjust_uncore_freq)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level); |
| 209 | int (*get_clos_information)(struct isst_id *id, int *enable, int *type); |
| 210 | int (*pm_qos_config)(struct isst_id *id, int enable_clos, int priority_type); |
| 211 | int (*pm_get_clos)(struct isst_id *id, int clos, struct isst_clos_config *clos_config); |
| 212 | int (*set_clos)(struct isst_id *id, int clos, struct isst_clos_config *clos_config); |
| 213 | int (*clos_get_assoc_status)(struct isst_id *id, int *clos_id); |
| 214 | int (*clos_associate)(struct isst_id *id, int clos_id); |
| 215 | }; |
| 216 | |
| 217 | extern int is_cpu_in_power_domain(int cpu, struct isst_id *id); |
| 218 | extern int get_topo_max_cpus(void); |
| 219 | extern int get_cpu_count(struct isst_id *id); |
| 220 | extern int get_max_punit_core_id(struct isst_id *id); |
| 221 | extern int api_version(void); |
| 222 | |
| 223 | /* Common interfaces */ |
| 224 | FILE *get_output_file(void); |
| 225 | extern int is_debug_enabled(void); |
| 226 | extern void debug_printf(const char *format, ...); |
| 227 | extern int out_format_is_json(void); |
| 228 | extern void set_isst_id(struct isst_id *id, int cpu); |
| 229 | extern size_t alloc_cpu_set(cpu_set_t **cpu_set); |
| 230 | extern void free_cpu_set(cpu_set_t *cpu_set); |
| 231 | extern int find_phy_core_num(int logical_cpu); |
| 232 | extern void set_cpu_mask_from_punit_coremask(struct isst_id *id, |
| 233 | unsigned long long core_mask, |
| 234 | size_t core_cpumask_size, |
| 235 | cpu_set_t *core_cpumask, |
| 236 | int *cpu_cnt); |
| 237 | extern int isst_send_msr_command(unsigned int cpu, unsigned int command, |
| 238 | int write, unsigned long long *req_resp); |
| 239 | |
| 240 | extern int isst_set_platform_ops(int api_version); |
| 241 | extern void isst_update_platform_param(enum isst_platform_param, int vale); |
| 242 | extern int isst_get_disp_freq_multiplier(void); |
| 243 | extern int isst_get_trl_max_levels(void); |
| 244 | extern char *isst_get_trl_level_name(int level); |
| 245 | extern int isst_is_punit_valid(struct isst_id *id); |
| 246 | |
| 247 | extern int isst_get_ctdp_levels(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev); |
| 248 | extern int isst_get_ctdp_control(struct isst_id *id, int config_index, |
| 249 | struct isst_pkg_ctdp_level_info *ctdp_level); |
| 250 | extern int isst_get_coremask_info(struct isst_id *id, int config_index, |
| 251 | struct isst_pkg_ctdp_level_info *ctdp_level); |
| 252 | extern void isst_adjust_uncore_freq(struct isst_id *id, int config_index, |
| 253 | struct isst_pkg_ctdp_level_info *ctdp_level); |
| 254 | extern int isst_get_process_ctdp(struct isst_id *id, int tdp_level, |
| 255 | struct isst_pkg_ctdp *pkg_dev); |
| 256 | extern void isst_get_process_ctdp_complete(struct isst_id *id, |
| 257 | struct isst_pkg_ctdp *pkg_dev); |
| 258 | extern void isst_ctdp_display_information(struct isst_id *id, FILE *outf, int tdp_level, |
| 259 | struct isst_pkg_ctdp *pkg_dev); |
| 260 | extern void isst_ctdp_display_core_info(struct isst_id *id, FILE *outf, char *prefix, |
| 261 | unsigned int val, char *str0, char *str1); |
| 262 | extern void isst_ctdp_display_information_start(FILE *outf); |
| 263 | extern void isst_ctdp_display_information_end(FILE *outf); |
| 264 | extern void isst_pbf_display_information(struct isst_id *id, FILE *outf, int level, |
| 265 | struct isst_pbf_info *info); |
| 266 | extern int isst_set_tdp_level(struct isst_id *id, int tdp_level); |
| 267 | extern int isst_set_pbf_fact_status(struct isst_id *id, int pbf, int enable); |
| 268 | extern int isst_get_pbf_info(struct isst_id *id, int level, |
| 269 | struct isst_pbf_info *pbf_info); |
| 270 | extern int isst_get_fact_info(struct isst_id *id, int level, int fact_bucket, |
| 271 | struct isst_fact_info *fact_info); |
| 272 | extern void isst_fact_display_information(struct isst_id *id, FILE *outf, int level, |
| 273 | int fact_bucket, int fact_avx, |
| 274 | struct isst_fact_info *fact_info); |
| 275 | extern int isst_set_trl(struct isst_id *id, unsigned long long trl); |
| 276 | extern int isst_get_trl(struct isst_id *id, unsigned long long *trl); |
| 277 | extern int isst_set_trl_from_current_tdp(struct isst_id *id, unsigned long long trl); |
| 278 | extern int isst_get_config_tdp_lock_status(struct isst_id *id); |
| 279 | |
| 280 | extern int isst_pm_qos_config(struct isst_id *id, int enable_clos, int priority_type); |
| 281 | extern int isst_pm_get_clos(struct isst_id *id, int clos, |
| 282 | struct isst_clos_config *clos_config); |
| 283 | extern int isst_set_clos(struct isst_id *id, int clos, |
| 284 | struct isst_clos_config *clos_config); |
| 285 | extern int isst_clos_associate(struct isst_id *id, int clos); |
| 286 | extern int isst_clos_get_assoc_status(struct isst_id *id, int *clos_id); |
| 287 | extern void isst_clos_display_information(struct isst_id *id, FILE *outf, int clos, |
| 288 | struct isst_clos_config *clos_config); |
| 289 | extern void isst_clos_display_assoc_information(struct isst_id *id, FILE *outf, int clos); |
| 290 | |
| 291 | extern void isst_display_result(struct isst_id *id, FILE *outf, char *feature, char *cmd, |
| 292 | int result); |
| 293 | |
| 294 | extern int isst_clos_get_clos_information(struct isst_id *id, int *enable, int *type); |
| 295 | extern void isst_clos_display_clos_information(struct isst_id *id, FILE *outf, |
| 296 | int clos_enable, int type, |
| 297 | int state, int cap); |
| 298 | extern int is_clx_n_platform(void); |
| 299 | extern int get_cpufreq_base_freq(int cpu); |
| 300 | extern int isst_read_pm_config(struct isst_id *id, int *cp_state, int *cp_cap); |
| 301 | extern void isst_display_error_info_message(int error, char *msg, int arg_valid, int arg); |
| 302 | extern int is_skx_based_platform(void); |
| 303 | extern int is_spr_platform(void); |
| 304 | extern int is_emr_platform(void); |
| 305 | extern int is_icx_platform(void); |
| 306 | extern void isst_trl_display_information(struct isst_id *id, FILE *outf, unsigned long long trl); |
| 307 | |
| 308 | extern void set_cpu_online_offline(int cpu, int state); |
| 309 | extern void for_each_online_power_domain_in_set(void (*callback)(struct isst_id *, void *, void *, |
| 310 | void *, void *), |
| 311 | void *arg1, void *arg2, void *arg3, |
| 312 | void *arg4); |
| 313 | extern int isst_daemon(int debug_mode, int poll_interval, int no_daemon); |
| 314 | extern void process_level_change(struct isst_id *id); |
| 315 | extern int hfi_main(void); |
| 316 | extern void hfi_exit(void); |
| 317 | |
| 318 | /* Interface specific callbacks */ |
| 319 | extern struct isst_platform_ops *mbox_get_platform_ops(void); |
| 320 | extern struct isst_platform_ops *tpmi_get_platform_ops(void); |
| 321 | |
| 322 | /* Cgroup related interface */ |
| 323 | extern int enable_cpuset_controller(void); |
| 324 | extern int isolate_cpus(struct isst_id *id, int mask_size, cpu_set_t *cpu_mask, |
| 325 | int level, int cpu_0_only); |
| 326 | extern int use_cgroupv2(void); |
| 327 | |
| 328 | #endif |
| 329 | |