1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Intel Speed Select -- Enumerate and control features
4 * Copyright (c) 2019 Intel Corporation.
12 #include <sys/types.h>
15 #include <sys/resource.h>
29 #include <sys/ioctl.h>
31 #include <linux/isst_if.h>
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))))
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
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
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
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
69 #define MBOX_CMD_WRITE_BIT 0x08
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
76 #define READ_PM_CONFIG 0x94
77 #define WRITE_PM_CONFIG 0x95
78 #define PM_FEATURE 0x03
80 #define DISP_FREQ_MULTIPLIER 100
82 #define MAX_PACKAGE_COUNT 8
83 #define MAX_DIE_PER_PACKAGE 2
84 #define MAX_PUNIT_PER_DIE 8
86 /* Unified structure to specific a CPU or a Power Domain */
94 struct isst_clos_config {
96 unsigned char clos_prop_prio;
97 unsigned char clos_min;
98 unsigned char clos_max;
99 unsigned char clos_desired;
102 struct isst_fact_bucket_info {
104 int hp_ratios[TRL_MAX_LEVELS];
107 struct isst_pbf_info {
110 size_t core_cpumask_size;
111 cpu_set_t *core_cpumask;
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];
126 struct isst_pkg_ctdp_level_info {
153 size_t core_cpumask_size;
154 cpu_set_t *core_cpumask;
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;
164 struct isst_pbf_info pbf_info;
165 struct isst_fact_info fact_info;
168 #define ISST_MAX_TDP_LEVELS (4 + 1) /* +1 for base config */
169 struct isst_pkg_ctdp {
176 struct isst_pkg_ctdp_level_info ctdp_level[ISST_MAX_TDP_LEVELS];
179 enum isst_platform_param {
180 ISST_PARAM_MBOX_DELAY,
181 ISST_PARAM_MBOX_RETRIES,
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);
198 extern int is_cpu_in_power_domain(int cpu, struct isst_id *id);
199 extern int get_topo_max_cpus(void);
200 extern int get_cpu_count(struct isst_id *id);
201 extern int get_max_punit_core_id(struct isst_id *id);
203 /* Common interfaces */
204 FILE *get_output_file(void);
205 extern void debug_printf(const char *format, ...);
206 extern int out_format_is_json(void);
207 extern void set_isst_id(struct isst_id *id, int cpu);
208 extern size_t alloc_cpu_set(cpu_set_t **cpu_set);
209 extern void free_cpu_set(cpu_set_t *cpu_set);
210 extern int find_phy_core_num(int logical_cpu);
211 extern void set_cpu_mask_from_punit_coremask(struct isst_id *id,
212 unsigned long long core_mask,
213 size_t core_cpumask_size,
214 cpu_set_t *core_cpumask,
216 extern int isst_send_mbox_command(unsigned int cpu, unsigned char command,
217 unsigned char sub_command,
219 unsigned int req_data, unsigned int *resp);
221 extern int isst_send_msr_command(unsigned int cpu, unsigned int command,
222 int write, unsigned long long *req_resp);
224 extern int isst_set_platform_ops(void);
225 extern void isst_update_platform_param(enum isst_platform_param, int vale);
226 extern int isst_get_disp_freq_multiplier(void);
227 extern int isst_get_trl_max_levels(void);
228 extern char *isst_get_trl_level_name(int level);
229 extern int isst_is_punit_valid(struct isst_id *id);
231 extern int isst_get_ctdp_levels(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev);
232 extern int isst_get_ctdp_control(struct isst_id *id, int config_index,
233 struct isst_pkg_ctdp_level_info *ctdp_level);
234 extern int isst_get_coremask_info(struct isst_id *id, int config_index,
235 struct isst_pkg_ctdp_level_info *ctdp_level);
236 extern void isst_get_uncore_p0_p1_info(struct isst_id *id, int config_index,
237 struct isst_pkg_ctdp_level_info *ctdp_level);
238 extern int isst_get_process_ctdp(struct isst_id *id, int tdp_level,
239 struct isst_pkg_ctdp *pkg_dev);
240 extern void isst_get_process_ctdp_complete(struct isst_id *id,
241 struct isst_pkg_ctdp *pkg_dev);
242 extern void isst_ctdp_display_information(struct isst_id *id, FILE *outf, int tdp_level,
243 struct isst_pkg_ctdp *pkg_dev);
244 extern void isst_ctdp_display_core_info(struct isst_id *id, FILE *outf, char *prefix,
245 unsigned int val, char *str0, char *str1);
246 extern void isst_ctdp_display_information_start(FILE *outf);
247 extern void isst_ctdp_display_information_end(FILE *outf);
248 extern void isst_pbf_display_information(struct isst_id *id, FILE *outf, int level,
249 struct isst_pbf_info *info);
250 extern int isst_set_tdp_level(struct isst_id *id, int tdp_level);
251 extern int isst_set_pbf_fact_status(struct isst_id *id, int pbf, int enable);
252 extern int isst_get_pbf_info(struct isst_id *id, int level,
253 struct isst_pbf_info *pbf_info);
254 extern void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info);
255 extern int isst_get_fact_info(struct isst_id *id, int level, int fact_bucket,
256 struct isst_fact_info *fact_info);
257 extern int isst_get_fact_bucket_info(struct isst_id *id, int level,
258 struct isst_fact_bucket_info *bucket_info);
259 extern void isst_fact_display_information(struct isst_id *id, FILE *outf, int level,
260 int fact_bucket, int fact_avx,
261 struct isst_fact_info *fact_info);
262 extern int isst_set_trl(struct isst_id *id, unsigned long long trl);
263 extern int isst_get_trl(struct isst_id *id, unsigned long long *trl);
264 extern int isst_set_trl_from_current_tdp(struct isst_id *id, unsigned long long trl);
265 extern int isst_get_config_tdp_lock_status(struct isst_id *id);
267 extern int isst_pm_qos_config(struct isst_id *id, int enable_clos, int priority_type);
268 extern int isst_pm_get_clos(struct isst_id *id, int clos,
269 struct isst_clos_config *clos_config);
270 extern int isst_set_clos(struct isst_id *id, int clos,
271 struct isst_clos_config *clos_config);
272 extern int isst_clos_associate(struct isst_id *id, int clos);
273 extern int isst_clos_get_assoc_status(struct isst_id *id, int *clos_id);
274 extern void isst_clos_display_information(struct isst_id *id, FILE *outf, int clos,
275 struct isst_clos_config *clos_config);
276 extern void isst_clos_display_assoc_information(struct isst_id *id, FILE *outf, int clos);
278 extern void isst_display_result(struct isst_id *id, FILE *outf, char *feature, char *cmd,
281 extern int isst_clos_get_clos_information(struct isst_id *id, int *enable, int *type);
282 extern void isst_clos_display_clos_information(struct isst_id *id, FILE *outf,
283 int clos_enable, int type,
285 extern int is_clx_n_platform(void);
286 extern int get_cpufreq_base_freq(int cpu);
287 extern int isst_read_pm_config(struct isst_id *id, int *cp_state, int *cp_cap);
288 extern void isst_display_error_info_message(int error, char *msg, int arg_valid, int arg);
289 extern int is_skx_based_platform(void);
290 extern int is_spr_platform(void);
291 extern int is_icx_platform(void);
292 extern void isst_trl_display_information(struct isst_id *id, FILE *outf, unsigned long long trl);
294 extern void set_cpu_online_offline(int cpu, int state);
295 extern void for_each_online_power_domain_in_set(void (*callback)(struct isst_id *, void *, void *,
297 void *arg1, void *arg2, void *arg3,
299 extern int isst_daemon(int debug_mode, int poll_interval, int no_daemon);
300 extern void process_level_change(struct isst_id *id);
301 extern int hfi_main(void);
302 extern void hfi_exit(void);
304 /* Interface specific callbacks */
305 extern struct isst_platform_ops *mbox_get_platform_ops(void);