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	8
83 #define MAX_DIE_PER_PACKAGE	2
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 sse_p1;
151 	int avx2_p1;
152 	int avx512_p1;
153 	int amx_p1;
154 	int mem_freq;
155 	size_t core_cpumask_size;
156 	cpu_set_t *core_cpumask;
157 	int cpu_count;
158 	unsigned long long trl_cores;	/* Buckets info */
159 	int trl_ratios[TRL_MAX_LEVELS][ISST_TRL_MAX_ACTIVE_CORES];
160 	int kobj_bucket_index;
161 	int active_bucket;
162 	int fact_max_index;
163 	int fact_max_config;
164 	int pbf_found;
165 	int pbf_active;
166 	struct isst_pbf_info pbf_info;
167 	struct isst_fact_info fact_info;
168 };
169 
170 #define ISST_MAX_TDP_LEVELS	(4 + 1) /* +1 for base config */
171 struct isst_pkg_ctdp {
172 	int locked;
173 	int version;
174 	int processed;
175 	int levels;
176 	int current_level;
177 	int enabled;
178 	struct isst_pkg_ctdp_level_info ctdp_level[ISST_MAX_TDP_LEVELS];
179 };
180 
181 enum isst_platform_param {
182 	ISST_PARAM_MBOX_DELAY,
183 	ISST_PARAM_MBOX_RETRIES,
184 };
185 
186 struct isst_platform_ops {
187 	int (*get_disp_freq_multiplier)(void);
188 	int (*get_trl_max_levels)(void);
189 	char *(*get_trl_level_name)(int level);
190 	void (*update_platform_param)(enum isst_platform_param param, int value);
191 	int (*is_punit_valid)(struct isst_id *id);
192 	int (*read_pm_config)(struct isst_id *id, int *cp_state, int *cp_cap);
193 	int (*get_config_levels)(struct isst_id *id, struct isst_pkg_ctdp *pkg_ctdp);
194 	int (*get_ctdp_control)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level);
195 	int (*get_tdp_info)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level);
196 	int (*get_pwr_info)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level);
197 	int (*get_coremask_info)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level);
198 	int (*get_get_trl)(struct isst_id *id, int level, int avx_level, int *trl);
199 	int (*get_get_trls)(struct isst_id *id, int level, struct isst_pkg_ctdp_level_info *ctdp_level);
200 	int (*get_trl_bucket_info)(struct isst_id *id, int level, unsigned long long *buckets_info);
201 	int (*set_tdp_level)(struct isst_id *id, int tdp_level);
202 	int (*get_pbf_info)(struct isst_id *id, int level, struct isst_pbf_info *pbf_info);
203 	int (*set_pbf_fact_status)(struct isst_id *id, int pbf, int enable);
204 	int (*get_fact_info)(struct isst_id *id, int level, int fact_bucket, struct isst_fact_info *fact_info);
205 	void (*adjust_uncore_freq)(struct isst_id *id, int config_index, struct isst_pkg_ctdp_level_info *ctdp_level);
206 	int (*get_clos_information)(struct isst_id *id, int *enable, int *type);
207 	int (*pm_qos_config)(struct isst_id *id, int enable_clos, int priority_type);
208 	int (*pm_get_clos)(struct isst_id *id, int clos, struct isst_clos_config *clos_config);
209 	int (*set_clos)(struct isst_id *id, int clos, struct isst_clos_config *clos_config);
210 	int (*clos_get_assoc_status)(struct isst_id *id, int *clos_id);
211 	int (*clos_associate)(struct isst_id *id, int clos_id);
212 };
213 
214 extern int is_cpu_in_power_domain(int cpu, struct isst_id *id);
215 extern int get_topo_max_cpus(void);
216 extern int get_cpu_count(struct isst_id *id);
217 extern int get_max_punit_core_id(struct isst_id *id);
218 extern int api_version(void);
219 
220 /* Common interfaces */
221 FILE *get_output_file(void);
222 extern int is_debug_enabled(void);
223 extern void debug_printf(const char *format, ...);
224 extern int out_format_is_json(void);
225 extern void set_isst_id(struct isst_id *id, int cpu);
226 extern size_t alloc_cpu_set(cpu_set_t **cpu_set);
227 extern void free_cpu_set(cpu_set_t *cpu_set);
228 extern int find_phy_core_num(int logical_cpu);
229 extern void set_cpu_mask_from_punit_coremask(struct isst_id *id,
230 					     unsigned long long core_mask,
231 					     size_t core_cpumask_size,
232 					     cpu_set_t *core_cpumask,
233 					     int *cpu_cnt);
234 extern int isst_send_msr_command(unsigned int cpu, unsigned int command,
235 				 int write, unsigned long long *req_resp);
236 
237 extern int isst_set_platform_ops(int api_version);
238 extern void isst_update_platform_param(enum isst_platform_param, int vale);
239 extern int isst_get_disp_freq_multiplier(void);
240 extern int isst_get_trl_max_levels(void);
241 extern char *isst_get_trl_level_name(int level);
242 extern int isst_is_punit_valid(struct isst_id *id);
243 
244 extern int isst_get_ctdp_levels(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev);
245 extern int isst_get_ctdp_control(struct isst_id *id, int config_index,
246 				 struct isst_pkg_ctdp_level_info *ctdp_level);
247 extern int isst_get_coremask_info(struct isst_id *id, int config_index,
248 			   struct isst_pkg_ctdp_level_info *ctdp_level);
249 extern void isst_adjust_uncore_freq(struct isst_id *id, int config_index,
250 					struct isst_pkg_ctdp_level_info *ctdp_level);
251 extern int isst_get_process_ctdp(struct isst_id *id, int tdp_level,
252 				 struct isst_pkg_ctdp *pkg_dev);
253 extern void isst_get_process_ctdp_complete(struct isst_id *id,
254 					   struct isst_pkg_ctdp *pkg_dev);
255 extern void isst_ctdp_display_information(struct isst_id *id, FILE *outf, int tdp_level,
256 					  struct isst_pkg_ctdp *pkg_dev);
257 extern void isst_ctdp_display_core_info(struct isst_id *id, FILE *outf, char *prefix,
258 					unsigned int val, char *str0, char *str1);
259 extern void isst_ctdp_display_information_start(FILE *outf);
260 extern void isst_ctdp_display_information_end(FILE *outf);
261 extern void isst_pbf_display_information(struct isst_id *id, FILE *outf, int level,
262 					 struct isst_pbf_info *info);
263 extern int isst_set_tdp_level(struct isst_id *id, int tdp_level);
264 extern int isst_set_pbf_fact_status(struct isst_id *id, int pbf, int enable);
265 extern int isst_get_pbf_info(struct isst_id *id, int level,
266 			     struct isst_pbf_info *pbf_info);
267 extern int isst_get_fact_info(struct isst_id *id, int level, int fact_bucket,
268 			      struct isst_fact_info *fact_info);
269 extern void isst_fact_display_information(struct isst_id *id, FILE *outf, int level,
270 					  int fact_bucket, int fact_avx,
271 					  struct isst_fact_info *fact_info);
272 extern int isst_set_trl(struct isst_id *id, unsigned long long trl);
273 extern int isst_get_trl(struct isst_id *id, unsigned long long *trl);
274 extern int isst_set_trl_from_current_tdp(struct isst_id *id, unsigned long long trl);
275 extern int isst_get_config_tdp_lock_status(struct isst_id *id);
276 
277 extern int isst_pm_qos_config(struct isst_id *id, int enable_clos, int priority_type);
278 extern int isst_pm_get_clos(struct isst_id *id, int clos,
279 			    struct isst_clos_config *clos_config);
280 extern int isst_set_clos(struct isst_id *id, int clos,
281 			 struct isst_clos_config *clos_config);
282 extern int isst_clos_associate(struct isst_id *id, int clos);
283 extern int isst_clos_get_assoc_status(struct isst_id *id, int *clos_id);
284 extern void isst_clos_display_information(struct isst_id *id, FILE *outf, int clos,
285 					  struct isst_clos_config *clos_config);
286 extern void isst_clos_display_assoc_information(struct isst_id *id, FILE *outf, int clos);
287 
288 extern void isst_display_result(struct isst_id *id, FILE *outf, char *feature, char *cmd,
289 				int result);
290 
291 extern int isst_clos_get_clos_information(struct isst_id *id, int *enable, int *type);
292 extern void isst_clos_display_clos_information(struct isst_id *id, FILE *outf,
293 					       int clos_enable, int type,
294 					       int state, int cap);
295 extern int is_clx_n_platform(void);
296 extern int get_cpufreq_base_freq(int cpu);
297 extern int isst_read_pm_config(struct isst_id *id, int *cp_state, int *cp_cap);
298 extern void isst_display_error_info_message(int error, char *msg, int arg_valid, int arg);
299 extern int is_skx_based_platform(void);
300 extern int is_spr_platform(void);
301 extern int is_emr_platform(void);
302 extern int is_icx_platform(void);
303 extern void isst_trl_display_information(struct isst_id *id, FILE *outf, unsigned long long trl);
304 
305 extern void set_cpu_online_offline(int cpu, int state);
306 extern void for_each_online_power_domain_in_set(void (*callback)(struct isst_id *, void *, void *,
307 							    void *, void *),
308 					   void *arg1, void *arg2, void *arg3,
309 					   void *arg4);
310 extern int isst_daemon(int debug_mode, int poll_interval, int no_daemon);
311 extern void process_level_change(struct isst_id *id);
312 extern int hfi_main(void);
313 extern void hfi_exit(void);
314 
315 /* Interface specific callbacks */
316 extern struct isst_platform_ops *mbox_get_platform_ops(void);
317 extern struct isst_platform_ops *tpmi_get_platform_ops(void);
318 
319 /* Cgroup related interface */
320 extern int enable_cpuset_controller(void);
321 extern int isolate_cpus(struct isst_id *id, int mask_size, cpu_set_t *cpu_mask, int level);
322 extern int use_cgroupv2(void);
323 
324 #endif
325