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 GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (sizeof(long) * 8 - 1 - (h))))
33 #define GENMASK_ULL(h, l)                                                      \
34 	(((~0ULL) << (l)) & (~0ULL >> (sizeof(long long) * 8 - 1 - (h))))
35 
36 #define CONFIG_TDP				0x7f
37 #define CONFIG_TDP_GET_LEVELS_INFO		0x00
38 #define CONFIG_TDP_GET_TDP_CONTROL		0x01
39 #define CONFIG_TDP_SET_TDP_CONTROL		0x02
40 #define CONFIG_TDP_GET_TDP_INFO			0x03
41 #define CONFIG_TDP_GET_PWR_INFO			0x04
42 #define CONFIG_TDP_GET_TJMAX_INFO		0x05
43 #define CONFIG_TDP_GET_CORE_MASK		0x06
44 #define CONFIG_TDP_GET_TURBO_LIMIT_RATIOS	0x07
45 #define CONFIG_TDP_SET_LEVEL			0x08
46 #define CONFIG_TDP_GET_UNCORE_P0_P1_INFO	0X09
47 #define CONFIG_TDP_GET_P1_INFO			0x0a
48 #define CONFIG_TDP_GET_MEM_FREQ			0x0b
49 
50 #define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES	0x10
51 #define CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS	0x11
52 #define CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO		0x12
53 
54 #define CONFIG_TDP_PBF_GET_CORE_MASK_INFO	0x20
55 #define CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO	0x21
56 #define CONFIG_TDP_PBF_GET_TJ_MAX_INFO		0x22
57 #define CONFIG_TDP_PBF_GET_TDP_INFO		0X23
58 
59 #define CONFIG_CLOS				0xd0
60 #define CLOS_PQR_ASSOC				0x00
61 #define CLOS_PM_CLOS				0x01
62 #define CLOS_PM_QOS_CONFIG			0x02
63 #define CLOS_STATUS				0x03
64 
65 #define MBOX_CMD_WRITE_BIT			0x08
66 
67 #define PM_QOS_INFO_OFFSET			0x00
68 #define PM_QOS_CONFIG_OFFSET			0x04
69 #define PM_CLOS_OFFSET				0x08
70 #define PQR_ASSOC_OFFSET			0x20
71 
72 #define READ_PM_CONFIG				0x94
73 #define WRITE_PM_CONFIG				0x95
74 #define PM_FEATURE				0x03
75 
76 #define DISP_FREQ_MULTIPLIER 100
77 
78 struct isst_clos_config {
79 	int pkg_id;
80 	int die_id;
81 	unsigned char epp;
82 	unsigned char clos_prop_prio;
83 	unsigned char clos_min;
84 	unsigned char clos_max;
85 	unsigned char clos_desired;
86 };
87 
88 struct isst_fact_bucket_info {
89 	int high_priority_cores_count;
90 	int sse_trl;
91 	int avx_trl;
92 	int avx512_trl;
93 };
94 
95 struct isst_pbf_info {
96 	int pbf_acticated;
97 	int pbf_available;
98 	size_t core_cpumask_size;
99 	cpu_set_t *core_cpumask;
100 	int p1_high;
101 	int p1_low;
102 	int t_control;
103 	int t_prochot;
104 	int tdp;
105 };
106 
107 #define ISST_TRL_MAX_ACTIVE_CORES	8
108 #define ISST_FACT_MAX_BUCKETS		8
109 struct isst_fact_info {
110 	int lp_clipping_ratio_license_sse;
111 	int lp_clipping_ratio_license_avx2;
112 	int lp_clipping_ratio_license_avx512;
113 	struct isst_fact_bucket_info bucket_info[ISST_FACT_MAX_BUCKETS];
114 };
115 
116 struct isst_pkg_ctdp_level_info {
117 	int processed;
118 	int control_cpu;
119 	int pkg_id;
120 	int die_id;
121 	int level;
122 	int fact_support;
123 	int pbf_support;
124 	int fact_enabled;
125 	int pbf_enabled;
126 	int sst_cp_support;
127 	int sst_cp_enabled;
128 	int tdp_ratio;
129 	int active;
130 	int tdp_control;
131 	int pkg_tdp;
132 	int pkg_min_power;
133 	int pkg_max_power;
134 	int fact;
135 	int t_proc_hot;
136 	int uncore_p0;
137 	int uncore_p1;
138 	int sse_p1;
139 	int avx2_p1;
140 	int avx512_p1;
141 	int mem_freq;
142 	size_t core_cpumask_size;
143 	cpu_set_t *core_cpumask;
144 	int cpu_count;
145 	unsigned long long buckets_info;
146 	int trl_sse_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
147 	int trl_avx_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
148 	int trl_avx_512_active_cores[ISST_TRL_MAX_ACTIVE_CORES];
149 	int kobj_bucket_index;
150 	int active_bucket;
151 	int fact_max_index;
152 	int fact_max_config;
153 	int pbf_found;
154 	int pbf_active;
155 	struct isst_pbf_info pbf_info;
156 	struct isst_fact_info fact_info;
157 };
158 
159 #define ISST_MAX_TDP_LEVELS	(4 + 1) /* +1 for base config */
160 struct isst_pkg_ctdp {
161 	int locked;
162 	int version;
163 	int processed;
164 	int levels;
165 	int current_level;
166 	int enabled;
167 	struct isst_pkg_ctdp_level_info ctdp_level[ISST_MAX_TDP_LEVELS];
168 };
169 
170 extern int get_topo_max_cpus(void);
171 extern int get_cpu_count(int pkg_id, int die_id);
172 extern int get_core_count(int pkg_id, int die_id);
173 
174 /* Common interfaces */
175 extern void debug_printf(const char *format, ...);
176 extern int out_format_is_json(void);
177 extern int get_physical_package_id(int cpu);
178 extern int get_physical_die_id(int cpu);
179 extern size_t alloc_cpu_set(cpu_set_t **cpu_set);
180 extern void free_cpu_set(cpu_set_t *cpu_set);
181 extern int find_logical_cpu(int pkg_id, int die_id, int phy_cpu);
182 extern int find_phy_cpu_num(int logical_cpu);
183 extern int find_phy_core_num(int logical_cpu);
184 extern void set_cpu_mask_from_punit_coremask(int cpu,
185 					     unsigned long long core_mask,
186 					     size_t core_cpumask_size,
187 					     cpu_set_t *core_cpumask,
188 					     int *cpu_cnt);
189 
190 extern int isst_send_mbox_command(unsigned int cpu, unsigned char command,
191 				  unsigned char sub_command,
192 				  unsigned int write,
193 				  unsigned int req_data, unsigned int *resp);
194 
195 extern int isst_send_msr_command(unsigned int cpu, unsigned int command,
196 				 int write, unsigned long long *req_resp);
197 
198 extern int isst_get_ctdp_levels(int cpu, struct isst_pkg_ctdp *pkg_dev);
199 extern int isst_get_coremask_info(int cpu, int config_index,
200 			   struct isst_pkg_ctdp_level_info *ctdp_level);
201 extern int isst_get_process_ctdp(int cpu, int tdp_level,
202 				 struct isst_pkg_ctdp *pkg_dev);
203 extern void isst_get_process_ctdp_complete(int cpu,
204 					   struct isst_pkg_ctdp *pkg_dev);
205 extern void isst_ctdp_display_information(int cpu, FILE *outf, int tdp_level,
206 					  struct isst_pkg_ctdp *pkg_dev);
207 extern void isst_ctdp_display_core_info(int cpu, FILE *outf, char *prefix,
208 					unsigned int val);
209 extern void isst_ctdp_display_information_start(FILE *outf);
210 extern void isst_ctdp_display_information_end(FILE *outf);
211 extern void isst_pbf_display_information(int cpu, FILE *outf, int level,
212 					 struct isst_pbf_info *info);
213 extern int isst_set_tdp_level(int cpu, int tdp_level);
214 extern int isst_set_tdp_level_msr(int cpu, int tdp_level);
215 extern int isst_set_pbf_fact_status(int cpu, int pbf, int enable);
216 extern int isst_get_pbf_info(int cpu, int level,
217 			     struct isst_pbf_info *pbf_info);
218 extern void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info);
219 extern int isst_get_fact_info(int cpu, int level,
220 			      struct isst_fact_info *fact_info);
221 extern int isst_get_fact_bucket_info(int cpu, int level,
222 				     struct isst_fact_bucket_info *bucket_info);
223 extern void isst_fact_display_information(int cpu, FILE *outf, int level,
224 					  int fact_bucket, int fact_avx,
225 					  struct isst_fact_info *fact_info);
226 extern int isst_set_trl(int cpu, unsigned long long trl);
227 extern int isst_set_trl_from_current_tdp(int cpu, unsigned long long trl);
228 extern int isst_get_config_tdp_lock_status(int cpu);
229 
230 extern int isst_pm_qos_config(int cpu, int enable_clos, int priority_type);
231 extern int isst_pm_get_clos(int cpu, int clos,
232 			    struct isst_clos_config *clos_config);
233 extern int isst_set_clos(int cpu, int clos,
234 			 struct isst_clos_config *clos_config);
235 extern int isst_clos_associate(int cpu, int clos);
236 extern int isst_clos_get_assoc_status(int cpu, int *clos_id);
237 extern void isst_clos_display_information(int cpu, FILE *outf, int clos,
238 					  struct isst_clos_config *clos_config);
239 extern void isst_clos_display_assoc_information(int cpu, FILE *outf, int clos);
240 extern int isst_read_reg(unsigned short reg, unsigned int *val);
241 extern int isst_write_reg(int reg, unsigned int val);
242 
243 extern void isst_display_result(int cpu, FILE *outf, char *feature, char *cmd,
244 				int result);
245 
246 extern int isst_clos_get_clos_information(int cpu, int *enable, int *type);
247 extern void isst_clos_display_clos_information(int cpu, FILE *outf,
248 					       int clos_enable, int type);
249 extern int is_clx_n_platform(void);
250 extern int get_cpufreq_base_freq(int cpu);
251 #endif
252