tools/power/x86/intel-speed-select: Abstract get_trl_bucket_info
[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, int level, unsigned long long *buckets_info)
350 {
351         CHECK_CB(get_trl_bucket_info);
352         return isst_ops->get_trl_bucket_info(id, level, buckets_info);
353 }
354
355 int isst_set_tdp_level(struct isst_id *id, int tdp_level)
356 {
357         unsigned int resp;
358         int ret;
359
360
361         if (isst_get_config_tdp_lock_status(id)) {
362                 isst_display_error_info_message(1, "TDP is locked", 0, 0);
363                 return -1;
364
365         }
366
367         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_SET_LEVEL, 0,
368                                      tdp_level, &resp);
369         if (ret) {
370                 isst_display_error_info_message(1, "Set TDP level failed for level", 1, tdp_level);
371                 return ret;
372         }
373
374         return 0;
375 }
376
377 int isst_get_pbf_info(struct isst_id *id, int level, struct isst_pbf_info *pbf_info)
378 {
379         struct isst_pkg_ctdp_level_info ctdp_level;
380         struct isst_pkg_ctdp pkg_dev;
381         int i, ret, max_punit_core, max_mask_index;
382         unsigned int req, resp;
383
384         ret = isst_get_ctdp_levels(id, &pkg_dev);
385         if (ret) {
386                 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
387                 return ret;
388         }
389
390         if (level > pkg_dev.levels) {
391                 isst_display_error_info_message(1, "Invalid level", 1, level);
392                 return -1;
393         }
394
395         ret = isst_get_ctdp_control(id, level, &ctdp_level);
396         if (ret)
397                 return ret;
398
399         if (!ctdp_level.pbf_support) {
400                 isst_display_error_info_message(1, "base-freq feature is not present at this level", 1, level);
401                 return -1;
402         }
403
404         pbf_info->core_cpumask_size = alloc_cpu_set(&pbf_info->core_cpumask);
405
406         max_punit_core = get_max_punit_core_id(id);
407         max_mask_index = max_punit_core > 32 ? 2 : 1;
408
409         for (i = 0; i < max_mask_index; ++i) {
410                 unsigned long long mask;
411                 int count;
412
413                 ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
414                                              CONFIG_TDP_PBF_GET_CORE_MASK_INFO,
415                                              0, (i << 8) | level, &resp);
416                 if (ret)
417                         break;
418
419                 debug_printf(
420                         "cpu:%d CONFIG_TDP_PBF_GET_CORE_MASK_INFO resp:%x\n",
421                         id->cpu, resp);
422
423                 mask = (unsigned long long)resp << (32 * i);
424                 set_cpu_mask_from_punit_coremask(id, mask,
425                                                  pbf_info->core_cpumask_size,
426                                                  pbf_info->core_cpumask,
427                                                  &count);
428         }
429
430         req = level;
431         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
432                                      CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO, 0, req,
433                                      &resp);
434         if (ret)
435                 return ret;
436
437         debug_printf("cpu:%d CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO resp:%x\n", id->cpu,
438                      resp);
439
440         pbf_info->p1_low = resp & 0xff;
441         pbf_info->p1_high = (resp & GENMASK(15, 8)) >> 8;
442
443         req = level;
444         ret = isst_send_mbox_command(
445                 id->cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TDP_INFO, 0, req, &resp);
446         if (ret)
447                 return ret;
448
449         debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TDP_INFO resp:%x\n", id->cpu, resp);
450
451         pbf_info->tdp = resp & 0xffff;
452
453         req = level;
454         ret = isst_send_mbox_command(
455                 id->cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TJ_MAX_INFO, 0, req, &resp);
456         if (ret)
457                 return ret;
458
459         debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TJ_MAX_INFO resp:%x\n", id->cpu,
460                      resp);
461         pbf_info->t_control = (resp >> 8) & 0xff;
462         pbf_info->t_prochot = resp & 0xff;
463
464         return 0;
465 }
466
467 void isst_get_pbf_info_complete(struct isst_pbf_info *pbf_info)
468 {
469         free_cpu_set(pbf_info->core_cpumask);
470 }
471
472 int isst_set_pbf_fact_status(struct isst_id *id, int pbf, int enable)
473 {
474         struct isst_pkg_ctdp pkg_dev;
475         struct isst_pkg_ctdp_level_info ctdp_level;
476         int current_level;
477         unsigned int req = 0, resp;
478         int ret;
479
480         ret = isst_get_ctdp_levels(id, &pkg_dev);
481         if (ret)
482                 debug_printf("cpu:%d No support for dynamic ISST\n", id->cpu);
483
484         current_level = pkg_dev.current_level;
485
486         ret = isst_get_ctdp_control(id, current_level, &ctdp_level);
487         if (ret)
488                 return ret;
489
490         if (pbf) {
491                 if (ctdp_level.fact_enabled)
492                         req = BIT(16);
493
494                 if (enable)
495                         req |= BIT(17);
496                 else
497                         req &= ~BIT(17);
498         } else {
499
500                 if (enable && !ctdp_level.sst_cp_enabled)
501                         isst_display_error_info_message(0, "Make sure to execute before: core-power enable", 0, 0);
502
503                 if (ctdp_level.pbf_enabled)
504                         req = BIT(17);
505
506                 if (enable)
507                         req |= BIT(16);
508                 else
509                         req &= ~BIT(16);
510         }
511
512         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
513                                      CONFIG_TDP_SET_TDP_CONTROL, 0, req, &resp);
514         if (ret)
515                 return ret;
516
517         debug_printf("cpu:%d CONFIG_TDP_SET_TDP_CONTROL pbf/fact:%d req:%x\n",
518                      id->cpu, pbf, req);
519
520         return 0;
521 }
522
523 int isst_get_fact_bucket_info(struct isst_id *id, int level,
524                               struct isst_fact_bucket_info *bucket_info)
525 {
526         unsigned int resp;
527         int i, k, ret;
528
529         for (i = 0; i < 2; ++i) {
530                 int j;
531
532                 ret = isst_send_mbox_command(
533                         id->cpu, CONFIG_TDP,
534                         CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES, 0,
535                         (i << 8) | level, &resp);
536                 if (ret)
537                         return ret;
538
539                 debug_printf(
540                         "cpu:%d CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES index:%d level:%d resp:%x\n",
541                         id->cpu, i, level, resp);
542
543                 for (j = 0; j < 4; ++j) {
544                         bucket_info[j + (i * 4)].hp_cores =
545                                 (resp >> (j * 8)) & 0xff;
546                 }
547         }
548
549         for (k = 0; k < 3; ++k) {
550                 for (i = 0; i < 2; ++i) {
551                         int j;
552
553                         ret = isst_send_mbox_command(
554                                 id->cpu, CONFIG_TDP,
555                                 CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS, 0,
556                                 (k << 16) | (i << 8) | level, &resp);
557                         if (ret)
558                                 return ret;
559
560                         debug_printf(
561                                 "cpu:%d CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS index:%d level:%d avx:%d resp:%x\n",
562                                 id->cpu, i, level, k, resp);
563
564                         for (j = 0; j < 4; ++j) {
565                                 bucket_info[j + (i * 4)].hp_ratios[k] =
566                                         (resp >> (j * 8)) & 0xff;
567                         }
568                 }
569         }
570
571         return 0;
572 }
573
574 int isst_get_fact_info(struct isst_id *id, int level, int fact_bucket, struct isst_fact_info *fact_info)
575 {
576         struct isst_pkg_ctdp_level_info ctdp_level;
577         struct isst_pkg_ctdp pkg_dev;
578         unsigned int resp;
579         int j, ret, print;
580
581         ret = isst_get_ctdp_levels(id, &pkg_dev);
582         if (ret) {
583                 isst_display_error_info_message(1, "Failed to get number of levels", 0, 0);
584                 return ret;
585         }
586
587         if (level > pkg_dev.levels) {
588                 isst_display_error_info_message(1, "Invalid level", 1, level);
589                 return -1;
590         }
591
592         ret = isst_get_ctdp_control(id, level, &ctdp_level);
593         if (ret)
594                 return ret;
595
596         if (!ctdp_level.fact_support) {
597                 isst_display_error_info_message(1, "turbo-freq feature is not present at this level", 1, level);
598                 return -1;
599         }
600
601         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
602                                      CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO, 0,
603                                      level, &resp);
604         if (ret)
605                 return ret;
606
607         debug_printf("cpu:%d CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO resp:%x\n",
608                      id->cpu, resp);
609
610         fact_info->lp_ratios[0] = resp & 0xff;
611         fact_info->lp_ratios[1] = (resp >> 8) & 0xff;
612         fact_info->lp_ratios[2] = (resp >> 16) & 0xff;
613
614         ret = isst_get_fact_bucket_info(id, level, fact_info->bucket_info);
615         if (ret)
616                 return ret;
617
618         print = 0;
619         for (j = 0; j < ISST_FACT_MAX_BUCKETS; ++j) {
620                 if (fact_bucket != 0xff && fact_bucket != j)
621                         continue;
622
623                 if (!fact_info->bucket_info[j].hp_cores)
624                         break;
625
626                 print = 1;
627         }
628         if (!print) {
629                 isst_display_error_info_message(1, "Invalid bucket", 0, 0);
630                 return -1;
631         }
632
633         return 0;
634 }
635
636 int isst_get_trl(struct isst_id *id, unsigned long long *trl)
637 {
638         int ret;
639
640         ret = isst_send_msr_command(id->cpu, 0x1AD, 0, trl);
641         if (ret)
642                 return ret;
643
644         return 0;
645 }
646
647 int isst_set_trl(struct isst_id *id, unsigned long long trl)
648 {
649         int ret;
650
651         if (!trl)
652                 trl = 0xFFFFFFFFFFFFFFFFULL;
653
654         ret = isst_send_msr_command(id->cpu, 0x1AD, 1, &trl);
655         if (ret)
656                 return ret;
657
658         return 0;
659 }
660
661 int isst_set_trl_from_current_tdp(struct isst_id *id, unsigned long long trl)
662 {
663         unsigned long long msr_trl;
664         int ret;
665
666         if (trl) {
667                 msr_trl = trl;
668         } else {
669                 struct isst_pkg_ctdp pkg_dev;
670                 int trl[8];
671                 int i;
672
673                 ret = isst_get_ctdp_levels(id, &pkg_dev);
674                 if (ret)
675                         return ret;
676
677                 ret = isst_get_get_trl(id, pkg_dev.current_level, 0, trl);
678                 if (ret)
679                         return ret;
680
681                 msr_trl = 0;
682                 for (i = 0; i < 8; ++i) {
683                         unsigned long long _trl = trl[i];
684
685                         msr_trl |= (_trl << (i * 8));
686                 }
687         }
688         ret = isst_send_msr_command(id->cpu, 0x1AD, 1, &msr_trl);
689         if (ret)
690                 return ret;
691
692         return 0;
693 }
694
695 /* Return 1 if locked */
696 int isst_get_config_tdp_lock_status(struct isst_id *id)
697 {
698         unsigned long long tdp_control = 0;
699         int ret;
700
701         ret = isst_send_msr_command(id->cpu, 0x64b, 0, &tdp_control);
702         if (ret)
703                 return ret;
704
705         ret = !!(tdp_control & BIT(31));
706
707         return ret;
708 }
709
710 void isst_get_process_ctdp_complete(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev)
711 {
712         int i;
713
714         if (!pkg_dev->processed)
715                 return;
716
717         for (i = 0; i < pkg_dev->levels; ++i) {
718                 struct isst_pkg_ctdp_level_info *ctdp_level;
719
720                 ctdp_level = &pkg_dev->ctdp_level[i];
721                 if (ctdp_level->pbf_support)
722                         free_cpu_set(ctdp_level->pbf_info.core_cpumask);
723                 free_cpu_set(ctdp_level->core_cpumask);
724         }
725 }
726
727 void isst_get_uncore_p0_p1_info(struct isst_id *id, int config_index,
728                                 struct isst_pkg_ctdp_level_info *ctdp_level)
729 {
730         unsigned int resp;
731         int ret;
732
733         ctdp_level->uncore_pm = 0;
734         ctdp_level->uncore_p0 = 0;
735         ctdp_level->uncore_p1 = 0;
736
737         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
738                                      CONFIG_TDP_GET_RATIO_INFO, 0,
739                                      (BIT(16) | config_index) , &resp);
740         if (ret) {
741                 goto try_uncore_mbox;
742         }
743
744         ctdp_level->uncore_p0 = resp & GENMASK(7, 0);
745         ctdp_level->uncore_p1 = (resp & GENMASK(15, 8)) >> 8;
746         ctdp_level->uncore_pm = (resp & GENMASK(31, 24)) >> 24;
747
748         debug_printf(
749                 "cpu:%d ctdp:%d CONFIG_TDP_GET_RATIO_INFO resp:%x uncore p0:%d uncore p1:%d uncore pm:%d\n",
750                 id->cpu, config_index, resp, ctdp_level->uncore_p0, ctdp_level->uncore_p1,
751                 ctdp_level->uncore_pm);
752
753         return;
754
755 try_uncore_mbox:
756         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
757                                      CONFIG_TDP_GET_UNCORE_P0_P1_INFO, 0,
758                                      config_index, &resp);
759         if (ret) {
760                 ctdp_level->uncore_p0 = 0;
761                 ctdp_level->uncore_p1 = 0;
762                 return;
763         }
764
765         ctdp_level->uncore_p0 = resp & GENMASK(7, 0);
766         ctdp_level->uncore_p1 = (resp & GENMASK(15, 8)) >> 8;
767         debug_printf(
768                 "cpu:%d ctdp:%d CONFIG_TDP_GET_UNCORE_P0_P1_INFO resp:%x uncore p0:%d uncore p1:%d\n",
769                 id->cpu, config_index, resp, ctdp_level->uncore_p0,
770                 ctdp_level->uncore_p1);
771 }
772
773 void isst_get_p1_info(struct isst_id *id, int config_index,
774                       struct isst_pkg_ctdp_level_info *ctdp_level)
775 {
776         unsigned int resp;
777         int ret;
778         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_GET_P1_INFO, 0,
779                                      config_index, &resp);
780         if (ret) {
781                 ctdp_level->sse_p1 = 0;
782                 ctdp_level->avx2_p1 = 0;
783                 ctdp_level->avx512_p1 = 0;
784                 return;
785         }
786
787         ctdp_level->sse_p1 = resp & GENMASK(7, 0);
788         ctdp_level->avx2_p1 = (resp & GENMASK(15, 8)) >> 8;
789         ctdp_level->avx512_p1 = (resp & GENMASK(23, 16)) >> 16;
790         debug_printf(
791                 "cpu:%d ctdp:%d CONFIG_TDP_GET_P1_INFO resp:%x sse_p1:%d avx2_p1:%d avx512_p1:%d\n",
792                 id->cpu, config_index, resp, ctdp_level->sse_p1,
793                 ctdp_level->avx2_p1, ctdp_level->avx512_p1);
794 }
795
796 void isst_get_uncore_mem_freq(struct isst_id *id, int config_index,
797                               struct isst_pkg_ctdp_level_info *ctdp_level)
798 {
799         unsigned int resp;
800         int ret;
801
802         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_GET_MEM_FREQ,
803                                      0, config_index, &resp);
804         if (ret) {
805                 ctdp_level->mem_freq = 0;
806                 return;
807         }
808
809         ctdp_level->mem_freq = resp & GENMASK(7, 0);
810         if (is_spr_platform()) {
811                 ctdp_level->mem_freq *= 200;
812         } else if (is_icx_platform()) {
813                 if (ctdp_level->mem_freq < 7) {
814                         ctdp_level->mem_freq = (12 - ctdp_level->mem_freq) * 133.33 * 2 * 10;
815                         ctdp_level->mem_freq /= 10;
816                         if (ctdp_level->mem_freq % 10 > 5)
817                                 ctdp_level->mem_freq++;
818                 } else {
819                         ctdp_level->mem_freq = 0;
820                 }
821         } else {
822                 ctdp_level->mem_freq = 0;
823         }
824         debug_printf(
825                 "cpu:%d ctdp:%d CONFIG_TDP_GET_MEM_FREQ resp:%x uncore mem_freq:%d\n",
826                 id->cpu, config_index, resp, ctdp_level->mem_freq);
827 }
828
829 int isst_get_process_ctdp(struct isst_id *id, int tdp_level, struct isst_pkg_ctdp *pkg_dev)
830 {
831         int i, ret, valid = 0;
832
833         if (pkg_dev->processed)
834                 return 0;
835
836         ret = isst_get_ctdp_levels(id, pkg_dev);
837         if (ret)
838                 return ret;
839
840         debug_printf("cpu: %d ctdp enable:%d current level: %d levels:%d\n",
841                      id->cpu, pkg_dev->enabled, pkg_dev->current_level,
842                      pkg_dev->levels);
843
844         if (tdp_level != 0xff && tdp_level > pkg_dev->levels) {
845                 isst_display_error_info_message(1, "Invalid level", 0, 0);
846                 return -1;
847         }
848
849         if (!pkg_dev->enabled)
850                 isst_display_error_info_message(0, "perf-profile feature is not supported, just base-config level 0 is valid", 0, 0);
851
852         for (i = 0; i <= pkg_dev->levels; ++i) {
853                 struct isst_pkg_ctdp_level_info *ctdp_level;
854                 int trl_max_levels = isst_get_trl_max_levels();
855                 int j;
856
857                 if (tdp_level != 0xff && i != tdp_level)
858                         continue;
859
860                 debug_printf("cpu:%d Get Information for TDP level:%d\n", id->cpu,
861                              i);
862                 ctdp_level = &pkg_dev->ctdp_level[i];
863
864                 ctdp_level->level = i;
865                 ctdp_level->control_cpu = id->cpu;
866                 ctdp_level->pkg_id = id->pkg;
867                 ctdp_level->die_id = id->die;
868
869                 ret = isst_get_ctdp_control(id, i, ctdp_level);
870                 if (ret)
871                         continue;
872
873                 valid = 1;
874                 pkg_dev->processed = 1;
875                 ctdp_level->processed = 1;
876
877                 if (ctdp_level->pbf_support) {
878                         ret = isst_get_pbf_info(id, i, &ctdp_level->pbf_info);
879                         if (!ret)
880                                 ctdp_level->pbf_found = 1;
881                 }
882
883                 if (ctdp_level->fact_support) {
884                         ret = isst_get_fact_info(id, i, 0xff,
885                                                  &ctdp_level->fact_info);
886                         if (ret)
887                                 return ret;
888                 }
889
890                 if (!pkg_dev->enabled && is_skx_based_platform()) {
891                         int freq;
892
893                         freq = get_cpufreq_base_freq(id->cpu);
894                         if (freq > 0) {
895                                 ctdp_level->sse_p1 = freq / 100000;
896                                 ctdp_level->tdp_ratio = ctdp_level->sse_p1;
897                         }
898
899                         isst_get_get_trl_from_msr(id, ctdp_level->trl_ratios[0]);
900                         isst_get_trl_bucket_info(id, i, &ctdp_level->trl_cores);
901                         continue;
902                 }
903
904                 ret = isst_get_tdp_info(id, i, ctdp_level);
905                 if (ret)
906                         return ret;
907
908                 ret = isst_get_pwr_info(id, i, ctdp_level);
909                 if (ret)
910                         return ret;
911
912                 ctdp_level->core_cpumask_size =
913                         alloc_cpu_set(&ctdp_level->core_cpumask);
914                 ret = isst_get_coremask_info(id, i, ctdp_level);
915                 if (ret)
916                         return ret;
917
918                 ret = isst_get_trl_bucket_info(id, i, &ctdp_level->trl_cores);
919                 if (ret)
920                         return ret;
921
922                 for (j = 0; j < trl_max_levels; j++) {
923                         ret = isst_get_get_trl(id, i, j,
924                                        ctdp_level->trl_ratios[j]);
925                         if (ret)
926                                 return ret;
927                 }
928
929                 isst_get_uncore_p0_p1_info(id, i, ctdp_level);
930                 isst_get_p1_info(id, i, ctdp_level);
931                 isst_get_uncore_mem_freq(id, i, ctdp_level);
932         }
933
934         if (!valid)
935                 isst_display_error_info_message(0, "Invalid level, Can't get TDP control information at specified levels on cpu", 1, id->cpu);
936
937         return 0;
938 }
939
940 int isst_clos_get_clos_information(struct isst_id *id, int *enable, int *type)
941 {
942         unsigned int resp;
943         int ret;
944
945         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, 0, 0,
946                                      &resp);
947         if (ret)
948                 return ret;
949
950         debug_printf("cpu:%d CLOS_PM_QOS_CONFIG resp:%x\n", id->cpu, resp);
951
952         if (resp & BIT(1))
953                 *enable = 1;
954         else
955                 *enable = 0;
956
957         if (resp & BIT(2))
958                 *type = 1;
959         else
960                 *type = 0;
961
962         return 0;
963 }
964
965 int isst_pm_qos_config(struct isst_id *id, int enable_clos, int priority_type)
966 {
967         unsigned int req, resp;
968         int ret;
969
970         if (!enable_clos) {
971                 struct isst_pkg_ctdp pkg_dev;
972                 struct isst_pkg_ctdp_level_info ctdp_level;
973
974                 ret = isst_get_ctdp_levels(id, &pkg_dev);
975                 if (ret) {
976                         debug_printf("isst_get_ctdp_levels\n");
977                         return ret;
978                 }
979
980                 ret = isst_get_ctdp_control(id, pkg_dev.current_level,
981                                             &ctdp_level);
982                 if (ret)
983                         return ret;
984
985                 if (ctdp_level.fact_enabled) {
986                         isst_display_error_info_message(1, "Ignoring request, turbo-freq feature is still enabled", 0, 0);
987                         return -EINVAL;
988                 }
989                 ret = isst_write_pm_config(id, 0);
990                 if (ret)
991                         isst_display_error_info_message(0, "WRITE_PM_CONFIG command failed, ignoring error", 0, 0);
992         } else {
993                 ret = isst_write_pm_config(id, 1);
994                 if (ret)
995                         isst_display_error_info_message(0, "WRITE_PM_CONFIG command failed, ignoring error", 0, 0);
996         }
997
998         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, 0, 0,
999                                      &resp);
1000         if (ret) {
1001                 isst_display_error_info_message(1, "CLOS_PM_QOS_CONFIG command failed", 0, 0);
1002                 return ret;
1003         }
1004
1005         debug_printf("cpu:%d CLOS_PM_QOS_CONFIG resp:%x\n", id->cpu, resp);
1006
1007         req = resp;
1008
1009         if (enable_clos)
1010                 req = req | BIT(1);
1011         else
1012                 req = req & ~BIT(1);
1013
1014         if (priority_type > 1)
1015                 isst_display_error_info_message(1, "Invalid priority type: Changing type to ordered", 0, 0);
1016
1017         if (priority_type)
1018                 req = req | BIT(2);
1019         else
1020                 req = req & ~BIT(2);
1021
1022         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG,
1023                                      BIT(MBOX_CMD_WRITE_BIT), req, &resp);
1024         if (ret)
1025                 return ret;
1026
1027         debug_printf("cpu:%d CLOS_PM_QOS_CONFIG priority type:%d req:%x\n", id->cpu,
1028                      priority_type, req);
1029
1030         return 0;
1031 }
1032
1033 int isst_pm_get_clos(struct isst_id *id, int clos, struct isst_clos_config *clos_config)
1034 {
1035         unsigned int resp;
1036         int ret;
1037
1038         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_CLOS, clos, 0,
1039                                      &resp);
1040         if (ret)
1041                 return ret;
1042
1043         clos_config->epp = resp & 0x0f;
1044         clos_config->clos_prop_prio = (resp >> 4) & 0x0f;
1045         clos_config->clos_min = (resp >> 8) & 0xff;
1046         clos_config->clos_max = (resp >> 16) & 0xff;
1047         clos_config->clos_desired = (resp >> 24) & 0xff;
1048
1049         return 0;
1050 }
1051
1052 int isst_set_clos(struct isst_id *id, int clos, struct isst_clos_config *clos_config)
1053 {
1054         unsigned int req, resp;
1055         unsigned int param;
1056         int ret;
1057
1058         req = clos_config->epp & 0x0f;
1059         req |= (clos_config->clos_prop_prio & 0x0f) << 4;
1060         req |= (clos_config->clos_min & 0xff) << 8;
1061         req |= (clos_config->clos_max & 0xff) << 16;
1062         req |= (clos_config->clos_desired & 0xff) << 24;
1063
1064         param = BIT(MBOX_CMD_WRITE_BIT) | clos;
1065
1066         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_CLOS, param, req,
1067                                      &resp);
1068         if (ret)
1069                 return ret;
1070
1071         debug_printf("cpu:%d CLOS_PM_CLOS param:%x req:%x\n", id->cpu, param, req);
1072
1073         return 0;
1074 }
1075
1076 int isst_clos_get_assoc_status(struct isst_id *id, int *clos_id)
1077 {
1078         unsigned int resp;
1079         unsigned int param;
1080         int core_id, ret;
1081
1082         core_id = find_phy_core_num(id->cpu);
1083         param = core_id;
1084
1085         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param, 0,
1086                                      &resp);
1087         if (ret)
1088                 return ret;
1089
1090         debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x resp:%x\n", id->cpu, param,
1091                      resp);
1092         *clos_id = (resp >> 16) & 0x03;
1093
1094         return 0;
1095 }
1096
1097 int isst_clos_associate(struct isst_id *id, int clos_id)
1098 {
1099         unsigned int req, resp;
1100         unsigned int param;
1101         int core_id, ret;
1102
1103         req = (clos_id & 0x03) << 16;
1104         core_id = find_phy_core_num(id->cpu);
1105         param = BIT(MBOX_CMD_WRITE_BIT) | core_id;
1106
1107         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param,
1108                                      req, &resp);
1109         if (ret)
1110                 return ret;
1111
1112         debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x req:%x\n", id->cpu, param,
1113                      req);
1114
1115         return 0;
1116 }