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