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 #define BIT(x) (1 << (x))
32 #define BIT_ULL(nr) (1ULL << (nr))
33 #define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (sizeof(long) * 8 - 1 - (h))))
34 #define GENMASK_ULL(h, l)                                                      \
35 	(((~0ULL) << (l)) & (~0ULL >> (sizeof(long long) * 8 - 1 - (h))))
36 
37 #define CONFIG_TDP				0x7f
38 #define CONFIG_TDP_GET_LEVELS_INFO		0x00
39 #define CONFIG_TDP_GET_TDP_CONTROL		0x01
40 #define CONFIG_TDP_SET_TDP_CONTROL		0x02
41 #define CONFIG_TDP_GET_TDP_INFO			0x03
42 #define CONFIG_TDP_GET_PWR_INFO			0x04
43 #define CONFIG_TDP_GET_TJMAX_INFO		0x05
44 #define CONFIG_TDP_GET_CORE_MASK		0x06
45 #define CONFIG_TDP_GET_TURBO_LIMIT_RATIOS	0x07
46 #define CONFIG_TDP_SET_LEVEL			0x08
47 #define CONFIG_TDP_GET_UNCORE_P0_P1_INFO	0X09
48 #define CONFIG_TDP_GET_P1_INFO			0x0a
49 #define CONFIG_TDP_GET_MEM_FREQ			0x0b
50 #define	CONFIG_TDP_GET_RATIO_INFO		0x0c
51 
52 #define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES	0x10
53 #define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS	0x11
54 #define CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO		0x12
55 
56 #define CONFIG_TDP_PBF_GET_CORE_MASK_INFO	0x20
57 #define CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO	0x21
58 #define CONFIG_TDP_PBF_GET_TJ_MAX_INFO		0x22
59 #define CONFIG_TDP_PBF_GET_TDP_INFO		0X23
60 
61 #define CONFIG_CLOS				0xd0
62 #define CLOS_PQR_ASSOC				0x00
63 #define CLOS_PM_CLOS				0x01
64 #define CLOS_PM_QOS_CONFIG			0x02
65 #define CLOS_STATUS				0x03
66 
67 #define MBOX_CMD_WRITE_BIT			0x08
68 
69 #define PM_QOS_INFO_OFFSET			0x00
70 #define PM_QOS_CONFIG_OFFSET			0x04
71 #define PM_CLOS_OFFSET				0x08
72 #define PQR_ASSOC_OFFSET			0x20
73 
74 #define READ_PM_CONFIG				0x94
75 #define WRITE_PM_CONFIG				0x95
76 #define PM_FEATURE				0x03
77 
78 #define DISP_FREQ_MULTIPLIER 100
79 
80 #define MAX_PACKAGE_COUNT 8
81 #define MAX_DIE_PER_PACKAGE 2
82 
83 /* Unified structure to specific a CPU or a Power Domain */
84 struct isst_id {
85 	int cpu;
86 	int pkg;
87 	int die;
88 };
89 
90 struct isst_clos_config {
91 	unsigned char epp;
92 	unsigned char clos_prop_prio;
93 	unsigned char clos_min;
94 	unsigned char clos_max;
95 	unsigned char clos_desired;
96 };
97 
98 struct isst_fact_bucket_info {
99 	int high_priority_cores_count;
100 	int sse_trl;
101 	int avx_trl;
102 	int avx512_trl;
103 };
104 
105 struct isst_pbf_info {
106 	int pbf_acticated;
107 	int pbf_available;
108 	size_t core_cpumask_size;
109 	cpu_set_t *core_cpumask;
110 	int p1_high;
111 	int p1_low;
112 	int t_control;
113 	int t_prochot;
114 	int tdp;
115 };
116 
117 #define ISST_TRL_MAX_ACTIVE_CORES	8
118 #define ISST_FACT_MAX_BUCKETS		8
119 struct isst_fact_info {
120 	int lp_clipping_ratio_license_sse;
121 	int lp_clipping_ratio_license_avx2;
122 	int lp_clipping_ratio_license_avx512;
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 buckets_info;
157 	int trl_sse_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
158 	int trl_avx_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
159 	int trl_avx_512_active_cores[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 extern int is_cpu_in_power_domain(int cpu, struct isst_id *id);
182 extern int get_topo_max_cpus(void);
183 extern int get_cpu_count(struct isst_id *id);
184 extern int get_max_punit_core_id(struct isst_id *id);
185 
186 /* Common interfaces */
187 FILE *get_output_file(void);
188 extern void debug_printf(const char *format, ...);
189 extern int out_format_is_json(void);
190 extern void set_isst_id(struct isst_id *id, int cpu);
191 extern size_t alloc_cpu_set(cpu_set_t **cpu_set);
192 extern void free_cpu_set(cpu_set_t *cpu_set);
193 extern int find_phy_core_num(int logical_cpu);
194 extern void set_cpu_mask_from_punit_coremask(struct isst_id *id,
195 					     unsigned long long core_mask,
196 					     size_t core_cpumask_size,
197 					     cpu_set_t *core_cpumask,
198 					     int *cpu_cnt);
199 
200 extern int isst_send_mbox_command(unsigned int cpu, unsigned char command,
201 				  unsigned char sub_command,
202 				  unsigned int write,
203 				  unsigned int req_data, unsigned int *resp);
204 
205 extern int isst_send_msr_command(unsigned int cpu, unsigned int command,
206 				 int write, unsigned long long *req_resp);
207 
208 extern int isst_get_ctdp_levels(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev);
209 extern int isst_get_ctdp_control(struct isst_id *id, int config_index,
210 				 struct isst_pkg_ctdp_level_info *ctdp_level);
211 extern int isst_get_coremask_info(struct isst_id *id, int config_index,
212 			   struct isst_pkg_ctdp_level_info *ctdp_level);
213 extern void isst_get_uncore_p0_p1_info(struct isst_id *id, int config_index,
214 					struct isst_pkg_ctdp_level_info *ctdp_level);
215 extern int isst_get_process_ctdp(struct isst_id *id, int tdp_level,
216 				 struct isst_pkg_ctdp *pkg_dev);
217 extern void isst_get_process_ctdp_complete(struct isst_id *id,
218 					   struct isst_pkg_ctdp *pkg_dev);
219 extern void isst_ctdp_display_information(struct isst_id *id, FILE *outf, int tdp_level,
220 					  struct isst_pkg_ctdp *pkg_dev);
221 extern void isst_ctdp_display_core_info(struct isst_id *id, FILE *outf, char *prefix,
222 					unsigned int val, char *str0, char *str1);
223 extern void isst_ctdp_display_information_start(FILE *outf);
224 extern void isst_ctdp_display_information_end(FILE *outf);
225 extern void isst_pbf_display_information(struct isst_id *id, FILE *outf, int level,
226 					 struct isst_pbf_info *info);
227 extern int isst_set_tdp_level(struct isst_id *id, int tdp_level);
228 extern int isst_set_pbf_fact_status(struct isst_id *id, int pbf, int enable);
229 extern int isst_get_pbf_info(struct isst_id *id, int level,
230 			     struct isst_pbf_info *pbf_info);
231 extern void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info);
232 extern int isst_get_fact_info(struct isst_id *id, int level, int fact_bucket,
233 			      struct isst_fact_info *fact_info);
234 extern int isst_get_fact_bucket_info(struct isst_id *id, int level,
235 				     struct isst_fact_bucket_info *bucket_info);
236 extern void isst_fact_display_information(struct isst_id *id, FILE *outf, int level,
237 					  int fact_bucket, int fact_avx,
238 					  struct isst_fact_info *fact_info);
239 extern int isst_set_trl(struct isst_id *id, unsigned long long trl);
240 extern int isst_get_trl(struct isst_id *id, unsigned long long *trl);
241 extern int isst_set_trl_from_current_tdp(struct isst_id *id, unsigned long long trl);
242 extern int isst_get_config_tdp_lock_status(struct isst_id *id);
243 
244 extern int isst_pm_qos_config(struct isst_id *id, int enable_clos, int priority_type);
245 extern int isst_pm_get_clos(struct isst_id *id, int clos,
246 			    struct isst_clos_config *clos_config);
247 extern int isst_set_clos(struct isst_id *id, int clos,
248 			 struct isst_clos_config *clos_config);
249 extern int isst_clos_associate(struct isst_id *id, int clos);
250 extern int isst_clos_get_assoc_status(struct isst_id *id, int *clos_id);
251 extern void isst_clos_display_information(struct isst_id *id, FILE *outf, int clos,
252 					  struct isst_clos_config *clos_config);
253 extern void isst_clos_display_assoc_information(struct isst_id *id, FILE *outf, int clos);
254 
255 extern void isst_display_result(struct isst_id *id, FILE *outf, char *feature, char *cmd,
256 				int result);
257 
258 extern int isst_clos_get_clos_information(struct isst_id *id, int *enable, int *type);
259 extern void isst_clos_display_clos_information(struct isst_id *id, FILE *outf,
260 					       int clos_enable, int type,
261 					       int state, int cap);
262 extern int is_clx_n_platform(void);
263 extern int get_cpufreq_base_freq(int cpu);
264 extern int isst_read_pm_config(struct isst_id *id, int *cp_state, int *cp_cap);
265 extern void isst_display_error_info_message(int error, char *msg, int arg_valid, int arg);
266 extern int is_skx_based_platform(void);
267 extern int is_spr_platform(void);
268 extern int is_icx_platform(void);
269 extern void isst_trl_display_information(struct isst_id *id, FILE *outf, unsigned long long trl);
270 
271 extern void set_cpu_online_offline(int cpu, int state);
272 extern void for_each_online_package_in_set(void (*callback)(struct isst_id *, void *, void *,
273 							    void *, void *),
274 					   void *arg1, void *arg2, void *arg3,
275 					   void *arg4);
276 extern int isst_daemon(int debug_mode, int poll_interval, int no_daemon);
277 extern void process_level_change(struct isst_id *id);
278 extern int hfi_main(void);
279 extern void hfi_exit(void);
280 #endif
281