blob: 18a3edeeb58faddf05b8b673cd6040a66947e002 [file] [log] [blame]
Eric Huangaabcb7c2015-08-26 16:52:28 -04001/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/fb.h>
26#include "linux/delay.h"
27
28#include "hwmgr.h"
29#include "fiji_smumgr.h"
30#include "atombios.h"
31#include "hardwaremanager.h"
32#include "ppatomctrl.h"
33#include "atombios.h"
34#include "cgs_common.h"
35#include "fiji_dyn_defaults.h"
36#include "fiji_powertune.h"
37#include "smu73.h"
38#include "smu/smu_7_1_3_d.h"
39#include "smu/smu_7_1_3_sh_mask.h"
40#include "gmc/gmc_8_1_d.h"
41#include "gmc/gmc_8_1_sh_mask.h"
42#include "bif/bif_5_0_d.h"
43#include "bif/bif_5_0_sh_mask.h"
44#include "dce/dce_10_0_d.h"
45#include "dce/dce_10_0_sh_mask.h"
46#include "pppcielanes.h"
47#include "fiji_hwmgr.h"
48#include "tonga_processpptables.h"
49#include "tonga_pptable.h"
50#include "pp_debug.h"
51#include "pp_acpi.h"
Alex Deucher16881da2015-11-11 20:18:52 -050052#include "amd_pcie_helpers.h"
Eric Huang60103812015-11-27 14:09:53 -050053#include "cgs_linux.h"
54#include "ppinterrupt.h"
Eric Huangaabcb7c2015-08-26 16:52:28 -040055
Eric Huang91c4c982015-11-20 15:58:11 -050056#include "fiji_clockpowergating.h"
Eric Huang60103812015-11-27 14:09:53 -050057#include "fiji_thermal.h"
Eric Huang91c4c982015-11-20 15:58:11 -050058
Eric Huangaabcb7c2015-08-26 16:52:28 -040059#define VOLTAGE_SCALE 4
60#define SMC_RAM_END 0x40000
61#define VDDC_VDDCI_DELTA 300
62
63#define MC_SEQ_MISC0_GDDR5_SHIFT 28
64#define MC_SEQ_MISC0_GDDR5_MASK 0xf0000000
65#define MC_SEQ_MISC0_GDDR5_VALUE 5
66
67#define MC_CG_ARB_FREQ_F0 0x0a /* boot-up default */
68#define MC_CG_ARB_FREQ_F1 0x0b
69#define MC_CG_ARB_FREQ_F2 0x0c
70#define MC_CG_ARB_FREQ_F3 0x0d
71
72/* From smc_reg.h */
73#define SMC_CG_IND_START 0xc0030000
74#define SMC_CG_IND_END 0xc0040000 /* First byte after SMC_CG_IND */
75
76#define VOLTAGE_SCALE 4
77#define VOLTAGE_VID_OFFSET_SCALE1 625
78#define VOLTAGE_VID_OFFSET_SCALE2 100
79
80#define VDDC_VDDCI_DELTA 300
81
82#define ixSWRST_COMMAND_1 0x1400103
83#define MC_SEQ_CNTL__CAC_EN_MASK 0x40000000
84
85/** Values for the CG_THERMAL_CTRL::DPM_EVENT_SRC field. */
86enum DPM_EVENT_SRC {
87 DPM_EVENT_SRC_ANALOG = 0, /* Internal analog trip point */
88 DPM_EVENT_SRC_EXTERNAL = 1, /* External (GPIO 17) signal */
89 DPM_EVENT_SRC_DIGITAL = 2, /* Internal digital trip point (DIG_THERM_DPM) */
90 DPM_EVENT_SRC_ANALOG_OR_EXTERNAL = 3, /* Internal analog or external */
91 DPM_EVENT_SRC_DIGITAL_OR_EXTERNAL = 4 /* Internal digital or external */
92};
93
Eric Huangaabcb7c2015-08-26 16:52:28 -040094
95/* [2.5%,~2.5%] Clock stretched is multiple of 2.5% vs
96 * not and [Fmin, Fmax, LDO_REFSEL, USE_FOR_LOW_FREQ]
97 */
Nils Wallméniusf498d9e2016-04-10 16:29:59 +020098static const uint16_t fiji_clock_stretcher_lookup_table[2][4] =
99{ {600, 1050, 3, 0}, {600, 1050, 6, 1} };
Eric Huangaabcb7c2015-08-26 16:52:28 -0400100
101/* [FF, SS] type, [] 4 voltage ranges, and
102 * [Floor Freq, Boundary Freq, VID min , VID max]
103 */
Nils Wallméniusf498d9e2016-04-10 16:29:59 +0200104static const uint32_t fiji_clock_stretcher_ddt_table[2][4][4] =
Eric Huangaabcb7c2015-08-26 16:52:28 -0400105{ { {265, 529, 120, 128}, {325, 650, 96, 119}, {430, 860, 32, 95}, {0, 0, 0, 31} },
106 { {275, 550, 104, 112}, {319, 638, 96, 103}, {360, 720, 64, 95}, {384, 768, 32, 63} } };
107
108/* [Use_For_Low_freq] value, [0%, 5%, 10%, 7.14%, 14.28%, 20%]
109 * (coming from PWR_CKS_CNTL.stretch_amount reg spec)
110 */
Nils Wallméniusf498d9e2016-04-10 16:29:59 +0200111static const uint8_t fiji_clock_stretch_amount_conversion[2][6] =
112{ {0, 1, 3, 2, 4, 5}, {0, 2, 4, 5, 6, 5} };
Eric Huangaabcb7c2015-08-26 16:52:28 -0400113
Nils Wallméniusf498d9e2016-04-10 16:29:59 +0200114static const unsigned long PhwFiji_Magic = (unsigned long)(PHM_VIslands_Magic);
Eric Huangaabcb7c2015-08-26 16:52:28 -0400115
116struct fiji_power_state *cast_phw_fiji_power_state(
117 struct pp_hw_power_state *hw_ps)
118{
119 PP_ASSERT_WITH_CODE((PhwFiji_Magic == hw_ps->magic),
120 "Invalid Powerstate Type!",
121 return NULL;);
122
123 return (struct fiji_power_state *)hw_ps;
124}
125
126const struct fiji_power_state *cast_const_phw_fiji_power_state(
127 const struct pp_hw_power_state *hw_ps)
128{
129 PP_ASSERT_WITH_CODE((PhwFiji_Magic == hw_ps->magic),
130 "Invalid Powerstate Type!",
131 return NULL;);
132
133 return (const struct fiji_power_state *)hw_ps;
134}
135
136static bool fiji_is_dpm_running(struct pp_hwmgr *hwmgr)
137{
138 return (1 == PHM_READ_INDIRECT_FIELD(hwmgr->device,
139 CGS_IND_REG__SMC, FEATURE_STATUS, VOLTAGE_CONTROLLER_ON))
140 ? true : false;
141}
142
143static void fiji_init_dpm_defaults(struct pp_hwmgr *hwmgr)
144{
145 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
146 struct fiji_ulv_parm *ulv = &data->ulv;
147
148 ulv->cg_ulv_parameter = PPFIJI_CGULVPARAMETER_DFLT;
149 data->voting_rights_clients0 = PPFIJI_VOTINGRIGHTSCLIENTS_DFLT0;
150 data->voting_rights_clients1 = PPFIJI_VOTINGRIGHTSCLIENTS_DFLT1;
151 data->voting_rights_clients2 = PPFIJI_VOTINGRIGHTSCLIENTS_DFLT2;
152 data->voting_rights_clients3 = PPFIJI_VOTINGRIGHTSCLIENTS_DFLT3;
153 data->voting_rights_clients4 = PPFIJI_VOTINGRIGHTSCLIENTS_DFLT4;
154 data->voting_rights_clients5 = PPFIJI_VOTINGRIGHTSCLIENTS_DFLT5;
155 data->voting_rights_clients6 = PPFIJI_VOTINGRIGHTSCLIENTS_DFLT6;
156 data->voting_rights_clients7 = PPFIJI_VOTINGRIGHTSCLIENTS_DFLT7;
157
158 data->static_screen_threshold_unit =
159 PPFIJI_STATICSCREENTHRESHOLDUNIT_DFLT;
160 data->static_screen_threshold =
161 PPFIJI_STATICSCREENTHRESHOLD_DFLT;
162
163 /* Unset ABM cap as it moved to DAL.
164 * Add PHM_PlatformCaps_NonABMSupportInPPLib
165 * for re-direct ABM related request to DAL
166 */
167 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
168 PHM_PlatformCaps_ABM);
169 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
170 PHM_PlatformCaps_NonABMSupportInPPLib);
171
172 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
173 PHM_PlatformCaps_DynamicACTiming);
174
175 fiji_initialize_power_tune_defaults(hwmgr);
176
177 data->mclk_stutter_mode_threshold = 60000;
178 data->pcie_gen_performance.max = PP_PCIEGen1;
179 data->pcie_gen_performance.min = PP_PCIEGen3;
180 data->pcie_gen_power_saving.max = PP_PCIEGen1;
181 data->pcie_gen_power_saving.min = PP_PCIEGen3;
182 data->pcie_lane_performance.max = 0;
183 data->pcie_lane_performance.min = 16;
184 data->pcie_lane_power_saving.max = 0;
185 data->pcie_lane_power_saving.min = 16;
186
187 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
188 PHM_PlatformCaps_DynamicUVDState);
189}
190
191static int fiji_get_sclk_for_voltage_evv(struct pp_hwmgr *hwmgr,
192 phm_ppt_v1_voltage_lookup_table *lookup_table,
193 uint16_t virtual_voltage_id, int32_t *sclk)
194{
195 uint8_t entryId;
196 uint8_t voltageId;
197 struct phm_ppt_v1_information *table_info =
198 (struct phm_ppt_v1_information *)(hwmgr->pptable);
199
200 PP_ASSERT_WITH_CODE(lookup_table->count != 0, "Lookup table is empty", return -EINVAL);
201
202 /* search for leakage voltage ID 0xff01 ~ 0xff08 and sckl */
203 for (entryId = 0; entryId < table_info->vdd_dep_on_sclk->count; entryId++) {
204 voltageId = table_info->vdd_dep_on_sclk->entries[entryId].vddInd;
205 if (lookup_table->entries[voltageId].us_vdd == virtual_voltage_id)
206 break;
207 }
208
209 PP_ASSERT_WITH_CODE(entryId < table_info->vdd_dep_on_sclk->count,
210 "Can't find requested voltage id in vdd_dep_on_sclk table!",
211 return -EINVAL;
212 );
213
214 *sclk = table_info->vdd_dep_on_sclk->entries[entryId].clk;
215
216 return 0;
217}
218
219/**
220* Get Leakage VDDC based on leakage ID.
221*
222* @param hwmgr the address of the powerplay hardware manager.
223* @return always 0
224*/
225static int fiji_get_evv_voltages(struct pp_hwmgr *hwmgr)
226{
227 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
228 uint16_t vv_id;
229 uint16_t vddc = 0;
230 uint16_t evv_default = 1150;
231 uint16_t i, j;
232 uint32_t sclk = 0;
233 struct phm_ppt_v1_information *table_info =
234 (struct phm_ppt_v1_information *)hwmgr->pptable;
235 struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
236 table_info->vdd_dep_on_sclk;
237 int result;
238
239 for (i = 0; i < FIJI_MAX_LEAKAGE_COUNT; i++) {
240 vv_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
241 if (!fiji_get_sclk_for_voltage_evv(hwmgr,
242 table_info->vddc_lookup_table, vv_id, &sclk)) {
243 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
244 PHM_PlatformCaps_ClockStretcher)) {
245 for (j = 1; j < sclk_table->count; j++) {
246 if (sclk_table->entries[j].clk == sclk &&
247 sclk_table->entries[j].cks_enable == 0) {
248 sclk += 5000;
249 break;
250 }
251 }
252 }
253
254 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
255 PHM_PlatformCaps_EnableDriverEVV))
256 result = atomctrl_calculate_voltage_evv_on_sclk(hwmgr,
257 VOLTAGE_TYPE_VDDC, sclk, vv_id, &vddc, i, true);
258 else
259 result = -EINVAL;
260
261 if (result)
262 result = atomctrl_get_voltage_evv_on_sclk(hwmgr,
263 VOLTAGE_TYPE_VDDC, sclk,vv_id, &vddc);
264
265 /* need to make sure vddc is less than 2v or else, it could burn the ASIC. */
266 PP_ASSERT_WITH_CODE((vddc < 2000),
267 "Invalid VDDC value, greater than 2v!", result = -EINVAL;);
268
269 if (result)
270 /* 1.15V is the default safe value for Fiji */
271 vddc = evv_default;
272
273 /* the voltage should not be zero nor equal to leakage ID */
274 if (vddc != 0 && vddc != vv_id) {
275 data->vddc_leakage.actual_voltage
276 [data->vddc_leakage.count] = vddc;
277 data->vddc_leakage.leakage_id
278 [data->vddc_leakage.count] = vv_id;
279 data->vddc_leakage.count++;
280 }
281 }
282 }
283 return 0;
284}
285
286/**
287 * Change virtual leakage voltage to actual value.
288 *
289 * @param hwmgr the address of the powerplay hardware manager.
290 * @param pointer to changing voltage
291 * @param pointer to leakage table
292 */
293static void fiji_patch_with_vdd_leakage(struct pp_hwmgr *hwmgr,
294 uint16_t *voltage, struct fiji_leakage_voltage *leakage_table)
295{
296 uint32_t index;
297
298 /* search for leakage voltage ID 0xff01 ~ 0xff08 */
299 for (index = 0; index < leakage_table->count; index++) {
300 /* if this voltage matches a leakage voltage ID */
301 /* patch with actual leakage voltage */
302 if (leakage_table->leakage_id[index] == *voltage) {
303 *voltage = leakage_table->actual_voltage[index];
304 break;
305 }
306 }
307
308 if (*voltage > ATOM_VIRTUAL_VOLTAGE_ID0)
309 printk(KERN_ERR "Voltage value looks like a Leakage ID but it's not patched \n");
310}
311
312/**
313* Patch voltage lookup table by EVV leakages.
314*
315* @param hwmgr the address of the powerplay hardware manager.
316* @param pointer to voltage lookup table
317* @param pointer to leakage table
318* @return always 0
319*/
320static int fiji_patch_lookup_table_with_leakage(struct pp_hwmgr *hwmgr,
321 phm_ppt_v1_voltage_lookup_table *lookup_table,
322 struct fiji_leakage_voltage *leakage_table)
323{
324 uint32_t i;
325
326 for (i = 0; i < lookup_table->count; i++)
327 fiji_patch_with_vdd_leakage(hwmgr,
328 &lookup_table->entries[i].us_vdd, leakage_table);
329
330 return 0;
331}
332
333static int fiji_patch_clock_voltage_limits_with_vddc_leakage(
334 struct pp_hwmgr *hwmgr, struct fiji_leakage_voltage *leakage_table,
335 uint16_t *vddc)
336{
337 struct phm_ppt_v1_information *table_info =
338 (struct phm_ppt_v1_information *)(hwmgr->pptable);
339 fiji_patch_with_vdd_leakage(hwmgr, (uint16_t *)vddc, leakage_table);
340 hwmgr->dyn_state.max_clock_voltage_on_dc.vddc =
341 table_info->max_clock_voltage_on_dc.vddc;
342 return 0;
343}
344
345static int fiji_patch_voltage_dependency_tables_with_lookup_table(
346 struct pp_hwmgr *hwmgr)
347{
348 uint8_t entryId;
349 uint8_t voltageId;
350 struct phm_ppt_v1_information *table_info =
351 (struct phm_ppt_v1_information *)(hwmgr->pptable);
352
353 struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
354 table_info->vdd_dep_on_sclk;
355 struct phm_ppt_v1_clock_voltage_dependency_table *mclk_table =
356 table_info->vdd_dep_on_mclk;
357 struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
358 table_info->mm_dep_table;
359
360 for (entryId = 0; entryId < sclk_table->count; ++entryId) {
361 voltageId = sclk_table->entries[entryId].vddInd;
362 sclk_table->entries[entryId].vddc =
363 table_info->vddc_lookup_table->entries[voltageId].us_vdd;
364 }
365
366 for (entryId = 0; entryId < mclk_table->count; ++entryId) {
367 voltageId = mclk_table->entries[entryId].vddInd;
368 mclk_table->entries[entryId].vddc =
369 table_info->vddc_lookup_table->entries[voltageId].us_vdd;
370 }
371
372 for (entryId = 0; entryId < mm_table->count; ++entryId) {
373 voltageId = mm_table->entries[entryId].vddcInd;
374 mm_table->entries[entryId].vddc =
375 table_info->vddc_lookup_table->entries[voltageId].us_vdd;
376 }
377
378 return 0;
379
380}
381
382static int fiji_calc_voltage_dependency_tables(struct pp_hwmgr *hwmgr)
383{
384 /* Need to determine if we need calculated voltage. */
385 return 0;
386}
387
388static int fiji_calc_mm_voltage_dependency_table(struct pp_hwmgr *hwmgr)
389{
390 /* Need to determine if we need calculated voltage from mm table. */
391 return 0;
392}
393
394static int fiji_sort_lookup_table(struct pp_hwmgr *hwmgr,
395 struct phm_ppt_v1_voltage_lookup_table *lookup_table)
396{
397 uint32_t table_size, i, j;
398 struct phm_ppt_v1_voltage_lookup_record tmp_voltage_lookup_record;
399 table_size = lookup_table->count;
400
401 PP_ASSERT_WITH_CODE(0 != lookup_table->count,
402 "Lookup table is empty", return -EINVAL);
403
404 /* Sorting voltages */
405 for (i = 0; i < table_size - 1; i++) {
406 for (j = i + 1; j > 0; j--) {
407 if (lookup_table->entries[j].us_vdd <
408 lookup_table->entries[j - 1].us_vdd) {
409 tmp_voltage_lookup_record = lookup_table->entries[j - 1];
410 lookup_table->entries[j - 1] = lookup_table->entries[j];
411 lookup_table->entries[j] = tmp_voltage_lookup_record;
412 }
413 }
414 }
415
416 return 0;
417}
418
419static int fiji_complete_dependency_tables(struct pp_hwmgr *hwmgr)
420{
421 int result = 0;
422 int tmp_result;
423 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
424 struct phm_ppt_v1_information *table_info =
425 (struct phm_ppt_v1_information *)(hwmgr->pptable);
426
427 tmp_result = fiji_patch_lookup_table_with_leakage(hwmgr,
428 table_info->vddc_lookup_table, &(data->vddc_leakage));
429 if (tmp_result)
430 result = tmp_result;
431
432 tmp_result = fiji_patch_clock_voltage_limits_with_vddc_leakage(hwmgr,
433 &(data->vddc_leakage), &table_info->max_clock_voltage_on_dc.vddc);
434 if (tmp_result)
435 result = tmp_result;
436
437 tmp_result = fiji_patch_voltage_dependency_tables_with_lookup_table(hwmgr);
438 if (tmp_result)
439 result = tmp_result;
440
441 tmp_result = fiji_calc_voltage_dependency_tables(hwmgr);
442 if (tmp_result)
443 result = tmp_result;
444
445 tmp_result = fiji_calc_mm_voltage_dependency_table(hwmgr);
446 if (tmp_result)
447 result = tmp_result;
448
449 tmp_result = fiji_sort_lookup_table(hwmgr, table_info->vddc_lookup_table);
450 if(tmp_result)
451 result = tmp_result;
452
453 return result;
454}
455
456static int fiji_set_private_data_based_on_pptable(struct pp_hwmgr *hwmgr)
457{
458 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
459 struct phm_ppt_v1_information *table_info =
460 (struct phm_ppt_v1_information *)(hwmgr->pptable);
461
462 struct phm_ppt_v1_clock_voltage_dependency_table *allowed_sclk_vdd_table =
463 table_info->vdd_dep_on_sclk;
464 struct phm_ppt_v1_clock_voltage_dependency_table *allowed_mclk_vdd_table =
465 table_info->vdd_dep_on_mclk;
466
467 PP_ASSERT_WITH_CODE(allowed_sclk_vdd_table != NULL,
Christian Königedf600d2016-05-03 15:54:54 +0200468 "VDD dependency on SCLK table is missing. \
Eric Huangaabcb7c2015-08-26 16:52:28 -0400469 This table is mandatory", return -EINVAL);
470 PP_ASSERT_WITH_CODE(allowed_sclk_vdd_table->count >= 1,
Christian Königedf600d2016-05-03 15:54:54 +0200471 "VDD dependency on SCLK table has to have is missing. \
Eric Huangaabcb7c2015-08-26 16:52:28 -0400472 This table is mandatory", return -EINVAL);
473
474 PP_ASSERT_WITH_CODE(allowed_mclk_vdd_table != NULL,
Christian Königedf600d2016-05-03 15:54:54 +0200475 "VDD dependency on MCLK table is missing. \
Eric Huangaabcb7c2015-08-26 16:52:28 -0400476 This table is mandatory", return -EINVAL);
477 PP_ASSERT_WITH_CODE(allowed_mclk_vdd_table->count >= 1,
478 "VDD dependency on MCLK table has to have is missing. \
479 This table is mandatory", return -EINVAL);
480
481 data->min_vddc_in_pptable = (uint16_t)allowed_sclk_vdd_table->entries[0].vddc;
482 data->max_vddc_in_pptable = (uint16_t)allowed_sclk_vdd_table->
483 entries[allowed_sclk_vdd_table->count - 1].vddc;
484
485 table_info->max_clock_voltage_on_ac.sclk =
486 allowed_sclk_vdd_table->entries[allowed_sclk_vdd_table->count - 1].clk;
487 table_info->max_clock_voltage_on_ac.mclk =
488 allowed_mclk_vdd_table->entries[allowed_mclk_vdd_table->count - 1].clk;
489 table_info->max_clock_voltage_on_ac.vddc =
490 allowed_sclk_vdd_table->entries[allowed_sclk_vdd_table->count - 1].vddc;
491 table_info->max_clock_voltage_on_ac.vddci =
492 allowed_mclk_vdd_table->entries[allowed_mclk_vdd_table->count - 1].vddci;
493
494 hwmgr->dyn_state.max_clock_voltage_on_ac.sclk =
495 table_info->max_clock_voltage_on_ac.sclk;
496 hwmgr->dyn_state.max_clock_voltage_on_ac.mclk =
497 table_info->max_clock_voltage_on_ac.mclk;
498 hwmgr->dyn_state.max_clock_voltage_on_ac.vddc =
499 table_info->max_clock_voltage_on_ac.vddc;
500 hwmgr->dyn_state.max_clock_voltage_on_ac.vddci =
501 table_info->max_clock_voltage_on_ac.vddci;
502
503 return 0;
504}
505
506static uint16_t fiji_get_current_pcie_speed(struct pp_hwmgr *hwmgr)
507{
508 uint32_t speedCntl = 0;
509
510 /* mmPCIE_PORT_INDEX rename as mmPCIE_INDEX */
511 speedCntl = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__PCIE,
512 ixPCIE_LC_SPEED_CNTL);
513 return((uint16_t)PHM_GET_FIELD(speedCntl,
514 PCIE_LC_SPEED_CNTL, LC_CURRENT_DATA_RATE));
515}
516
517static int fiji_get_current_pcie_lane_number(struct pp_hwmgr *hwmgr)
518{
519 uint32_t link_width;
520
521 /* mmPCIE_PORT_INDEX rename as mmPCIE_INDEX */
522 link_width = PHM_READ_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__PCIE,
523 PCIE_LC_LINK_WIDTH_CNTL, LC_LINK_WIDTH_RD);
524
525 PP_ASSERT_WITH_CODE((7 >= link_width),
526 "Invalid PCIe lane width!", return 0);
527
528 return decode_pcie_lane_width(link_width);
529}
530
531/** Patch the Boot State to match VBIOS boot clocks and voltage.
532*
533* @param hwmgr Pointer to the hardware manager.
534* @param pPowerState The address of the PowerState instance being created.
535*
536*/
537static int fiji_patch_boot_state(struct pp_hwmgr *hwmgr,
538 struct pp_hw_power_state *hw_ps)
539{
540 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
541 struct fiji_power_state *ps = (struct fiji_power_state *)hw_ps;
542 ATOM_FIRMWARE_INFO_V2_2 *fw_info;
543 uint16_t size;
544 uint8_t frev, crev;
545 int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
546
547 /* First retrieve the Boot clocks and VDDC from the firmware info table.
548 * We assume here that fw_info is unchanged if this call fails.
549 */
550 fw_info = (ATOM_FIRMWARE_INFO_V2_2 *)cgs_atom_get_data_table(
551 hwmgr->device, index,
552 &size, &frev, &crev);
553 if (!fw_info)
554 /* During a test, there is no firmware info table. */
555 return 0;
556
557 /* Patch the state. */
558 data->vbios_boot_state.sclk_bootup_value =
559 le32_to_cpu(fw_info->ulDefaultEngineClock);
560 data->vbios_boot_state.mclk_bootup_value =
561 le32_to_cpu(fw_info->ulDefaultMemoryClock);
562 data->vbios_boot_state.mvdd_bootup_value =
563 le16_to_cpu(fw_info->usBootUpMVDDCVoltage);
564 data->vbios_boot_state.vddc_bootup_value =
565 le16_to_cpu(fw_info->usBootUpVDDCVoltage);
566 data->vbios_boot_state.vddci_bootup_value =
567 le16_to_cpu(fw_info->usBootUpVDDCIVoltage);
568 data->vbios_boot_state.pcie_gen_bootup_value =
569 fiji_get_current_pcie_speed(hwmgr);
570 data->vbios_boot_state.pcie_lane_bootup_value =
571 (uint16_t)fiji_get_current_pcie_lane_number(hwmgr);
572
573 /* set boot power state */
574 ps->performance_levels[0].memory_clock = data->vbios_boot_state.mclk_bootup_value;
575 ps->performance_levels[0].engine_clock = data->vbios_boot_state.sclk_bootup_value;
576 ps->performance_levels[0].pcie_gen = data->vbios_boot_state.pcie_gen_bootup_value;
577 ps->performance_levels[0].pcie_lane = data->vbios_boot_state.pcie_lane_bootup_value;
578
579 return 0;
580}
581
Eric Huang65ba4f22016-04-15 16:33:20 -0400582static int fiji_hwmgr_backend_fini(struct pp_hwmgr *hwmgr)
583{
584 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
585
586 if (data->soft_pp_table) {
587 kfree(data->soft_pp_table);
588 data->soft_pp_table = NULL;
589 }
590
591 return phm_hwmgr_backend_fini(hwmgr);
592}
593
Eric Huangaabcb7c2015-08-26 16:52:28 -0400594static int fiji_hwmgr_backend_init(struct pp_hwmgr *hwmgr)
595{
596 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
597 uint32_t i;
598 struct phm_ppt_v1_information *table_info =
599 (struct phm_ppt_v1_information *)(hwmgr->pptable);
600 bool stay_in_boot;
601 int result;
602
603 data->dll_default_on = false;
604 data->sram_end = SMC_RAM_END;
605
606 for (i = 0; i < SMU73_MAX_LEVELS_GRAPHICS; i++)
607 data->activity_target[i] = FIJI_AT_DFLT;
608
609 data->vddc_vddci_delta = VDDC_VDDCI_DELTA;
610
611 data->mclk_activity_target = PPFIJI_MCLK_TARGETACTIVITY_DFLT;
612 data->mclk_dpm0_activity_target = 0xa;
613
614 data->sclk_dpm_key_disabled = 0;
615 data->mclk_dpm_key_disabled = 0;
616 data->pcie_dpm_key_disabled = 0;
617
618 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
619 PHM_PlatformCaps_UnTabledHardwareInterface);
620 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
621 PHM_PlatformCaps_TablelessHardwareInterface);
622
623 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
624 PHM_PlatformCaps_SclkDeepSleep);
625
626 data->gpio_debug = 0;
627
628 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
629 PHM_PlatformCaps_DynamicPatchPowerState);
630
631 /* need to set voltage control types before EVV patching */
632 data->voltage_control = FIJI_VOLTAGE_CONTROL_NONE;
633 data->vddci_control = FIJI_VOLTAGE_CONTROL_NONE;
634 data->mvdd_control = FIJI_VOLTAGE_CONTROL_NONE;
635
Rex Zhua2fb4932016-06-13 17:46:31 +0800636 data->force_pcie_gen = PP_PCIEGenInvalid;
637
Eric Huangaabcb7c2015-08-26 16:52:28 -0400638 if (atomctrl_is_voltage_controled_by_gpio_v3(hwmgr,
639 VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_SVID2))
640 data->voltage_control = FIJI_VOLTAGE_CONTROL_BY_SVID2;
641
642 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
643 PHM_PlatformCaps_EnableMVDDControl))
644 if (atomctrl_is_voltage_controled_by_gpio_v3(hwmgr,
645 VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_GPIO_LUT))
646 data->mvdd_control = FIJI_VOLTAGE_CONTROL_BY_GPIO;
647
648 if (data->mvdd_control == FIJI_VOLTAGE_CONTROL_NONE)
649 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
650 PHM_PlatformCaps_EnableMVDDControl);
651
652 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
653 PHM_PlatformCaps_ControlVDDCI)) {
654 if (atomctrl_is_voltage_controled_by_gpio_v3(hwmgr,
655 VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_GPIO_LUT))
656 data->vddci_control = FIJI_VOLTAGE_CONTROL_BY_GPIO;
657 else if (atomctrl_is_voltage_controled_by_gpio_v3(hwmgr,
658 VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_SVID2))
659 data->vddci_control = FIJI_VOLTAGE_CONTROL_BY_SVID2;
660 }
661
662 if (data->vddci_control == FIJI_VOLTAGE_CONTROL_NONE)
663 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
664 PHM_PlatformCaps_ControlVDDCI);
665
666 if (table_info && table_info->cac_dtp_table->usClockStretchAmount)
667 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
668 PHM_PlatformCaps_ClockStretcher);
669
670 fiji_init_dpm_defaults(hwmgr);
671
672 /* Get leakage voltage based on leakage ID. */
673 fiji_get_evv_voltages(hwmgr);
674
675 /* Patch our voltage dependency table with actual leakage voltage
676 * We need to perform leakage translation before it's used by other functions
677 */
678 fiji_complete_dependency_tables(hwmgr);
679
680 /* Parse pptable data read from VBIOS */
681 fiji_set_private_data_based_on_pptable(hwmgr);
682
683 /* ULV Support */
684 data->ulv.ulv_supported = true; /* ULV feature is enabled by default */
685
686 /* Initalize Dynamic State Adjustment Rule Settings */
687 result = tonga_initializa_dynamic_state_adjustment_rule_settings(hwmgr);
688
689 if (!result) {
690 data->uvd_enabled = false;
691 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
692 PHM_PlatformCaps_EnableSMU7ThermalManagement);
693 data->vddc_phase_shed_control = false;
694 }
695
696 stay_in_boot = phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
697 PHM_PlatformCaps_StayInBootState);
698
699 if (0 == result) {
Alex Deucher464cea32015-11-11 21:02:16 -0500700 struct cgs_system_info sys_info = {0};
701
Eric Huangaabcb7c2015-08-26 16:52:28 -0400702 data->is_tlu_enabled = 0;
703 hwmgr->platform_descriptor.hardwareActivityPerformanceLevels =
704 FIJI_MAX_HARDWARE_POWERLEVELS;
705 hwmgr->platform_descriptor.hardwarePerformanceLevels = 2;
706 hwmgr->platform_descriptor.minimumClocksReductionPercentage = 50;
707
Eric Huang60103812015-11-27 14:09:53 -0500708 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
709 PHM_PlatformCaps_FanSpeedInTableIsRPM);
710
711 if (table_info->cac_dtp_table->usDefaultTargetOperatingTemp &&
712 hwmgr->thermal_controller.
713 advanceFanControlParameters.ucFanControlMode) {
714 hwmgr->thermal_controller.advanceFanControlParameters.usMaxFanPWM =
715 hwmgr->thermal_controller.advanceFanControlParameters.usDefaultMaxFanPWM;
716 hwmgr->thermal_controller.advanceFanControlParameters.usMaxFanRPM =
717 hwmgr->thermal_controller.advanceFanControlParameters.usDefaultMaxFanRPM;
718 hwmgr->dyn_state.cac_dtp_table->usOperatingTempMinLimit =
719 table_info->cac_dtp_table->usOperatingTempMinLimit;
720 hwmgr->dyn_state.cac_dtp_table->usOperatingTempMaxLimit =
721 table_info->cac_dtp_table->usOperatingTempMaxLimit;
722 hwmgr->dyn_state.cac_dtp_table->usDefaultTargetOperatingTemp =
723 table_info->cac_dtp_table->usDefaultTargetOperatingTemp;
724 hwmgr->dyn_state.cac_dtp_table->usOperatingTempStep =
725 table_info->cac_dtp_table->usOperatingTempStep;
726 hwmgr->dyn_state.cac_dtp_table->usTargetOperatingTemp =
727 table_info->cac_dtp_table->usTargetOperatingTemp;
728
729 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
730 PHM_PlatformCaps_ODFuzzyFanControlSupport);
731 }
732
Alex Deucher464cea32015-11-11 21:02:16 -0500733 sys_info.size = sizeof(struct cgs_system_info);
734 sys_info.info_id = CGS_SYSTEM_INFO_PCIE_GEN_INFO;
735 result = cgs_query_system_info(hwmgr->device, &sys_info);
736 if (result)
737 data->pcie_gen_cap = 0x30007;
738 else
739 data->pcie_gen_cap = (uint32_t)sys_info.value;
740 if (data->pcie_gen_cap & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
741 data->pcie_spc_cap = 20;
742 sys_info.size = sizeof(struct cgs_system_info);
743 sys_info.info_id = CGS_SYSTEM_INFO_PCIE_MLW;
744 result = cgs_query_system_info(hwmgr->device, &sys_info);
745 if (result)
746 data->pcie_lane_cap = 0x2f0000;
747 else
748 data->pcie_lane_cap = (uint32_t)sys_info.value;
Eric Huangaabcb7c2015-08-26 16:52:28 -0400749 } else {
750 /* Ignore return value in here, we are cleaning up a mess. */
Eric Huang65ba4f22016-04-15 16:33:20 -0400751 fiji_hwmgr_backend_fini(hwmgr);
Eric Huangaabcb7c2015-08-26 16:52:28 -0400752 }
753
754 return 0;
755}
756
757/**
758 * Read clock related registers.
759 *
760 * @param hwmgr the address of the powerplay hardware manager.
761 * @return always 0
762 */
763static int fiji_read_clock_registers(struct pp_hwmgr *hwmgr)
764{
765 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
766
767 data->clock_registers.vCG_SPLL_FUNC_CNTL =
768 cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
769 ixCG_SPLL_FUNC_CNTL);
770 data->clock_registers.vCG_SPLL_FUNC_CNTL_2 =
771 cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
772 ixCG_SPLL_FUNC_CNTL_2);
773 data->clock_registers.vCG_SPLL_FUNC_CNTL_3 =
774 cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
775 ixCG_SPLL_FUNC_CNTL_3);
776 data->clock_registers.vCG_SPLL_FUNC_CNTL_4 =
777 cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
778 ixCG_SPLL_FUNC_CNTL_4);
779 data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM =
780 cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
781 ixCG_SPLL_SPREAD_SPECTRUM);
782 data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2 =
783 cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
784 ixCG_SPLL_SPREAD_SPECTRUM_2);
785
786 return 0;
787}
788
789/**
790 * Find out if memory is GDDR5.
791 *
792 * @param hwmgr the address of the powerplay hardware manager.
793 * @return always 0
794 */
795static int fiji_get_memory_type(struct pp_hwmgr *hwmgr)
796{
797 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
798 uint32_t temp;
799
800 temp = cgs_read_register(hwmgr->device, mmMC_SEQ_MISC0);
801
802 data->is_memory_gddr5 = (MC_SEQ_MISC0_GDDR5_VALUE ==
803 ((temp & MC_SEQ_MISC0_GDDR5_MASK) >>
804 MC_SEQ_MISC0_GDDR5_SHIFT));
805
806 return 0;
807}
808
809/**
810 * Enables Dynamic Power Management by SMC
811 *
812 * @param hwmgr the address of the powerplay hardware manager.
813 * @return always 0
814 */
815static int fiji_enable_acpi_power_management(struct pp_hwmgr *hwmgr)
816{
817 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
818 GENERAL_PWRMGT, STATIC_PM_EN, 1);
819
820 return 0;
821}
822
823/**
824 * Initialize PowerGating States for different engines
825 *
826 * @param hwmgr the address of the powerplay hardware manager.
827 * @return always 0
828 */
829static int fiji_init_power_gate_state(struct pp_hwmgr *hwmgr)
830{
831 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
832
833 data->uvd_power_gated = false;
834 data->vce_power_gated = false;
835 data->samu_power_gated = false;
836 data->acp_power_gated = false;
837 data->pg_acp_init = true;
838
839 return 0;
840}
841
842static int fiji_init_sclk_threshold(struct pp_hwmgr *hwmgr)
843{
844 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
845 data->low_sclk_interrupt_threshold = 0;
846
847 return 0;
848}
849
850static int fiji_setup_asic_task(struct pp_hwmgr *hwmgr)
851{
852 int tmp_result, result = 0;
853
854 tmp_result = fiji_read_clock_registers(hwmgr);
855 PP_ASSERT_WITH_CODE((0 == tmp_result),
856 "Failed to read clock registers!", result = tmp_result);
857
858 tmp_result = fiji_get_memory_type(hwmgr);
859 PP_ASSERT_WITH_CODE((0 == tmp_result),
860 "Failed to get memory type!", result = tmp_result);
861
862 tmp_result = fiji_enable_acpi_power_management(hwmgr);
863 PP_ASSERT_WITH_CODE((0 == tmp_result),
864 "Failed to enable ACPI power management!", result = tmp_result);
865
866 tmp_result = fiji_init_power_gate_state(hwmgr);
867 PP_ASSERT_WITH_CODE((0 == tmp_result),
868 "Failed to init power gate state!", result = tmp_result);
869
870 tmp_result = tonga_get_mc_microcode_version(hwmgr);
871 PP_ASSERT_WITH_CODE((0 == tmp_result),
872 "Failed to get MC microcode version!", result = tmp_result);
873
874 tmp_result = fiji_init_sclk_threshold(hwmgr);
875 PP_ASSERT_WITH_CODE((0 == tmp_result),
876 "Failed to init sclk threshold!", result = tmp_result);
877
878 return result;
879}
880
881/**
882* Checks if we want to support voltage control
883*
884* @param hwmgr the address of the powerplay hardware manager.
885*/
886static bool fiji_voltage_control(const struct pp_hwmgr *hwmgr)
887{
888 const struct fiji_hwmgr *data =
889 (const struct fiji_hwmgr *)(hwmgr->backend);
890
891 return (FIJI_VOLTAGE_CONTROL_NONE != data->voltage_control);
892}
893
894/**
895* Enable voltage control
896*
897* @param hwmgr the address of the powerplay hardware manager.
898* @return always 0
899*/
900static int fiji_enable_voltage_control(struct pp_hwmgr *hwmgr)
901{
902 /* enable voltage control */
903 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
904 GENERAL_PWRMGT, VOLT_PWRMGT_EN, 1);
905
906 return 0;
907}
908
909/**
910* Remove repeated voltage values and create table with unique values.
911*
912* @param hwmgr the address of the powerplay hardware manager.
913* @param vol_table the pointer to changing voltage table
914* @return 0 in success
915*/
916
917static int fiji_trim_voltage_table(struct pp_hwmgr *hwmgr,
918 struct pp_atomctrl_voltage_table *vol_table)
919{
920 uint32_t i, j;
921 uint16_t vvalue;
922 bool found = false;
923 struct pp_atomctrl_voltage_table *table;
924
925 PP_ASSERT_WITH_CODE((NULL != vol_table),
926 "Voltage Table empty.", return -EINVAL);
927 table = kzalloc(sizeof(struct pp_atomctrl_voltage_table),
928 GFP_KERNEL);
929
930 if (NULL == table)
Rex Zhuc15c8d72016-01-06 16:48:38 +0800931 return -ENOMEM;
Eric Huangaabcb7c2015-08-26 16:52:28 -0400932
933 table->mask_low = vol_table->mask_low;
934 table->phase_delay = vol_table->phase_delay;
935
936 for (i = 0; i < vol_table->count; i++) {
937 vvalue = vol_table->entries[i].value;
938 found = false;
939
940 for (j = 0; j < table->count; j++) {
941 if (vvalue == table->entries[j].value) {
942 found = true;
943 break;
944 }
945 }
946
947 if (!found) {
948 table->entries[table->count].value = vvalue;
949 table->entries[table->count].smio_low =
950 vol_table->entries[i].smio_low;
951 table->count++;
952 }
953 }
954
955 memcpy(vol_table, table, sizeof(struct pp_atomctrl_voltage_table));
956 kfree(table);
957
Rex Zhu75ac63d2016-01-06 16:38:48 +0800958 return 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -0400959}
Rex Zhu75ac63d2016-01-06 16:38:48 +0800960
Eric Huangaabcb7c2015-08-26 16:52:28 -0400961static int fiji_get_svi2_mvdd_voltage_table(struct pp_hwmgr *hwmgr,
962 phm_ppt_v1_clock_voltage_dependency_table *dep_table)
963{
964 uint32_t i;
965 int result;
966 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
967 struct pp_atomctrl_voltage_table *vol_table = &(data->mvdd_voltage_table);
968
969 PP_ASSERT_WITH_CODE((0 != dep_table->count),
970 "Voltage Dependency Table empty.", return -EINVAL);
971
972 vol_table->mask_low = 0;
973 vol_table->phase_delay = 0;
974 vol_table->count = dep_table->count;
975
976 for (i = 0; i < dep_table->count; i++) {
977 vol_table->entries[i].value = dep_table->entries[i].mvdd;
978 vol_table->entries[i].smio_low = 0;
979 }
980
981 result = fiji_trim_voltage_table(hwmgr, vol_table);
982 PP_ASSERT_WITH_CODE((0 == result),
983 "Failed to trim MVDD table.", return result);
984
985 return 0;
986}
987
988static int fiji_get_svi2_vddci_voltage_table(struct pp_hwmgr *hwmgr,
989 phm_ppt_v1_clock_voltage_dependency_table *dep_table)
990{
991 uint32_t i;
992 int result;
993 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
994 struct pp_atomctrl_voltage_table *vol_table = &(data->vddci_voltage_table);
995
996 PP_ASSERT_WITH_CODE((0 != dep_table->count),
997 "Voltage Dependency Table empty.", return -EINVAL);
998
999 vol_table->mask_low = 0;
1000 vol_table->phase_delay = 0;
1001 vol_table->count = dep_table->count;
1002
1003 for (i = 0; i < dep_table->count; i++) {
1004 vol_table->entries[i].value = dep_table->entries[i].vddci;
1005 vol_table->entries[i].smio_low = 0;
1006 }
1007
1008 result = fiji_trim_voltage_table(hwmgr, vol_table);
1009 PP_ASSERT_WITH_CODE((0 == result),
1010 "Failed to trim VDDCI table.", return result);
1011
1012 return 0;
1013}
1014
1015static int fiji_get_svi2_vdd_voltage_table(struct pp_hwmgr *hwmgr,
1016 phm_ppt_v1_voltage_lookup_table *lookup_table)
1017{
1018 int i = 0;
1019 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1020 struct pp_atomctrl_voltage_table *vol_table = &(data->vddc_voltage_table);
1021
1022 PP_ASSERT_WITH_CODE((0 != lookup_table->count),
1023 "Voltage Lookup Table empty.", return -EINVAL);
1024
1025 vol_table->mask_low = 0;
1026 vol_table->phase_delay = 0;
1027
1028 vol_table->count = lookup_table->count;
1029
1030 for (i = 0; i < vol_table->count; i++) {
1031 vol_table->entries[i].value = lookup_table->entries[i].us_vdd;
1032 vol_table->entries[i].smio_low = 0;
1033 }
1034
1035 return 0;
1036}
1037
1038/* ---- Voltage Tables ----
1039 * If the voltage table would be bigger than
1040 * what will fit into the state table on
1041 * the SMC keep only the higher entries.
1042 */
1043static void fiji_trim_voltage_table_to_fit_state_table(struct pp_hwmgr *hwmgr,
1044 uint32_t max_vol_steps, struct pp_atomctrl_voltage_table *vol_table)
1045{
1046 unsigned int i, diff;
1047
1048 if (vol_table->count <= max_vol_steps)
1049 return;
1050
1051 diff = vol_table->count - max_vol_steps;
1052
1053 for (i = 0; i < max_vol_steps; i++)
1054 vol_table->entries[i] = vol_table->entries[i + diff];
1055
1056 vol_table->count = max_vol_steps;
1057
1058 return;
1059}
1060
1061/**
1062* Create Voltage Tables.
1063*
1064* @param hwmgr the address of the powerplay hardware manager.
1065* @return always 0
1066*/
1067static int fiji_construct_voltage_tables(struct pp_hwmgr *hwmgr)
1068{
1069 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1070 struct phm_ppt_v1_information *table_info =
1071 (struct phm_ppt_v1_information *)hwmgr->pptable;
1072 int result;
1073
1074 if (FIJI_VOLTAGE_CONTROL_BY_GPIO == data->mvdd_control) {
1075 result = atomctrl_get_voltage_table_v3(hwmgr,
1076 VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_GPIO_LUT,
1077 &(data->mvdd_voltage_table));
1078 PP_ASSERT_WITH_CODE((0 == result),
1079 "Failed to retrieve MVDD table.",
1080 return result);
1081 } else if (FIJI_VOLTAGE_CONTROL_BY_SVID2 == data->mvdd_control) {
1082 result = fiji_get_svi2_mvdd_voltage_table(hwmgr,
1083 table_info->vdd_dep_on_mclk);
1084 PP_ASSERT_WITH_CODE((0 == result),
1085 "Failed to retrieve SVI2 MVDD table from dependancy table.",
1086 return result;);
1087 }
1088
1089 if (FIJI_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control) {
1090 result = atomctrl_get_voltage_table_v3(hwmgr,
1091 VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_GPIO_LUT,
1092 &(data->vddci_voltage_table));
1093 PP_ASSERT_WITH_CODE((0 == result),
1094 "Failed to retrieve VDDCI table.",
1095 return result);
1096 } else if (FIJI_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control) {
1097 result = fiji_get_svi2_vddci_voltage_table(hwmgr,
1098 table_info->vdd_dep_on_mclk);
1099 PP_ASSERT_WITH_CODE((0 == result),
1100 "Failed to retrieve SVI2 VDDCI table from dependancy table.",
1101 return result);
1102 }
1103
1104 if(FIJI_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control) {
1105 result = fiji_get_svi2_vdd_voltage_table(hwmgr,
1106 table_info->vddc_lookup_table);
1107 PP_ASSERT_WITH_CODE((0 == result),
1108 "Failed to retrieve SVI2 VDDC table from lookup table.",
1109 return result);
1110 }
1111
1112 PP_ASSERT_WITH_CODE(
1113 (data->vddc_voltage_table.count <= (SMU73_MAX_LEVELS_VDDC)),
1114 "Too many voltage values for VDDC. Trimming to fit state table.",
1115 fiji_trim_voltage_table_to_fit_state_table(hwmgr,
1116 SMU73_MAX_LEVELS_VDDC, &(data->vddc_voltage_table)));
1117
1118 PP_ASSERT_WITH_CODE(
1119 (data->vddci_voltage_table.count <= (SMU73_MAX_LEVELS_VDDCI)),
1120 "Too many voltage values for VDDCI. Trimming to fit state table.",
1121 fiji_trim_voltage_table_to_fit_state_table(hwmgr,
1122 SMU73_MAX_LEVELS_VDDCI, &(data->vddci_voltage_table)));
1123
1124 PP_ASSERT_WITH_CODE(
1125 (data->mvdd_voltage_table.count <= (SMU73_MAX_LEVELS_MVDD)),
1126 "Too many voltage values for MVDD. Trimming to fit state table.",
1127 fiji_trim_voltage_table_to_fit_state_table(hwmgr,
1128 SMU73_MAX_LEVELS_MVDD, &(data->mvdd_voltage_table)));
1129
Rex Zhu75ac63d2016-01-06 16:38:48 +08001130 return 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -04001131}
1132
1133static int fiji_initialize_mc_reg_table(struct pp_hwmgr *hwmgr)
1134{
1135 /* Program additional LP registers
1136 * that are no longer programmed by VBIOS
1137 */
1138 cgs_write_register(hwmgr->device, mmMC_SEQ_RAS_TIMING_LP,
1139 cgs_read_register(hwmgr->device, mmMC_SEQ_RAS_TIMING));
1140 cgs_write_register(hwmgr->device, mmMC_SEQ_CAS_TIMING_LP,
1141 cgs_read_register(hwmgr->device, mmMC_SEQ_CAS_TIMING));
1142 cgs_write_register(hwmgr->device, mmMC_SEQ_MISC_TIMING2_LP,
1143 cgs_read_register(hwmgr->device, mmMC_SEQ_MISC_TIMING2));
1144 cgs_write_register(hwmgr->device, mmMC_SEQ_WR_CTL_D1_LP,
1145 cgs_read_register(hwmgr->device, mmMC_SEQ_WR_CTL_D1));
1146 cgs_write_register(hwmgr->device, mmMC_SEQ_RD_CTL_D0_LP,
1147 cgs_read_register(hwmgr->device, mmMC_SEQ_RD_CTL_D0));
1148 cgs_write_register(hwmgr->device, mmMC_SEQ_RD_CTL_D1_LP,
1149 cgs_read_register(hwmgr->device, mmMC_SEQ_RD_CTL_D1));
1150 cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_TIMING_LP,
1151 cgs_read_register(hwmgr->device, mmMC_SEQ_PMG_TIMING));
1152
1153 return 0;
1154}
1155
1156/**
1157* Programs static screed detection parameters
1158*
1159* @param hwmgr the address of the powerplay hardware manager.
1160* @return always 0
1161*/
1162static int fiji_program_static_screen_threshold_parameters(
1163 struct pp_hwmgr *hwmgr)
1164{
1165 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1166
1167 /* Set static screen threshold unit */
1168 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1169 CG_STATIC_SCREEN_PARAMETER, STATIC_SCREEN_THRESHOLD_UNIT,
1170 data->static_screen_threshold_unit);
1171 /* Set static screen threshold */
1172 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1173 CG_STATIC_SCREEN_PARAMETER, STATIC_SCREEN_THRESHOLD,
1174 data->static_screen_threshold);
1175
Rex Zhu75ac63d2016-01-06 16:38:48 +08001176 return 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -04001177}
1178
1179/**
1180* Setup display gap for glitch free memory clock switching.
1181*
1182* @param hwmgr the address of the powerplay hardware manager.
1183* @return always 0
1184*/
1185static int fiji_enable_display_gap(struct pp_hwmgr *hwmgr)
1186{
1187 uint32_t displayGap =
1188 cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1189 ixCG_DISPLAY_GAP_CNTL);
1190
1191 displayGap = PHM_SET_FIELD(displayGap, CG_DISPLAY_GAP_CNTL,
1192 DISP_GAP, DISPLAY_GAP_IGNORE);
1193
1194 displayGap = PHM_SET_FIELD(displayGap, CG_DISPLAY_GAP_CNTL,
1195 DISP_GAP_MCHG, DISPLAY_GAP_VBLANK);
1196
1197 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1198 ixCG_DISPLAY_GAP_CNTL, displayGap);
1199
1200 return 0;
1201}
1202
1203/**
1204* Programs activity state transition voting clients
1205*
1206* @param hwmgr the address of the powerplay hardware manager.
1207* @return always 0
1208*/
1209static int fiji_program_voting_clients(struct pp_hwmgr *hwmgr)
1210{
1211 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1212
1213 /* Clear reset for voting clients before enabling DPM */
1214 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1215 SCLK_PWRMGT_CNTL, RESET_SCLK_CNT, 0);
1216 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1217 SCLK_PWRMGT_CNTL, RESET_BUSY_CNT, 0);
1218
1219 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1220 ixCG_FREQ_TRAN_VOTING_0, data->voting_rights_clients0);
1221 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1222 ixCG_FREQ_TRAN_VOTING_1, data->voting_rights_clients1);
1223 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1224 ixCG_FREQ_TRAN_VOTING_2, data->voting_rights_clients2);
1225 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1226 ixCG_FREQ_TRAN_VOTING_3, data->voting_rights_clients3);
1227 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1228 ixCG_FREQ_TRAN_VOTING_4, data->voting_rights_clients4);
1229 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1230 ixCG_FREQ_TRAN_VOTING_5, data->voting_rights_clients5);
1231 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1232 ixCG_FREQ_TRAN_VOTING_6, data->voting_rights_clients6);
1233 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1234 ixCG_FREQ_TRAN_VOTING_7, data->voting_rights_clients7);
1235
1236 return 0;
1237}
1238
Eric Huang55d74fc2016-05-31 17:06:14 -04001239static int fiji_clear_voting_clients(struct pp_hwmgr *hwmgr)
1240{
1241 /* Reset voting clients before disabling DPM */
1242 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1243 SCLK_PWRMGT_CNTL, RESET_SCLK_CNT, 1);
1244 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
1245 SCLK_PWRMGT_CNTL, RESET_BUSY_CNT, 1);
1246
1247 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1248 ixCG_FREQ_TRAN_VOTING_0, 0);
1249 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1250 ixCG_FREQ_TRAN_VOTING_1, 0);
1251 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1252 ixCG_FREQ_TRAN_VOTING_2, 0);
1253 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1254 ixCG_FREQ_TRAN_VOTING_3, 0);
1255 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1256 ixCG_FREQ_TRAN_VOTING_4, 0);
1257 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1258 ixCG_FREQ_TRAN_VOTING_5, 0);
1259 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1260 ixCG_FREQ_TRAN_VOTING_6, 0);
1261 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
1262 ixCG_FREQ_TRAN_VOTING_7, 0);
1263
1264 return 0;
1265}
1266
Eric Huangaabcb7c2015-08-26 16:52:28 -04001267/**
1268* Get the location of various tables inside the FW image.
1269*
1270* @param hwmgr the address of the powerplay hardware manager.
1271* @return always 0
1272*/
1273static int fiji_process_firmware_header(struct pp_hwmgr *hwmgr)
1274{
1275 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1276 struct fiji_smumgr *smu_data = (struct fiji_smumgr *)(hwmgr->smumgr->backend);
1277 uint32_t tmp;
1278 int result;
1279 bool error = false;
1280
1281 result = fiji_read_smc_sram_dword(hwmgr->smumgr,
1282 SMU7_FIRMWARE_HEADER_LOCATION +
1283 offsetof(SMU73_Firmware_Header, DpmTable),
1284 &tmp, data->sram_end);
1285
1286 if (0 == result)
1287 data->dpm_table_start = tmp;
1288
1289 error |= (0 != result);
1290
1291 result = fiji_read_smc_sram_dword(hwmgr->smumgr,
1292 SMU7_FIRMWARE_HEADER_LOCATION +
1293 offsetof(SMU73_Firmware_Header, SoftRegisters),
1294 &tmp, data->sram_end);
1295
1296 if (!result) {
1297 data->soft_regs_start = tmp;
1298 smu_data->soft_regs_start = tmp;
1299 }
1300
1301 error |= (0 != result);
1302
1303 result = fiji_read_smc_sram_dword(hwmgr->smumgr,
1304 SMU7_FIRMWARE_HEADER_LOCATION +
1305 offsetof(SMU73_Firmware_Header, mcRegisterTable),
1306 &tmp, data->sram_end);
1307
1308 if (!result)
1309 data->mc_reg_table_start = tmp;
1310
1311 result = fiji_read_smc_sram_dword(hwmgr->smumgr,
1312 SMU7_FIRMWARE_HEADER_LOCATION +
1313 offsetof(SMU73_Firmware_Header, FanTable),
1314 &tmp, data->sram_end);
1315
1316 if (!result)
1317 data->fan_table_start = tmp;
1318
1319 error |= (0 != result);
1320
1321 result = fiji_read_smc_sram_dword(hwmgr->smumgr,
1322 SMU7_FIRMWARE_HEADER_LOCATION +
1323 offsetof(SMU73_Firmware_Header, mcArbDramTimingTable),
1324 &tmp, data->sram_end);
1325
1326 if (!result)
1327 data->arb_table_start = tmp;
1328
1329 error |= (0 != result);
1330
1331 result = fiji_read_smc_sram_dword(hwmgr->smumgr,
1332 SMU7_FIRMWARE_HEADER_LOCATION +
1333 offsetof(SMU73_Firmware_Header, Version),
1334 &tmp, data->sram_end);
1335
1336 if (!result)
1337 hwmgr->microcode_version_info.SMC = tmp;
1338
1339 error |= (0 != result);
1340
Rex Zhu75ac63d2016-01-06 16:38:48 +08001341 return error ? -1 : 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -04001342}
1343
1344/* Copy one arb setting to another and then switch the active set.
1345 * arb_src and arb_dest is one of the MC_CG_ARB_FREQ_Fx constants.
1346 */
1347static int fiji_copy_and_switch_arb_sets(struct pp_hwmgr *hwmgr,
1348 uint32_t arb_src, uint32_t arb_dest)
1349{
1350 uint32_t mc_arb_dram_timing;
1351 uint32_t mc_arb_dram_timing2;
1352 uint32_t burst_time;
1353 uint32_t mc_cg_config;
1354
1355 switch (arb_src) {
1356 case MC_CG_ARB_FREQ_F0:
1357 mc_arb_dram_timing = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING);
1358 mc_arb_dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2);
1359 burst_time = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0);
1360 break;
1361 case MC_CG_ARB_FREQ_F1:
1362 mc_arb_dram_timing = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING_1);
1363 mc_arb_dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2_1);
1364 burst_time = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE1);
1365 break;
1366 default:
1367 return -EINVAL;
1368 }
1369
1370 switch (arb_dest) {
1371 case MC_CG_ARB_FREQ_F0:
1372 cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING, mc_arb_dram_timing);
1373 cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2, mc_arb_dram_timing2);
1374 PHM_WRITE_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0, burst_time);
1375 break;
1376 case MC_CG_ARB_FREQ_F1:
1377 cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING_1, mc_arb_dram_timing);
1378 cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2_1, mc_arb_dram_timing2);
1379 PHM_WRITE_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE1, burst_time);
1380 break;
1381 default:
1382 return -EINVAL;
1383 }
1384
Rex Zhu75ac63d2016-01-06 16:38:48 +08001385 mc_cg_config = cgs_read_register(hwmgr->device, mmMC_CG_CONFIG);
1386 mc_cg_config |= 0x0000000F;
1387 cgs_write_register(hwmgr->device, mmMC_CG_CONFIG, mc_cg_config);
1388 PHM_WRITE_FIELD(hwmgr->device, MC_ARB_CG, CG_ARB_REQ, arb_dest);
Eric Huangaabcb7c2015-08-26 16:52:28 -04001389
Rex Zhu75ac63d2016-01-06 16:38:48 +08001390 return 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -04001391}
1392
1393/**
Eric Huang55d74fc2016-05-31 17:06:14 -04001394* Call SMC to reset S0/S1 to S1 and Reset SMIO to initial value
1395*
1396* @param hwmgr the address of the powerplay hardware manager.
1397* @return if success then 0;
1398*/
1399static int fiji_reset_to_default(struct pp_hwmgr *hwmgr)
1400{
1401 return smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_ResetToDefaults);
1402}
1403
1404/**
Eric Huangaabcb7c2015-08-26 16:52:28 -04001405* Initial switch from ARB F0->F1
1406*
1407* @param hwmgr the address of the powerplay hardware manager.
1408* @return always 0
1409* This function is to be called from the SetPowerState table.
1410*/
1411static int fiji_initial_switch_from_arbf0_to_f1(struct pp_hwmgr *hwmgr)
1412{
1413 return fiji_copy_and_switch_arb_sets(hwmgr,
1414 MC_CG_ARB_FREQ_F0, MC_CG_ARB_FREQ_F1);
1415}
1416
Eric Huang55d74fc2016-05-31 17:06:14 -04001417static int fiji_force_switch_to_arbf0(struct pp_hwmgr *hwmgr)
1418{
1419 uint32_t tmp;
1420
1421 tmp = (cgs_read_ind_register(hwmgr->device,
1422 CGS_IND_REG__SMC, ixSMC_SCRATCH9) &
1423 0x0000ff00) >> 8;
1424
1425 if (tmp == MC_CG_ARB_FREQ_F0)
1426 return 0;
1427
1428 return fiji_copy_and_switch_arb_sets(hwmgr,
1429 tmp, MC_CG_ARB_FREQ_F0);
1430}
1431
Eric Huangaabcb7c2015-08-26 16:52:28 -04001432static int fiji_reset_single_dpm_table(struct pp_hwmgr *hwmgr,
1433 struct fiji_single_dpm_table *dpm_table, uint32_t count)
1434{
1435 int i;
1436 PP_ASSERT_WITH_CODE(count <= MAX_REGULAR_DPM_NUMBER,
1437 "Fatal error, can not set up single DPM table entries "
1438 "to exceed max number!",);
1439
1440 dpm_table->count = count;
1441 for (i = 0; i < MAX_REGULAR_DPM_NUMBER; i++)
1442 dpm_table->dpm_levels[i].enabled = false;
1443
1444 return 0;
1445}
1446
1447static void fiji_setup_pcie_table_entry(
1448 struct fiji_single_dpm_table *dpm_table,
1449 uint32_t index, uint32_t pcie_gen,
1450 uint32_t pcie_lanes)
1451{
1452 dpm_table->dpm_levels[index].value = pcie_gen;
1453 dpm_table->dpm_levels[index].param1 = pcie_lanes;
1454 dpm_table->dpm_levels[index].enabled = 1;
1455}
1456
1457static int fiji_setup_default_pcie_table(struct pp_hwmgr *hwmgr)
1458{
1459 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1460 struct phm_ppt_v1_information *table_info =
1461 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1462 struct phm_ppt_v1_pcie_table *pcie_table = table_info->pcie_table;
1463 uint32_t i, max_entry;
1464
1465 PP_ASSERT_WITH_CODE((data->use_pcie_performance_levels ||
1466 data->use_pcie_power_saving_levels), "No pcie performance levels!",
1467 return -EINVAL);
1468
1469 if (data->use_pcie_performance_levels &&
1470 !data->use_pcie_power_saving_levels) {
1471 data->pcie_gen_power_saving = data->pcie_gen_performance;
1472 data->pcie_lane_power_saving = data->pcie_lane_performance;
1473 } else if (!data->use_pcie_performance_levels &&
1474 data->use_pcie_power_saving_levels) {
1475 data->pcie_gen_performance = data->pcie_gen_power_saving;
1476 data->pcie_lane_performance = data->pcie_lane_power_saving;
1477 }
1478
1479 fiji_reset_single_dpm_table(hwmgr,
1480 &data->dpm_table.pcie_speed_table, SMU73_MAX_LEVELS_LINK);
1481
1482 if (pcie_table != NULL) {
1483 /* max_entry is used to make sure we reserve one PCIE level
1484 * for boot level (fix for A+A PSPP issue).
1485 * If PCIE table from PPTable have ULV entry + 8 entries,
1486 * then ignore the last entry.*/
1487 max_entry = (SMU73_MAX_LEVELS_LINK < pcie_table->count) ?
1488 SMU73_MAX_LEVELS_LINK : pcie_table->count;
1489 for (i = 1; i < max_entry; i++) {
1490 fiji_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, i - 1,
1491 get_pcie_gen_support(data->pcie_gen_cap,
1492 pcie_table->entries[i].gen_speed),
1493 get_pcie_lane_support(data->pcie_lane_cap,
1494 pcie_table->entries[i].lane_width));
1495 }
1496 data->dpm_table.pcie_speed_table.count = max_entry - 1;
1497 } else {
1498 /* Hardcode Pcie Table */
1499 fiji_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 0,
1500 get_pcie_gen_support(data->pcie_gen_cap,
1501 PP_Min_PCIEGen),
1502 get_pcie_lane_support(data->pcie_lane_cap,
1503 PP_Max_PCIELane));
1504 fiji_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 1,
1505 get_pcie_gen_support(data->pcie_gen_cap,
1506 PP_Min_PCIEGen),
1507 get_pcie_lane_support(data->pcie_lane_cap,
1508 PP_Max_PCIELane));
1509 fiji_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 2,
1510 get_pcie_gen_support(data->pcie_gen_cap,
1511 PP_Max_PCIEGen),
1512 get_pcie_lane_support(data->pcie_lane_cap,
1513 PP_Max_PCIELane));
1514 fiji_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 3,
1515 get_pcie_gen_support(data->pcie_gen_cap,
1516 PP_Max_PCIEGen),
1517 get_pcie_lane_support(data->pcie_lane_cap,
1518 PP_Max_PCIELane));
1519 fiji_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 4,
1520 get_pcie_gen_support(data->pcie_gen_cap,
1521 PP_Max_PCIEGen),
1522 get_pcie_lane_support(data->pcie_lane_cap,
1523 PP_Max_PCIELane));
1524 fiji_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 5,
1525 get_pcie_gen_support(data->pcie_gen_cap,
1526 PP_Max_PCIEGen),
1527 get_pcie_lane_support(data->pcie_lane_cap,
1528 PP_Max_PCIELane));
1529
1530 data->dpm_table.pcie_speed_table.count = 6;
1531 }
1532 /* Populate last level for boot PCIE level, but do not increment count. */
1533 fiji_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table,
1534 data->dpm_table.pcie_speed_table.count,
1535 get_pcie_gen_support(data->pcie_gen_cap,
1536 PP_Min_PCIEGen),
1537 get_pcie_lane_support(data->pcie_lane_cap,
1538 PP_Max_PCIELane));
1539
1540 return 0;
1541}
1542
1543/*
1544 * This function is to initalize all DPM state tables
1545 * for SMU7 based on the dependency table.
1546 * Dynamic state patching function will then trim these
1547 * state tables to the allowed range based
1548 * on the power policy or external client requests,
1549 * such as UVD request, etc.
1550 */
1551static int fiji_setup_default_dpm_tables(struct pp_hwmgr *hwmgr)
1552{
1553 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1554 struct phm_ppt_v1_information *table_info =
1555 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1556 uint32_t i;
1557
1558 struct phm_ppt_v1_clock_voltage_dependency_table *dep_sclk_table =
1559 table_info->vdd_dep_on_sclk;
1560 struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table =
1561 table_info->vdd_dep_on_mclk;
1562
1563 PP_ASSERT_WITH_CODE(dep_sclk_table != NULL,
1564 "SCLK dependency table is missing. This table is mandatory",
1565 return -EINVAL);
1566 PP_ASSERT_WITH_CODE(dep_sclk_table->count >= 1,
1567 "SCLK dependency table has to have is missing. "
1568 "This table is mandatory",
1569 return -EINVAL);
1570
1571 PP_ASSERT_WITH_CODE(dep_mclk_table != NULL,
1572 "MCLK dependency table is missing. This table is mandatory",
1573 return -EINVAL);
1574 PP_ASSERT_WITH_CODE(dep_mclk_table->count >= 1,
1575 "MCLK dependency table has to have is missing. "
1576 "This table is mandatory",
1577 return -EINVAL);
1578
1579 /* clear the state table to reset everything to default */
1580 fiji_reset_single_dpm_table(hwmgr,
1581 &data->dpm_table.sclk_table, SMU73_MAX_LEVELS_GRAPHICS);
1582 fiji_reset_single_dpm_table(hwmgr,
1583 &data->dpm_table.mclk_table, SMU73_MAX_LEVELS_MEMORY);
1584
1585 /* Initialize Sclk DPM table based on allow Sclk values */
1586 data->dpm_table.sclk_table.count = 0;
1587 for (i = 0; i < dep_sclk_table->count; i++) {
1588 if (i == 0 || data->dpm_table.sclk_table.dpm_levels
1589 [data->dpm_table.sclk_table.count - 1].value !=
1590 dep_sclk_table->entries[i].clk) {
1591 data->dpm_table.sclk_table.dpm_levels
1592 [data->dpm_table.sclk_table.count].value =
1593 dep_sclk_table->entries[i].clk;
1594 data->dpm_table.sclk_table.dpm_levels
1595 [data->dpm_table.sclk_table.count].enabled =
1596 (i == 0) ? true : false;
1597 data->dpm_table.sclk_table.count++;
1598 }
1599 }
1600
1601 /* Initialize Mclk DPM table based on allow Mclk values */
1602 data->dpm_table.mclk_table.count = 0;
1603 for (i=0; i<dep_mclk_table->count; i++) {
1604 if ( i==0 || data->dpm_table.mclk_table.dpm_levels
1605 [data->dpm_table.mclk_table.count - 1].value !=
1606 dep_mclk_table->entries[i].clk) {
1607 data->dpm_table.mclk_table.dpm_levels
1608 [data->dpm_table.mclk_table.count].value =
1609 dep_mclk_table->entries[i].clk;
1610 data->dpm_table.mclk_table.dpm_levels
1611 [data->dpm_table.mclk_table.count].enabled =
1612 (i == 0) ? true : false;
1613 data->dpm_table.mclk_table.count++;
1614 }
1615 }
1616
1617 /* setup PCIE gen speed levels */
1618 fiji_setup_default_pcie_table(hwmgr);
1619
1620 /* save a copy of the default DPM table */
1621 memcpy(&(data->golden_dpm_table), &(data->dpm_table),
1622 sizeof(struct fiji_dpm_table));
1623
1624 return 0;
1625}
1626
1627/**
1628 * @brief PhwFiji_GetVoltageOrder
1629 * Returns index of requested voltage record in lookup(table)
1630 * @param lookup_table - lookup list to search in
1631 * @param voltage - voltage to look for
1632 * @return 0 on success
1633 */
1634uint8_t fiji_get_voltage_index(
1635 struct phm_ppt_v1_voltage_lookup_table *lookup_table, uint16_t voltage)
1636{
1637 uint8_t count = (uint8_t) (lookup_table->count);
1638 uint8_t i;
1639
1640 PP_ASSERT_WITH_CODE((NULL != lookup_table),
1641 "Lookup Table empty.", return 0);
1642 PP_ASSERT_WITH_CODE((0 != count),
1643 "Lookup Table empty.", return 0);
1644
1645 for (i = 0; i < lookup_table->count; i++) {
1646 /* find first voltage equal or bigger than requested */
1647 if (lookup_table->entries[i].us_vdd >= voltage)
1648 return i;
1649 }
1650 /* voltage is bigger than max voltage in the table */
1651 return i - 1;
1652}
1653
1654/**
1655* Preparation of vddc and vddgfx CAC tables for SMC.
1656*
1657* @param hwmgr the address of the hardware manager
1658* @param table the SMC DPM table structure to be populated
1659* @return always 0
1660*/
1661static int fiji_populate_cac_table(struct pp_hwmgr *hwmgr,
1662 struct SMU73_Discrete_DpmTable *table)
1663{
1664 uint32_t count;
1665 uint8_t index;
1666 int result = 0;
1667 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1668 struct phm_ppt_v1_information *table_info =
1669 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1670 struct phm_ppt_v1_voltage_lookup_table *lookup_table =
1671 table_info->vddc_lookup_table;
1672 /* tables is already swapped, so in order to use the value from it,
1673 * we need to swap it back.
1674 * We are populating vddc CAC data to BapmVddc table
1675 * in split and merged mode
1676 */
1677 for( count = 0; count<lookup_table->count; count++) {
1678 index = fiji_get_voltage_index(lookup_table,
1679 data->vddc_voltage_table.entries[count].value);
1680 table->BapmVddcVidLoSidd[count] = (uint8_t) ((6200 -
1681 (lookup_table->entries[index].us_cac_low *
1682 VOLTAGE_SCALE)) / 25);
1683 table->BapmVddcVidHiSidd[count] = (uint8_t) ((6200 -
1684 (lookup_table->entries[index].us_cac_high *
1685 VOLTAGE_SCALE)) / 25);
1686 }
1687
1688 return result;
1689}
1690
1691/**
1692* Preparation of voltage tables for SMC.
1693*
1694* @param hwmgr the address of the hardware manager
1695* @param table the SMC DPM table structure to be populated
1696* @return always 0
1697*/
1698
1699int fiji_populate_smc_voltage_tables(struct pp_hwmgr *hwmgr,
1700 struct SMU73_Discrete_DpmTable *table)
1701{
1702 int result;
1703
1704 result = fiji_populate_cac_table(hwmgr, table);
1705 PP_ASSERT_WITH_CODE(0 == result,
1706 "can not populate CAC voltage tables to SMC",
1707 return -EINVAL);
1708
1709 return 0;
1710}
1711
1712static int fiji_populate_ulv_level(struct pp_hwmgr *hwmgr,
1713 struct SMU73_Discrete_Ulv *state)
1714{
1715 int result = 0;
1716 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1717 struct phm_ppt_v1_information *table_info =
1718 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1719
1720 state->CcPwrDynRm = 0;
1721 state->CcPwrDynRm1 = 0;
1722
1723 state->VddcOffset = (uint16_t) table_info->us_ulv_voltage_offset;
1724 state->VddcOffsetVid = (uint8_t)( table_info->us_ulv_voltage_offset *
1725 VOLTAGE_VID_OFFSET_SCALE2 / VOLTAGE_VID_OFFSET_SCALE1 );
1726
1727 state->VddcPhase = (data->vddc_phase_shed_control) ? 0 : 1;
1728
1729 if (!result) {
1730 CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm);
1731 CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm1);
1732 CONVERT_FROM_HOST_TO_SMC_US(state->VddcOffset);
1733 }
1734 return result;
1735}
1736
1737static int fiji_populate_ulv_state(struct pp_hwmgr *hwmgr,
1738 struct SMU73_Discrete_DpmTable *table)
1739{
1740 return fiji_populate_ulv_level(hwmgr, &table->Ulv);
1741}
1742
1743static int32_t fiji_get_dpm_level_enable_mask_value(
1744 struct fiji_single_dpm_table* dpm_table)
1745{
1746 int32_t i;
1747 int32_t mask = 0;
1748
1749 for (i = dpm_table->count; i > 0; i--) {
1750 mask = mask << 1;
1751 if (dpm_table->dpm_levels[i - 1].enabled)
1752 mask |= 0x1;
1753 else
1754 mask &= 0xFFFFFFFE;
1755 }
1756 return mask;
1757}
1758
1759static int fiji_populate_smc_link_level(struct pp_hwmgr *hwmgr,
1760 struct SMU73_Discrete_DpmTable *table)
1761{
1762 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1763 struct fiji_dpm_table *dpm_table = &data->dpm_table;
1764 int i;
1765
1766 /* Index (dpm_table->pcie_speed_table.count)
1767 * is reserved for PCIE boot level. */
1768 for (i = 0; i <= dpm_table->pcie_speed_table.count; i++) {
1769 table->LinkLevel[i].PcieGenSpeed =
1770 (uint8_t)dpm_table->pcie_speed_table.dpm_levels[i].value;
1771 table->LinkLevel[i].PcieLaneCount = (uint8_t)encode_pcie_lane_width(
1772 dpm_table->pcie_speed_table.dpm_levels[i].param1);
1773 table->LinkLevel[i].EnabledForActivity = 1;
1774 table->LinkLevel[i].SPC = (uint8_t)(data->pcie_spc_cap & 0xff);
1775 table->LinkLevel[i].DownThreshold = PP_HOST_TO_SMC_UL(5);
1776 table->LinkLevel[i].UpThreshold = PP_HOST_TO_SMC_UL(30);
1777 }
1778
1779 data->smc_state_table.LinkLevelCount =
1780 (uint8_t)dpm_table->pcie_speed_table.count;
1781 data->dpm_level_enable_mask.pcie_dpm_enable_mask =
1782 fiji_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
1783
1784 return 0;
1785}
1786
1787/**
1788* Calculates the SCLK dividers using the provided engine clock
1789*
1790* @param hwmgr the address of the hardware manager
1791* @param clock the engine clock to use to populate the structure
1792* @param sclk the SMC SCLK structure to be populated
1793*/
1794static int fiji_calculate_sclk_params(struct pp_hwmgr *hwmgr,
1795 uint32_t clock, struct SMU73_Discrete_GraphicsLevel *sclk)
1796{
1797 const struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1798 struct pp_atomctrl_clock_dividers_vi dividers;
1799 uint32_t spll_func_cntl = data->clock_registers.vCG_SPLL_FUNC_CNTL;
1800 uint32_t spll_func_cntl_3 = data->clock_registers.vCG_SPLL_FUNC_CNTL_3;
1801 uint32_t spll_func_cntl_4 = data->clock_registers.vCG_SPLL_FUNC_CNTL_4;
1802 uint32_t cg_spll_spread_spectrum = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM;
1803 uint32_t cg_spll_spread_spectrum_2 = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2;
1804 uint32_t ref_clock;
1805 uint32_t ref_divider;
1806 uint32_t fbdiv;
1807 int result;
1808
1809 /* get the engine clock dividers for this clock value */
1810 result = atomctrl_get_engine_pll_dividers_vi(hwmgr, clock, &dividers);
1811
1812 PP_ASSERT_WITH_CODE(result == 0,
1813 "Error retrieving Engine Clock dividers from VBIOS.",
1814 return result);
1815
1816 /* To get FBDIV we need to multiply this by 16384 and divide it by Fref. */
1817 ref_clock = atomctrl_get_reference_clock(hwmgr);
1818 ref_divider = 1 + dividers.uc_pll_ref_div;
1819
1820 /* low 14 bits is fraction and high 12 bits is divider */
1821 fbdiv = dividers.ul_fb_div.ul_fb_divider & 0x3FFFFFF;
1822
1823 /* SPLL_FUNC_CNTL setup */
1824 spll_func_cntl = PHM_SET_FIELD(spll_func_cntl, CG_SPLL_FUNC_CNTL,
1825 SPLL_REF_DIV, dividers.uc_pll_ref_div);
1826 spll_func_cntl = PHM_SET_FIELD(spll_func_cntl, CG_SPLL_FUNC_CNTL,
1827 SPLL_PDIV_A, dividers.uc_pll_post_div);
1828
1829 /* SPLL_FUNC_CNTL_3 setup*/
1830 spll_func_cntl_3 = PHM_SET_FIELD(spll_func_cntl_3, CG_SPLL_FUNC_CNTL_3,
1831 SPLL_FB_DIV, fbdiv);
1832
1833 /* set to use fractional accumulation*/
1834 spll_func_cntl_3 = PHM_SET_FIELD(spll_func_cntl_3, CG_SPLL_FUNC_CNTL_3,
1835 SPLL_DITHEN, 1);
1836
1837 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1838 PHM_PlatformCaps_EngineSpreadSpectrumSupport)) {
1839 struct pp_atomctrl_internal_ss_info ssInfo;
1840
1841 uint32_t vco_freq = clock * dividers.uc_pll_post_div;
1842 if (!atomctrl_get_engine_clock_spread_spectrum(hwmgr,
1843 vco_freq, &ssInfo)) {
1844 /*
1845 * ss_info.speed_spectrum_percentage -- in unit of 0.01%
1846 * ss_info.speed_spectrum_rate -- in unit of khz
1847 *
1848 * clks = reference_clock * 10 / (REFDIV + 1) / speed_spectrum_rate / 2
1849 */
1850 uint32_t clk_s = ref_clock * 5 /
1851 (ref_divider * ssInfo.speed_spectrum_rate);
1852 /* clkv = 2 * D * fbdiv / NS */
1853 uint32_t clk_v = 4 * ssInfo.speed_spectrum_percentage *
1854 fbdiv / (clk_s * 10000);
1855
1856 cg_spll_spread_spectrum = PHM_SET_FIELD(cg_spll_spread_spectrum,
1857 CG_SPLL_SPREAD_SPECTRUM, CLKS, clk_s);
1858 cg_spll_spread_spectrum = PHM_SET_FIELD(cg_spll_spread_spectrum,
1859 CG_SPLL_SPREAD_SPECTRUM, SSEN, 1);
1860 cg_spll_spread_spectrum_2 = PHM_SET_FIELD(cg_spll_spread_spectrum_2,
1861 CG_SPLL_SPREAD_SPECTRUM_2, CLKV, clk_v);
1862 }
1863 }
1864
1865 sclk->SclkFrequency = clock;
1866 sclk->CgSpllFuncCntl3 = spll_func_cntl_3;
1867 sclk->CgSpllFuncCntl4 = spll_func_cntl_4;
1868 sclk->SpllSpreadSpectrum = cg_spll_spread_spectrum;
1869 sclk->SpllSpreadSpectrum2 = cg_spll_spread_spectrum_2;
1870 sclk->SclkDid = (uint8_t)dividers.pll_post_divider;
1871
1872 return 0;
1873}
1874
1875static uint16_t fiji_find_closest_vddci(struct pp_hwmgr *hwmgr, uint16_t vddci)
1876{
1877 uint32_t i;
1878 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1879 struct pp_atomctrl_voltage_table *vddci_table =
1880 &(data->vddci_voltage_table);
1881
1882 for (i = 0; i < vddci_table->count; i++) {
1883 if (vddci_table->entries[i].value >= vddci)
1884 return vddci_table->entries[i].value;
1885 }
1886
1887 PP_ASSERT_WITH_CODE(false,
1888 "VDDCI is larger than max VDDCI in VDDCI Voltage Table!",
Rex Zhud2e31212016-06-02 19:11:01 +08001889 return vddci_table->entries[i-1].value);
Eric Huangaabcb7c2015-08-26 16:52:28 -04001890}
1891
1892static int fiji_get_dependency_volt_by_clk(struct pp_hwmgr *hwmgr,
1893 struct phm_ppt_v1_clock_voltage_dependency_table* dep_table,
1894 uint32_t clock, SMU_VoltageLevel *voltage, uint32_t *mvdd)
1895{
1896 uint32_t i;
1897 uint16_t vddci;
1898 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1899
1900 *voltage = *mvdd = 0;
1901
1902 /* clock - voltage dependency table is empty table */
1903 if (dep_table->count == 0)
1904 return -EINVAL;
1905
1906 for (i = 0; i < dep_table->count; i++) {
1907 /* find first sclk bigger than request */
1908 if (dep_table->entries[i].clk >= clock) {
1909 *voltage |= (dep_table->entries[i].vddc *
1910 VOLTAGE_SCALE) << VDDC_SHIFT;
1911 if (FIJI_VOLTAGE_CONTROL_NONE == data->vddci_control)
1912 *voltage |= (data->vbios_boot_state.vddci_bootup_value *
1913 VOLTAGE_SCALE) << VDDCI_SHIFT;
1914 else if (dep_table->entries[i].vddci)
1915 *voltage |= (dep_table->entries[i].vddci *
1916 VOLTAGE_SCALE) << VDDCI_SHIFT;
1917 else {
1918 vddci = fiji_find_closest_vddci(hwmgr,
1919 (dep_table->entries[i].vddc -
1920 (uint16_t)data->vddc_vddci_delta));
1921 *voltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
1922 }
1923
1924 if (FIJI_VOLTAGE_CONTROL_NONE == data->mvdd_control)
1925 *mvdd = data->vbios_boot_state.mvdd_bootup_value *
1926 VOLTAGE_SCALE;
1927 else if (dep_table->entries[i].mvdd)
1928 *mvdd = (uint32_t) dep_table->entries[i].mvdd *
1929 VOLTAGE_SCALE;
1930
1931 *voltage |= 1 << PHASES_SHIFT;
1932 return 0;
1933 }
1934 }
1935
1936 /* sclk is bigger than max sclk in the dependence table */
1937 *voltage |= (dep_table->entries[i - 1].vddc * VOLTAGE_SCALE) << VDDC_SHIFT;
1938
1939 if (FIJI_VOLTAGE_CONTROL_NONE == data->vddci_control)
1940 *voltage |= (data->vbios_boot_state.vddci_bootup_value *
1941 VOLTAGE_SCALE) << VDDCI_SHIFT;
1942 else if (dep_table->entries[i-1].vddci) {
1943 vddci = fiji_find_closest_vddci(hwmgr,
1944 (dep_table->entries[i].vddc -
1945 (uint16_t)data->vddc_vddci_delta));
1946 *voltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
1947 }
1948
1949 if (FIJI_VOLTAGE_CONTROL_NONE == data->mvdd_control)
1950 *mvdd = data->vbios_boot_state.mvdd_bootup_value * VOLTAGE_SCALE;
1951 else if (dep_table->entries[i].mvdd)
1952 *mvdd = (uint32_t) dep_table->entries[i - 1].mvdd * VOLTAGE_SCALE;
1953
1954 return 0;
1955}
Rex Zhu0eadeab2016-03-29 19:32:37 +08001956
Nils Wallménius438498a2016-05-05 09:07:48 +02001957static uint8_t fiji_get_sleep_divider_id_from_clock(uint32_t clock,
1958 uint32_t clock_insr)
Rex Zhu0eadeab2016-03-29 19:32:37 +08001959{
1960 uint8_t i;
1961 uint32_t temp;
Nils Wallménius9887e422016-05-05 09:07:46 +02001962 uint32_t min = max(clock_insr, (uint32_t)FIJI_MINIMUM_ENGINE_CLOCK);
Rex Zhu0eadeab2016-03-29 19:32:37 +08001963
1964 PP_ASSERT_WITH_CODE((clock >= min), "Engine clock can't satisfy stutter requirement!", return 0);
1965 for (i = FIJI_MAX_DEEPSLEEP_DIVIDER_ID; ; i--) {
Nils Wallménius354ef922016-05-05 09:07:47 +02001966 temp = clock >> i;
Rex Zhu0eadeab2016-03-29 19:32:37 +08001967
1968 if (temp >= min || i == 0)
1969 break;
1970 }
1971 return i;
1972}
Eric Huangaabcb7c2015-08-26 16:52:28 -04001973/**
1974* Populates single SMC SCLK structure using the provided engine clock
1975*
1976* @param hwmgr the address of the hardware manager
1977* @param clock the engine clock to use to populate the structure
1978* @param sclk the SMC SCLK structure to be populated
1979*/
1980
1981static int fiji_populate_single_graphic_level(struct pp_hwmgr *hwmgr,
1982 uint32_t clock, uint16_t sclk_al_threshold,
1983 struct SMU73_Discrete_GraphicsLevel *level)
1984{
1985 int result;
1986 /* PP_Clocks minClocks; */
1987 uint32_t threshold, mvdd;
1988 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
1989 struct phm_ppt_v1_information *table_info =
1990 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1991
1992 result = fiji_calculate_sclk_params(hwmgr, clock, level);
1993
1994 /* populate graphics levels */
1995 result = fiji_get_dependency_volt_by_clk(hwmgr,
1996 table_info->vdd_dep_on_sclk, clock,
1997 &level->MinVoltage, &mvdd);
1998 PP_ASSERT_WITH_CODE((0 == result),
1999 "can not find VDDC voltage value for "
2000 "VDDC engine clock dependency table",
2001 return result);
2002
2003 level->SclkFrequency = clock;
2004 level->ActivityLevel = sclk_al_threshold;
2005 level->CcPwrDynRm = 0;
2006 level->CcPwrDynRm1 = 0;
2007 level->EnabledForActivity = 0;
2008 level->EnabledForThrottle = 1;
2009 level->UpHyst = 10;
2010 level->DownHyst = 0;
2011 level->VoltageDownHyst = 0;
2012 level->PowerThrottle = 0;
2013
2014 threshold = clock * data->fast_watermark_threshold / 100;
2015
Eric Huangaabcb7c2015-08-26 16:52:28 -04002016
Rex Zhu0eadeab2016-03-29 19:32:37 +08002017 data->display_timing.min_clock_in_sr = hwmgr->display_config.min_core_set_clock_in_sr;
2018
2019 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep))
Nils Wallménius438498a2016-05-05 09:07:48 +02002020 level->DeepSleepDivId = fiji_get_sleep_divider_id_from_clock(clock,
Rex Zhu0eadeab2016-03-29 19:32:37 +08002021 hwmgr->display_config.min_core_set_clock_in_sr);
2022
Eric Huangaabcb7c2015-08-26 16:52:28 -04002023
2024 /* Default to slow, highest DPM level will be
2025 * set to PPSMC_DISPLAY_WATERMARK_LOW later.
2026 */
2027 level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
2028
2029 CONVERT_FROM_HOST_TO_SMC_UL(level->MinVoltage);
2030 CONVERT_FROM_HOST_TO_SMC_UL(level->SclkFrequency);
2031 CONVERT_FROM_HOST_TO_SMC_US(level->ActivityLevel);
2032 CONVERT_FROM_HOST_TO_SMC_UL(level->CgSpllFuncCntl3);
2033 CONVERT_FROM_HOST_TO_SMC_UL(level->CgSpllFuncCntl4);
2034 CONVERT_FROM_HOST_TO_SMC_UL(level->SpllSpreadSpectrum);
2035 CONVERT_FROM_HOST_TO_SMC_UL(level->SpllSpreadSpectrum2);
2036 CONVERT_FROM_HOST_TO_SMC_UL(level->CcPwrDynRm);
2037 CONVERT_FROM_HOST_TO_SMC_UL(level->CcPwrDynRm1);
2038
2039 return 0;
2040}
2041/**
2042* Populates all SMC SCLK levels' structure based on the trimmed allowed dpm engine clock states
2043*
2044* @param hwmgr the address of the hardware manager
2045*/
2046static int fiji_populate_all_graphic_levels(struct pp_hwmgr *hwmgr)
2047{
2048 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2049 struct fiji_dpm_table *dpm_table = &data->dpm_table;
2050 struct phm_ppt_v1_information *table_info =
2051 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2052 struct phm_ppt_v1_pcie_table *pcie_table = table_info->pcie_table;
2053 uint8_t pcie_entry_cnt = (uint8_t) data->dpm_table.pcie_speed_table.count;
2054 int result = 0;
2055 uint32_t array = data->dpm_table_start +
2056 offsetof(SMU73_Discrete_DpmTable, GraphicsLevel);
2057 uint32_t array_size = sizeof(struct SMU73_Discrete_GraphicsLevel) *
2058 SMU73_MAX_LEVELS_GRAPHICS;
2059 struct SMU73_Discrete_GraphicsLevel *levels =
2060 data->smc_state_table.GraphicsLevel;
2061 uint32_t i, max_entry;
2062 uint8_t hightest_pcie_level_enabled = 0,
2063 lowest_pcie_level_enabled = 0,
2064 mid_pcie_level_enabled = 0,
2065 count = 0;
2066
2067 for (i = 0; i < dpm_table->sclk_table.count; i++) {
2068 result = fiji_populate_single_graphic_level(hwmgr,
2069 dpm_table->sclk_table.dpm_levels[i].value,
2070 (uint16_t)data->activity_target[i],
2071 &levels[i]);
2072 if (result)
2073 return result;
2074
2075 /* Making sure only DPM level 0-1 have Deep Sleep Div ID populated. */
2076 if (i > 1)
2077 levels[i].DeepSleepDivId = 0;
2078 }
2079
2080 /* Only enable level 0 for now.*/
2081 levels[0].EnabledForActivity = 1;
2082
2083 /* set highest level watermark to high */
2084 levels[dpm_table->sclk_table.count - 1].DisplayWatermark =
2085 PPSMC_DISPLAY_WATERMARK_HIGH;
2086
2087 data->smc_state_table.GraphicsDpmLevelCount =
2088 (uint8_t)dpm_table->sclk_table.count;
2089 data->dpm_level_enable_mask.sclk_dpm_enable_mask =
2090 fiji_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
2091
2092 if (pcie_table != NULL) {
2093 PP_ASSERT_WITH_CODE((1 <= pcie_entry_cnt),
2094 "There must be 1 or more PCIE levels defined in PPTable.",
2095 return -EINVAL);
2096 max_entry = pcie_entry_cnt - 1;
2097 for (i = 0; i < dpm_table->sclk_table.count; i++)
2098 levels[i].pcieDpmLevel =
2099 (uint8_t) ((i < max_entry)? i : max_entry);
2100 } else {
2101 while (data->dpm_level_enable_mask.pcie_dpm_enable_mask &&
2102 ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
2103 (1 << (hightest_pcie_level_enabled + 1))) != 0 ))
2104 hightest_pcie_level_enabled++;
2105
2106 while (data->dpm_level_enable_mask.pcie_dpm_enable_mask &&
2107 ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
2108 (1 << lowest_pcie_level_enabled)) == 0 ))
2109 lowest_pcie_level_enabled++;
2110
2111 while ((count < hightest_pcie_level_enabled) &&
2112 ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
2113 (1 << (lowest_pcie_level_enabled + 1 + count))) == 0 ))
2114 count++;
2115
2116 mid_pcie_level_enabled = (lowest_pcie_level_enabled + 1+ count) <
2117 hightest_pcie_level_enabled?
2118 (lowest_pcie_level_enabled + 1 + count) :
2119 hightest_pcie_level_enabled;
2120
2121 /* set pcieDpmLevel to hightest_pcie_level_enabled */
2122 for(i = 2; i < dpm_table->sclk_table.count; i++)
2123 levels[i].pcieDpmLevel = hightest_pcie_level_enabled;
2124
2125 /* set pcieDpmLevel to lowest_pcie_level_enabled */
2126 levels[0].pcieDpmLevel = lowest_pcie_level_enabled;
2127
2128 /* set pcieDpmLevel to mid_pcie_level_enabled */
2129 levels[1].pcieDpmLevel = mid_pcie_level_enabled;
2130 }
2131 /* level count will send to smc once at init smc table and never change */
2132 result = fiji_copy_bytes_to_smc(hwmgr->smumgr, array, (uint8_t *)levels,
2133 (uint32_t)array_size, data->sram_end);
2134
2135 return result;
2136}
2137
2138/**
2139 * MCLK Frequency Ratio
2140 * SEQ_CG_RESP Bit[31:24] - 0x0
2141 * Bit[27:24] \96 DDR3 Frequency ratio
2142 * 0x0 <= 100MHz, 450 < 0x8 <= 500MHz
2143 * 100 < 0x1 <= 150MHz, 500 < 0x9 <= 550MHz
2144 * 150 < 0x2 <= 200MHz, 550 < 0xA <= 600MHz
2145 * 200 < 0x3 <= 250MHz, 600 < 0xB <= 650MHz
2146 * 250 < 0x4 <= 300MHz, 650 < 0xC <= 700MHz
2147 * 300 < 0x5 <= 350MHz, 700 < 0xD <= 750MHz
2148 * 350 < 0x6 <= 400MHz, 750 < 0xE <= 800MHz
2149 * 400 < 0x7 <= 450MHz, 800 < 0xF
2150 */
2151static uint8_t fiji_get_mclk_frequency_ratio(uint32_t mem_clock)
2152{
2153 if (mem_clock <= 10000) return 0x0;
2154 if (mem_clock <= 15000) return 0x1;
2155 if (mem_clock <= 20000) return 0x2;
2156 if (mem_clock <= 25000) return 0x3;
2157 if (mem_clock <= 30000) return 0x4;
2158 if (mem_clock <= 35000) return 0x5;
2159 if (mem_clock <= 40000) return 0x6;
2160 if (mem_clock <= 45000) return 0x7;
2161 if (mem_clock <= 50000) return 0x8;
2162 if (mem_clock <= 55000) return 0x9;
2163 if (mem_clock <= 60000) return 0xa;
2164 if (mem_clock <= 65000) return 0xb;
2165 if (mem_clock <= 70000) return 0xc;
2166 if (mem_clock <= 75000) return 0xd;
2167 if (mem_clock <= 80000) return 0xe;
2168 /* mem_clock > 800MHz */
2169 return 0xf;
2170}
2171
2172/**
2173* Populates the SMC MCLK structure using the provided memory clock
2174*
2175* @param hwmgr the address of the hardware manager
2176* @param clock the memory clock to use to populate the structure
2177* @param sclk the SMC SCLK structure to be populated
2178*/
2179static int fiji_calculate_mclk_params(struct pp_hwmgr *hwmgr,
2180 uint32_t clock, struct SMU73_Discrete_MemoryLevel *mclk)
2181{
2182 struct pp_atomctrl_memory_clock_param mem_param;
2183 int result;
2184
2185 result = atomctrl_get_memory_pll_dividers_vi(hwmgr, clock, &mem_param);
2186 PP_ASSERT_WITH_CODE((0 == result),
2187 "Failed to get Memory PLL Dividers.",);
2188
2189 /* Save the result data to outpupt memory level structure */
2190 mclk->MclkFrequency = clock;
2191 mclk->MclkDivider = (uint8_t)mem_param.mpll_post_divider;
2192 mclk->FreqRange = fiji_get_mclk_frequency_ratio(clock);
2193
2194 return result;
2195}
2196
2197static int fiji_populate_single_memory_level(struct pp_hwmgr *hwmgr,
2198 uint32_t clock, struct SMU73_Discrete_MemoryLevel *mem_level)
2199{
2200 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2201 struct phm_ppt_v1_information *table_info =
2202 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2203 int result = 0;
2204
2205 if (table_info->vdd_dep_on_mclk) {
2206 result = fiji_get_dependency_volt_by_clk(hwmgr,
2207 table_info->vdd_dep_on_mclk, clock,
2208 &mem_level->MinVoltage, &mem_level->MinMvdd);
2209 PP_ASSERT_WITH_CODE((0 == result),
2210 "can not find MinVddc voltage value from memory "
2211 "VDDC voltage dependency table", return result);
2212 }
2213
2214 mem_level->EnabledForThrottle = 1;
2215 mem_level->EnabledForActivity = 0;
2216 mem_level->UpHyst = 0;
2217 mem_level->DownHyst = 100;
2218 mem_level->VoltageDownHyst = 0;
2219 mem_level->ActivityLevel = (uint16_t)data->mclk_activity_target;
2220 mem_level->StutterEnable = false;
2221
2222 mem_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
2223
2224 /* enable stutter mode if all the follow condition applied
2225 * PECI_GetNumberOfActiveDisplays(hwmgr->pPECI,
2226 * &(data->DisplayTiming.numExistingDisplays));
2227 */
2228 data->display_timing.num_existing_displays = 1;
2229
2230 if ((data->mclk_stutter_mode_threshold) &&
2231 (clock <= data->mclk_stutter_mode_threshold) &&
2232 (!data->is_uvd_enabled) &&
2233 (PHM_READ_FIELD(hwmgr->device, DPG_PIPE_STUTTER_CONTROL,
2234 STUTTER_ENABLE) & 0x1))
2235 mem_level->StutterEnable = true;
2236
2237 result = fiji_calculate_mclk_params(hwmgr, clock, mem_level);
2238 if (!result) {
2239 CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MinMvdd);
2240 CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MclkFrequency);
2241 CONVERT_FROM_HOST_TO_SMC_US(mem_level->ActivityLevel);
2242 CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MinVoltage);
2243 }
2244 return result;
2245}
2246
2247/**
2248* Populates all SMC MCLK levels' structure based on the trimmed allowed dpm memory clock states
2249*
2250* @param hwmgr the address of the hardware manager
2251*/
2252static int fiji_populate_all_memory_levels(struct pp_hwmgr *hwmgr)
2253{
2254 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2255 struct fiji_dpm_table *dpm_table = &data->dpm_table;
2256 int result;
2257 /* populate MCLK dpm table to SMU7 */
2258 uint32_t array = data->dpm_table_start +
2259 offsetof(SMU73_Discrete_DpmTable, MemoryLevel);
2260 uint32_t array_size = sizeof(SMU73_Discrete_MemoryLevel) *
2261 SMU73_MAX_LEVELS_MEMORY;
2262 struct SMU73_Discrete_MemoryLevel *levels =
2263 data->smc_state_table.MemoryLevel;
2264 uint32_t i;
2265
2266 for (i = 0; i < dpm_table->mclk_table.count; i++) {
2267 PP_ASSERT_WITH_CODE((0 != dpm_table->mclk_table.dpm_levels[i].value),
2268 "can not populate memory level as memory clock is zero",
2269 return -EINVAL);
2270 result = fiji_populate_single_memory_level(hwmgr,
2271 dpm_table->mclk_table.dpm_levels[i].value,
2272 &levels[i]);
2273 if (result)
2274 return result;
2275 }
2276
2277 /* Only enable level 0 for now. */
2278 levels[0].EnabledForActivity = 1;
2279
2280 /* in order to prevent MC activity from stutter mode to push DPM up.
2281 * the UVD change complements this by putting the MCLK in
2282 * a higher state by default such that we are not effected by
2283 * up threshold or and MCLK DPM latency.
2284 */
2285 levels[0].ActivityLevel = (uint16_t)data->mclk_dpm0_activity_target;
2286 CONVERT_FROM_HOST_TO_SMC_US(levels[0].ActivityLevel);
2287
2288 data->smc_state_table.MemoryDpmLevelCount =
2289 (uint8_t)dpm_table->mclk_table.count;
2290 data->dpm_level_enable_mask.mclk_dpm_enable_mask =
2291 fiji_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
2292 /* set highest level watermark to high */
2293 levels[dpm_table->mclk_table.count - 1].DisplayWatermark =
2294 PPSMC_DISPLAY_WATERMARK_HIGH;
2295
2296 /* level count will send to smc once at init smc table and never change */
2297 result = fiji_copy_bytes_to_smc(hwmgr->smumgr, array, (uint8_t *)levels,
2298 (uint32_t)array_size, data->sram_end);
2299
2300 return result;
2301}
2302
2303/**
2304* Populates the SMC MVDD structure using the provided memory clock.
2305*
2306* @param hwmgr the address of the hardware manager
2307* @param mclk the MCLK value to be used in the decision if MVDD should be high or low.
2308* @param voltage the SMC VOLTAGE structure to be populated
2309*/
2310int fiji_populate_mvdd_value(struct pp_hwmgr *hwmgr,
2311 uint32_t mclk, SMIO_Pattern *smio_pat)
2312{
2313 const struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2314 struct phm_ppt_v1_information *table_info =
2315 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2316 uint32_t i = 0;
2317
2318 if (FIJI_VOLTAGE_CONTROL_NONE != data->mvdd_control) {
2319 /* find mvdd value which clock is more than request */
2320 for (i = 0; i < table_info->vdd_dep_on_mclk->count; i++) {
2321 if (mclk <= table_info->vdd_dep_on_mclk->entries[i].clk) {
2322 smio_pat->Voltage = data->mvdd_voltage_table.entries[i].value;
2323 break;
2324 }
2325 }
2326 PP_ASSERT_WITH_CODE(i < table_info->vdd_dep_on_mclk->count,
2327 "MVDD Voltage is outside the supported range.",
2328 return -EINVAL);
2329 } else
2330 return -EINVAL;
2331
2332 return 0;
2333}
2334
2335static int fiji_populate_smc_acpi_level(struct pp_hwmgr *hwmgr,
2336 SMU73_Discrete_DpmTable *table)
2337{
2338 int result = 0;
2339 const struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2340 struct phm_ppt_v1_information *table_info =
2341 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2342 struct pp_atomctrl_clock_dividers_vi dividers;
2343 SMIO_Pattern vol_level;
2344 uint32_t mvdd;
2345 uint16_t us_mvdd;
2346 uint32_t spll_func_cntl = data->clock_registers.vCG_SPLL_FUNC_CNTL;
2347 uint32_t spll_func_cntl_2 = data->clock_registers.vCG_SPLL_FUNC_CNTL_2;
2348
2349 table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
2350
2351 if (!data->sclk_dpm_key_disabled) {
2352 /* Get MinVoltage and Frequency from DPM0,
2353 * already converted to SMC_UL */
2354 table->ACPILevel.SclkFrequency =
2355 data->dpm_table.sclk_table.dpm_levels[0].value;
2356 result = fiji_get_dependency_volt_by_clk(hwmgr,
2357 table_info->vdd_dep_on_sclk,
2358 table->ACPILevel.SclkFrequency,
2359 &table->ACPILevel.MinVoltage, &mvdd);
2360 PP_ASSERT_WITH_CODE((0 == result),
2361 "Cannot find ACPI VDDC voltage value "
2362 "in Clock Dependency Table",);
2363 } else {
2364 table->ACPILevel.SclkFrequency =
2365 data->vbios_boot_state.sclk_bootup_value;
2366 table->ACPILevel.MinVoltage =
2367 data->vbios_boot_state.vddc_bootup_value * VOLTAGE_SCALE;
2368 }
2369
2370 /* get the engine clock dividers for this clock value */
2371 result = atomctrl_get_engine_pll_dividers_vi(hwmgr,
2372 table->ACPILevel.SclkFrequency, &dividers);
2373 PP_ASSERT_WITH_CODE(result == 0,
2374 "Error retrieving Engine Clock dividers from VBIOS.",
2375 return result);
2376
2377 table->ACPILevel.SclkDid = (uint8_t)dividers.pll_post_divider;
2378 table->ACPILevel.DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
2379 table->ACPILevel.DeepSleepDivId = 0;
2380
2381 spll_func_cntl = PHM_SET_FIELD(spll_func_cntl, CG_SPLL_FUNC_CNTL,
2382 SPLL_PWRON, 0);
2383 spll_func_cntl = PHM_SET_FIELD(spll_func_cntl, CG_SPLL_FUNC_CNTL,
2384 SPLL_RESET, 1);
2385 spll_func_cntl_2 = PHM_SET_FIELD(spll_func_cntl_2, CG_SPLL_FUNC_CNTL_2,
2386 SCLK_MUX_SEL, 4);
2387
2388 table->ACPILevel.CgSpllFuncCntl = spll_func_cntl;
2389 table->ACPILevel.CgSpllFuncCntl2 = spll_func_cntl_2;
2390 table->ACPILevel.CgSpllFuncCntl3 = data->clock_registers.vCG_SPLL_FUNC_CNTL_3;
2391 table->ACPILevel.CgSpllFuncCntl4 = data->clock_registers.vCG_SPLL_FUNC_CNTL_4;
2392 table->ACPILevel.SpllSpreadSpectrum = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM;
2393 table->ACPILevel.SpllSpreadSpectrum2 = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2;
2394 table->ACPILevel.CcPwrDynRm = 0;
2395 table->ACPILevel.CcPwrDynRm1 = 0;
2396
2397 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.Flags);
2398 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SclkFrequency);
2399 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.MinVoltage);
2400 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl);
2401 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl2);
2402 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl3);
2403 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl4);
2404 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SpllSpreadSpectrum);
2405 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SpllSpreadSpectrum2);
2406 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm);
2407 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm1);
2408
2409 if (!data->mclk_dpm_key_disabled) {
2410 /* Get MinVoltage and Frequency from DPM0, already converted to SMC_UL */
2411 table->MemoryACPILevel.MclkFrequency =
2412 data->dpm_table.mclk_table.dpm_levels[0].value;
2413 result = fiji_get_dependency_volt_by_clk(hwmgr,
2414 table_info->vdd_dep_on_mclk,
2415 table->MemoryACPILevel.MclkFrequency,
2416 &table->MemoryACPILevel.MinVoltage, &mvdd);
2417 PP_ASSERT_WITH_CODE((0 == result),
2418 "Cannot find ACPI VDDCI voltage value "
2419 "in Clock Dependency Table",);
2420 } else {
2421 table->MemoryACPILevel.MclkFrequency =
2422 data->vbios_boot_state.mclk_bootup_value;
2423 table->MemoryACPILevel.MinVoltage =
2424 data->vbios_boot_state.vddci_bootup_value * VOLTAGE_SCALE;
2425 }
2426
2427 us_mvdd = 0;
2428 if ((FIJI_VOLTAGE_CONTROL_NONE == data->mvdd_control) ||
2429 (data->mclk_dpm_key_disabled))
2430 us_mvdd = data->vbios_boot_state.mvdd_bootup_value;
2431 else {
2432 if (!fiji_populate_mvdd_value(hwmgr,
2433 data->dpm_table.mclk_table.dpm_levels[0].value,
2434 &vol_level))
2435 us_mvdd = vol_level.Voltage;
2436 }
2437
2438 table->MemoryACPILevel.MinMvdd =
2439 PP_HOST_TO_SMC_UL(us_mvdd * VOLTAGE_SCALE);
2440
2441 table->MemoryACPILevel.EnabledForThrottle = 0;
2442 table->MemoryACPILevel.EnabledForActivity = 0;
2443 table->MemoryACPILevel.UpHyst = 0;
2444 table->MemoryACPILevel.DownHyst = 100;
2445 table->MemoryACPILevel.VoltageDownHyst = 0;
2446 table->MemoryACPILevel.ActivityLevel =
2447 PP_HOST_TO_SMC_US((uint16_t)data->mclk_activity_target);
2448
2449 table->MemoryACPILevel.StutterEnable = false;
2450 CONVERT_FROM_HOST_TO_SMC_UL(table->MemoryACPILevel.MclkFrequency);
2451 CONVERT_FROM_HOST_TO_SMC_UL(table->MemoryACPILevel.MinVoltage);
2452
2453 return result;
2454}
2455
2456static int fiji_populate_smc_vce_level(struct pp_hwmgr *hwmgr,
2457 SMU73_Discrete_DpmTable *table)
2458{
2459 int result = -EINVAL;
2460 uint8_t count;
2461 struct pp_atomctrl_clock_dividers_vi dividers;
2462 struct phm_ppt_v1_information *table_info =
2463 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2464 struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
2465 table_info->mm_dep_table;
2466 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2467
2468 table->VceLevelCount = (uint8_t)(mm_table->count);
2469 table->VceBootLevel = 0;
2470
2471 for(count = 0; count < table->VceLevelCount; count++) {
2472 table->VceLevel[count].Frequency = mm_table->entries[count].eclk;
Rex Zhu0168f782016-04-04 15:57:10 +08002473 table->VceLevel[count].MinVoltage = 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -04002474 table->VceLevel[count].MinVoltage |=
2475 (mm_table->entries[count].vddc * VOLTAGE_SCALE) << VDDC_SHIFT;
2476 table->VceLevel[count].MinVoltage |=
2477 ((mm_table->entries[count].vddc - data->vddc_vddci_delta) *
2478 VOLTAGE_SCALE) << VDDCI_SHIFT;
2479 table->VceLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
2480
2481 /*retrieve divider value for VBIOS */
2482 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
2483 table->VceLevel[count].Frequency, &dividers);
2484 PP_ASSERT_WITH_CODE((0 == result),
2485 "can not find divide id for VCE engine clock",
2486 return result);
2487
2488 table->VceLevel[count].Divider = (uint8_t)dividers.pll_post_divider;
2489
2490 CONVERT_FROM_HOST_TO_SMC_UL(table->VceLevel[count].Frequency);
2491 CONVERT_FROM_HOST_TO_SMC_UL(table->VceLevel[count].MinVoltage);
2492 }
2493 return result;
2494}
2495
2496static int fiji_populate_smc_acp_level(struct pp_hwmgr *hwmgr,
2497 SMU73_Discrete_DpmTable *table)
2498{
2499 int result = -EINVAL;
2500 uint8_t count;
2501 struct pp_atomctrl_clock_dividers_vi dividers;
2502 struct phm_ppt_v1_information *table_info =
2503 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2504 struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
2505 table_info->mm_dep_table;
2506 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2507
2508 table->AcpLevelCount = (uint8_t)(mm_table->count);
2509 table->AcpBootLevel = 0;
2510
2511 for (count = 0; count < table->AcpLevelCount; count++) {
2512 table->AcpLevel[count].Frequency = mm_table->entries[count].aclk;
2513 table->AcpLevel[count].MinVoltage |= (mm_table->entries[count].vddc *
2514 VOLTAGE_SCALE) << VDDC_SHIFT;
2515 table->AcpLevel[count].MinVoltage |= ((mm_table->entries[count].vddc -
2516 data->vddc_vddci_delta) * VOLTAGE_SCALE) << VDDCI_SHIFT;
2517 table->AcpLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
2518
2519 /* retrieve divider value for VBIOS */
2520 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
2521 table->AcpLevel[count].Frequency, &dividers);
2522 PP_ASSERT_WITH_CODE((0 == result),
2523 "can not find divide id for engine clock", return result);
2524
2525 table->AcpLevel[count].Divider = (uint8_t)dividers.pll_post_divider;
2526
2527 CONVERT_FROM_HOST_TO_SMC_UL(table->AcpLevel[count].Frequency);
2528 CONVERT_FROM_HOST_TO_SMC_UL(table->AcpLevel[count].MinVoltage);
2529 }
2530 return result;
2531}
2532
2533static int fiji_populate_smc_samu_level(struct pp_hwmgr *hwmgr,
2534 SMU73_Discrete_DpmTable *table)
2535{
2536 int result = -EINVAL;
2537 uint8_t count;
2538 struct pp_atomctrl_clock_dividers_vi dividers;
2539 struct phm_ppt_v1_information *table_info =
2540 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2541 struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
2542 table_info->mm_dep_table;
2543 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2544
2545 table->SamuBootLevel = 0;
2546 table->SamuLevelCount = (uint8_t)(mm_table->count);
2547
2548 for (count = 0; count < table->SamuLevelCount; count++) {
2549 /* not sure whether we need evclk or not */
Rex Zhu0168f782016-04-04 15:57:10 +08002550 table->SamuLevel[count].MinVoltage = 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -04002551 table->SamuLevel[count].Frequency = mm_table->entries[count].samclock;
2552 table->SamuLevel[count].MinVoltage |= (mm_table->entries[count].vddc *
2553 VOLTAGE_SCALE) << VDDC_SHIFT;
2554 table->SamuLevel[count].MinVoltage |= ((mm_table->entries[count].vddc -
2555 data->vddc_vddci_delta) * VOLTAGE_SCALE) << VDDCI_SHIFT;
2556 table->SamuLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
2557
2558 /* retrieve divider value for VBIOS */
2559 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
2560 table->SamuLevel[count].Frequency, &dividers);
2561 PP_ASSERT_WITH_CODE((0 == result),
2562 "can not find divide id for samu clock", return result);
2563
2564 table->SamuLevel[count].Divider = (uint8_t)dividers.pll_post_divider;
2565
2566 CONVERT_FROM_HOST_TO_SMC_UL(table->SamuLevel[count].Frequency);
2567 CONVERT_FROM_HOST_TO_SMC_UL(table->SamuLevel[count].MinVoltage);
2568 }
2569 return result;
2570}
2571
2572static int fiji_populate_memory_timing_parameters(struct pp_hwmgr *hwmgr,
2573 int32_t eng_clock, int32_t mem_clock,
2574 struct SMU73_Discrete_MCArbDramTimingTableEntry *arb_regs)
2575{
2576 uint32_t dram_timing;
2577 uint32_t dram_timing2;
2578 uint32_t burstTime;
2579 ULONG state, trrds, trrdl;
2580 int result;
2581
2582 result = atomctrl_set_engine_dram_timings_rv770(hwmgr,
2583 eng_clock, mem_clock);
2584 PP_ASSERT_WITH_CODE(result == 0,
2585 "Error calling VBIOS to set DRAM_TIMING.", return result);
2586
2587 dram_timing = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING);
2588 dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2);
2589 burstTime = cgs_read_register(hwmgr->device, mmMC_ARB_BURST_TIME);
2590
2591 state = PHM_GET_FIELD(burstTime, MC_ARB_BURST_TIME, STATE0);
2592 trrds = PHM_GET_FIELD(burstTime, MC_ARB_BURST_TIME, TRRDS0);
2593 trrdl = PHM_GET_FIELD(burstTime, MC_ARB_BURST_TIME, TRRDL0);
2594
2595 arb_regs->McArbDramTiming = PP_HOST_TO_SMC_UL(dram_timing);
2596 arb_regs->McArbDramTiming2 = PP_HOST_TO_SMC_UL(dram_timing2);
2597 arb_regs->McArbBurstTime = (uint8_t)burstTime;
2598 arb_regs->TRRDS = (uint8_t)trrds;
2599 arb_regs->TRRDL = (uint8_t)trrdl;
2600
2601 return 0;
2602}
2603
2604static int fiji_program_memory_timing_parameters(struct pp_hwmgr *hwmgr)
2605{
2606 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2607 struct SMU73_Discrete_MCArbDramTimingTable arb_regs;
2608 uint32_t i, j;
2609 int result = 0;
2610
2611 for (i = 0; i < data->dpm_table.sclk_table.count; i++) {
2612 for (j = 0; j < data->dpm_table.mclk_table.count; j++) {
2613 result = fiji_populate_memory_timing_parameters(hwmgr,
2614 data->dpm_table.sclk_table.dpm_levels[i].value,
2615 data->dpm_table.mclk_table.dpm_levels[j].value,
2616 &arb_regs.entries[i][j]);
2617 if (result)
2618 break;
2619 }
2620 }
2621
2622 if (!result)
2623 result = fiji_copy_bytes_to_smc(
2624 hwmgr->smumgr,
2625 data->arb_table_start,
2626 (uint8_t *)&arb_regs,
2627 sizeof(SMU73_Discrete_MCArbDramTimingTable),
2628 data->sram_end);
2629 return result;
2630}
2631
2632static int fiji_populate_smc_uvd_level(struct pp_hwmgr *hwmgr,
2633 struct SMU73_Discrete_DpmTable *table)
2634{
2635 int result = -EINVAL;
2636 uint8_t count;
2637 struct pp_atomctrl_clock_dividers_vi dividers;
2638 struct phm_ppt_v1_information *table_info =
2639 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2640 struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
2641 table_info->mm_dep_table;
2642 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2643
2644 table->UvdLevelCount = (uint8_t)(mm_table->count);
2645 table->UvdBootLevel = 0;
2646
2647 for (count = 0; count < table->UvdLevelCount; count++) {
Rex Zhu0168f782016-04-04 15:57:10 +08002648 table->UvdLevel[count].MinVoltage = 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -04002649 table->UvdLevel[count].VclkFrequency = mm_table->entries[count].vclk;
2650 table->UvdLevel[count].DclkFrequency = mm_table->entries[count].dclk;
2651 table->UvdLevel[count].MinVoltage |= (mm_table->entries[count].vddc *
2652 VOLTAGE_SCALE) << VDDC_SHIFT;
2653 table->UvdLevel[count].MinVoltage |= ((mm_table->entries[count].vddc -
2654 data->vddc_vddci_delta) * VOLTAGE_SCALE) << VDDCI_SHIFT;
2655 table->UvdLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
2656
2657 /* retrieve divider value for VBIOS */
2658 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
2659 table->UvdLevel[count].VclkFrequency, &dividers);
2660 PP_ASSERT_WITH_CODE((0 == result),
2661 "can not find divide id for Vclk clock", return result);
2662
2663 table->UvdLevel[count].VclkDivider = (uint8_t)dividers.pll_post_divider;
2664
2665 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
2666 table->UvdLevel[count].DclkFrequency, &dividers);
2667 PP_ASSERT_WITH_CODE((0 == result),
2668 "can not find divide id for Dclk clock", return result);
2669
2670 table->UvdLevel[count].DclkDivider = (uint8_t)dividers.pll_post_divider;
2671
2672 CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].VclkFrequency);
2673 CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].DclkFrequency);
2674 CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].MinVoltage);
2675
2676 }
2677 return result;
2678}
2679
2680static int fiji_find_boot_level(struct fiji_single_dpm_table *table,
2681 uint32_t value, uint32_t *boot_level)
2682{
2683 int result = -EINVAL;
2684 uint32_t i;
2685
2686 for (i = 0; i < table->count; i++) {
2687 if (value == table->dpm_levels[i].value) {
2688 *boot_level = i;
2689 result = 0;
2690 }
2691 }
2692 return result;
2693}
2694
2695static int fiji_populate_smc_boot_level(struct pp_hwmgr *hwmgr,
2696 struct SMU73_Discrete_DpmTable *table)
2697{
2698 int result = 0;
2699 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2700
2701 table->GraphicsBootLevel = 0;
2702 table->MemoryBootLevel = 0;
2703
2704 /* find boot level from dpm table */
2705 result = fiji_find_boot_level(&(data->dpm_table.sclk_table),
2706 data->vbios_boot_state.sclk_bootup_value,
2707 (uint32_t *)&(table->GraphicsBootLevel));
2708
2709 result = fiji_find_boot_level(&(data->dpm_table.mclk_table),
2710 data->vbios_boot_state.mclk_bootup_value,
2711 (uint32_t *)&(table->MemoryBootLevel));
2712
2713 table->BootVddc = data->vbios_boot_state.vddc_bootup_value *
2714 VOLTAGE_SCALE;
2715 table->BootVddci = data->vbios_boot_state.vddci_bootup_value *
2716 VOLTAGE_SCALE;
2717 table->BootMVdd = data->vbios_boot_state.mvdd_bootup_value *
2718 VOLTAGE_SCALE;
2719
2720 CONVERT_FROM_HOST_TO_SMC_US(table->BootVddc);
2721 CONVERT_FROM_HOST_TO_SMC_US(table->BootVddci);
2722 CONVERT_FROM_HOST_TO_SMC_US(table->BootMVdd);
2723
2724 return 0;
2725}
2726
2727static int fiji_populate_smc_initailial_state(struct pp_hwmgr *hwmgr)
2728{
2729 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2730 struct phm_ppt_v1_information *table_info =
2731 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2732 uint8_t count, level;
2733
2734 count = (uint8_t)(table_info->vdd_dep_on_sclk->count);
2735 for (level = 0; level < count; level++) {
2736 if(table_info->vdd_dep_on_sclk->entries[level].clk >=
2737 data->vbios_boot_state.sclk_bootup_value) {
2738 data->smc_state_table.GraphicsBootLevel = level;
2739 break;
2740 }
2741 }
2742
2743 count = (uint8_t)(table_info->vdd_dep_on_mclk->count);
2744 for (level = 0; level < count; level++) {
2745 if(table_info->vdd_dep_on_mclk->entries[level].clk >=
2746 data->vbios_boot_state.mclk_bootup_value) {
2747 data->smc_state_table.MemoryBootLevel = level;
2748 break;
2749 }
2750 }
2751
2752 return 0;
2753}
2754
2755static int fiji_populate_clock_stretcher_data_table(struct pp_hwmgr *hwmgr)
2756{
2757 uint32_t ro, efuse, efuse2, clock_freq, volt_without_cks,
2758 volt_with_cks, value;
2759 uint16_t clock_freq_u16;
2760 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2761 uint8_t type, i, j, cks_setting, stretch_amount, stretch_amount2,
2762 volt_offset = 0;
2763 struct phm_ppt_v1_information *table_info =
2764 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2765 struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
2766 table_info->vdd_dep_on_sclk;
2767
2768 stretch_amount = (uint8_t)table_info->cac_dtp_table->usClockStretchAmount;
2769
2770 /* Read SMU_Eefuse to read and calculate RO and determine
2771 * if the part is SS or FF. if RO >= 1660MHz, part is FF.
2772 */
2773 efuse = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
2774 ixSMU_EFUSE_0 + (146 * 4));
2775 efuse2 = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
2776 ixSMU_EFUSE_0 + (148 * 4));
2777 efuse &= 0xFF000000;
2778 efuse = efuse >> 24;
2779 efuse2 &= 0xF;
2780
2781 if (efuse2 == 1)
2782 ro = (2300 - 1350) * efuse / 255 + 1350;
2783 else
2784 ro = (2500 - 1000) * efuse / 255 + 1000;
2785
2786 if (ro >= 1660)
2787 type = 0;
2788 else
2789 type = 1;
2790
2791 /* Populate Stretch amount */
2792 data->smc_state_table.ClockStretcherAmount = stretch_amount;
2793
2794 /* Populate Sclk_CKS_masterEn0_7 and Sclk_voltageOffset */
2795 for (i = 0; i < sclk_table->count; i++) {
2796 data->smc_state_table.Sclk_CKS_masterEn0_7 |=
2797 sclk_table->entries[i].cks_enable << i;
2798 volt_without_cks = (uint32_t)((14041 *
2799 (sclk_table->entries[i].clk/100) / 10000 + 3571 + 75 - ro) * 1000 /
2800 (4026 - (13924 * (sclk_table->entries[i].clk/100) / 10000)));
2801 volt_with_cks = (uint32_t)((13946 *
2802 (sclk_table->entries[i].clk/100) / 10000 + 3320 + 45 - ro) * 1000 /
2803 (3664 - (11454 * (sclk_table->entries[i].clk/100) / 10000)));
2804 if (volt_without_cks >= volt_with_cks)
2805 volt_offset = (uint8_t)(((volt_without_cks - volt_with_cks +
2806 sclk_table->entries[i].cks_voffset) * 100 / 625) + 1);
2807 data->smc_state_table.Sclk_voltageOffset[i] = volt_offset;
2808 }
2809
2810 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, PWR_CKS_ENABLE,
2811 STRETCH_ENABLE, 0x0);
2812 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, PWR_CKS_ENABLE,
2813 masterReset, 0x1);
2814 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, PWR_CKS_ENABLE,
2815 staticEnable, 0x1);
2816 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, PWR_CKS_ENABLE,
2817 masterReset, 0x0);
2818
2819 /* Populate CKS Lookup Table */
2820 if (stretch_amount == 1 || stretch_amount == 2 || stretch_amount == 5)
2821 stretch_amount2 = 0;
2822 else if (stretch_amount == 3 || stretch_amount == 4)
2823 stretch_amount2 = 1;
2824 else {
2825 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2826 PHM_PlatformCaps_ClockStretcher);
2827 PP_ASSERT_WITH_CODE(false,
2828 "Stretch Amount in PPTable not supported\n",
2829 return -EINVAL);
2830 }
2831
2832 value = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
2833 ixPWR_CKS_CNTL);
2834 value &= 0xFFC2FF87;
2835 data->smc_state_table.CKS_LOOKUPTable.CKS_LOOKUPTableEntry[0].minFreq =
2836 fiji_clock_stretcher_lookup_table[stretch_amount2][0];
2837 data->smc_state_table.CKS_LOOKUPTable.CKS_LOOKUPTableEntry[0].maxFreq =
2838 fiji_clock_stretcher_lookup_table[stretch_amount2][1];
2839 clock_freq_u16 = (uint16_t)(PP_SMC_TO_HOST_UL(data->smc_state_table.
2840 GraphicsLevel[data->smc_state_table.GraphicsDpmLevelCount - 1].
2841 SclkFrequency) / 100);
2842 if (fiji_clock_stretcher_lookup_table[stretch_amount2][0] <
2843 clock_freq_u16 &&
Rex Zhu75ac63d2016-01-06 16:38:48 +08002844 fiji_clock_stretcher_lookup_table[stretch_amount2][1] >
Eric Huangaabcb7c2015-08-26 16:52:28 -04002845 clock_freq_u16) {
2846 /* Program PWR_CKS_CNTL. CKS_USE_FOR_LOW_FREQ */
2847 value |= (fiji_clock_stretcher_lookup_table[stretch_amount2][3]) << 16;
2848 /* Program PWR_CKS_CNTL. CKS_LDO_REFSEL */
2849 value |= (fiji_clock_stretcher_lookup_table[stretch_amount2][2]) << 18;
2850 /* Program PWR_CKS_CNTL. CKS_STRETCH_AMOUNT */
2851 value |= (fiji_clock_stretch_amount_conversion
2852 [fiji_clock_stretcher_lookup_table[stretch_amount2][3]]
2853 [stretch_amount]) << 3;
2854 }
2855 CONVERT_FROM_HOST_TO_SMC_US(data->smc_state_table.CKS_LOOKUPTable.
2856 CKS_LOOKUPTableEntry[0].minFreq);
2857 CONVERT_FROM_HOST_TO_SMC_US(data->smc_state_table.CKS_LOOKUPTable.
2858 CKS_LOOKUPTableEntry[0].maxFreq);
2859 data->smc_state_table.CKS_LOOKUPTable.CKS_LOOKUPTableEntry[0].setting =
2860 fiji_clock_stretcher_lookup_table[stretch_amount2][2] & 0x7F;
2861 data->smc_state_table.CKS_LOOKUPTable.CKS_LOOKUPTableEntry[0].setting |=
2862 (fiji_clock_stretcher_lookup_table[stretch_amount2][3]) << 7;
2863
2864 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
2865 ixPWR_CKS_CNTL, value);
2866
2867 /* Populate DDT Lookup Table */
2868 for (i = 0; i < 4; i++) {
2869 /* Assign the minimum and maximum VID stored
2870 * in the last row of Clock Stretcher Voltage Table.
2871 */
2872 data->smc_state_table.ClockStretcherDataTable.
2873 ClockStretcherDataTableEntry[i].minVID =
2874 (uint8_t) fiji_clock_stretcher_ddt_table[type][i][2];
2875 data->smc_state_table.ClockStretcherDataTable.
2876 ClockStretcherDataTableEntry[i].maxVID =
2877 (uint8_t) fiji_clock_stretcher_ddt_table[type][i][3];
2878 /* Loop through each SCLK and check the frequency
2879 * to see if it lies within the frequency for clock stretcher.
2880 */
2881 for (j = 0; j < data->smc_state_table.GraphicsDpmLevelCount; j++) {
2882 cks_setting = 0;
2883 clock_freq = PP_SMC_TO_HOST_UL(
2884 data->smc_state_table.GraphicsLevel[j].SclkFrequency);
2885 /* Check the allowed frequency against the sclk level[j].
2886 * Sclk's endianness has already been converted,
2887 * and it's in 10Khz unit,
2888 * as opposed to Data table, which is in Mhz unit.
2889 */
2890 if (clock_freq >=
2891 (fiji_clock_stretcher_ddt_table[type][i][0]) * 100) {
2892 cks_setting |= 0x2;
2893 if (clock_freq <
2894 (fiji_clock_stretcher_ddt_table[type][i][1]) * 100)
2895 cks_setting |= 0x1;
2896 }
2897 data->smc_state_table.ClockStretcherDataTable.
2898 ClockStretcherDataTableEntry[i].setting |= cks_setting << (j * 2);
2899 }
2900 CONVERT_FROM_HOST_TO_SMC_US(data->smc_state_table.
2901 ClockStretcherDataTable.
2902 ClockStretcherDataTableEntry[i].setting);
2903 }
2904
2905 value = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixPWR_CKS_CNTL);
2906 value &= 0xFFFFFFFE;
2907 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixPWR_CKS_CNTL, value);
2908
2909 return 0;
2910}
2911
2912/**
2913* Populates the SMC VRConfig field in DPM table.
2914*
2915* @param hwmgr the address of the hardware manager
2916* @param table the SMC DPM table structure to be populated
2917* @return always 0
2918*/
2919static int fiji_populate_vr_config(struct pp_hwmgr *hwmgr,
2920 struct SMU73_Discrete_DpmTable *table)
2921{
2922 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2923 uint16_t config;
2924
2925 config = VR_MERGED_WITH_VDDC;
2926 table->VRConfig |= (config << VRCONF_VDDGFX_SHIFT);
2927
2928 /* Set Vddc Voltage Controller */
2929 if(FIJI_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control) {
2930 config = VR_SVI2_PLANE_1;
2931 table->VRConfig |= config;
2932 } else {
2933 PP_ASSERT_WITH_CODE(false,
2934 "VDDC should be on SVI2 control in merged mode!",);
2935 }
2936 /* Set Vddci Voltage Controller */
2937 if(FIJI_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control) {
2938 config = VR_SVI2_PLANE_2; /* only in merged mode */
2939 table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
2940 } else if (FIJI_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control) {
2941 config = VR_SMIO_PATTERN_1;
2942 table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
2943 } else {
2944 config = VR_STATIC_VOLTAGE;
2945 table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
2946 }
2947 /* Set Mvdd Voltage Controller */
2948 if(FIJI_VOLTAGE_CONTROL_BY_SVID2 == data->mvdd_control) {
2949 config = VR_SVI2_PLANE_2;
2950 table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
2951 } else if(FIJI_VOLTAGE_CONTROL_BY_GPIO == data->mvdd_control) {
2952 config = VR_SMIO_PATTERN_2;
2953 table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
2954 } else {
2955 config = VR_STATIC_VOLTAGE;
2956 table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
2957 }
2958
2959 return 0;
2960}
2961
2962/**
2963* Initializes the SMC table and uploads it
2964*
2965* @param hwmgr the address of the powerplay hardware manager.
2966* @param pInput the pointer to input data (PowerState)
2967* @return always 0
2968*/
2969static int fiji_init_smc_table(struct pp_hwmgr *hwmgr)
2970{
2971 int result;
2972 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
2973 struct phm_ppt_v1_information *table_info =
2974 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2975 struct SMU73_Discrete_DpmTable *table = &(data->smc_state_table);
2976 const struct fiji_ulv_parm *ulv = &(data->ulv);
2977 uint8_t i;
2978 struct pp_atomctrl_gpio_pin_assignment gpio_pin;
2979
2980 result = fiji_setup_default_dpm_tables(hwmgr);
2981 PP_ASSERT_WITH_CODE(0 == result,
2982 "Failed to setup default DPM tables!", return result);
2983
2984 if(FIJI_VOLTAGE_CONTROL_NONE != data->voltage_control)
2985 fiji_populate_smc_voltage_tables(hwmgr, table);
2986
Rex Zhu0168f782016-04-04 15:57:10 +08002987 table->SystemFlags = 0;
2988
Eric Huangaabcb7c2015-08-26 16:52:28 -04002989 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2990 PHM_PlatformCaps_AutomaticDCTransition))
2991 table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
2992
2993 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2994 PHM_PlatformCaps_StepVddc))
2995 table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
2996
2997 if (data->is_memory_gddr5)
2998 table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
2999
3000 if (ulv->ulv_supported && table_info->us_ulv_voltage_offset) {
3001 result = fiji_populate_ulv_state(hwmgr, table);
3002 PP_ASSERT_WITH_CODE(0 == result,
3003 "Failed to initialize ULV state!", return result);
3004 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3005 ixCG_ULV_PARAMETER, ulv->cg_ulv_parameter);
3006 }
3007
3008 result = fiji_populate_smc_link_level(hwmgr, table);
3009 PP_ASSERT_WITH_CODE(0 == result,
3010 "Failed to initialize Link Level!", return result);
3011
3012 result = fiji_populate_all_graphic_levels(hwmgr);
3013 PP_ASSERT_WITH_CODE(0 == result,
3014 "Failed to initialize Graphics Level!", return result);
3015
3016 result = fiji_populate_all_memory_levels(hwmgr);
3017 PP_ASSERT_WITH_CODE(0 == result,
3018 "Failed to initialize Memory Level!", return result);
3019
3020 result = fiji_populate_smc_acpi_level(hwmgr, table);
3021 PP_ASSERT_WITH_CODE(0 == result,
3022 "Failed to initialize ACPI Level!", return result);
3023
3024 result = fiji_populate_smc_vce_level(hwmgr, table);
3025 PP_ASSERT_WITH_CODE(0 == result,
3026 "Failed to initialize VCE Level!", return result);
3027
3028 result = fiji_populate_smc_acp_level(hwmgr, table);
3029 PP_ASSERT_WITH_CODE(0 == result,
3030 "Failed to initialize ACP Level!", return result);
3031
3032 result = fiji_populate_smc_samu_level(hwmgr, table);
3033 PP_ASSERT_WITH_CODE(0 == result,
3034 "Failed to initialize SAMU Level!", return result);
3035
3036 /* Since only the initial state is completely set up at this point
3037 * (the other states are just copies of the boot state) we only
3038 * need to populate the ARB settings for the initial state.
3039 */
3040 result = fiji_program_memory_timing_parameters(hwmgr);
3041 PP_ASSERT_WITH_CODE(0 == result,
3042 "Failed to Write ARB settings for the initial state.", return result);
3043
3044 result = fiji_populate_smc_uvd_level(hwmgr, table);
3045 PP_ASSERT_WITH_CODE(0 == result,
3046 "Failed to initialize UVD Level!", return result);
3047
3048 result = fiji_populate_smc_boot_level(hwmgr, table);
3049 PP_ASSERT_WITH_CODE(0 == result,
3050 "Failed to initialize Boot Level!", return result);
3051
3052 result = fiji_populate_smc_initailial_state(hwmgr);
3053 PP_ASSERT_WITH_CODE(0 == result,
3054 "Failed to initialize Boot State!", return result);
3055
3056 result = fiji_populate_bapm_parameters_in_dpm_table(hwmgr);
3057 PP_ASSERT_WITH_CODE(0 == result,
3058 "Failed to populate BAPM Parameters!", return result);
3059
3060 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3061 PHM_PlatformCaps_ClockStretcher)) {
3062 result = fiji_populate_clock_stretcher_data_table(hwmgr);
3063 PP_ASSERT_WITH_CODE(0 == result,
3064 "Failed to populate Clock Stretcher Data Table!",
3065 return result);
3066 }
3067
3068 table->GraphicsVoltageChangeEnable = 1;
3069 table->GraphicsThermThrottleEnable = 1;
3070 table->GraphicsInterval = 1;
3071 table->VoltageInterval = 1;
3072 table->ThermalInterval = 1;
3073 table->TemperatureLimitHigh =
3074 table_info->cac_dtp_table->usTargetOperatingTemp *
3075 FIJI_Q88_FORMAT_CONVERSION_UNIT;
3076 table->TemperatureLimitLow =
3077 (table_info->cac_dtp_table->usTargetOperatingTemp - 1) *
3078 FIJI_Q88_FORMAT_CONVERSION_UNIT;
3079 table->MemoryVoltageChangeEnable = 1;
3080 table->MemoryInterval = 1;
3081 table->VoltageResponseTime = 0;
3082 table->PhaseResponseTime = 0;
3083 table->MemoryThermThrottleEnable = 1;
3084 table->PCIeBootLinkLevel = 0; /* 0:Gen1 1:Gen2 2:Gen3*/
3085 table->PCIeGenInterval = 1;
Rex Zhu0168f782016-04-04 15:57:10 +08003086 table->VRConfig = 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -04003087
3088 result = fiji_populate_vr_config(hwmgr, table);
3089 PP_ASSERT_WITH_CODE(0 == result,
3090 "Failed to populate VRConfig setting!", return result);
3091
3092 table->ThermGpio = 17;
3093 table->SclkStepSize = 0x4000;
3094
3095 if (atomctrl_get_pp_assign_pin(hwmgr, VDDC_VRHOT_GPIO_PINID, &gpio_pin)) {
3096 table->VRHotGpio = gpio_pin.uc_gpio_pin_bit_shift;
3097 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
3098 PHM_PlatformCaps_RegulatorHot);
3099 } else {
3100 table->VRHotGpio = FIJI_UNUSED_GPIO_PIN;
3101 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
3102 PHM_PlatformCaps_RegulatorHot);
3103 }
3104
3105 if (atomctrl_get_pp_assign_pin(hwmgr, PP_AC_DC_SWITCH_GPIO_PINID,
3106 &gpio_pin)) {
3107 table->AcDcGpio = gpio_pin.uc_gpio_pin_bit_shift;
3108 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
3109 PHM_PlatformCaps_AutomaticDCTransition);
3110 } else {
3111 table->AcDcGpio = FIJI_UNUSED_GPIO_PIN;
3112 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
3113 PHM_PlatformCaps_AutomaticDCTransition);
3114 }
3115
3116 /* Thermal Output GPIO */
3117 if (atomctrl_get_pp_assign_pin(hwmgr, THERMAL_INT_OUTPUT_GPIO_PINID,
3118 &gpio_pin)) {
3119 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
3120 PHM_PlatformCaps_ThermalOutGPIO);
3121
3122 table->ThermOutGpio = gpio_pin.uc_gpio_pin_bit_shift;
3123
3124 /* For porlarity read GPIOPAD_A with assigned Gpio pin
3125 * since VBIOS will program this register to set 'inactive state',
3126 * driver can then determine 'active state' from this and
3127 * program SMU with correct polarity
3128 */
3129 table->ThermOutPolarity = (0 == (cgs_read_register(hwmgr->device, mmGPIOPAD_A) &
3130 (1 << gpio_pin.uc_gpio_pin_bit_shift))) ? 1:0;
3131 table->ThermOutMode = SMU7_THERM_OUT_MODE_THERM_ONLY;
3132
3133 /* if required, combine VRHot/PCC with thermal out GPIO */
3134 if(phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3135 PHM_PlatformCaps_RegulatorHot) &&
3136 phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3137 PHM_PlatformCaps_CombinePCCWithThermalSignal))
3138 table->ThermOutMode = SMU7_THERM_OUT_MODE_THERM_VRHOT;
3139 } else {
3140 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
3141 PHM_PlatformCaps_ThermalOutGPIO);
3142 table->ThermOutGpio = 17;
3143 table->ThermOutPolarity = 1;
3144 table->ThermOutMode = SMU7_THERM_OUT_MODE_DISABLE;
3145 }
3146
3147 for (i = 0; i < SMU73_MAX_ENTRIES_SMIO; i++)
3148 table->Smio[i] = PP_HOST_TO_SMC_UL(table->Smio[i]);
3149
3150 CONVERT_FROM_HOST_TO_SMC_UL(table->SystemFlags);
3151 CONVERT_FROM_HOST_TO_SMC_UL(table->VRConfig);
3152 CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMask1);
3153 CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMask2);
3154 CONVERT_FROM_HOST_TO_SMC_UL(table->SclkStepSize);
3155 CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitHigh);
3156 CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitLow);
3157 CONVERT_FROM_HOST_TO_SMC_US(table->VoltageResponseTime);
3158 CONVERT_FROM_HOST_TO_SMC_US(table->PhaseResponseTime);
3159
3160 /* Upload all dpm data to SMC memory.(dpm level, dpm level count etc) */
3161 result = fiji_copy_bytes_to_smc(hwmgr->smumgr,
3162 data->dpm_table_start +
3163 offsetof(SMU73_Discrete_DpmTable, SystemFlags),
3164 (uint8_t *)&(table->SystemFlags),
3165 sizeof(SMU73_Discrete_DpmTable) - 3 * sizeof(SMU73_PIDController),
3166 data->sram_end);
3167 PP_ASSERT_WITH_CODE(0 == result,
3168 "Failed to upload dpm data to SMC memory!", return result);
3169
3170 return 0;
3171}
3172
3173/**
3174* Initialize the ARB DRAM timing table's index field.
3175*
3176* @param hwmgr the address of the powerplay hardware manager.
3177* @return always 0
3178*/
3179static int fiji_init_arb_table_index(struct pp_hwmgr *hwmgr)
3180{
3181 const struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3182 uint32_t tmp;
3183 int result;
3184
3185 /* This is a read-modify-write on the first byte of the ARB table.
3186 * The first byte in the SMU73_Discrete_MCArbDramTimingTable structure
3187 * is the field 'current'.
3188 * This solution is ugly, but we never write the whole table only
3189 * individual fields in it.
3190 * In reality this field should not be in that structure
3191 * but in a soft register.
3192 */
3193 result = fiji_read_smc_sram_dword(hwmgr->smumgr,
3194 data->arb_table_start, &tmp, data->sram_end);
3195
3196 if (result)
3197 return result;
3198
3199 tmp &= 0x00FFFFFF;
3200 tmp |= ((uint32_t)MC_CG_ARB_FREQ_F1) << 24;
3201
3202 return fiji_write_smc_sram_dword(hwmgr->smumgr,
3203 data->arb_table_start, tmp, data->sram_end);
3204}
3205
3206static int fiji_enable_vrhot_gpio_interrupt(struct pp_hwmgr *hwmgr)
3207{
3208 if(phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3209 PHM_PlatformCaps_RegulatorHot))
3210 return smum_send_msg_to_smc(hwmgr->smumgr,
3211 PPSMC_MSG_EnableVRHotGPIOInterrupt);
3212
3213 return 0;
3214}
3215
3216static int fiji_enable_sclk_control(struct pp_hwmgr *hwmgr)
3217{
3218 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SCLK_PWRMGT_CNTL,
3219 SCLK_PWRMGT_OFF, 0);
3220 return 0;
3221}
3222
3223static int fiji_enable_ulv(struct pp_hwmgr *hwmgr)
3224{
3225 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3226 struct fiji_ulv_parm *ulv = &(data->ulv);
3227
3228 if (ulv->ulv_supported)
3229 return smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_EnableULV);
3230
3231 return 0;
3232}
3233
Eric Huang55d74fc2016-05-31 17:06:14 -04003234static int fiji_disable_ulv(struct pp_hwmgr *hwmgr)
3235{
3236 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3237 struct fiji_ulv_parm *ulv = &(data->ulv);
3238
3239 if (ulv->ulv_supported)
3240 return smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_DisableULV);
3241
3242 return 0;
3243}
3244
Eric Huangaabcb7c2015-08-26 16:52:28 -04003245static int fiji_enable_deep_sleep_master_switch(struct pp_hwmgr *hwmgr)
3246{
3247 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3248 PHM_PlatformCaps_SclkDeepSleep)) {
3249 if (smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_MASTER_DeepSleep_ON))
3250 PP_ASSERT_WITH_CODE(false,
3251 "Attempt to enable Master Deep Sleep switch failed!",
3252 return -1);
3253 } else {
3254 if (smum_send_msg_to_smc(hwmgr->smumgr,
3255 PPSMC_MSG_MASTER_DeepSleep_OFF)) {
3256 PP_ASSERT_WITH_CODE(false,
3257 "Attempt to disable Master Deep Sleep switch failed!",
3258 return -1);
3259 }
3260 }
3261
3262 return 0;
3263}
3264
Eric Huang55d74fc2016-05-31 17:06:14 -04003265static int fiji_disable_deep_sleep_master_switch(struct pp_hwmgr *hwmgr)
3266{
3267 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3268 PHM_PlatformCaps_SclkDeepSleep)) {
3269 if (smum_send_msg_to_smc(hwmgr->smumgr,
3270 PPSMC_MSG_MASTER_DeepSleep_OFF)) {
3271 PP_ASSERT_WITH_CODE(false,
3272 "Attempt to disable Master Deep Sleep switch failed!",
3273 return -1);
3274 }
3275 }
3276
3277 return 0;
3278}
3279
Eric Huangaabcb7c2015-08-26 16:52:28 -04003280static int fiji_enable_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
3281{
3282 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3283 uint32_t val, val0, val2;
3284 uint32_t i, cpl_cntl, cpl_threshold, mc_threshold;
3285
3286 /* enable SCLK dpm */
3287 if(!data->sclk_dpm_key_disabled)
3288 PP_ASSERT_WITH_CODE(
Rex Zhu75ac63d2016-01-06 16:38:48 +08003289 (0 == smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_DPM_Enable)),
3290 "Failed to enable SCLK DPM during DPM Start Function!",
3291 return -1);
Eric Huangaabcb7c2015-08-26 16:52:28 -04003292
3293 /* enable MCLK dpm */
3294 if(0 == data->mclk_dpm_key_disabled) {
3295 cpl_threshold = 0;
3296 mc_threshold = 0;
3297
3298 /* Read per MCD tile (0 - 7) */
3299 for (i = 0; i < 8; i++) {
3300 PHM_WRITE_FIELD(hwmgr->device, MC_CONFIG_MCD, MC_RD_ENABLE, i);
3301 val = cgs_read_register(hwmgr->device, mmMC_SEQ_RESERVE_0_S) & 0xf0000000;
3302 if (0xf0000000 != val) {
3303 /* count number of MCQ that has channel(s) enabled */
3304 cpl_threshold++;
3305 /* only harvest 3 or full 4 supported */
3306 mc_threshold = val ? 3 : 4;
3307 }
3308 }
3309 PP_ASSERT_WITH_CODE(0 != cpl_threshold,
3310 "Number of MCQ is zero!", return -EINVAL;);
3311
3312 mc_threshold = ((mc_threshold & LCAC_MC0_CNTL__MC0_THRESHOLD_MASK) <<
3313 LCAC_MC0_CNTL__MC0_THRESHOLD__SHIFT) |
3314 LCAC_MC0_CNTL__MC0_ENABLE_MASK;
3315 cpl_cntl = ((cpl_threshold & LCAC_CPL_CNTL__CPL_THRESHOLD_MASK) <<
3316 LCAC_CPL_CNTL__CPL_THRESHOLD__SHIFT) |
3317 LCAC_CPL_CNTL__CPL_ENABLE_MASK;
3318 cpl_cntl = (cpl_cntl | (8 << LCAC_CPL_CNTL__CPL_BLOCK_ID__SHIFT));
3319 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3320 ixLCAC_MC0_CNTL, mc_threshold);
3321 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3322 ixLCAC_MC1_CNTL, mc_threshold);
3323 if (8 == cpl_threshold) {
3324 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3325 ixLCAC_MC2_CNTL, mc_threshold);
3326 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3327 ixLCAC_MC3_CNTL, mc_threshold);
3328 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3329 ixLCAC_MC4_CNTL, mc_threshold);
3330 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3331 ixLCAC_MC5_CNTL, mc_threshold);
3332 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3333 ixLCAC_MC6_CNTL, mc_threshold);
3334 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3335 ixLCAC_MC7_CNTL, mc_threshold);
3336 }
3337 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3338 ixLCAC_CPL_CNTL, cpl_cntl);
3339
3340 udelay(5);
3341
3342 mc_threshold = mc_threshold |
3343 (1 << LCAC_MC0_CNTL__MC0_SIGNAL_ID__SHIFT);
3344 cpl_cntl = cpl_cntl | (1 << LCAC_CPL_CNTL__CPL_SIGNAL_ID__SHIFT);
3345 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3346 ixLCAC_MC0_CNTL, mc_threshold);
3347 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3348 ixLCAC_MC1_CNTL, mc_threshold);
3349 if (8 == cpl_threshold) {
3350 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3351 ixLCAC_MC2_CNTL, mc_threshold);
3352 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3353 ixLCAC_MC3_CNTL, mc_threshold);
3354 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3355 ixLCAC_MC4_CNTL, mc_threshold);
3356 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3357 ixLCAC_MC5_CNTL, mc_threshold);
3358 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3359 ixLCAC_MC6_CNTL, mc_threshold);
3360 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3361 ixLCAC_MC7_CNTL, mc_threshold);
3362 }
3363 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3364 ixLCAC_CPL_CNTL, cpl_cntl);
3365
3366 /* Program CAC_EN per MCD (0-7) Tile */
3367 val0 = val = cgs_read_register(hwmgr->device, mmMC_CONFIG_MCD);
3368 val &= ~(MC_CONFIG_MCD__MCD0_WR_ENABLE_MASK |
3369 MC_CONFIG_MCD__MCD1_WR_ENABLE_MASK |
3370 MC_CONFIG_MCD__MCD2_WR_ENABLE_MASK |
3371 MC_CONFIG_MCD__MCD3_WR_ENABLE_MASK |
3372 MC_CONFIG_MCD__MCD4_WR_ENABLE_MASK |
3373 MC_CONFIG_MCD__MCD5_WR_ENABLE_MASK |
3374 MC_CONFIG_MCD__MCD6_WR_ENABLE_MASK |
3375 MC_CONFIG_MCD__MCD7_WR_ENABLE_MASK |
3376 MC_CONFIG_MCD__MC_RD_ENABLE_MASK);
3377
3378 for (i = 0; i < 8; i++) {
3379 /* Enable MCD i Tile read & write */
3380 val2 = (val | (i << MC_CONFIG_MCD__MC_RD_ENABLE__SHIFT) |
3381 (1 << i));
3382 cgs_write_register(hwmgr->device, mmMC_CONFIG_MCD, val2);
3383 /* Enbale CAC_ON MCD i Tile */
3384 val2 = cgs_read_register(hwmgr->device, mmMC_SEQ_CNTL);
3385 val2 |= MC_SEQ_CNTL__CAC_EN_MASK;
3386 cgs_write_register(hwmgr->device, mmMC_SEQ_CNTL, val2);
3387 }
3388 /* Set MC_CONFIG_MCD back to its default setting val0 */
3389 cgs_write_register(hwmgr->device, mmMC_CONFIG_MCD, val0);
3390
3391 PP_ASSERT_WITH_CODE(
3392 (0 == smum_send_msg_to_smc(hwmgr->smumgr,
3393 PPSMC_MSG_MCLKDPM_Enable)),
3394 "Failed to enable MCLK DPM during DPM Start Function!",
3395 return -1);
3396 }
3397 return 0;
3398}
3399
3400static int fiji_start_dpm(struct pp_hwmgr *hwmgr)
3401{
3402 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3403
3404 /*enable general power management */
3405 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
3406 GLOBAL_PWRMGT_EN, 1);
3407 /* enable sclk deep sleep */
3408 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SCLK_PWRMGT_CNTL,
3409 DYNAMIC_PM_EN, 1);
3410 /* prepare for PCIE DPM */
3411 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
3412 data->soft_regs_start + offsetof(SMU73_SoftRegisters,
3413 VoltageChangeTimeout), 0x1000);
3414 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__PCIE,
3415 SWRST_COMMAND_1, RESETLC, 0x0);
3416
3417 PP_ASSERT_WITH_CODE(
3418 (0 == smum_send_msg_to_smc(hwmgr->smumgr,
3419 PPSMC_MSG_Voltage_Cntl_Enable)),
3420 "Failed to enable voltage DPM during DPM Start Function!",
3421 return -1);
3422
3423 if (fiji_enable_sclk_mclk_dpm(hwmgr)) {
3424 printk(KERN_ERR "Failed to enable Sclk DPM and Mclk DPM!");
3425 return -1;
3426 }
3427
3428 /* enable PCIE dpm */
3429 if(!data->pcie_dpm_key_disabled) {
3430 PP_ASSERT_WITH_CODE(
3431 (0 == smum_send_msg_to_smc(hwmgr->smumgr,
3432 PPSMC_MSG_PCIeDPM_Enable)),
3433 "Failed to enable pcie DPM during DPM Start Function!",
3434 return -1);
3435 }
3436
Rex Zhu75ac63d2016-01-06 16:38:48 +08003437 return 0;
Eric Huangaabcb7c2015-08-26 16:52:28 -04003438}
3439
Eric Huang55d74fc2016-05-31 17:06:14 -04003440static int fiji_disable_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
3441{
3442 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3443
3444 /* disable SCLK dpm */
3445 if (!data->sclk_dpm_key_disabled)
3446 PP_ASSERT_WITH_CODE(
3447 (smum_send_msg_to_smc(hwmgr->smumgr,
3448 PPSMC_MSG_DPM_Disable) == 0),
3449 "Failed to disable SCLK DPM!",
3450 return -1);
3451
3452 /* disable MCLK dpm */
3453 if (!data->mclk_dpm_key_disabled) {
3454 PP_ASSERT_WITH_CODE(
3455 (smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3456 PPSMC_MSG_MCLKDPM_SetEnabledMask, 1) == 0),
3457 "Failed to force MCLK DPM0!",
3458 return -1);
3459
3460 PP_ASSERT_WITH_CODE(
3461 (smum_send_msg_to_smc(hwmgr->smumgr,
3462 PPSMC_MSG_MCLKDPM_Disable) == 0),
3463 "Failed to disable MCLK DPM!",
3464 return -1);
3465 }
3466
3467 return 0;
3468}
3469
3470static int fiji_stop_dpm(struct pp_hwmgr *hwmgr)
3471{
3472 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3473
3474 /* disable general power management */
3475 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
3476 GLOBAL_PWRMGT_EN, 0);
3477 /* disable sclk deep sleep */
3478 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SCLK_PWRMGT_CNTL,
3479 DYNAMIC_PM_EN, 0);
3480
3481 /* disable PCIE dpm */
3482 if (!data->pcie_dpm_key_disabled) {
3483 PP_ASSERT_WITH_CODE(
3484 (smum_send_msg_to_smc(hwmgr->smumgr,
3485 PPSMC_MSG_PCIeDPM_Disable) == 0),
3486 "Failed to disable pcie DPM during DPM Stop Function!",
3487 return -1);
3488 }
3489
3490 if (fiji_disable_sclk_mclk_dpm(hwmgr)) {
3491 printk(KERN_ERR "Failed to disable Sclk DPM and Mclk DPM!");
3492 return -1;
3493 }
3494
3495 PP_ASSERT_WITH_CODE(
3496 (smum_send_msg_to_smc(hwmgr->smumgr,
3497 PPSMC_MSG_Voltage_Cntl_Disable) == 0),
3498 "Failed to disable voltage DPM during DPM Stop Function!",
3499 return -1);
3500
3501 return 0;
3502}
3503
Eric Huangaabcb7c2015-08-26 16:52:28 -04003504static void fiji_set_dpm_event_sources(struct pp_hwmgr *hwmgr,
3505 uint32_t sources)
3506{
3507 bool protection;
3508 enum DPM_EVENT_SRC src;
3509
3510 switch (sources) {
3511 default:
3512 printk(KERN_ERR "Unknown throttling event sources.");
3513 /* fall through */
3514 case 0:
3515 protection = false;
3516 /* src is unused */
3517 break;
3518 case (1 << PHM_AutoThrottleSource_Thermal):
3519 protection = true;
3520 src = DPM_EVENT_SRC_DIGITAL;
3521 break;
3522 case (1 << PHM_AutoThrottleSource_External):
3523 protection = true;
3524 src = DPM_EVENT_SRC_EXTERNAL;
3525 break;
3526 case (1 << PHM_AutoThrottleSource_External) |
3527 (1 << PHM_AutoThrottleSource_Thermal):
3528 protection = true;
3529 src = DPM_EVENT_SRC_DIGITAL_OR_EXTERNAL;
3530 break;
3531 }
3532 /* Order matters - don't enable thermal protection for the wrong source. */
3533 if (protection) {
3534 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, CG_THERMAL_CTRL,
3535 DPM_EVENT_SRC, src);
3536 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
3537 THERMAL_PROTECTION_DIS,
Rex Zhuf0911de2016-03-23 14:50:22 +08003538 !phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
Eric Huangaabcb7c2015-08-26 16:52:28 -04003539 PHM_PlatformCaps_ThermalController));
3540 } else
3541 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
3542 THERMAL_PROTECTION_DIS, 1);
3543}
3544
3545static int fiji_enable_auto_throttle_source(struct pp_hwmgr *hwmgr,
3546 PHM_AutoThrottleSource source)
3547{
3548 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3549
3550 if (!(data->active_auto_throttle_sources & (1 << source))) {
3551 data->active_auto_throttle_sources |= 1 << source;
3552 fiji_set_dpm_event_sources(hwmgr, data->active_auto_throttle_sources);
3553 }
3554 return 0;
3555}
3556
3557static int fiji_enable_thermal_auto_throttle(struct pp_hwmgr *hwmgr)
3558{
Rex Zhu75ac63d2016-01-06 16:38:48 +08003559 return fiji_enable_auto_throttle_source(hwmgr, PHM_AutoThrottleSource_Thermal);
Eric Huangaabcb7c2015-08-26 16:52:28 -04003560}
3561
Eric Huang55d74fc2016-05-31 17:06:14 -04003562static int fiji_disable_auto_throttle_source(struct pp_hwmgr *hwmgr,
3563 PHM_AutoThrottleSource source)
3564{
3565 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3566
3567 if (data->active_auto_throttle_sources & (1 << source)) {
3568 data->active_auto_throttle_sources &= ~(1 << source);
3569 fiji_set_dpm_event_sources(hwmgr, data->active_auto_throttle_sources);
3570 }
3571 return 0;
3572}
3573
3574static int fiji_disable_thermal_auto_throttle(struct pp_hwmgr *hwmgr)
3575{
3576 return fiji_disable_auto_throttle_source(hwmgr, PHM_AutoThrottleSource_Thermal);
3577}
3578
Eric Huangaabcb7c2015-08-26 16:52:28 -04003579static int fiji_enable_dpm_tasks(struct pp_hwmgr *hwmgr)
3580{
3581 int tmp_result, result = 0;
3582
3583 tmp_result = (!fiji_is_dpm_running(hwmgr))? 0 : -1;
3584 PP_ASSERT_WITH_CODE(result == 0,
3585 "DPM is already running right now, no need to enable DPM!",
3586 return 0);
3587
3588 if (fiji_voltage_control(hwmgr)) {
3589 tmp_result = fiji_enable_voltage_control(hwmgr);
3590 PP_ASSERT_WITH_CODE(tmp_result == 0,
3591 "Failed to enable voltage control!",
3592 result = tmp_result);
3593 }
3594
3595 if (fiji_voltage_control(hwmgr)) {
3596 tmp_result = fiji_construct_voltage_tables(hwmgr);
3597 PP_ASSERT_WITH_CODE((0 == tmp_result),
3598 "Failed to contruct voltage tables!",
3599 result = tmp_result);
3600 }
3601
3602 tmp_result = fiji_initialize_mc_reg_table(hwmgr);
3603 PP_ASSERT_WITH_CODE((0 == tmp_result),
3604 "Failed to initialize MC reg table!", result = tmp_result);
3605
3606 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3607 PHM_PlatformCaps_EngineSpreadSpectrumSupport))
3608 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
3609 GENERAL_PWRMGT, DYN_SPREAD_SPECTRUM_EN, 1);
3610
3611 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3612 PHM_PlatformCaps_ThermalController))
3613 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
3614 GENERAL_PWRMGT, THERMAL_PROTECTION_DIS, 0);
3615
3616 tmp_result = fiji_program_static_screen_threshold_parameters(hwmgr);
3617 PP_ASSERT_WITH_CODE((0 == tmp_result),
3618 "Failed to program static screen threshold parameters!",
3619 result = tmp_result);
3620
3621 tmp_result = fiji_enable_display_gap(hwmgr);
3622 PP_ASSERT_WITH_CODE((0 == tmp_result),
3623 "Failed to enable display gap!", result = tmp_result);
3624
3625 tmp_result = fiji_program_voting_clients(hwmgr);
3626 PP_ASSERT_WITH_CODE((0 == tmp_result),
3627 "Failed to program voting clients!", result = tmp_result);
3628
3629 tmp_result = fiji_process_firmware_header(hwmgr);
3630 PP_ASSERT_WITH_CODE((0 == tmp_result),
3631 "Failed to process firmware header!", result = tmp_result);
3632
3633 tmp_result = fiji_initial_switch_from_arbf0_to_f1(hwmgr);
3634 PP_ASSERT_WITH_CODE((0 == tmp_result),
3635 "Failed to initialize switch from ArbF0 to F1!",
3636 result = tmp_result);
3637
3638 tmp_result = fiji_init_smc_table(hwmgr);
3639 PP_ASSERT_WITH_CODE((0 == tmp_result),
3640 "Failed to initialize SMC table!", result = tmp_result);
3641
3642 tmp_result = fiji_init_arb_table_index(hwmgr);
3643 PP_ASSERT_WITH_CODE((0 == tmp_result),
3644 "Failed to initialize ARB table index!", result = tmp_result);
3645
3646 tmp_result = fiji_populate_pm_fuses(hwmgr);
3647 PP_ASSERT_WITH_CODE((0 == tmp_result),
3648 "Failed to populate PM fuses!", result = tmp_result);
3649
3650 tmp_result = fiji_enable_vrhot_gpio_interrupt(hwmgr);
3651 PP_ASSERT_WITH_CODE((0 == tmp_result),
3652 "Failed to enable VR hot GPIO interrupt!", result = tmp_result);
3653
Eric Huangea617bc2015-11-24 17:00:56 -05003654 tmp_result = tonga_notify_smc_display_change(hwmgr, false);
3655 PP_ASSERT_WITH_CODE((0 == tmp_result),
3656 "Failed to notify no display!", result = tmp_result);
3657
Eric Huangaabcb7c2015-08-26 16:52:28 -04003658 tmp_result = fiji_enable_sclk_control(hwmgr);
3659 PP_ASSERT_WITH_CODE((0 == tmp_result),
3660 "Failed to enable SCLK control!", result = tmp_result);
3661
3662 tmp_result = fiji_enable_ulv(hwmgr);
3663 PP_ASSERT_WITH_CODE((0 == tmp_result),
3664 "Failed to enable ULV!", result = tmp_result);
3665
3666 tmp_result = fiji_enable_deep_sleep_master_switch(hwmgr);
3667 PP_ASSERT_WITH_CODE((0 == tmp_result),
3668 "Failed to enable deep sleep master switch!", result = tmp_result);
3669
3670 tmp_result = fiji_start_dpm(hwmgr);
3671 PP_ASSERT_WITH_CODE((0 == tmp_result),
3672 "Failed to start DPM!", result = tmp_result);
3673
3674 tmp_result = fiji_enable_smc_cac(hwmgr);
3675 PP_ASSERT_WITH_CODE((0 == tmp_result),
3676 "Failed to enable SMC CAC!", result = tmp_result);
3677
3678 tmp_result = fiji_enable_power_containment(hwmgr);
3679 PP_ASSERT_WITH_CODE((0 == tmp_result),
3680 "Failed to enable power containment!", result = tmp_result);
3681
3682 tmp_result = fiji_power_control_set_level(hwmgr);
3683 PP_ASSERT_WITH_CODE((0 == tmp_result),
3684 "Failed to power control set level!", result = tmp_result);
3685
3686 tmp_result = fiji_enable_thermal_auto_throttle(hwmgr);
3687 PP_ASSERT_WITH_CODE((0 == tmp_result),
3688 "Failed to enable thermal auto throttle!", result = tmp_result);
3689
3690 return result;
3691}
3692
Eric Huang55d74fc2016-05-31 17:06:14 -04003693static int fiji_disable_dpm_tasks(struct pp_hwmgr *hwmgr)
3694{
3695 int tmp_result, result = 0;
3696
3697 tmp_result = (fiji_is_dpm_running(hwmgr)) ? 0 : -1;
3698 PP_ASSERT_WITH_CODE(tmp_result == 0,
3699 "DPM is not running right now, no need to disable DPM!",
3700 return 0);
3701
3702 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3703 PHM_PlatformCaps_ThermalController))
3704 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
3705 GENERAL_PWRMGT, THERMAL_PROTECTION_DIS, 1);
3706
3707 tmp_result = fiji_disable_power_containment(hwmgr);
3708 PP_ASSERT_WITH_CODE((tmp_result == 0),
3709 "Failed to disable power containment!", result = tmp_result);
3710
3711 tmp_result = fiji_disable_smc_cac(hwmgr);
3712 PP_ASSERT_WITH_CODE((tmp_result == 0),
3713 "Failed to disable SMC CAC!", result = tmp_result);
3714
3715 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
3716 CG_SPLL_SPREAD_SPECTRUM, SSEN, 0);
3717 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
3718 GENERAL_PWRMGT, DYN_SPREAD_SPECTRUM_EN, 0);
3719
3720 tmp_result = fiji_disable_thermal_auto_throttle(hwmgr);
3721 PP_ASSERT_WITH_CODE((tmp_result == 0),
3722 "Failed to disable thermal auto throttle!", result = tmp_result);
3723
3724 tmp_result = fiji_stop_dpm(hwmgr);
3725 PP_ASSERT_WITH_CODE((tmp_result == 0),
3726 "Failed to stop DPM!", result = tmp_result);
3727
3728 tmp_result = fiji_disable_deep_sleep_master_switch(hwmgr);
3729 PP_ASSERT_WITH_CODE((tmp_result == 0),
3730 "Failed to disable deep sleep master switch!", result = tmp_result);
3731
3732 tmp_result = fiji_disable_ulv(hwmgr);
3733 PP_ASSERT_WITH_CODE((tmp_result == 0),
3734 "Failed to disable ULV!", result = tmp_result);
3735
3736 tmp_result = fiji_clear_voting_clients(hwmgr);
3737 PP_ASSERT_WITH_CODE((tmp_result == 0),
3738 "Failed to clear voting clients!", result = tmp_result);
3739
3740 tmp_result = fiji_reset_to_default(hwmgr);
3741 PP_ASSERT_WITH_CODE((tmp_result == 0),
3742 "Failed to reset to default!", result = tmp_result);
3743
3744 tmp_result = fiji_force_switch_to_arbf0(hwmgr);
3745 PP_ASSERT_WITH_CODE((tmp_result == 0),
3746 "Failed to force to switch arbf0!", result = tmp_result);
3747
3748 return result;
3749}
3750
Eric Huangaabcb7c2015-08-26 16:52:28 -04003751static int fiji_force_dpm_highest(struct pp_hwmgr *hwmgr)
3752{
3753 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3754 uint32_t level, tmp;
3755
3756 if (!data->sclk_dpm_key_disabled) {
3757 if (data->dpm_level_enable_mask.sclk_dpm_enable_mask) {
3758 level = 0;
3759 tmp = data->dpm_level_enable_mask.sclk_dpm_enable_mask;
3760 while (tmp >>= 1)
3761 level++;
3762 if (level)
3763 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3764 PPSMC_MSG_SCLKDPM_SetEnabledMask,
3765 (1 << level));
3766 }
3767 }
3768
3769 if (!data->mclk_dpm_key_disabled) {
3770 if (data->dpm_level_enable_mask.mclk_dpm_enable_mask) {
3771 level = 0;
3772 tmp = data->dpm_level_enable_mask.mclk_dpm_enable_mask;
3773 while (tmp >>= 1)
3774 level++;
3775 if (level)
3776 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3777 PPSMC_MSG_MCLKDPM_SetEnabledMask,
3778 (1 << level));
3779 }
3780 }
3781
3782 if (!data->pcie_dpm_key_disabled) {
3783 if (data->dpm_level_enable_mask.pcie_dpm_enable_mask) {
3784 level = 0;
3785 tmp = data->dpm_level_enable_mask.pcie_dpm_enable_mask;
3786 while (tmp >>= 1)
3787 level++;
3788 if (level)
3789 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3790 PPSMC_MSG_PCIeDPM_ForceLevel,
3791 (1 << level));
3792 }
3793 }
3794 return 0;
3795}
3796
Eric Huangaabcb7c2015-08-26 16:52:28 -04003797static int fiji_upload_dpmlevel_enable_mask(struct pp_hwmgr *hwmgr)
3798{
3799 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3800
Rex Zhu8b41e7a2016-05-10 15:37:00 +08003801 phm_apply_dal_min_voltage_request(hwmgr);
Eric Huangaabcb7c2015-08-26 16:52:28 -04003802
3803 if (!data->sclk_dpm_key_disabled) {
3804 if (data->dpm_level_enable_mask.sclk_dpm_enable_mask)
3805 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3806 PPSMC_MSG_SCLKDPM_SetEnabledMask,
3807 data->dpm_level_enable_mask.sclk_dpm_enable_mask);
3808 }
3809 return 0;
3810}
3811
3812static int fiji_unforce_dpm_levels(struct pp_hwmgr *hwmgr)
3813{
3814 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3815
3816 if (!fiji_is_dpm_running(hwmgr))
3817 return -EINVAL;
3818
3819 if (!data->pcie_dpm_key_disabled) {
3820 smum_send_msg_to_smc(hwmgr->smumgr,
3821 PPSMC_MSG_PCIeDPM_UnForceLevel);
3822 }
3823
3824 return fiji_upload_dpmlevel_enable_mask(hwmgr);
3825}
3826
3827static uint32_t fiji_get_lowest_enabled_level(
3828 struct pp_hwmgr *hwmgr, uint32_t mask)
3829{
3830 uint32_t level = 0;
3831
3832 while(0 == (mask & (1 << level)))
3833 level++;
3834
3835 return level;
3836}
3837
3838static int fiji_force_dpm_lowest(struct pp_hwmgr *hwmgr)
3839{
3840 struct fiji_hwmgr *data =
3841 (struct fiji_hwmgr *)(hwmgr->backend);
Alex Deucher74c577b2015-11-11 00:31:00 -05003842 uint32_t level;
Eric Huangaabcb7c2015-08-26 16:52:28 -04003843
Eric Huangaabcb7c2015-08-26 16:52:28 -04003844 if (!data->sclk_dpm_key_disabled)
3845 if (data->dpm_level_enable_mask.sclk_dpm_enable_mask) {
3846 level = fiji_get_lowest_enabled_level(hwmgr,
Alex Deucher74c577b2015-11-11 00:31:00 -05003847 data->dpm_level_enable_mask.sclk_dpm_enable_mask);
Eric Huangaabcb7c2015-08-26 16:52:28 -04003848 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
Alex Deucher74c577b2015-11-11 00:31:00 -05003849 PPSMC_MSG_SCLKDPM_SetEnabledMask,
3850 (1 << level));
Eric Huangaabcb7c2015-08-26 16:52:28 -04003851
3852 }
Alex Deucher74c577b2015-11-11 00:31:00 -05003853
3854 if (!data->mclk_dpm_key_disabled) {
3855 if (data->dpm_level_enable_mask.mclk_dpm_enable_mask) {
3856 level = fiji_get_lowest_enabled_level(hwmgr,
3857 data->dpm_level_enable_mask.mclk_dpm_enable_mask);
3858 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3859 PPSMC_MSG_MCLKDPM_SetEnabledMask,
3860 (1 << level));
3861 }
3862 }
3863
3864 if (!data->pcie_dpm_key_disabled) {
3865 if (data->dpm_level_enable_mask.pcie_dpm_enable_mask) {
3866 level = fiji_get_lowest_enabled_level(hwmgr,
3867 data->dpm_level_enable_mask.pcie_dpm_enable_mask);
3868 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3869 PPSMC_MSG_PCIeDPM_ForceLevel,
3870 (1 << level));
3871 }
3872 }
3873
Eric Huangaabcb7c2015-08-26 16:52:28 -04003874 return 0;
3875
3876}
3877static int fiji_dpm_force_dpm_level(struct pp_hwmgr *hwmgr,
3878 enum amd_dpm_forced_level level)
3879{
3880 int ret = 0;
3881
3882 switch (level) {
3883 case AMD_DPM_FORCED_LEVEL_HIGH:
3884 ret = fiji_force_dpm_highest(hwmgr);
3885 if (ret)
3886 return ret;
3887 break;
3888 case AMD_DPM_FORCED_LEVEL_LOW:
3889 ret = fiji_force_dpm_lowest(hwmgr);
3890 if (ret)
3891 return ret;
3892 break;
3893 case AMD_DPM_FORCED_LEVEL_AUTO:
3894 ret = fiji_unforce_dpm_levels(hwmgr);
3895 if (ret)
3896 return ret;
3897 break;
3898 default:
3899 break;
3900 }
3901
3902 hwmgr->dpm_level = level;
3903
3904 return ret;
3905}
3906
3907static int fiji_get_power_state_size(struct pp_hwmgr *hwmgr)
3908{
3909 return sizeof(struct fiji_power_state);
3910}
3911
3912static int fiji_get_pp_table_entry_callback_func(struct pp_hwmgr *hwmgr,
3913 void *state, struct pp_power_state *power_state,
3914 void *pp_table, uint32_t classification_flag)
3915{
3916 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
3917 struct fiji_power_state *fiji_power_state =
3918 (struct fiji_power_state *)(&(power_state->hardware));
3919 struct fiji_performance_level *performance_level;
3920 ATOM_Tonga_State *state_entry = (ATOM_Tonga_State *)state;
3921 ATOM_Tonga_POWERPLAYTABLE *powerplay_table =
3922 (ATOM_Tonga_POWERPLAYTABLE *)pp_table;
3923 ATOM_Tonga_SCLK_Dependency_Table *sclk_dep_table =
3924 (ATOM_Tonga_SCLK_Dependency_Table *)
3925 (((unsigned long)powerplay_table) +
3926 le16_to_cpu(powerplay_table->usSclkDependencyTableOffset));
3927 ATOM_Tonga_MCLK_Dependency_Table *mclk_dep_table =
3928 (ATOM_Tonga_MCLK_Dependency_Table *)
3929 (((unsigned long)powerplay_table) +
3930 le16_to_cpu(powerplay_table->usMclkDependencyTableOffset));
3931
3932 /* The following fields are not initialized here: id orderedList allStatesList */
3933 power_state->classification.ui_label =
3934 (le16_to_cpu(state_entry->usClassification) &
3935 ATOM_PPLIB_CLASSIFICATION_UI_MASK) >>
3936 ATOM_PPLIB_CLASSIFICATION_UI_SHIFT;
3937 power_state->classification.flags = classification_flag;
3938 /* NOTE: There is a classification2 flag in BIOS that is not being used right now */
3939
3940 power_state->classification.temporary_state = false;
3941 power_state->classification.to_be_deleted = false;
3942
3943 power_state->validation.disallowOnDC =
3944 (0 != (le32_to_cpu(state_entry->ulCapsAndSettings) &
3945 ATOM_Tonga_DISALLOW_ON_DC));
3946
3947 power_state->pcie.lanes = 0;
3948
3949 power_state->display.disableFrameModulation = false;
3950 power_state->display.limitRefreshrate = false;
3951 power_state->display.enableVariBright =
3952 (0 != (le32_to_cpu(state_entry->ulCapsAndSettings) &
3953 ATOM_Tonga_ENABLE_VARIBRIGHT));
3954
3955 power_state->validation.supportedPowerLevels = 0;
3956 power_state->uvd_clocks.VCLK = 0;
3957 power_state->uvd_clocks.DCLK = 0;
3958 power_state->temperatures.min = 0;
3959 power_state->temperatures.max = 0;
3960
3961 performance_level = &(fiji_power_state->performance_levels
3962 [fiji_power_state->performance_level_count++]);
3963
3964 PP_ASSERT_WITH_CODE(
3965 (fiji_power_state->performance_level_count < SMU73_MAX_LEVELS_GRAPHICS),
3966 "Performance levels exceeds SMC limit!",
3967 return -1);
3968
3969 PP_ASSERT_WITH_CODE(
3970 (fiji_power_state->performance_level_count <=
3971 hwmgr->platform_descriptor.hardwareActivityPerformanceLevels),
3972 "Performance levels exceeds Driver limit!",
3973 return -1);
3974
3975 /* Performance levels are arranged from low to high. */
3976 performance_level->memory_clock = mclk_dep_table->entries
3977 [state_entry->ucMemoryClockIndexLow].ulMclk;
3978 performance_level->engine_clock = sclk_dep_table->entries
3979 [state_entry->ucEngineClockIndexLow].ulSclk;
3980 performance_level->pcie_gen = get_pcie_gen_support(data->pcie_gen_cap,
3981 state_entry->ucPCIEGenLow);
3982 performance_level->pcie_lane = get_pcie_lane_support(data->pcie_lane_cap,
3983 state_entry->ucPCIELaneHigh);
3984
3985 performance_level = &(fiji_power_state->performance_levels
3986 [fiji_power_state->performance_level_count++]);
3987 performance_level->memory_clock = mclk_dep_table->entries
3988 [state_entry->ucMemoryClockIndexHigh].ulMclk;
3989 performance_level->engine_clock = sclk_dep_table->entries
3990 [state_entry->ucEngineClockIndexHigh].ulSclk;
3991 performance_level->pcie_gen = get_pcie_gen_support(data->pcie_gen_cap,
3992 state_entry->ucPCIEGenHigh);
3993 performance_level->pcie_lane = get_pcie_lane_support(data->pcie_lane_cap,
3994 state_entry->ucPCIELaneHigh);
3995
3996 return 0;
3997}
3998
3999static int fiji_get_pp_table_entry(struct pp_hwmgr *hwmgr,
4000 unsigned long entry_index, struct pp_power_state *state)
4001{
4002 int result;
4003 struct fiji_power_state *ps;
4004 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4005 struct phm_ppt_v1_information *table_info =
4006 (struct phm_ppt_v1_information *)(hwmgr->pptable);
4007 struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table =
4008 table_info->vdd_dep_on_mclk;
4009
4010 state->hardware.magic = PHM_VIslands_Magic;
4011
4012 ps = (struct fiji_power_state *)(&state->hardware);
4013
4014 result = tonga_get_powerplay_table_entry(hwmgr, entry_index, state,
4015 fiji_get_pp_table_entry_callback_func);
4016
4017 /* This is the earliest time we have all the dependency table and the VBIOS boot state
4018 * as PP_Tables_GetPowerPlayTableEntry retrieves the VBIOS boot state
4019 * if there is only one VDDCI/MCLK level, check if it's the same as VBIOS boot state
4020 */
4021 if (dep_mclk_table != NULL && dep_mclk_table->count == 1) {
4022 if (dep_mclk_table->entries[0].clk !=
4023 data->vbios_boot_state.mclk_bootup_value)
4024 printk(KERN_ERR "Single MCLK entry VDDCI/MCLK dependency table "
4025 "does not match VBIOS boot MCLK level");
4026 if (dep_mclk_table->entries[0].vddci !=
4027 data->vbios_boot_state.vddci_bootup_value)
4028 printk(KERN_ERR "Single VDDCI entry VDDCI/MCLK dependency table "
4029 "does not match VBIOS boot VDDCI level");
4030 }
4031
4032 /* set DC compatible flag if this state supports DC */
4033 if (!state->validation.disallowOnDC)
4034 ps->dc_compatible = true;
4035
4036 if (state->classification.flags & PP_StateClassificationFlag_ACPI)
4037 data->acpi_pcie_gen = ps->performance_levels[0].pcie_gen;
4038
4039 ps->uvd_clks.vclk = state->uvd_clocks.VCLK;
4040 ps->uvd_clks.dclk = state->uvd_clocks.DCLK;
4041
4042 if (!result) {
4043 uint32_t i;
4044
4045 switch (state->classification.ui_label) {
4046 case PP_StateUILabel_Performance:
4047 data->use_pcie_performance_levels = true;
4048
4049 for (i = 0; i < ps->performance_level_count; i++) {
4050 if (data->pcie_gen_performance.max <
4051 ps->performance_levels[i].pcie_gen)
4052 data->pcie_gen_performance.max =
4053 ps->performance_levels[i].pcie_gen;
4054
4055 if (data->pcie_gen_performance.min >
4056 ps->performance_levels[i].pcie_gen)
4057 data->pcie_gen_performance.min =
4058 ps->performance_levels[i].pcie_gen;
4059
4060 if (data->pcie_lane_performance.max <
4061 ps->performance_levels[i].pcie_lane)
4062 data->pcie_lane_performance.max =
4063 ps->performance_levels[i].pcie_lane;
4064
4065 if (data->pcie_lane_performance.min >
4066 ps->performance_levels[i].pcie_lane)
4067 data->pcie_lane_performance.min =
4068 ps->performance_levels[i].pcie_lane;
4069 }
4070 break;
4071 case PP_StateUILabel_Battery:
4072 data->use_pcie_power_saving_levels = true;
4073
4074 for (i = 0; i < ps->performance_level_count; i++) {
4075 if (data->pcie_gen_power_saving.max <
4076 ps->performance_levels[i].pcie_gen)
4077 data->pcie_gen_power_saving.max =
4078 ps->performance_levels[i].pcie_gen;
4079
4080 if (data->pcie_gen_power_saving.min >
4081 ps->performance_levels[i].pcie_gen)
4082 data->pcie_gen_power_saving.min =
4083 ps->performance_levels[i].pcie_gen;
4084
4085 if (data->pcie_lane_power_saving.max <
4086 ps->performance_levels[i].pcie_lane)
4087 data->pcie_lane_power_saving.max =
4088 ps->performance_levels[i].pcie_lane;
4089
4090 if (data->pcie_lane_power_saving.min >
4091 ps->performance_levels[i].pcie_lane)
4092 data->pcie_lane_power_saving.min =
4093 ps->performance_levels[i].pcie_lane;
4094 }
4095 break;
4096 default:
4097 break;
4098 }
4099 }
4100 return 0;
4101}
4102
4103static int fiji_apply_state_adjust_rules(struct pp_hwmgr *hwmgr,
4104 struct pp_power_state *request_ps,
4105 const struct pp_power_state *current_ps)
4106{
4107 struct fiji_power_state *fiji_ps =
4108 cast_phw_fiji_power_state(&request_ps->hardware);
4109 uint32_t sclk;
4110 uint32_t mclk;
4111 struct PP_Clocks minimum_clocks = {0};
4112 bool disable_mclk_switching;
4113 bool disable_mclk_switching_for_frame_lock;
4114 struct cgs_display_info info = {0};
4115 const struct phm_clock_and_voltage_limits *max_limits;
4116 uint32_t i;
4117 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4118 struct phm_ppt_v1_information *table_info =
4119 (struct phm_ppt_v1_information *)(hwmgr->pptable);
4120 int32_t count;
4121 int32_t stable_pstate_sclk = 0, stable_pstate_mclk = 0;
4122
4123 data->battery_state = (PP_StateUILabel_Battery ==
4124 request_ps->classification.ui_label);
4125
4126 PP_ASSERT_WITH_CODE(fiji_ps->performance_level_count == 2,
4127 "VI should always have 2 performance levels",);
4128
4129 max_limits = (PP_PowerSource_AC == hwmgr->power_source) ?
4130 &(hwmgr->dyn_state.max_clock_voltage_on_ac) :
4131 &(hwmgr->dyn_state.max_clock_voltage_on_dc);
4132
4133 /* Cap clock DPM tables at DC MAX if it is in DC. */
4134 if (PP_PowerSource_DC == hwmgr->power_source) {
4135 for (i = 0; i < fiji_ps->performance_level_count; i++) {
4136 if (fiji_ps->performance_levels[i].memory_clock > max_limits->mclk)
4137 fiji_ps->performance_levels[i].memory_clock = max_limits->mclk;
4138 if (fiji_ps->performance_levels[i].engine_clock > max_limits->sclk)
4139 fiji_ps->performance_levels[i].engine_clock = max_limits->sclk;
4140 }
4141 }
4142
4143 fiji_ps->vce_clks.evclk = hwmgr->vce_arbiter.evclk;
4144 fiji_ps->vce_clks.ecclk = hwmgr->vce_arbiter.ecclk;
4145
4146 fiji_ps->acp_clk = hwmgr->acp_arbiter.acpclk;
4147
4148 cgs_get_active_displays_info(hwmgr->device, &info);
4149
4150 /*TO DO result = PHM_CheckVBlankTime(hwmgr, &vblankTooShort);*/
4151
4152 /* TO DO GetMinClockSettings(hwmgr->pPECI, &minimum_clocks); */
4153
4154 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4155 PHM_PlatformCaps_StablePState)) {
4156 max_limits = &(hwmgr->dyn_state.max_clock_voltage_on_ac);
4157 stable_pstate_sclk = (max_limits->sclk * 75) / 100;
4158
4159 for (count = table_info->vdd_dep_on_sclk->count - 1;
4160 count >= 0; count--) {
4161 if (stable_pstate_sclk >=
4162 table_info->vdd_dep_on_sclk->entries[count].clk) {
4163 stable_pstate_sclk =
4164 table_info->vdd_dep_on_sclk->entries[count].clk;
4165 break;
4166 }
4167 }
4168
4169 if (count < 0)
4170 stable_pstate_sclk = table_info->vdd_dep_on_sclk->entries[0].clk;
4171
4172 stable_pstate_mclk = max_limits->mclk;
4173
4174 minimum_clocks.engineClock = stable_pstate_sclk;
4175 minimum_clocks.memoryClock = stable_pstate_mclk;
4176 }
4177
4178 if (minimum_clocks.engineClock < hwmgr->gfx_arbiter.sclk)
4179 minimum_clocks.engineClock = hwmgr->gfx_arbiter.sclk;
4180
4181 if (minimum_clocks.memoryClock < hwmgr->gfx_arbiter.mclk)
4182 minimum_clocks.memoryClock = hwmgr->gfx_arbiter.mclk;
4183
4184 fiji_ps->sclk_threshold = hwmgr->gfx_arbiter.sclk_threshold;
4185
4186 if (0 != hwmgr->gfx_arbiter.sclk_over_drive) {
4187 PP_ASSERT_WITH_CODE((hwmgr->gfx_arbiter.sclk_over_drive <=
4188 hwmgr->platform_descriptor.overdriveLimit.engineClock),
4189 "Overdrive sclk exceeds limit",
4190 hwmgr->gfx_arbiter.sclk_over_drive =
4191 hwmgr->platform_descriptor.overdriveLimit.engineClock);
4192
4193 if (hwmgr->gfx_arbiter.sclk_over_drive >= hwmgr->gfx_arbiter.sclk)
4194 fiji_ps->performance_levels[1].engine_clock =
4195 hwmgr->gfx_arbiter.sclk_over_drive;
4196 }
4197
4198 if (0 != hwmgr->gfx_arbiter.mclk_over_drive) {
4199 PP_ASSERT_WITH_CODE((hwmgr->gfx_arbiter.mclk_over_drive <=
4200 hwmgr->platform_descriptor.overdriveLimit.memoryClock),
4201 "Overdrive mclk exceeds limit",
4202 hwmgr->gfx_arbiter.mclk_over_drive =
4203 hwmgr->platform_descriptor.overdriveLimit.memoryClock);
4204
4205 if (hwmgr->gfx_arbiter.mclk_over_drive >= hwmgr->gfx_arbiter.mclk)
4206 fiji_ps->performance_levels[1].memory_clock =
4207 hwmgr->gfx_arbiter.mclk_over_drive;
4208 }
4209
4210 disable_mclk_switching_for_frame_lock = phm_cap_enabled(
4211 hwmgr->platform_descriptor.platformCaps,
4212 PHM_PlatformCaps_DisableMclkSwitchingForFrameLock);
4213
4214 disable_mclk_switching = (1 < info.display_count) ||
4215 disable_mclk_switching_for_frame_lock;
4216
4217 sclk = fiji_ps->performance_levels[0].engine_clock;
4218 mclk = fiji_ps->performance_levels[0].memory_clock;
4219
4220 if (disable_mclk_switching)
4221 mclk = fiji_ps->performance_levels
4222 [fiji_ps->performance_level_count - 1].memory_clock;
4223
4224 if (sclk < minimum_clocks.engineClock)
4225 sclk = (minimum_clocks.engineClock > max_limits->sclk) ?
4226 max_limits->sclk : minimum_clocks.engineClock;
4227
4228 if (mclk < minimum_clocks.memoryClock)
4229 mclk = (minimum_clocks.memoryClock > max_limits->mclk) ?
4230 max_limits->mclk : minimum_clocks.memoryClock;
4231
4232 fiji_ps->performance_levels[0].engine_clock = sclk;
4233 fiji_ps->performance_levels[0].memory_clock = mclk;
4234
4235 fiji_ps->performance_levels[1].engine_clock =
4236 (fiji_ps->performance_levels[1].engine_clock >=
4237 fiji_ps->performance_levels[0].engine_clock) ?
4238 fiji_ps->performance_levels[1].engine_clock :
4239 fiji_ps->performance_levels[0].engine_clock;
4240
4241 if (disable_mclk_switching) {
4242 if (mclk < fiji_ps->performance_levels[1].memory_clock)
4243 mclk = fiji_ps->performance_levels[1].memory_clock;
4244
4245 fiji_ps->performance_levels[0].memory_clock = mclk;
4246 fiji_ps->performance_levels[1].memory_clock = mclk;
4247 } else {
4248 if (fiji_ps->performance_levels[1].memory_clock <
4249 fiji_ps->performance_levels[0].memory_clock)
4250 fiji_ps->performance_levels[1].memory_clock =
4251 fiji_ps->performance_levels[0].memory_clock;
4252 }
4253
4254 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4255 PHM_PlatformCaps_StablePState)) {
4256 for (i = 0; i < fiji_ps->performance_level_count; i++) {
4257 fiji_ps->performance_levels[i].engine_clock = stable_pstate_sclk;
4258 fiji_ps->performance_levels[i].memory_clock = stable_pstate_mclk;
4259 fiji_ps->performance_levels[i].pcie_gen = data->pcie_gen_performance.max;
4260 fiji_ps->performance_levels[i].pcie_lane = data->pcie_gen_performance.max;
4261 }
4262 }
4263
4264 return 0;
4265}
4266
4267static int fiji_find_dpm_states_clocks_in_dpm_table(struct pp_hwmgr *hwmgr, const void *input)
4268{
4269 const struct phm_set_power_state_input *states =
4270 (const struct phm_set_power_state_input *)input;
4271 const struct fiji_power_state *fiji_ps =
4272 cast_const_phw_fiji_power_state(states->pnew_state);
4273 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4274 struct fiji_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
4275 uint32_t sclk = fiji_ps->performance_levels
4276 [fiji_ps->performance_level_count - 1].engine_clock;
4277 struct fiji_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
4278 uint32_t mclk = fiji_ps->performance_levels
4279 [fiji_ps->performance_level_count - 1].memory_clock;
Eric Huangaabcb7c2015-08-26 16:52:28 -04004280 uint32_t i;
4281 struct cgs_display_info info = {0};
4282
4283 data->need_update_smu7_dpm_table = 0;
4284
4285 for (i = 0; i < sclk_table->count; i++) {
4286 if (sclk == sclk_table->dpm_levels[i].value)
4287 break;
4288 }
4289
4290 if (i >= sclk_table->count)
4291 data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_SCLK;
4292 else {
Rex Zhu0eadeab2016-03-29 19:32:37 +08004293 if(data->display_timing.min_clock_in_sr !=
4294 hwmgr->display_config.min_core_set_clock_in_sr)
Eric Huangaabcb7c2015-08-26 16:52:28 -04004295 data->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_SCLK;
4296 }
4297
4298 for (i = 0; i < mclk_table->count; i++) {
4299 if (mclk == mclk_table->dpm_levels[i].value)
4300 break;
4301 }
4302
4303 if (i >= mclk_table->count)
4304 data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_MCLK;
4305
4306 cgs_get_active_displays_info(hwmgr->device, &info);
4307
4308 if (data->display_timing.num_existing_displays != info.display_count)
4309 data->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_MCLK;
4310
4311 return 0;
4312}
4313
4314static uint16_t fiji_get_maximum_link_speed(struct pp_hwmgr *hwmgr,
4315 const struct fiji_power_state *fiji_ps)
4316{
4317 uint32_t i;
4318 uint32_t sclk, max_sclk = 0;
4319 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4320 struct fiji_dpm_table *dpm_table = &data->dpm_table;
4321
4322 for (i = 0; i < fiji_ps->performance_level_count; i++) {
4323 sclk = fiji_ps->performance_levels[i].engine_clock;
4324 if (max_sclk < sclk)
4325 max_sclk = sclk;
4326 }
4327
4328 for (i = 0; i < dpm_table->sclk_table.count; i++) {
4329 if (dpm_table->sclk_table.dpm_levels[i].value == max_sclk)
4330 return (uint16_t) ((i >= dpm_table->pcie_speed_table.count) ?
4331 dpm_table->pcie_speed_table.dpm_levels
4332 [dpm_table->pcie_speed_table.count - 1].value :
4333 dpm_table->pcie_speed_table.dpm_levels[i].value);
4334 }
4335
4336 return 0;
4337}
4338
4339static int fiji_request_link_speed_change_before_state_change(
4340 struct pp_hwmgr *hwmgr, const void *input)
4341{
4342 const struct phm_set_power_state_input *states =
4343 (const struct phm_set_power_state_input *)input;
4344 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4345 const struct fiji_power_state *fiji_nps =
4346 cast_const_phw_fiji_power_state(states->pnew_state);
4347 const struct fiji_power_state *fiji_cps =
4348 cast_const_phw_fiji_power_state(states->pcurrent_state);
4349
4350 uint16_t target_link_speed = fiji_get_maximum_link_speed(hwmgr, fiji_nps);
4351 uint16_t current_link_speed;
4352
4353 if (data->force_pcie_gen == PP_PCIEGenInvalid)
4354 current_link_speed = fiji_get_maximum_link_speed(hwmgr, fiji_cps);
4355 else
4356 current_link_speed = data->force_pcie_gen;
4357
4358 data->force_pcie_gen = PP_PCIEGenInvalid;
4359 data->pspp_notify_required = false;
4360 if (target_link_speed > current_link_speed) {
4361 switch(target_link_speed) {
4362 case PP_PCIEGen3:
4363 if (0 == acpi_pcie_perf_request(hwmgr->device, PCIE_PERF_REQ_GEN3, false))
4364 break;
4365 data->force_pcie_gen = PP_PCIEGen2;
4366 if (current_link_speed == PP_PCIEGen2)
4367 break;
4368 case PP_PCIEGen2:
4369 if (0 == acpi_pcie_perf_request(hwmgr->device, PCIE_PERF_REQ_GEN2, false))
4370 break;
4371 default:
4372 data->force_pcie_gen = fiji_get_current_pcie_speed(hwmgr);
4373 break;
4374 }
4375 } else {
4376 if (target_link_speed < current_link_speed)
4377 data->pspp_notify_required = true;
4378 }
4379
4380 return 0;
4381}
4382
4383static int fiji_freeze_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
4384{
4385 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4386
4387 if (0 == data->need_update_smu7_dpm_table)
4388 return 0;
4389
4390 if ((0 == data->sclk_dpm_key_disabled) &&
4391 (data->need_update_smu7_dpm_table &
4392 (DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_UPDATE_SCLK))) {
4393 PP_ASSERT_WITH_CODE(true == fiji_is_dpm_running(hwmgr),
4394 "Trying to freeze SCLK DPM when DPM is disabled",);
4395 PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr->smumgr,
4396 PPSMC_MSG_SCLKDPM_FreezeLevel),
4397 "Failed to freeze SCLK DPM during FreezeSclkMclkDPM Function!",
4398 return -1);
4399 }
4400
4401 if ((0 == data->mclk_dpm_key_disabled) &&
4402 (data->need_update_smu7_dpm_table &
4403 DPMTABLE_OD_UPDATE_MCLK)) {
4404 PP_ASSERT_WITH_CODE(true == fiji_is_dpm_running(hwmgr),
4405 "Trying to freeze MCLK DPM when DPM is disabled",);
4406 PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr->smumgr,
4407 PPSMC_MSG_MCLKDPM_FreezeLevel),
4408 "Failed to freeze MCLK DPM during FreezeSclkMclkDPM Function!",
4409 return -1);
4410 }
4411
4412 return 0;
4413}
4414
4415static int fiji_populate_and_upload_sclk_mclk_dpm_levels(
4416 struct pp_hwmgr *hwmgr, const void *input)
4417{
4418 int result = 0;
4419 const struct phm_set_power_state_input *states =
4420 (const struct phm_set_power_state_input *)input;
4421 const struct fiji_power_state *fiji_ps =
4422 cast_const_phw_fiji_power_state(states->pnew_state);
4423 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4424 uint32_t sclk = fiji_ps->performance_levels
4425 [fiji_ps->performance_level_count - 1].engine_clock;
4426 uint32_t mclk = fiji_ps->performance_levels
4427 [fiji_ps->performance_level_count - 1].memory_clock;
4428 struct fiji_dpm_table *dpm_table = &data->dpm_table;
4429
4430 struct fiji_dpm_table *golden_dpm_table = &data->golden_dpm_table;
4431 uint32_t dpm_count, clock_percent;
4432 uint32_t i;
4433
4434 if (0 == data->need_update_smu7_dpm_table)
4435 return 0;
4436
4437 if (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_SCLK) {
4438 dpm_table->sclk_table.dpm_levels
4439 [dpm_table->sclk_table.count - 1].value = sclk;
4440
4441 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4442 PHM_PlatformCaps_OD6PlusinACSupport) ||
4443 phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4444 PHM_PlatformCaps_OD6PlusinDCSupport)) {
4445 /* Need to do calculation based on the golden DPM table
4446 * as the Heatmap GPU Clock axis is also based on the default values
4447 */
4448 PP_ASSERT_WITH_CODE(
4449 (golden_dpm_table->sclk_table.dpm_levels
4450 [golden_dpm_table->sclk_table.count - 1].value != 0),
4451 "Divide by 0!",
4452 return -1);
4453 dpm_count = dpm_table->sclk_table.count < 2 ?
4454 0 : dpm_table->sclk_table.count - 2;
4455 for (i = dpm_count; i > 1; i--) {
4456 if (sclk > golden_dpm_table->sclk_table.dpm_levels
4457 [golden_dpm_table->sclk_table.count-1].value) {
4458 clock_percent =
4459 ((sclk - golden_dpm_table->sclk_table.dpm_levels
4460 [golden_dpm_table->sclk_table.count-1].value) * 100) /
4461 golden_dpm_table->sclk_table.dpm_levels
4462 [golden_dpm_table->sclk_table.count-1].value;
4463
4464 dpm_table->sclk_table.dpm_levels[i].value =
4465 golden_dpm_table->sclk_table.dpm_levels[i].value +
4466 (golden_dpm_table->sclk_table.dpm_levels[i].value *
4467 clock_percent)/100;
4468
4469 } else if (golden_dpm_table->sclk_table.dpm_levels
4470 [dpm_table->sclk_table.count-1].value > sclk) {
4471 clock_percent =
4472 ((golden_dpm_table->sclk_table.dpm_levels
4473 [golden_dpm_table->sclk_table.count - 1].value - sclk) *
4474 100) /
4475 golden_dpm_table->sclk_table.dpm_levels
4476 [golden_dpm_table->sclk_table.count-1].value;
4477
4478 dpm_table->sclk_table.dpm_levels[i].value =
4479 golden_dpm_table->sclk_table.dpm_levels[i].value -
4480 (golden_dpm_table->sclk_table.dpm_levels[i].value *
4481 clock_percent) / 100;
4482 } else
4483 dpm_table->sclk_table.dpm_levels[i].value =
4484 golden_dpm_table->sclk_table.dpm_levels[i].value;
4485 }
4486 }
4487 }
4488
4489 if (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK) {
4490 dpm_table->mclk_table.dpm_levels
4491 [dpm_table->mclk_table.count - 1].value = mclk;
Eric Huangaabcb7c2015-08-26 16:52:28 -04004492 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4493 PHM_PlatformCaps_OD6PlusinACSupport) ||
4494 phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4495 PHM_PlatformCaps_OD6PlusinDCSupport)) {
4496
4497 PP_ASSERT_WITH_CODE(
4498 (golden_dpm_table->mclk_table.dpm_levels
4499 [golden_dpm_table->mclk_table.count-1].value != 0),
4500 "Divide by 0!",
4501 return -1);
4502 dpm_count = dpm_table->mclk_table.count < 2 ?
4503 0 : dpm_table->mclk_table.count - 2;
4504 for (i = dpm_count; i > 1; i--) {
4505 if (mclk > golden_dpm_table->mclk_table.dpm_levels
4506 [golden_dpm_table->mclk_table.count-1].value) {
4507 clock_percent = ((mclk -
4508 golden_dpm_table->mclk_table.dpm_levels
4509 [golden_dpm_table->mclk_table.count-1].value) * 100) /
4510 golden_dpm_table->mclk_table.dpm_levels
4511 [golden_dpm_table->mclk_table.count-1].value;
4512
4513 dpm_table->mclk_table.dpm_levels[i].value =
4514 golden_dpm_table->mclk_table.dpm_levels[i].value +
4515 (golden_dpm_table->mclk_table.dpm_levels[i].value *
4516 clock_percent) / 100;
4517
4518 } else if (golden_dpm_table->mclk_table.dpm_levels
4519 [dpm_table->mclk_table.count-1].value > mclk) {
4520 clock_percent = ((golden_dpm_table->mclk_table.dpm_levels
4521 [golden_dpm_table->mclk_table.count-1].value - mclk) * 100) /
4522 golden_dpm_table->mclk_table.dpm_levels
4523 [golden_dpm_table->mclk_table.count-1].value;
4524
4525 dpm_table->mclk_table.dpm_levels[i].value =
4526 golden_dpm_table->mclk_table.dpm_levels[i].value -
4527 (golden_dpm_table->mclk_table.dpm_levels[i].value *
4528 clock_percent) / 100;
4529 } else
4530 dpm_table->mclk_table.dpm_levels[i].value =
4531 golden_dpm_table->mclk_table.dpm_levels[i].value;
4532 }
4533 }
4534 }
4535
4536 if (data->need_update_smu7_dpm_table &
4537 (DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_UPDATE_SCLK)) {
Eric Huangbd6080d2016-05-10 14:39:09 -04004538 result = fiji_populate_all_graphic_levels(hwmgr);
Eric Huangaabcb7c2015-08-26 16:52:28 -04004539 PP_ASSERT_WITH_CODE((0 == result),
4540 "Failed to populate SCLK during PopulateNewDPMClocksStates Function!",
4541 return result);
4542 }
4543
4544 if (data->need_update_smu7_dpm_table &
4545 (DPMTABLE_OD_UPDATE_MCLK + DPMTABLE_UPDATE_MCLK)) {
4546 /*populate MCLK dpm table to SMU7 */
4547 result = fiji_populate_all_memory_levels(hwmgr);
4548 PP_ASSERT_WITH_CODE((0 == result),
4549 "Failed to populate MCLK during PopulateNewDPMClocksStates Function!",
4550 return result);
4551 }
4552
4553 return result;
4554}
4555
4556static int fiji_trim_single_dpm_states(struct pp_hwmgr *hwmgr,
4557 struct fiji_single_dpm_table * dpm_table,
4558 uint32_t low_limit, uint32_t high_limit)
4559{
4560 uint32_t i;
4561
4562 for (i = 0; i < dpm_table->count; i++) {
4563 if ((dpm_table->dpm_levels[i].value < low_limit) ||
4564 (dpm_table->dpm_levels[i].value > high_limit))
4565 dpm_table->dpm_levels[i].enabled = false;
4566 else
4567 dpm_table->dpm_levels[i].enabled = true;
4568 }
4569 return 0;
4570}
4571
4572static int fiji_trim_dpm_states(struct pp_hwmgr *hwmgr,
4573 const struct fiji_power_state *fiji_ps)
4574{
4575 int result = 0;
4576 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4577 uint32_t high_limit_count;
4578
4579 PP_ASSERT_WITH_CODE((fiji_ps->performance_level_count >= 1),
4580 "power state did not have any performance level",
4581 return -1);
4582
4583 high_limit_count = (1 == fiji_ps->performance_level_count) ? 0 : 1;
4584
4585 fiji_trim_single_dpm_states(hwmgr,
4586 &(data->dpm_table.sclk_table),
4587 fiji_ps->performance_levels[0].engine_clock,
4588 fiji_ps->performance_levels[high_limit_count].engine_clock);
4589
4590 fiji_trim_single_dpm_states(hwmgr,
4591 &(data->dpm_table.mclk_table),
4592 fiji_ps->performance_levels[0].memory_clock,
4593 fiji_ps->performance_levels[high_limit_count].memory_clock);
4594
4595 return result;
4596}
4597
4598static int fiji_generate_dpm_level_enable_mask(
4599 struct pp_hwmgr *hwmgr, const void *input)
4600{
4601 int result;
4602 const struct phm_set_power_state_input *states =
4603 (const struct phm_set_power_state_input *)input;
4604 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4605 const struct fiji_power_state *fiji_ps =
4606 cast_const_phw_fiji_power_state(states->pnew_state);
4607
4608 result = fiji_trim_dpm_states(hwmgr, fiji_ps);
4609 if (result)
4610 return result;
4611
4612 data->dpm_level_enable_mask.sclk_dpm_enable_mask =
4613 fiji_get_dpm_level_enable_mask_value(&data->dpm_table.sclk_table);
4614 data->dpm_level_enable_mask.mclk_dpm_enable_mask =
4615 fiji_get_dpm_level_enable_mask_value(&data->dpm_table.mclk_table);
4616 data->last_mclk_dpm_enable_mask =
4617 data->dpm_level_enable_mask.mclk_dpm_enable_mask;
4618
4619 if (data->uvd_enabled) {
4620 if (data->dpm_level_enable_mask.mclk_dpm_enable_mask & 1)
4621 data->dpm_level_enable_mask.mclk_dpm_enable_mask &= 0xFFFFFFFE;
4622 }
4623
4624 data->dpm_level_enable_mask.pcie_dpm_enable_mask =
4625 fiji_get_dpm_level_enable_mask_value(&data->dpm_table.pcie_speed_table);
4626
4627 return 0;
4628}
4629
Eric Huang91c4c982015-11-20 15:58:11 -05004630int fiji_enable_disable_uvd_dpm(struct pp_hwmgr *hwmgr, bool enable)
4631{
4632 return smum_send_msg_to_smc(hwmgr->smumgr, enable ?
4633 (PPSMC_Msg)PPSMC_MSG_UVDDPM_Enable :
4634 (PPSMC_Msg)PPSMC_MSG_UVDDPM_Disable);
4635}
4636
4637int fiji_enable_disable_vce_dpm(struct pp_hwmgr *hwmgr, bool enable)
Eric Huangaabcb7c2015-08-26 16:52:28 -04004638{
4639 return smum_send_msg_to_smc(hwmgr->smumgr, enable?
4640 PPSMC_MSG_VCEDPM_Enable :
4641 PPSMC_MSG_VCEDPM_Disable);
4642}
4643
Eric Huang91c4c982015-11-20 15:58:11 -05004644int fiji_enable_disable_samu_dpm(struct pp_hwmgr *hwmgr, bool enable)
4645{
4646 return smum_send_msg_to_smc(hwmgr->smumgr, enable?
4647 PPSMC_MSG_SAMUDPM_Enable :
4648 PPSMC_MSG_SAMUDPM_Disable);
4649}
4650
4651int fiji_enable_disable_acp_dpm(struct pp_hwmgr *hwmgr, bool enable)
4652{
4653 return smum_send_msg_to_smc(hwmgr->smumgr, enable?
4654 PPSMC_MSG_ACPDPM_Enable :
4655 PPSMC_MSG_ACPDPM_Disable);
4656}
4657
4658int fiji_update_uvd_dpm(struct pp_hwmgr *hwmgr, bool bgate)
4659{
4660 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4661 uint32_t mm_boot_level_offset, mm_boot_level_value;
4662 struct phm_ppt_v1_information *table_info =
4663 (struct phm_ppt_v1_information *)(hwmgr->pptable);
4664
4665 if (!bgate) {
4666 data->smc_state_table.UvdBootLevel = 0;
4667 if (table_info->mm_dep_table->count > 0)
4668 data->smc_state_table.UvdBootLevel =
4669 (uint8_t) (table_info->mm_dep_table->count - 1);
4670 mm_boot_level_offset = data->dpm_table_start +
4671 offsetof(SMU73_Discrete_DpmTable, UvdBootLevel);
4672 mm_boot_level_offset /= 4;
4673 mm_boot_level_offset *= 4;
4674 mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
4675 CGS_IND_REG__SMC, mm_boot_level_offset);
4676 mm_boot_level_value &= 0x00FFFFFF;
4677 mm_boot_level_value |= data->smc_state_table.UvdBootLevel << 24;
4678 cgs_write_ind_register(hwmgr->device,
4679 CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
4680
4681 if (!phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4682 PHM_PlatformCaps_UVDDPM) ||
4683 phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4684 PHM_PlatformCaps_StablePState))
4685 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4686 PPSMC_MSG_UVDDPM_SetEnabledMask,
4687 (uint32_t)(1 << data->smc_state_table.UvdBootLevel));
4688 }
4689
4690 return fiji_enable_disable_uvd_dpm(hwmgr, !bgate);
4691}
4692
4693int fiji_update_vce_dpm(struct pp_hwmgr *hwmgr, const void *input)
Eric Huangaabcb7c2015-08-26 16:52:28 -04004694{
4695 const struct phm_set_power_state_input *states =
4696 (const struct phm_set_power_state_input *)input;
4697 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4698 const struct fiji_power_state *fiji_nps =
4699 cast_const_phw_fiji_power_state(states->pnew_state);
4700 const struct fiji_power_state *fiji_cps =
4701 cast_const_phw_fiji_power_state(states->pcurrent_state);
4702
4703 uint32_t mm_boot_level_offset, mm_boot_level_value;
4704 struct phm_ppt_v1_information *table_info =
4705 (struct phm_ppt_v1_information *)(hwmgr->pptable);
4706
4707 if (fiji_nps->vce_clks.evclk >0 &&
4708 (fiji_cps == NULL || fiji_cps->vce_clks.evclk == 0)) {
4709 data->smc_state_table.VceBootLevel =
4710 (uint8_t) (table_info->mm_dep_table->count - 1);
4711
4712 mm_boot_level_offset = data->dpm_table_start +
4713 offsetof(SMU73_Discrete_DpmTable, VceBootLevel);
4714 mm_boot_level_offset /= 4;
4715 mm_boot_level_offset *= 4;
4716 mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
4717 CGS_IND_REG__SMC, mm_boot_level_offset);
4718 mm_boot_level_value &= 0xFF00FFFF;
4719 mm_boot_level_value |= data->smc_state_table.VceBootLevel << 16;
4720 cgs_write_ind_register(hwmgr->device,
4721 CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
4722
4723 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4724 PHM_PlatformCaps_StablePState)) {
4725 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4726 PPSMC_MSG_VCEDPM_SetEnabledMask,
4727 (uint32_t)1 << data->smc_state_table.VceBootLevel);
4728
4729 fiji_enable_disable_vce_dpm(hwmgr, true);
4730 } else if (fiji_nps->vce_clks.evclk == 0 &&
4731 fiji_cps != NULL &&
4732 fiji_cps->vce_clks.evclk > 0)
4733 fiji_enable_disable_vce_dpm(hwmgr, false);
4734 }
4735
4736 return 0;
4737}
4738
Eric Huang91c4c982015-11-20 15:58:11 -05004739int fiji_update_samu_dpm(struct pp_hwmgr *hwmgr, bool bgate)
4740{
4741 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4742 uint32_t mm_boot_level_offset, mm_boot_level_value;
4743 struct phm_ppt_v1_information *table_info =
4744 (struct phm_ppt_v1_information *)(hwmgr->pptable);
4745
4746 if (!bgate) {
4747 data->smc_state_table.SamuBootLevel =
4748 (uint8_t) (table_info->mm_dep_table->count - 1);
4749 mm_boot_level_offset = data->dpm_table_start +
4750 offsetof(SMU73_Discrete_DpmTable, SamuBootLevel);
4751 mm_boot_level_offset /= 4;
4752 mm_boot_level_offset *= 4;
4753 mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
4754 CGS_IND_REG__SMC, mm_boot_level_offset);
4755 mm_boot_level_value &= 0xFFFFFF00;
4756 mm_boot_level_value |= data->smc_state_table.SamuBootLevel << 0;
4757 cgs_write_ind_register(hwmgr->device,
4758 CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
4759
4760 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4761 PHM_PlatformCaps_StablePState))
4762 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4763 PPSMC_MSG_SAMUDPM_SetEnabledMask,
4764 (uint32_t)(1 << data->smc_state_table.SamuBootLevel));
4765 }
4766
4767 return fiji_enable_disable_samu_dpm(hwmgr, !bgate);
4768}
4769
4770int fiji_update_acp_dpm(struct pp_hwmgr *hwmgr, bool bgate)
4771{
4772 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4773 uint32_t mm_boot_level_offset, mm_boot_level_value;
4774 struct phm_ppt_v1_information *table_info =
4775 (struct phm_ppt_v1_information *)(hwmgr->pptable);
4776
4777 if (!bgate) {
4778 data->smc_state_table.AcpBootLevel =
4779 (uint8_t) (table_info->mm_dep_table->count - 1);
4780 mm_boot_level_offset = data->dpm_table_start +
4781 offsetof(SMU73_Discrete_DpmTable, AcpBootLevel);
4782 mm_boot_level_offset /= 4;
4783 mm_boot_level_offset *= 4;
4784 mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
4785 CGS_IND_REG__SMC, mm_boot_level_offset);
4786 mm_boot_level_value &= 0xFFFF00FF;
4787 mm_boot_level_value |= data->smc_state_table.AcpBootLevel << 8;
4788 cgs_write_ind_register(hwmgr->device,
4789 CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
4790
4791 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4792 PHM_PlatformCaps_StablePState))
4793 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4794 PPSMC_MSG_ACPDPM_SetEnabledMask,
4795 (uint32_t)(1 << data->smc_state_table.AcpBootLevel));
4796 }
4797
4798 return fiji_enable_disable_acp_dpm(hwmgr, !bgate);
4799}
4800
Eric Huangaabcb7c2015-08-26 16:52:28 -04004801static int fiji_update_sclk_threshold(struct pp_hwmgr *hwmgr)
4802{
4803 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4804
4805 int result = 0;
4806 uint32_t low_sclk_interrupt_threshold = 0;
4807
4808 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4809 PHM_PlatformCaps_SclkThrottleLowNotification)
4810 && (hwmgr->gfx_arbiter.sclk_threshold !=
4811 data->low_sclk_interrupt_threshold)) {
4812 data->low_sclk_interrupt_threshold =
4813 hwmgr->gfx_arbiter.sclk_threshold;
4814 low_sclk_interrupt_threshold =
4815 data->low_sclk_interrupt_threshold;
4816
4817 CONVERT_FROM_HOST_TO_SMC_UL(low_sclk_interrupt_threshold);
4818
4819 result = fiji_copy_bytes_to_smc(
4820 hwmgr->smumgr,
4821 data->dpm_table_start +
4822 offsetof(SMU73_Discrete_DpmTable,
4823 LowSclkInterruptThreshold),
4824 (uint8_t *)&low_sclk_interrupt_threshold,
4825 sizeof(uint32_t),
4826 data->sram_end);
4827 }
4828
4829 return result;
4830}
4831
4832static int fiji_program_mem_timing_parameters(struct pp_hwmgr *hwmgr)
4833{
4834 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4835
4836 if (data->need_update_smu7_dpm_table &
4837 (DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_OD_UPDATE_MCLK))
4838 return fiji_program_memory_timing_parameters(hwmgr);
4839
4840 return 0;
4841}
4842
4843static int fiji_unfreeze_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
4844{
4845 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4846
4847 if (0 == data->need_update_smu7_dpm_table)
4848 return 0;
4849
4850 if ((0 == data->sclk_dpm_key_disabled) &&
4851 (data->need_update_smu7_dpm_table &
4852 (DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_UPDATE_SCLK))) {
4853
4854 PP_ASSERT_WITH_CODE(true == fiji_is_dpm_running(hwmgr),
4855 "Trying to Unfreeze SCLK DPM when DPM is disabled",);
4856 PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr->smumgr,
4857 PPSMC_MSG_SCLKDPM_UnfreezeLevel),
4858 "Failed to unfreeze SCLK DPM during UnFreezeSclkMclkDPM Function!",
4859 return -1);
4860 }
4861
4862 if ((0 == data->mclk_dpm_key_disabled) &&
4863 (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
4864
4865 PP_ASSERT_WITH_CODE(true == fiji_is_dpm_running(hwmgr),
4866 "Trying to Unfreeze MCLK DPM when DPM is disabled",);
4867 PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr->smumgr,
4868 PPSMC_MSG_SCLKDPM_UnfreezeLevel),
4869 "Failed to unfreeze MCLK DPM during UnFreezeSclkMclkDPM Function!",
4870 return -1);
4871 }
4872
4873 data->need_update_smu7_dpm_table = 0;
4874
4875 return 0;
4876}
4877
4878/* Look up the voltaged based on DAL's requested level.
4879 * and then send the requested VDDC voltage to SMC
4880 */
4881static void fiji_apply_dal_minimum_voltage_request(struct pp_hwmgr *hwmgr)
4882{
4883 return;
4884}
4885
4886int fiji_upload_dpm_level_enable_mask(struct pp_hwmgr *hwmgr)
4887{
4888 int result;
4889 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4890
4891 /* Apply minimum voltage based on DAL's request level */
4892 fiji_apply_dal_minimum_voltage_request(hwmgr);
4893
4894 if (0 == data->sclk_dpm_key_disabled) {
4895 /* Checking if DPM is running. If we discover hang because of this,
4896 * we should skip this message.
4897 */
4898 if (!fiji_is_dpm_running(hwmgr))
4899 printk(KERN_ERR "[ powerplay ] "
4900 "Trying to set Enable Mask when DPM is disabled \n");
4901
4902 if (data->dpm_level_enable_mask.sclk_dpm_enable_mask) {
4903 result = smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4904 PPSMC_MSG_SCLKDPM_SetEnabledMask,
4905 data->dpm_level_enable_mask.sclk_dpm_enable_mask);
4906 PP_ASSERT_WITH_CODE((0 == result),
4907 "Set Sclk Dpm enable Mask failed", return -1);
4908 }
4909 }
4910
4911 if (0 == data->mclk_dpm_key_disabled) {
4912 /* Checking if DPM is running. If we discover hang because of this,
4913 * we should skip this message.
4914 */
4915 if (!fiji_is_dpm_running(hwmgr))
4916 printk(KERN_ERR "[ powerplay ]"
4917 " Trying to set Enable Mask when DPM is disabled \n");
4918
4919 if (data->dpm_level_enable_mask.mclk_dpm_enable_mask) {
4920 result = smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4921 PPSMC_MSG_MCLKDPM_SetEnabledMask,
4922 data->dpm_level_enable_mask.mclk_dpm_enable_mask);
4923 PP_ASSERT_WITH_CODE((0 == result),
4924 "Set Mclk Dpm enable Mask failed", return -1);
4925 }
4926 }
4927
4928 return 0;
4929}
4930
4931static int fiji_notify_link_speed_change_after_state_change(
4932 struct pp_hwmgr *hwmgr, const void *input)
4933{
4934 const struct phm_set_power_state_input *states =
4935 (const struct phm_set_power_state_input *)input;
4936 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
4937 const struct fiji_power_state *fiji_ps =
4938 cast_const_phw_fiji_power_state(states->pnew_state);
4939 uint16_t target_link_speed = fiji_get_maximum_link_speed(hwmgr, fiji_ps);
4940 uint8_t request;
4941
4942 if (data->pspp_notify_required) {
4943 if (target_link_speed == PP_PCIEGen3)
4944 request = PCIE_PERF_REQ_GEN3;
4945 else if (target_link_speed == PP_PCIEGen2)
4946 request = PCIE_PERF_REQ_GEN2;
4947 else
4948 request = PCIE_PERF_REQ_GEN1;
4949
4950 if(request == PCIE_PERF_REQ_GEN1 &&
4951 fiji_get_current_pcie_speed(hwmgr) > 0)
4952 return 0;
4953
4954 if (acpi_pcie_perf_request(hwmgr->device, request, false)) {
4955 if (PP_PCIEGen2 == target_link_speed)
4956 printk("PSPP request to switch to Gen2 from Gen3 Failed!");
4957 else
4958 printk("PSPP request to switch to Gen1 from Gen2 Failed!");
4959 }
4960 }
4961
4962 return 0;
4963}
4964
4965static int fiji_set_power_state_tasks(struct pp_hwmgr *hwmgr,
4966 const void *input)
4967{
4968 int tmp_result, result = 0;
4969
4970 tmp_result = fiji_find_dpm_states_clocks_in_dpm_table(hwmgr, input);
4971 PP_ASSERT_WITH_CODE((0 == tmp_result),
4972 "Failed to find DPM states clocks in DPM table!",
4973 result = tmp_result);
4974
4975 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4976 PHM_PlatformCaps_PCIEPerformanceRequest)) {
4977 tmp_result =
4978 fiji_request_link_speed_change_before_state_change(hwmgr, input);
4979 PP_ASSERT_WITH_CODE((0 == tmp_result),
4980 "Failed to request link speed change before state change!",
4981 result = tmp_result);
4982 }
4983
4984 tmp_result = fiji_freeze_sclk_mclk_dpm(hwmgr);
4985 PP_ASSERT_WITH_CODE((0 == tmp_result),
4986 "Failed to freeze SCLK MCLK DPM!", result = tmp_result);
4987
4988 tmp_result = fiji_populate_and_upload_sclk_mclk_dpm_levels(hwmgr, input);
4989 PP_ASSERT_WITH_CODE((0 == tmp_result),
4990 "Failed to populate and upload SCLK MCLK DPM levels!",
4991 result = tmp_result);
4992
4993 tmp_result = fiji_generate_dpm_level_enable_mask(hwmgr, input);
4994 PP_ASSERT_WITH_CODE((0 == tmp_result),
4995 "Failed to generate DPM level enabled mask!",
4996 result = tmp_result);
4997
4998 tmp_result = fiji_update_vce_dpm(hwmgr, input);
4999 PP_ASSERT_WITH_CODE((0 == tmp_result),
5000 "Failed to update VCE DPM!",
5001 result = tmp_result);
5002
5003 tmp_result = fiji_update_sclk_threshold(hwmgr);
5004 PP_ASSERT_WITH_CODE((0 == tmp_result),
5005 "Failed to update SCLK threshold!",
5006 result = tmp_result);
5007
5008 tmp_result = fiji_program_mem_timing_parameters(hwmgr);
5009 PP_ASSERT_WITH_CODE((0 == tmp_result),
5010 "Failed to program memory timing parameters!",
5011 result = tmp_result);
5012
5013 tmp_result = fiji_unfreeze_sclk_mclk_dpm(hwmgr);
5014 PP_ASSERT_WITH_CODE((0 == tmp_result),
5015 "Failed to unfreeze SCLK MCLK DPM!",
5016 result = tmp_result);
5017
5018 tmp_result = fiji_upload_dpm_level_enable_mask(hwmgr);
5019 PP_ASSERT_WITH_CODE((0 == tmp_result),
5020 "Failed to upload DPM level enabled mask!",
5021 result = tmp_result);
5022
5023 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
5024 PHM_PlatformCaps_PCIEPerformanceRequest)) {
5025 tmp_result =
5026 fiji_notify_link_speed_change_after_state_change(hwmgr, input);
5027 PP_ASSERT_WITH_CODE((0 == tmp_result),
5028 "Failed to notify link speed change after state change!",
5029 result = tmp_result);
5030 }
5031
5032 return result;
5033}
5034
5035static int fiji_dpm_get_sclk(struct pp_hwmgr *hwmgr, bool low)
5036{
5037 struct pp_power_state *ps;
5038 struct fiji_power_state *fiji_ps;
5039
5040 if (hwmgr == NULL)
5041 return -EINVAL;
5042
5043 ps = hwmgr->request_ps;
5044
5045 if (ps == NULL)
5046 return -EINVAL;
5047
5048 fiji_ps = cast_phw_fiji_power_state(&ps->hardware);
5049
5050 if (low)
5051 return fiji_ps->performance_levels[0].engine_clock;
5052 else
5053 return fiji_ps->performance_levels
5054 [fiji_ps->performance_level_count-1].engine_clock;
5055}
5056
5057static int fiji_dpm_get_mclk(struct pp_hwmgr *hwmgr, bool low)
5058{
5059 struct pp_power_state *ps;
5060 struct fiji_power_state *fiji_ps;
5061
5062 if (hwmgr == NULL)
5063 return -EINVAL;
5064
5065 ps = hwmgr->request_ps;
5066
5067 if (ps == NULL)
5068 return -EINVAL;
5069
5070 fiji_ps = cast_phw_fiji_power_state(&ps->hardware);
5071
5072 if (low)
5073 return fiji_ps->performance_levels[0].memory_clock;
5074 else
5075 return fiji_ps->performance_levels
5076 [fiji_ps->performance_level_count-1].memory_clock;
5077}
5078
5079static void fiji_print_current_perforce_level(
5080 struct pp_hwmgr *hwmgr, struct seq_file *m)
5081{
Rex Zhu0cfd9f22016-01-06 17:15:59 +08005082 uint32_t sclk, mclk, activity_percent = 0;
5083 uint32_t offset;
5084 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
Eric Huangaabcb7c2015-08-26 16:52:28 -04005085
5086 smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_API_GetSclkFrequency);
5087
5088 sclk = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
5089
5090 smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_API_GetMclkFrequency);
5091
5092 mclk = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
5093 seq_printf(m, "\n [ mclk ]: %u MHz\n\n [ sclk ]: %u MHz\n",
5094 mclk / 100, sclk / 100);
Rex Zhu0cfd9f22016-01-06 17:15:59 +08005095
5096 offset = data->soft_regs_start + offsetof(SMU73_SoftRegisters, AverageGraphicsActivity);
5097 activity_percent = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset);
5098 activity_percent += 0x80;
5099 activity_percent >>= 8;
5100
5101 seq_printf(m, "\n [GPU load]: %u%%\n\n", activity_percent > 100 ? 100 : activity_percent);
Rex Zhu66f48542016-03-01 17:02:51 +08005102
5103 seq_printf(m, "uvd %sabled\n", data->uvd_power_gated ? "dis" : "en");
5104
5105 seq_printf(m, "vce %sabled\n", data->vce_power_gated ? "dis" : "en");
Eric Huangaabcb7c2015-08-26 16:52:28 -04005106}
5107
Eric Huangea617bc2015-11-24 17:00:56 -05005108static int fiji_program_display_gap(struct pp_hwmgr *hwmgr)
5109{
5110 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5111 uint32_t num_active_displays = 0;
5112 uint32_t display_gap = cgs_read_ind_register(hwmgr->device,
5113 CGS_IND_REG__SMC, ixCG_DISPLAY_GAP_CNTL);
5114 uint32_t display_gap2;
5115 uint32_t pre_vbi_time_in_us;
5116 uint32_t frame_time_in_us;
5117 uint32_t ref_clock;
5118 uint32_t refresh_rate = 0;
5119 struct cgs_display_info info = {0};
5120 struct cgs_mode_info mode_info;
5121
5122 info.mode_info = &mode_info;
5123
5124 cgs_get_active_displays_info(hwmgr->device, &info);
5125 num_active_displays = info.display_count;
5126
5127 display_gap = PHM_SET_FIELD(display_gap, CG_DISPLAY_GAP_CNTL,
5128 DISP_GAP, (num_active_displays > 0)?
5129 DISPLAY_GAP_VBLANK_OR_WM : DISPLAY_GAP_IGNORE);
5130 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
5131 ixCG_DISPLAY_GAP_CNTL, display_gap);
5132
5133 ref_clock = mode_info.ref_clock;
5134 refresh_rate = mode_info.refresh_rate;
5135
5136 if (refresh_rate == 0)
5137 refresh_rate = 60;
5138
5139 frame_time_in_us = 1000000 / refresh_rate;
5140
5141 pre_vbi_time_in_us = frame_time_in_us - 200 - mode_info.vblank_time_us;
5142 display_gap2 = pre_vbi_time_in_us * (ref_clock / 100);
5143
5144 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
5145 ixCG_DISPLAY_GAP_CNTL2, display_gap2);
5146
5147 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
5148 data->soft_regs_start +
5149 offsetof(SMU73_SoftRegisters, PreVBlankGap), 0x64);
5150
5151 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
5152 data->soft_regs_start +
5153 offsetof(SMU73_SoftRegisters, VBlankTimeout),
5154 (frame_time_in_us - pre_vbi_time_in_us));
5155
5156 if (num_active_displays == 1)
5157 tonga_notify_smc_display_change(hwmgr, true);
5158
5159 return 0;
5160}
5161
5162int fiji_display_configuration_changed_task(struct pp_hwmgr *hwmgr)
5163{
5164 return fiji_program_display_gap(hwmgr);
5165}
5166
Eric Huang60103812015-11-27 14:09:53 -05005167static int fiji_set_max_fan_pwm_output(struct pp_hwmgr *hwmgr,
5168 uint16_t us_max_fan_pwm)
5169{
5170 hwmgr->thermal_controller.
5171 advanceFanControlParameters.usMaxFanPWM = us_max_fan_pwm;
5172
5173 if (phm_is_hw_access_blocked(hwmgr))
5174 return 0;
5175
5176 return smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
5177 PPSMC_MSG_SetFanPwmMax, us_max_fan_pwm);
5178}
5179
5180static int fiji_set_max_fan_rpm_output(struct pp_hwmgr *hwmgr,
5181 uint16_t us_max_fan_rpm)
5182{
5183 hwmgr->thermal_controller.
5184 advanceFanControlParameters.usMaxFanRPM = us_max_fan_rpm;
5185
5186 if (phm_is_hw_access_blocked(hwmgr))
5187 return 0;
5188
5189 return smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
5190 PPSMC_MSG_SetFanRpmMax, us_max_fan_rpm);
5191}
5192
5193int fiji_dpm_set_interrupt_state(void *private_data,
5194 unsigned src_id, unsigned type,
5195 int enabled)
5196{
5197 uint32_t cg_thermal_int;
5198 struct pp_hwmgr *hwmgr = ((struct pp_eventmgr *)private_data)->hwmgr;
5199
5200 if (hwmgr == NULL)
5201 return -EINVAL;
5202
5203 switch (type) {
5204 case AMD_THERMAL_IRQ_LOW_TO_HIGH:
5205 if (enabled) {
5206 cg_thermal_int = cgs_read_ind_register(hwmgr->device,
5207 CGS_IND_REG__SMC, ixCG_THERMAL_INT);
5208 cg_thermal_int |= CG_THERMAL_INT_CTRL__THERM_INTH_MASK_MASK;
5209 cgs_write_ind_register(hwmgr->device,
5210 CGS_IND_REG__SMC, ixCG_THERMAL_INT, cg_thermal_int);
5211 } else {
5212 cg_thermal_int = cgs_read_ind_register(hwmgr->device,
5213 CGS_IND_REG__SMC, ixCG_THERMAL_INT);
5214 cg_thermal_int &= ~CG_THERMAL_INT_CTRL__THERM_INTH_MASK_MASK;
5215 cgs_write_ind_register(hwmgr->device,
5216 CGS_IND_REG__SMC, ixCG_THERMAL_INT, cg_thermal_int);
5217 }
5218 break;
5219
5220 case AMD_THERMAL_IRQ_HIGH_TO_LOW:
5221 if (enabled) {
5222 cg_thermal_int = cgs_read_ind_register(hwmgr->device,
5223 CGS_IND_REG__SMC, ixCG_THERMAL_INT);
5224 cg_thermal_int |= CG_THERMAL_INT_CTRL__THERM_INTL_MASK_MASK;
5225 cgs_write_ind_register(hwmgr->device,
5226 CGS_IND_REG__SMC, ixCG_THERMAL_INT, cg_thermal_int);
5227 } else {
5228 cg_thermal_int = cgs_read_ind_register(hwmgr->device,
5229 CGS_IND_REG__SMC, ixCG_THERMAL_INT);
5230 cg_thermal_int &= ~CG_THERMAL_INT_CTRL__THERM_INTL_MASK_MASK;
5231 cgs_write_ind_register(hwmgr->device,
5232 CGS_IND_REG__SMC, ixCG_THERMAL_INT, cg_thermal_int);
5233 }
5234 break;
5235 default:
5236 break;
5237 }
5238 return 0;
5239}
5240
5241int fiji_register_internal_thermal_interrupt(struct pp_hwmgr *hwmgr,
5242 const void *thermal_interrupt_info)
5243{
5244 int result;
5245 const struct pp_interrupt_registration_info *info =
5246 (const struct pp_interrupt_registration_info *)
5247 thermal_interrupt_info;
5248
5249 if (info == NULL)
5250 return -EINVAL;
5251
5252 result = cgs_add_irq_source(hwmgr->device, 230, AMD_THERMAL_IRQ_LAST,
5253 fiji_dpm_set_interrupt_state,
5254 info->call_back, info->context);
5255
5256 if (result)
5257 return -EINVAL;
5258
5259 result = cgs_add_irq_source(hwmgr->device, 231, AMD_THERMAL_IRQ_LAST,
5260 fiji_dpm_set_interrupt_state,
5261 info->call_back, info->context);
5262
5263 if (result)
5264 return -EINVAL;
5265
5266 return 0;
5267}
5268
Eric Huangdb18ce32015-12-04 15:49:02 -05005269static int fiji_set_fan_control_mode(struct pp_hwmgr *hwmgr, uint32_t mode)
5270{
5271 if (mode) {
5272 /* stop auto-manage */
5273 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
5274 PHM_PlatformCaps_MicrocodeFanControl))
5275 fiji_fan_ctrl_stop_smc_fan_control(hwmgr);
5276 fiji_fan_ctrl_set_static_mode(hwmgr, mode);
5277 } else
5278 /* restart auto-manage */
5279 fiji_fan_ctrl_reset_fan_speed_to_default(hwmgr);
5280
5281 return 0;
5282}
5283
5284static int fiji_get_fan_control_mode(struct pp_hwmgr *hwmgr)
5285{
5286 if (hwmgr->fan_ctrl_is_in_default_mode)
5287 return hwmgr->fan_ctrl_default_mode;
5288 else
5289 return PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
5290 CG_FDO_CTRL2, FDO_PWM_MODE);
5291}
5292
Eric Huangb9c1a772015-12-14 13:49:37 -05005293static int fiji_get_pp_table(struct pp_hwmgr *hwmgr, char **table)
5294{
5295 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5296
Eric Huang65ba4f22016-04-15 16:33:20 -04005297 if (!data->soft_pp_table) {
Muhammad Falak R Wani761de5f2016-05-19 16:45:27 +05305298 data->soft_pp_table = kmemdup(hwmgr->soft_pp_table,
5299 hwmgr->soft_pp_table_size,
5300 GFP_KERNEL);
Eric Huang65ba4f22016-04-15 16:33:20 -04005301 if (!data->soft_pp_table)
5302 return -ENOMEM;
Eric Huang65ba4f22016-04-15 16:33:20 -04005303 }
Eric Huangb9c1a772015-12-14 13:49:37 -05005304
Eric Huang65ba4f22016-04-15 16:33:20 -04005305 *table = (char *)&data->soft_pp_table;
5306
5307 return hwmgr->soft_pp_table_size;
Eric Huangb9c1a772015-12-14 13:49:37 -05005308}
5309
5310static int fiji_set_pp_table(struct pp_hwmgr *hwmgr, const char *buf, size_t size)
5311{
5312 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5313
Eric Huang65ba4f22016-04-15 16:33:20 -04005314 if (!data->soft_pp_table) {
5315 data->soft_pp_table = kzalloc(hwmgr->soft_pp_table_size, GFP_KERNEL);
5316 if (!data->soft_pp_table)
5317 return -ENOMEM;
5318 }
Eric Huangb9c1a772015-12-14 13:49:37 -05005319
Eric Huang65ba4f22016-04-15 16:33:20 -04005320 memcpy(data->soft_pp_table, buf, size);
5321
5322 hwmgr->soft_pp_table = data->soft_pp_table;
5323
5324 /* TODO: re-init powerplay to implement modified pptable */
Eric Huangb9c1a772015-12-14 13:49:37 -05005325
5326 return 0;
5327}
5328
5329static int fiji_force_clock_level(struct pp_hwmgr *hwmgr,
Eric Huang56327082016-04-12 14:57:23 -04005330 enum pp_clock_type type, uint32_t mask)
Eric Huangb9c1a772015-12-14 13:49:37 -05005331{
5332 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5333
5334 if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
5335 return -EINVAL;
5336
5337 switch (type) {
5338 case PP_SCLK:
5339 if (!data->sclk_dpm_key_disabled)
5340 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
5341 PPSMC_MSG_SCLKDPM_SetEnabledMask,
Eric Huang56327082016-04-12 14:57:23 -04005342 data->dpm_level_enable_mask.sclk_dpm_enable_mask & mask);
Eric Huangb9c1a772015-12-14 13:49:37 -05005343 break;
Eric Huang56327082016-04-12 14:57:23 -04005344
Eric Huangb9c1a772015-12-14 13:49:37 -05005345 case PP_MCLK:
5346 if (!data->mclk_dpm_key_disabled)
5347 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
5348 PPSMC_MSG_MCLKDPM_SetEnabledMask,
Eric Huang56327082016-04-12 14:57:23 -04005349 data->dpm_level_enable_mask.mclk_dpm_enable_mask & mask);
Eric Huangb9c1a772015-12-14 13:49:37 -05005350 break;
Eric Huang56327082016-04-12 14:57:23 -04005351
Eric Huangb9c1a772015-12-14 13:49:37 -05005352 case PP_PCIE:
Eric Huang56327082016-04-12 14:57:23 -04005353 {
5354 uint32_t tmp = mask & data->dpm_level_enable_mask.pcie_dpm_enable_mask;
5355 uint32_t level = 0;
5356
5357 while (tmp >>= 1)
5358 level++;
5359
Eric Huangb9c1a772015-12-14 13:49:37 -05005360 if (!data->pcie_dpm_key_disabled)
5361 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
5362 PPSMC_MSG_PCIeDPM_ForceLevel,
Eric Huang56327082016-04-12 14:57:23 -04005363 level);
Eric Huangb9c1a772015-12-14 13:49:37 -05005364 break;
Eric Huang56327082016-04-12 14:57:23 -04005365 }
Eric Huangb9c1a772015-12-14 13:49:37 -05005366 default:
5367 break;
5368 }
5369
5370 return 0;
5371}
5372
5373static int fiji_print_clock_levels(struct pp_hwmgr *hwmgr,
5374 enum pp_clock_type type, char *buf)
5375{
5376 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5377 struct fiji_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
5378 struct fiji_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
5379 struct fiji_single_dpm_table *pcie_table = &(data->dpm_table.pcie_speed_table);
5380 int i, now, size = 0;
5381 uint32_t clock, pcie_speed;
5382
5383 switch (type) {
5384 case PP_SCLK:
5385 smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_API_GetSclkFrequency);
5386 clock = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
5387
5388 for (i = 0; i < sclk_table->count; i++) {
5389 if (clock > sclk_table->dpm_levels[i].value)
5390 continue;
5391 break;
5392 }
5393 now = i;
5394
5395 for (i = 0; i < sclk_table->count; i++)
5396 size += sprintf(buf + size, "%d: %uMhz %s\n",
5397 i, sclk_table->dpm_levels[i].value / 100,
5398 (i == now) ? "*" : "");
5399 break;
5400 case PP_MCLK:
5401 smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_API_GetMclkFrequency);
5402 clock = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
5403
5404 for (i = 0; i < mclk_table->count; i++) {
5405 if (clock > mclk_table->dpm_levels[i].value)
5406 continue;
5407 break;
5408 }
5409 now = i;
5410
5411 for (i = 0; i < mclk_table->count; i++)
5412 size += sprintf(buf + size, "%d: %uMhz %s\n",
5413 i, mclk_table->dpm_levels[i].value / 100,
5414 (i == now) ? "*" : "");
5415 break;
5416 case PP_PCIE:
5417 pcie_speed = fiji_get_current_pcie_speed(hwmgr);
5418 for (i = 0; i < pcie_table->count; i++) {
5419 if (pcie_speed != pcie_table->dpm_levels[i].value)
5420 continue;
5421 break;
5422 }
5423 now = i;
5424
5425 for (i = 0; i < pcie_table->count; i++)
5426 size += sprintf(buf + size, "%d: %s %s\n", i,
5427 (pcie_table->dpm_levels[i].value == 0) ? "2.5GB, x1" :
5428 (pcie_table->dpm_levels[i].value == 1) ? "5.0GB, x16" :
5429 (pcie_table->dpm_levels[i].value == 2) ? "8.0GB, x16" : "",
5430 (i == now) ? "*" : "");
5431 break;
5432 default:
5433 break;
5434 }
5435 return size;
5436}
5437
Rex Zhubbe6aa92016-03-29 18:31:43 +08005438static inline bool fiji_are_power_levels_equal(const struct fiji_performance_level *pl1,
5439 const struct fiji_performance_level *pl2)
5440{
5441 return ((pl1->memory_clock == pl2->memory_clock) &&
5442 (pl1->engine_clock == pl2->engine_clock) &&
5443 (pl1->pcie_gen == pl2->pcie_gen) &&
5444 (pl1->pcie_lane == pl2->pcie_lane));
5445}
5446
5447int fiji_check_states_equal(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *pstate1, const struct pp_hw_power_state *pstate2, bool *equal)
5448{
5449 const struct fiji_power_state *psa = cast_const_phw_fiji_power_state(pstate1);
5450 const struct fiji_power_state *psb = cast_const_phw_fiji_power_state(pstate2);
5451 int i;
5452
5453 if (equal == NULL || psa == NULL || psb == NULL)
5454 return -EINVAL;
5455
5456 /* If the two states don't even have the same number of performance levels they cannot be the same state. */
5457 if (psa->performance_level_count != psb->performance_level_count) {
5458 *equal = false;
5459 return 0;
5460 }
5461
5462 for (i = 0; i < psa->performance_level_count; i++) {
5463 if (!fiji_are_power_levels_equal(&(psa->performance_levels[i]), &(psb->performance_levels[i]))) {
5464 /* If we have found even one performance level pair that is different the states are different. */
5465 *equal = false;
5466 return 0;
5467 }
5468 }
5469
5470 /* If all performance levels are the same try to use the UVD clocks to break the tie.*/
5471 *equal = ((psa->uvd_clks.vclk == psb->uvd_clks.vclk) && (psa->uvd_clks.dclk == psb->uvd_clks.dclk));
5472 *equal &= ((psa->vce_clks.evclk == psb->vce_clks.evclk) && (psa->vce_clks.ecclk == psb->vce_clks.ecclk));
5473 *equal &= (psa->sclk_threshold == psb->sclk_threshold);
5474 *equal &= (psa->acp_clk == psb->acp_clk);
5475
5476 return 0;
5477}
5478
5479bool fiji_check_smc_update_required_for_display_configuration(struct pp_hwmgr *hwmgr)
5480{
5481 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5482 bool is_update_required = false;
5483 struct cgs_display_info info = {0,0,NULL};
5484
5485 cgs_get_active_displays_info(hwmgr->device, &info);
5486
5487 if (data->display_timing.num_existing_displays != info.display_count)
5488 is_update_required = true;
Rex Zhu0eadeab2016-03-29 19:32:37 +08005489
5490 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep)) {
5491 if(hwmgr->display_config.min_core_set_clock_in_sr != data->display_timing.min_clock_in_sr)
Rex Zhubbe6aa92016-03-29 18:31:43 +08005492 is_update_required = true;
Rex Zhu0eadeab2016-03-29 19:32:37 +08005493 }
5494
Rex Zhubbe6aa92016-03-29 18:31:43 +08005495 return is_update_required;
5496}
5497
Eric Huangdecb5fb2016-05-12 15:06:10 -04005498static int fiji_get_sclk_od(struct pp_hwmgr *hwmgr)
5499{
5500 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5501 struct fiji_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
5502 struct fiji_single_dpm_table *golden_sclk_table =
5503 &(data->golden_dpm_table.sclk_table);
5504 int value;
5505
5506 value = (sclk_table->dpm_levels[sclk_table->count - 1].value -
5507 golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value) *
5508 100 /
5509 golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value;
5510
5511 return value;
5512}
5513
5514static int fiji_set_sclk_od(struct pp_hwmgr *hwmgr, uint32_t value)
5515{
5516 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5517 struct fiji_single_dpm_table *golden_sclk_table =
5518 &(data->golden_dpm_table.sclk_table);
5519 struct pp_power_state *ps;
5520 struct fiji_power_state *fiji_ps;
5521
5522 if (value > 20)
5523 value = 20;
5524
5525 ps = hwmgr->request_ps;
5526
5527 if (ps == NULL)
5528 return -EINVAL;
5529
5530 fiji_ps = cast_phw_fiji_power_state(&ps->hardware);
5531
5532 fiji_ps->performance_levels[fiji_ps->performance_level_count - 1].engine_clock =
5533 golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value *
5534 value / 100 +
5535 golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value;
5536
5537 return 0;
5538}
Rex Zhubbe6aa92016-03-29 18:31:43 +08005539
Eric Huang391be532016-05-24 16:14:50 -04005540static int fiji_get_mclk_od(struct pp_hwmgr *hwmgr)
5541{
5542 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5543 struct fiji_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
5544 struct fiji_single_dpm_table *golden_mclk_table =
5545 &(data->golden_dpm_table.mclk_table);
5546 int value;
5547
5548 value = (mclk_table->dpm_levels[mclk_table->count - 1].value -
5549 golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value) *
5550 100 /
5551 golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value;
5552
5553 return value;
5554}
5555
5556static int fiji_set_mclk_od(struct pp_hwmgr *hwmgr, uint32_t value)
5557{
5558 struct fiji_hwmgr *data = (struct fiji_hwmgr *)(hwmgr->backend);
5559 struct fiji_single_dpm_table *golden_mclk_table =
5560 &(data->golden_dpm_table.mclk_table);
5561 struct pp_power_state *ps;
5562 struct fiji_power_state *fiji_ps;
5563
5564 if (value > 20)
5565 value = 20;
5566
5567 ps = hwmgr->request_ps;
5568
5569 if (ps == NULL)
5570 return -EINVAL;
5571
5572 fiji_ps = cast_phw_fiji_power_state(&ps->hardware);
5573
5574 fiji_ps->performance_levels[fiji_ps->performance_level_count - 1].memory_clock =
5575 golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value *
5576 value / 100 +
5577 golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value;
5578
5579 return 0;
5580}
5581
Eric Huangaabcb7c2015-08-26 16:52:28 -04005582static const struct pp_hwmgr_func fiji_hwmgr_funcs = {
5583 .backend_init = &fiji_hwmgr_backend_init,
Eric Huang65ba4f22016-04-15 16:33:20 -04005584 .backend_fini = &fiji_hwmgr_backend_fini,
Eric Huangaabcb7c2015-08-26 16:52:28 -04005585 .asic_setup = &fiji_setup_asic_task,
5586 .dynamic_state_management_enable = &fiji_enable_dpm_tasks,
Eric Huang55d74fc2016-05-31 17:06:14 -04005587 .dynamic_state_management_disable = &fiji_disable_dpm_tasks,
Eric Huangaabcb7c2015-08-26 16:52:28 -04005588 .force_dpm_level = &fiji_dpm_force_dpm_level,
5589 .get_num_of_pp_table_entries = &tonga_get_number_of_powerplay_table_entries,
5590 .get_power_state_size = &fiji_get_power_state_size,
5591 .get_pp_table_entry = &fiji_get_pp_table_entry,
5592 .patch_boot_state = &fiji_patch_boot_state,
5593 .apply_state_adjust_rules = &fiji_apply_state_adjust_rules,
5594 .power_state_set = &fiji_set_power_state_tasks,
5595 .get_sclk = &fiji_dpm_get_sclk,
5596 .get_mclk = &fiji_dpm_get_mclk,
5597 .print_current_perforce_level = &fiji_print_current_perforce_level,
Eric Huang91c4c982015-11-20 15:58:11 -05005598 .powergate_uvd = &fiji_phm_powergate_uvd,
5599 .powergate_vce = &fiji_phm_powergate_vce,
5600 .disable_clock_power_gating = &fiji_phm_disable_clock_power_gating,
Eric Huangea617bc2015-11-24 17:00:56 -05005601 .notify_smc_display_config_after_ps_adjustment =
5602 &tonga_notify_smc_display_config_after_ps_adjustment,
5603 .display_config_changed = &fiji_display_configuration_changed_task,
Eric Huang60103812015-11-27 14:09:53 -05005604 .set_max_fan_pwm_output = fiji_set_max_fan_pwm_output,
5605 .set_max_fan_rpm_output = fiji_set_max_fan_rpm_output,
5606 .get_temperature = fiji_thermal_get_temperature,
5607 .stop_thermal_controller = fiji_thermal_stop_thermal_controller,
5608 .get_fan_speed_info = fiji_fan_ctrl_get_fan_speed_info,
5609 .get_fan_speed_percent = fiji_fan_ctrl_get_fan_speed_percent,
5610 .set_fan_speed_percent = fiji_fan_ctrl_set_fan_speed_percent,
5611 .reset_fan_speed_to_default = fiji_fan_ctrl_reset_fan_speed_to_default,
5612 .get_fan_speed_rpm = fiji_fan_ctrl_get_fan_speed_rpm,
5613 .set_fan_speed_rpm = fiji_fan_ctrl_set_fan_speed_rpm,
5614 .uninitialize_thermal_controller = fiji_thermal_ctrl_uninitialize_thermal_controller,
5615 .register_internal_thermal_interrupt = fiji_register_internal_thermal_interrupt,
Eric Huangdb18ce32015-12-04 15:49:02 -05005616 .set_fan_control_mode = fiji_set_fan_control_mode,
5617 .get_fan_control_mode = fiji_get_fan_control_mode,
Rex Zhubbe6aa92016-03-29 18:31:43 +08005618 .check_states_equal = fiji_check_states_equal,
5619 .check_smc_update_required_for_display_configuration = fiji_check_smc_update_required_for_display_configuration,
Eric Huangb9c1a772015-12-14 13:49:37 -05005620 .get_pp_table = fiji_get_pp_table,
5621 .set_pp_table = fiji_set_pp_table,
5622 .force_clock_level = fiji_force_clock_level,
5623 .print_clock_levels = fiji_print_clock_levels,
Eric Huangdecb5fb2016-05-12 15:06:10 -04005624 .get_sclk_od = fiji_get_sclk_od,
5625 .set_sclk_od = fiji_set_sclk_od,
Eric Huang391be532016-05-24 16:14:50 -04005626 .get_mclk_od = fiji_get_mclk_od,
5627 .set_mclk_od = fiji_set_mclk_od,
Eric Huangaabcb7c2015-08-26 16:52:28 -04005628};
5629
5630int fiji_hwmgr_init(struct pp_hwmgr *hwmgr)
5631{
5632 struct fiji_hwmgr *data;
5633 int ret = 0;
5634
5635 data = kzalloc(sizeof(struct fiji_hwmgr), GFP_KERNEL);
5636 if (data == NULL)
5637 return -ENOMEM;
5638
5639 hwmgr->backend = data;
5640 hwmgr->hwmgr_func = &fiji_hwmgr_funcs;
5641 hwmgr->pptable_func = &tonga_pptable_funcs;
Eric Huang60103812015-11-27 14:09:53 -05005642 pp_fiji_thermal_initialize(hwmgr);
Eric Huangaabcb7c2015-08-26 16:52:28 -04005643 return ret;
5644}