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