1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Speed Select -- Enumerate and control features
4 * Copyright (c) 2019 Intel Corporation.
10 static int mbox_retries = 3;
12 static struct isst_platform_ops *isst_ops;
14 #define CHECK_CB(_name) \
16 if (!isst_ops || !isst_ops->_name) { \
17 fprintf(stderr, "Invalid ops\n"); \
22 int isst_set_platform_ops(void)
24 isst_ops = mbox_get_platform_ops();
31 void isst_update_platform_param(enum isst_platform_param param, int value)
34 case ISST_PARAM_MBOX_DELAY:
37 case ISST_PARAM_MBOX_RETRIES:
45 int isst_get_disp_freq_multiplier(void)
47 CHECK_CB(get_disp_freq_multiplier);
48 return isst_ops->get_disp_freq_multiplier();
51 int isst_get_trl_max_levels(void)
53 CHECK_CB(get_trl_max_levels);
54 return isst_ops->get_trl_max_levels();
57 char *isst_get_trl_level_name(int level)
59 CHECK_CB(get_trl_level_name);
60 return isst_ops->get_trl_level_name(level);
63 int isst_is_punit_valid(struct isst_id *id)
65 CHECK_CB(is_punit_valid);
66 return isst_ops->is_punit_valid(id);
69 static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
72 struct isst_if_io_regs io_regs;
73 const char *pathname = "/dev/isst_interface";
75 FILE *outf = get_output_file();
78 debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
80 fd = open(pathname, O_RDWR);
82 err(-1, "%s open failed", pathname);
84 io_regs.req_count = 1;
85 io_regs.io_reg[0].logical_cpu = cpu;
86 io_regs.io_reg[0].reg = reg;
89 io_regs.io_reg[0].read_write = 1;
90 io_regs.io_reg[0].value = *value;
92 io_regs.io_reg[0].read_write = 0;
95 if (ioctl(fd, cmd, &io_regs) == -1) {
96 if (errno == ENOTTY) {
97 perror("ISST_IF_IO_COMMAND\n");
98 fprintf(stderr, "Check presence of kernel modules: isst_if_mmio\n");
101 fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
105 *value = io_regs.io_reg[0].value;
108 "mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
109 cpu, reg, write, *value);
117 int isst_send_mbox_command(unsigned int cpu, unsigned char command,
118 unsigned char sub_command, unsigned int parameter,
119 unsigned int req_data, unsigned int *resp)
121 const char *pathname = "/dev/isst_interface";
123 struct isst_if_mbox_cmds mbox_cmds = { 0 };
126 "mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
127 cpu, command, sub_command, parameter, req_data);
129 if (!is_skx_based_platform() && command == CONFIG_CLOS &&
130 sub_command != CLOS_PM_QOS_CONFIG) {
133 int clos_id, core_id, ret = 0;
135 debug_printf("CPU %d\n", cpu);
137 if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
142 switch (sub_command) {
144 core_id = parameter & 0xff;
145 ret = isst_send_mmio_command(
146 cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
152 clos_id = parameter & 0x03;
153 ret = isst_send_mmio_command(
154 cpu, PM_CLOS_OFFSET + clos_id * 4, write,
167 mbox_cmds.cmd_count = 1;
168 mbox_cmds.mbox_cmd[0].logical_cpu = cpu;
169 mbox_cmds.mbox_cmd[0].command = command;
170 mbox_cmds.mbox_cmd[0].sub_command = sub_command;
171 mbox_cmds.mbox_cmd[0].parameter = parameter;
172 mbox_cmds.mbox_cmd[0].req_data = req_data;
175 usleep(mbox_delay * 1000);
177 fd = open(pathname, O_RDWR);
179 err(-1, "%s open failed", pathname);
181 retry = mbox_retries;
184 if (ioctl(fd, ISST_IF_MBOX_COMMAND, &mbox_cmds) == -1) {
185 if (errno == ENOTTY) {
186 perror("ISST_IF_MBOX_COMMAND\n");
187 fprintf(stderr, "Check presence of kernel modules: isst_if_mbox_pci or isst_if_mbox_msr\n");
191 "Error: mbox_cmd cpu:%d command:%x sub_command:%x parameter:%x req_data:%x errorno:%d\n",
192 cpu, command, sub_command, parameter, req_data, errno);
195 *resp = mbox_cmds.mbox_cmd[0].resp_data;
197 "mbox_cmd response: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x resp:%x\n",
198 cpu, command, sub_command, parameter, req_data, *resp);
206 debug_printf("Failed mbox command even after retries\n");
213 int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
214 unsigned long long *req_resp)
216 struct isst_if_msr_cmds msr_cmds;
217 const char *pathname = "/dev/isst_interface";
218 FILE *outf = get_output_file();
221 fd = open(pathname, O_RDWR);
223 err(-1, "%s open failed", pathname);
225 msr_cmds.cmd_count = 1;
226 msr_cmds.msr_cmd[0].logical_cpu = cpu;
227 msr_cmds.msr_cmd[0].msr = msr;
228 msr_cmds.msr_cmd[0].read_write = write;
230 msr_cmds.msr_cmd[0].data = *req_resp;
232 if (ioctl(fd, ISST_IF_MSR_COMMAND, &msr_cmds) == -1) {
233 perror("ISST_IF_MSR_COMMAND");
234 fprintf(outf, "Error: msr_cmd cpu:%d msr:%x read_write:%d\n",
238 *req_resp = msr_cmds.msr_cmd[0].data;
241 "msr_cmd response: cpu:%d msr:%x rd_write:%x resp:%llx %llx\n",
242 cpu, msr, write, *req_resp, msr_cmds.msr_cmd[0].data);
250 int isst_write_pm_config(struct isst_id *id, int cp_state)
252 unsigned int req, resp;
260 ret = isst_send_mbox_command(id->cpu, WRITE_PM_CONFIG, PM_FEATURE, 0, req,
265 debug_printf("cpu:%d WRITE_PM_CONFIG resp:%x\n", id->cpu, resp);
270 int isst_read_pm_config(struct isst_id *id, int *cp_state, int *cp_cap)
275 ret = isst_send_mbox_command(id->cpu, READ_PM_CONFIG, PM_FEATURE, 0, 0,
280 debug_printf("cpu:%d READ_PM_CONFIG resp:%x\n", id->cpu, resp);
282 *cp_state = resp & BIT(16);
283 *cp_cap = resp & BIT(0) ? 1 : 0;
288 int isst_get_ctdp_levels(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev)
290 CHECK_CB(get_config_levels);
291 return isst_ops->get_config_levels(id, pkg_dev);
294 int isst_get_ctdp_control(struct isst_id *id, int config_index,
295 struct isst_pkg_ctdp_level_info *ctdp_level)
297 CHECK_CB(get_ctdp_control);
298 return isst_ops->get_ctdp_control(id, config_index, ctdp_level);
301 int isst_get_tdp_info(struct isst_id *id, int config_index,
302 struct isst_pkg_ctdp_level_info *ctdp_level)
304 CHECK_CB(get_tdp_info);
305 return isst_ops->get_tdp_info(id, config_index, ctdp_level);
308 int isst_get_pwr_info(struct isst_id *id, int config_index,
309 struct isst_pkg_ctdp_level_info *ctdp_level)
311 CHECK_CB(get_pwr_info);
312 return isst_ops->get_pwr_info(id, config_index, ctdp_level);
315 int isst_get_coremask_info(struct isst_id *id, int config_index,
316 struct isst_pkg_ctdp_level_info *ctdp_level)
318 CHECK_CB(get_coremask_info);
319 return isst_ops->get_coremask_info(id, config_index, ctdp_level);
322 int isst_get_get_trl_from_msr(struct isst_id *id, int *trl)
324 unsigned long long msr_trl;
327 ret = isst_send_msr_command(id->cpu, 0x1AD, 0, &msr_trl);
331 trl[0] = msr_trl & GENMASK(7, 0);
332 trl[1] = (msr_trl & GENMASK(15, 8)) >> 8;
333 trl[2] = (msr_trl & GENMASK(23, 16)) >> 16;
334 trl[3] = (msr_trl & GENMASK(31, 24)) >> 24;
335 trl[4] = (msr_trl & GENMASK(39, 32)) >> 32;
336 trl[5] = (msr_trl & GENMASK(47, 40)) >> 40;
337 trl[6] = (msr_trl & GENMASK(55, 48)) >> 48;
338 trl[7] = (msr_trl & GENMASK(63, 56)) >> 56;
343 int isst_get_get_trl(struct isst_id *id, int level, int avx_level, int *trl)
345 CHECK_CB(get_get_trl);
346 return isst_ops->get_get_trl(id, level, avx_level, trl);
349 int isst_get_get_trls(struct isst_id *id, int level, struct isst_pkg_ctdp_level_info *ctdp_level)
351 CHECK_CB(get_get_trls);
352 return isst_ops->get_get_trls(id, level, ctdp_level);
355 int isst_get_trl_bucket_info(struct isst_id *id, int level, unsigned long long *buckets_info)
357 CHECK_CB(get_trl_bucket_info);
358 return isst_ops->get_trl_bucket_info(id, level, buckets_info);
361 int isst_set_tdp_level(struct isst_id *id, int tdp_level)
363 CHECK_CB(set_tdp_level);
364 return isst_ops->set_tdp_level(id, tdp_level);
367 int isst_get_pbf_info(struct isst_id *id, int level, struct isst_pbf_info *pbf_info)
369 struct isst_pkg_ctdp_level_info ctdp_level;
370 struct isst_pkg_ctdp pkg_dev;
373 ret = isst_get_ctdp_levels(id, &pkg_dev);
375 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
379 if (level > pkg_dev.levels) {
380 isst_display_error_info_message(1, "Invalid level", 1, level);
384 ret = isst_get_ctdp_control(id, level, &ctdp_level);
388 if (!ctdp_level.pbf_support) {
389 isst_display_error_info_message(1, "base-freq feature is not present at this level", 1, level);
393 pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
395 CHECK_CB(get_pbf_info);
396 return isst_ops->get_pbf_info(id, level, pbf_info);
399 int isst_set_pbf_fact_status(struct isst_id *id, int pbf, int enable)
401 CHECK_CB(set_pbf_fact_status);
402 return isst_ops->set_pbf_fact_status(id, pbf, enable);
407 int isst_get_fact_info(struct isst_id *id, int level, int fact_bucket, struct isst_fact_info *fact_info)
409 struct isst_pkg_ctdp_level_info ctdp_level;
410 struct isst_pkg_ctdp pkg_dev;
413 ret = isst_get_ctdp_levels(id, &pkg_dev);
415 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
419 if (level > pkg_dev.levels) {
420 isst_display_error_info_message(1, "Invalid level", 1, level);
424 ret = isst_get_ctdp_control(id, level, &ctdp_level);
428 if (!ctdp_level.fact_support) {
429 isst_display_error_info_message(1, "turbo-freq feature is not present at this level", 1, level);
432 CHECK_CB(get_fact_info);
433 return isst_ops->get_fact_info(id, level, fact_bucket, fact_info);
436 int isst_get_trl(struct isst_id *id, unsigned long long *trl)
440 ret = isst_send_msr_command(id->cpu, 0x1AD, 0, trl);
447 int isst_set_trl(struct isst_id *id, unsigned long long trl)
452 trl = 0xFFFFFFFFFFFFFFFFULL;
454 ret = isst_send_msr_command(id->cpu, 0x1AD, 1, &trl);
461 int isst_set_trl_from_current_tdp(struct isst_id *id, unsigned long long trl)
463 unsigned long long msr_trl;
469 struct isst_pkg_ctdp pkg_dev;
473 ret = isst_get_ctdp_levels(id, &pkg_dev);
477 ret = isst_get_get_trl(id, pkg_dev.current_level, 0, trl);
482 for (i = 0; i < 8; ++i) {
483 unsigned long long _trl = trl[i];
485 msr_trl |= (_trl << (i * 8));
488 ret = isst_send_msr_command(id->cpu, 0x1AD, 1, &msr_trl);
495 /* Return 1 if locked */
496 int isst_get_config_tdp_lock_status(struct isst_id *id)
498 unsigned long long tdp_control = 0;
501 ret = isst_send_msr_command(id->cpu, 0x64b, 0, &tdp_control);
505 ret = !!(tdp_control & BIT(31));
510 void isst_get_process_ctdp_complete(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev)
514 if (!pkg_dev->processed)
517 for (i = 0; i < pkg_dev->levels; ++i) {
518 struct isst_pkg_ctdp_level_info *ctdp_level;
520 ctdp_level = &pkg_dev->ctdp_level[i];
521 if (ctdp_level->pbf_support)
522 free_cpu_set(ctdp_level->pbf_info.core_cpumask);
523 free_cpu_set(ctdp_level->core_cpumask);
527 void isst_get_uncore_p0_p1_info(struct isst_id *id, int config_index,
528 struct isst_pkg_ctdp_level_info *ctdp_level)
530 CHECK_CB(get_uncore_p0_p1_info);
531 return isst_ops->get_uncore_p0_p1_info(id, config_index, ctdp_level);
534 int isst_get_process_ctdp(struct isst_id *id, int tdp_level, struct isst_pkg_ctdp *pkg_dev)
536 int i, ret, valid = 0;
538 if (pkg_dev->processed)
541 ret = isst_get_ctdp_levels(id, pkg_dev);
545 debug_printf("cpu: %d ctdp enable:%d current level: %d levels:%d\n",
546 id->cpu, pkg_dev->enabled, pkg_dev->current_level,
549 if (tdp_level != 0xff && tdp_level > pkg_dev->levels) {
550 isst_display_error_info_message(1, "Invalid level", 0, 0);
554 if (!pkg_dev->enabled)
555 isst_display_error_info_message(0, "perf-profile feature is not supported, just base-config level 0 is valid", 0, 0);
557 for (i = 0; i <= pkg_dev->levels; ++i) {
558 struct isst_pkg_ctdp_level_info *ctdp_level;
560 if (tdp_level != 0xff && i != tdp_level)
563 debug_printf("cpu:%d Get Information for TDP level:%d\n", id->cpu,
565 ctdp_level = &pkg_dev->ctdp_level[i];
567 ctdp_level->level = i;
568 ctdp_level->control_cpu = id->cpu;
569 ctdp_level->pkg_id = id->pkg;
570 ctdp_level->die_id = id->die;
572 ret = isst_get_ctdp_control(id, i, ctdp_level);
577 pkg_dev->processed = 1;
578 ctdp_level->processed = 1;
580 if (ctdp_level->pbf_support) {
581 ret = isst_get_pbf_info(id, i, &ctdp_level->pbf_info);
583 ctdp_level->pbf_found = 1;
586 if (ctdp_level->fact_support) {
587 ret = isst_get_fact_info(id, i, 0xff,
588 &ctdp_level->fact_info);
593 if (!pkg_dev->enabled && is_skx_based_platform()) {
596 freq = get_cpufreq_base_freq(id->cpu);
598 ctdp_level->sse_p1 = freq / 100000;
599 ctdp_level->tdp_ratio = ctdp_level->sse_p1;
602 isst_get_get_trl_from_msr(id, ctdp_level->trl_ratios[0]);
603 isst_get_trl_bucket_info(id, i, &ctdp_level->trl_cores);
607 ret = isst_get_tdp_info(id, i, ctdp_level);
611 ret = isst_get_pwr_info(id, i, ctdp_level);
615 ctdp_level->core_cpumask_size =
616 alloc_cpu_set(&ctdp_level->core_cpumask);
617 ret = isst_get_coremask_info(id, i, ctdp_level);
621 ret = isst_get_trl_bucket_info(id, i, &ctdp_level->trl_cores);
625 ret = isst_get_get_trls(id, i, ctdp_level);
631 isst_display_error_info_message(0, "Invalid level, Can't get TDP control information at specified levels on cpu", 1, id->cpu);
636 int isst_clos_get_clos_information(struct isst_id *id, int *enable, int *type)
638 CHECK_CB(get_clos_information);
639 return isst_ops->get_clos_information(id, enable, type);
642 int isst_pm_qos_config(struct isst_id *id, int enable_clos, int priority_type)
644 unsigned int req, resp;
648 struct isst_pkg_ctdp pkg_dev;
649 struct isst_pkg_ctdp_level_info ctdp_level;
651 ret = isst_get_ctdp_levels(id, &pkg_dev);
653 debug_printf("isst_get_ctdp_levels\n");
657 ret = isst_get_ctdp_control(id, pkg_dev.current_level,
662 if (ctdp_level.fact_enabled) {
663 isst_display_error_info_message(1, "Ignoring request, turbo-freq feature is still enabled", 0, 0);
666 ret = isst_write_pm_config(id, 0);
668 isst_display_error_info_message(0, "WRITE_PM_CONFIG command failed, ignoring error", 0, 0);
670 ret = isst_write_pm_config(id, 1);
672 isst_display_error_info_message(0, "WRITE_PM_CONFIG command failed, ignoring error", 0, 0);
675 ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, 0, 0,
678 isst_display_error_info_message(1, "CLOS_PM_QOS_CONFIG command failed", 0, 0);
682 debug_printf("cpu:%d CLOS_PM_QOS_CONFIG resp:%x\n", id->cpu, resp);
691 if (priority_type > 1)
692 isst_display_error_info_message(1, "Invalid priority type: Changing type to ordered", 0, 0);
699 ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG,
700 BIT(MBOX_CMD_WRITE_BIT), req, &resp);
704 debug_printf("cpu:%d CLOS_PM_QOS_CONFIG priority type:%d req:%x\n", id->cpu,
710 int isst_pm_get_clos(struct isst_id *id, int clos, struct isst_clos_config *clos_config)
715 ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_CLOS, clos, 0,
720 clos_config->epp = resp & 0x0f;
721 clos_config->clos_prop_prio = (resp >> 4) & 0x0f;
722 clos_config->clos_min = (resp >> 8) & 0xff;
723 clos_config->clos_max = (resp >> 16) & 0xff;
724 clos_config->clos_desired = (resp >> 24) & 0xff;
729 int isst_set_clos(struct isst_id *id, int clos, struct isst_clos_config *clos_config)
731 unsigned int req, resp;
735 req = clos_config->epp & 0x0f;
736 req |= (clos_config->clos_prop_prio & 0x0f) << 4;
737 req |= (clos_config->clos_min & 0xff) << 8;
738 req |= (clos_config->clos_max & 0xff) << 16;
739 req |= (clos_config->clos_desired & 0xff) << 24;
741 param = BIT(MBOX_CMD_WRITE_BIT) | clos;
743 ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_CLOS, param, req,
748 debug_printf("cpu:%d CLOS_PM_CLOS param:%x req:%x\n", id->cpu, param, req);
753 int isst_clos_get_assoc_status(struct isst_id *id, int *clos_id)
759 core_id = find_phy_core_num(id->cpu);
762 ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param, 0,
767 debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x resp:%x\n", id->cpu, param,
769 *clos_id = (resp >> 16) & 0x03;
774 int isst_clos_associate(struct isst_id *id, int clos_id)
776 unsigned int req, resp;
780 req = (clos_id & 0x03) << 16;
781 core_id = find_phy_core_num(id->cpu);
782 param = BIT(MBOX_CMD_WRITE_BIT) | core_id;
784 ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param,
789 debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x req:%x\n", id->cpu, param,