8e224d154e040e8dcc2b6bd6a260972035c48cc9
[platform/kernel/linux-rpi.git] / tools / power / x86 / intel-speed-select / isst-core-mbox.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel Speed Select -- Enumerate and control features for Mailbox Interface
4  * Copyright (c) 2023 Intel Corporation.
5  */
6 #include "isst.h"
7
8 static int mbox_get_disp_freq_multiplier(void)
9 {
10         return DISP_FREQ_MULTIPLIER;
11 }
12
13 static int mbox_get_trl_max_levels(void)
14 {
15         return 3;
16 }
17
18 static char *mbox_get_trl_level_name(int level)
19 {
20         switch (level) {
21         case 0:
22                 return "sse";
23         case 1:
24                 return "avx2";
25         case 2:
26                 return "avx512";
27         default:
28                 return NULL;
29         }
30 }
31
32 static int mbox_is_punit_valid(struct isst_id *id)
33 {
34         if (id->cpu < 0)
35                 return 0;
36
37         if (id->pkg < 0 || id->die < 0 || id->punit)
38                 return 0;
39
40         return 1;
41 }
42
43 static int mbox_read_pm_config(struct isst_id *id, int *cp_state, int *cp_cap)
44 {
45         unsigned int resp;
46         int ret;
47
48         ret = isst_send_mbox_command(id->cpu, READ_PM_CONFIG, PM_FEATURE, 0, 0,
49                                         &resp);
50         if (ret)
51                 return ret;
52
53         debug_printf("cpu:%d READ_PM_CONFIG resp:%x\n", id->cpu, resp);
54
55         *cp_state = resp & BIT(16);
56         *cp_cap = resp & BIT(0) ? 1 : 0;
57
58         return 0;
59 }
60
61 static int mbox_get_config_levels(struct isst_id *id, struct isst_pkg_ctdp *pkg_dev)
62 {
63         unsigned int resp;
64         int ret;
65
66         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
67                                      CONFIG_TDP_GET_LEVELS_INFO, 0, 0, &resp);
68         if (ret) {
69                 pkg_dev->levels = 0;
70                 pkg_dev->locked = 1;
71                 pkg_dev->current_level = 0;
72                 pkg_dev->version = 0;
73                 pkg_dev->enabled = 0;
74                 return 0;
75         }
76
77         debug_printf("cpu:%d CONFIG_TDP_GET_LEVELS_INFO resp:%x\n", id->cpu, resp);
78
79         pkg_dev->version = resp & 0xff;
80         pkg_dev->levels = (resp >> 8) & 0xff;
81         pkg_dev->current_level = (resp >> 16) & 0xff;
82         pkg_dev->locked = !!(resp & BIT(24));
83         pkg_dev->enabled = !!(resp & BIT(31));
84
85         return 0;
86 }
87
88 static int mbox_get_ctdp_control(struct isst_id *id, int config_index,
89                           struct isst_pkg_ctdp_level_info *ctdp_level)
90 {
91         int cp_state, cp_cap;
92         unsigned int resp;
93         int ret;
94
95         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
96                                      CONFIG_TDP_GET_TDP_CONTROL, 0,
97                                      config_index, &resp);
98         if (ret)
99                 return ret;
100
101         ctdp_level->fact_support = resp & BIT(0);
102         ctdp_level->pbf_support = !!(resp & BIT(1));
103         ctdp_level->fact_enabled = !!(resp & BIT(16));
104         ctdp_level->pbf_enabled = !!(resp & BIT(17));
105
106         ret = isst_read_pm_config(id, &cp_state, &cp_cap);
107         if (ret) {
108                 debug_printf("cpu:%d pm_config is not supported\n", id->cpu);
109         } else {
110                 debug_printf("cpu:%d pm_config SST-CP state:%d cap:%d\n", id->cpu, cp_state, cp_cap);
111                 ctdp_level->sst_cp_support = cp_cap;
112                 ctdp_level->sst_cp_enabled = cp_state;
113         }
114
115         debug_printf(
116                 "cpu:%d CONFIG_TDP_GET_TDP_CONTROL resp:%x fact_support:%d pbf_support: %d fact_enabled:%d pbf_enabled:%d\n",
117                 id->cpu, resp, ctdp_level->fact_support, ctdp_level->pbf_support,
118                 ctdp_level->fact_enabled, ctdp_level->pbf_enabled);
119
120         return 0;
121 }
122
123 static void _get_uncore_p0_p1_info(struct isst_id *id, int config_index,
124                                 struct isst_pkg_ctdp_level_info *ctdp_level)
125 {
126         unsigned int resp;
127         int ret;
128
129         ctdp_level->uncore_pm = 0;
130         ctdp_level->uncore_p0 = 0;
131         ctdp_level->uncore_p1 = 0;
132
133         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
134                                      CONFIG_TDP_GET_RATIO_INFO, 0,
135                                      (BIT(16) | config_index) , &resp);
136         if (ret) {
137                 goto try_uncore_mbox;
138         }
139
140         ctdp_level->uncore_p0 = resp & GENMASK(7, 0);
141         ctdp_level->uncore_p1 = (resp & GENMASK(15, 8)) >> 8;
142         ctdp_level->uncore_pm = (resp & GENMASK(31, 24)) >> 24;
143
144         debug_printf(
145                 "cpu:%d ctdp:%d CONFIG_TDP_GET_RATIO_INFO resp:%x uncore p0:%d uncore p1:%d uncore pm:%d\n",
146                 id->cpu, config_index, resp, ctdp_level->uncore_p0, ctdp_level->uncore_p1,
147                 ctdp_level->uncore_pm);
148
149         return;
150
151 try_uncore_mbox:
152         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
153                                      CONFIG_TDP_GET_UNCORE_P0_P1_INFO, 0,
154                                      config_index, &resp);
155         if (ret) {
156                 ctdp_level->uncore_p0 = 0;
157                 ctdp_level->uncore_p1 = 0;
158                 return;
159         }
160
161         ctdp_level->uncore_p0 = resp & GENMASK(7, 0);
162         ctdp_level->uncore_p1 = (resp & GENMASK(15, 8)) >> 8;
163         debug_printf(
164                 "cpu:%d ctdp:%d CONFIG_TDP_GET_UNCORE_P0_P1_INFO resp:%x uncore p0:%d uncore p1:%d\n",
165                 id->cpu, config_index, resp, ctdp_level->uncore_p0,
166                 ctdp_level->uncore_p1);
167 }
168
169 static int _set_uncore_min_max(struct isst_id *id, int max, int freq)
170 {
171         char buffer[128], freq_str[16];
172         int fd, ret, len;
173
174         if (max)
175                 snprintf(buffer, sizeof(buffer),
176                          "/sys/devices/system/cpu/intel_uncore_frequency/package_%02d_die_%02d/max_freq_khz", id->pkg, id->die);
177         else
178                 snprintf(buffer, sizeof(buffer),
179                          "/sys/devices/system/cpu/intel_uncore_frequency/package_%02d_die_%02d/min_freq_khz", id->pkg, id->die);
180
181         fd = open(buffer, O_WRONLY);
182         if (fd < 0)
183                 return fd;
184
185         snprintf(freq_str, sizeof(freq_str), "%d", freq);
186         len = strlen(freq_str);
187         ret = write(fd, freq_str, len);
188         if (ret == -1) {
189                 close(fd);
190                 return ret;
191         }
192         close(fd);
193
194         return 0;
195 }
196
197 static void mbox_adjust_uncore_freq(struct isst_id *id, int config_index,
198                                 struct isst_pkg_ctdp_level_info *ctdp_level)
199 {
200         _get_uncore_p0_p1_info(id, config_index, ctdp_level);
201         if (ctdp_level->uncore_pm)
202                 _set_uncore_min_max(id, 0, ctdp_level->uncore_pm * 100000);
203
204         if (ctdp_level->uncore_p0)
205                 _set_uncore_min_max(id, 1, ctdp_level->uncore_p0 * 100000);
206 }
207
208 static void _get_p1_info(struct isst_id *id, int config_index,
209                       struct isst_pkg_ctdp_level_info *ctdp_level)
210 {
211         unsigned int resp;
212         int ret;
213         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_GET_P1_INFO, 0,
214                                      config_index, &resp);
215         if (ret) {
216                 ctdp_level->sse_p1 = 0;
217                 ctdp_level->avx2_p1 = 0;
218                 ctdp_level->avx512_p1 = 0;
219                 return;
220         }
221
222         ctdp_level->sse_p1 = resp & GENMASK(7, 0);
223         ctdp_level->avx2_p1 = (resp & GENMASK(15, 8)) >> 8;
224         ctdp_level->avx512_p1 = (resp & GENMASK(23, 16)) >> 16;
225         debug_printf(
226                 "cpu:%d ctdp:%d CONFIG_TDP_GET_P1_INFO resp:%x sse_p1:%d avx2_p1:%d avx512_p1:%d\n",
227                 id->cpu, config_index, resp, ctdp_level->sse_p1,
228                 ctdp_level->avx2_p1, ctdp_level->avx512_p1);
229 }
230
231 static void _get_uncore_mem_freq(struct isst_id *id, int config_index,
232                               struct isst_pkg_ctdp_level_info *ctdp_level)
233 {
234         unsigned int resp;
235         int ret;
236
237         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_GET_MEM_FREQ,
238                                      0, config_index, &resp);
239         if (ret) {
240                 ctdp_level->mem_freq = 0;
241                 return;
242         }
243
244         ctdp_level->mem_freq = resp & GENMASK(7, 0);
245         if (is_spr_platform()) {
246                 ctdp_level->mem_freq *= 200;
247         } else if (is_icx_platform()) {
248                 if (ctdp_level->mem_freq < 7) {
249                         ctdp_level->mem_freq = (12 - ctdp_level->mem_freq) * 133.33 * 2 * 10;
250                         ctdp_level->mem_freq /= 10;
251                         if (ctdp_level->mem_freq % 10 > 5)
252                                 ctdp_level->mem_freq++;
253                 } else {
254                         ctdp_level->mem_freq = 0;
255                 }
256         } else {
257                 ctdp_level->mem_freq = 0;
258         }
259         debug_printf(
260                 "cpu:%d ctdp:%d CONFIG_TDP_GET_MEM_FREQ resp:%x uncore mem_freq:%d\n",
261                 id->cpu, config_index, resp, ctdp_level->mem_freq);
262 }
263
264 static int mbox_get_tdp_info(struct isst_id *id, int config_index,
265                       struct isst_pkg_ctdp_level_info *ctdp_level)
266 {
267         unsigned int resp;
268         int ret;
269
270         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_GET_TDP_INFO,
271                                      0, config_index, &resp);
272         if (ret) {
273                 isst_display_error_info_message(1, "Invalid level, Can't get TDP information at level", 1, config_index);
274                 return ret;
275         }
276
277         ctdp_level->pkg_tdp = resp & GENMASK(14, 0);
278         ctdp_level->tdp_ratio = (resp & GENMASK(23, 16)) >> 16;
279
280         debug_printf(
281                 "cpu:%d ctdp:%d CONFIG_TDP_GET_TDP_INFO resp:%x tdp_ratio:%d pkg_tdp:%d\n",
282                 id->cpu, config_index, resp, ctdp_level->tdp_ratio,
283                 ctdp_level->pkg_tdp);
284
285         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_GET_TJMAX_INFO,
286                                      0, config_index, &resp);
287         if (ret)
288                 return ret;
289
290         ctdp_level->t_proc_hot = resp & GENMASK(7, 0);
291
292         _get_uncore_p0_p1_info(id, config_index, ctdp_level);
293         _get_p1_info(id, config_index, ctdp_level);
294         _get_uncore_mem_freq(id, config_index, ctdp_level);
295
296         debug_printf(
297                 "cpu:%d ctdp:%d CONFIG_TDP_GET_TJMAX_INFO resp:%x t_proc_hot:%d\n",
298                 id->cpu, config_index, resp, ctdp_level->t_proc_hot);
299
300         return 0;
301 }
302
303 static int mbox_get_pwr_info(struct isst_id *id, int config_index,
304                       struct isst_pkg_ctdp_level_info *ctdp_level)
305 {
306         unsigned int resp;
307         int ret;
308
309         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_GET_PWR_INFO,
310                                      0, config_index, &resp);
311         if (ret)
312                 return ret;
313
314         ctdp_level->pkg_max_power = resp & GENMASK(14, 0);
315         ctdp_level->pkg_min_power = (resp & GENMASK(30, 16)) >> 16;
316
317         debug_printf(
318                 "cpu:%d ctdp:%d CONFIG_TDP_GET_PWR_INFO resp:%x pkg_max_power:%d pkg_min_power:%d\n",
319                 id->cpu, config_index, resp, ctdp_level->pkg_max_power,
320                 ctdp_level->pkg_min_power);
321
322         return 0;
323 }
324
325 static int mbox_get_coremask_info(struct isst_id *id, int config_index,
326                            struct isst_pkg_ctdp_level_info *ctdp_level)
327 {
328         unsigned int resp;
329         int i, ret;
330
331         ctdp_level->cpu_count = 0;
332         for (i = 0; i < 2; ++i) {
333                 unsigned long long mask;
334                 int cpu_count = 0;
335
336                 ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
337                                              CONFIG_TDP_GET_CORE_MASK, 0,
338                                              (i << 8) | config_index, &resp);
339                 if (ret)
340                         return ret;
341
342                 debug_printf(
343                         "cpu:%d ctdp:%d mask:%d CONFIG_TDP_GET_CORE_MASK resp:%x\n",
344                         id->cpu, config_index, i, resp);
345
346                 mask = (unsigned long long)resp << (32 * i);
347                 set_cpu_mask_from_punit_coremask(id, mask,
348                                                  ctdp_level->core_cpumask_size,
349                                                  ctdp_level->core_cpumask,
350                                                  &cpu_count);
351                 ctdp_level->cpu_count += cpu_count;
352                 debug_printf("cpu:%d ctdp:%d mask:%d cpu count:%d\n", id->cpu,
353                              config_index, i, ctdp_level->cpu_count);
354         }
355
356         return 0;
357 }
358
359 static int mbox_get_get_trl(struct isst_id *id, int level, int avx_level, int *trl)
360 {
361         unsigned int req, resp;
362         int ret;
363
364         req = level | (avx_level << 16);
365         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
366                                      CONFIG_TDP_GET_TURBO_LIMIT_RATIOS, 0, req,
367                                      &resp);
368         if (ret)
369                 return ret;
370
371         debug_printf(
372                 "cpu:%d CONFIG_TDP_GET_TURBO_LIMIT_RATIOS req:%x resp:%x\n",
373                 id->cpu, req, resp);
374
375         trl[0] = resp & GENMASK(7, 0);
376         trl[1] = (resp & GENMASK(15, 8)) >> 8;
377         trl[2] = (resp & GENMASK(23, 16)) >> 16;
378         trl[3] = (resp & GENMASK(31, 24)) >> 24;
379
380         req = level | BIT(8) | (avx_level << 16);
381         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
382                                      CONFIG_TDP_GET_TURBO_LIMIT_RATIOS, 0, req,
383                                      &resp);
384         if (ret)
385                 return ret;
386
387         debug_printf("cpu:%d CONFIG_TDP_GET_TURBO_LIMIT req:%x resp:%x\n", id->cpu,
388                      req, resp);
389
390         trl[4] = resp & GENMASK(7, 0);
391         trl[5] = (resp & GENMASK(15, 8)) >> 8;
392         trl[6] = (resp & GENMASK(23, 16)) >> 16;
393         trl[7] = (resp & GENMASK(31, 24)) >> 24;
394
395         return 0;
396 }
397
398 static int mbox_get_get_trls(struct isst_id *id, int level, struct isst_pkg_ctdp_level_info *ctdp_level)
399 {
400         int trl_max_levels = isst_get_trl_max_levels();
401         int i, ret;
402
403         for (i = 0; i < trl_max_levels; i++) {
404                 ret = mbox_get_get_trl(id, level, i, ctdp_level->trl_ratios[i]);
405                 if (ret)
406                         return ret;
407         }
408         return 0;
409 }
410
411 static int mbox_get_trl_bucket_info(struct isst_id *id, int level, unsigned long long *buckets_info)
412 {
413         int ret;
414
415         debug_printf("cpu:%d bucket info via MSR\n", id->cpu);
416
417         *buckets_info = 0;
418
419         ret = isst_send_msr_command(id->cpu, 0x1ae, 0, buckets_info);
420         if (ret)
421                 return ret;
422
423         debug_printf("cpu:%d bucket info via MSR successful 0x%llx\n", id->cpu,
424                      *buckets_info);
425
426         return 0;
427 }
428
429 static int mbox_set_tdp_level(struct isst_id *id, int tdp_level)
430 {
431         unsigned int resp;
432         int ret;
433
434
435         if (isst_get_config_tdp_lock_status(id)) {
436                 isst_display_error_info_message(1, "TDP is locked", 0, 0);
437                 return -1;
438
439         }
440
441         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP, CONFIG_TDP_SET_LEVEL, 0,
442                                      tdp_level, &resp);
443         if (ret) {
444                 isst_display_error_info_message(1, "Set TDP level failed for level", 1, tdp_level);
445                 return ret;
446         }
447
448         return 0;
449 }
450
451 static int mbox_get_pbf_info(struct isst_id *id, int level, struct isst_pbf_info *pbf_info)
452 {
453         int max_punit_core, max_mask_index;
454         unsigned int req, resp;
455         int i, ret;
456
457         max_punit_core = get_max_punit_core_id(id);
458         max_mask_index = max_punit_core > 32 ? 2 : 1;
459
460         for (i = 0; i < max_mask_index; ++i) {
461                 unsigned long long mask;
462                 int count;
463
464                 ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
465                                              CONFIG_TDP_PBF_GET_CORE_MASK_INFO,
466                                              0, (i << 8) | level, &resp);
467                 if (ret)
468                         break;
469
470                 debug_printf(
471                         "cpu:%d CONFIG_TDP_PBF_GET_CORE_MASK_INFO resp:%x\n",
472                         id->cpu, resp);
473
474                 mask = (unsigned long long)resp << (32 * i);
475                 set_cpu_mask_from_punit_coremask(id, mask,
476                                                  pbf_info->core_cpumask_size,
477                                                  pbf_info->core_cpumask,
478                                                  &count);
479         }
480
481         req = level;
482         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
483                                      CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO, 0, req,
484                                      &resp);
485         if (ret)
486                 return ret;
487
488         debug_printf("cpu:%d CONFIG_TDP_PBF_GET_P1HI_P1LO_INFO resp:%x\n", id->cpu,
489                      resp);
490
491         pbf_info->p1_low = resp & 0xff;
492         pbf_info->p1_high = (resp & GENMASK(15, 8)) >> 8;
493
494         req = level;
495         ret = isst_send_mbox_command(
496                 id->cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TDP_INFO, 0, req, &resp);
497         if (ret)
498                 return ret;
499
500         debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TDP_INFO resp:%x\n", id->cpu, resp);
501
502         pbf_info->tdp = resp & 0xffff;
503
504         req = level;
505         ret = isst_send_mbox_command(
506                 id->cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_TJ_MAX_INFO, 0, req, &resp);
507         if (ret)
508                 return ret;
509
510         debug_printf("cpu:%d CONFIG_TDP_PBF_GET_TJ_MAX_INFO resp:%x\n", id->cpu,
511                      resp);
512         pbf_info->t_control = (resp >> 8) & 0xff;
513         pbf_info->t_prochot = resp & 0xff;
514
515         return 0;
516 }
517
518 static int mbox_set_pbf_fact_status(struct isst_id *id, int pbf, int enable)
519 {
520         struct isst_pkg_ctdp pkg_dev;
521         struct isst_pkg_ctdp_level_info ctdp_level;
522         int current_level;
523         unsigned int req = 0, resp;
524         int ret;
525
526         ret = isst_get_ctdp_levels(id, &pkg_dev);
527         if (ret)
528                 debug_printf("cpu:%d No support for dynamic ISST\n", id->cpu);
529
530         current_level = pkg_dev.current_level;
531
532         ret = isst_get_ctdp_control(id, current_level, &ctdp_level);
533         if (ret)
534                 return ret;
535
536         if (pbf) {
537                 if (ctdp_level.fact_enabled)
538                         req = BIT(16);
539
540                 if (enable)
541                         req |= BIT(17);
542                 else
543                         req &= ~BIT(17);
544         } else {
545
546                 if (enable && !ctdp_level.sst_cp_enabled)
547                         isst_display_error_info_message(0, "Make sure to execute before: core-power enable", 0, 0);
548
549                 if (ctdp_level.pbf_enabled)
550                         req = BIT(17);
551
552                 if (enable)
553                         req |= BIT(16);
554                 else
555                         req &= ~BIT(16);
556         }
557
558         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
559                                      CONFIG_TDP_SET_TDP_CONTROL, 0, req, &resp);
560         if (ret)
561                 return ret;
562
563         debug_printf("cpu:%d CONFIG_TDP_SET_TDP_CONTROL pbf/fact:%d req:%x\n",
564                      id->cpu, pbf, req);
565
566         return 0;
567 }
568
569 static int _get_fact_bucket_info(struct isst_id *id, int level,
570                               struct isst_fact_bucket_info *bucket_info)
571 {
572         unsigned int resp;
573         int i, k, ret;
574
575         for (i = 0; i < 2; ++i) {
576                 int j;
577
578                 ret = isst_send_mbox_command(
579                         id->cpu, CONFIG_TDP,
580                         CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES, 0,
581                         (i << 8) | level, &resp);
582                 if (ret)
583                         return ret;
584
585                 debug_printf(
586                         "cpu:%d CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_NUMCORES index:%d level:%d resp:%x\n",
587                         id->cpu, i, level, resp);
588
589                 for (j = 0; j < 4; ++j) {
590                         bucket_info[j + (i * 4)].hp_cores =
591                                 (resp >> (j * 8)) & 0xff;
592                 }
593         }
594
595         for (k = 0; k < 3; ++k) {
596                 for (i = 0; i < 2; ++i) {
597                         int j;
598
599                         ret = isst_send_mbox_command(
600                                 id->cpu, CONFIG_TDP,
601                                 CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS, 0,
602                                 (k << 16) | (i << 8) | level, &resp);
603                         if (ret)
604                                 return ret;
605
606                         debug_printf(
607                                 "cpu:%d CONFIG_TDP_GET_FACT_HP_TURBO_LIMIT_RATIOS index:%d level:%d avx:%d resp:%x\n",
608                                 id->cpu, i, level, k, resp);
609
610                         for (j = 0; j < 4; ++j) {
611                                 bucket_info[j + (i * 4)].hp_ratios[k] =
612                                         (resp >> (j * 8)) & 0xff;
613                         }
614                 }
615         }
616
617         return 0;
618 }
619
620 static int mbox_get_fact_info(struct isst_id *id, int level, int fact_bucket, struct isst_fact_info *fact_info)
621 {
622         unsigned int resp;
623         int j, ret, print;
624
625         ret = isst_send_mbox_command(id->cpu, CONFIG_TDP,
626                                      CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO, 0,
627                                      level, &resp);
628         if (ret)
629                 return ret;
630
631         debug_printf("cpu:%d CONFIG_TDP_GET_FACT_LP_CLIPPING_RATIO resp:%x\n",
632                      id->cpu, resp);
633
634         fact_info->lp_ratios[0] = resp & 0xff;
635         fact_info->lp_ratios[1] = (resp >> 8) & 0xff;
636         fact_info->lp_ratios[2] = (resp >> 16) & 0xff;
637
638         ret = _get_fact_bucket_info(id, level, fact_info->bucket_info);
639         if (ret)
640                 return ret;
641
642         print = 0;
643         for (j = 0; j < ISST_FACT_MAX_BUCKETS; ++j) {
644                 if (fact_bucket != 0xff && fact_bucket != j)
645                         continue;
646
647                 if (!fact_info->bucket_info[j].hp_cores)
648                         break;
649
650                 print = 1;
651         }
652         if (!print) {
653                 isst_display_error_info_message(1, "Invalid bucket", 0, 0);
654                 return -1;
655         }
656
657         return 0;
658 }
659
660 static int mbox_get_clos_information(struct isst_id *id, int *enable, int *type)
661 {
662         unsigned int resp;
663         int ret;
664
665         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, 0, 0,
666                                      &resp);
667         if (ret)
668                 return ret;
669
670         debug_printf("cpu:%d CLOS_PM_QOS_CONFIG resp:%x\n", id->cpu, resp);
671
672         if (resp & BIT(1))
673                 *enable = 1;
674         else
675                 *enable = 0;
676
677         if (resp & BIT(2))
678                 *type = 1;
679         else
680                 *type = 0;
681
682         return 0;
683 }
684
685 static int _write_pm_config(struct isst_id *id, int cp_state)
686 {
687         unsigned int req, resp;
688         int ret;
689
690         if (cp_state)
691                 req = BIT(16);
692         else
693                 req = 0;
694
695         ret = isst_send_mbox_command(id->cpu, WRITE_PM_CONFIG, PM_FEATURE, 0, req,
696                                      &resp);
697         if (ret)
698                 return ret;
699
700         debug_printf("cpu:%d WRITE_PM_CONFIG resp:%x\n", id->cpu, resp);
701
702         return 0;
703 }
704
705 static int mbox_pm_qos_config(struct isst_id *id, int enable_clos, int priority_type)
706 {
707         unsigned int req, resp;
708         int ret;
709
710         if (!enable_clos) {
711                 struct isst_pkg_ctdp pkg_dev;
712                 struct isst_pkg_ctdp_level_info ctdp_level;
713
714                 ret = isst_get_ctdp_levels(id, &pkg_dev);
715                 if (ret) {
716                         debug_printf("isst_get_ctdp_levels\n");
717                         return ret;
718                 }
719
720                 ret = isst_get_ctdp_control(id, pkg_dev.current_level,
721                                             &ctdp_level);
722                 if (ret)
723                         return ret;
724
725                 if (ctdp_level.fact_enabled) {
726                         isst_display_error_info_message(1, "Ignoring request, turbo-freq feature is still enabled", 0, 0);
727                         return -EINVAL;
728                 }
729                 ret = _write_pm_config(id, 0);
730                 if (ret)
731                         isst_display_error_info_message(0, "WRITE_PM_CONFIG command failed, ignoring error", 0, 0);
732         } else {
733                 ret = _write_pm_config(id, 1);
734                 if (ret)
735                         isst_display_error_info_message(0, "WRITE_PM_CONFIG command failed, ignoring error", 0, 0);
736         }
737
738         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, 0, 0,
739                                      &resp);
740         if (ret) {
741                 isst_display_error_info_message(1, "CLOS_PM_QOS_CONFIG command failed", 0, 0);
742                 return ret;
743         }
744
745         debug_printf("cpu:%d CLOS_PM_QOS_CONFIG resp:%x\n", id->cpu, resp);
746
747         req = resp;
748
749         if (enable_clos)
750                 req = req | BIT(1);
751         else
752                 req = req & ~BIT(1);
753
754         if (priority_type > 1)
755                 isst_display_error_info_message(1, "Invalid priority type: Changing type to ordered", 0, 0);
756
757         if (priority_type)
758                 req = req | BIT(2);
759         else
760                 req = req & ~BIT(2);
761
762         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG,
763                                      BIT(MBOX_CMD_WRITE_BIT), req, &resp);
764         if (ret)
765                 return ret;
766
767         debug_printf("cpu:%d CLOS_PM_QOS_CONFIG priority type:%d req:%x\n", id->cpu,
768                      priority_type, req);
769
770         return 0;
771 }
772
773 static int mbox_pm_get_clos(struct isst_id *id, int clos, struct isst_clos_config *clos_config)
774 {
775         unsigned int resp;
776         int ret;
777
778         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_CLOS, clos, 0,
779                                      &resp);
780         if (ret)
781                 return ret;
782
783         clos_config->epp = resp & 0x0f;
784         clos_config->clos_prop_prio = (resp >> 4) & 0x0f;
785         clos_config->clos_min = (resp >> 8) & 0xff;
786         clos_config->clos_max = (resp >> 16) & 0xff;
787         clos_config->clos_desired = (resp >> 24) & 0xff;
788
789         return 0;
790 }
791
792 static int mbox_set_clos(struct isst_id *id, int clos, struct isst_clos_config *clos_config)
793 {
794         unsigned int req, resp;
795         unsigned int param;
796         int ret;
797
798         req = clos_config->epp & 0x0f;
799         req |= (clos_config->clos_prop_prio & 0x0f) << 4;
800         req |= (clos_config->clos_min & 0xff) << 8;
801         req |= (clos_config->clos_max & 0xff) << 16;
802         req |= (clos_config->clos_desired & 0xff) << 24;
803
804         param = BIT(MBOX_CMD_WRITE_BIT) | clos;
805
806         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PM_CLOS, param, req,
807                                      &resp);
808         if (ret)
809                 return ret;
810
811         debug_printf("cpu:%d CLOS_PM_CLOS param:%x req:%x\n", id->cpu, param, req);
812
813         return 0;
814 }
815
816 static int mbox_clos_get_assoc_status(struct isst_id *id, int *clos_id)
817 {
818         unsigned int resp;
819         unsigned int param;
820         int core_id, ret;
821
822         core_id = find_phy_core_num(id->cpu);
823         param = core_id;
824
825         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param, 0,
826                                      &resp);
827         if (ret)
828                 return ret;
829
830         debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x resp:%x\n", id->cpu, param,
831                      resp);
832         *clos_id = (resp >> 16) & 0x03;
833
834         return 0;
835 }
836
837 static int mbox_clos_associate(struct isst_id *id, int clos_id)
838 {
839         unsigned int req, resp;
840         unsigned int param;
841         int core_id, ret;
842
843         req = (clos_id & 0x03) << 16;
844         core_id = find_phy_core_num(id->cpu);
845         param = BIT(MBOX_CMD_WRITE_BIT) | core_id;
846
847         ret = isst_send_mbox_command(id->cpu, CONFIG_CLOS, CLOS_PQR_ASSOC, param,
848                                      req, &resp);
849         if (ret)
850                 return ret;
851
852         debug_printf("cpu:%d CLOS_PQR_ASSOC param:%x req:%x\n", id->cpu, param,
853                      req);
854
855         return 0;
856 }
857
858 static struct isst_platform_ops mbox_ops = {
859         .get_disp_freq_multiplier = mbox_get_disp_freq_multiplier,
860         .get_trl_max_levels = mbox_get_trl_max_levels,
861         .get_trl_level_name = mbox_get_trl_level_name,
862         .is_punit_valid = mbox_is_punit_valid,
863         .read_pm_config = mbox_read_pm_config,
864         .get_config_levels = mbox_get_config_levels,
865         .get_ctdp_control = mbox_get_ctdp_control,
866         .get_tdp_info = mbox_get_tdp_info,
867         .get_pwr_info = mbox_get_pwr_info,
868         .get_coremask_info = mbox_get_coremask_info,
869         .get_get_trl = mbox_get_get_trl,
870         .get_get_trls = mbox_get_get_trls,
871         .get_trl_bucket_info = mbox_get_trl_bucket_info,
872         .set_tdp_level = mbox_set_tdp_level,
873         .get_pbf_info = mbox_get_pbf_info,
874         .set_pbf_fact_status = mbox_set_pbf_fact_status,
875         .get_fact_info = mbox_get_fact_info,
876         .adjust_uncore_freq = mbox_adjust_uncore_freq,
877         .get_clos_information = mbox_get_clos_information,
878         .pm_qos_config = mbox_pm_qos_config,
879         .pm_get_clos = mbox_pm_get_clos,
880         .set_clos = mbox_set_clos,
881         .clos_get_assoc_status = mbox_clos_get_assoc_status,
882         .clos_associate = mbox_clos_associate,
883 };
884
885 struct isst_platform_ops *mbox_get_platform_ops(void)
886 {
887         return &mbox_ops;
888 }