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 
51 #define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES	0x10
52 #define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS	0x11
53 #define CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO		0x12
54 
55 #define CONFIG_TDP_PBF_GET_CORE_MASK_INFO	0x20
56 #define CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO	0x21
57 #define CONFIG_TDP_PBF_GET_TJ_MAX_INFO		0x22
58 #define CONFIG_TDP_PBF_GET_TDP_INFO		0X23
59 
60 #define CONFIG_CLOS				0xd0
61 #define CLOS_PQR_ASSOC				0x00
62 #define CLOS_PM_CLOS				0x01
63 #define CLOS_PM_QOS_CONFIG			0x02
64 #define CLOS_STATUS				0x03
65 
66 #define MBOX_CMD_WRITE_BIT			0x08
67 
68 #define PM_QOS_INFO_OFFSET			0x00
69 #define PM_QOS_CONFIG_OFFSET			0x04
70 #define PM_CLOS_OFFSET				0x08
71 #define PQR_ASSOC_OFFSET			0x20
72 
73 #define READ_PM_CONFIG				0x94
74 #define WRITE_PM_CONFIG				0x95
75 #define PM_FEATURE				0x03
76 
77 #define DISP_FREQ_MULTIPLIER 100
78 
79 #define MAX_PACKAGE_COUNT 8
80 #define MAX_DIE_PER_PACKAGE 2
81 
82 /* Unified structure to specific a CPU or a Power Domain */
83 struct isst_id {
84 	int cpu;
85 	int pkg;
86 	int die;
87 };
88 
89 struct isst_clos_config {
90 	unsigned char epp;
91 	unsigned char clos_prop_prio;
92 	unsigned char clos_min;
93 	unsigned char clos_max;
94 	unsigned char clos_desired;
95 };
96 
97 struct isst_fact_bucket_info {
98 	int high_priority_cores_count;
99 	int sse_trl;
100 	int avx_trl;
101 	int avx512_trl;
102 };
103 
104 struct isst_pbf_info {
105 	int pbf_acticated;
106 	int pbf_available;
107 	size_t core_cpumask_size;
108 	cpu_set_t *core_cpumask;
109 	int p1_high;
110 	int p1_low;
111 	int t_control;
112 	int t_prochot;
113 	int tdp;
114 };
115 
116 #define ISST_TRL_MAX_ACTIVE_CORES	8
117 #define ISST_FACT_MAX_BUCKETS		8
118 struct isst_fact_info {
119 	int lp_clipping_ratio_license_sse;
120 	int lp_clipping_ratio_license_avx2;
121 	int lp_clipping_ratio_license_avx512;
122 	struct isst_fact_bucket_info bucket_info[ISST_FACT_MAX_BUCKETS];
123 };
124 
125 struct isst_pkg_ctdp_level_info {
126 	int processed;
127 	int control_cpu;
128 	int pkg_id;
129 	int die_id;
130 	int level;
131 	int fact_support;
132 	int pbf_support;
133 	int fact_enabled;
134 	int pbf_enabled;
135 	int sst_cp_support;
136 	int sst_cp_enabled;
137 	int tdp_ratio;
138 	int active;
139 	int tdp_control;
140 	int pkg_tdp;
141 	int pkg_min_power;
142 	int pkg_max_power;
143 	int fact;
144 	int t_proc_hot;
145 	int uncore_p0;
146 	int uncore_p1;
147 	int sse_p1;
148 	int avx2_p1;
149 	int avx512_p1;
150 	int mem_freq;
151 	size_t core_cpumask_size;
152 	cpu_set_t *core_cpumask;
153 	int cpu_count;
154 	unsigned long long buckets_info;
155 	int trl_sse_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
156 	int trl_avx_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
157 	int trl_avx_512_active_cores[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 extern int is_cpu_in_power_domain(int cpu, struct isst_id *id);
180 extern int get_topo_max_cpus(void);
181 extern int get_cpu_count(struct isst_id *id);
182 extern int get_max_punit_core_id(struct isst_id *id);
183 
184 /* Common interfaces */
185 FILE *get_output_file(void);
186 extern void debug_printf(const char *format, ...);
187 extern int out_format_is_json(void);
188 extern void set_isst_id(struct isst_id *id, int cpu);
189 extern size_t alloc_cpu_set(cpu_set_t **cpu_set);
190 extern void free_cpu_set(cpu_set_t *cpu_set);
191 extern int find_phy_core_num(int logical_cpu);
192 extern void set_cpu_mask_from_punit_coremask(struct isst_id *id,
193 					     unsigned long long core_mask,
194 					     size_t core_cpumask_size,
195 					     cpu_set_t *core_cpumask,
196 					     int *cpu_cnt);
197 
198 extern int isst_send_mbox_command(unsigned int cpu, unsigned char command,
199 				  unsigned char sub_command,
200 				  unsigned int write,
201 				  unsigned int req_data, unsigned int *resp);
202 
203 extern int isst_send_msr_command(unsigned int cpu, unsigned int command,
204 				 int write, unsigned long long *req_resp);
205 
206 extern int isst_get_ctdp_levels(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev);
207 extern int isst_get_ctdp_control(struct isst_id *id, int config_index,
208 				 struct isst_pkg_ctdp_level_info *ctdp_level);
209 extern int isst_get_coremask_info(struct isst_id *id, int config_index,
210 			   struct isst_pkg_ctdp_level_info *ctdp_level);
211 extern int isst_get_process_ctdp(struct isst_id *id, int tdp_level,
212 				 struct isst_pkg_ctdp *pkg_dev);
213 extern void isst_get_process_ctdp_complete(struct isst_id *id,
214 					   struct isst_pkg_ctdp *pkg_dev);
215 extern void isst_ctdp_display_information(struct isst_id *id, FILE *outf, int tdp_level,
216 					  struct isst_pkg_ctdp *pkg_dev);
217 extern void isst_ctdp_display_core_info(struct isst_id *id, FILE *outf, char *prefix,
218 					unsigned int val, char *str0, char *str1);
219 extern void isst_ctdp_display_information_start(FILE *outf);
220 extern void isst_ctdp_display_information_end(FILE *outf);
221 extern void isst_pbf_display_information(struct isst_id *id, FILE *outf, int level,
222 					 struct isst_pbf_info *info);
223 extern int isst_set_tdp_level(struct isst_id *id, int tdp_level);
224 extern int isst_set_pbf_fact_status(struct isst_id *id, int pbf, int enable);
225 extern int isst_get_pbf_info(struct isst_id *id, int level,
226 			     struct isst_pbf_info *pbf_info);
227 extern void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info);
228 extern int isst_get_fact_info(struct isst_id *id, int level, int fact_bucket,
229 			      struct isst_fact_info *fact_info);
230 extern int isst_get_fact_bucket_info(struct isst_id *id, int level,
231 				     struct isst_fact_bucket_info *bucket_info);
232 extern void isst_fact_display_information(struct isst_id *id, FILE *outf, int level,
233 					  int fact_bucket, int fact_avx,
234 					  struct isst_fact_info *fact_info);
235 extern int isst_set_trl(struct isst_id *id, unsigned long long trl);
236 extern int isst_get_trl(struct isst_id *id, unsigned long long *trl);
237 extern int isst_set_trl_from_current_tdp(struct isst_id *id, unsigned long long trl);
238 extern int isst_get_config_tdp_lock_status(struct isst_id *id);
239 
240 extern int isst_pm_qos_config(struct isst_id *id, int enable_clos, int priority_type);
241 extern int isst_pm_get_clos(struct isst_id *id, int clos,
242 			    struct isst_clos_config *clos_config);
243 extern int isst_set_clos(struct isst_id *id, int clos,
244 			 struct isst_clos_config *clos_config);
245 extern int isst_clos_associate(struct isst_id *id, int clos);
246 extern int isst_clos_get_assoc_status(struct isst_id *id, int *clos_id);
247 extern void isst_clos_display_information(struct isst_id *id, FILE *outf, int clos,
248 					  struct isst_clos_config *clos_config);
249 extern void isst_clos_display_assoc_information(struct isst_id *id, FILE *outf, int clos);
250 
251 extern void isst_display_result(struct isst_id *id, FILE *outf, char *feature, char *cmd,
252 				int result);
253 
254 extern int isst_clos_get_clos_information(struct isst_id *id, int *enable, int *type);
255 extern void isst_clos_display_clos_information(struct isst_id *id, FILE *outf,
256 					       int clos_enable, int type,
257 					       int state, int cap);
258 extern int is_clx_n_platform(void);
259 extern int get_cpufreq_base_freq(int cpu);
260 extern int isst_read_pm_config(struct isst_id *id, int *cp_state, int *cp_cap);
261 extern void isst_display_error_info_message(int error, char *msg, int arg_valid, int arg);
262 extern int is_skx_based_platform(void);
263 extern int is_spr_platform(void);
264 extern int is_icx_platform(void);
265 extern void isst_trl_display_information(struct isst_id *id, FILE *outf, unsigned long long trl);
266 
267 extern void set_cpu_online_offline(int cpu, int state);
268 extern void for_each_online_package_in_set(void (*callback)(struct isst_id *, void *, void *,
269 							    void *, void *),
270 					   void *arg1, void *arg2, void *arg3,
271 					   void *arg4);
272 extern int isst_daemon(int debug_mode, int poll_interval, int no_daemon);
273 extern void process_level_change(struct isst_id *id);
274 extern int hfi_main(void);
275 extern void hfi_exit(void);
276 #endif
277