tools/power/x86/intel-speed-select: Abstract get_get_trl
[platform/kernel/linux-starfive.git] / tools / power / x86 / intel-speed-select / isst-core.c
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 #include "isst.h"
8
9 static int mbox_delay;
10 static int mbox_retries = 3;
11
12 static struct isst_platform_ops         *isst_ops;
13
14 #define CHECK_CB(_name) \
15         do {    \
16                 if (!isst_ops || !isst_ops->_name) {    \
17                         fprintf(stderr, "Invalid ops\n");       \
18                         exit(0);        \
19                 }       \
20         } while (0)
21
22 int isst_set_platform_ops(void)
23 {
24         isst_ops = mbox_get_platform_ops();
25
26         if (!isst_ops)
27                 return -1;
28         return 0;
29 }
30
31 void isst_update_platform_param(enum isst_platform_param param, int value)
32 {
33         switch (param) {
34         case ISST_PARAM_MBOX_DELAY:
35                 mbox_delay = value;
36                 break;
37         case ISST_PARAM_MBOX_RETRIES:
38                 mbox_retries = value;
39                 break;
40         default:
41                 break;
42         }
43 }
44
45 int isst_get_disp_freq_multiplier(void)
46 {
47         CHECK_CB(get_disp_freq_multiplier);
48         return isst_ops->get_disp_freq_multiplier();
49 }
50
51 int isst_get_trl_max_levels(void)
52 {
53         CHECK_CB(get_trl_max_levels);
54         return isst_ops->get_trl_max_levels();
55 }
56
57 char *isst_get_trl_level_name(int level)
58 {
59         CHECK_CB(get_trl_level_name);
60         return isst_ops->get_trl_level_name(level);
61 }
62
63 int isst_is_punit_valid(struct isst_id *id)
64 {
65         CHECK_CB(is_punit_valid);
66         return isst_ops->is_punit_valid(id);
67 }
68
69 static int isst_send_mmio_command(unsigned int cpu, unsigned int reg, int write,
70                                   unsigned int *value)
71 {
72         struct isst_if_io_regs io_regs;
73         const char *pathname = "/dev/isst_interface";
74         int cmd;
75         FILE *outf = get_output_file();
76         int fd;
77
78         debug_printf("mmio_cmd cpu:%d reg:%d write:%d\n", cpu, reg, write);
79
80         fd = open(pathname, O_RDWR);
81         if (fd < 0)
82                 err(-1, "%s open failed", pathname);
83
84         io_regs.req_count = 1;
85         io_regs.io_reg[0].logical_cpu = cpu;
86         io_regs.io_reg[0].reg = reg;
87         cmd = ISST_IF_IO_CMD;
88         if (write) {
89                 io_regs.io_reg[0].read_write = 1;
90                 io_regs.io_reg[0].value = *value;
91         } else {
92                 io_regs.io_reg[0].read_write = 0;
93         }
94
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");
99                         exit(0);
100                 }
101                 fprintf(outf, "Error: mmio_cmd cpu:%d reg:%x read_write:%x\n",
102                         cpu, reg, write);
103         } else {
104                 if (!write)
105                         *value = io_regs.io_reg[0].value;
106
107                 debug_printf(
108                         "mmio_cmd response: cpu:%d reg:%x rd_write:%x resp:%x\n",
109                         cpu, reg, write, *value);
110         }
111
112         close(fd);
113
114         return 0;
115 }
116
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)
120 {
121         const char *pathname = "/dev/isst_interface";
122         int fd, retry;
123         struct isst_if_mbox_cmds mbox_cmds = { 0 };
124
125         debug_printf(
126                 "mbox_send: cpu:%d command:%x sub_command:%x parameter:%x req_data:%x\n",
127                 cpu, command, sub_command, parameter, req_data);
128
129         if (!is_skx_based_platform() && command == CONFIG_CLOS &&
130             sub_command != CLOS_PM_QOS_CONFIG) {
131                 unsigned int value;
132                 int write = 0;
133                 int clos_id, core_id, ret = 0;
134
135                 debug_printf("CPU %d\n", cpu);
136
137                 if (parameter & BIT(MBOX_CMD_WRITE_BIT)) {
138                         value = req_data;
139                         write = 1;
140                 }
141
142                 switch (sub_command) {
143                 case CLOS_PQR_ASSOC:
144                         core_id = parameter & 0xff;
145                         ret = isst_send_mmio_command(
146                                 cpu, PQR_ASSOC_OFFSET + core_id * 4, write,
147                                 &value);
148                         if (!ret && !write)
149                                 *resp = value;
150                         break;
151                 case CLOS_PM_CLOS:
152                         clos_id = parameter & 0x03;
153                         ret = isst_send_mmio_command(
154                                 cpu, PM_CLOS_OFFSET + clos_id * 4, write,
155                                 &value);
156                         if (!ret && !write)
157                                 *resp = value;
158                         break;
159                 case CLOS_STATUS:
160                         break;
161                 default:
162                         break;
163                 }
164                 return ret;
165         }
166
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;
173
174         if (mbox_delay)
175                 usleep(mbox_delay * 1000);
176
177         fd = open(pathname, O_RDWR);
178         if (fd < 0)
179                 err(-1, "%s open failed", pathname);
180
181         retry = mbox_retries;
182
183         do {
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");
188                                 exit(0);
189                         }
190                         debug_printf(
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);
193                         --retry;
194                 } else {
195                         *resp = mbox_cmds.mbox_cmd[0].resp_data;
196                         debug_printf(
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);
199                         break;
200                 }
201         } while (retry);
202
203         close(fd);
204
205         if (!retry) {
206                 debug_printf("Failed mbox command even after retries\n");
207                 return -1;
208
209         }
210         return 0;
211 }
212
213 int isst_send_msr_command(unsigned int cpu, unsigned int msr, int write,
214                           unsigned long long *req_resp)
215 {
216         struct isst_if_msr_cmds msr_cmds;
217         const char *pathname = "/dev/isst_interface";
218         FILE *outf = get_output_file();
219         int fd;
220
221         fd = open(pathname, O_RDWR);
222         if (fd < 0)
223                 err(-1, "%s open failed", pathname);
224
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;
229         if (write)
230                 msr_cmds.msr_cmd[0].data = *req_resp;
231
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",
235                         cpu, msr, write);
236         } else {
237                 if (!write)
238                         *req_resp = msr_cmds.msr_cmd[0].data;
239
240                 debug_printf(
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);
243         }
244
245         close(fd);
246
247         return 0;
248 }
249
250 int isst_write_pm_config(struct isst_id *id, int cp_state)
251 {
252         unsigned int req, resp;
253         int ret;
254
255         if (cp_state)
256                 req = BIT(16);
257         else
258                 req = 0;
259
260         ret = isst_send_mbox_command(id->cpu, WRITE_PM_CONFIG, PM_FEATURE, 0, req,
261                                      &resp);
262         if (ret)
263                 return ret;
264
265         debug_printf("cpu:%d WRITE_PM_CONFIG resp:%x\n", id->cpu, resp);
266
267         return 0;
268 }
269
270 int isst_read_pm_config(struct isst_id *id, int *cp_state, int *cp_cap)
271 {
272         unsigned int resp;
273         int ret;
274
275         ret = isst_send_mbox_command(id->cpu, READ_PM_CONFIG, PM_FEATURE, 0, 0,
276                                      &resp);
277         if (ret)
278                 return ret;
279
280         debug_printf("cpu:%d READ_PM_CONFIG resp:%x\n", id->cpu, resp);
281
282         *cp_state = resp & BIT(16);
283         *cp_cap = resp & BIT(0) ? 1 : 0;
284
285         return 0;
286 }
287
288 int isst_get_ctdp_levels(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev)
289 {
290         CHECK_CB(get_config_levels);
291         return isst_ops->get_config_levels(id, pkg_dev);
292 }
293
294 int isst_get_ctdp_control(struct isst_id *id, int config_index,
295                           struct isst_pkg_ctdp_level_info *ctdp_level)
296 {
297         CHECK_CB(get_ctdp_control);
298         return isst_ops->get_ctdp_control(id, config_index, ctdp_level);
299 }
300
301 int isst_get_tdp_info(struct isst_id *id, int config_index,
302                       struct isst_pkg_ctdp_level_info *ctdp_level)
303 {
304         CHECK_CB(get_tdp_info);
305         return isst_ops->get_tdp_info(id, config_index, ctdp_level);
306 }
307
308 int isst_get_pwr_info(struct isst_id *id, int config_index,
309                       struct isst_pkg_ctdp_level_info *ctdp_level)
310 {
311         CHECK_CB(get_pwr_info);
312         return isst_ops->get_pwr_info(id, config_index, ctdp_level);
313 }
314
315 int isst_get_coremask_info(struct isst_id *id, int config_index,
316                            struct isst_pkg_ctdp_level_info *ctdp_level)
317 {
318         CHECK_CB(get_coremask_info);
319         return isst_ops->get_coremask_info(id, config_index, ctdp_level);
320 }
321
322 int isst_get_get_trl_from_msr(struct isst_id *id, int *trl)
323 {
324         unsigned long long msr_trl;
325         int ret;
326
327         ret = isst_send_msr_command(id->cpu, 0x1AD, 0, &msr_trl);
328         if (ret)
329                 return ret;
330
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;
339
340         return 0;
341 }
342
343 int isst_get_get_trl(struct isst_id *id, int level, int avx_level, int *trl)
344 {
345         CHECK_CB(get_get_trl);
346         return isst_ops->get_get_trl(id, level, avx_level, trl);
347 }
348
349 int isst_get_trl_bucket_info(struct isst_id *id, unsigned long long *buckets_info)
350 {
351         int ret;
352
353         debug_printf("cpu:%d bucket info via MSR\n", id->cpu);
354
355         *buckets_info = 0;
356
357         ret = isst_send_msr_command(id->cpu, 0x1ae, 0, buckets_info);
358         if (ret)
359                 return ret;
360
361         debug_printf("cpu:%d bucket info via MSR successful 0x%llx\n", id->cpu,
362                      *buckets_info);
363
364         return 0;
365 }
366
367 int isst_set_tdp_level(struct isst_id *id, int tdp_level)
368 {
369         unsigned int resp;
370         int ret;
371
372
373         if (isst_get_config_tdp_lock_status(id)) {
374                 isst_display_error_info_message(1, "TDP is locked", 0, 0);
375                 return -1;
376
377         }
378
379         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_SET_LEVEL, 0,
380                                      tdp_level, &resp);
381         if (ret) {
382                 isst_display_error_info_message(1, "Set TDP level failed for level", 1, tdp_level);
383                 return ret;
384         }
385
386         return 0;
387 }
388
389 int isst_get_pbf_info(struct isst_id *id, int level, struct isst_pbf_info *pbf_info)
390 {
391         struct isst_pkg_ctdp_level_info ctdp_level;
392         struct isst_pkg_ctdp pkg_dev;
393         int i, ret, max_punit_core, max_mask_index;
394         unsigned int req, resp;
395
396         ret = isst_get_ctdp_levels(id, &pkg_dev);
397         if (ret) {
398                 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
399                 return ret;
400         }
401
402         if (level > pkg_dev.levels) {
403                 isst_display_error_info_message(1, "Invalid level", 1, level);
404                 return -1;
405         }
406
407         ret = isst_get_ctdp_control(id, level, &ctdp_level);
408         if (ret)
409                 return ret;
410
411         if (!ctdp_level.pbf_support) {
412                 isst_display_error_info_message(1, "base-freq feature is not present at this level", 1, level);
413                 return -1;
414         }
415
416         pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
417
418         max_punit_core = get_max_punit_core_id(id);
419         max_mask_index = max_punit_core > 32 ? 2 : 1;
420
421         for (i = 0; i < max_mask_index; ++i) {
422                 unsigned long long mask;
423                 int count;
424
425                 ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
426                                              CONFIG_TDP_PBF_GET_CORE_MASK_INFO,
427                                              0, (i << 8) | level, &resp);
428                 if (ret)
429                         break;
430
431                 debug_printf(
432                         "cpu:%d CONFIG_TDP_PBF_GET_CORE_MASK_INFO resp:%x\n",
433                         id->cpu, resp);
434
435                 mask = (unsigned long long)resp << (32 * i);
436                 set_cpu_mask_from_punit_coremask(id, mask,
437                                                  pbf_info->core_cpumask_size,
438                                                  pbf_info->core_cpumask,
439                                                  &count);
440         }
441
442         req = level;
443         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
444                                      CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO, 0, req,
445                                      &resp);
446         if (ret)
447                 return ret;
448
449         debug_printf("cpu:%d CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO resp:%x\n", id->cpu,
450                      resp);
451
452         pbf_info->p1_low = resp & 0xff;
453         pbf_info->p1_high = (resp & GENMASK(15, 8)) >> 8;
454
455         req = level;
456         ret = isst_send_mbox_command(
457                 id->cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TDP_INFO, 0, req, &resp);
458         if (ret)
459                 return ret;
460
461         debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TDP_INFO resp:%x\n", id->cpu, resp);
462
463         pbf_info->tdp = resp & 0xffff;
464
465         req = level;
466         ret = isst_send_mbox_command(
467                 id->cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TJ_MAX_INFO, 0, req, &resp);
468         if (ret)
469                 return ret;
470
471         debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TJ_MAX_INFO resp:%x\n", id->cpu,
472                      resp);
473         pbf_info->t_control = (resp >> 8) & 0xff;
474         pbf_info->t_prochot = resp & 0xff;
475
476         return 0;
477 }
478
479 void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info)
480 {
481         free_cpu_set(pbf_info->core_cpumask);
482 }
483
484 int isst_set_pbf_fact_status(struct isst_id *id, int pbf, int enable)
485 {
486         struct isst_pkg_ctdp pkg_dev;
487         struct isst_pkg_ctdp_level_info ctdp_level;
488         int current_level;
489         unsigned int req = 0, resp;
490         int ret;
491
492         ret = isst_get_ctdp_levels(id, &pkg_dev);
493         if (ret)
494                 debug_printf("cpu:%d No support for dynamic ISST\n", id->cpu);
495
496         current_level = pkg_dev.current_level;
497
498         ret = isst_get_ctdp_control(id, current_level, &ctdp_level);
499         if (ret)
500                 return ret;
501
502         if (pbf) {
503                 if (ctdp_level.fact_enabled)
504                         req = BIT(16);
505
506                 if (enable)
507                         req |= BIT(17);
508                 else
509                         req &= ~BIT(17);
510         } else {
511
512                 if (enable && !ctdp_level.sst_cp_enabled)
513                         isst_display_error_info_message(0, "Make sure to execute before: core-power enable", 0, 0);
514
515                 if (ctdp_level.pbf_enabled)
516                         req = BIT(17);
517
518                 if (enable)
519                         req |= BIT(16);
520                 else
521                         req &= ~BIT(16);
522         }
523
524         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
525                                      CONFIG_TDP_SET_TDP_CONTROL, 0, req, &resp);
526         if (ret)
527                 return ret;
528
529         debug_printf("cpu:%d CONFIG_TDP_SET_TDP_CONTROL pbf/fact:%d req:%x\n",
530                      id->cpu, pbf, req);
531
532         return 0;
533 }
534
535 int isst_get_fact_bucket_info(struct isst_id *id, int level,
536                               struct isst_fact_bucket_info *bucket_info)
537 {
538         unsigned int resp;
539         int i, k, ret;
540
541         for (i = 0; i < 2; ++i) {
542                 int j;
543
544                 ret = isst_send_mbox_command(
545                         id->cpu, CONFIG_TDP,
546                         CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES, 0,
547                         (i << 8) | level, &resp);
548                 if (ret)
549                         return ret;
550
551                 debug_printf(
552                         "cpu:%d CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES index:%d level:%d resp:%x\n",
553                         id->cpu, i, level, resp);
554
555                 for (j = 0; j < 4; ++j) {
556                         bucket_info[j + (i * 4)].hp_cores =
557                                 (resp >> (j * 8)) & 0xff;
558                 }
559         }
560
561         for (k = 0; k < 3; ++k) {
562                 for (i = 0; i < 2; ++i) {
563                         int j;
564
565                         ret = isst_send_mbox_command(
566                                 id->cpu, CONFIG_TDP,
567                                 CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS, 0,
568                                 (k << 16) | (i << 8) | level, &resp);
569                         if (ret)
570                                 return ret;
571
572                         debug_printf(
573                                 "cpu:%d CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS index:%d level:%d avx:%d resp:%x\n",
574                                 id->cpu, i, level, k, resp);
575
576                         for (j = 0; j < 4; ++j) {
577                                 bucket_info[j + (i * 4)].hp_ratios[k] =
578                                         (resp >> (j * 8)) & 0xff;
579                         }
580                 }
581         }
582
583         return 0;
584 }
585
586 int isst_get_fact_info(struct isst_id *id, int level, int fact_bucket, struct isst_fact_info *fact_info)
587 {
588         struct isst_pkg_ctdp_level_info ctdp_level;
589         struct isst_pkg_ctdp pkg_dev;
590         unsigned int resp;
591         int j, ret, print;
592
593         ret = isst_get_ctdp_levels(id, &pkg_dev);
594         if (ret) {
595                 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
596                 return ret;
597         }
598
599         if (level > pkg_dev.levels) {
600                 isst_display_error_info_message(1, "Invalid level", 1, level);
601                 return -1;
602         }
603
604         ret = isst_get_ctdp_control(id, level, &ctdp_level);
605         if (ret)
606                 return ret;
607
608         if (!ctdp_level.fact_support) {
609                 isst_display_error_info_message(1, "turbo-freq feature is not present at this level", 1, level);
610                 return -1;
611         }
612
613         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
614                                      CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO, 0,
615                                      level, &resp);
616         if (ret)
617                 return ret;
618
619         debug_printf("cpu:%d CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO resp:%x\n",
620                      id->cpu, resp);
621
622         fact_info->lp_ratios[0] = resp & 0xff;
623         fact_info->lp_ratios[1] = (resp >> 8) & 0xff;
624         fact_info->lp_ratios[2] = (resp >> 16) & 0xff;
625
626         ret = isst_get_fact_bucket_info(id, level, fact_info->bucket_info);
627         if (ret)
628                 return ret;
629
630         print = 0;
631         for (j = 0; j < ISST_FACT_MAX_BUCKETS; ++j) {
632                 if (fact_bucket != 0xff && fact_bucket != j)
633                         continue;
634
635                 if (!fact_info->bucket_info[j].hp_cores)
636                         break;
637
638                 print = 1;
639         }
640         if (!print) {
641                 isst_display_error_info_message(1, "Invalid bucket", 0, 0);
642                 return -1;
643         }
644
645         return 0;
646 }
647
648 int isst_get_trl(struct isst_id *id, unsigned long long *trl)
649 {
650         int ret;
651
652         ret = isst_send_msr_command(id->cpu, 0x1AD, 0, trl);
653         if (ret)
654                 return ret;
655
656         return 0;
657 }
658
659 int isst_set_trl(struct isst_id *id, unsigned long long trl)
660 {
661         int ret;
662
663         if (!trl)
664                 trl = 0xFFFFFFFFFFFFFFFFULL;
665
666         ret = isst_send_msr_command(id->cpu, 0x1AD, 1, &trl);
667         if (ret)
668                 return ret;
669
670         return 0;
671 }
672
673 int isst_set_trl_from_current_tdp(struct isst_id *id, unsigned long long trl)
674 {
675         unsigned long long msr_trl;
676         int ret;
677
678         if (trl) {
679                 msr_trl = trl;
680         } else {
681                 struct isst_pkg_ctdp pkg_dev;
682                 int trl[8];
683                 int i;
684
685                 ret = isst_get_ctdp_levels(id, &pkg_dev);
686                 if (ret)
687                         return ret;
688
689                 ret = isst_get_get_trl(id, pkg_dev.current_level, 0, trl);
690                 if (ret)
691                         return ret;
692
693                 msr_trl = 0;
694                 for (i = 0; i < 8; ++i) {
695                         unsigned long long _trl = trl[i];
696
697                         msr_trl |= (_trl << (i * 8));
698                 }
699         }
700         ret = isst_send_msr_command(id->cpu, 0x1AD, 1, &msr_trl);
701         if (ret)
702                 return ret;
703
704         return 0;
705 }
706
707 /* Return 1 if locked */
708 int isst_get_config_tdp_lock_status(struct isst_id *id)
709 {
710         unsigned long long tdp_control = 0;
711         int ret;
712
713         ret = isst_send_msr_command(id->cpu, 0x64b, 0, &tdp_control);
714         if (ret)
715                 return ret;
716
717         ret = !!(tdp_control & BIT(31));
718
719         return ret;
720 }
721
722 void isst_get_process_ctdp_complete(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev)
723 {
724         int i;
725
726         if (!pkg_dev->processed)
727                 return;
728
729         for (i = 0; i < pkg_dev->levels; ++i) {
730                 struct isst_pkg_ctdp_level_info *ctdp_level;
731
732                 ctdp_level = &pkg_dev->ctdp_level[i];
733                 if (ctdp_level->pbf_support)
734                         free_cpu_set(ctdp_level->pbf_info.core_cpumask);
735                 free_cpu_set(ctdp_level->core_cpumask);
736         }
737 }
738
739 void isst_get_uncore_p0_p1_info(struct isst_id *id, int config_index,
740                                 struct isst_pkg_ctdp_level_info *ctdp_level)
741 {
742         unsigned int resp;
743         int ret;
744
745         ctdp_level->uncore_pm = 0;
746         ctdp_level->uncore_p0 = 0;
747         ctdp_level->uncore_p1 = 0;
748
749         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
750                                      CONFIG_TDP_GET_RATIO_INFO, 0,
751                                      (BIT(16) | config_index) , &resp);
752         if (ret) {
753                 goto try_uncore_mbox;
754         }
755
756         ctdp_level->uncore_p0 = resp & GENMASK(7, 0);
757         ctdp_level->uncore_p1 = (resp & GENMASK(15, 8)) >> 8;
758         ctdp_level->uncore_pm = (resp & GENMASK(31, 24)) >> 24;
759
760         debug_printf(
761                 "cpu:%d ctdp:%d CONFIG_TDP_GET_RATIO_INFO resp:%x uncore p0:%d uncore p1:%d uncore pm:%d\n",
762                 id->cpu, config_index, resp, ctdp_level->uncore_p0, ctdp_level->uncore_p1,
763                 ctdp_level->uncore_pm);
764
765         return;
766
767 try_uncore_mbox:
768         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
769                                      CONFIG_TDP_GET_UNCORE_P0_P1_INFO, 0,
770                                      config_index, &resp);
771         if (ret) {
772                 ctdp_level->uncore_p0 = 0;
773                 ctdp_level->uncore_p1 = 0;
774                 return;
775         }
776
777         ctdp_level->uncore_p0 = resp & GENMASK(7, 0);
778         ctdp_level->uncore_p1 = (resp & GENMASK(15, 8)) >> 8;
779         debug_printf(
780                 "cpu:%d ctdp:%d CONFIG_TDP_GET_UNCORE_P0_P1_INFO resp:%x uncore p0:%d uncore p1:%d\n",
781                 id->cpu, config_index, resp, ctdp_level->uncore_p0,
782                 ctdp_level->uncore_p1);
783 }
784
785 void isst_get_p1_info(struct isst_id *id, int config_index,
786                       struct isst_pkg_ctdp_level_info *ctdp_level)
787 {
788         unsigned int resp;
789         int ret;
790         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_GET_P1_INFO, 0,
791                                      config_index, &resp);
792         if (ret) {
793                 ctdp_level->sse_p1 = 0;
794                 ctdp_level->avx2_p1 = 0;
795                 ctdp_level->avx512_p1 = 0;
796                 return;
797         }
798
799         ctdp_level->sse_p1 = resp & GENMASK(7, 0);
800         ctdp_level->avx2_p1 = (resp & GENMASK(15, 8)) >> 8;
801         ctdp_level->avx512_p1 = (resp & GENMASK(23, 16)) >> 16;
802         debug_printf(
803                 "cpu:%d ctdp:%d CONFIG_TDP_GET_P1_INFO resp:%x sse_p1:%d avx2_p1:%d avx512_p1:%d\n",
804                 id->cpu, config_index, resp, ctdp_level->sse_p1,
805                 ctdp_level->avx2_p1, ctdp_level->avx512_p1);
806 }
807
808 void isst_get_uncore_mem_freq(struct isst_id *id, int config_index,
809                               struct isst_pkg_ctdp_level_info *ctdp_level)
810 {
811         unsigned int resp;
812         int ret;
813
814         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_GET_MEM_FREQ,
815                                      0, config_index, &resp);
816         if (ret) {
817                 ctdp_level->mem_freq = 0;
818                 return;
819         }
820
821         ctdp_level->mem_freq = resp & GENMASK(7, 0);
822         if (is_spr_platform()) {
823                 ctdp_level->mem_freq *= 200;
824         } else if (is_icx_platform()) {
825                 if (ctdp_level->mem_freq < 7) {
826                         ctdp_level->mem_freq = (12 - ctdp_level->mem_freq) * 133.33 * 2 * 10;
827                         ctdp_level->mem_freq /= 10;
828                         if (ctdp_level->mem_freq % 10 > 5)
829                                 ctdp_level->mem_freq++;
830                 } else {
831                         ctdp_level->mem_freq = 0;
832                 }
833         } else {
834                 ctdp_level->mem_freq = 0;
835         }
836         debug_printf(
837                 "cpu:%d ctdp:%d CONFIG_TDP_GET_MEM_FREQ resp:%x uncore mem_freq:%d\n",
838                 id->cpu, config_index, resp, ctdp_level->mem_freq);
839 }
840
841 int isst_get_process_ctdp(struct isst_id *id, int tdp_level, struct isst_pkg_ctdp *pkg_dev)
842 {
843         int i, ret, valid = 0;
844
845         if (pkg_dev->processed)
846                 return 0;
847
848         ret = isst_get_ctdp_levels(id, pkg_dev);
849         if (ret)
850                 return ret;
851
852         debug_printf("cpu: %d ctdp enable:%d current level: %d levels:%d\n",
853                      id->cpu, pkg_dev->enabled, pkg_dev->current_level,
854                      pkg_dev->levels);
855
856         if (tdp_level != 0xff && tdp_level > pkg_dev->levels) {
857                 isst_display_error_info_message(1, "Invalid level", 0, 0);
858                 return -1;
859         }
860
861         if (!pkg_dev->enabled)
862                 isst_display_error_info_message(0, "perf-profile feature is not supported, just base-config level 0 is valid", 0, 0);
863
864         for (i = 0; i <= pkg_dev->levels; ++i) {
865                 struct isst_pkg_ctdp_level_info *ctdp_level;
866                 int trl_max_levels = isst_get_trl_max_levels();
867                 int j;
868
869                 if (tdp_level != 0xff && i != tdp_level)
870                         continue;
871
872                 debug_printf("cpu:%d Get Information for TDP level:%d\n", id->cpu,
873                              i);
874                 ctdp_level = &pkg_dev->ctdp_level[i];
875
876                 ctdp_level->level = i;
877                 ctdp_level->control_cpu = id->cpu;
878                 ctdp_level->pkg_id = id->pkg;
879                 ctdp_level->die_id = id->die;
880
881                 ret = isst_get_ctdp_control(id, i, ctdp_level);
882                 if (ret)
883                         continue;
884
885                 valid = 1;
886                 pkg_dev->processed = 1;
887                 ctdp_level->processed = 1;
888
889                 if (ctdp_level->pbf_support) {
890                         ret = isst_get_pbf_info(id, i, &ctdp_level->pbf_info);
891                         if (!ret)
892                                 ctdp_level->pbf_found = 1;
893                 }
894
895                 if (ctdp_level->fact_support) {
896                         ret = isst_get_fact_info(id, i, 0xff,
897                                                  &ctdp_level->fact_info);
898                         if (ret)
899                                 return ret;
900                 }
901
902                 if (!pkg_dev->enabled && is_skx_based_platform()) {
903                         int freq;
904
905                         freq = get_cpufreq_base_freq(id->cpu);
906                         if (freq > 0) {
907                                 ctdp_level->sse_p1 = freq / 100000;
908                                 ctdp_level->tdp_ratio = ctdp_level->sse_p1;
909                         }
910
911                         isst_get_get_trl_from_msr(id, ctdp_level->trl_ratios[0]);
912                         isst_get_trl_bucket_info(id, &ctdp_level->trl_cores);
913                         continue;
914                 }
915
916                 ret = isst_get_tdp_info(id, i, ctdp_level);
917                 if (ret)
918                         return ret;
919
920                 ret = isst_get_pwr_info(id, i, ctdp_level);
921                 if (ret)
922                         return ret;
923
924                 ctdp_level->core_cpumask_size =
925                         alloc_cpu_set(&ctdp_level->core_cpumask);
926                 ret = isst_get_coremask_info(id, i, ctdp_level);
927                 if (ret)
928                         return ret;
929
930                 ret = isst_get_trl_bucket_info(id, &ctdp_level->trl_cores);
931                 if (ret)
932                         return ret;
933
934                 for (j = 0; j < trl_max_levels; j++) {
935                         ret = isst_get_get_trl(id, i, j,
936                                        ctdp_level->trl_ratios[j]);
937                         if (ret)
938                                 return ret;
939                 }
940
941                 isst_get_uncore_p0_p1_info(id, i, ctdp_level);
942                 isst_get_p1_info(id, i, ctdp_level);
943                 isst_get_uncore_mem_freq(id, i, ctdp_level);
944         }
945
946         if (!valid)
947                 isst_display_error_info_message(0, "Invalid level, Can't get TDP control information at specified levels on cpu", 1, id->cpu);
948
949         return 0;
950 }
951
952 int isst_clos_get_clos_information(struct isst_id *id, int *enable, int *type)
953 {
954         unsigned int resp;
955         int ret;
956
957         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, 0, 0,
958                                      &resp);
959         if (ret)
960                 return ret;
961
962         debug_printf("cpu:%d CLOS_PM_QOS_CONFIG resp:%x\n", id->cpu, resp);
963
964         if (resp & BIT(1))
965                 *enable = 1;
966         else
967                 *enable = 0;
968
969         if (resp & BIT(2))
970                 *type = 1;
971         else
972                 *type = 0;
973
974         return 0;
975 }
976
977 int isst_pm_qos_config(struct isst_id *id, int enable_clos, int priority_type)
978 {
979         unsigned int req, resp;
980         int ret;
981
982         if (!enable_clos) {
983                 struct isst_pkg_ctdp pkg_dev;
984                 struct isst_pkg_ctdp_level_info ctdp_level;
985
986                 ret = isst_get_ctdp_levels(id, &pkg_dev);
987                 if (ret) {
988                         debug_printf("isst_get_ctdp_levels\n");
989                         return ret;
990                 }
991
992                 ret = isst_get_ctdp_control(id, pkg_dev.current_level,
993                                             &ctdp_level);
994                 if (ret)
995                         return ret;
996
997                 if (ctdp_level.fact_enabled) {
998                         isst_display_error_info_message(1, "Ignoring request, turbo-freq feature is still enabled", 0, 0);
999                         return -EINVAL;
1000                 }
1001                 ret = isst_write_pm_config(id, 0);
1002                 if (ret)
1003                         isst_display_error_info_message(0, "WRITE_PM_CONFIG command failed, ignoring error", 0, 0);
1004         } else {
1005                 ret = isst_write_pm_config(id, 1);
1006                 if (ret)
1007                         isst_display_error_info_message(0, "WRITE_PM_CONFIG command failed, ignoring error", 0, 0);
1008         }
1009
1010         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, 0, 0,
1011                                      &resp);
1012         if (ret) {
1013                 isst_display_error_info_message(1, "CLOS_PM_QOS_CONFIG command failed", 0, 0);
1014                 return ret;
1015         }
1016
1017         debug_printf("cpu:%d CLOS_PM_QOS_CONFIG resp:%x\n", id->cpu, resp);
1018
1019         req = resp;
1020
1021         if (enable_clos)
1022                 req = req | BIT(1);
1023         else
1024                 req = req & ~BIT(1);
1025
1026         if (priority_type > 1)
1027                 isst_display_error_info_message(1, "Invalid priority type: Changing type to ordered", 0, 0);
1028
1029         if (priority_type)
1030                 req = req | BIT(2);
1031         else
1032                 req = req & ~BIT(2);
1033
1034         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG,
1035                                      BIT(MBOX_CMD_WRITE_BIT), req, &resp);
1036         if (ret)
1037                 return ret;
1038
1039         debug_printf("cpu:%d CLOS_PM_QOS_CONFIG priority type:%d req:%x\n", id->cpu,
1040                      priority_type, req);
1041
1042         return 0;
1043 }
1044
1045 int isst_pm_get_clos(struct isst_id *id, int clos, struct isst_clos_config *clos_config)
1046 {
1047         unsigned int resp;
1048         int ret;
1049
1050         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_CLOS, clos, 0,
1051                                      &resp);
1052         if (ret)
1053                 return ret;
1054
1055         clos_config->epp = resp & 0x0f;
1056         clos_config->clos_prop_prio = (resp >> 4) & 0x0f;
1057         clos_config->clos_min = (resp >> 8) & 0xff;
1058         clos_config->clos_max = (resp >> 16) & 0xff;
1059         clos_config->clos_desired = (resp >> 24) & 0xff;
1060
1061         return 0;
1062 }
1063
1064 int isst_set_clos(struct isst_id *id, int clos, struct isst_clos_config *clos_config)
1065 {
1066         unsigned int req, resp;
1067         unsigned int param;
1068         int ret;
1069
1070         req = clos_config->epp & 0x0f;
1071         req |= (clos_config->clos_prop_prio & 0x0f) << 4;
1072         req |= (clos_config->clos_min & 0xff) << 8;
1073         req |= (clos_config->clos_max & 0xff) << 16;
1074         req |= (clos_config->clos_desired & 0xff) << 24;
1075
1076         param = BIT(MBOX_CMD_WRITE_BIT) | clos;
1077
1078         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_CLOS, param, req,
1079                                      &resp);
1080         if (ret)
1081                 return ret;
1082
1083         debug_printf("cpu:%d CLOS_PM_CLOS param:%x req:%x\n", id->cpu, param, req);
1084
1085         return 0;
1086 }
1087
1088 int isst_clos_get_assoc_status(struct isst_id *id, int *clos_id)
1089 {
1090         unsigned int resp;
1091         unsigned int param;
1092         int core_id, ret;
1093
1094         core_id = find_phy_core_num(id->cpu);
1095         param = core_id;
1096
1097         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param, 0,
1098                                      &resp);
1099         if (ret)
1100                 return ret;
1101
1102         debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x resp:%x\n", id->cpu, param,
1103                      resp);
1104         *clos_id = (resp >> 16) & 0x03;
1105
1106         return 0;
1107 }
1108
1109 int isst_clos_associate(struct isst_id *id, int clos_id)
1110 {
1111         unsigned int req, resp;
1112         unsigned int param;
1113         int core_id, ret;
1114
1115         req = (clos_id & 0x03) << 16;
1116         core_id = find_phy_core_num(id->cpu);
1117         param = BIT(MBOX_CMD_WRITE_BIT) | core_id;
1118
1119         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param,
1120                                      req, &resp);
1121         if (ret)
1122                 return ret;
1123
1124         debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x req:%x\n", id->cpu, param,
1125                      req);
1126
1127         return 0;
1128 }