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