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 struct isst_clos_config {
80 	int pkg_id;
81 	int die_id;
82 	unsigned char epp;
83 	unsigned char clos_prop_prio;
84 	unsigned char clos_min;
85 	unsigned char clos_max;
86 	unsigned char clos_desired;
87 };
88 
89 struct isst_fact_bucket_info {
90 	int high_priority_cores_count;
91 	int sse_trl;
92 	int avx_trl;
93 	int avx512_trl;
94 };
95 
96 struct isst_pbf_info {
97 	int pbf_acticated;
98 	int pbf_available;
99 	size_t core_cpumask_size;
100 	cpu_set_t *core_cpumask;
101 	int p1_high;
102 	int p1_low;
103 	int t_control;
104 	int t_prochot;
105 	int tdp;
106 };
107 
108 #define ISST_TRL_MAX_ACTIVE_CORES	8
109 #define ISST_FACT_MAX_BUCKETS		8
110 struct isst_fact_info {
111 	int lp_clipping_ratio_license_sse;
112 	int lp_clipping_ratio_license_avx2;
113 	int lp_clipping_ratio_license_avx512;
114 	struct isst_fact_bucket_info bucket_info[ISST_FACT_MAX_BUCKETS];
115 };
116 
117 struct isst_pkg_ctdp_level_info {
118 	int processed;
119 	int control_cpu;
120 	int pkg_id;
121 	int die_id;
122 	int level;
123 	int fact_support;
124 	int pbf_support;
125 	int fact_enabled;
126 	int pbf_enabled;
127 	int sst_cp_support;
128 	int sst_cp_enabled;
129 	int tdp_ratio;
130 	int active;
131 	int tdp_control;
132 	int pkg_tdp;
133 	int pkg_min_power;
134 	int pkg_max_power;
135 	int fact;
136 	int t_proc_hot;
137 	int uncore_p0;
138 	int uncore_p1;
139 	int sse_p1;
140 	int avx2_p1;
141 	int avx512_p1;
142 	int mem_freq;
143 	size_t core_cpumask_size;
144 	cpu_set_t *core_cpumask;
145 	int cpu_count;
146 	unsigned long long buckets_info;
147 	int trl_sse_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
148 	int trl_avx_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
149 	int trl_avx_512_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
150 	int kobj_bucket_index;
151 	int active_bucket;
152 	int fact_max_index;
153 	int fact_max_config;
154 	int pbf_found;
155 	int pbf_active;
156 	struct isst_pbf_info pbf_info;
157 	struct isst_fact_info fact_info;
158 };
159 
160 #define ISST_MAX_TDP_LEVELS	(4 + 1) /* +1 for base config */
161 struct isst_pkg_ctdp {
162 	int locked;
163 	int version;
164 	int processed;
165 	int levels;
166 	int current_level;
167 	int enabled;
168 	struct isst_pkg_ctdp_level_info ctdp_level[ISST_MAX_TDP_LEVELS];
169 };
170 
171 extern int get_topo_max_cpus(void);
172 extern int get_cpu_count(int pkg_id, int die_id);
173 extern int get_max_punit_core_id(int pkg_id, int die_id);
174 
175 /* Common interfaces */
176 FILE *get_output_file(void);
177 extern void debug_printf(const char *format, ...);
178 extern int out_format_is_json(void);
179 extern int get_physical_package_id(int cpu);
180 extern int get_physical_die_id(int cpu);
181 extern size_t alloc_cpu_set(cpu_set_t **cpu_set);
182 extern void free_cpu_set(cpu_set_t *cpu_set);
183 extern int find_logical_cpu(int pkg_id, int die_id, int phy_cpu);
184 extern int find_phy_cpu_num(int logical_cpu);
185 extern int find_phy_core_num(int logical_cpu);
186 extern void set_cpu_mask_from_punit_coremask(int cpu,
187 					     unsigned long long core_mask,
188 					     size_t core_cpumask_size,
189 					     cpu_set_t *core_cpumask,
190 					     int *cpu_cnt);
191 
192 extern int isst_send_mbox_command(unsigned int cpu, unsigned char command,
193 				  unsigned char sub_command,
194 				  unsigned int write,
195 				  unsigned int req_data, unsigned int *resp);
196 
197 extern int isst_send_msr_command(unsigned int cpu, unsigned int command,
198 				 int write, unsigned long long *req_resp);
199 
200 extern int isst_get_ctdp_levels(int cpu, struct isst_pkg_ctdp *pkg_dev);
201 extern int isst_get_ctdp_control(int cpu, int config_index,
202 				 struct isst_pkg_ctdp_level_info *ctdp_level);
203 extern int isst_get_coremask_info(int cpu, int config_index,
204 			   struct isst_pkg_ctdp_level_info *ctdp_level);
205 extern int isst_get_process_ctdp(int cpu, int tdp_level,
206 				 struct isst_pkg_ctdp *pkg_dev);
207 extern void isst_get_process_ctdp_complete(int cpu,
208 					   struct isst_pkg_ctdp *pkg_dev);
209 extern void isst_ctdp_display_information(int cpu, FILE *outf, int tdp_level,
210 					  struct isst_pkg_ctdp *pkg_dev);
211 extern void isst_ctdp_display_core_info(int cpu, FILE *outf, char *prefix,
212 					unsigned int val, char *str0, char *str1);
213 extern void isst_ctdp_display_information_start(FILE *outf);
214 extern void isst_ctdp_display_information_end(FILE *outf);
215 extern void isst_pbf_display_information(int cpu, FILE *outf, int level,
216 					 struct isst_pbf_info *info);
217 extern int isst_set_tdp_level(int cpu, int tdp_level);
218 extern int isst_set_tdp_level_msr(int cpu, int tdp_level);
219 extern int isst_set_pbf_fact_status(int cpu, int pbf, int enable);
220 extern int isst_get_pbf_info(int cpu, int level,
221 			     struct isst_pbf_info *pbf_info);
222 extern void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info);
223 extern int isst_get_fact_info(int cpu, int level, int fact_bucket,
224 			      struct isst_fact_info *fact_info);
225 extern int isst_get_fact_bucket_info(int cpu, int level,
226 				     struct isst_fact_bucket_info *bucket_info);
227 extern void isst_fact_display_information(int cpu, FILE *outf, int level,
228 					  int fact_bucket, int fact_avx,
229 					  struct isst_fact_info *fact_info);
230 extern int isst_set_trl(int cpu, unsigned long long trl);
231 extern int isst_set_trl_from_current_tdp(int cpu, unsigned long long trl);
232 extern int isst_get_config_tdp_lock_status(int cpu);
233 
234 extern int isst_pm_qos_config(int cpu, int enable_clos, int priority_type);
235 extern int isst_pm_get_clos(int cpu, int clos,
236 			    struct isst_clos_config *clos_config);
237 extern int isst_set_clos(int cpu, int clos,
238 			 struct isst_clos_config *clos_config);
239 extern int isst_clos_associate(int cpu, int clos);
240 extern int isst_clos_get_assoc_status(int cpu, int *clos_id);
241 extern void isst_clos_display_information(int cpu, FILE *outf, int clos,
242 					  struct isst_clos_config *clos_config);
243 extern void isst_clos_display_assoc_information(int cpu, FILE *outf, int clos);
244 extern int isst_read_reg(unsigned short reg, unsigned int *val);
245 extern int isst_write_reg(int reg, unsigned int val);
246 
247 extern void isst_display_result(int cpu, FILE *outf, char *feature, char *cmd,
248 				int result);
249 
250 extern int isst_clos_get_clos_information(int cpu, int *enable, int *type);
251 extern void isst_clos_display_clos_information(int cpu, FILE *outf,
252 					       int clos_enable, int type,
253 					       int state, int cap);
254 extern int is_clx_n_platform(void);
255 extern int get_cpufreq_base_freq(int cpu);
256 extern int isst_read_pm_config(int cpu, int *cp_state, int *cp_cap);
257 extern void isst_display_error_info_message(int error, char *msg, int arg_valid, int arg);
258 extern int is_skx_based_platform(void);
259 #endif
260