blob: 417e525dc3748e4de5840a235a43ed8d7d753428 [file] [log] [blame]
Rex Zhua23eefa2015-11-19 18:23:32 +08001/*
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>
Slava Grigorevae17c992016-03-22 23:34:29 -040026#include <asm/div64.h>
Rex Zhua23eefa2015-11-19 18:23:32 +080027#include "linux/delay.h"
28#include "pp_acpi.h"
29#include "hwmgr.h"
Flora Cui2cc0c0b2016-03-14 18:33:29 -040030#include "polaris10_hwmgr.h"
31#include "polaris10_powertune.h"
32#include "polaris10_dyn_defaults.h"
33#include "polaris10_smumgr.h"
Rex Zhua23eefa2015-11-19 18:23:32 +080034#include "pp_debug.h"
35#include "ppatomctrl.h"
36#include "atombios.h"
37#include "tonga_pptable.h"
38#include "pppcielanes.h"
39#include "amd_pcie_helpers.h"
40#include "hardwaremanager.h"
41#include "tonga_processpptables.h"
42#include "cgs_common.h"
43#include "smu74.h"
44#include "smu_ucode_xfer_vi.h"
45#include "smu74_discrete.h"
46#include "smu/smu_7_1_3_d.h"
47#include "smu/smu_7_1_3_sh_mask.h"
48#include "gmc/gmc_8_1_d.h"
49#include "gmc/gmc_8_1_sh_mask.h"
50#include "oss/oss_3_0_d.h"
51#include "gca/gfx_8_0_d.h"
52#include "bif/bif_5_0_d.h"
53#include "bif/bif_5_0_sh_mask.h"
54#include "gmc/gmc_8_1_d.h"
55#include "gmc/gmc_8_1_sh_mask.h"
56#include "bif/bif_5_0_d.h"
57#include "bif/bif_5_0_sh_mask.h"
58#include "dce/dce_10_0_d.h"
59#include "dce/dce_10_0_sh_mask.h"
60
Flora Cui2cc0c0b2016-03-14 18:33:29 -040061#include "polaris10_thermal.h"
62#include "polaris10_clockpowergating.h"
Eric Huangeede5262016-02-02 16:09:24 -050063
Rex Zhua23eefa2015-11-19 18:23:32 +080064#define MC_CG_ARB_FREQ_F0 0x0a
65#define MC_CG_ARB_FREQ_F1 0x0b
66#define MC_CG_ARB_FREQ_F2 0x0c
67#define MC_CG_ARB_FREQ_F3 0x0d
68
69#define MC_CG_SEQ_DRAMCONF_S0 0x05
70#define MC_CG_SEQ_DRAMCONF_S1 0x06
71#define MC_CG_SEQ_YCLK_SUSPEND 0x04
72#define MC_CG_SEQ_YCLK_RESUME 0x0a
73
74
75#define SMC_RAM_END 0x40000
76
77#define SMC_CG_IND_START 0xc0030000
78#define SMC_CG_IND_END 0xc0040000
79
80#define VOLTAGE_SCALE 4
81#define VOLTAGE_VID_OFFSET_SCALE1 625
82#define VOLTAGE_VID_OFFSET_SCALE2 100
83
84#define VDDC_VDDCI_DELTA 200
85
86#define MEM_FREQ_LOW_LATENCY 25000
87#define MEM_FREQ_HIGH_LATENCY 80000
88
89#define MEM_LATENCY_HIGH 45
90#define MEM_LATENCY_LOW 35
91#define MEM_LATENCY_ERR 0xFFFF
92
93#define MC_SEQ_MISC0_GDDR5_SHIFT 28
94#define MC_SEQ_MISC0_GDDR5_MASK 0xf0000000
95#define MC_SEQ_MISC0_GDDR5_VALUE 5
96
97
98#define PCIE_BUS_CLK 10000
99#define TCLK (PCIE_BUS_CLK / 10)
100
101
Nils Wallménius909a0632016-04-25 21:31:34 +0200102static const uint16_t polaris10_clock_stretcher_lookup_table[2][4] =
103{ {600, 1050, 3, 0}, {600, 1050, 6, 1} };
Rex Zhua23eefa2015-11-19 18:23:32 +0800104
105/* [FF, SS] type, [] 4 voltage ranges, and [Floor Freq, Boundary Freq, VID min , VID max] */
Nils Wallménius909a0632016-04-25 21:31:34 +0200106static const uint32_t polaris10_clock_stretcher_ddt_table[2][4][4] =
107{ { {265, 529, 120, 128}, {325, 650, 96, 119}, {430, 860, 32, 95}, {0, 0, 0, 31} },
108 { {275, 550, 104, 112}, {319, 638, 96, 103}, {360, 720, 64, 95}, {384, 768, 32, 63} } };
Rex Zhua23eefa2015-11-19 18:23:32 +0800109
110/* [Use_For_Low_freq] value, [0%, 5%, 10%, 7.14%, 14.28%, 20%] (coming from PWR_CKS_CNTL.stretch_amount reg spec) */
Nils Wallménius909a0632016-04-25 21:31:34 +0200111static const uint8_t polaris10_clock_stretch_amount_conversion[2][6] =
112{ {0, 1, 3, 2, 4, 5}, {0, 2, 4, 5, 6, 5} };
Rex Zhua23eefa2015-11-19 18:23:32 +0800113
114/** Values for the CG_THERMAL_CTRL::DPM_EVENT_SRC field. */
115enum DPM_EVENT_SRC {
116 DPM_EVENT_SRC_ANALOG = 0,
117 DPM_EVENT_SRC_EXTERNAL = 1,
118 DPM_EVENT_SRC_DIGITAL = 2,
119 DPM_EVENT_SRC_ANALOG_OR_EXTERNAL = 3,
120 DPM_EVENT_SRC_DIGITAL_OR_EXTERNAL = 4
121};
122
Nils Wallménius909a0632016-04-25 21:31:34 +0200123static const unsigned long PhwPolaris10_Magic = (unsigned long)(PHM_VIslands_Magic);
Rex Zhua23eefa2015-11-19 18:23:32 +0800124
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400125struct polaris10_power_state *cast_phw_polaris10_power_state(
Rex Zhua23eefa2015-11-19 18:23:32 +0800126 struct pp_hw_power_state *hw_ps)
127{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400128 PP_ASSERT_WITH_CODE((PhwPolaris10_Magic == hw_ps->magic),
Rex Zhua23eefa2015-11-19 18:23:32 +0800129 "Invalid Powerstate Type!",
130 return NULL);
131
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400132 return (struct polaris10_power_state *)hw_ps;
Rex Zhua23eefa2015-11-19 18:23:32 +0800133}
134
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400135const struct polaris10_power_state *cast_const_phw_polaris10_power_state(
Rex Zhua23eefa2015-11-19 18:23:32 +0800136 const struct pp_hw_power_state *hw_ps)
137{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400138 PP_ASSERT_WITH_CODE((PhwPolaris10_Magic == hw_ps->magic),
Rex Zhua23eefa2015-11-19 18:23:32 +0800139 "Invalid Powerstate Type!",
140 return NULL);
141
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400142 return (const struct polaris10_power_state *)hw_ps;
Rex Zhua23eefa2015-11-19 18:23:32 +0800143}
144
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400145static bool polaris10_is_dpm_running(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800146{
147 return (1 == PHM_READ_INDIRECT_FIELD(hwmgr->device,
148 CGS_IND_REG__SMC, FEATURE_STATUS, VOLTAGE_CONTROLLER_ON))
149 ? true : false;
150}
151
152/**
153 * Find the MC microcode version and store it in the HwMgr struct
154 *
155 * @param hwmgr the address of the powerplay hardware manager.
156 * @return always 0
157 */
158int phm_get_mc_microcode_version (struct pp_hwmgr *hwmgr)
159{
160 cgs_write_register(hwmgr->device, mmMC_SEQ_IO_DEBUG_INDEX, 0x9F);
161
162 hwmgr->microcode_version_info.MC = cgs_read_register(hwmgr->device, mmMC_SEQ_IO_DEBUG_DATA);
163
164 return 0;
165}
166
167uint16_t phm_get_current_pcie_speed(struct pp_hwmgr *hwmgr)
168{
169 uint32_t speedCntl = 0;
170
171 /* mmPCIE_PORT_INDEX rename as mmPCIE_INDEX */
172 speedCntl = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__PCIE,
173 ixPCIE_LC_SPEED_CNTL);
174 return((uint16_t)PHM_GET_FIELD(speedCntl,
175 PCIE_LC_SPEED_CNTL, LC_CURRENT_DATA_RATE));
176}
177
178int phm_get_current_pcie_lane_number(struct pp_hwmgr *hwmgr)
179{
180 uint32_t link_width;
181
182 /* mmPCIE_PORT_INDEX rename as mmPCIE_INDEX */
183 link_width = PHM_READ_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__PCIE,
184 PCIE_LC_LINK_WIDTH_CNTL, LC_LINK_WIDTH_RD);
185
186 PP_ASSERT_WITH_CODE((7 >= link_width),
187 "Invalid PCIe lane width!", return 0);
188
189 return decode_pcie_lane_width(link_width);
190}
191
yanyang1e85c7d62016-02-06 13:28:47 +0800192/**
193* Enable voltage control
194*
195* @param pHwMgr the address of the powerplay hardware manager.
196* @return always PP_Result_OK
197*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400198int polaris10_enable_smc_voltage_controller(struct pp_hwmgr *hwmgr)
yanyang1e85c7d62016-02-06 13:28:47 +0800199{
200 PP_ASSERT_WITH_CODE(
201 (hwmgr->smumgr->smumgr_funcs->send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_Voltage_Cntl_Enable) == 0),
202 "Failed to enable voltage DPM during DPM Start Function!",
203 return 1;
204 );
205
206 return 0;
207}
Rex Zhua23eefa2015-11-19 18:23:32 +0800208
209/**
210* Checks if we want to support voltage control
211*
212* @param hwmgr the address of the powerplay hardware manager.
213*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400214static bool polaris10_voltage_control(const struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800215{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400216 const struct polaris10_hwmgr *data =
217 (const struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800218
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400219 return (POLARIS10_VOLTAGE_CONTROL_NONE != data->voltage_control);
Rex Zhua23eefa2015-11-19 18:23:32 +0800220}
221
222/**
223* Enable voltage control
224*
225* @param hwmgr the address of the powerplay hardware manager.
226* @return always 0
227*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400228static int polaris10_enable_voltage_control(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800229{
230 /* enable voltage control */
231 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
232 GENERAL_PWRMGT, VOLT_PWRMGT_EN, 1);
233
234 return 0;
235}
236
237/**
238* Create Voltage Tables.
239*
240* @param hwmgr the address of the powerplay hardware manager.
241* @return always 0
242*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400243static int polaris10_construct_voltage_tables(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800244{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400245 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800246 struct phm_ppt_v1_information *table_info =
247 (struct phm_ppt_v1_information *)hwmgr->pptable;
248 int result;
249
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400250 if (POLARIS10_VOLTAGE_CONTROL_BY_GPIO == data->mvdd_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +0800251 result = atomctrl_get_voltage_table_v3(hwmgr,
252 VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_GPIO_LUT,
253 &(data->mvdd_voltage_table));
254 PP_ASSERT_WITH_CODE((0 == result),
255 "Failed to retrieve MVDD table.",
256 return result);
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400257 } else if (POLARIS10_VOLTAGE_CONTROL_BY_SVID2 == data->mvdd_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +0800258 result = phm_get_svi2_mvdd_voltage_table(&(data->mvdd_voltage_table),
259 table_info->vdd_dep_on_mclk);
260 PP_ASSERT_WITH_CODE((0 == result),
261 "Failed to retrieve SVI2 MVDD table from dependancy table.",
262 return result;);
263 }
264
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400265 if (POLARIS10_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +0800266 result = atomctrl_get_voltage_table_v3(hwmgr,
267 VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_GPIO_LUT,
268 &(data->vddci_voltage_table));
269 PP_ASSERT_WITH_CODE((0 == result),
270 "Failed to retrieve VDDCI table.",
271 return result);
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400272 } else if (POLARIS10_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +0800273 result = phm_get_svi2_vddci_voltage_table(&(data->vddci_voltage_table),
274 table_info->vdd_dep_on_mclk);
275 PP_ASSERT_WITH_CODE((0 == result),
276 "Failed to retrieve SVI2 VDDCI table from dependancy table.",
277 return result);
278 }
279
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400280 if (POLARIS10_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +0800281 result = phm_get_svi2_vdd_voltage_table(&(data->vddc_voltage_table),
282 table_info->vddc_lookup_table);
283 PP_ASSERT_WITH_CODE((0 == result),
284 "Failed to retrieve SVI2 VDDC table from lookup table.",
285 return result);
286 }
287
288 PP_ASSERT_WITH_CODE(
289 (data->vddc_voltage_table.count <= (SMU74_MAX_LEVELS_VDDC)),
290 "Too many voltage values for VDDC. Trimming to fit state table.",
291 phm_trim_voltage_table_to_fit_state_table(SMU74_MAX_LEVELS_VDDC,
292 &(data->vddc_voltage_table)));
293
294 PP_ASSERT_WITH_CODE(
295 (data->vddci_voltage_table.count <= (SMU74_MAX_LEVELS_VDDCI)),
296 "Too many voltage values for VDDCI. Trimming to fit state table.",
297 phm_trim_voltage_table_to_fit_state_table(SMU74_MAX_LEVELS_VDDCI,
298 &(data->vddci_voltage_table)));
299
300 PP_ASSERT_WITH_CODE(
301 (data->mvdd_voltage_table.count <= (SMU74_MAX_LEVELS_MVDD)),
302 "Too many voltage values for MVDD. Trimming to fit state table.",
303 phm_trim_voltage_table_to_fit_state_table(SMU74_MAX_LEVELS_MVDD,
304 &(data->mvdd_voltage_table)));
305
306 return 0;
307}
308
309/**
310* Programs static screed detection parameters
311*
312* @param hwmgr the address of the powerplay hardware manager.
313* @return always 0
314*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400315static int polaris10_program_static_screen_threshold_parameters(
Rex Zhua23eefa2015-11-19 18:23:32 +0800316 struct pp_hwmgr *hwmgr)
317{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400318 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800319
320 /* Set static screen threshold unit */
321 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
322 CG_STATIC_SCREEN_PARAMETER, STATIC_SCREEN_THRESHOLD_UNIT,
323 data->static_screen_threshold_unit);
324 /* Set static screen threshold */
325 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
326 CG_STATIC_SCREEN_PARAMETER, STATIC_SCREEN_THRESHOLD,
327 data->static_screen_threshold);
328
329 return 0;
330}
331
332/**
333* Setup display gap for glitch free memory clock switching.
334*
335* @param hwmgr the address of the powerplay hardware manager.
336* @return always 0
337*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400338static int polaris10_enable_display_gap(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800339{
340 uint32_t display_gap =
341 cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
342 ixCG_DISPLAY_GAP_CNTL);
343
344 display_gap = PHM_SET_FIELD(display_gap, CG_DISPLAY_GAP_CNTL,
345 DISP_GAP, DISPLAY_GAP_IGNORE);
346
347 display_gap = PHM_SET_FIELD(display_gap, CG_DISPLAY_GAP_CNTL,
348 DISP_GAP_MCHG, DISPLAY_GAP_VBLANK);
349
350 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
351 ixCG_DISPLAY_GAP_CNTL, display_gap);
352
353 return 0;
354}
355
356/**
357* Programs activity state transition voting clients
358*
359* @param hwmgr the address of the powerplay hardware manager.
360* @return always 0
361*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400362static int polaris10_program_voting_clients(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800363{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400364 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800365
366 /* Clear reset for voting clients before enabling DPM */
367 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
368 SCLK_PWRMGT_CNTL, RESET_SCLK_CNT, 0);
369 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
370 SCLK_PWRMGT_CNTL, RESET_BUSY_CNT, 0);
371
372 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
373 ixCG_FREQ_TRAN_VOTING_0, data->voting_rights_clients0);
374 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
375 ixCG_FREQ_TRAN_VOTING_1, data->voting_rights_clients1);
376 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
377 ixCG_FREQ_TRAN_VOTING_2, data->voting_rights_clients2);
378 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
379 ixCG_FREQ_TRAN_VOTING_3, data->voting_rights_clients3);
380 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
381 ixCG_FREQ_TRAN_VOTING_4, data->voting_rights_clients4);
382 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
383 ixCG_FREQ_TRAN_VOTING_5, data->voting_rights_clients5);
384 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
385 ixCG_FREQ_TRAN_VOTING_6, data->voting_rights_clients6);
386 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
387 ixCG_FREQ_TRAN_VOTING_7, data->voting_rights_clients7);
388
389 return 0;
390}
391
Eric Huangc27371b2016-06-06 16:42:46 -0400392static int polaris10_clear_voting_clients(struct pp_hwmgr *hwmgr)
393{
394 /* Reset voting clients before disabling DPM */
395 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
396 SCLK_PWRMGT_CNTL, RESET_SCLK_CNT, 1);
397 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
398 SCLK_PWRMGT_CNTL, RESET_BUSY_CNT, 1);
399
400 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
401 ixCG_FREQ_TRAN_VOTING_0, 0);
402 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
403 ixCG_FREQ_TRAN_VOTING_1, 0);
404 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
405 ixCG_FREQ_TRAN_VOTING_2, 0);
406 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
407 ixCG_FREQ_TRAN_VOTING_3, 0);
408 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
409 ixCG_FREQ_TRAN_VOTING_4, 0);
410 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
411 ixCG_FREQ_TRAN_VOTING_5, 0);
412 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
413 ixCG_FREQ_TRAN_VOTING_6, 0);
414 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
415 ixCG_FREQ_TRAN_VOTING_7, 0);
416
417 return 0;
418}
419
Rex Zhua23eefa2015-11-19 18:23:32 +0800420/**
421* Get the location of various tables inside the FW image.
422*
423* @param hwmgr the address of the powerplay hardware manager.
424* @return always 0
425*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400426static int polaris10_process_firmware_header(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800427{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400428 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
429 struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(hwmgr->smumgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800430 uint32_t tmp;
431 int result;
432 bool error = false;
433
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400434 result = polaris10_read_smc_sram_dword(hwmgr->smumgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800435 SMU7_FIRMWARE_HEADER_LOCATION +
436 offsetof(SMU74_Firmware_Header, DpmTable),
437 &tmp, data->sram_end);
438
439 if (0 == result)
440 data->dpm_table_start = tmp;
441
442 error |= (0 != result);
443
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400444 result = polaris10_read_smc_sram_dword(hwmgr->smumgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800445 SMU7_FIRMWARE_HEADER_LOCATION +
446 offsetof(SMU74_Firmware_Header, SoftRegisters),
447 &tmp, data->sram_end);
448
449 if (!result) {
450 data->soft_regs_start = tmp;
451 smu_data->soft_regs_start = tmp;
452 }
453
454 error |= (0 != result);
455
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400456 result = polaris10_read_smc_sram_dword(hwmgr->smumgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800457 SMU7_FIRMWARE_HEADER_LOCATION +
458 offsetof(SMU74_Firmware_Header, mcRegisterTable),
459 &tmp, data->sram_end);
460
461 if (!result)
462 data->mc_reg_table_start = tmp;
463
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400464 result = polaris10_read_smc_sram_dword(hwmgr->smumgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800465 SMU7_FIRMWARE_HEADER_LOCATION +
466 offsetof(SMU74_Firmware_Header, FanTable),
467 &tmp, data->sram_end);
468
469 if (!result)
470 data->fan_table_start = tmp;
471
472 error |= (0 != result);
473
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400474 result = polaris10_read_smc_sram_dword(hwmgr->smumgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800475 SMU7_FIRMWARE_HEADER_LOCATION +
476 offsetof(SMU74_Firmware_Header, mcArbDramTimingTable),
477 &tmp, data->sram_end);
478
479 if (!result)
480 data->arb_table_start = tmp;
481
482 error |= (0 != result);
483
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400484 result = polaris10_read_smc_sram_dword(hwmgr->smumgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800485 SMU7_FIRMWARE_HEADER_LOCATION +
486 offsetof(SMU74_Firmware_Header, Version),
487 &tmp, data->sram_end);
488
489 if (!result)
490 hwmgr->microcode_version_info.SMC = tmp;
491
492 error |= (0 != result);
493
494 return error ? -1 : 0;
495}
496
497/* Copy one arb setting to another and then switch the active set.
498 * arb_src and arb_dest is one of the MC_CG_ARB_FREQ_Fx constants.
499 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400500static int polaris10_copy_and_switch_arb_sets(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800501 uint32_t arb_src, uint32_t arb_dest)
502{
503 uint32_t mc_arb_dram_timing;
504 uint32_t mc_arb_dram_timing2;
505 uint32_t burst_time;
506 uint32_t mc_cg_config;
507
508 switch (arb_src) {
509 case MC_CG_ARB_FREQ_F0:
510 mc_arb_dram_timing = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING);
511 mc_arb_dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2);
512 burst_time = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0);
513 break;
514 case MC_CG_ARB_FREQ_F1:
515 mc_arb_dram_timing = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING_1);
516 mc_arb_dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2_1);
517 burst_time = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE1);
518 break;
519 default:
520 return -EINVAL;
521 }
522
523 switch (arb_dest) {
524 case MC_CG_ARB_FREQ_F0:
525 cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING, mc_arb_dram_timing);
526 cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2, mc_arb_dram_timing2);
527 PHM_WRITE_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0, burst_time);
528 break;
529 case MC_CG_ARB_FREQ_F1:
530 cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING_1, mc_arb_dram_timing);
531 cgs_write_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2_1, mc_arb_dram_timing2);
532 PHM_WRITE_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE1, burst_time);
533 break;
534 default:
535 return -EINVAL;
536 }
537
538 mc_cg_config = cgs_read_register(hwmgr->device, mmMC_CG_CONFIG);
539 mc_cg_config |= 0x0000000F;
540 cgs_write_register(hwmgr->device, mmMC_CG_CONFIG, mc_cg_config);
541 PHM_WRITE_FIELD(hwmgr->device, MC_ARB_CG, CG_ARB_REQ, arb_dest);
542
543 return 0;
544}
545
Eric Huangc27371b2016-06-06 16:42:46 -0400546static int polaris10_reset_to_default(struct pp_hwmgr *hwmgr)
547{
548 return smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_ResetToDefaults);
549}
550
Rex Zhua23eefa2015-11-19 18:23:32 +0800551/**
552* Initial switch from ARB F0->F1
553*
554* @param hwmgr the address of the powerplay hardware manager.
555* @return always 0
556* This function is to be called from the SetPowerState table.
557*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400558static int polaris10_initial_switch_from_arbf0_to_f1(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800559{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400560 return polaris10_copy_and_switch_arb_sets(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800561 MC_CG_ARB_FREQ_F0, MC_CG_ARB_FREQ_F1);
562}
563
Eric Huangc27371b2016-06-06 16:42:46 -0400564static int polaris10_force_switch_to_arbf0(struct pp_hwmgr *hwmgr)
565{
566 uint32_t tmp;
567
568 tmp = (cgs_read_ind_register(hwmgr->device,
569 CGS_IND_REG__SMC, ixSMC_SCRATCH9) &
570 0x0000ff00) >> 8;
571
572 if (tmp == MC_CG_ARB_FREQ_F0)
573 return 0;
574
575 return polaris10_copy_and_switch_arb_sets(hwmgr,
576 tmp, MC_CG_ARB_FREQ_F0);
577}
578
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400579static int polaris10_setup_default_pcie_table(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800580{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400581 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800582 struct phm_ppt_v1_information *table_info =
583 (struct phm_ppt_v1_information *)(hwmgr->pptable);
584 struct phm_ppt_v1_pcie_table *pcie_table = table_info->pcie_table;
585 uint32_t i, max_entry;
586
587 PP_ASSERT_WITH_CODE((data->use_pcie_performance_levels ||
588 data->use_pcie_power_saving_levels), "No pcie performance levels!",
589 return -EINVAL);
590
591 if (data->use_pcie_performance_levels &&
592 !data->use_pcie_power_saving_levels) {
593 data->pcie_gen_power_saving = data->pcie_gen_performance;
594 data->pcie_lane_power_saving = data->pcie_lane_performance;
595 } else if (!data->use_pcie_performance_levels &&
596 data->use_pcie_power_saving_levels) {
597 data->pcie_gen_performance = data->pcie_gen_power_saving;
598 data->pcie_lane_performance = data->pcie_lane_power_saving;
599 }
600
601 phm_reset_single_dpm_table(&data->dpm_table.pcie_speed_table,
602 SMU74_MAX_LEVELS_LINK,
603 MAX_REGULAR_DPM_NUMBER);
604
605 if (pcie_table != NULL) {
606 /* max_entry is used to make sure we reserve one PCIE level
607 * for boot level (fix for A+A PSPP issue).
608 * If PCIE table from PPTable have ULV entry + 8 entries,
609 * then ignore the last entry.*/
610 max_entry = (SMU74_MAX_LEVELS_LINK < pcie_table->count) ?
611 SMU74_MAX_LEVELS_LINK : pcie_table->count;
612 for (i = 1; i < max_entry; i++) {
613 phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, i - 1,
614 get_pcie_gen_support(data->pcie_gen_cap,
615 pcie_table->entries[i].gen_speed),
616 get_pcie_lane_support(data->pcie_lane_cap,
617 pcie_table->entries[i].lane_width));
618 }
619 data->dpm_table.pcie_speed_table.count = max_entry - 1;
yanyang1e85c7d62016-02-06 13:28:47 +0800620
621 /* Setup BIF_SCLK levels */
622 for (i = 0; i < max_entry; i++)
623 data->bif_sclk_table[i] = pcie_table->entries[i].pcie_sclk;
Rex Zhua23eefa2015-11-19 18:23:32 +0800624 } else {
625 /* Hardcode Pcie Table */
626 phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 0,
627 get_pcie_gen_support(data->pcie_gen_cap,
628 PP_Min_PCIEGen),
629 get_pcie_lane_support(data->pcie_lane_cap,
630 PP_Max_PCIELane));
631 phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 1,
632 get_pcie_gen_support(data->pcie_gen_cap,
633 PP_Min_PCIEGen),
634 get_pcie_lane_support(data->pcie_lane_cap,
635 PP_Max_PCIELane));
636 phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 2,
637 get_pcie_gen_support(data->pcie_gen_cap,
638 PP_Max_PCIEGen),
639 get_pcie_lane_support(data->pcie_lane_cap,
640 PP_Max_PCIELane));
641 phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 3,
642 get_pcie_gen_support(data->pcie_gen_cap,
643 PP_Max_PCIEGen),
644 get_pcie_lane_support(data->pcie_lane_cap,
645 PP_Max_PCIELane));
646 phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 4,
647 get_pcie_gen_support(data->pcie_gen_cap,
648 PP_Max_PCIEGen),
649 get_pcie_lane_support(data->pcie_lane_cap,
650 PP_Max_PCIELane));
651 phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table, 5,
652 get_pcie_gen_support(data->pcie_gen_cap,
653 PP_Max_PCIEGen),
654 get_pcie_lane_support(data->pcie_lane_cap,
655 PP_Max_PCIELane));
656
657 data->dpm_table.pcie_speed_table.count = 6;
658 }
659 /* Populate last level for boot PCIE level, but do not increment count. */
660 phm_setup_pcie_table_entry(&data->dpm_table.pcie_speed_table,
661 data->dpm_table.pcie_speed_table.count,
662 get_pcie_gen_support(data->pcie_gen_cap,
663 PP_Min_PCIEGen),
664 get_pcie_lane_support(data->pcie_lane_cap,
665 PP_Max_PCIELane));
666
667 return 0;
668}
669
670/*
671 * This function is to initalize all DPM state tables
672 * for SMU7 based on the dependency table.
673 * Dynamic state patching function will then trim these
674 * state tables to the allowed range based
675 * on the power policy or external client requests,
676 * such as UVD request, etc.
677 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400678int polaris10_setup_default_dpm_tables(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800679{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400680 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800681 struct phm_ppt_v1_information *table_info =
682 (struct phm_ppt_v1_information *)(hwmgr->pptable);
683 uint32_t i;
684
685 struct phm_ppt_v1_clock_voltage_dependency_table *dep_sclk_table =
686 table_info->vdd_dep_on_sclk;
687 struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table =
688 table_info->vdd_dep_on_mclk;
689
690 PP_ASSERT_WITH_CODE(dep_sclk_table != NULL,
691 "SCLK dependency table is missing. This table is mandatory",
692 return -EINVAL);
693 PP_ASSERT_WITH_CODE(dep_sclk_table->count >= 1,
694 "SCLK dependency table has to have is missing."
695 "This table is mandatory",
696 return -EINVAL);
697
698 PP_ASSERT_WITH_CODE(dep_mclk_table != NULL,
699 "MCLK dependency table is missing. This table is mandatory",
700 return -EINVAL);
701 PP_ASSERT_WITH_CODE(dep_mclk_table->count >= 1,
702 "MCLK dependency table has to have is missing."
703 "This table is mandatory",
704 return -EINVAL);
705
706 /* clear the state table to reset everything to default */
707 phm_reset_single_dpm_table(
708 &data->dpm_table.sclk_table, SMU74_MAX_LEVELS_GRAPHICS, MAX_REGULAR_DPM_NUMBER);
709 phm_reset_single_dpm_table(
710 &data->dpm_table.mclk_table, SMU74_MAX_LEVELS_MEMORY, MAX_REGULAR_DPM_NUMBER);
711
712
713 /* Initialize Sclk DPM table based on allow Sclk values */
714 data->dpm_table.sclk_table.count = 0;
715 for (i = 0; i < dep_sclk_table->count; i++) {
716 if (i == 0 || data->dpm_table.sclk_table.dpm_levels[data->dpm_table.sclk_table.count - 1].value !=
717 dep_sclk_table->entries[i].clk) {
718
719 data->dpm_table.sclk_table.dpm_levels[data->dpm_table.sclk_table.count].value =
720 dep_sclk_table->entries[i].clk;
721
722 data->dpm_table.sclk_table.dpm_levels[data->dpm_table.sclk_table.count].enabled =
723 (i == 0) ? true : false;
724 data->dpm_table.sclk_table.count++;
725 }
726 }
727
728 /* Initialize Mclk DPM table based on allow Mclk values */
729 data->dpm_table.mclk_table.count = 0;
730 for (i = 0; i < dep_mclk_table->count; i++) {
731 if (i == 0 || data->dpm_table.mclk_table.dpm_levels
732 [data->dpm_table.mclk_table.count - 1].value !=
733 dep_mclk_table->entries[i].clk) {
734 data->dpm_table.mclk_table.dpm_levels[data->dpm_table.mclk_table.count].value =
735 dep_mclk_table->entries[i].clk;
736 data->dpm_table.mclk_table.dpm_levels[data->dpm_table.mclk_table.count].enabled =
737 (i == 0) ? true : false;
738 data->dpm_table.mclk_table.count++;
739 }
740 }
741
742 /* setup PCIE gen speed levels */
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400743 polaris10_setup_default_pcie_table(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +0800744
745 /* save a copy of the default DPM table */
746 memcpy(&(data->golden_dpm_table), &(data->dpm_table),
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400747 sizeof(struct polaris10_dpm_table));
Rex Zhua23eefa2015-11-19 18:23:32 +0800748
749 return 0;
750}
751
752uint8_t convert_to_vid(uint16_t vddc)
753{
754 return (uint8_t) ((6200 - (vddc * VOLTAGE_SCALE)) / 25);
755}
756
757/**
758 * Mvdd table preparation for SMC.
759 *
760 * @param *hwmgr The address of the hardware manager.
761 * @param *table The SMC DPM table structure to be populated.
762 * @return 0
763 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400764static int polaris10_populate_smc_mvdd_table(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800765 SMU74_Discrete_DpmTable *table)
766{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400767 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800768 uint32_t count, level;
769
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400770 if (POLARIS10_VOLTAGE_CONTROL_BY_GPIO == data->mvdd_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +0800771 count = data->mvdd_voltage_table.count;
772 if (count > SMU_MAX_SMIO_LEVELS)
773 count = SMU_MAX_SMIO_LEVELS;
774 for (level = 0; level < count; level++) {
775 table->SmioTable2.Pattern[level].Voltage =
776 PP_HOST_TO_SMC_US(data->mvdd_voltage_table.entries[count].value * VOLTAGE_SCALE);
777 /* Index into DpmTable.Smio. Drive bits from Smio entry to get this voltage level.*/
778 table->SmioTable2.Pattern[level].Smio =
779 (uint8_t) level;
780 table->Smio[level] |=
781 data->mvdd_voltage_table.entries[level].smio_low;
782 }
Huang Rui095d28c2016-07-06 09:31:35 +0800783 table->SmioMask2 = data->mvdd_voltage_table.mask_low;
Rex Zhua23eefa2015-11-19 18:23:32 +0800784
785 table->MvddLevelCount = (uint32_t) PP_HOST_TO_SMC_UL(count);
786 }
787
788 return 0;
789}
790
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400791static int polaris10_populate_smc_vddci_table(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800792 struct SMU74_Discrete_DpmTable *table)
793{
794 uint32_t count, level;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400795 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800796
797 count = data->vddci_voltage_table.count;
798
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400799 if (POLARIS10_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +0800800 if (count > SMU_MAX_SMIO_LEVELS)
801 count = SMU_MAX_SMIO_LEVELS;
802 for (level = 0; level < count; ++level) {
803 table->SmioTable1.Pattern[level].Voltage =
804 PP_HOST_TO_SMC_US(data->vddci_voltage_table.entries[level].value * VOLTAGE_SCALE);
805 table->SmioTable1.Pattern[level].Smio = (uint8_t) level;
806
807 table->Smio[level] |= data->vddci_voltage_table.entries[level].smio_low;
808 }
809 }
810
811 table->SmioMask1 = data->vddci_voltage_table.mask_low;
812
813 return 0;
814}
815
816/**
817* Preparation of vddc and vddgfx CAC tables for SMC.
818*
819* @param hwmgr the address of the hardware manager
820* @param table the SMC DPM table structure to be populated
821* @return always 0
822*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400823static int polaris10_populate_cac_table(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800824 struct SMU74_Discrete_DpmTable *table)
825{
826 uint32_t count;
827 uint8_t index;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400828 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800829 struct phm_ppt_v1_information *table_info =
830 (struct phm_ppt_v1_information *)(hwmgr->pptable);
831 struct phm_ppt_v1_voltage_lookup_table *lookup_table =
832 table_info->vddc_lookup_table;
833 /* tables is already swapped, so in order to use the value from it,
834 * we need to swap it back.
835 * We are populating vddc CAC data to BapmVddc table
836 * in split and merged mode
837 */
838 for (count = 0; count < lookup_table->count; count++) {
839 index = phm_get_voltage_index(lookup_table,
840 data->vddc_voltage_table.entries[count].value);
841 table->BapmVddcVidLoSidd[count] = convert_to_vid(lookup_table->entries[index].us_cac_low);
842 table->BapmVddcVidHiSidd[count] = convert_to_vid(lookup_table->entries[index].us_cac_mid);
843 table->BapmVddcVidHiSidd2[count] = convert_to_vid(lookup_table->entries[index].us_cac_high);
844 }
845
846 return 0;
847}
848
849/**
850* Preparation of voltage tables for SMC.
851*
852* @param hwmgr the address of the hardware manager
853* @param table the SMC DPM table structure to be populated
854* @return always 0
855*/
856
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400857int polaris10_populate_smc_voltage_tables(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800858 struct SMU74_Discrete_DpmTable *table)
859{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400860 polaris10_populate_smc_vddci_table(hwmgr, table);
861 polaris10_populate_smc_mvdd_table(hwmgr, table);
862 polaris10_populate_cac_table(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +0800863
864 return 0;
865}
866
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400867static int polaris10_populate_ulv_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800868 struct SMU74_Discrete_Ulv *state)
869{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400870 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800871 struct phm_ppt_v1_information *table_info =
872 (struct phm_ppt_v1_information *)(hwmgr->pptable);
873
874 state->CcPwrDynRm = 0;
875 state->CcPwrDynRm1 = 0;
876
877 state->VddcOffset = (uint16_t) table_info->us_ulv_voltage_offset;
878 state->VddcOffsetVid = (uint8_t)(table_info->us_ulv_voltage_offset *
879 VOLTAGE_VID_OFFSET_SCALE2 / VOLTAGE_VID_OFFSET_SCALE1);
880
881 state->VddcPhase = (data->vddc_phase_shed_control) ? 0 : 1;
882
883 CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm);
884 CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm1);
885 CONVERT_FROM_HOST_TO_SMC_US(state->VddcOffset);
886
887 return 0;
888}
889
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400890static int polaris10_populate_ulv_state(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800891 struct SMU74_Discrete_DpmTable *table)
892{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400893 return polaris10_populate_ulv_level(hwmgr, &table->Ulv);
Rex Zhua23eefa2015-11-19 18:23:32 +0800894}
895
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400896static int polaris10_populate_smc_link_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800897 struct SMU74_Discrete_DpmTable *table)
898{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400899 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
900 struct polaris10_dpm_table *dpm_table = &data->dpm_table;
Rex Zhua23eefa2015-11-19 18:23:32 +0800901 int i;
902
903 /* Index (dpm_table->pcie_speed_table.count)
904 * is reserved for PCIE boot level. */
905 for (i = 0; i <= dpm_table->pcie_speed_table.count; i++) {
906 table->LinkLevel[i].PcieGenSpeed =
907 (uint8_t)dpm_table->pcie_speed_table.dpm_levels[i].value;
908 table->LinkLevel[i].PcieLaneCount = (uint8_t)encode_pcie_lane_width(
909 dpm_table->pcie_speed_table.dpm_levels[i].param1);
910 table->LinkLevel[i].EnabledForActivity = 1;
911 table->LinkLevel[i].SPC = (uint8_t)(data->pcie_spc_cap & 0xff);
912 table->LinkLevel[i].DownThreshold = PP_HOST_TO_SMC_UL(5);
913 table->LinkLevel[i].UpThreshold = PP_HOST_TO_SMC_UL(30);
914 }
915
916 data->smc_state_table.LinkLevelCount =
917 (uint8_t)dpm_table->pcie_speed_table.count;
918 data->dpm_level_enable_mask.pcie_dpm_enable_mask =
919 phm_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
920
921 return 0;
922}
923
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400924static uint32_t polaris10_get_xclk(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +0800925{
926 uint32_t reference_clock, tmp;
927 struct cgs_display_info info = {0};
928 struct cgs_mode_info mode_info;
929
930 info.mode_info = &mode_info;
931
932 tmp = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, CG_CLKPIN_CNTL_2, MUX_TCLK_TO_XCLK);
933
934 if (tmp)
935 return TCLK;
936
937 cgs_get_active_displays_info(hwmgr->device, &info);
938 reference_clock = mode_info.ref_clock;
939
940 tmp = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, CG_CLKPIN_CNTL, XTALIN_DIVIDE);
941
942 if (0 != tmp)
943 return reference_clock / 4;
944
945 return reference_clock;
946}
947
948/**
949* Calculates the SCLK dividers using the provided engine clock
950*
951* @param hwmgr the address of the hardware manager
952* @param clock the engine clock to use to populate the structure
953* @param sclk the SMC SCLK structure to be populated
954*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400955static int polaris10_calculate_sclk_params(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +0800956 uint32_t clock, SMU_SclkSetting *sclk_setting)
957{
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400958 const struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +0800959 const SMU74_Discrete_DpmTable *table = &(data->smc_state_table);
960 struct pp_atomctrl_clock_dividers_ai dividers;
961
962 uint32_t ref_clock;
963 uint32_t pcc_target_percent, pcc_target_freq, ss_target_percent, ss_target_freq;
964 uint8_t i;
965 int result;
966 uint64_t temp;
967
968 sclk_setting->SclkFrequency = clock;
969 /* get the engine clock dividers for this clock value */
970 result = atomctrl_get_engine_pll_dividers_ai(hwmgr, clock, &dividers);
971 if (result == 0) {
972 sclk_setting->Fcw_int = dividers.usSclk_fcw_int;
973 sclk_setting->Fcw_frac = dividers.usSclk_fcw_frac;
974 sclk_setting->Pcc_fcw_int = dividers.usPcc_fcw_int;
975 sclk_setting->PllRange = dividers.ucSclkPllRange;
yanyang1e85c7d62016-02-06 13:28:47 +0800976 sclk_setting->Sclk_slew_rate = 0x400;
977 sclk_setting->Pcc_up_slew_rate = dividers.usPcc_fcw_slew_frac;
978 sclk_setting->Pcc_down_slew_rate = 0xffff;
Rex Zhua23eefa2015-11-19 18:23:32 +0800979 sclk_setting->SSc_En = dividers.ucSscEnable;
980 sclk_setting->Fcw1_int = dividers.usSsc_fcw1_int;
981 sclk_setting->Fcw1_frac = dividers.usSsc_fcw1_frac;
yanyang1e85c7d62016-02-06 13:28:47 +0800982 sclk_setting->Sclk_ss_slew_rate = dividers.usSsc_fcw_slew_frac;
Rex Zhua23eefa2015-11-19 18:23:32 +0800983 return result;
984 }
985
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400986 ref_clock = polaris10_get_xclk(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +0800987
988 for (i = 0; i < NUM_SCLK_RANGE; i++) {
989 if (clock > data->range_table[i].trans_lower_frequency
990 && clock <= data->range_table[i].trans_upper_frequency) {
991 sclk_setting->PllRange = i;
992 break;
993 }
994 }
995
996 sclk_setting->Fcw_int = (uint16_t)((clock << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv) / ref_clock);
997 temp = clock << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv;
998 temp <<= 0x10;
Slava Grigorevae17c992016-03-22 23:34:29 -0400999 do_div(temp, ref_clock);
1000 sclk_setting->Fcw_frac = temp & 0xffff;
Rex Zhua23eefa2015-11-19 18:23:32 +08001001
1002 pcc_target_percent = 10; /* Hardcode 10% for now. */
1003 pcc_target_freq = clock - (clock * pcc_target_percent / 100);
1004 sclk_setting->Pcc_fcw_int = (uint16_t)((pcc_target_freq << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv) / ref_clock);
1005
1006 ss_target_percent = 2; /* Hardcode 2% for now. */
1007 sclk_setting->SSc_En = 0;
1008 if (ss_target_percent) {
1009 sclk_setting->SSc_En = 1;
1010 ss_target_freq = clock - (clock * ss_target_percent / 100);
1011 sclk_setting->Fcw1_int = (uint16_t)((ss_target_freq << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv) / ref_clock);
1012 temp = ss_target_freq << table->SclkFcwRangeTable[sclk_setting->PllRange].postdiv;
1013 temp <<= 0x10;
Slava Grigorevae17c992016-03-22 23:34:29 -04001014 do_div(temp, ref_clock);
1015 sclk_setting->Fcw1_frac = temp & 0xffff;
Rex Zhua23eefa2015-11-19 18:23:32 +08001016 }
1017
1018 return 0;
1019}
1020
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001021static int polaris10_get_dependency_volt_by_clk(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001022 struct phm_ppt_v1_clock_voltage_dependency_table *dep_table,
1023 uint32_t clock, SMU_VoltageLevel *voltage, uint32_t *mvdd)
1024{
1025 uint32_t i;
1026 uint16_t vddci;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001027 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001028
1029 *voltage = *mvdd = 0;
1030
1031 /* clock - voltage dependency table is empty table */
1032 if (dep_table->count == 0)
1033 return -EINVAL;
1034
1035 for (i = 0; i < dep_table->count; i++) {
1036 /* find first sclk bigger than request */
1037 if (dep_table->entries[i].clk >= clock) {
1038 *voltage |= (dep_table->entries[i].vddc *
1039 VOLTAGE_SCALE) << VDDC_SHIFT;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001040 if (POLARIS10_VOLTAGE_CONTROL_NONE == data->vddci_control)
Rex Zhua23eefa2015-11-19 18:23:32 +08001041 *voltage |= (data->vbios_boot_state.vddci_bootup_value *
1042 VOLTAGE_SCALE) << VDDCI_SHIFT;
1043 else if (dep_table->entries[i].vddci)
1044 *voltage |= (dep_table->entries[i].vddci *
1045 VOLTAGE_SCALE) << VDDCI_SHIFT;
1046 else {
1047 vddci = phm_find_closest_vddci(&(data->vddci_voltage_table),
1048 (dep_table->entries[i].vddc -
1049 (uint16_t)data->vddc_vddci_delta));
Rex Zhu3ff21122016-06-08 19:04:35 +08001050 *voltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
Rex Zhua23eefa2015-11-19 18:23:32 +08001051 }
1052
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001053 if (POLARIS10_VOLTAGE_CONTROL_NONE == data->mvdd_control)
Rex Zhua23eefa2015-11-19 18:23:32 +08001054 *mvdd = data->vbios_boot_state.mvdd_bootup_value *
1055 VOLTAGE_SCALE;
1056 else if (dep_table->entries[i].mvdd)
1057 *mvdd = (uint32_t) dep_table->entries[i].mvdd *
1058 VOLTAGE_SCALE;
1059
1060 *voltage |= 1 << PHASES_SHIFT;
1061 return 0;
1062 }
1063 }
1064
1065 /* sclk is bigger than max sclk in the dependence table */
1066 *voltage |= (dep_table->entries[i - 1].vddc * VOLTAGE_SCALE) << VDDC_SHIFT;
1067
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001068 if (POLARIS10_VOLTAGE_CONTROL_NONE == data->vddci_control)
Rex Zhua23eefa2015-11-19 18:23:32 +08001069 *voltage |= (data->vbios_boot_state.vddci_bootup_value *
1070 VOLTAGE_SCALE) << VDDCI_SHIFT;
1071 else if (dep_table->entries[i-1].vddci) {
1072 vddci = phm_find_closest_vddci(&(data->vddci_voltage_table),
1073 (dep_table->entries[i].vddc -
1074 (uint16_t)data->vddc_vddci_delta));
1075 *voltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
1076 }
1077
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001078 if (POLARIS10_VOLTAGE_CONTROL_NONE == data->mvdd_control)
Rex Zhua23eefa2015-11-19 18:23:32 +08001079 *mvdd = data->vbios_boot_state.mvdd_bootup_value * VOLTAGE_SCALE;
1080 else if (dep_table->entries[i].mvdd)
1081 *mvdd = (uint32_t) dep_table->entries[i - 1].mvdd * VOLTAGE_SCALE;
1082
1083 return 0;
1084}
1085
Nils Wallménius909a0632016-04-25 21:31:34 +02001086static const sclkFcwRange_t Range_Table[NUM_SCLK_RANGE] =
1087{ {VCO_2_4, POSTDIV_DIV_BY_16, 75, 160, 112},
1088 {VCO_3_6, POSTDIV_DIV_BY_16, 112, 224, 160},
1089 {VCO_2_4, POSTDIV_DIV_BY_8, 75, 160, 112},
1090 {VCO_3_6, POSTDIV_DIV_BY_8, 112, 224, 160},
1091 {VCO_2_4, POSTDIV_DIV_BY_4, 75, 160, 112},
1092 {VCO_3_6, POSTDIV_DIV_BY_4, 112, 216, 160},
1093 {VCO_2_4, POSTDIV_DIV_BY_2, 75, 160, 108},
1094 {VCO_3_6, POSTDIV_DIV_BY_2, 112, 216, 160} };
Rex Zhua23eefa2015-11-19 18:23:32 +08001095
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001096static void polaris10_get_sclk_range_table(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08001097{
1098 uint32_t i, ref_clk;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001099 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001100 SMU74_Discrete_DpmTable *table = &(data->smc_state_table);
1101 struct pp_atom_ctrl_sclk_range_table range_table_from_vbios = { { {0} } };
1102
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001103 ref_clk = polaris10_get_xclk(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08001104
1105 if (0 == atomctrl_get_smc_sclk_range_table(hwmgr, &range_table_from_vbios)) {
1106 for (i = 0; i < NUM_SCLK_RANGE; i++) {
1107 table->SclkFcwRangeTable[i].vco_setting = range_table_from_vbios.entry[i].ucVco_setting;
1108 table->SclkFcwRangeTable[i].postdiv = range_table_from_vbios.entry[i].ucPostdiv;
1109 table->SclkFcwRangeTable[i].fcw_pcc = range_table_from_vbios.entry[i].usFcw_pcc;
1110
1111 table->SclkFcwRangeTable[i].fcw_trans_upper = range_table_from_vbios.entry[i].usFcw_trans_upper;
1112 table->SclkFcwRangeTable[i].fcw_trans_lower = range_table_from_vbios.entry[i].usRcw_trans_lower;
1113
1114 CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_pcc);
1115 CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_trans_upper);
1116 CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_trans_lower);
1117 }
1118 return;
1119 }
1120
1121 for (i = 0; i < NUM_SCLK_RANGE; i++) {
1122
1123 data->range_table[i].trans_lower_frequency = (ref_clk * Range_Table[i].fcw_trans_lower) >> Range_Table[i].postdiv;
1124 data->range_table[i].trans_upper_frequency = (ref_clk * Range_Table[i].fcw_trans_upper) >> Range_Table[i].postdiv;
1125
1126 table->SclkFcwRangeTable[i].vco_setting = Range_Table[i].vco_setting;
1127 table->SclkFcwRangeTable[i].postdiv = Range_Table[i].postdiv;
1128 table->SclkFcwRangeTable[i].fcw_pcc = Range_Table[i].fcw_pcc;
1129
1130 table->SclkFcwRangeTable[i].fcw_trans_upper = Range_Table[i].fcw_trans_upper;
1131 table->SclkFcwRangeTable[i].fcw_trans_lower = Range_Table[i].fcw_trans_lower;
1132
1133 CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_pcc);
1134 CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_trans_upper);
1135 CONVERT_FROM_HOST_TO_SMC_US(table->SclkFcwRangeTable[i].fcw_trans_lower);
1136 }
1137}
1138
1139/**
1140* Populates single SMC SCLK structure using the provided engine clock
1141*
1142* @param hwmgr the address of the hardware manager
1143* @param clock the engine clock to use to populate the structure
1144* @param sclk the SMC SCLK structure to be populated
1145*/
1146
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001147static int polaris10_populate_single_graphic_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001148 uint32_t clock, uint16_t sclk_al_threshold,
1149 struct SMU74_Discrete_GraphicsLevel *level)
1150{
1151 int result, i, temp;
1152 /* PP_Clocks minClocks; */
1153 uint32_t mvdd;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001154 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001155 struct phm_ppt_v1_information *table_info =
1156 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1157 SMU_SclkSetting curr_sclk_setting = { 0 };
1158
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001159 result = polaris10_calculate_sclk_params(hwmgr, clock, &curr_sclk_setting);
Rex Zhua23eefa2015-11-19 18:23:32 +08001160
1161 /* populate graphics levels */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001162 result = polaris10_get_dependency_volt_by_clk(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001163 table_info->vdd_dep_on_sclk, clock,
1164 &level->MinVoltage, &mvdd);
1165
1166 PP_ASSERT_WITH_CODE((0 == result),
1167 "can not find VDDC voltage value for "
1168 "VDDC engine clock dependency table",
1169 return result);
1170 level->ActivityLevel = sclk_al_threshold;
1171
1172 level->CcPwrDynRm = 0;
1173 level->CcPwrDynRm1 = 0;
1174 level->EnabledForActivity = 0;
1175 level->EnabledForThrottle = 1;
1176 level->UpHyst = 10;
1177 level->DownHyst = 0;
1178 level->VoltageDownHyst = 0;
1179 level->PowerThrottle = 0;
1180
1181 /*
1182 * TODO: get minimum clocks from dal configaration
1183 * PECI_GetMinClockSettings(hwmgr->pPECI, &minClocks);
1184 */
1185 /* data->DisplayTiming.minClockInSR = minClocks.engineClockInSR; */
1186
1187 /* get level->DeepSleepDivId
1188 if (phm_cap_enabled(hwmgr->platformDescriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep))
1189 level->DeepSleepDivId = PhwFiji_GetSleepDividerIdFromClock(hwmgr, clock, minClocks.engineClockInSR);
1190 */
Nils Wallménius859b8b62016-05-05 09:07:45 +02001191 PP_ASSERT_WITH_CODE((clock >= POLARIS10_MINIMUM_ENGINE_CLOCK), "Engine clock can't satisfy stutter requirement!", return 0);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001192 for (i = POLARIS10_MAX_DEEPSLEEP_DIVIDER_ID; ; i--) {
Nils Wallménius354ef922016-05-05 09:07:47 +02001193 temp = clock >> i;
Rex Zhua23eefa2015-11-19 18:23:32 +08001194
Nils Wallménius859b8b62016-05-05 09:07:45 +02001195 if (temp >= POLARIS10_MINIMUM_ENGINE_CLOCK || i == 0)
Rex Zhua23eefa2015-11-19 18:23:32 +08001196 break;
1197 }
1198
1199 level->DeepSleepDivId = i;
1200
1201 /* Default to slow, highest DPM level will be
1202 * set to PPSMC_DISPLAY_WATERMARK_LOW later.
1203 */
1204 if (data->update_up_hyst)
1205 level->UpHyst = (uint8_t)data->up_hyst;
1206 if (data->update_down_hyst)
1207 level->DownHyst = (uint8_t)data->down_hyst;
1208
1209 level->SclkSetting = curr_sclk_setting;
1210
1211 CONVERT_FROM_HOST_TO_SMC_UL(level->MinVoltage);
1212 CONVERT_FROM_HOST_TO_SMC_UL(level->CcPwrDynRm);
1213 CONVERT_FROM_HOST_TO_SMC_UL(level->CcPwrDynRm1);
1214 CONVERT_FROM_HOST_TO_SMC_US(level->ActivityLevel);
1215 CONVERT_FROM_HOST_TO_SMC_UL(level->SclkSetting.SclkFrequency);
1216 CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Fcw_int);
1217 CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Fcw_frac);
1218 CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Pcc_fcw_int);
yanyang1e85c7d62016-02-06 13:28:47 +08001219 CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Sclk_slew_rate);
1220 CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Pcc_up_slew_rate);
1221 CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Pcc_down_slew_rate);
Rex Zhua23eefa2015-11-19 18:23:32 +08001222 CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Fcw1_int);
1223 CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Fcw1_frac);
yanyang1e85c7d62016-02-06 13:28:47 +08001224 CONVERT_FROM_HOST_TO_SMC_US(level->SclkSetting.Sclk_ss_slew_rate);
Rex Zhua23eefa2015-11-19 18:23:32 +08001225 return 0;
1226}
1227
1228/**
1229* Populates all SMC SCLK levels' structure based on the trimmed allowed dpm engine clock states
1230*
1231* @param hwmgr the address of the hardware manager
1232*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001233static int polaris10_populate_all_graphic_levels(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08001234{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001235 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
1236 struct polaris10_dpm_table *dpm_table = &data->dpm_table;
Rex Zhua23eefa2015-11-19 18:23:32 +08001237 struct phm_ppt_v1_information *table_info =
1238 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1239 struct phm_ppt_v1_pcie_table *pcie_table = table_info->pcie_table;
1240 uint8_t pcie_entry_cnt = (uint8_t) data->dpm_table.pcie_speed_table.count;
1241 int result = 0;
1242 uint32_t array = data->dpm_table_start +
1243 offsetof(SMU74_Discrete_DpmTable, GraphicsLevel);
1244 uint32_t array_size = sizeof(struct SMU74_Discrete_GraphicsLevel) *
1245 SMU74_MAX_LEVELS_GRAPHICS;
1246 struct SMU74_Discrete_GraphicsLevel *levels =
1247 data->smc_state_table.GraphicsLevel;
1248 uint32_t i, max_entry;
1249 uint8_t hightest_pcie_level_enabled = 0,
1250 lowest_pcie_level_enabled = 0,
1251 mid_pcie_level_enabled = 0,
1252 count = 0;
1253
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001254 polaris10_get_sclk_range_table(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08001255
1256 for (i = 0; i < dpm_table->sclk_table.count; i++) {
1257
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001258 result = polaris10_populate_single_graphic_level(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001259 dpm_table->sclk_table.dpm_levels[i].value,
1260 (uint16_t)data->activity_target[i],
1261 &(data->smc_state_table.GraphicsLevel[i]));
1262 if (result)
1263 return result;
1264
1265 /* Making sure only DPM level 0-1 have Deep Sleep Div ID populated. */
1266 if (i > 1)
1267 levels[i].DeepSleepDivId = 0;
1268 }
Rex Zhu5de95e52016-03-22 14:21:18 +08001269 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
1270 PHM_PlatformCaps_SPLLShutdownSupport))
1271 data->smc_state_table.GraphicsLevel[0].SclkSetting.SSc_En = 0;
Rex Zhua23eefa2015-11-19 18:23:32 +08001272
1273 data->smc_state_table.GraphicsLevel[0].EnabledForActivity = 1;
1274 data->smc_state_table.GraphicsDpmLevelCount =
1275 (uint8_t)dpm_table->sclk_table.count;
1276 data->dpm_level_enable_mask.sclk_dpm_enable_mask =
1277 phm_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
1278
1279
1280 if (pcie_table != NULL) {
1281 PP_ASSERT_WITH_CODE((1 <= pcie_entry_cnt),
1282 "There must be 1 or more PCIE levels defined in PPTable.",
1283 return -EINVAL);
1284 max_entry = pcie_entry_cnt - 1;
1285 for (i = 0; i < dpm_table->sclk_table.count; i++)
1286 levels[i].pcieDpmLevel =
1287 (uint8_t) ((i < max_entry) ? i : max_entry);
1288 } else {
1289 while (data->dpm_level_enable_mask.pcie_dpm_enable_mask &&
1290 ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1291 (1 << (hightest_pcie_level_enabled + 1))) != 0))
1292 hightest_pcie_level_enabled++;
1293
1294 while (data->dpm_level_enable_mask.pcie_dpm_enable_mask &&
1295 ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1296 (1 << lowest_pcie_level_enabled)) == 0))
1297 lowest_pcie_level_enabled++;
1298
1299 while ((count < hightest_pcie_level_enabled) &&
1300 ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
1301 (1 << (lowest_pcie_level_enabled + 1 + count))) == 0))
1302 count++;
1303
1304 mid_pcie_level_enabled = (lowest_pcie_level_enabled + 1 + count) <
1305 hightest_pcie_level_enabled ?
1306 (lowest_pcie_level_enabled + 1 + count) :
1307 hightest_pcie_level_enabled;
1308
1309 /* set pcieDpmLevel to hightest_pcie_level_enabled */
1310 for (i = 2; i < dpm_table->sclk_table.count; i++)
1311 levels[i].pcieDpmLevel = hightest_pcie_level_enabled;
1312
1313 /* set pcieDpmLevel to lowest_pcie_level_enabled */
1314 levels[0].pcieDpmLevel = lowest_pcie_level_enabled;
1315
1316 /* set pcieDpmLevel to mid_pcie_level_enabled */
1317 levels[1].pcieDpmLevel = mid_pcie_level_enabled;
1318 }
1319 /* level count will send to smc once at init smc table and never change */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001320 result = polaris10_copy_bytes_to_smc(hwmgr->smumgr, array, (uint8_t *)levels,
Rex Zhua23eefa2015-11-19 18:23:32 +08001321 (uint32_t)array_size, data->sram_end);
1322
1323 return result;
1324}
1325
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001326static int polaris10_populate_single_memory_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001327 uint32_t clock, struct SMU74_Discrete_MemoryLevel *mem_level)
1328{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001329 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001330 struct phm_ppt_v1_information *table_info =
1331 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1332 int result = 0;
1333 struct cgs_display_info info = {0, 0, NULL};
1334
1335 cgs_get_active_displays_info(hwmgr->device, &info);
1336
1337 if (table_info->vdd_dep_on_mclk) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001338 result = polaris10_get_dependency_volt_by_clk(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001339 table_info->vdd_dep_on_mclk, clock,
1340 &mem_level->MinVoltage, &mem_level->MinMvdd);
1341 PP_ASSERT_WITH_CODE((0 == result),
1342 "can not find MinVddc voltage value from memory "
1343 "VDDC voltage dependency table", return result);
1344 }
1345
1346 mem_level->MclkFrequency = clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08001347 mem_level->EnabledForThrottle = 1;
1348 mem_level->EnabledForActivity = 0;
1349 mem_level->UpHyst = 0;
1350 mem_level->DownHyst = 100;
1351 mem_level->VoltageDownHyst = 0;
1352 mem_level->ActivityLevel = (uint16_t)data->mclk_activity_target;
1353 mem_level->StutterEnable = false;
Rex Zhua23eefa2015-11-19 18:23:32 +08001354 mem_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
1355
1356 data->display_timing.num_existing_displays = info.display_count;
1357
1358 if ((data->mclk_stutter_mode_threshold) &&
1359 (clock <= data->mclk_stutter_mode_threshold) &&
1360 (PHM_READ_FIELD(hwmgr->device, DPG_PIPE_STUTTER_CONTROL,
1361 STUTTER_ENABLE) & 0x1))
1362 mem_level->StutterEnable = true;
1363
1364 if (!result) {
1365 CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MinMvdd);
1366 CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MclkFrequency);
1367 CONVERT_FROM_HOST_TO_SMC_US(mem_level->ActivityLevel);
1368 CONVERT_FROM_HOST_TO_SMC_UL(mem_level->MinVoltage);
1369 }
1370 return result;
1371}
1372
1373/**
1374* Populates all SMC MCLK levels' structure based on the trimmed allowed dpm memory clock states
1375*
1376* @param hwmgr the address of the hardware manager
1377*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001378static int polaris10_populate_all_memory_levels(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08001379{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001380 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
1381 struct polaris10_dpm_table *dpm_table = &data->dpm_table;
Rex Zhua23eefa2015-11-19 18:23:32 +08001382 int result;
1383 /* populate MCLK dpm table to SMU7 */
1384 uint32_t array = data->dpm_table_start +
1385 offsetof(SMU74_Discrete_DpmTable, MemoryLevel);
1386 uint32_t array_size = sizeof(SMU74_Discrete_MemoryLevel) *
1387 SMU74_MAX_LEVELS_MEMORY;
1388 struct SMU74_Discrete_MemoryLevel *levels =
1389 data->smc_state_table.MemoryLevel;
1390 uint32_t i;
1391
1392 for (i = 0; i < dpm_table->mclk_table.count; i++) {
1393 PP_ASSERT_WITH_CODE((0 != dpm_table->mclk_table.dpm_levels[i].value),
1394 "can not populate memory level as memory clock is zero",
1395 return -EINVAL);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001396 result = polaris10_populate_single_memory_level(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001397 dpm_table->mclk_table.dpm_levels[i].value,
1398 &levels[i]);
Rex Zhub4c6f992016-03-15 14:39:12 +08001399 if (i == dpm_table->mclk_table.count - 1) {
1400 levels[i].DisplayWatermark = PPSMC_DISPLAY_WATERMARK_HIGH;
1401 levels[i].EnabledForActivity = 1;
1402 }
Rex Zhua23eefa2015-11-19 18:23:32 +08001403 if (result)
1404 return result;
1405 }
1406
Alexandre Demers880a0762016-06-22 23:31:29 -04001407 /* In order to prevent MC activity from stutter mode to push DPM up,
Rex Zhua23eefa2015-11-19 18:23:32 +08001408 * the UVD change complements this by putting the MCLK in
Alexandre Demers880a0762016-06-22 23:31:29 -04001409 * a higher state by default such that we are not affected by
Rex Zhua23eefa2015-11-19 18:23:32 +08001410 * up threshold or and MCLK DPM latency.
1411 */
Rex Zhu9a3c1b32016-06-08 19:42:48 +08001412 levels[0].ActivityLevel = 0x1f;
Rex Zhua23eefa2015-11-19 18:23:32 +08001413 CONVERT_FROM_HOST_TO_SMC_US(levels[0].ActivityLevel);
1414
1415 data->smc_state_table.MemoryDpmLevelCount =
1416 (uint8_t)dpm_table->mclk_table.count;
1417 data->dpm_level_enable_mask.mclk_dpm_enable_mask =
1418 phm_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
Rex Zhua23eefa2015-11-19 18:23:32 +08001419
1420 /* level count will send to smc once at init smc table and never change */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001421 result = polaris10_copy_bytes_to_smc(hwmgr->smumgr, array, (uint8_t *)levels,
Rex Zhua23eefa2015-11-19 18:23:32 +08001422 (uint32_t)array_size, data->sram_end);
1423
1424 return result;
1425}
1426
1427/**
1428* Populates the SMC MVDD structure using the provided memory clock.
1429*
1430* @param hwmgr the address of the hardware manager
1431* @param mclk the MCLK value to be used in the decision if MVDD should be high or low.
1432* @param voltage the SMC VOLTAGE structure to be populated
1433*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001434int polaris10_populate_mvdd_value(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001435 uint32_t mclk, SMIO_Pattern *smio_pat)
1436{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001437 const struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001438 struct phm_ppt_v1_information *table_info =
1439 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1440 uint32_t i = 0;
1441
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001442 if (POLARIS10_VOLTAGE_CONTROL_NONE != data->mvdd_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +08001443 /* find mvdd value which clock is more than request */
1444 for (i = 0; i < table_info->vdd_dep_on_mclk->count; i++) {
1445 if (mclk <= table_info->vdd_dep_on_mclk->entries[i].clk) {
1446 smio_pat->Voltage = data->mvdd_voltage_table.entries[i].value;
1447 break;
1448 }
1449 }
1450 PP_ASSERT_WITH_CODE(i < table_info->vdd_dep_on_mclk->count,
1451 "MVDD Voltage is outside the supported range.",
1452 return -EINVAL);
1453 } else
1454 return -EINVAL;
1455
1456 return 0;
1457}
1458
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001459static int polaris10_populate_smc_acpi_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001460 SMU74_Discrete_DpmTable *table)
1461{
1462 int result = 0;
1463 uint32_t sclk_frequency;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001464 const struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001465 struct phm_ppt_v1_information *table_info =
1466 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1467 SMIO_Pattern vol_level;
1468 uint32_t mvdd;
1469 uint16_t us_mvdd;
1470
1471 table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
1472
Rex Zhu0636e0d2016-06-27 17:30:24 +08001473
1474 /* Get MinVoltage and Frequency from DPM0,
1475 * already converted to SMC_UL */
Rex Zhuc0bcc4e2016-06-29 20:59:59 +08001476 sclk_frequency = data->vbios_boot_state.sclk_bootup_value;
Rex Zhu0636e0d2016-06-27 17:30:24 +08001477 result = polaris10_get_dependency_volt_by_clk(hwmgr,
1478 table_info->vdd_dep_on_sclk,
1479 sclk_frequency,
1480 &table->ACPILevel.MinVoltage, &mvdd);
1481 PP_ASSERT_WITH_CODE((0 == result),
1482 "Cannot find ACPI VDDC voltage value "
1483 "in Clock Dependency Table",
1484 );
1485
Rex Zhua23eefa2015-11-19 18:23:32 +08001486
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001487 result = polaris10_calculate_sclk_params(hwmgr, sclk_frequency, &(table->ACPILevel.SclkSetting));
Rex Zhua23eefa2015-11-19 18:23:32 +08001488 PP_ASSERT_WITH_CODE(result == 0, "Error retrieving Engine Clock dividers from VBIOS.", return result);
1489
1490 table->ACPILevel.DeepSleepDivId = 0;
1491 table->ACPILevel.CcPwrDynRm = 0;
1492 table->ACPILevel.CcPwrDynRm1 = 0;
1493
1494 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.Flags);
1495 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.MinVoltage);
1496 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm);
1497 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm1);
1498
1499 CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SclkSetting.SclkFrequency);
1500 CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Fcw_int);
1501 CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Fcw_frac);
1502 CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Pcc_fcw_int);
yanyang1e85c7d62016-02-06 13:28:47 +08001503 CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Sclk_slew_rate);
1504 CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Pcc_up_slew_rate);
1505 CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Pcc_down_slew_rate);
Rex Zhua23eefa2015-11-19 18:23:32 +08001506 CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Fcw1_int);
1507 CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Fcw1_frac);
yanyang1e85c7d62016-02-06 13:28:47 +08001508 CONVERT_FROM_HOST_TO_SMC_US(table->ACPILevel.SclkSetting.Sclk_ss_slew_rate);
Rex Zhua23eefa2015-11-19 18:23:32 +08001509
Rex Zhu0636e0d2016-06-27 17:30:24 +08001510
1511 /* Get MinVoltage and Frequency from DPM0, already converted to SMC_UL */
Rex Zhuc0bcc4e2016-06-29 20:59:59 +08001512 table->MemoryACPILevel.MclkFrequency = data->vbios_boot_state.mclk_bootup_value;
Rex Zhu0636e0d2016-06-27 17:30:24 +08001513 result = polaris10_get_dependency_volt_by_clk(hwmgr,
1514 table_info->vdd_dep_on_mclk,
1515 table->MemoryACPILevel.MclkFrequency,
1516 &table->MemoryACPILevel.MinVoltage, &mvdd);
1517 PP_ASSERT_WITH_CODE((0 == result),
1518 "Cannot find ACPI VDDCI voltage value "
1519 "in Clock Dependency Table",
1520 );
Rex Zhua23eefa2015-11-19 18:23:32 +08001521
1522 us_mvdd = 0;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001523 if ((POLARIS10_VOLTAGE_CONTROL_NONE == data->mvdd_control) ||
Rex Zhua23eefa2015-11-19 18:23:32 +08001524 (data->mclk_dpm_key_disabled))
1525 us_mvdd = data->vbios_boot_state.mvdd_bootup_value;
1526 else {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001527 if (!polaris10_populate_mvdd_value(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001528 data->dpm_table.mclk_table.dpm_levels[0].value,
1529 &vol_level))
1530 us_mvdd = vol_level.Voltage;
1531 }
1532
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001533 if (0 == polaris10_populate_mvdd_value(hwmgr, 0, &vol_level))
Rex Zhua23eefa2015-11-19 18:23:32 +08001534 table->MemoryACPILevel.MinMvdd = PP_HOST_TO_SMC_UL(vol_level.Voltage);
1535 else
1536 table->MemoryACPILevel.MinMvdd = 0;
1537
1538 table->MemoryACPILevel.StutterEnable = false;
1539
1540 table->MemoryACPILevel.EnabledForThrottle = 0;
1541 table->MemoryACPILevel.EnabledForActivity = 0;
1542 table->MemoryACPILevel.UpHyst = 0;
1543 table->MemoryACPILevel.DownHyst = 100;
1544 table->MemoryACPILevel.VoltageDownHyst = 0;
1545 table->MemoryACPILevel.ActivityLevel =
1546 PP_HOST_TO_SMC_US((uint16_t)data->mclk_activity_target);
1547
1548 CONVERT_FROM_HOST_TO_SMC_UL(table->MemoryACPILevel.MclkFrequency);
1549 CONVERT_FROM_HOST_TO_SMC_UL(table->MemoryACPILevel.MinVoltage);
1550
1551 return result;
1552}
1553
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001554static int polaris10_populate_smc_vce_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001555 SMU74_Discrete_DpmTable *table)
1556{
1557 int result = -EINVAL;
1558 uint8_t count;
1559 struct pp_atomctrl_clock_dividers_vi dividers;
1560 struct phm_ppt_v1_information *table_info =
1561 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1562 struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
1563 table_info->mm_dep_table;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001564 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhu0636e0d2016-06-27 17:30:24 +08001565 uint32_t vddci;
Rex Zhua23eefa2015-11-19 18:23:32 +08001566
1567 table->VceLevelCount = (uint8_t)(mm_table->count);
1568 table->VceBootLevel = 0;
1569
1570 for (count = 0; count < table->VceLevelCount; count++) {
1571 table->VceLevel[count].Frequency = mm_table->entries[count].eclk;
Eric Huang681ed012016-04-04 11:52:56 -04001572 table->VceLevel[count].MinVoltage = 0;
Rex Zhua23eefa2015-11-19 18:23:32 +08001573 table->VceLevel[count].MinVoltage |=
1574 (mm_table->entries[count].vddc * VOLTAGE_SCALE) << VDDC_SHIFT;
Rex Zhu0636e0d2016-06-27 17:30:24 +08001575
1576 if (POLARIS10_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control)
1577 vddci = (uint32_t)phm_find_closest_vddci(&(data->vddci_voltage_table),
1578 mm_table->entries[count].vddc - VDDC_VDDCI_DELTA);
1579 else if (POLARIS10_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control)
1580 vddci = mm_table->entries[count].vddc - VDDC_VDDCI_DELTA;
1581 else
1582 vddci = (data->vbios_boot_state.vddci_bootup_value * VOLTAGE_SCALE) << VDDCI_SHIFT;
1583
1584
Rex Zhua23eefa2015-11-19 18:23:32 +08001585 table->VceLevel[count].MinVoltage |=
Rex Zhu0636e0d2016-06-27 17:30:24 +08001586 (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
Rex Zhua23eefa2015-11-19 18:23:32 +08001587 table->VceLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
1588
1589 /*retrieve divider value for VBIOS */
1590 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1591 table->VceLevel[count].Frequency, &dividers);
1592 PP_ASSERT_WITH_CODE((0 == result),
1593 "can not find divide id for VCE engine clock",
1594 return result);
1595
1596 table->VceLevel[count].Divider = (uint8_t)dividers.pll_post_divider;
1597
1598 CONVERT_FROM_HOST_TO_SMC_UL(table->VceLevel[count].Frequency);
1599 CONVERT_FROM_HOST_TO_SMC_UL(table->VceLevel[count].MinVoltage);
1600 }
1601 return result;
1602}
1603
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001604static int polaris10_populate_smc_samu_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001605 SMU74_Discrete_DpmTable *table)
1606{
1607 int result = -EINVAL;
1608 uint8_t count;
1609 struct pp_atomctrl_clock_dividers_vi dividers;
1610 struct phm_ppt_v1_information *table_info =
1611 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1612 struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
1613 table_info->mm_dep_table;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001614 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhu0636e0d2016-06-27 17:30:24 +08001615 uint32_t vddci;
Rex Zhua23eefa2015-11-19 18:23:32 +08001616
1617 table->SamuBootLevel = 0;
1618 table->SamuLevelCount = (uint8_t)(mm_table->count);
1619
1620 for (count = 0; count < table->SamuLevelCount; count++) {
1621 /* not sure whether we need evclk or not */
Eric Huang681ed012016-04-04 11:52:56 -04001622 table->SamuLevel[count].MinVoltage = 0;
Rex Zhua23eefa2015-11-19 18:23:32 +08001623 table->SamuLevel[count].Frequency = mm_table->entries[count].samclock;
1624 table->SamuLevel[count].MinVoltage |= (mm_table->entries[count].vddc *
1625 VOLTAGE_SCALE) << VDDC_SHIFT;
Rex Zhu0636e0d2016-06-27 17:30:24 +08001626
1627 if (POLARIS10_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control)
1628 vddci = (uint32_t)phm_find_closest_vddci(&(data->vddci_voltage_table),
1629 mm_table->entries[count].vddc - VDDC_VDDCI_DELTA);
1630 else if (POLARIS10_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control)
1631 vddci = mm_table->entries[count].vddc - VDDC_VDDCI_DELTA;
1632 else
1633 vddci = (data->vbios_boot_state.vddci_bootup_value * VOLTAGE_SCALE) << VDDCI_SHIFT;
1634
1635 table->SamuLevel[count].MinVoltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
Rex Zhua23eefa2015-11-19 18:23:32 +08001636 table->SamuLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
1637
1638 /* retrieve divider value for VBIOS */
1639 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1640 table->SamuLevel[count].Frequency, &dividers);
1641 PP_ASSERT_WITH_CODE((0 == result),
1642 "can not find divide id for samu clock", return result);
1643
1644 table->SamuLevel[count].Divider = (uint8_t)dividers.pll_post_divider;
1645
1646 CONVERT_FROM_HOST_TO_SMC_UL(table->SamuLevel[count].Frequency);
1647 CONVERT_FROM_HOST_TO_SMC_UL(table->SamuLevel[count].MinVoltage);
1648 }
1649 return result;
1650}
1651
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001652static int polaris10_populate_memory_timing_parameters(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001653 int32_t eng_clock, int32_t mem_clock,
1654 SMU74_Discrete_MCArbDramTimingTableEntry *arb_regs)
1655{
1656 uint32_t dram_timing;
1657 uint32_t dram_timing2;
1658 uint32_t burst_time;
1659 int result;
1660
1661 result = atomctrl_set_engine_dram_timings_rv770(hwmgr,
1662 eng_clock, mem_clock);
1663 PP_ASSERT_WITH_CODE(result == 0,
1664 "Error calling VBIOS to set DRAM_TIMING.", return result);
1665
1666 dram_timing = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING);
1667 dram_timing2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2);
1668 burst_time = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0);
1669
1670
1671 arb_regs->McArbDramTiming = PP_HOST_TO_SMC_UL(dram_timing);
1672 arb_regs->McArbDramTiming2 = PP_HOST_TO_SMC_UL(dram_timing2);
1673 arb_regs->McArbBurstTime = (uint8_t)burst_time;
1674
1675 return 0;
1676}
1677
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001678static int polaris10_program_memory_timing_parameters(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08001679{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001680 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001681 struct SMU74_Discrete_MCArbDramTimingTable arb_regs;
1682 uint32_t i, j;
1683 int result = 0;
1684
1685 for (i = 0; i < data->dpm_table.sclk_table.count; i++) {
1686 for (j = 0; j < data->dpm_table.mclk_table.count; j++) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001687 result = polaris10_populate_memory_timing_parameters(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001688 data->dpm_table.sclk_table.dpm_levels[i].value,
1689 data->dpm_table.mclk_table.dpm_levels[j].value,
1690 &arb_regs.entries[i][j]);
1691 if (result == 0)
1692 result = atomctrl_set_ac_timing_ai(hwmgr, data->dpm_table.mclk_table.dpm_levels[j].value, j);
1693 if (result != 0)
1694 return result;
1695 }
1696 }
1697
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001698 result = polaris10_copy_bytes_to_smc(
Rex Zhua23eefa2015-11-19 18:23:32 +08001699 hwmgr->smumgr,
1700 data->arb_table_start,
1701 (uint8_t *)&arb_regs,
1702 sizeof(SMU74_Discrete_MCArbDramTimingTable),
1703 data->sram_end);
1704 return result;
1705}
1706
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001707static int polaris10_populate_smc_uvd_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001708 struct SMU74_Discrete_DpmTable *table)
1709{
1710 int result = -EINVAL;
1711 uint8_t count;
1712 struct pp_atomctrl_clock_dividers_vi dividers;
1713 struct phm_ppt_v1_information *table_info =
1714 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1715 struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
1716 table_info->mm_dep_table;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001717 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhu0636e0d2016-06-27 17:30:24 +08001718 uint32_t vddci;
Rex Zhua23eefa2015-11-19 18:23:32 +08001719
1720 table->UvdLevelCount = (uint8_t)(mm_table->count);
1721 table->UvdBootLevel = 0;
1722
1723 for (count = 0; count < table->UvdLevelCount; count++) {
Eric Huang681ed012016-04-04 11:52:56 -04001724 table->UvdLevel[count].MinVoltage = 0;
Rex Zhua23eefa2015-11-19 18:23:32 +08001725 table->UvdLevel[count].VclkFrequency = mm_table->entries[count].vclk;
1726 table->UvdLevel[count].DclkFrequency = mm_table->entries[count].dclk;
1727 table->UvdLevel[count].MinVoltage |= (mm_table->entries[count].vddc *
1728 VOLTAGE_SCALE) << VDDC_SHIFT;
Rex Zhu0636e0d2016-06-27 17:30:24 +08001729
1730 if (POLARIS10_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control)
1731 vddci = (uint32_t)phm_find_closest_vddci(&(data->vddci_voltage_table),
1732 mm_table->entries[count].vddc - VDDC_VDDCI_DELTA);
1733 else if (POLARIS10_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control)
1734 vddci = mm_table->entries[count].vddc - VDDC_VDDCI_DELTA;
1735 else
1736 vddci = (data->vbios_boot_state.vddci_bootup_value * VOLTAGE_SCALE) << VDDCI_SHIFT;
1737
1738 table->UvdLevel[count].MinVoltage |= (vddci * VOLTAGE_SCALE) << VDDCI_SHIFT;
Rex Zhua23eefa2015-11-19 18:23:32 +08001739 table->UvdLevel[count].MinVoltage |= 1 << PHASES_SHIFT;
1740
1741 /* retrieve divider value for VBIOS */
1742 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1743 table->UvdLevel[count].VclkFrequency, &dividers);
1744 PP_ASSERT_WITH_CODE((0 == result),
1745 "can not find divide id for Vclk clock", return result);
1746
1747 table->UvdLevel[count].VclkDivider = (uint8_t)dividers.pll_post_divider;
1748
1749 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr,
1750 table->UvdLevel[count].DclkFrequency, &dividers);
1751 PP_ASSERT_WITH_CODE((0 == result),
1752 "can not find divide id for Dclk clock", return result);
1753
1754 table->UvdLevel[count].DclkDivider = (uint8_t)dividers.pll_post_divider;
1755
1756 CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].VclkFrequency);
1757 CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].DclkFrequency);
1758 CONVERT_FROM_HOST_TO_SMC_UL(table->UvdLevel[count].MinVoltage);
Rex Zhua23eefa2015-11-19 18:23:32 +08001759 }
Rex Zhu0636e0d2016-06-27 17:30:24 +08001760
Rex Zhua23eefa2015-11-19 18:23:32 +08001761 return result;
1762}
1763
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001764static int polaris10_populate_smc_boot_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001765 struct SMU74_Discrete_DpmTable *table)
1766{
1767 int result = 0;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001768 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001769
1770 table->GraphicsBootLevel = 0;
1771 table->MemoryBootLevel = 0;
1772
1773 /* find boot level from dpm table */
1774 result = phm_find_boot_level(&(data->dpm_table.sclk_table),
1775 data->vbios_boot_state.sclk_bootup_value,
1776 (uint32_t *)&(table->GraphicsBootLevel));
1777
1778 result = phm_find_boot_level(&(data->dpm_table.mclk_table),
1779 data->vbios_boot_state.mclk_bootup_value,
1780 (uint32_t *)&(table->MemoryBootLevel));
1781
1782 table->BootVddc = data->vbios_boot_state.vddc_bootup_value *
1783 VOLTAGE_SCALE;
1784 table->BootVddci = data->vbios_boot_state.vddci_bootup_value *
1785 VOLTAGE_SCALE;
1786 table->BootMVdd = data->vbios_boot_state.mvdd_bootup_value *
1787 VOLTAGE_SCALE;
1788
1789 CONVERT_FROM_HOST_TO_SMC_US(table->BootVddc);
1790 CONVERT_FROM_HOST_TO_SMC_US(table->BootVddci);
1791 CONVERT_FROM_HOST_TO_SMC_US(table->BootMVdd);
1792
1793 return 0;
1794}
1795
1796
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001797static int polaris10_populate_smc_initailial_state(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08001798{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001799 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001800 struct phm_ppt_v1_information *table_info =
1801 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1802 uint8_t count, level;
1803
1804 count = (uint8_t)(table_info->vdd_dep_on_sclk->count);
1805
1806 for (level = 0; level < count; level++) {
1807 if (table_info->vdd_dep_on_sclk->entries[level].clk >=
1808 data->vbios_boot_state.sclk_bootup_value) {
1809 data->smc_state_table.GraphicsBootLevel = level;
1810 break;
1811 }
1812 }
1813
1814 count = (uint8_t)(table_info->vdd_dep_on_mclk->count);
1815 for (level = 0; level < count; level++) {
1816 if (table_info->vdd_dep_on_mclk->entries[level].clk >=
1817 data->vbios_boot_state.mclk_bootup_value) {
1818 data->smc_state_table.MemoryBootLevel = level;
1819 break;
1820 }
1821 }
1822
1823 return 0;
1824}
1825
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001826static int polaris10_populate_clock_stretcher_data_table(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08001827{
Rex Zhu270d0132016-06-07 18:39:06 +08001828 uint32_t ro, efuse, volt_without_cks, volt_with_cks, value, max, min;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001829 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Matthias Beyer037d6df2016-06-30 18:38:49 +02001830 uint8_t i, stretch_amount, volt_offset = 0;
Rex Zhua23eefa2015-11-19 18:23:32 +08001831 struct phm_ppt_v1_information *table_info =
1832 (struct phm_ppt_v1_information *)(hwmgr->pptable);
1833 struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
1834 table_info->vdd_dep_on_sclk;
1835
1836 stretch_amount = (uint8_t)table_info->cac_dtp_table->usClockStretchAmount;
1837
1838 /* Read SMU_Eefuse to read and calculate RO and determine
1839 * if the part is SS or FF. if RO >= 1660MHz, part is FF.
1840 */
1841 efuse = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
Rex Zhu270d0132016-06-07 18:39:06 +08001842 ixSMU_EFUSE_0 + (67 * 4));
Rex Zhua23eefa2015-11-19 18:23:32 +08001843 efuse &= 0xFF000000;
1844 efuse = efuse >> 24;
Rex Zhua23eefa2015-11-19 18:23:32 +08001845
Rex Zhu270d0132016-06-07 18:39:06 +08001846 if (hwmgr->chip_id == CHIP_POLARIS10) {
1847 min = 1000;
1848 max = 2300;
1849 } else {
1850 min = 1100;
1851 max = 2100;
1852 }
Rex Zhua23eefa2015-11-19 18:23:32 +08001853
Rex Zhu270d0132016-06-07 18:39:06 +08001854 ro = efuse * (max -min)/255 + min;
Rex Zhua23eefa2015-11-19 18:23:32 +08001855
1856 /* Populate Sclk_CKS_masterEn0_7 and Sclk_voltageOffset */
1857 for (i = 0; i < sclk_table->count; i++) {
1858 data->smc_state_table.Sclk_CKS_masterEn0_7 |=
1859 sclk_table->entries[i].cks_enable << i;
Rex Zhu0812a942016-06-22 21:00:09 +08001860 if (hwmgr->chip_id == CHIP_POLARIS10) {
Rex Zhuab6bad02016-06-28 16:55:52 -04001861 volt_without_cks = (uint32_t)((2753594000U + (sclk_table->entries[i].clk/100) * 136418 -(ro - 70) * 1000000) / \
Rex Zhu0812a942016-06-22 21:00:09 +08001862 (2424180 - (sclk_table->entries[i].clk/100) * 1132925/1000));
Rex Zhuab6bad02016-06-28 16:55:52 -04001863 volt_with_cks = (uint32_t)((2797202000U + sclk_table->entries[i].clk/100 * 3232 - (ro - 65) * 1000000) / \
1864 (2522480 - sclk_table->entries[i].clk/100 * 115764/100));
Rex Zhu0812a942016-06-22 21:00:09 +08001865 } else {
Rex Zhuab6bad02016-06-28 16:55:52 -04001866 volt_without_cks = (uint32_t)((2416794800U + (sclk_table->entries[i].clk/100) * 1476925/10 -(ro - 50) * 1000000) / \
1867 (2625416 - (sclk_table->entries[i].clk/100) * (12586807/10000)));
1868 volt_with_cks = (uint32_t)((2999656000U - sclk_table->entries[i].clk/100 * 392803 - (ro - 44) * 1000000) / \
1869 (3422454 - sclk_table->entries[i].clk/100 * (18886376/10000)));
Rex Zhu0812a942016-06-22 21:00:09 +08001870 }
Rex Zhu270d0132016-06-07 18:39:06 +08001871
Rex Zhua23eefa2015-11-19 18:23:32 +08001872 if (volt_without_cks >= volt_with_cks)
1873 volt_offset = (uint8_t)(((volt_without_cks - volt_with_cks +
Rex Zhuab6bad02016-06-28 16:55:52 -04001874 sclk_table->entries[i].cks_voffset) * 100 + 624) / 625);
Rex Zhu270d0132016-06-07 18:39:06 +08001875
Rex Zhua23eefa2015-11-19 18:23:32 +08001876 data->smc_state_table.Sclk_voltageOffset[i] = volt_offset;
1877 }
1878
Rex Zhu83a7af62016-06-23 11:05:00 +08001879 data->smc_state_table.LdoRefSel = (table_info->cac_dtp_table->ucCKS_LDO_REFSEL != 0) ? table_info->cac_dtp_table->ucCKS_LDO_REFSEL : 6;
Rex Zhua23eefa2015-11-19 18:23:32 +08001880 /* Populate CKS Lookup Table */
Matthias Beyer037d6df2016-06-30 18:38:49 +02001881 if (stretch_amount != 1 && stretch_amount != 2 && stretch_amount != 3 &&
1882 stretch_amount != 4 && stretch_amount != 5) {
Rex Zhua23eefa2015-11-19 18:23:32 +08001883 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
1884 PHM_PlatformCaps_ClockStretcher);
1885 PP_ASSERT_WITH_CODE(false,
1886 "Stretch Amount in PPTable not supported\n",
1887 return -EINVAL);
1888 }
1889
Rex Zhua23eefa2015-11-19 18:23:32 +08001890 value = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixPWR_CKS_CNTL);
1891 value &= 0xFFFFFFFE;
1892 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixPWR_CKS_CNTL, value);
1893
1894 return 0;
1895}
1896
1897/**
1898* Populates the SMC VRConfig field in DPM table.
1899*
1900* @param hwmgr the address of the hardware manager
1901* @param table the SMC DPM table structure to be populated
1902* @return always 0
1903*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001904static int polaris10_populate_vr_config(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08001905 struct SMU74_Discrete_DpmTable *table)
1906{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001907 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08001908 uint16_t config;
1909
1910 config = VR_MERGED_WITH_VDDC;
1911 table->VRConfig |= (config << VRCONF_VDDGFX_SHIFT);
1912
1913 /* Set Vddc Voltage Controller */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001914 if (POLARIS10_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +08001915 config = VR_SVI2_PLANE_1;
1916 table->VRConfig |= config;
1917 } else {
1918 PP_ASSERT_WITH_CODE(false,
1919 "VDDC should be on SVI2 control in merged mode!",
1920 );
1921 }
1922 /* Set Vddci Voltage Controller */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001923 if (POLARIS10_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +08001924 config = VR_SVI2_PLANE_2; /* only in merged mode */
1925 table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001926 } else if (POLARIS10_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +08001927 config = VR_SMIO_PATTERN_1;
1928 table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
1929 } else {
1930 config = VR_STATIC_VOLTAGE;
1931 table->VRConfig |= (config << VRCONF_VDDCI_SHIFT);
1932 }
1933 /* Set Mvdd Voltage Controller */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001934 if (POLARIS10_VOLTAGE_CONTROL_BY_SVID2 == data->mvdd_control) {
Rex Zhua23eefa2015-11-19 18:23:32 +08001935 config = VR_SVI2_PLANE_2;
1936 table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
Rex Zhu696b2d72016-06-27 17:30:24 +08001937 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, data->soft_regs_start +
1938 offsetof(SMU74_SoftRegisters, AllowMvddSwitch), 0x1);
Rex Zhua23eefa2015-11-19 18:23:32 +08001939 } else {
1940 config = VR_STATIC_VOLTAGE;
1941 table->VRConfig |= (config << VRCONF_MVDD_SHIFT);
1942 }
1943
1944 return 0;
1945}
1946
Rex Zhu432c3a32016-06-08 19:39:42 +08001947
1948int polaris10_populate_avfs_parameters(struct pp_hwmgr *hwmgr)
1949{
1950 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
1951 SMU74_Discrete_DpmTable *table = &(data->smc_state_table);
1952 int result = 0;
1953 struct pp_atom_ctrl__avfs_parameters avfs_params = {0};
1954 AVFS_meanNsigma_t AVFS_meanNsigma = { {0} };
1955 AVFS_Sclk_Offset_t AVFS_SclkOffset = { {0} };
1956 uint32_t tmp, i;
1957 struct pp_smumgr *smumgr = hwmgr->smumgr;
1958 struct polaris10_smumgr *smu_data = (struct polaris10_smumgr *)(smumgr->backend);
1959
1960 struct phm_ppt_v1_information *table_info =
1961 (struct phm_ppt_v1_information *)hwmgr->pptable;
1962 struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
1963 table_info->vdd_dep_on_sclk;
1964
1965
1966 if (smu_data->avfs.avfs_btc_status == AVFS_BTC_NOTSUPPORTED)
1967 return result;
1968
1969 result = atomctrl_get_avfs_information(hwmgr, &avfs_params);
1970
1971 if (0 == result) {
1972 table->BTCGB_VDROOP_TABLE[0].a0 = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSON_a0);
1973 table->BTCGB_VDROOP_TABLE[0].a1 = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSON_a1);
1974 table->BTCGB_VDROOP_TABLE[0].a2 = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSON_a2);
1975 table->BTCGB_VDROOP_TABLE[1].a0 = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSOFF_a0);
1976 table->BTCGB_VDROOP_TABLE[1].a1 = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSOFF_a1);
1977 table->BTCGB_VDROOP_TABLE[1].a2 = PP_HOST_TO_SMC_UL(avfs_params.ulGB_VDROOP_TABLE_CKSOFF_a2);
1978 table->AVFSGB_VDROOP_TABLE[0].m1 = PP_HOST_TO_SMC_UL(avfs_params.ulAVFSGB_FUSE_TABLE_CKSON_m1);
1979 table->AVFSGB_VDROOP_TABLE[0].m2 = PP_HOST_TO_SMC_US(avfs_params.usAVFSGB_FUSE_TABLE_CKSON_m2);
1980 table->AVFSGB_VDROOP_TABLE[0].b = PP_HOST_TO_SMC_UL(avfs_params.ulAVFSGB_FUSE_TABLE_CKSON_b);
1981 table->AVFSGB_VDROOP_TABLE[0].m1_shift = 24;
1982 table->AVFSGB_VDROOP_TABLE[0].m2_shift = 12;
1983 table->AVFSGB_VDROOP_TABLE[1].m1 = PP_HOST_TO_SMC_UL(avfs_params.ulAVFSGB_FUSE_TABLE_CKSOFF_m1);
1984 table->AVFSGB_VDROOP_TABLE[1].m2 = PP_HOST_TO_SMC_US(avfs_params.usAVFSGB_FUSE_TABLE_CKSOFF_m2);
1985 table->AVFSGB_VDROOP_TABLE[1].b = PP_HOST_TO_SMC_UL(avfs_params.ulAVFSGB_FUSE_TABLE_CKSOFF_b);
1986 table->AVFSGB_VDROOP_TABLE[1].m1_shift = 24;
1987 table->AVFSGB_VDROOP_TABLE[1].m2_shift = 12;
1988 table->MaxVoltage = PP_HOST_TO_SMC_US(avfs_params.usMaxVoltage_0_25mv);
1989 AVFS_meanNsigma.Aconstant[0] = PP_HOST_TO_SMC_UL(avfs_params.ulAVFS_meanNsigma_Acontant0);
1990 AVFS_meanNsigma.Aconstant[1] = PP_HOST_TO_SMC_UL(avfs_params.ulAVFS_meanNsigma_Acontant1);
1991 AVFS_meanNsigma.Aconstant[2] = PP_HOST_TO_SMC_UL(avfs_params.ulAVFS_meanNsigma_Acontant2);
1992 AVFS_meanNsigma.DC_tol_sigma = PP_HOST_TO_SMC_US(avfs_params.usAVFS_meanNsigma_DC_tol_sigma);
1993 AVFS_meanNsigma.Platform_mean = PP_HOST_TO_SMC_US(avfs_params.usAVFS_meanNsigma_Platform_mean);
1994 AVFS_meanNsigma.PSM_Age_CompFactor = PP_HOST_TO_SMC_US(avfs_params.usPSM_Age_ComFactor);
1995 AVFS_meanNsigma.Platform_sigma = PP_HOST_TO_SMC_US(avfs_params.usAVFS_meanNsigma_Platform_sigma);
1996
1997 for (i = 0; i < NUM_VFT_COLUMNS; i++) {
1998 AVFS_meanNsigma.Static_Voltage_Offset[i] = (uint8_t)(sclk_table->entries[i].cks_voffset * 100 / 625);
1999 AVFS_SclkOffset.Sclk_Offset[i] = PP_HOST_TO_SMC_US((uint16_t)(sclk_table->entries[i].sclk_offset) / 100);
2000 }
2001
2002 result = polaris10_read_smc_sram_dword(smumgr,
2003 SMU7_FIRMWARE_HEADER_LOCATION + offsetof(SMU74_Firmware_Header, AvfsMeanNSigma),
2004 &tmp, data->sram_end);
2005
2006 polaris10_copy_bytes_to_smc(smumgr,
2007 tmp,
2008 (uint8_t *)&AVFS_meanNsigma,
2009 sizeof(AVFS_meanNsigma_t),
2010 data->sram_end);
2011
2012 result = polaris10_read_smc_sram_dword(smumgr,
2013 SMU7_FIRMWARE_HEADER_LOCATION + offsetof(SMU74_Firmware_Header, AvfsSclkOffsetTable),
2014 &tmp, data->sram_end);
2015 polaris10_copy_bytes_to_smc(smumgr,
2016 tmp,
2017 (uint8_t *)&AVFS_SclkOffset,
2018 sizeof(AVFS_Sclk_Offset_t),
2019 data->sram_end);
2020
2021 data->avfs_vdroop_override_setting = (avfs_params.ucEnableGB_VDROOP_TABLE_CKSON << BTCGB0_Vdroop_Enable_SHIFT) |
2022 (avfs_params.ucEnableGB_VDROOP_TABLE_CKSOFF << BTCGB1_Vdroop_Enable_SHIFT) |
2023 (avfs_params.ucEnableGB_FUSE_TABLE_CKSON << AVFSGB0_Vdroop_Enable_SHIFT) |
2024 (avfs_params.ucEnableGB_FUSE_TABLE_CKSOFF << AVFSGB1_Vdroop_Enable_SHIFT);
2025 data->apply_avfs_cks_off_voltage = (avfs_params.ucEnableApplyAVFS_CKS_OFF_Voltage == 1) ? true : false;
2026 }
2027 return result;
2028}
2029
2030
Rex Zhua23eefa2015-11-19 18:23:32 +08002031/**
2032* Initializes the SMC table and uploads it
2033*
2034* @param hwmgr the address of the powerplay hardware manager.
2035* @return always 0
2036*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002037static int polaris10_init_smc_table(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002038{
2039 int result;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002040 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08002041 struct phm_ppt_v1_information *table_info =
2042 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2043 struct SMU74_Discrete_DpmTable *table = &(data->smc_state_table);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002044 const struct polaris10_ulv_parm *ulv = &(data->ulv);
Rex Zhua23eefa2015-11-19 18:23:32 +08002045 uint8_t i;
2046 struct pp_atomctrl_gpio_pin_assignment gpio_pin;
yanyang1e85c7d62016-02-06 13:28:47 +08002047 pp_atomctrl_clock_dividers_vi dividers;
Rex Zhua23eefa2015-11-19 18:23:32 +08002048
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002049 result = polaris10_setup_default_dpm_tables(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002050 PP_ASSERT_WITH_CODE(0 == result,
2051 "Failed to setup default DPM tables!", return result);
2052
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002053 if (POLARIS10_VOLTAGE_CONTROL_NONE != data->voltage_control)
2054 polaris10_populate_smc_voltage_tables(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002055
Eric Huang681ed012016-04-04 11:52:56 -04002056 table->SystemFlags = 0;
Rex Zhua23eefa2015-11-19 18:23:32 +08002057 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2058 PHM_PlatformCaps_AutomaticDCTransition))
2059 table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
2060
2061 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2062 PHM_PlatformCaps_StepVddc))
2063 table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
2064
2065 if (data->is_memory_gddr5)
2066 table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
2067
2068 if (ulv->ulv_supported && table_info->us_ulv_voltage_offset) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002069 result = polaris10_populate_ulv_state(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002070 PP_ASSERT_WITH_CODE(0 == result,
2071 "Failed to initialize ULV state!", return result);
2072 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002073 ixCG_ULV_PARAMETER, PPPOLARIS10_CGULVPARAMETER_DFLT);
Rex Zhua23eefa2015-11-19 18:23:32 +08002074 }
2075
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002076 result = polaris10_populate_smc_link_level(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002077 PP_ASSERT_WITH_CODE(0 == result,
2078 "Failed to initialize Link Level!", return result);
2079
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002080 result = polaris10_populate_all_graphic_levels(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002081 PP_ASSERT_WITH_CODE(0 == result,
2082 "Failed to initialize Graphics Level!", return result);
2083
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002084 result = polaris10_populate_all_memory_levels(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002085 PP_ASSERT_WITH_CODE(0 == result,
2086 "Failed to initialize Memory Level!", return result);
2087
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002088 result = polaris10_populate_smc_acpi_level(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002089 PP_ASSERT_WITH_CODE(0 == result,
2090 "Failed to initialize ACPI Level!", return result);
2091
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002092 result = polaris10_populate_smc_vce_level(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002093 PP_ASSERT_WITH_CODE(0 == result,
2094 "Failed to initialize VCE Level!", return result);
2095
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002096 result = polaris10_populate_smc_samu_level(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002097 PP_ASSERT_WITH_CODE(0 == result,
2098 "Failed to initialize SAMU Level!", return result);
2099
2100 /* Since only the initial state is completely set up at this point
2101 * (the other states are just copies of the boot state) we only
2102 * need to populate the ARB settings for the initial state.
2103 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002104 result = polaris10_program_memory_timing_parameters(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002105 PP_ASSERT_WITH_CODE(0 == result,
2106 "Failed to Write ARB settings for the initial state.", return result);
2107
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002108 result = polaris10_populate_smc_uvd_level(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002109 PP_ASSERT_WITH_CODE(0 == result,
2110 "Failed to initialize UVD Level!", return result);
2111
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002112 result = polaris10_populate_smc_boot_level(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002113 PP_ASSERT_WITH_CODE(0 == result,
2114 "Failed to initialize Boot Level!", return result);
2115
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002116 result = polaris10_populate_smc_initailial_state(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002117 PP_ASSERT_WITH_CODE(0 == result,
2118 "Failed to initialize Boot State!", return result);
2119
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002120 result = polaris10_populate_bapm_parameters_in_dpm_table(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002121 PP_ASSERT_WITH_CODE(0 == result,
2122 "Failed to populate BAPM Parameters!", return result);
2123
2124 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2125 PHM_PlatformCaps_ClockStretcher)) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002126 result = polaris10_populate_clock_stretcher_data_table(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002127 PP_ASSERT_WITH_CODE(0 == result,
2128 "Failed to populate Clock Stretcher Data Table!",
2129 return result);
2130 }
Rex Zhu432c3a32016-06-08 19:39:42 +08002131
2132 result = polaris10_populate_avfs_parameters(hwmgr);
2133 PP_ASSERT_WITH_CODE(0 == result, "Failed to populate AVFS Parameters!", return result;);
2134
Rex Zhu9ab9cf02016-05-03 15:47:15 +08002135 table->CurrSclkPllRange = 0xff;
Rex Zhua23eefa2015-11-19 18:23:32 +08002136 table->GraphicsVoltageChangeEnable = 1;
2137 table->GraphicsThermThrottleEnable = 1;
2138 table->GraphicsInterval = 1;
2139 table->VoltageInterval = 1;
2140 table->ThermalInterval = 1;
2141 table->TemperatureLimitHigh =
2142 table_info->cac_dtp_table->usTargetOperatingTemp *
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002143 POLARIS10_Q88_FORMAT_CONVERSION_UNIT;
Rex Zhua23eefa2015-11-19 18:23:32 +08002144 table->TemperatureLimitLow =
2145 (table_info->cac_dtp_table->usTargetOperatingTemp - 1) *
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002146 POLARIS10_Q88_FORMAT_CONVERSION_UNIT;
Rex Zhua23eefa2015-11-19 18:23:32 +08002147 table->MemoryVoltageChangeEnable = 1;
2148 table->MemoryInterval = 1;
2149 table->VoltageResponseTime = 0;
2150 table->PhaseResponseTime = 0;
2151 table->MemoryThermThrottleEnable = 1;
2152 table->PCIeBootLinkLevel = 0;
2153 table->PCIeGenInterval = 1;
Eric Huang681ed012016-04-04 11:52:56 -04002154 table->VRConfig = 0;
Rex Zhua23eefa2015-11-19 18:23:32 +08002155
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002156 result = polaris10_populate_vr_config(hwmgr, table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002157 PP_ASSERT_WITH_CODE(0 == result,
2158 "Failed to populate VRConfig setting!", return result);
2159
2160 table->ThermGpio = 17;
2161 table->SclkStepSize = 0x4000;
2162
2163 if (atomctrl_get_pp_assign_pin(hwmgr, VDDC_VRHOT_GPIO_PINID, &gpio_pin)) {
2164 table->VRHotGpio = gpio_pin.uc_gpio_pin_bit_shift;
2165 } else {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002166 table->VRHotGpio = POLARIS10_UNUSED_GPIO_PIN;
Rex Zhua23eefa2015-11-19 18:23:32 +08002167 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2168 PHM_PlatformCaps_RegulatorHot);
2169 }
2170
2171 if (atomctrl_get_pp_assign_pin(hwmgr, PP_AC_DC_SWITCH_GPIO_PINID,
2172 &gpio_pin)) {
2173 table->AcDcGpio = gpio_pin.uc_gpio_pin_bit_shift;
2174 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2175 PHM_PlatformCaps_AutomaticDCTransition);
2176 } else {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002177 table->AcDcGpio = POLARIS10_UNUSED_GPIO_PIN;
Rex Zhua23eefa2015-11-19 18:23:32 +08002178 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2179 PHM_PlatformCaps_AutomaticDCTransition);
2180 }
2181
2182 /* Thermal Output GPIO */
2183 if (atomctrl_get_pp_assign_pin(hwmgr, THERMAL_INT_OUTPUT_GPIO_PINID,
2184 &gpio_pin)) {
2185 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2186 PHM_PlatformCaps_ThermalOutGPIO);
2187
2188 table->ThermOutGpio = gpio_pin.uc_gpio_pin_bit_shift;
2189
2190 /* For porlarity read GPIOPAD_A with assigned Gpio pin
2191 * since VBIOS will program this register to set 'inactive state',
2192 * driver can then determine 'active state' from this and
2193 * program SMU with correct polarity
2194 */
2195 table->ThermOutPolarity = (0 == (cgs_read_register(hwmgr->device, mmGPIOPAD_A)
2196 & (1 << gpio_pin.uc_gpio_pin_bit_shift))) ? 1:0;
2197 table->ThermOutMode = SMU7_THERM_OUT_MODE_THERM_ONLY;
2198
2199 /* if required, combine VRHot/PCC with thermal out GPIO */
2200 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_RegulatorHot)
2201 && phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_CombinePCCWithThermalSignal))
2202 table->ThermOutMode = SMU7_THERM_OUT_MODE_THERM_VRHOT;
2203 } else {
2204 table->ThermOutGpio = 17;
2205 table->ThermOutPolarity = 1;
2206 table->ThermOutMode = SMU7_THERM_OUT_MODE_DISABLE;
2207 }
2208
yanyang1e85c7d62016-02-06 13:28:47 +08002209 /* Populate BIF_SCLK levels into SMC DPM table */
2210 for (i = 0; i <= data->dpm_table.pcie_speed_table.count; i++) {
2211 result = atomctrl_get_dfs_pll_dividers_vi(hwmgr, data->bif_sclk_table[i], &dividers);
2212 PP_ASSERT_WITH_CODE((result == 0), "Can not find DFS divide id for Sclk", return result);
2213
2214 if (i == 0)
2215 table->Ulv.BifSclkDfs = PP_HOST_TO_SMC_US((USHORT)(dividers.pll_post_divider));
2216 else
2217 table->LinkLevel[i-1].BifSclkDfs = PP_HOST_TO_SMC_US((USHORT)(dividers.pll_post_divider));
2218 }
2219
Rex Zhua23eefa2015-11-19 18:23:32 +08002220 for (i = 0; i < SMU74_MAX_ENTRIES_SMIO; i++)
2221 table->Smio[i] = PP_HOST_TO_SMC_UL(table->Smio[i]);
2222
2223 CONVERT_FROM_HOST_TO_SMC_UL(table->SystemFlags);
2224 CONVERT_FROM_HOST_TO_SMC_UL(table->VRConfig);
2225 CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMask1);
2226 CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMask2);
2227 CONVERT_FROM_HOST_TO_SMC_UL(table->SclkStepSize);
Rex Zhu9ab9cf02016-05-03 15:47:15 +08002228 CONVERT_FROM_HOST_TO_SMC_UL(table->CurrSclkPllRange);
Rex Zhua23eefa2015-11-19 18:23:32 +08002229 CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitHigh);
2230 CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitLow);
2231 CONVERT_FROM_HOST_TO_SMC_US(table->VoltageResponseTime);
2232 CONVERT_FROM_HOST_TO_SMC_US(table->PhaseResponseTime);
2233
2234 /* Upload all dpm data to SMC memory.(dpm level, dpm level count etc) */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002235 result = polaris10_copy_bytes_to_smc(hwmgr->smumgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08002236 data->dpm_table_start +
2237 offsetof(SMU74_Discrete_DpmTable, SystemFlags),
2238 (uint8_t *)&(table->SystemFlags),
2239 sizeof(SMU74_Discrete_DpmTable) - 3 * sizeof(SMU74_PIDController),
2240 data->sram_end);
2241 PP_ASSERT_WITH_CODE(0 == result,
2242 "Failed to upload dpm data to SMC memory!", return result);
2243
2244 return 0;
2245}
2246
2247/**
2248* Initialize the ARB DRAM timing table's index field.
2249*
2250* @param hwmgr the address of the powerplay hardware manager.
2251* @return always 0
2252*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002253static int polaris10_init_arb_table_index(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002254{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002255 const struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08002256 uint32_t tmp;
2257 int result;
2258
2259 /* This is a read-modify-write on the first byte of the ARB table.
2260 * The first byte in the SMU73_Discrete_MCArbDramTimingTable structure
2261 * is the field 'current'.
2262 * This solution is ugly, but we never write the whole table only
2263 * individual fields in it.
2264 * In reality this field should not be in that structure
2265 * but in a soft register.
2266 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002267 result = polaris10_read_smc_sram_dword(hwmgr->smumgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08002268 data->arb_table_start, &tmp, data->sram_end);
2269
2270 if (result)
2271 return result;
2272
2273 tmp &= 0x00FFFFFF;
2274 tmp |= ((uint32_t)MC_CG_ARB_FREQ_F1) << 24;
2275
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002276 return polaris10_write_smc_sram_dword(hwmgr->smumgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08002277 data->arb_table_start, tmp, data->sram_end);
2278}
2279
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002280static int polaris10_enable_vrhot_gpio_interrupt(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002281{
2282 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2283 PHM_PlatformCaps_RegulatorHot))
2284 return smum_send_msg_to_smc(hwmgr->smumgr,
2285 PPSMC_MSG_EnableVRHotGPIOInterrupt);
2286
2287 return 0;
2288}
2289
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002290static int polaris10_enable_sclk_control(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002291{
2292 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SCLK_PWRMGT_CNTL,
2293 SCLK_PWRMGT_OFF, 0);
2294 return 0;
2295}
2296
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002297static int polaris10_enable_ulv(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002298{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002299 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
2300 struct polaris10_ulv_parm *ulv = &(data->ulv);
Rex Zhua23eefa2015-11-19 18:23:32 +08002301
2302 if (ulv->ulv_supported)
2303 return smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_EnableULV);
2304
2305 return 0;
2306}
2307
Eric Huangc27371b2016-06-06 16:42:46 -04002308static int polaris10_disable_ulv(struct pp_hwmgr *hwmgr)
2309{
2310 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
2311 struct polaris10_ulv_parm *ulv = &(data->ulv);
2312
2313 if (ulv->ulv_supported)
2314 return smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_DisableULV);
2315
2316 return 0;
2317}
2318
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002319static int polaris10_enable_deep_sleep_master_switch(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002320{
2321 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2322 PHM_PlatformCaps_SclkDeepSleep)) {
2323 if (smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_MASTER_DeepSleep_ON))
2324 PP_ASSERT_WITH_CODE(false,
2325 "Attempt to enable Master Deep Sleep switch failed!",
2326 return -1);
2327 } else {
2328 if (smum_send_msg_to_smc(hwmgr->smumgr,
2329 PPSMC_MSG_MASTER_DeepSleep_OFF)) {
2330 PP_ASSERT_WITH_CODE(false,
2331 "Attempt to disable Master Deep Sleep switch failed!",
2332 return -1);
2333 }
2334 }
2335
2336 return 0;
2337}
2338
Eric Huangc27371b2016-06-06 16:42:46 -04002339static int polaris10_disable_deep_sleep_master_switch(struct pp_hwmgr *hwmgr)
2340{
2341 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2342 PHM_PlatformCaps_SclkDeepSleep)) {
2343 if (smum_send_msg_to_smc(hwmgr->smumgr,
2344 PPSMC_MSG_MASTER_DeepSleep_OFF)) {
2345 PP_ASSERT_WITH_CODE(false,
2346 "Attempt to disable Master Deep Sleep switch failed!",
2347 return -1);
2348 }
2349 }
2350
2351 return 0;
2352}
2353
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002354static int polaris10_enable_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002355{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002356 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhu40787ef2016-06-08 19:41:00 +08002357 uint32_t soft_register_value = 0;
2358 uint32_t handshake_disables_offset = data->soft_regs_start
2359 + offsetof(SMU74_SoftRegisters, HandshakeDisables);
Rex Zhua23eefa2015-11-19 18:23:32 +08002360
2361 /* enable SCLK dpm */
2362 if (!data->sclk_dpm_key_disabled)
2363 PP_ASSERT_WITH_CODE(
2364 (0 == smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_DPM_Enable)),
2365 "Failed to enable SCLK DPM during DPM Start Function!",
2366 return -1);
2367
2368 /* enable MCLK dpm */
2369 if (0 == data->mclk_dpm_key_disabled) {
Rex Zhu40787ef2016-06-08 19:41:00 +08002370/* Disable UVD - SMU handshake for MCLK. */
2371 soft_register_value = cgs_read_ind_register(hwmgr->device,
2372 CGS_IND_REG__SMC, handshake_disables_offset);
2373 soft_register_value |= SMU7_UVD_MCLK_HANDSHAKE_DISABLE;
2374 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
2375 handshake_disables_offset, soft_register_value);
Rex Zhua23eefa2015-11-19 18:23:32 +08002376
2377 PP_ASSERT_WITH_CODE(
2378 (0 == smum_send_msg_to_smc(hwmgr->smumgr,
2379 PPSMC_MSG_MCLKDPM_Enable)),
2380 "Failed to enable MCLK DPM during DPM Start Function!",
2381 return -1);
2382
Rex Zhua23eefa2015-11-19 18:23:32 +08002383 PHM_WRITE_FIELD(hwmgr->device, MC_SEQ_CNTL_3, CAC_EN, 0x1);
2384
2385 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC0_CNTL, 0x5);
2386 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC1_CNTL, 0x5);
2387 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_CPL_CNTL, 0x100005);
2388 udelay(10);
2389 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC0_CNTL, 0x400005);
2390 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_MC1_CNTL, 0x400005);
2391 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixLCAC_CPL_CNTL, 0x500005);
2392 }
2393
2394 return 0;
2395}
2396
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002397static int polaris10_start_dpm(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002398{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002399 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08002400
2401 /*enable general power management */
2402
2403 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
2404 GLOBAL_PWRMGT_EN, 1);
2405
2406 /* enable sclk deep sleep */
2407
2408 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SCLK_PWRMGT_CNTL,
2409 DYNAMIC_PM_EN, 1);
2410
2411 /* prepare for PCIE DPM */
2412
2413 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
2414 data->soft_regs_start + offsetof(SMU74_SoftRegisters,
2415 VoltageChangeTimeout), 0x1000);
2416 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__PCIE,
2417 SWRST_COMMAND_1, RESETLC, 0x0);
yanyang1e85c7d62016-02-06 13:28:47 +08002418/*
Rex Zhua23eefa2015-11-19 18:23:32 +08002419 PP_ASSERT_WITH_CODE(
2420 (0 == smum_send_msg_to_smc(hwmgr->smumgr,
2421 PPSMC_MSG_Voltage_Cntl_Enable)),
2422 "Failed to enable voltage DPM during DPM Start Function!",
2423 return -1);
yanyang1e85c7d62016-02-06 13:28:47 +08002424*/
Rex Zhua23eefa2015-11-19 18:23:32 +08002425
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002426 if (polaris10_enable_sclk_mclk_dpm(hwmgr)) {
Rex Zhua23eefa2015-11-19 18:23:32 +08002427 printk(KERN_ERR "Failed to enable Sclk DPM and Mclk DPM!");
2428 return -1;
2429 }
2430
2431 /* enable PCIE dpm */
2432 if (0 == data->pcie_dpm_key_disabled) {
2433 PP_ASSERT_WITH_CODE(
2434 (0 == smum_send_msg_to_smc(hwmgr->smumgr,
2435 PPSMC_MSG_PCIeDPM_Enable)),
2436 "Failed to enable pcie DPM during DPM Start Function!",
2437 return -1);
2438 }
2439
Eric Huangc8c67442016-03-24 16:44:18 -04002440 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2441 PHM_PlatformCaps_Falcon_QuickTransition)) {
2442 PP_ASSERT_WITH_CODE((0 == smum_send_msg_to_smc(hwmgr->smumgr,
2443 PPSMC_MSG_EnableACDCGPIOInterrupt)),
2444 "Failed to enable AC DC GPIO Interrupt!",
2445 );
2446 }
Rex Zhua23eefa2015-11-19 18:23:32 +08002447
2448 return 0;
2449}
2450
Eric Huangc27371b2016-06-06 16:42:46 -04002451static int polaris10_disable_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
2452{
2453 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
2454
2455 /* disable SCLK dpm */
2456 if (!data->sclk_dpm_key_disabled)
2457 PP_ASSERT_WITH_CODE(
2458 (smum_send_msg_to_smc(hwmgr->smumgr,
2459 PPSMC_MSG_DPM_Disable) == 0),
2460 "Failed to disable SCLK DPM!",
2461 return -1);
2462
2463 /* disable MCLK dpm */
2464 if (!data->mclk_dpm_key_disabled) {
2465 PP_ASSERT_WITH_CODE(
2466 (smum_send_msg_to_smc(hwmgr->smumgr,
2467 PPSMC_MSG_MCLKDPM_Disable) == 0),
2468 "Failed to disable MCLK DPM!",
2469 return -1);
2470 }
2471
2472 return 0;
2473}
2474
2475static int polaris10_stop_dpm(struct pp_hwmgr *hwmgr)
2476{
2477 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
2478
2479 /* disable general power management */
2480 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
2481 GLOBAL_PWRMGT_EN, 0);
2482 /* disable sclk deep sleep */
2483 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, SCLK_PWRMGT_CNTL,
2484 DYNAMIC_PM_EN, 0);
2485
2486 /* disable PCIE dpm */
2487 if (!data->pcie_dpm_key_disabled) {
2488 PP_ASSERT_WITH_CODE(
2489 (smum_send_msg_to_smc(hwmgr->smumgr,
2490 PPSMC_MSG_PCIeDPM_Disable) == 0),
2491 "Failed to disable pcie DPM during DPM Stop Function!",
2492 return -1);
2493 }
2494
2495 if (polaris10_disable_sclk_mclk_dpm(hwmgr)) {
2496 printk(KERN_ERR "Failed to disable Sclk DPM and Mclk DPM!");
2497 return -1;
2498 }
2499
2500 return 0;
2501}
2502
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002503static void polaris10_set_dpm_event_sources(struct pp_hwmgr *hwmgr, uint32_t sources)
Rex Zhua23eefa2015-11-19 18:23:32 +08002504{
2505 bool protection;
2506 enum DPM_EVENT_SRC src;
2507
2508 switch (sources) {
2509 default:
2510 printk(KERN_ERR "Unknown throttling event sources.");
2511 /* fall through */
2512 case 0:
2513 protection = false;
2514 /* src is unused */
2515 break;
2516 case (1 << PHM_AutoThrottleSource_Thermal):
2517 protection = true;
2518 src = DPM_EVENT_SRC_DIGITAL;
2519 break;
2520 case (1 << PHM_AutoThrottleSource_External):
2521 protection = true;
2522 src = DPM_EVENT_SRC_EXTERNAL;
2523 break;
2524 case (1 << PHM_AutoThrottleSource_External) |
2525 (1 << PHM_AutoThrottleSource_Thermal):
2526 protection = true;
2527 src = DPM_EVENT_SRC_DIGITAL_OR_EXTERNAL;
2528 break;
2529 }
2530 /* Order matters - don't enable thermal protection for the wrong source. */
2531 if (protection) {
2532 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, CG_THERMAL_CTRL,
2533 DPM_EVENT_SRC, src);
2534 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
2535 THERMAL_PROTECTION_DIS,
Rex Zhuf0911de2016-03-23 14:50:22 +08002536 !phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
Rex Zhua23eefa2015-11-19 18:23:32 +08002537 PHM_PlatformCaps_ThermalController));
2538 } else
2539 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, GENERAL_PWRMGT,
2540 THERMAL_PROTECTION_DIS, 1);
2541}
2542
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002543static int polaris10_enable_auto_throttle_source(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08002544 PHM_AutoThrottleSource source)
2545{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002546 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08002547
2548 if (!(data->active_auto_throttle_sources & (1 << source))) {
2549 data->active_auto_throttle_sources |= 1 << source;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002550 polaris10_set_dpm_event_sources(hwmgr, data->active_auto_throttle_sources);
Rex Zhua23eefa2015-11-19 18:23:32 +08002551 }
2552 return 0;
2553}
2554
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002555static int polaris10_enable_thermal_auto_throttle(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002556{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002557 return polaris10_enable_auto_throttle_source(hwmgr, PHM_AutoThrottleSource_Thermal);
Rex Zhua23eefa2015-11-19 18:23:32 +08002558}
2559
Eric Huangc27371b2016-06-06 16:42:46 -04002560static int polaris10_disable_auto_throttle_source(struct pp_hwmgr *hwmgr,
2561 PHM_AutoThrottleSource source)
2562{
2563 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
2564
2565 if (data->active_auto_throttle_sources & (1 << source)) {
2566 data->active_auto_throttle_sources &= ~(1 << source);
2567 polaris10_set_dpm_event_sources(hwmgr, data->active_auto_throttle_sources);
2568 }
2569 return 0;
2570}
2571
2572static int polaris10_disable_thermal_auto_throttle(struct pp_hwmgr *hwmgr)
2573{
2574 return polaris10_disable_auto_throttle_source(hwmgr, PHM_AutoThrottleSource_Thermal);
2575}
2576
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002577int polaris10_pcie_performance_request(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002578{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002579 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08002580 data->pcie_performance_request = true;
2581
2582 return 0;
2583}
2584
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002585int polaris10_enable_dpm_tasks(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002586{
2587 int tmp_result, result = 0;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002588 tmp_result = (!polaris10_is_dpm_running(hwmgr)) ? 0 : -1;
Rex Zhua23eefa2015-11-19 18:23:32 +08002589 PP_ASSERT_WITH_CODE(result == 0,
2590 "DPM is already running right now, no need to enable DPM!",
2591 return 0);
2592
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002593 if (polaris10_voltage_control(hwmgr)) {
2594 tmp_result = polaris10_enable_voltage_control(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002595 PP_ASSERT_WITH_CODE(tmp_result == 0,
2596 "Failed to enable voltage control!",
2597 result = tmp_result);
2598
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002599 tmp_result = polaris10_construct_voltage_tables(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002600 PP_ASSERT_WITH_CODE((0 == tmp_result),
2601 "Failed to contruct voltage tables!",
2602 result = tmp_result);
2603 }
2604
2605 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2606 PHM_PlatformCaps_EngineSpreadSpectrumSupport))
2607 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
2608 GENERAL_PWRMGT, DYN_SPREAD_SPECTRUM_EN, 1);
2609
2610 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2611 PHM_PlatformCaps_ThermalController))
2612 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
2613 GENERAL_PWRMGT, THERMAL_PROTECTION_DIS, 0);
2614
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002615 tmp_result = polaris10_program_static_screen_threshold_parameters(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002616 PP_ASSERT_WITH_CODE((0 == tmp_result),
2617 "Failed to program static screen threshold parameters!",
2618 result = tmp_result);
2619
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002620 tmp_result = polaris10_enable_display_gap(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002621 PP_ASSERT_WITH_CODE((0 == tmp_result),
2622 "Failed to enable display gap!", result = tmp_result);
2623
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002624 tmp_result = polaris10_program_voting_clients(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002625 PP_ASSERT_WITH_CODE((0 == tmp_result),
2626 "Failed to program voting clients!", result = tmp_result);
2627
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002628 tmp_result = polaris10_process_firmware_header(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002629 PP_ASSERT_WITH_CODE((0 == tmp_result),
2630 "Failed to process firmware header!", result = tmp_result);
2631
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002632 tmp_result = polaris10_initial_switch_from_arbf0_to_f1(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002633 PP_ASSERT_WITH_CODE((0 == tmp_result),
2634 "Failed to initialize switch from ArbF0 to F1!",
2635 result = tmp_result);
2636
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002637 tmp_result = polaris10_init_smc_table(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002638 PP_ASSERT_WITH_CODE((0 == tmp_result),
2639 "Failed to initialize SMC table!", result = tmp_result);
2640
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002641 tmp_result = polaris10_init_arb_table_index(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002642 PP_ASSERT_WITH_CODE((0 == tmp_result),
2643 "Failed to initialize ARB table index!", result = tmp_result);
2644
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002645 tmp_result = polaris10_populate_pm_fuses(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002646 PP_ASSERT_WITH_CODE((0 == tmp_result),
2647 "Failed to populate PM fuses!", result = tmp_result);
2648
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002649 tmp_result = polaris10_enable_vrhot_gpio_interrupt(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002650 PP_ASSERT_WITH_CODE((0 == tmp_result),
2651 "Failed to enable VR hot GPIO interrupt!", result = tmp_result);
2652
Rex Zhu83a7af62016-06-23 11:05:00 +08002653 smum_send_msg_to_smc(hwmgr->smumgr, (PPSMC_Msg)PPSMC_HasDisplay);
2654
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002655 tmp_result = polaris10_enable_sclk_control(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002656 PP_ASSERT_WITH_CODE((0 == tmp_result),
2657 "Failed to enable SCLK control!", result = tmp_result);
2658
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002659 tmp_result = polaris10_enable_smc_voltage_controller(hwmgr);
yanyang1e85c7d62016-02-06 13:28:47 +08002660 PP_ASSERT_WITH_CODE((0 == tmp_result),
2661 "Failed to enable voltage control!", result = tmp_result);
2662
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002663 tmp_result = polaris10_enable_ulv(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002664 PP_ASSERT_WITH_CODE((0 == tmp_result),
2665 "Failed to enable ULV!", result = tmp_result);
2666
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002667 tmp_result = polaris10_enable_deep_sleep_master_switch(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002668 PP_ASSERT_WITH_CODE((0 == tmp_result),
2669 "Failed to enable deep sleep master switch!", result = tmp_result);
2670
Rex Zhu36e6b912016-06-08 12:56:20 +08002671 tmp_result = polaris10_enable_didt_config(hwmgr);
2672 PP_ASSERT_WITH_CODE((tmp_result == 0),
2673 "Failed to enable deep sleep master switch!", result = tmp_result);
2674
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002675 tmp_result = polaris10_start_dpm(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002676 PP_ASSERT_WITH_CODE((0 == tmp_result),
2677 "Failed to start DPM!", result = tmp_result);
2678
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002679 tmp_result = polaris10_enable_smc_cac(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002680 PP_ASSERT_WITH_CODE((0 == tmp_result),
2681 "Failed to enable SMC CAC!", result = tmp_result);
2682
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002683 tmp_result = polaris10_enable_power_containment(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002684 PP_ASSERT_WITH_CODE((0 == tmp_result),
2685 "Failed to enable power containment!", result = tmp_result);
2686
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002687 tmp_result = polaris10_power_control_set_level(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002688 PP_ASSERT_WITH_CODE((0 == tmp_result),
2689 "Failed to power control set level!", result = tmp_result);
2690
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002691 tmp_result = polaris10_enable_thermal_auto_throttle(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002692 PP_ASSERT_WITH_CODE((0 == tmp_result),
2693 "Failed to enable thermal auto throttle!", result = tmp_result);
2694
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002695 tmp_result = polaris10_pcie_performance_request(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002696 PP_ASSERT_WITH_CODE((0 == tmp_result),
Alex Deucher5f885672016-03-25 12:23:49 -04002697 "pcie performance request failed!", result = tmp_result);
Rex Zhua23eefa2015-11-19 18:23:32 +08002698
2699 return result;
2700}
2701
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002702int polaris10_disable_dpm_tasks(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002703{
Eric Huangc27371b2016-06-06 16:42:46 -04002704 int tmp_result, result = 0;
Rex Zhua23eefa2015-11-19 18:23:32 +08002705
Eric Huangc27371b2016-06-06 16:42:46 -04002706 tmp_result = (polaris10_is_dpm_running(hwmgr)) ? 0 : -1;
2707 PP_ASSERT_WITH_CODE(tmp_result == 0,
2708 "DPM is not running right now, no need to disable DPM!",
2709 return 0);
2710
2711 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2712 PHM_PlatformCaps_ThermalController))
2713 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
2714 GENERAL_PWRMGT, THERMAL_PROTECTION_DIS, 1);
2715
2716 tmp_result = polaris10_disable_power_containment(hwmgr);
2717 PP_ASSERT_WITH_CODE((tmp_result == 0),
2718 "Failed to disable power containment!", result = tmp_result);
2719
2720 tmp_result = polaris10_disable_smc_cac(hwmgr);
2721 PP_ASSERT_WITH_CODE((tmp_result == 0),
2722 "Failed to disable SMC CAC!", result = tmp_result);
2723
2724 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
2725 CG_SPLL_SPREAD_SPECTRUM, SSEN, 0);
2726 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
2727 GENERAL_PWRMGT, DYN_SPREAD_SPECTRUM_EN, 0);
2728
2729 tmp_result = polaris10_disable_thermal_auto_throttle(hwmgr);
2730 PP_ASSERT_WITH_CODE((tmp_result == 0),
2731 "Failed to disable thermal auto throttle!", result = tmp_result);
2732
2733 tmp_result = polaris10_stop_dpm(hwmgr);
2734 PP_ASSERT_WITH_CODE((tmp_result == 0),
2735 "Failed to stop DPM!", result = tmp_result);
2736
2737 tmp_result = polaris10_disable_deep_sleep_master_switch(hwmgr);
2738 PP_ASSERT_WITH_CODE((tmp_result == 0),
2739 "Failed to disable deep sleep master switch!", result = tmp_result);
2740
2741 tmp_result = polaris10_disable_ulv(hwmgr);
2742 PP_ASSERT_WITH_CODE((tmp_result == 0),
2743 "Failed to disable ULV!", result = tmp_result);
2744
2745 tmp_result = polaris10_clear_voting_clients(hwmgr);
2746 PP_ASSERT_WITH_CODE((tmp_result == 0),
2747 "Failed to clear voting clients!", result = tmp_result);
2748
2749 tmp_result = polaris10_reset_to_default(hwmgr);
2750 PP_ASSERT_WITH_CODE((tmp_result == 0),
2751 "Failed to reset to default!", result = tmp_result);
2752
2753 tmp_result = polaris10_force_switch_to_arbf0(hwmgr);
2754 PP_ASSERT_WITH_CODE((tmp_result == 0),
2755 "Failed to force to switch arbf0!", result = tmp_result);
2756
2757 return result;
Rex Zhua23eefa2015-11-19 18:23:32 +08002758}
2759
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002760int polaris10_reset_asic_tasks(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002761{
2762
2763 return 0;
2764}
2765
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002766int polaris10_hwmgr_backend_fini(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002767{
2768 return phm_hwmgr_backend_fini(hwmgr);
2769}
2770
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002771int polaris10_set_features_platform_caps(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002772{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002773 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08002774
2775 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2776 PHM_PlatformCaps_SclkDeepSleep);
2777
Rex Zhuf0911de2016-03-23 14:50:22 +08002778 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2779 PHM_PlatformCaps_DynamicPatchPowerState);
2780
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002781 if (data->mvdd_control == POLARIS10_VOLTAGE_CONTROL_NONE)
Rex Zhua23eefa2015-11-19 18:23:32 +08002782 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2783 PHM_PlatformCaps_EnableMVDDControl);
2784
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002785 if (data->vddci_control == POLARIS10_VOLTAGE_CONTROL_NONE)
Rex Zhua23eefa2015-11-19 18:23:32 +08002786 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2787 PHM_PlatformCaps_ControlVDDCI);
2788
2789 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2790 PHM_PlatformCaps_TablelessHardwareInterface);
2791
2792 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2793 PHM_PlatformCaps_EnableSMU7ThermalManagement);
2794
2795 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2796 PHM_PlatformCaps_DynamicPowerManagement);
2797
2798 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
Rex Zhuf0911de2016-03-23 14:50:22 +08002799 PHM_PlatformCaps_UnTabledHardwareInterface);
2800
2801 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
Rex Zhua23eefa2015-11-19 18:23:32 +08002802 PHM_PlatformCaps_TablelessHardwareInterface);
2803
2804 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2805 PHM_PlatformCaps_SMC);
2806
2807 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2808 PHM_PlatformCaps_NonABMSupportInPPLib);
2809
2810 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2811 PHM_PlatformCaps_DynamicUVDState);
2812
Rex Zhua23eefa2015-11-19 18:23:32 +08002813 /* power tune caps Assume disabled */
Rex Zhu36e6b912016-06-08 12:56:20 +08002814 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
Rex Zhua23eefa2015-11-19 18:23:32 +08002815 PHM_PlatformCaps_SQRamping);
Rex Zhu36e6b912016-06-08 12:56:20 +08002816 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
Rex Zhua23eefa2015-11-19 18:23:32 +08002817 PHM_PlatformCaps_DBRamping);
Rex Zhu36e6b912016-06-08 12:56:20 +08002818 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
Rex Zhua23eefa2015-11-19 18:23:32 +08002819 PHM_PlatformCaps_TDRamping);
Rex Zhu36e6b912016-06-08 12:56:20 +08002820 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
Rex Zhua23eefa2015-11-19 18:23:32 +08002821 PHM_PlatformCaps_TCPRamping);
2822
Huang Rui6bb6b292016-05-24 13:47:05 +08002823 if (hwmgr->powercontainment_enabled)
2824 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2825 PHM_PlatformCaps_PowerContainment);
2826 else
2827 phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
2828 PHM_PlatformCaps_PowerContainment);
2829
Rex Zhuf0911de2016-03-23 14:50:22 +08002830 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2831 PHM_PlatformCaps_CAC);
2832
2833 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2834 PHM_PlatformCaps_RegulatorHot);
2835
2836 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2837 PHM_PlatformCaps_AutomaticDCTransition);
2838
2839 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2840 PHM_PlatformCaps_ODFuzzyFanControlSupport);
2841
2842 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2843 PHM_PlatformCaps_FanSpeedInTableIsRPM);
Rex Zhu919e3342016-05-11 17:04:07 +08002844
Rex Zhu5de95e52016-03-22 14:21:18 +08002845 if (hwmgr->chip_id == CHIP_POLARIS11)
2846 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
2847 PHM_PlatformCaps_SPLLShutdownSupport);
Rex Zhua23eefa2015-11-19 18:23:32 +08002848 return 0;
2849}
2850
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002851static void polaris10_init_dpm_defaults(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002852{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002853 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08002854
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002855 polaris10_initialize_power_tune_defaults(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08002856
2857 data->pcie_gen_performance.max = PP_PCIEGen1;
2858 data->pcie_gen_performance.min = PP_PCIEGen3;
2859 data->pcie_gen_power_saving.max = PP_PCIEGen1;
2860 data->pcie_gen_power_saving.min = PP_PCIEGen3;
2861 data->pcie_lane_performance.max = 0;
2862 data->pcie_lane_performance.min = 16;
2863 data->pcie_lane_power_saving.max = 0;
2864 data->pcie_lane_power_saving.min = 16;
2865}
2866
2867/**
2868* Get Leakage VDDC based on leakage ID.
2869*
2870* @param hwmgr the address of the powerplay hardware manager.
2871* @return always 0
2872*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002873static int polaris10_get_evv_voltages(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08002874{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002875 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08002876 uint16_t vv_id;
Rex Zhue5eb3712016-06-29 16:37:35 +08002877 uint32_t vddc = 0;
Rex Zhua23eefa2015-11-19 18:23:32 +08002878 uint16_t i, j;
2879 uint32_t sclk = 0;
2880 struct phm_ppt_v1_information *table_info =
2881 (struct phm_ppt_v1_information *)hwmgr->pptable;
2882 struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
2883 table_info->vdd_dep_on_sclk;
2884 int result;
2885
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002886 for (i = 0; i < POLARIS10_MAX_LEAKAGE_COUNT; i++) {
Rex Zhua23eefa2015-11-19 18:23:32 +08002887 vv_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
2888 if (!phm_get_sclk_for_voltage_evv(hwmgr,
2889 table_info->vddc_lookup_table, vv_id, &sclk)) {
2890 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
2891 PHM_PlatformCaps_ClockStretcher)) {
2892 for (j = 1; j < sclk_table->count; j++) {
2893 if (sclk_table->entries[j].clk == sclk &&
2894 sclk_table->entries[j].cks_enable == 0) {
2895 sclk += 5000;
2896 break;
2897 }
2898 }
2899 }
2900
Rex Zhub1814a12016-07-05 19:18:15 +08002901 if (atomctrl_get_voltage_evv_on_sclk_ai(hwmgr,
2902 VOLTAGE_TYPE_VDDC,
2903 sclk, vv_id, &vddc) != 0) {
2904 printk(KERN_WARNING "failed to retrieving EVV voltage!\n");
2905 continue;
2906 }
Rex Zhua23eefa2015-11-19 18:23:32 +08002907
Rex Zhue5eb3712016-06-29 16:37:35 +08002908 /* need to make sure vddc is less than 2v or else, it could burn the ASIC.
2909 * real voltage level in unit of 0.01mv */
2910 PP_ASSERT_WITH_CODE((vddc < 200000 && vddc != 0),
Rex Zhua23eefa2015-11-19 18:23:32 +08002911 "Invalid VDDC value", result = -EINVAL;);
2912
2913 /* the voltage should not be zero nor equal to leakage ID */
2914 if (vddc != 0 && vddc != vv_id) {
2915 data->vddc_leakage.actual_voltage[data->vddc_leakage.count] = (uint16_t)(vddc/100);
2916 data->vddc_leakage.leakage_id[data->vddc_leakage.count] = vv_id;
2917 data->vddc_leakage.count++;
2918 }
2919 }
2920 }
2921
2922 return 0;
2923}
2924
2925/**
2926 * Change virtual leakage voltage to actual value.
2927 *
2928 * @param hwmgr the address of the powerplay hardware manager.
2929 * @param pointer to changing voltage
2930 * @param pointer to leakage table
2931 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002932static void polaris10_patch_with_vdd_leakage(struct pp_hwmgr *hwmgr,
2933 uint16_t *voltage, struct polaris10_leakage_voltage *leakage_table)
Rex Zhua23eefa2015-11-19 18:23:32 +08002934{
2935 uint32_t index;
2936
2937 /* search for leakage voltage ID 0xff01 ~ 0xff08 */
2938 for (index = 0; index < leakage_table->count; index++) {
2939 /* if this voltage matches a leakage voltage ID */
2940 /* patch with actual leakage voltage */
2941 if (leakage_table->leakage_id[index] == *voltage) {
2942 *voltage = leakage_table->actual_voltage[index];
2943 break;
2944 }
2945 }
2946
2947 if (*voltage > ATOM_VIRTUAL_VOLTAGE_ID0)
2948 printk(KERN_ERR "Voltage value looks like a Leakage ID but it's not patched \n");
2949}
2950
2951/**
2952* Patch voltage lookup table by EVV leakages.
2953*
2954* @param hwmgr the address of the powerplay hardware manager.
2955* @param pointer to voltage lookup table
2956* @param pointer to leakage table
2957* @return always 0
2958*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002959static int polaris10_patch_lookup_table_with_leakage(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08002960 phm_ppt_v1_voltage_lookup_table *lookup_table,
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002961 struct polaris10_leakage_voltage *leakage_table)
Rex Zhua23eefa2015-11-19 18:23:32 +08002962{
2963 uint32_t i;
2964
2965 for (i = 0; i < lookup_table->count; i++)
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002966 polaris10_patch_with_vdd_leakage(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08002967 &lookup_table->entries[i].us_vdd, leakage_table);
2968
2969 return 0;
2970}
2971
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002972static int polaris10_patch_clock_voltage_limits_with_vddc_leakage(
2973 struct pp_hwmgr *hwmgr, struct polaris10_leakage_voltage *leakage_table,
Rex Zhua23eefa2015-11-19 18:23:32 +08002974 uint16_t *vddc)
2975{
2976 struct phm_ppt_v1_information *table_info =
2977 (struct phm_ppt_v1_information *)(hwmgr->pptable);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002978 polaris10_patch_with_vdd_leakage(hwmgr, (uint16_t *)vddc, leakage_table);
Rex Zhua23eefa2015-11-19 18:23:32 +08002979 hwmgr->dyn_state.max_clock_voltage_on_dc.vddc =
2980 table_info->max_clock_voltage_on_dc.vddc;
2981 return 0;
2982}
2983
Flora Cui2cc0c0b2016-03-14 18:33:29 -04002984static int polaris10_patch_voltage_dependency_tables_with_lookup_table(
Rex Zhua23eefa2015-11-19 18:23:32 +08002985 struct pp_hwmgr *hwmgr)
2986{
2987 uint8_t entryId;
2988 uint8_t voltageId;
2989 struct phm_ppt_v1_information *table_info =
2990 (struct phm_ppt_v1_information *)(hwmgr->pptable);
2991
2992 struct phm_ppt_v1_clock_voltage_dependency_table *sclk_table =
2993 table_info->vdd_dep_on_sclk;
2994 struct phm_ppt_v1_clock_voltage_dependency_table *mclk_table =
2995 table_info->vdd_dep_on_mclk;
2996 struct phm_ppt_v1_mm_clock_voltage_dependency_table *mm_table =
2997 table_info->mm_dep_table;
2998
2999 for (entryId = 0; entryId < sclk_table->count; ++entryId) {
3000 voltageId = sclk_table->entries[entryId].vddInd;
3001 sclk_table->entries[entryId].vddc =
3002 table_info->vddc_lookup_table->entries[voltageId].us_vdd;
3003 }
3004
3005 for (entryId = 0; entryId < mclk_table->count; ++entryId) {
3006 voltageId = mclk_table->entries[entryId].vddInd;
3007 mclk_table->entries[entryId].vddc =
3008 table_info->vddc_lookup_table->entries[voltageId].us_vdd;
3009 }
3010
3011 for (entryId = 0; entryId < mm_table->count; ++entryId) {
3012 voltageId = mm_table->entries[entryId].vddcInd;
3013 mm_table->entries[entryId].vddc =
3014 table_info->vddc_lookup_table->entries[voltageId].us_vdd;
3015 }
3016
3017 return 0;
3018
3019}
3020
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003021static int polaris10_calc_voltage_dependency_tables(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003022{
3023 /* Need to determine if we need calculated voltage. */
3024 return 0;
3025}
3026
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003027static int polaris10_calc_mm_voltage_dependency_table(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003028{
3029 /* Need to determine if we need calculated voltage from mm table. */
3030 return 0;
3031}
3032
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003033static int polaris10_sort_lookup_table(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08003034 struct phm_ppt_v1_voltage_lookup_table *lookup_table)
3035{
3036 uint32_t table_size, i, j;
3037 struct phm_ppt_v1_voltage_lookup_record tmp_voltage_lookup_record;
3038 table_size = lookup_table->count;
3039
3040 PP_ASSERT_WITH_CODE(0 != lookup_table->count,
3041 "Lookup table is empty", return -EINVAL);
3042
3043 /* Sorting voltages */
3044 for (i = 0; i < table_size - 1; i++) {
3045 for (j = i + 1; j > 0; j--) {
3046 if (lookup_table->entries[j].us_vdd <
3047 lookup_table->entries[j - 1].us_vdd) {
3048 tmp_voltage_lookup_record = lookup_table->entries[j - 1];
3049 lookup_table->entries[j - 1] = lookup_table->entries[j];
3050 lookup_table->entries[j] = tmp_voltage_lookup_record;
3051 }
3052 }
3053 }
3054
3055 return 0;
3056}
3057
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003058static int polaris10_complete_dependency_tables(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003059{
3060 int result = 0;
3061 int tmp_result;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003062 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08003063 struct phm_ppt_v1_information *table_info =
3064 (struct phm_ppt_v1_information *)(hwmgr->pptable);
3065
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003066 tmp_result = polaris10_patch_lookup_table_with_leakage(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08003067 table_info->vddc_lookup_table, &(data->vddc_leakage));
3068 if (tmp_result)
3069 result = tmp_result;
3070
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003071 tmp_result = polaris10_patch_clock_voltage_limits_with_vddc_leakage(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08003072 &(data->vddc_leakage), &table_info->max_clock_voltage_on_dc.vddc);
3073 if (tmp_result)
3074 result = tmp_result;
3075
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003076 tmp_result = polaris10_patch_voltage_dependency_tables_with_lookup_table(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003077 if (tmp_result)
3078 result = tmp_result;
3079
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003080 tmp_result = polaris10_calc_voltage_dependency_tables(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003081 if (tmp_result)
3082 result = tmp_result;
3083
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003084 tmp_result = polaris10_calc_mm_voltage_dependency_table(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003085 if (tmp_result)
3086 result = tmp_result;
3087
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003088 tmp_result = polaris10_sort_lookup_table(hwmgr, table_info->vddc_lookup_table);
Rex Zhua23eefa2015-11-19 18:23:32 +08003089 if (tmp_result)
3090 result = tmp_result;
3091
3092 return result;
3093}
3094
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003095static int polaris10_set_private_data_based_on_pptable(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003096{
3097 struct phm_ppt_v1_information *table_info =
3098 (struct phm_ppt_v1_information *)(hwmgr->pptable);
3099
3100 struct phm_ppt_v1_clock_voltage_dependency_table *allowed_sclk_vdd_table =
3101 table_info->vdd_dep_on_sclk;
3102 struct phm_ppt_v1_clock_voltage_dependency_table *allowed_mclk_vdd_table =
3103 table_info->vdd_dep_on_mclk;
3104
3105 PP_ASSERT_WITH_CODE(allowed_sclk_vdd_table != NULL,
Christian Königedf600d2016-05-03 15:54:54 +02003106 "VDD dependency on SCLK table is missing. \
Rex Zhua23eefa2015-11-19 18:23:32 +08003107 This table is mandatory", return -EINVAL);
3108 PP_ASSERT_WITH_CODE(allowed_sclk_vdd_table->count >= 1,
Christian Königedf600d2016-05-03 15:54:54 +02003109 "VDD dependency on SCLK table has to have is missing. \
Rex Zhua23eefa2015-11-19 18:23:32 +08003110 This table is mandatory", return -EINVAL);
3111
3112 PP_ASSERT_WITH_CODE(allowed_mclk_vdd_table != NULL,
Christian Königedf600d2016-05-03 15:54:54 +02003113 "VDD dependency on MCLK table is missing. \
Rex Zhua23eefa2015-11-19 18:23:32 +08003114 This table is mandatory", return -EINVAL);
3115 PP_ASSERT_WITH_CODE(allowed_mclk_vdd_table->count >= 1,
3116 "VDD dependency on MCLK table has to have is missing. \
3117 This table is mandatory", return -EINVAL);
3118
3119 table_info->max_clock_voltage_on_ac.sclk =
3120 allowed_sclk_vdd_table->entries[allowed_sclk_vdd_table->count - 1].clk;
3121 table_info->max_clock_voltage_on_ac.mclk =
3122 allowed_mclk_vdd_table->entries[allowed_mclk_vdd_table->count - 1].clk;
3123 table_info->max_clock_voltage_on_ac.vddc =
3124 allowed_sclk_vdd_table->entries[allowed_sclk_vdd_table->count - 1].vddc;
3125 table_info->max_clock_voltage_on_ac.vddci =
3126 allowed_mclk_vdd_table->entries[allowed_mclk_vdd_table->count - 1].vddci;
3127
Rex Zhuf0911de2016-03-23 14:50:22 +08003128 hwmgr->dyn_state.max_clock_voltage_on_ac.sclk = table_info->max_clock_voltage_on_ac.sclk;
3129 hwmgr->dyn_state.max_clock_voltage_on_ac.mclk = table_info->max_clock_voltage_on_ac.mclk;
3130 hwmgr->dyn_state.max_clock_voltage_on_ac.vddc = table_info->max_clock_voltage_on_ac.vddc;
3131 hwmgr->dyn_state.max_clock_voltage_on_ac.vddci =table_info->max_clock_voltage_on_ac.vddci;
3132
Rex Zhua23eefa2015-11-19 18:23:32 +08003133 return 0;
3134}
3135
Rex Zhu3a8bd712016-06-27 14:46:47 +08003136int polaris10_patch_voltage_workaround(struct pp_hwmgr *hwmgr)
3137{
3138 struct phm_ppt_v1_information *table_info =
3139 (struct phm_ppt_v1_information *)(hwmgr->pptable);
3140 struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table =
3141 table_info->vdd_dep_on_mclk;
3142 struct phm_ppt_v1_voltage_lookup_table *lookup_table =
3143 table_info->vddc_lookup_table;
3144 uint32_t i;
3145
3146 if (hwmgr->chip_id == CHIP_POLARIS10 && hwmgr->hw_revision == 0xC7) {
3147 if (lookup_table->entries[dep_mclk_table->entries[dep_mclk_table->count-1].vddInd].us_vdd >= 1000)
3148 return 0;
3149
3150 for (i = 0; i < lookup_table->count; i++) {
3151 if (lookup_table->entries[i].us_vdd < 0xff01 && lookup_table->entries[i].us_vdd >= 1000) {
3152 dep_mclk_table->entries[dep_mclk_table->count-1].vddInd = (uint8_t) i;
3153 return 0;
3154 }
3155 }
3156 }
3157 return 0;
3158}
3159
3160
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003161int polaris10_hwmgr_backend_init(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003162{
Eric Huang76ad42c2016-06-02 16:15:59 -04003163 struct polaris10_hwmgr *data;
Rex Zhua23eefa2015-11-19 18:23:32 +08003164 struct pp_atomctrl_gpio_pin_assignment gpio_pin_assignment;
3165 uint32_t temp_reg;
3166 int result;
Rex Zhuf0911de2016-03-23 14:50:22 +08003167 struct phm_ppt_v1_information *table_info =
3168 (struct phm_ppt_v1_information *)(hwmgr->pptable);
Rex Zhua23eefa2015-11-19 18:23:32 +08003169
Eric Huang76ad42c2016-06-02 16:15:59 -04003170 data = kzalloc(sizeof(struct polaris10_hwmgr), GFP_KERNEL);
3171 if (data == NULL)
3172 return -ENOMEM;
3173
3174 hwmgr->backend = data;
3175
Rex Zhua23eefa2015-11-19 18:23:32 +08003176 data->dll_default_on = false;
3177 data->sram_end = SMC_RAM_END;
Rex Zhu7d367c22016-04-01 19:56:07 +08003178 data->mclk_dpm0_activity_target = 0xa;
Rex Zhua23eefa2015-11-19 18:23:32 +08003179 data->disable_dpm_mask = 0xFF;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003180 data->static_screen_threshold = PPPOLARIS10_STATICSCREENTHRESHOLD_DFLT;
3181 data->static_screen_threshold_unit = PPPOLARIS10_STATICSCREENTHRESHOLD_DFLT;
3182 data->activity_target[0] = PPPOLARIS10_TARGETACTIVITY_DFLT;
3183 data->activity_target[1] = PPPOLARIS10_TARGETACTIVITY_DFLT;
3184 data->activity_target[2] = PPPOLARIS10_TARGETACTIVITY_DFLT;
3185 data->activity_target[3] = PPPOLARIS10_TARGETACTIVITY_DFLT;
3186 data->activity_target[4] = PPPOLARIS10_TARGETACTIVITY_DFLT;
3187 data->activity_target[5] = PPPOLARIS10_TARGETACTIVITY_DFLT;
3188 data->activity_target[6] = PPPOLARIS10_TARGETACTIVITY_DFLT;
3189 data->activity_target[7] = PPPOLARIS10_TARGETACTIVITY_DFLT;
Rex Zhua23eefa2015-11-19 18:23:32 +08003190
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003191 data->voting_rights_clients0 = PPPOLARIS10_VOTINGRIGHTSCLIENTS_DFLT0;
3192 data->voting_rights_clients1 = PPPOLARIS10_VOTINGRIGHTSCLIENTS_DFLT1;
3193 data->voting_rights_clients2 = PPPOLARIS10_VOTINGRIGHTSCLIENTS_DFLT2;
3194 data->voting_rights_clients3 = PPPOLARIS10_VOTINGRIGHTSCLIENTS_DFLT3;
3195 data->voting_rights_clients4 = PPPOLARIS10_VOTINGRIGHTSCLIENTS_DFLT4;
3196 data->voting_rights_clients5 = PPPOLARIS10_VOTINGRIGHTSCLIENTS_DFLT5;
3197 data->voting_rights_clients6 = PPPOLARIS10_VOTINGRIGHTSCLIENTS_DFLT6;
3198 data->voting_rights_clients7 = PPPOLARIS10_VOTINGRIGHTSCLIENTS_DFLT7;
Rex Zhua23eefa2015-11-19 18:23:32 +08003199
3200 data->vddc_vddci_delta = VDDC_VDDCI_DELTA;
3201
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003202 data->mclk_activity_target = PPPOLARIS10_MCLK_TARGETACTIVITY_DFLT;
Rex Zhua23eefa2015-11-19 18:23:32 +08003203
3204 /* need to set voltage control types before EVV patching */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003205 data->voltage_control = POLARIS10_VOLTAGE_CONTROL_NONE;
3206 data->vddci_control = POLARIS10_VOLTAGE_CONTROL_NONE;
3207 data->mvdd_control = POLARIS10_VOLTAGE_CONTROL_NONE;
Rex Zhua23eefa2015-11-19 18:23:32 +08003208
Rex Zhu919e3342016-05-11 17:04:07 +08003209 data->enable_tdc_limit_feature = true;
3210 data->enable_pkg_pwr_tracking_feature = true;
Rex Zhua2fb4932016-06-13 17:46:31 +08003211 data->force_pcie_gen = PP_PCIEGenInvalid;
Rex Zhu9a3c1b32016-06-08 19:42:48 +08003212 data->mclk_stutter_mode_threshold = 40000;
Rex Zhu919e3342016-05-11 17:04:07 +08003213
Rex Zhua23eefa2015-11-19 18:23:32 +08003214 if (atomctrl_is_voltage_controled_by_gpio_v3(hwmgr,
3215 VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_SVID2))
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003216 data->voltage_control = POLARIS10_VOLTAGE_CONTROL_BY_SVID2;
Rex Zhua23eefa2015-11-19 18:23:32 +08003217
Rex Zhua23eefa2015-11-19 18:23:32 +08003218 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3219 PHM_PlatformCaps_EnableMVDDControl)) {
3220 if (atomctrl_is_voltage_controled_by_gpio_v3(hwmgr,
3221 VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_GPIO_LUT))
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003222 data->mvdd_control = POLARIS10_VOLTAGE_CONTROL_BY_GPIO;
Rex Zhua23eefa2015-11-19 18:23:32 +08003223 else if (atomctrl_is_voltage_controled_by_gpio_v3(hwmgr,
3224 VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_SVID2))
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003225 data->mvdd_control = POLARIS10_VOLTAGE_CONTROL_BY_SVID2;
Rex Zhua23eefa2015-11-19 18:23:32 +08003226 }
3227
3228 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3229 PHM_PlatformCaps_ControlVDDCI)) {
3230 if (atomctrl_is_voltage_controled_by_gpio_v3(hwmgr,
3231 VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_GPIO_LUT))
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003232 data->vddci_control = POLARIS10_VOLTAGE_CONTROL_BY_GPIO;
Rex Zhua23eefa2015-11-19 18:23:32 +08003233 else if (atomctrl_is_voltage_controled_by_gpio_v3(hwmgr,
3234 VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_SVID2))
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003235 data->vddci_control = POLARIS10_VOLTAGE_CONTROL_BY_SVID2;
Rex Zhua23eefa2015-11-19 18:23:32 +08003236 }
3237
Rex Zhu270d0132016-06-07 18:39:06 +08003238 if (table_info->cac_dtp_table->usClockStretchAmount != 0)
3239 phm_cap_set(hwmgr->platform_descriptor.platformCaps,
3240 PHM_PlatformCaps_ClockStretcher);
3241
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003242 polaris10_set_features_platform_caps(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003243
Rex Zhu3a8bd712016-06-27 14:46:47 +08003244 polaris10_patch_voltage_workaround(hwmgr);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003245 polaris10_init_dpm_defaults(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003246
3247 /* Get leakage voltage based on leakage ID. */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003248 result = polaris10_get_evv_voltages(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003249
3250 if (result) {
3251 printk("Get EVV Voltage Failed. Abort Driver loading!\n");
3252 return -1;
3253 }
3254
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003255 polaris10_complete_dependency_tables(hwmgr);
3256 polaris10_set_private_data_based_on_pptable(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003257
3258 /* Initalize Dynamic State Adjustment Rule Settings */
3259 result = phm_initializa_dynamic_state_adjustment_rule_settings(hwmgr);
3260
3261 if (0 == result) {
3262 struct cgs_system_info sys_info = {0};
3263
Edward O'Callaghaned5121a2016-07-12 10:17:52 +10003264 data->is_tlu_enabled = false;
Rex Zhua23eefa2015-11-19 18:23:32 +08003265
3266 hwmgr->platform_descriptor.hardwareActivityPerformanceLevels =
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003267 POLARIS10_MAX_HARDWARE_POWERLEVELS;
Rex Zhua23eefa2015-11-19 18:23:32 +08003268 hwmgr->platform_descriptor.hardwarePerformanceLevels = 2;
3269 hwmgr->platform_descriptor.minimumClocksReductionPercentage = 50;
Rex Zhuf0911de2016-03-23 14:50:22 +08003270
Rex Zhua23eefa2015-11-19 18:23:32 +08003271
3272 if (atomctrl_get_pp_assign_pin(hwmgr, VDDC_PCC_GPIO_PINID, &gpio_pin_assignment)) {
3273 temp_reg = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCNB_PWRMGT_CNTL);
3274 switch (gpio_pin_assignment.uc_gpio_pin_bit_shift) {
3275 case 0:
3276 temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, GNB_SLOW_MODE, 0x1);
3277 break;
3278 case 1:
3279 temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, GNB_SLOW_MODE, 0x2);
3280 break;
3281 case 2:
3282 temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, GNB_SLOW, 0x1);
3283 break;
3284 case 3:
3285 temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, FORCE_NB_PS1, 0x1);
3286 break;
3287 case 4:
3288 temp_reg = PHM_SET_FIELD(temp_reg, CNB_PWRMGT_CNTL, DPM_ENABLED, 0x1);
3289 break;
3290 default:
3291 PP_ASSERT_WITH_CODE(0,
3292 "Failed to setup PCC HW register! Wrong GPIO assigned for VDDC_PCC_GPIO_PINID!",
3293 );
3294 break;
3295 }
3296 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCNB_PWRMGT_CNTL, temp_reg);
3297 }
3298
Rex Zhuf0911de2016-03-23 14:50:22 +08003299 if (table_info->cac_dtp_table->usDefaultTargetOperatingTemp != 0 &&
3300 hwmgr->thermal_controller.advanceFanControlParameters.ucFanControlMode) {
3301 hwmgr->thermal_controller.advanceFanControlParameters.usFanPWMMinLimit =
3302 (uint16_t)hwmgr->thermal_controller.advanceFanControlParameters.ucMinimumPWMLimit;
3303
3304 hwmgr->thermal_controller.advanceFanControlParameters.usFanPWMMaxLimit =
3305 (uint16_t)hwmgr->thermal_controller.advanceFanControlParameters.usDefaultMaxFanPWM;
3306
3307 hwmgr->thermal_controller.advanceFanControlParameters.usFanPWMStep = 1;
3308
3309 hwmgr->thermal_controller.advanceFanControlParameters.usFanRPMMaxLimit = 100;
3310
3311 hwmgr->thermal_controller.advanceFanControlParameters.usFanRPMMinLimit =
3312 (uint16_t)hwmgr->thermal_controller.advanceFanControlParameters.ucMinimumPWMLimit;
3313
3314 hwmgr->thermal_controller.advanceFanControlParameters.usFanRPMStep = 1;
3315
3316 table_info->cac_dtp_table->usDefaultTargetOperatingTemp = (table_info->cac_dtp_table->usDefaultTargetOperatingTemp >= 50) ?
3317 (table_info->cac_dtp_table->usDefaultTargetOperatingTemp -50) : 0;
3318
3319 table_info->cac_dtp_table->usOperatingTempMaxLimit = table_info->cac_dtp_table->usDefaultTargetOperatingTemp;
3320 table_info->cac_dtp_table->usOperatingTempStep = 1;
3321 table_info->cac_dtp_table->usOperatingTempHyst = 1;
3322
3323 hwmgr->thermal_controller.advanceFanControlParameters.usMaxFanPWM =
3324 hwmgr->thermal_controller.advanceFanControlParameters.usDefaultMaxFanPWM;
3325
3326 hwmgr->thermal_controller.advanceFanControlParameters.usMaxFanRPM =
3327 hwmgr->thermal_controller.advanceFanControlParameters.usDefaultMaxFanRPM;
3328
3329 hwmgr->dyn_state.cac_dtp_table->usOperatingTempMinLimit =
3330 table_info->cac_dtp_table->usOperatingTempMinLimit;
3331
3332 hwmgr->dyn_state.cac_dtp_table->usOperatingTempMaxLimit =
3333 table_info->cac_dtp_table->usOperatingTempMaxLimit;
3334
3335 hwmgr->dyn_state.cac_dtp_table->usDefaultTargetOperatingTemp =
3336 table_info->cac_dtp_table->usDefaultTargetOperatingTemp;
3337
3338 hwmgr->dyn_state.cac_dtp_table->usOperatingTempStep =
3339 table_info->cac_dtp_table->usOperatingTempStep;
3340
3341 hwmgr->dyn_state.cac_dtp_table->usTargetOperatingTemp =
3342 table_info->cac_dtp_table->usTargetOperatingTemp;
3343 }
3344
Rex Zhua23eefa2015-11-19 18:23:32 +08003345 sys_info.size = sizeof(struct cgs_system_info);
3346 sys_info.info_id = CGS_SYSTEM_INFO_PCIE_GEN_INFO;
3347 result = cgs_query_system_info(hwmgr->device, &sys_info);
3348 if (result)
Huang Ruid1371f82016-06-22 13:49:48 +08003349 data->pcie_gen_cap = AMDGPU_DEFAULT_PCIE_GEN_MASK;
Rex Zhua23eefa2015-11-19 18:23:32 +08003350 else
3351 data->pcie_gen_cap = (uint32_t)sys_info.value;
3352 if (data->pcie_gen_cap & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
3353 data->pcie_spc_cap = 20;
3354 sys_info.size = sizeof(struct cgs_system_info);
3355 sys_info.info_id = CGS_SYSTEM_INFO_PCIE_MLW;
3356 result = cgs_query_system_info(hwmgr->device, &sys_info);
3357 if (result)
Huang Ruid1371f82016-06-22 13:49:48 +08003358 data->pcie_lane_cap = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Rex Zhua23eefa2015-11-19 18:23:32 +08003359 else
3360 data->pcie_lane_cap = (uint32_t)sys_info.value;
Rex Zhuf0911de2016-03-23 14:50:22 +08003361
3362 hwmgr->platform_descriptor.vbiosInterruptId = 0x20000400; /* IRQ_SOURCE1_SW_INT */
3363/* The true clock step depends on the frequency, typically 4.5 or 9 MHz. Here we use 5. */
3364 hwmgr->platform_descriptor.clockStep.engineClock = 500;
3365 hwmgr->platform_descriptor.clockStep.memoryClock = 500;
Rex Zhua23eefa2015-11-19 18:23:32 +08003366 } else {
3367 /* Ignore return value in here, we are cleaning up a mess. */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003368 polaris10_hwmgr_backend_fini(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003369 }
3370
3371 return 0;
3372}
3373
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003374static int polaris10_force_dpm_highest(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003375{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003376 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08003377 uint32_t level, tmp;
3378
3379 if (!data->pcie_dpm_key_disabled) {
3380 if (data->dpm_level_enable_mask.pcie_dpm_enable_mask) {
3381 level = 0;
3382 tmp = data->dpm_level_enable_mask.pcie_dpm_enable_mask;
3383 while (tmp >>= 1)
3384 level++;
3385
3386 if (level)
3387 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3388 PPSMC_MSG_PCIeDPM_ForceLevel, level);
3389 }
3390 }
3391
3392 if (!data->sclk_dpm_key_disabled) {
3393 if (data->dpm_level_enable_mask.sclk_dpm_enable_mask) {
3394 level = 0;
3395 tmp = data->dpm_level_enable_mask.sclk_dpm_enable_mask;
3396 while (tmp >>= 1)
3397 level++;
3398
3399 if (level)
3400 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3401 PPSMC_MSG_SCLKDPM_SetEnabledMask,
3402 (1 << level));
3403 }
3404 }
3405
3406 if (!data->mclk_dpm_key_disabled) {
3407 if (data->dpm_level_enable_mask.mclk_dpm_enable_mask) {
3408 level = 0;
3409 tmp = data->dpm_level_enable_mask.mclk_dpm_enable_mask;
3410 while (tmp >>= 1)
3411 level++;
3412
3413 if (level)
3414 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3415 PPSMC_MSG_MCLKDPM_SetEnabledMask,
3416 (1 << level));
3417 }
3418 }
3419
3420 return 0;
3421}
3422
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003423static int polaris10_upload_dpm_level_enable_mask(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003424{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003425 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08003426
3427 phm_apply_dal_min_voltage_request(hwmgr);
3428
3429 if (!data->sclk_dpm_key_disabled) {
3430 if (data->dpm_level_enable_mask.sclk_dpm_enable_mask)
3431 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3432 PPSMC_MSG_SCLKDPM_SetEnabledMask,
3433 data->dpm_level_enable_mask.sclk_dpm_enable_mask);
3434 }
3435
3436 if (!data->mclk_dpm_key_disabled) {
3437 if (data->dpm_level_enable_mask.mclk_dpm_enable_mask)
3438 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3439 PPSMC_MSG_MCLKDPM_SetEnabledMask,
3440 data->dpm_level_enable_mask.mclk_dpm_enable_mask);
3441 }
3442
3443 return 0;
3444}
3445
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003446static int polaris10_unforce_dpm_levels(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003447{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003448 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08003449
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003450 if (!polaris10_is_dpm_running(hwmgr))
Rex Zhua23eefa2015-11-19 18:23:32 +08003451 return -EINVAL;
3452
3453 if (!data->pcie_dpm_key_disabled) {
3454 smum_send_msg_to_smc(hwmgr->smumgr,
3455 PPSMC_MSG_PCIeDPM_UnForceLevel);
3456 }
3457
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003458 return polaris10_upload_dpm_level_enable_mask(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003459}
3460
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003461static int polaris10_force_dpm_lowest(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003462{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003463 struct polaris10_hwmgr *data =
3464 (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08003465 uint32_t level;
3466
3467 if (!data->sclk_dpm_key_disabled)
3468 if (data->dpm_level_enable_mask.sclk_dpm_enable_mask) {
3469 level = phm_get_lowest_enabled_level(hwmgr,
3470 data->dpm_level_enable_mask.sclk_dpm_enable_mask);
3471 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3472 PPSMC_MSG_SCLKDPM_SetEnabledMask,
3473 (1 << level));
3474
3475 }
Rex Zhu2043f432016-03-15 19:30:00 +08003476
Rex Zhua23eefa2015-11-19 18:23:32 +08003477 if (!data->mclk_dpm_key_disabled) {
3478 if (data->dpm_level_enable_mask.mclk_dpm_enable_mask) {
3479 level = phm_get_lowest_enabled_level(hwmgr,
3480 data->dpm_level_enable_mask.mclk_dpm_enable_mask);
3481 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3482 PPSMC_MSG_MCLKDPM_SetEnabledMask,
3483 (1 << level));
3484 }
3485 }
Rex Zhu2043f432016-03-15 19:30:00 +08003486
Rex Zhua23eefa2015-11-19 18:23:32 +08003487 if (!data->pcie_dpm_key_disabled) {
3488 if (data->dpm_level_enable_mask.pcie_dpm_enable_mask) {
3489 level = phm_get_lowest_enabled_level(hwmgr,
3490 data->dpm_level_enable_mask.pcie_dpm_enable_mask);
3491 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
3492 PPSMC_MSG_PCIeDPM_ForceLevel,
3493 (level));
3494 }
3495 }
3496
3497 return 0;
3498
3499}
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003500static int polaris10_force_dpm_level(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08003501 enum amd_dpm_forced_level level)
3502{
3503 int ret = 0;
3504
3505 switch (level) {
3506 case AMD_DPM_FORCED_LEVEL_HIGH:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003507 ret = polaris10_force_dpm_highest(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003508 if (ret)
3509 return ret;
3510 break;
3511 case AMD_DPM_FORCED_LEVEL_LOW:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003512 ret = polaris10_force_dpm_lowest(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003513 if (ret)
3514 return ret;
3515 break;
3516 case AMD_DPM_FORCED_LEVEL_AUTO:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003517 ret = polaris10_unforce_dpm_levels(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08003518 if (ret)
3519 return ret;
3520 break;
3521 default:
3522 break;
3523 }
3524
3525 hwmgr->dpm_level = level;
3526
3527 return ret;
3528}
3529
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003530static int polaris10_get_power_state_size(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08003531{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003532 return sizeof(struct polaris10_power_state);
Rex Zhua23eefa2015-11-19 18:23:32 +08003533}
3534
3535
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003536static int polaris10_apply_state_adjust_rules(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08003537 struct pp_power_state *request_ps,
3538 const struct pp_power_state *current_ps)
3539{
3540
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003541 struct polaris10_power_state *polaris10_ps =
3542 cast_phw_polaris10_power_state(&request_ps->hardware);
Rex Zhua23eefa2015-11-19 18:23:32 +08003543 uint32_t sclk;
3544 uint32_t mclk;
3545 struct PP_Clocks minimum_clocks = {0};
3546 bool disable_mclk_switching;
3547 bool disable_mclk_switching_for_frame_lock;
3548 struct cgs_display_info info = {0};
3549 const struct phm_clock_and_voltage_limits *max_limits;
3550 uint32_t i;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003551 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08003552 struct phm_ppt_v1_information *table_info =
3553 (struct phm_ppt_v1_information *)(hwmgr->pptable);
3554 int32_t count;
3555 int32_t stable_pstate_sclk = 0, stable_pstate_mclk = 0;
3556
3557 data->battery_state = (PP_StateUILabel_Battery ==
3558 request_ps->classification.ui_label);
3559
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003560 PP_ASSERT_WITH_CODE(polaris10_ps->performance_level_count == 2,
Rex Zhua23eefa2015-11-19 18:23:32 +08003561 "VI should always have 2 performance levels",
3562 );
3563
3564 max_limits = (PP_PowerSource_AC == hwmgr->power_source) ?
3565 &(hwmgr->dyn_state.max_clock_voltage_on_ac) :
3566 &(hwmgr->dyn_state.max_clock_voltage_on_dc);
3567
3568 /* Cap clock DPM tables at DC MAX if it is in DC. */
3569 if (PP_PowerSource_DC == hwmgr->power_source) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003570 for (i = 0; i < polaris10_ps->performance_level_count; i++) {
3571 if (polaris10_ps->performance_levels[i].memory_clock > max_limits->mclk)
3572 polaris10_ps->performance_levels[i].memory_clock = max_limits->mclk;
3573 if (polaris10_ps->performance_levels[i].engine_clock > max_limits->sclk)
3574 polaris10_ps->performance_levels[i].engine_clock = max_limits->sclk;
Rex Zhua23eefa2015-11-19 18:23:32 +08003575 }
3576 }
3577
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003578 polaris10_ps->vce_clks.evclk = hwmgr->vce_arbiter.evclk;
3579 polaris10_ps->vce_clks.ecclk = hwmgr->vce_arbiter.ecclk;
Rex Zhua23eefa2015-11-19 18:23:32 +08003580
3581 cgs_get_active_displays_info(hwmgr->device, &info);
3582
3583 /*TO DO result = PHM_CheckVBlankTime(hwmgr, &vblankTooShort);*/
3584
3585 /* TO DO GetMinClockSettings(hwmgr->pPECI, &minimum_clocks); */
3586
3587 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3588 PHM_PlatformCaps_StablePState)) {
3589 max_limits = &(hwmgr->dyn_state.max_clock_voltage_on_ac);
3590 stable_pstate_sclk = (max_limits->sclk * 75) / 100;
3591
3592 for (count = table_info->vdd_dep_on_sclk->count - 1;
3593 count >= 0; count--) {
3594 if (stable_pstate_sclk >=
3595 table_info->vdd_dep_on_sclk->entries[count].clk) {
3596 stable_pstate_sclk =
3597 table_info->vdd_dep_on_sclk->entries[count].clk;
3598 break;
3599 }
3600 }
3601
3602 if (count < 0)
3603 stable_pstate_sclk = table_info->vdd_dep_on_sclk->entries[0].clk;
3604
3605 stable_pstate_mclk = max_limits->mclk;
3606
3607 minimum_clocks.engineClock = stable_pstate_sclk;
3608 minimum_clocks.memoryClock = stable_pstate_mclk;
3609 }
3610
3611 if (minimum_clocks.engineClock < hwmgr->gfx_arbiter.sclk)
3612 minimum_clocks.engineClock = hwmgr->gfx_arbiter.sclk;
3613
3614 if (minimum_clocks.memoryClock < hwmgr->gfx_arbiter.mclk)
3615 minimum_clocks.memoryClock = hwmgr->gfx_arbiter.mclk;
3616
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003617 polaris10_ps->sclk_threshold = hwmgr->gfx_arbiter.sclk_threshold;
Rex Zhua23eefa2015-11-19 18:23:32 +08003618
3619 if (0 != hwmgr->gfx_arbiter.sclk_over_drive) {
3620 PP_ASSERT_WITH_CODE((hwmgr->gfx_arbiter.sclk_over_drive <=
3621 hwmgr->platform_descriptor.overdriveLimit.engineClock),
3622 "Overdrive sclk exceeds limit",
3623 hwmgr->gfx_arbiter.sclk_over_drive =
3624 hwmgr->platform_descriptor.overdriveLimit.engineClock);
3625
3626 if (hwmgr->gfx_arbiter.sclk_over_drive >= hwmgr->gfx_arbiter.sclk)
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003627 polaris10_ps->performance_levels[1].engine_clock =
Rex Zhua23eefa2015-11-19 18:23:32 +08003628 hwmgr->gfx_arbiter.sclk_over_drive;
3629 }
3630
3631 if (0 != hwmgr->gfx_arbiter.mclk_over_drive) {
3632 PP_ASSERT_WITH_CODE((hwmgr->gfx_arbiter.mclk_over_drive <=
3633 hwmgr->platform_descriptor.overdriveLimit.memoryClock),
3634 "Overdrive mclk exceeds limit",
3635 hwmgr->gfx_arbiter.mclk_over_drive =
3636 hwmgr->platform_descriptor.overdriveLimit.memoryClock);
3637
3638 if (hwmgr->gfx_arbiter.mclk_over_drive >= hwmgr->gfx_arbiter.mclk)
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003639 polaris10_ps->performance_levels[1].memory_clock =
Rex Zhua23eefa2015-11-19 18:23:32 +08003640 hwmgr->gfx_arbiter.mclk_over_drive;
3641 }
3642
3643 disable_mclk_switching_for_frame_lock = phm_cap_enabled(
3644 hwmgr->platform_descriptor.platformCaps,
3645 PHM_PlatformCaps_DisableMclkSwitchingForFrameLock);
3646
Rex Zhu36e6b912016-06-08 12:56:20 +08003647
Rex Zhua23eefa2015-11-19 18:23:32 +08003648 disable_mclk_switching = (1 < info.display_count) ||
3649 disable_mclk_switching_for_frame_lock;
3650
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003651 sclk = polaris10_ps->performance_levels[0].engine_clock;
3652 mclk = polaris10_ps->performance_levels[0].memory_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08003653
3654 if (disable_mclk_switching)
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003655 mclk = polaris10_ps->performance_levels
3656 [polaris10_ps->performance_level_count - 1].memory_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08003657
3658 if (sclk < minimum_clocks.engineClock)
3659 sclk = (minimum_clocks.engineClock > max_limits->sclk) ?
3660 max_limits->sclk : minimum_clocks.engineClock;
3661
3662 if (mclk < minimum_clocks.memoryClock)
3663 mclk = (minimum_clocks.memoryClock > max_limits->mclk) ?
3664 max_limits->mclk : minimum_clocks.memoryClock;
3665
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003666 polaris10_ps->performance_levels[0].engine_clock = sclk;
3667 polaris10_ps->performance_levels[0].memory_clock = mclk;
Rex Zhua23eefa2015-11-19 18:23:32 +08003668
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003669 polaris10_ps->performance_levels[1].engine_clock =
3670 (polaris10_ps->performance_levels[1].engine_clock >=
3671 polaris10_ps->performance_levels[0].engine_clock) ?
3672 polaris10_ps->performance_levels[1].engine_clock :
3673 polaris10_ps->performance_levels[0].engine_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08003674
3675 if (disable_mclk_switching) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003676 if (mclk < polaris10_ps->performance_levels[1].memory_clock)
3677 mclk = polaris10_ps->performance_levels[1].memory_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08003678
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003679 polaris10_ps->performance_levels[0].memory_clock = mclk;
3680 polaris10_ps->performance_levels[1].memory_clock = mclk;
Rex Zhua23eefa2015-11-19 18:23:32 +08003681 } else {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003682 if (polaris10_ps->performance_levels[1].memory_clock <
3683 polaris10_ps->performance_levels[0].memory_clock)
3684 polaris10_ps->performance_levels[1].memory_clock =
3685 polaris10_ps->performance_levels[0].memory_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08003686 }
3687
3688 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
3689 PHM_PlatformCaps_StablePState)) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003690 for (i = 0; i < polaris10_ps->performance_level_count; i++) {
3691 polaris10_ps->performance_levels[i].engine_clock = stable_pstate_sclk;
3692 polaris10_ps->performance_levels[i].memory_clock = stable_pstate_mclk;
3693 polaris10_ps->performance_levels[i].pcie_gen = data->pcie_gen_performance.max;
3694 polaris10_ps->performance_levels[i].pcie_lane = data->pcie_gen_performance.max;
Rex Zhua23eefa2015-11-19 18:23:32 +08003695 }
3696 }
3697 return 0;
3698}
3699
3700
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003701static int polaris10_dpm_get_mclk(struct pp_hwmgr *hwmgr, bool low)
Rex Zhua23eefa2015-11-19 18:23:32 +08003702{
3703 struct pp_power_state *ps;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003704 struct polaris10_power_state *polaris10_ps;
Rex Zhua23eefa2015-11-19 18:23:32 +08003705
3706 if (hwmgr == NULL)
3707 return -EINVAL;
3708
3709 ps = hwmgr->request_ps;
3710
3711 if (ps == NULL)
3712 return -EINVAL;
3713
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003714 polaris10_ps = cast_phw_polaris10_power_state(&ps->hardware);
Rex Zhua23eefa2015-11-19 18:23:32 +08003715
3716 if (low)
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003717 return polaris10_ps->performance_levels[0].memory_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08003718 else
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003719 return polaris10_ps->performance_levels
3720 [polaris10_ps->performance_level_count-1].memory_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08003721}
3722
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003723static int polaris10_dpm_get_sclk(struct pp_hwmgr *hwmgr, bool low)
Rex Zhua23eefa2015-11-19 18:23:32 +08003724{
3725 struct pp_power_state *ps;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003726 struct polaris10_power_state *polaris10_ps;
Rex Zhua23eefa2015-11-19 18:23:32 +08003727
3728 if (hwmgr == NULL)
3729 return -EINVAL;
3730
3731 ps = hwmgr->request_ps;
3732
3733 if (ps == NULL)
3734 return -EINVAL;
3735
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003736 polaris10_ps = cast_phw_polaris10_power_state(&ps->hardware);
Rex Zhua23eefa2015-11-19 18:23:32 +08003737
3738 if (low)
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003739 return polaris10_ps->performance_levels[0].engine_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08003740 else
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003741 return polaris10_ps->performance_levels
3742 [polaris10_ps->performance_level_count-1].engine_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08003743}
3744
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003745static int polaris10_dpm_patch_boot_state(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08003746 struct pp_hw_power_state *hw_ps)
3747{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003748 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
3749 struct polaris10_power_state *ps = (struct polaris10_power_state *)hw_ps;
Rex Zhua23eefa2015-11-19 18:23:32 +08003750 ATOM_FIRMWARE_INFO_V2_2 *fw_info;
3751 uint16_t size;
3752 uint8_t frev, crev;
3753 int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
3754
3755 /* First retrieve the Boot clocks and VDDC from the firmware info table.
3756 * We assume here that fw_info is unchanged if this call fails.
3757 */
3758 fw_info = (ATOM_FIRMWARE_INFO_V2_2 *)cgs_atom_get_data_table(
3759 hwmgr->device, index,
3760 &size, &frev, &crev);
3761 if (!fw_info)
3762 /* During a test, there is no firmware info table. */
3763 return 0;
3764
3765 /* Patch the state. */
3766 data->vbios_boot_state.sclk_bootup_value =
3767 le32_to_cpu(fw_info->ulDefaultEngineClock);
3768 data->vbios_boot_state.mclk_bootup_value =
3769 le32_to_cpu(fw_info->ulDefaultMemoryClock);
3770 data->vbios_boot_state.mvdd_bootup_value =
3771 le16_to_cpu(fw_info->usBootUpMVDDCVoltage);
3772 data->vbios_boot_state.vddc_bootup_value =
3773 le16_to_cpu(fw_info->usBootUpVDDCVoltage);
3774 data->vbios_boot_state.vddci_bootup_value =
3775 le16_to_cpu(fw_info->usBootUpVDDCIVoltage);
3776 data->vbios_boot_state.pcie_gen_bootup_value =
3777 phm_get_current_pcie_speed(hwmgr);
3778
3779 data->vbios_boot_state.pcie_lane_bootup_value =
3780 (uint16_t)phm_get_current_pcie_lane_number(hwmgr);
3781
3782 /* set boot power state */
3783 ps->performance_levels[0].memory_clock = data->vbios_boot_state.mclk_bootup_value;
3784 ps->performance_levels[0].engine_clock = data->vbios_boot_state.sclk_bootup_value;
3785 ps->performance_levels[0].pcie_gen = data->vbios_boot_state.pcie_gen_bootup_value;
3786 ps->performance_levels[0].pcie_lane = data->vbios_boot_state.pcie_lane_bootup_value;
3787
3788 return 0;
3789}
3790
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003791static int polaris10_get_pp_table_entry_callback_func(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08003792 void *state, struct pp_power_state *power_state,
3793 void *pp_table, uint32_t classification_flag)
3794{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003795 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
3796 struct polaris10_power_state *polaris10_power_state =
3797 (struct polaris10_power_state *)(&(power_state->hardware));
3798 struct polaris10_performance_level *performance_level;
Rex Zhua23eefa2015-11-19 18:23:32 +08003799 ATOM_Tonga_State *state_entry = (ATOM_Tonga_State *)state;
3800 ATOM_Tonga_POWERPLAYTABLE *powerplay_table =
3801 (ATOM_Tonga_POWERPLAYTABLE *)pp_table;
Rex Zhu3ff21122016-06-08 19:04:35 +08003802 PPTable_Generic_SubTable_Header *sclk_dep_table =
3803 (PPTable_Generic_SubTable_Header *)
Rex Zhua23eefa2015-11-19 18:23:32 +08003804 (((unsigned long)powerplay_table) +
3805 le16_to_cpu(powerplay_table->usSclkDependencyTableOffset));
Rex Zhu3ff21122016-06-08 19:04:35 +08003806
Rex Zhua23eefa2015-11-19 18:23:32 +08003807 ATOM_Tonga_MCLK_Dependency_Table *mclk_dep_table =
3808 (ATOM_Tonga_MCLK_Dependency_Table *)
3809 (((unsigned long)powerplay_table) +
3810 le16_to_cpu(powerplay_table->usMclkDependencyTableOffset));
3811
3812 /* The following fields are not initialized here: id orderedList allStatesList */
3813 power_state->classification.ui_label =
3814 (le16_to_cpu(state_entry->usClassification) &
3815 ATOM_PPLIB_CLASSIFICATION_UI_MASK) >>
3816 ATOM_PPLIB_CLASSIFICATION_UI_SHIFT;
3817 power_state->classification.flags = classification_flag;
3818 /* NOTE: There is a classification2 flag in BIOS that is not being used right now */
3819
3820 power_state->classification.temporary_state = false;
3821 power_state->classification.to_be_deleted = false;
3822
3823 power_state->validation.disallowOnDC =
3824 (0 != (le32_to_cpu(state_entry->ulCapsAndSettings) &
3825 ATOM_Tonga_DISALLOW_ON_DC));
3826
3827 power_state->pcie.lanes = 0;
3828
3829 power_state->display.disableFrameModulation = false;
3830 power_state->display.limitRefreshrate = false;
3831 power_state->display.enableVariBright =
3832 (0 != (le32_to_cpu(state_entry->ulCapsAndSettings) &
3833 ATOM_Tonga_ENABLE_VARIBRIGHT));
3834
3835 power_state->validation.supportedPowerLevels = 0;
3836 power_state->uvd_clocks.VCLK = 0;
3837 power_state->uvd_clocks.DCLK = 0;
3838 power_state->temperatures.min = 0;
3839 power_state->temperatures.max = 0;
3840
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003841 performance_level = &(polaris10_power_state->performance_levels
3842 [polaris10_power_state->performance_level_count++]);
Rex Zhua23eefa2015-11-19 18:23:32 +08003843
3844 PP_ASSERT_WITH_CODE(
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003845 (polaris10_power_state->performance_level_count < SMU74_MAX_LEVELS_GRAPHICS),
Rex Zhua23eefa2015-11-19 18:23:32 +08003846 "Performance levels exceeds SMC limit!",
3847 return -1);
3848
3849 PP_ASSERT_WITH_CODE(
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003850 (polaris10_power_state->performance_level_count <=
Rex Zhua23eefa2015-11-19 18:23:32 +08003851 hwmgr->platform_descriptor.hardwareActivityPerformanceLevels),
3852 "Performance levels exceeds Driver limit!",
3853 return -1);
3854
3855 /* Performance levels are arranged from low to high. */
3856 performance_level->memory_clock = mclk_dep_table->entries
3857 [state_entry->ucMemoryClockIndexLow].ulMclk;
Rex Zhu3ff21122016-06-08 19:04:35 +08003858 if (sclk_dep_table->ucRevId == 0)
3859 performance_level->engine_clock = ((ATOM_Tonga_SCLK_Dependency_Table *)sclk_dep_table)->entries
3860 [state_entry->ucEngineClockIndexLow].ulSclk;
3861 else if (sclk_dep_table->ucRevId == 1)
3862 performance_level->engine_clock = ((ATOM_Polaris_SCLK_Dependency_Table *)sclk_dep_table)->entries
Rex Zhua23eefa2015-11-19 18:23:32 +08003863 [state_entry->ucEngineClockIndexLow].ulSclk;
3864 performance_level->pcie_gen = get_pcie_gen_support(data->pcie_gen_cap,
3865 state_entry->ucPCIEGenLow);
3866 performance_level->pcie_lane = get_pcie_lane_support(data->pcie_lane_cap,
3867 state_entry->ucPCIELaneHigh);
3868
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003869 performance_level = &(polaris10_power_state->performance_levels
3870 [polaris10_power_state->performance_level_count++]);
Rex Zhua23eefa2015-11-19 18:23:32 +08003871 performance_level->memory_clock = mclk_dep_table->entries
3872 [state_entry->ucMemoryClockIndexHigh].ulMclk;
Rex Zhu3ff21122016-06-08 19:04:35 +08003873
3874 if (sclk_dep_table->ucRevId == 0)
3875 performance_level->engine_clock = ((ATOM_Tonga_SCLK_Dependency_Table *)sclk_dep_table)->entries
Rex Zhua23eefa2015-11-19 18:23:32 +08003876 [state_entry->ucEngineClockIndexHigh].ulSclk;
Rex Zhu3ff21122016-06-08 19:04:35 +08003877 else if (sclk_dep_table->ucRevId == 1)
3878 performance_level->engine_clock = ((ATOM_Polaris_SCLK_Dependency_Table *)sclk_dep_table)->entries
3879 [state_entry->ucEngineClockIndexHigh].ulSclk;
3880
Rex Zhua23eefa2015-11-19 18:23:32 +08003881 performance_level->pcie_gen = get_pcie_gen_support(data->pcie_gen_cap,
3882 state_entry->ucPCIEGenHigh);
3883 performance_level->pcie_lane = get_pcie_lane_support(data->pcie_lane_cap,
3884 state_entry->ucPCIELaneHigh);
3885
3886 return 0;
3887}
3888
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003889static int polaris10_get_pp_table_entry(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08003890 unsigned long entry_index, struct pp_power_state *state)
3891{
3892 int result;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003893 struct polaris10_power_state *ps;
3894 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08003895 struct phm_ppt_v1_information *table_info =
3896 (struct phm_ppt_v1_information *)(hwmgr->pptable);
3897 struct phm_ppt_v1_clock_voltage_dependency_table *dep_mclk_table =
3898 table_info->vdd_dep_on_mclk;
3899
3900 state->hardware.magic = PHM_VIslands_Magic;
3901
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003902 ps = (struct polaris10_power_state *)(&state->hardware);
Rex Zhua23eefa2015-11-19 18:23:32 +08003903
3904 result = tonga_get_powerplay_table_entry(hwmgr, entry_index, state,
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003905 polaris10_get_pp_table_entry_callback_func);
Rex Zhua23eefa2015-11-19 18:23:32 +08003906
3907 /* This is the earliest time we have all the dependency table and the VBIOS boot state
3908 * as PP_Tables_GetPowerPlayTableEntry retrieves the VBIOS boot state
3909 * if there is only one VDDCI/MCLK level, check if it's the same as VBIOS boot state
3910 */
3911 if (dep_mclk_table != NULL && dep_mclk_table->count == 1) {
3912 if (dep_mclk_table->entries[0].clk !=
3913 data->vbios_boot_state.mclk_bootup_value)
3914 printk(KERN_ERR "Single MCLK entry VDDCI/MCLK dependency table "
3915 "does not match VBIOS boot MCLK level");
3916 if (dep_mclk_table->entries[0].vddci !=
3917 data->vbios_boot_state.vddci_bootup_value)
3918 printk(KERN_ERR "Single VDDCI entry VDDCI/MCLK dependency table "
3919 "does not match VBIOS boot VDDCI level");
3920 }
3921
3922 /* set DC compatible flag if this state supports DC */
3923 if (!state->validation.disallowOnDC)
3924 ps->dc_compatible = true;
3925
3926 if (state->classification.flags & PP_StateClassificationFlag_ACPI)
3927 data->acpi_pcie_gen = ps->performance_levels[0].pcie_gen;
3928
3929 ps->uvd_clks.vclk = state->uvd_clocks.VCLK;
3930 ps->uvd_clks.dclk = state->uvd_clocks.DCLK;
3931
3932 if (!result) {
3933 uint32_t i;
3934
3935 switch (state->classification.ui_label) {
3936 case PP_StateUILabel_Performance:
3937 data->use_pcie_performance_levels = true;
Rex Zhua23eefa2015-11-19 18:23:32 +08003938 for (i = 0; i < ps->performance_level_count; i++) {
3939 if (data->pcie_gen_performance.max <
3940 ps->performance_levels[i].pcie_gen)
3941 data->pcie_gen_performance.max =
3942 ps->performance_levels[i].pcie_gen;
3943
3944 if (data->pcie_gen_performance.min >
3945 ps->performance_levels[i].pcie_gen)
3946 data->pcie_gen_performance.min =
3947 ps->performance_levels[i].pcie_gen;
3948
3949 if (data->pcie_lane_performance.max <
3950 ps->performance_levels[i].pcie_lane)
3951 data->pcie_lane_performance.max =
3952 ps->performance_levels[i].pcie_lane;
Rex Zhua23eefa2015-11-19 18:23:32 +08003953 if (data->pcie_lane_performance.min >
3954 ps->performance_levels[i].pcie_lane)
3955 data->pcie_lane_performance.min =
3956 ps->performance_levels[i].pcie_lane;
3957 }
3958 break;
3959 case PP_StateUILabel_Battery:
3960 data->use_pcie_power_saving_levels = true;
3961
3962 for (i = 0; i < ps->performance_level_count; i++) {
3963 if (data->pcie_gen_power_saving.max <
3964 ps->performance_levels[i].pcie_gen)
3965 data->pcie_gen_power_saving.max =
3966 ps->performance_levels[i].pcie_gen;
3967
3968 if (data->pcie_gen_power_saving.min >
3969 ps->performance_levels[i].pcie_gen)
3970 data->pcie_gen_power_saving.min =
3971 ps->performance_levels[i].pcie_gen;
3972
3973 if (data->pcie_lane_power_saving.max <
3974 ps->performance_levels[i].pcie_lane)
3975 data->pcie_lane_power_saving.max =
3976 ps->performance_levels[i].pcie_lane;
3977
3978 if (data->pcie_lane_power_saving.min >
3979 ps->performance_levels[i].pcie_lane)
3980 data->pcie_lane_power_saving.min =
3981 ps->performance_levels[i].pcie_lane;
3982 }
3983 break;
3984 default:
3985 break;
3986 }
3987 }
3988 return 0;
3989}
3990
3991static void
Flora Cui2cc0c0b2016-03-14 18:33:29 -04003992polaris10_print_current_perforce_level(struct pp_hwmgr *hwmgr, struct seq_file *m)
Rex Zhua23eefa2015-11-19 18:23:32 +08003993{
Rex Zhub2d96142016-03-01 17:01:30 +08003994 uint32_t sclk, mclk, activity_percent;
3995 uint32_t offset;
3996 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08003997
3998 smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_API_GetSclkFrequency);
3999
4000 sclk = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
4001
4002 smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_API_GetMclkFrequency);
4003
4004 mclk = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
4005 seq_printf(m, "\n [ mclk ]: %u MHz\n\n [ sclk ]: %u MHz\n",
4006 mclk / 100, sclk / 100);
Rex Zhub2d96142016-03-01 17:01:30 +08004007
4008 offset = data->soft_regs_start + offsetof(SMU74_SoftRegisters, AverageGraphicsActivity);
4009 activity_percent = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, offset);
4010 activity_percent += 0x80;
4011 activity_percent >>= 8;
4012
4013 seq_printf(m, "\n [GPU load]: %u%%\n\n", activity_percent > 100 ? 100 : activity_percent);
4014
4015 seq_printf(m, "uvd %sabled\n", data->uvd_power_gated ? "dis" : "en");
4016
4017 seq_printf(m, "vce %sabled\n", data->vce_power_gated ? "dis" : "en");
Rex Zhua23eefa2015-11-19 18:23:32 +08004018}
4019
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004020static int polaris10_find_dpm_states_clocks_in_dpm_table(struct pp_hwmgr *hwmgr, const void *input)
Rex Zhua23eefa2015-11-19 18:23:32 +08004021{
4022 const struct phm_set_power_state_input *states =
4023 (const struct phm_set_power_state_input *)input;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004024 const struct polaris10_power_state *polaris10_ps =
4025 cast_const_phw_polaris10_power_state(states->pnew_state);
4026 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
4027 struct polaris10_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
4028 uint32_t sclk = polaris10_ps->performance_levels
4029 [polaris10_ps->performance_level_count - 1].engine_clock;
4030 struct polaris10_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
4031 uint32_t mclk = polaris10_ps->performance_levels
4032 [polaris10_ps->performance_level_count - 1].memory_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08004033 struct PP_Clocks min_clocks = {0};
4034 uint32_t i;
4035 struct cgs_display_info info = {0};
4036
4037 data->need_update_smu7_dpm_table = 0;
4038
4039 for (i = 0; i < sclk_table->count; i++) {
4040 if (sclk == sclk_table->dpm_levels[i].value)
4041 break;
4042 }
4043
4044 if (i >= sclk_table->count)
4045 data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_SCLK;
4046 else {
4047 /* TODO: Check SCLK in DAL's minimum clocks
4048 * in case DeepSleep divider update is required.
4049 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004050 if (data->display_timing.min_clock_in_sr != min_clocks.engineClockInSR &&
4051 (min_clocks.engineClockInSR >= POLARIS10_MINIMUM_ENGINE_CLOCK ||
4052 data->display_timing.min_clock_in_sr >= POLARIS10_MINIMUM_ENGINE_CLOCK))
Rex Zhua23eefa2015-11-19 18:23:32 +08004053 data->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_SCLK;
4054 }
4055
4056 for (i = 0; i < mclk_table->count; i++) {
4057 if (mclk == mclk_table->dpm_levels[i].value)
4058 break;
4059 }
4060
4061 if (i >= mclk_table->count)
4062 data->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_MCLK;
4063
4064 cgs_get_active_displays_info(hwmgr->device, &info);
4065
4066 if (data->display_timing.num_existing_displays != info.display_count)
4067 data->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_MCLK;
4068
4069 return 0;
4070}
4071
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004072static uint16_t polaris10_get_maximum_link_speed(struct pp_hwmgr *hwmgr,
4073 const struct polaris10_power_state *polaris10_ps)
Rex Zhua23eefa2015-11-19 18:23:32 +08004074{
4075 uint32_t i;
4076 uint32_t sclk, max_sclk = 0;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004077 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
4078 struct polaris10_dpm_table *dpm_table = &data->dpm_table;
Rex Zhua23eefa2015-11-19 18:23:32 +08004079
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004080 for (i = 0; i < polaris10_ps->performance_level_count; i++) {
4081 sclk = polaris10_ps->performance_levels[i].engine_clock;
Rex Zhua23eefa2015-11-19 18:23:32 +08004082 if (max_sclk < sclk)
4083 max_sclk = sclk;
4084 }
4085
4086 for (i = 0; i < dpm_table->sclk_table.count; i++) {
4087 if (dpm_table->sclk_table.dpm_levels[i].value == max_sclk)
4088 return (uint16_t) ((i >= dpm_table->pcie_speed_table.count) ?
4089 dpm_table->pcie_speed_table.dpm_levels
4090 [dpm_table->pcie_speed_table.count - 1].value :
4091 dpm_table->pcie_speed_table.dpm_levels[i].value);
4092 }
4093
4094 return 0;
4095}
4096
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004097static int polaris10_request_link_speed_change_before_state_change(
Rex Zhua23eefa2015-11-19 18:23:32 +08004098 struct pp_hwmgr *hwmgr, const void *input)
4099{
4100 const struct phm_set_power_state_input *states =
4101 (const struct phm_set_power_state_input *)input;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004102 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
4103 const struct polaris10_power_state *polaris10_nps =
4104 cast_const_phw_polaris10_power_state(states->pnew_state);
4105 const struct polaris10_power_state *polaris10_cps =
4106 cast_const_phw_polaris10_power_state(states->pcurrent_state);
Rex Zhua23eefa2015-11-19 18:23:32 +08004107
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004108 uint16_t target_link_speed = polaris10_get_maximum_link_speed(hwmgr, polaris10_nps);
Rex Zhua23eefa2015-11-19 18:23:32 +08004109 uint16_t current_link_speed;
4110
4111 if (data->force_pcie_gen == PP_PCIEGenInvalid)
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004112 current_link_speed = polaris10_get_maximum_link_speed(hwmgr, polaris10_cps);
Rex Zhua23eefa2015-11-19 18:23:32 +08004113 else
4114 current_link_speed = data->force_pcie_gen;
4115
4116 data->force_pcie_gen = PP_PCIEGenInvalid;
4117 data->pspp_notify_required = false;
4118
4119 if (target_link_speed > current_link_speed) {
4120 switch (target_link_speed) {
4121 case PP_PCIEGen3:
4122 if (0 == acpi_pcie_perf_request(hwmgr->device, PCIE_PERF_REQ_GEN3, false))
4123 break;
4124 data->force_pcie_gen = PP_PCIEGen2;
4125 if (current_link_speed == PP_PCIEGen2)
4126 break;
4127 case PP_PCIEGen2:
4128 if (0 == acpi_pcie_perf_request(hwmgr->device, PCIE_PERF_REQ_GEN2, false))
4129 break;
4130 default:
4131 data->force_pcie_gen = phm_get_current_pcie_speed(hwmgr);
4132 break;
4133 }
4134 } else {
4135 if (target_link_speed < current_link_speed)
4136 data->pspp_notify_required = true;
4137 }
4138
4139 return 0;
4140}
4141
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004142static int polaris10_freeze_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004143{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004144 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004145
4146 if (0 == data->need_update_smu7_dpm_table)
4147 return 0;
4148
4149 if ((0 == data->sclk_dpm_key_disabled) &&
4150 (data->need_update_smu7_dpm_table &
4151 (DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_UPDATE_SCLK))) {
Edward O'Callaghan4ba27f92016-07-12 10:17:56 +10004152 PP_ASSERT_WITH_CODE(polaris10_is_dpm_running(hwmgr),
4153 "Trying to freeze SCLK DPM when DPM is disabled",
Rex Zhua23eefa2015-11-19 18:23:32 +08004154 );
4155 PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr->smumgr,
4156 PPSMC_MSG_SCLKDPM_FreezeLevel),
4157 "Failed to freeze SCLK DPM during FreezeSclkMclkDPM Function!",
4158 return -1);
4159 }
4160
4161 if ((0 == data->mclk_dpm_key_disabled) &&
4162 (data->need_update_smu7_dpm_table &
4163 DPMTABLE_OD_UPDATE_MCLK)) {
Edward O'Callaghan4ba27f92016-07-12 10:17:56 +10004164 PP_ASSERT_WITH_CODE(polaris10_is_dpm_running(hwmgr),
4165 "Trying to freeze MCLK DPM when DPM is disabled",
Rex Zhua23eefa2015-11-19 18:23:32 +08004166 );
4167 PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr->smumgr,
4168 PPSMC_MSG_MCLKDPM_FreezeLevel),
4169 "Failed to freeze MCLK DPM during FreezeSclkMclkDPM Function!",
4170 return -1);
4171 }
4172
4173 return 0;
4174}
4175
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004176static int polaris10_populate_and_upload_sclk_mclk_dpm_levels(
Rex Zhua23eefa2015-11-19 18:23:32 +08004177 struct pp_hwmgr *hwmgr, const void *input)
4178{
4179 int result = 0;
4180 const struct phm_set_power_state_input *states =
4181 (const struct phm_set_power_state_input *)input;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004182 const struct polaris10_power_state *polaris10_ps =
4183 cast_const_phw_polaris10_power_state(states->pnew_state);
4184 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
4185 uint32_t sclk = polaris10_ps->performance_levels
4186 [polaris10_ps->performance_level_count - 1].engine_clock;
4187 uint32_t mclk = polaris10_ps->performance_levels
4188 [polaris10_ps->performance_level_count - 1].memory_clock;
4189 struct polaris10_dpm_table *dpm_table = &data->dpm_table;
Rex Zhua23eefa2015-11-19 18:23:32 +08004190
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004191 struct polaris10_dpm_table *golden_dpm_table = &data->golden_dpm_table;
Rex Zhua23eefa2015-11-19 18:23:32 +08004192 uint32_t dpm_count, clock_percent;
4193 uint32_t i;
4194
4195 if (0 == data->need_update_smu7_dpm_table)
4196 return 0;
4197
4198 if (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_SCLK) {
4199 dpm_table->sclk_table.dpm_levels
4200 [dpm_table->sclk_table.count - 1].value = sclk;
4201
4202 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_OD6PlusinACSupport) ||
4203 phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_OD6PlusinDCSupport)) {
4204 /* Need to do calculation based on the golden DPM table
4205 * as the Heatmap GPU Clock axis is also based on the default values
4206 */
4207 PP_ASSERT_WITH_CODE(
4208 (golden_dpm_table->sclk_table.dpm_levels
4209 [golden_dpm_table->sclk_table.count - 1].value != 0),
4210 "Divide by 0!",
4211 return -1);
4212 dpm_count = dpm_table->sclk_table.count < 2 ? 0 : dpm_table->sclk_table.count - 2;
4213
4214 for (i = dpm_count; i > 1; i--) {
4215 if (sclk > golden_dpm_table->sclk_table.dpm_levels[golden_dpm_table->sclk_table.count-1].value) {
4216 clock_percent =
4217 ((sclk
4218 - golden_dpm_table->sclk_table.dpm_levels[golden_dpm_table->sclk_table.count-1].value
4219 ) * 100)
4220 / golden_dpm_table->sclk_table.dpm_levels[golden_dpm_table->sclk_table.count-1].value;
4221
4222 dpm_table->sclk_table.dpm_levels[i].value =
4223 golden_dpm_table->sclk_table.dpm_levels[i].value +
4224 (golden_dpm_table->sclk_table.dpm_levels[i].value *
4225 clock_percent)/100;
4226
4227 } else if (golden_dpm_table->sclk_table.dpm_levels[dpm_table->sclk_table.count-1].value > sclk) {
4228 clock_percent =
4229 ((golden_dpm_table->sclk_table.dpm_levels[golden_dpm_table->sclk_table.count - 1].value
4230 - sclk) * 100)
4231 / golden_dpm_table->sclk_table.dpm_levels[golden_dpm_table->sclk_table.count-1].value;
4232
4233 dpm_table->sclk_table.dpm_levels[i].value =
4234 golden_dpm_table->sclk_table.dpm_levels[i].value -
4235 (golden_dpm_table->sclk_table.dpm_levels[i].value *
4236 clock_percent) / 100;
4237 } else
4238 dpm_table->sclk_table.dpm_levels[i].value =
4239 golden_dpm_table->sclk_table.dpm_levels[i].value;
4240 }
4241 }
4242 }
4243
4244 if (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK) {
4245 dpm_table->mclk_table.dpm_levels
4246 [dpm_table->mclk_table.count - 1].value = mclk;
4247
4248 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_OD6PlusinACSupport) ||
4249 phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_OD6PlusinDCSupport)) {
4250
4251 PP_ASSERT_WITH_CODE(
4252 (golden_dpm_table->mclk_table.dpm_levels
4253 [golden_dpm_table->mclk_table.count-1].value != 0),
4254 "Divide by 0!",
4255 return -1);
4256 dpm_count = dpm_table->mclk_table.count < 2 ? 0 : dpm_table->mclk_table.count - 2;
4257 for (i = dpm_count; i > 1; i--) {
4258 if (golden_dpm_table->mclk_table.dpm_levels[golden_dpm_table->mclk_table.count-1].value < mclk) {
4259 clock_percent = ((mclk -
4260 golden_dpm_table->mclk_table.dpm_levels[golden_dpm_table->mclk_table.count-1].value) * 100)
4261 / golden_dpm_table->mclk_table.dpm_levels[golden_dpm_table->mclk_table.count-1].value;
4262
4263 dpm_table->mclk_table.dpm_levels[i].value =
4264 golden_dpm_table->mclk_table.dpm_levels[i].value +
4265 (golden_dpm_table->mclk_table.dpm_levels[i].value *
4266 clock_percent) / 100;
4267
4268 } else if (golden_dpm_table->mclk_table.dpm_levels[dpm_table->mclk_table.count-1].value > mclk) {
4269 clock_percent = (
4270 (golden_dpm_table->mclk_table.dpm_levels[golden_dpm_table->mclk_table.count-1].value - mclk)
4271 * 100)
4272 / golden_dpm_table->mclk_table.dpm_levels[golden_dpm_table->mclk_table.count-1].value;
4273
4274 dpm_table->mclk_table.dpm_levels[i].value =
4275 golden_dpm_table->mclk_table.dpm_levels[i].value -
4276 (golden_dpm_table->mclk_table.dpm_levels[i].value *
4277 clock_percent) / 100;
4278 } else
4279 dpm_table->mclk_table.dpm_levels[i].value =
4280 golden_dpm_table->mclk_table.dpm_levels[i].value;
4281 }
4282 }
4283 }
4284
4285 if (data->need_update_smu7_dpm_table &
4286 (DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_UPDATE_SCLK)) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004287 result = polaris10_populate_all_graphic_levels(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004288 PP_ASSERT_WITH_CODE((0 == result),
4289 "Failed to populate SCLK during PopulateNewDPMClocksStates Function!",
4290 return result);
4291 }
4292
4293 if (data->need_update_smu7_dpm_table &
4294 (DPMTABLE_OD_UPDATE_MCLK + DPMTABLE_UPDATE_MCLK)) {
4295 /*populate MCLK dpm table to SMU7 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004296 result = polaris10_populate_all_memory_levels(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004297 PP_ASSERT_WITH_CODE((0 == result),
4298 "Failed to populate MCLK during PopulateNewDPMClocksStates Function!",
4299 return result);
4300 }
4301
4302 return result;
4303}
4304
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004305static int polaris10_trim_single_dpm_states(struct pp_hwmgr *hwmgr,
4306 struct polaris10_single_dpm_table *dpm_table,
Rex Zhua23eefa2015-11-19 18:23:32 +08004307 uint32_t low_limit, uint32_t high_limit)
4308{
4309 uint32_t i;
Rex Zhua23eefa2015-11-19 18:23:32 +08004310
4311 for (i = 0; i < dpm_table->count; i++) {
4312 if ((dpm_table->dpm_levels[i].value < low_limit)
4313 || (dpm_table->dpm_levels[i].value > high_limit))
4314 dpm_table->dpm_levels[i].enabled = false;
Rex Zhua23eefa2015-11-19 18:23:32 +08004315 else
4316 dpm_table->dpm_levels[i].enabled = true;
4317 }
4318
4319 return 0;
4320}
4321
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004322static int polaris10_trim_dpm_states(struct pp_hwmgr *hwmgr,
4323 const struct polaris10_power_state *polaris10_ps)
Rex Zhua23eefa2015-11-19 18:23:32 +08004324{
4325 int result = 0;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004326 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004327 uint32_t high_limit_count;
4328
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004329 PP_ASSERT_WITH_CODE((polaris10_ps->performance_level_count >= 1),
Rex Zhua23eefa2015-11-19 18:23:32 +08004330 "power state did not have any performance level",
4331 return -1);
4332
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004333 high_limit_count = (1 == polaris10_ps->performance_level_count) ? 0 : 1;
Rex Zhua23eefa2015-11-19 18:23:32 +08004334
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004335 polaris10_trim_single_dpm_states(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08004336 &(data->dpm_table.sclk_table),
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004337 polaris10_ps->performance_levels[0].engine_clock,
4338 polaris10_ps->performance_levels[high_limit_count].engine_clock);
Rex Zhua23eefa2015-11-19 18:23:32 +08004339
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004340 polaris10_trim_single_dpm_states(hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08004341 &(data->dpm_table.mclk_table),
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004342 polaris10_ps->performance_levels[0].memory_clock,
4343 polaris10_ps->performance_levels[high_limit_count].memory_clock);
Rex Zhua23eefa2015-11-19 18:23:32 +08004344
4345 return result;
4346}
4347
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004348static int polaris10_generate_dpm_level_enable_mask(
Rex Zhua23eefa2015-11-19 18:23:32 +08004349 struct pp_hwmgr *hwmgr, const void *input)
4350{
4351 int result;
4352 const struct phm_set_power_state_input *states =
4353 (const struct phm_set_power_state_input *)input;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004354 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
4355 const struct polaris10_power_state *polaris10_ps =
4356 cast_const_phw_polaris10_power_state(states->pnew_state);
Rex Zhua23eefa2015-11-19 18:23:32 +08004357
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004358 result = polaris10_trim_dpm_states(hwmgr, polaris10_ps);
Rex Zhua23eefa2015-11-19 18:23:32 +08004359 if (result)
4360 return result;
4361
4362 data->dpm_level_enable_mask.sclk_dpm_enable_mask =
4363 phm_get_dpm_level_enable_mask_value(&data->dpm_table.sclk_table);
4364 data->dpm_level_enable_mask.mclk_dpm_enable_mask =
4365 phm_get_dpm_level_enable_mask_value(&data->dpm_table.mclk_table);
4366 data->dpm_level_enable_mask.pcie_dpm_enable_mask =
4367 phm_get_dpm_level_enable_mask_value(&data->dpm_table.pcie_speed_table);
4368
4369 return 0;
4370}
4371
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004372int polaris10_enable_disable_uvd_dpm(struct pp_hwmgr *hwmgr, bool enable)
Rex Zhua23eefa2015-11-19 18:23:32 +08004373{
4374 return smum_send_msg_to_smc(hwmgr->smumgr, enable ?
Eric Huang92c6d642016-02-05 14:47:06 -05004375 PPSMC_MSG_UVDDPM_Enable :
4376 PPSMC_MSG_UVDDPM_Disable);
4377}
4378
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004379int polaris10_enable_disable_vce_dpm(struct pp_hwmgr *hwmgr, bool enable)
Eric Huang92c6d642016-02-05 14:47:06 -05004380{
4381 return smum_send_msg_to_smc(hwmgr->smumgr, enable?
Rex Zhua23eefa2015-11-19 18:23:32 +08004382 PPSMC_MSG_VCEDPM_Enable :
4383 PPSMC_MSG_VCEDPM_Disable);
4384}
4385
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004386int polaris10_enable_disable_samu_dpm(struct pp_hwmgr *hwmgr, bool enable)
Eric Huang92c6d642016-02-05 14:47:06 -05004387{
4388 return smum_send_msg_to_smc(hwmgr->smumgr, enable?
4389 PPSMC_MSG_SAMUDPM_Enable :
4390 PPSMC_MSG_SAMUDPM_Disable);
4391}
4392
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004393int polaris10_update_uvd_dpm(struct pp_hwmgr *hwmgr, bool bgate)
Eric Huang92c6d642016-02-05 14:47:06 -05004394{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004395 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Eric Huang92c6d642016-02-05 14:47:06 -05004396 uint32_t mm_boot_level_offset, mm_boot_level_value;
4397 struct phm_ppt_v1_information *table_info =
4398 (struct phm_ppt_v1_information *)(hwmgr->pptable);
4399
4400 if (!bgate) {
4401 data->smc_state_table.UvdBootLevel = 0;
4402 if (table_info->mm_dep_table->count > 0)
4403 data->smc_state_table.UvdBootLevel =
4404 (uint8_t) (table_info->mm_dep_table->count - 1);
4405 mm_boot_level_offset = data->dpm_table_start +
4406 offsetof(SMU74_Discrete_DpmTable, UvdBootLevel);
4407 mm_boot_level_offset /= 4;
4408 mm_boot_level_offset *= 4;
4409 mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
4410 CGS_IND_REG__SMC, mm_boot_level_offset);
4411 mm_boot_level_value &= 0x00FFFFFF;
4412 mm_boot_level_value |= data->smc_state_table.UvdBootLevel << 24;
4413 cgs_write_ind_register(hwmgr->device,
4414 CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
4415
4416 if (!phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4417 PHM_PlatformCaps_UVDDPM) ||
4418 phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4419 PHM_PlatformCaps_StablePState))
4420 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4421 PPSMC_MSG_UVDDPM_SetEnabledMask,
4422 (uint32_t)(1 << data->smc_state_table.UvdBootLevel));
4423 }
4424
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004425 return polaris10_enable_disable_uvd_dpm(hwmgr, !bgate);
Eric Huang92c6d642016-02-05 14:47:06 -05004426}
4427
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004428static int polaris10_update_vce_dpm(struct pp_hwmgr *hwmgr, const void *input)
Rex Zhua23eefa2015-11-19 18:23:32 +08004429{
4430 const struct phm_set_power_state_input *states =
4431 (const struct phm_set_power_state_input *)input;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004432 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
4433 const struct polaris10_power_state *polaris10_nps =
4434 cast_const_phw_polaris10_power_state(states->pnew_state);
4435 const struct polaris10_power_state *polaris10_cps =
4436 cast_const_phw_polaris10_power_state(states->pcurrent_state);
Rex Zhua23eefa2015-11-19 18:23:32 +08004437
4438 uint32_t mm_boot_level_offset, mm_boot_level_value;
4439 struct phm_ppt_v1_information *table_info =
4440 (struct phm_ppt_v1_information *)(hwmgr->pptable);
4441
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004442 if (polaris10_nps->vce_clks.evclk > 0 &&
4443 (polaris10_cps == NULL || polaris10_cps->vce_clks.evclk == 0)) {
Rex Zhua23eefa2015-11-19 18:23:32 +08004444
4445 data->smc_state_table.VceBootLevel =
4446 (uint8_t) (table_info->mm_dep_table->count - 1);
4447
4448 mm_boot_level_offset = data->dpm_table_start +
4449 offsetof(SMU74_Discrete_DpmTable, VceBootLevel);
4450 mm_boot_level_offset /= 4;
4451 mm_boot_level_offset *= 4;
4452 mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
4453 CGS_IND_REG__SMC, mm_boot_level_offset);
4454 mm_boot_level_value &= 0xFF00FFFF;
4455 mm_boot_level_value |= data->smc_state_table.VceBootLevel << 16;
4456 cgs_write_ind_register(hwmgr->device,
4457 CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
4458
4459 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_StablePState)) {
4460 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4461 PPSMC_MSG_VCEDPM_SetEnabledMask,
4462 (uint32_t)1 << data->smc_state_table.VceBootLevel);
4463
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004464 polaris10_enable_disable_vce_dpm(hwmgr, true);
4465 } else if (polaris10_nps->vce_clks.evclk == 0 &&
4466 polaris10_cps != NULL &&
4467 polaris10_cps->vce_clks.evclk > 0)
4468 polaris10_enable_disable_vce_dpm(hwmgr, false);
Rex Zhua23eefa2015-11-19 18:23:32 +08004469 }
4470
4471 return 0;
4472}
4473
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004474int polaris10_update_samu_dpm(struct pp_hwmgr *hwmgr, bool bgate)
Eric Huang92c6d642016-02-05 14:47:06 -05004475{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004476 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Eric Huang92c6d642016-02-05 14:47:06 -05004477 uint32_t mm_boot_level_offset, mm_boot_level_value;
Eric Huang92c6d642016-02-05 14:47:06 -05004478
4479 if (!bgate) {
Rex Zhu871fd842016-06-12 11:18:01 +08004480 data->smc_state_table.SamuBootLevel = 0;
Eric Huang92c6d642016-02-05 14:47:06 -05004481 mm_boot_level_offset = data->dpm_table_start +
4482 offsetof(SMU74_Discrete_DpmTable, SamuBootLevel);
4483 mm_boot_level_offset /= 4;
4484 mm_boot_level_offset *= 4;
4485 mm_boot_level_value = cgs_read_ind_register(hwmgr->device,
4486 CGS_IND_REG__SMC, mm_boot_level_offset);
4487 mm_boot_level_value &= 0xFFFFFF00;
4488 mm_boot_level_value |= data->smc_state_table.SamuBootLevel << 0;
4489 cgs_write_ind_register(hwmgr->device,
4490 CGS_IND_REG__SMC, mm_boot_level_offset, mm_boot_level_value);
4491
4492 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4493 PHM_PlatformCaps_StablePState))
4494 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4495 PPSMC_MSG_SAMUDPM_SetEnabledMask,
4496 (uint32_t)(1 << data->smc_state_table.SamuBootLevel));
4497 }
4498
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004499 return polaris10_enable_disable_samu_dpm(hwmgr, !bgate);
Eric Huang92c6d642016-02-05 14:47:06 -05004500}
4501
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004502static int polaris10_update_sclk_threshold(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004503{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004504 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004505
4506 int result = 0;
4507 uint32_t low_sclk_interrupt_threshold = 0;
4508
4509 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4510 PHM_PlatformCaps_SclkThrottleLowNotification)
4511 && (hwmgr->gfx_arbiter.sclk_threshold !=
4512 data->low_sclk_interrupt_threshold)) {
4513 data->low_sclk_interrupt_threshold =
4514 hwmgr->gfx_arbiter.sclk_threshold;
4515 low_sclk_interrupt_threshold =
4516 data->low_sclk_interrupt_threshold;
4517
4518 CONVERT_FROM_HOST_TO_SMC_UL(low_sclk_interrupt_threshold);
4519
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004520 result = polaris10_copy_bytes_to_smc(
Rex Zhua23eefa2015-11-19 18:23:32 +08004521 hwmgr->smumgr,
4522 data->dpm_table_start +
4523 offsetof(SMU74_Discrete_DpmTable,
4524 LowSclkInterruptThreshold),
4525 (uint8_t *)&low_sclk_interrupt_threshold,
4526 sizeof(uint32_t),
4527 data->sram_end);
4528 }
4529
4530 return result;
4531}
4532
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004533static int polaris10_program_mem_timing_parameters(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004534{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004535 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004536
4537 if (data->need_update_smu7_dpm_table &
4538 (DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_OD_UPDATE_MCLK))
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004539 return polaris10_program_memory_timing_parameters(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004540
4541 return 0;
4542}
4543
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004544static int polaris10_unfreeze_sclk_mclk_dpm(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004545{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004546 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004547
4548 if (0 == data->need_update_smu7_dpm_table)
4549 return 0;
4550
4551 if ((0 == data->sclk_dpm_key_disabled) &&
4552 (data->need_update_smu7_dpm_table &
4553 (DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_UPDATE_SCLK))) {
4554
Edward O'Callaghan4ba27f92016-07-12 10:17:56 +10004555 PP_ASSERT_WITH_CODE(polaris10_is_dpm_running(hwmgr),
4556 "Trying to Unfreeze SCLK DPM when DPM is disabled",
Rex Zhua23eefa2015-11-19 18:23:32 +08004557 );
4558 PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr->smumgr,
4559 PPSMC_MSG_SCLKDPM_UnfreezeLevel),
4560 "Failed to unfreeze SCLK DPM during UnFreezeSclkMclkDPM Function!",
4561 return -1);
4562 }
4563
4564 if ((0 == data->mclk_dpm_key_disabled) &&
4565 (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
4566
Edward O'Callaghan4ba27f92016-07-12 10:17:56 +10004567 PP_ASSERT_WITH_CODE(polaris10_is_dpm_running(hwmgr),
4568 "Trying to Unfreeze MCLK DPM when DPM is disabled",
Rex Zhua23eefa2015-11-19 18:23:32 +08004569 );
4570 PP_ASSERT_WITH_CODE(0 == smum_send_msg_to_smc(hwmgr->smumgr,
4571 PPSMC_MSG_SCLKDPM_UnfreezeLevel),
4572 "Failed to unfreeze MCLK DPM during UnFreezeSclkMclkDPM Function!",
4573 return -1);
4574 }
4575
4576 data->need_update_smu7_dpm_table = 0;
4577
4578 return 0;
4579}
4580
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004581static int polaris10_notify_link_speed_change_after_state_change(
Rex Zhua23eefa2015-11-19 18:23:32 +08004582 struct pp_hwmgr *hwmgr, const void *input)
4583{
4584 const struct phm_set_power_state_input *states =
4585 (const struct phm_set_power_state_input *)input;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004586 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
4587 const struct polaris10_power_state *polaris10_ps =
4588 cast_const_phw_polaris10_power_state(states->pnew_state);
4589 uint16_t target_link_speed = polaris10_get_maximum_link_speed(hwmgr, polaris10_ps);
Rex Zhua23eefa2015-11-19 18:23:32 +08004590 uint8_t request;
4591
4592 if (data->pspp_notify_required) {
4593 if (target_link_speed == PP_PCIEGen3)
4594 request = PCIE_PERF_REQ_GEN3;
4595 else if (target_link_speed == PP_PCIEGen2)
4596 request = PCIE_PERF_REQ_GEN2;
4597 else
4598 request = PCIE_PERF_REQ_GEN1;
4599
4600 if (request == PCIE_PERF_REQ_GEN1 &&
4601 phm_get_current_pcie_speed(hwmgr) > 0)
4602 return 0;
4603
4604 if (acpi_pcie_perf_request(hwmgr->device, request, false)) {
4605 if (PP_PCIEGen2 == target_link_speed)
4606 printk("PSPP request to switch to Gen2 from Gen3 Failed!");
4607 else
4608 printk("PSPP request to switch to Gen1 from Gen2 Failed!");
4609 }
4610 }
4611
4612 return 0;
4613}
4614
Rex Zhu83a7af62016-06-23 11:05:00 +08004615static int polaris10_notify_smc_display(struct pp_hwmgr *hwmgr)
4616{
4617 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
4618
4619 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4620 (PPSMC_Msg)PPSMC_MSG_SetVBITimeout, data->frame_time_x2);
4621 return (smum_send_msg_to_smc(hwmgr->smumgr, (PPSMC_Msg)PPSMC_HasDisplay) == 0) ? 0 : -EINVAL;
4622}
4623
Rex Zhu36e6b912016-06-08 12:56:20 +08004624
4625
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004626static int polaris10_set_power_state_tasks(struct pp_hwmgr *hwmgr, const void *input)
Rex Zhua23eefa2015-11-19 18:23:32 +08004627{
4628 int tmp_result, result = 0;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004629 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004630
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004631 tmp_result = polaris10_find_dpm_states_clocks_in_dpm_table(hwmgr, input);
Rex Zhua23eefa2015-11-19 18:23:32 +08004632 PP_ASSERT_WITH_CODE((0 == tmp_result),
4633 "Failed to find DPM states clocks in DPM table!",
4634 result = tmp_result);
4635
4636 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4637 PHM_PlatformCaps_PCIEPerformanceRequest)) {
4638 tmp_result =
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004639 polaris10_request_link_speed_change_before_state_change(hwmgr, input);
Rex Zhua23eefa2015-11-19 18:23:32 +08004640 PP_ASSERT_WITH_CODE((0 == tmp_result),
4641 "Failed to request link speed change before state change!",
4642 result = tmp_result);
4643 }
4644
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004645 tmp_result = polaris10_freeze_sclk_mclk_dpm(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004646 PP_ASSERT_WITH_CODE((0 == tmp_result),
4647 "Failed to freeze SCLK MCLK DPM!", result = tmp_result);
4648
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004649 tmp_result = polaris10_populate_and_upload_sclk_mclk_dpm_levels(hwmgr, input);
Rex Zhua23eefa2015-11-19 18:23:32 +08004650 PP_ASSERT_WITH_CODE((0 == tmp_result),
4651 "Failed to populate and upload SCLK MCLK DPM levels!",
4652 result = tmp_result);
4653
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004654 tmp_result = polaris10_generate_dpm_level_enable_mask(hwmgr, input);
Rex Zhua23eefa2015-11-19 18:23:32 +08004655 PP_ASSERT_WITH_CODE((0 == tmp_result),
4656 "Failed to generate DPM level enabled mask!",
4657 result = tmp_result);
4658
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004659 tmp_result = polaris10_update_vce_dpm(hwmgr, input);
Rex Zhua23eefa2015-11-19 18:23:32 +08004660 PP_ASSERT_WITH_CODE((0 == tmp_result),
4661 "Failed to update VCE DPM!",
4662 result = tmp_result);
4663
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004664 tmp_result = polaris10_update_sclk_threshold(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004665 PP_ASSERT_WITH_CODE((0 == tmp_result),
4666 "Failed to update SCLK threshold!",
4667 result = tmp_result);
4668
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004669 tmp_result = polaris10_program_mem_timing_parameters(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004670 PP_ASSERT_WITH_CODE((0 == tmp_result),
4671 "Failed to program memory timing parameters!",
4672 result = tmp_result);
4673
Rex Zhu83a7af62016-06-23 11:05:00 +08004674 tmp_result = polaris10_notify_smc_display(hwmgr);
4675 PP_ASSERT_WITH_CODE((0 == tmp_result),
4676 "Failed to notify smc display settings!",
4677 result = tmp_result);
4678
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004679 tmp_result = polaris10_unfreeze_sclk_mclk_dpm(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004680 PP_ASSERT_WITH_CODE((0 == tmp_result),
4681 "Failed to unfreeze SCLK MCLK DPM!",
4682 result = tmp_result);
4683
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004684 tmp_result = polaris10_upload_dpm_level_enable_mask(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004685 PP_ASSERT_WITH_CODE((0 == tmp_result),
4686 "Failed to upload DPM level enabled mask!",
4687 result = tmp_result);
4688
4689 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
4690 PHM_PlatformCaps_PCIEPerformanceRequest)) {
4691 tmp_result =
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004692 polaris10_notify_link_speed_change_after_state_change(hwmgr, input);
Rex Zhua23eefa2015-11-19 18:23:32 +08004693 PP_ASSERT_WITH_CODE((0 == tmp_result),
4694 "Failed to notify link speed change after state change!",
4695 result = tmp_result);
4696 }
4697 data->apply_optimized_settings = false;
4698 return result;
4699}
4700
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004701static int polaris10_set_max_fan_pwm_output(struct pp_hwmgr *hwmgr, uint16_t us_max_fan_pwm)
Rex Zhua23eefa2015-11-19 18:23:32 +08004702{
Eric Huangeede5262016-02-02 16:09:24 -05004703 hwmgr->thermal_controller.
4704 advanceFanControlParameters.usMaxFanPWM = us_max_fan_pwm;
Rex Zhua23eefa2015-11-19 18:23:32 +08004705
Eric Huangeede5262016-02-02 16:09:24 -05004706 if (phm_is_hw_access_blocked(hwmgr))
Rex Zhua23eefa2015-11-19 18:23:32 +08004707 return 0;
Eric Huangeede5262016-02-02 16:09:24 -05004708
4709 return smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4710 PPSMC_MSG_SetFanPwmMax, us_max_fan_pwm);
Rex Zhua23eefa2015-11-19 18:23:32 +08004711}
4712
Rex Zhu83a7af62016-06-23 11:05:00 +08004713
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004714int polaris10_notify_smc_display_change(struct pp_hwmgr *hwmgr, bool has_display)
Rex Zhua23eefa2015-11-19 18:23:32 +08004715{
4716 PPSMC_Msg msg = has_display ? (PPSMC_Msg)PPSMC_HasDisplay : (PPSMC_Msg)PPSMC_NoDisplay;
4717
4718 return (smum_send_msg_to_smc(hwmgr->smumgr, msg) == 0) ? 0 : -1;
4719}
4720
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004721int polaris10_notify_smc_display_config_after_ps_adjustment(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004722{
4723 uint32_t num_active_displays = 0;
4724 struct cgs_display_info info = {0};
4725 info.mode_info = NULL;
4726
4727 cgs_get_active_displays_info(hwmgr->device, &info);
4728
4729 num_active_displays = info.display_count;
4730
4731 if (num_active_displays > 1) /* to do && (pHwMgr->pPECI->displayConfiguration.bMultiMonitorInSync != TRUE)) */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004732 polaris10_notify_smc_display_change(hwmgr, false);
Rex Zhua23eefa2015-11-19 18:23:32 +08004733
Rex Zhu36e6b912016-06-08 12:56:20 +08004734
Rex Zhua23eefa2015-11-19 18:23:32 +08004735 return 0;
4736}
4737
4738/**
4739* Programs the display gap
4740*
4741* @param hwmgr the address of the powerplay hardware manager.
4742* @return always OK
4743*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004744int polaris10_program_display_gap(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004745{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004746 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004747 uint32_t num_active_displays = 0;
4748 uint32_t display_gap = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_DISPLAY_GAP_CNTL);
4749 uint32_t display_gap2;
4750 uint32_t pre_vbi_time_in_us;
4751 uint32_t frame_time_in_us;
4752 uint32_t ref_clock;
4753 uint32_t refresh_rate = 0;
4754 struct cgs_display_info info = {0};
4755 struct cgs_mode_info mode_info;
4756
4757 info.mode_info = &mode_info;
4758
4759 cgs_get_active_displays_info(hwmgr->device, &info);
4760 num_active_displays = info.display_count;
4761
4762 display_gap = PHM_SET_FIELD(display_gap, CG_DISPLAY_GAP_CNTL, DISP_GAP, (num_active_displays > 0) ? DISPLAY_GAP_VBLANK_OR_WM : DISPLAY_GAP_IGNORE);
4763 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_DISPLAY_GAP_CNTL, display_gap);
4764
4765 ref_clock = mode_info.ref_clock;
4766 refresh_rate = mode_info.refresh_rate;
4767
4768 if (0 == refresh_rate)
4769 refresh_rate = 60;
4770
4771 frame_time_in_us = 1000000 / refresh_rate;
4772
4773 pre_vbi_time_in_us = frame_time_in_us - 200 - mode_info.vblank_time_us;
Rex Zhu83a7af62016-06-23 11:05:00 +08004774 data->frame_time_x2 = frame_time_in_us * 2 / 100;
4775
Rex Zhua23eefa2015-11-19 18:23:32 +08004776 display_gap2 = pre_vbi_time_in_us * (ref_clock / 100);
4777
4778 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixCG_DISPLAY_GAP_CNTL2, display_gap2);
4779
4780 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, data->soft_regs_start + offsetof(SMU74_SoftRegisters, PreVBlankGap), 0x64);
4781
4782 cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC, data->soft_regs_start + offsetof(SMU74_SoftRegisters, VBlankTimeout), (frame_time_in_us - pre_vbi_time_in_us));
4783
Rex Zhu36e6b912016-06-08 12:56:20 +08004784
Rex Zhua23eefa2015-11-19 18:23:32 +08004785 return 0;
4786}
4787
4788
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004789int polaris10_display_configuration_changed_task(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004790{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004791 return polaris10_program_display_gap(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004792}
4793
4794/**
4795* Set maximum target operating fan output RPM
4796*
4797* @param hwmgr: the address of the powerplay hardware manager.
4798* @param usMaxFanRpm: max operating fan RPM value.
4799* @return The response that came from the SMC.
4800*/
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004801static int polaris10_set_max_fan_rpm_output(struct pp_hwmgr *hwmgr, uint16_t us_max_fan_rpm)
Rex Zhua23eefa2015-11-19 18:23:32 +08004802{
Eric Huangeede5262016-02-02 16:09:24 -05004803 hwmgr->thermal_controller.
4804 advanceFanControlParameters.usMaxFanRPM = us_max_fan_rpm;
4805
4806 if (phm_is_hw_access_blocked(hwmgr))
4807 return 0;
4808
4809 return smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
4810 PPSMC_MSG_SetFanRpmMax, us_max_fan_rpm);
Rex Zhua23eefa2015-11-19 18:23:32 +08004811}
4812
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004813int polaris10_register_internal_thermal_interrupt(struct pp_hwmgr *hwmgr,
Rex Zhua23eefa2015-11-19 18:23:32 +08004814 const void *thermal_interrupt_info)
4815{
4816 return 0;
4817}
4818
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004819bool polaris10_check_smc_update_required_for_display_configuration(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004820{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004821 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004822 bool is_update_required = false;
4823 struct cgs_display_info info = {0, 0, NULL};
4824
4825 cgs_get_active_displays_info(hwmgr->device, &info);
4826
4827 if (data->display_timing.num_existing_displays != info.display_count)
4828 is_update_required = true;
4829/* TO DO NEED TO GET DEEP SLEEP CLOCK FROM DAL
4830 if (phm_cap_enabled(hwmgr->hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep)) {
4831 cgs_get_min_clock_settings(hwmgr->device, &min_clocks);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004832 if (min_clocks.engineClockInSR != data->display_timing.minClockInSR &&
4833 (min_clocks.engineClockInSR >= POLARIS10_MINIMUM_ENGINE_CLOCK ||
4834 data->display_timing.minClockInSR >= POLARIS10_MINIMUM_ENGINE_CLOCK))
Rex Zhua23eefa2015-11-19 18:23:32 +08004835 is_update_required = true;
4836*/
4837 return is_update_required;
4838}
4839
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004840static inline bool polaris10_are_power_levels_equal(const struct polaris10_performance_level *pl1,
4841 const struct polaris10_performance_level *pl2)
Rex Zhua23eefa2015-11-19 18:23:32 +08004842{
4843 return ((pl1->memory_clock == pl2->memory_clock) &&
4844 (pl1->engine_clock == pl2->engine_clock) &&
4845 (pl1->pcie_gen == pl2->pcie_gen) &&
4846 (pl1->pcie_lane == pl2->pcie_lane));
4847}
4848
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004849int polaris10_check_states_equal(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *pstate1, const struct pp_hw_power_state *pstate2, bool *equal)
Rex Zhua23eefa2015-11-19 18:23:32 +08004850{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004851 const struct polaris10_power_state *psa = cast_const_phw_polaris10_power_state(pstate1);
4852 const struct polaris10_power_state *psb = cast_const_phw_polaris10_power_state(pstate2);
Rex Zhua23eefa2015-11-19 18:23:32 +08004853 int i;
4854
4855 if (pstate1 == NULL || pstate2 == NULL || equal == NULL)
4856 return -EINVAL;
4857
4858 /* If the two states don't even have the same number of performance levels they cannot be the same state. */
4859 if (psa->performance_level_count != psb->performance_level_count) {
4860 *equal = false;
4861 return 0;
4862 }
4863
4864 for (i = 0; i < psa->performance_level_count; i++) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004865 if (!polaris10_are_power_levels_equal(&(psa->performance_levels[i]), &(psb->performance_levels[i]))) {
Rex Zhua23eefa2015-11-19 18:23:32 +08004866 /* If we have found even one performance level pair that is different the states are different. */
4867 *equal = false;
4868 return 0;
4869 }
4870 }
4871
4872 /* If all performance levels are the same try to use the UVD clocks to break the tie.*/
4873 *equal = ((psa->uvd_clks.vclk == psb->uvd_clks.vclk) && (psa->uvd_clks.dclk == psb->uvd_clks.dclk));
4874 *equal &= ((psa->vce_clks.evclk == psb->vce_clks.evclk) && (psa->vce_clks.ecclk == psb->vce_clks.ecclk));
4875 *equal &= (psa->sclk_threshold == psb->sclk_threshold);
4876
4877 return 0;
4878}
4879
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004880int polaris10_upload_mc_firmware(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004881{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004882 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004883
4884 uint32_t vbios_version;
4885
4886 /* Read MC indirect register offset 0x9F bits [3:0] to see if VBIOS has already loaded a full version of MC ucode or not.*/
4887
4888 phm_get_mc_microcode_version(hwmgr);
4889 vbios_version = hwmgr->microcode_version_info.MC & 0xf;
4890 /* Full version of MC ucode has already been loaded. */
4891 if (vbios_version == 0) {
4892 data->need_long_memory_training = false;
4893 return 0;
4894 }
4895
Rex Zhu83a7af62016-06-23 11:05:00 +08004896 data->need_long_memory_training = false;
Rex Zhua23eefa2015-11-19 18:23:32 +08004897
4898/*
Christian Königedf600d2016-05-03 15:54:54 +02004899 * PPMCME_FirmwareDescriptorEntry *pfd = NULL;
Rex Zhua23eefa2015-11-19 18:23:32 +08004900 pfd = &tonga_mcmeFirmware;
4901 if (0 == PHM_READ_FIELD(hwmgr->device, MC_SEQ_SUP_CNTL, RUN))
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004902 polaris10_load_mc_microcode(hwmgr, pfd->dpmThreshold,
Rex Zhua23eefa2015-11-19 18:23:32 +08004903 pfd->cfgArray, pfd->cfgSize, pfd->ioDebugArray,
4904 pfd->ioDebugSize, pfd->ucodeArray, pfd->ucodeSize);
4905*/
4906 return 0;
4907}
4908
4909/**
4910 * Read clock related registers.
4911 *
4912 * @param hwmgr the address of the powerplay hardware manager.
4913 * @return always 0
4914 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004915static int polaris10_read_clock_registers(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004916{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004917 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004918
4919 data->clock_registers.vCG_SPLL_FUNC_CNTL = cgs_read_ind_register(hwmgr->device,
4920 CGS_IND_REG__SMC, ixCG_SPLL_FUNC_CNTL)
4921 & CG_SPLL_FUNC_CNTL__SPLL_BYPASS_EN_MASK;
4922
4923 data->clock_registers.vCG_SPLL_FUNC_CNTL_2 = cgs_read_ind_register(hwmgr->device,
4924 CGS_IND_REG__SMC, ixCG_SPLL_FUNC_CNTL_2)
4925 & CG_SPLL_FUNC_CNTL_2__SCLK_MUX_SEL_MASK;
4926
4927 data->clock_registers.vCG_SPLL_FUNC_CNTL_4 = cgs_read_ind_register(hwmgr->device,
4928 CGS_IND_REG__SMC, ixCG_SPLL_FUNC_CNTL_4)
4929 & CG_SPLL_FUNC_CNTL_4__SPLL_SPARE_MASK;
4930
4931 return 0;
4932}
4933
4934/**
4935 * Find out if memory is GDDR5.
4936 *
4937 * @param hwmgr the address of the powerplay hardware manager.
4938 * @return always 0
4939 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004940static int polaris10_get_memory_type(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004941{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004942 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004943 uint32_t temp;
4944
4945 temp = cgs_read_register(hwmgr->device, mmMC_SEQ_MISC0);
4946
4947 data->is_memory_gddr5 = (MC_SEQ_MISC0_GDDR5_VALUE ==
4948 ((temp & MC_SEQ_MISC0_GDDR5_MASK) >>
4949 MC_SEQ_MISC0_GDDR5_SHIFT));
4950
4951 return 0;
4952}
4953
4954/**
4955 * Enables Dynamic Power Management by SMC
4956 *
4957 * @param hwmgr the address of the powerplay hardware manager.
4958 * @return always 0
4959 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004960static int polaris10_enable_acpi_power_management(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004961{
4962 PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
4963 GENERAL_PWRMGT, STATIC_PM_EN, 1);
4964
4965 return 0;
4966}
4967
4968/**
4969 * Initialize PowerGating States for different engines
4970 *
4971 * @param hwmgr the address of the powerplay hardware manager.
4972 * @return always 0
4973 */
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004974static int polaris10_init_power_gate_state(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004975{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004976 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004977
4978 data->uvd_power_gated = false;
4979 data->vce_power_gated = false;
4980 data->samu_power_gated = false;
4981
4982 return 0;
4983}
4984
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004985static int polaris10_init_sclk_threshold(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004986{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004987 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
Rex Zhua23eefa2015-11-19 18:23:32 +08004988 data->low_sclk_interrupt_threshold = 0;
4989
4990 return 0;
4991}
4992
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004993int polaris10_setup_asic_task(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08004994{
4995 int tmp_result, result = 0;
4996
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004997 polaris10_upload_mc_firmware(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08004998
Flora Cui2cc0c0b2016-03-14 18:33:29 -04004999 tmp_result = polaris10_read_clock_registers(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08005000 PP_ASSERT_WITH_CODE((0 == tmp_result),
5001 "Failed to read clock registers!", result = tmp_result);
5002
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005003 tmp_result = polaris10_get_memory_type(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08005004 PP_ASSERT_WITH_CODE((0 == tmp_result),
5005 "Failed to get memory type!", result = tmp_result);
5006
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005007 tmp_result = polaris10_enable_acpi_power_management(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08005008 PP_ASSERT_WITH_CODE((0 == tmp_result),
5009 "Failed to enable ACPI power management!", result = tmp_result);
5010
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005011 tmp_result = polaris10_init_power_gate_state(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08005012 PP_ASSERT_WITH_CODE((0 == tmp_result),
5013 "Failed to init power gate state!", result = tmp_result);
5014
5015 tmp_result = phm_get_mc_microcode_version(hwmgr);
5016 PP_ASSERT_WITH_CODE((0 == tmp_result),
5017 "Failed to get MC microcode version!", result = tmp_result);
5018
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005019 tmp_result = polaris10_init_sclk_threshold(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08005020 PP_ASSERT_WITH_CODE((0 == tmp_result),
5021 "Failed to init sclk threshold!", result = tmp_result);
5022
5023 return result;
5024}
5025
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005026static int polaris10_force_clock_level(struct pp_hwmgr *hwmgr,
Eric Huang56327082016-04-12 14:57:23 -04005027 enum pp_clock_type type, uint32_t mask)
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005028{
5029 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
5030
5031 if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
5032 return -EINVAL;
5033
5034 switch (type) {
5035 case PP_SCLK:
5036 if (!data->sclk_dpm_key_disabled)
5037 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
5038 PPSMC_MSG_SCLKDPM_SetEnabledMask,
Eric Huang56327082016-04-12 14:57:23 -04005039 data->dpm_level_enable_mask.sclk_dpm_enable_mask & mask);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005040 break;
5041 case PP_MCLK:
5042 if (!data->mclk_dpm_key_disabled)
5043 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
5044 PPSMC_MSG_MCLKDPM_SetEnabledMask,
Eric Huang56327082016-04-12 14:57:23 -04005045 data->dpm_level_enable_mask.mclk_dpm_enable_mask & mask);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005046 break;
5047 case PP_PCIE:
Eric Huang56327082016-04-12 14:57:23 -04005048 {
5049 uint32_t tmp = mask & data->dpm_level_enable_mask.pcie_dpm_enable_mask;
5050 uint32_t level = 0;
5051
5052 while (tmp >>= 1)
5053 level++;
5054
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005055 if (!data->pcie_dpm_key_disabled)
5056 smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
5057 PPSMC_MSG_PCIeDPM_ForceLevel,
Eric Huang56327082016-04-12 14:57:23 -04005058 level);
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005059 break;
Eric Huang56327082016-04-12 14:57:23 -04005060 }
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005061 default:
5062 break;
5063 }
5064
5065 return 0;
5066}
5067
5068static uint16_t polaris10_get_current_pcie_speed(struct pp_hwmgr *hwmgr)
5069{
5070 uint32_t speedCntl = 0;
5071
5072 /* mmPCIE_PORT_INDEX rename as mmPCIE_INDEX */
5073 speedCntl = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__PCIE,
5074 ixPCIE_LC_SPEED_CNTL);
5075 return((uint16_t)PHM_GET_FIELD(speedCntl,
5076 PCIE_LC_SPEED_CNTL, LC_CURRENT_DATA_RATE));
5077}
5078
5079static int polaris10_print_clock_levels(struct pp_hwmgr *hwmgr,
5080 enum pp_clock_type type, char *buf)
5081{
5082 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
5083 struct polaris10_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
5084 struct polaris10_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
5085 struct polaris10_single_dpm_table *pcie_table = &(data->dpm_table.pcie_speed_table);
5086 int i, now, size = 0;
5087 uint32_t clock, pcie_speed;
5088
5089 switch (type) {
5090 case PP_SCLK:
5091 smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_API_GetSclkFrequency);
5092 clock = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
5093
5094 for (i = 0; i < sclk_table->count; i++) {
5095 if (clock > sclk_table->dpm_levels[i].value)
5096 continue;
5097 break;
5098 }
5099 now = i;
5100
5101 for (i = 0; i < sclk_table->count; i++)
5102 size += sprintf(buf + size, "%d: %uMhz %s\n",
5103 i, sclk_table->dpm_levels[i].value / 100,
5104 (i == now) ? "*" : "");
5105 break;
5106 case PP_MCLK:
5107 smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_API_GetMclkFrequency);
5108 clock = cgs_read_register(hwmgr->device, mmSMC_MSG_ARG_0);
5109
5110 for (i = 0; i < mclk_table->count; i++) {
5111 if (clock > mclk_table->dpm_levels[i].value)
5112 continue;
5113 break;
5114 }
5115 now = i;
5116
5117 for (i = 0; i < mclk_table->count; i++)
5118 size += sprintf(buf + size, "%d: %uMhz %s\n",
5119 i, mclk_table->dpm_levels[i].value / 100,
5120 (i == now) ? "*" : "");
5121 break;
5122 case PP_PCIE:
5123 pcie_speed = polaris10_get_current_pcie_speed(hwmgr);
5124 for (i = 0; i < pcie_table->count; i++) {
5125 if (pcie_speed != pcie_table->dpm_levels[i].value)
5126 continue;
5127 break;
5128 }
5129 now = i;
5130
5131 for (i = 0; i < pcie_table->count; i++)
5132 size += sprintf(buf + size, "%d: %s %s\n", i,
5133 (pcie_table->dpm_levels[i].value == 0) ? "2.5GB, x8" :
5134 (pcie_table->dpm_levels[i].value == 1) ? "5.0GB, x16" :
5135 (pcie_table->dpm_levels[i].value == 2) ? "8.0GB, x16" : "",
5136 (i == now) ? "*" : "");
5137 break;
5138 default:
5139 break;
5140 }
5141 return size;
5142}
5143
Rex Zhu9e26bbb2016-03-23 18:47:29 +08005144static int polaris10_set_fan_control_mode(struct pp_hwmgr *hwmgr, uint32_t mode)
5145{
5146 if (mode) {
5147 /* stop auto-manage */
5148 if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
5149 PHM_PlatformCaps_MicrocodeFanControl))
5150 polaris10_fan_ctrl_stop_smc_fan_control(hwmgr);
5151 polaris10_fan_ctrl_set_static_mode(hwmgr, mode);
5152 } else
5153 /* restart auto-manage */
5154 polaris10_fan_ctrl_reset_fan_speed_to_default(hwmgr);
5155
5156 return 0;
5157}
5158
5159static int polaris10_get_fan_control_mode(struct pp_hwmgr *hwmgr)
5160{
5161 if (hwmgr->fan_ctrl_is_in_default_mode)
5162 return hwmgr->fan_ctrl_default_mode;
5163 else
5164 return PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
5165 CG_FDO_CTRL2, FDO_PWM_MODE);
5166}
5167
Eric Huang09a04262016-05-12 15:19:10 -04005168static int polaris10_get_sclk_od(struct pp_hwmgr *hwmgr)
5169{
5170 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
5171 struct polaris10_single_dpm_table *sclk_table = &(data->dpm_table.sclk_table);
5172 struct polaris10_single_dpm_table *golden_sclk_table =
5173 &(data->golden_dpm_table.sclk_table);
5174 int value;
5175
5176 value = (sclk_table->dpm_levels[sclk_table->count - 1].value -
5177 golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value) *
5178 100 /
5179 golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value;
5180
5181 return value;
5182}
5183
5184static int polaris10_set_sclk_od(struct pp_hwmgr *hwmgr, uint32_t value)
5185{
5186 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
5187 struct polaris10_single_dpm_table *golden_sclk_table =
5188 &(data->golden_dpm_table.sclk_table);
5189 struct pp_power_state *ps;
5190 struct polaris10_power_state *polaris10_ps;
5191
5192 if (value > 20)
5193 value = 20;
5194
5195 ps = hwmgr->request_ps;
5196
5197 if (ps == NULL)
5198 return -EINVAL;
5199
5200 polaris10_ps = cast_phw_polaris10_power_state(&ps->hardware);
5201
5202 polaris10_ps->performance_levels[polaris10_ps->performance_level_count - 1].engine_clock =
5203 golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value *
5204 value / 100 +
5205 golden_sclk_table->dpm_levels[golden_sclk_table->count - 1].value;
5206
5207 return 0;
5208}
5209
Eric Huang0c9e2002016-05-24 16:22:34 -04005210static int polaris10_get_mclk_od(struct pp_hwmgr *hwmgr)
5211{
5212 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
5213 struct polaris10_single_dpm_table *mclk_table = &(data->dpm_table.mclk_table);
5214 struct polaris10_single_dpm_table *golden_mclk_table =
5215 &(data->golden_dpm_table.mclk_table);
5216 int value;
5217
5218 value = (mclk_table->dpm_levels[mclk_table->count - 1].value -
5219 golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value) *
5220 100 /
5221 golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value;
5222
5223 return value;
5224}
5225
5226static int polaris10_set_mclk_od(struct pp_hwmgr *hwmgr, uint32_t value)
5227{
5228 struct polaris10_hwmgr *data = (struct polaris10_hwmgr *)(hwmgr->backend);
5229 struct polaris10_single_dpm_table *golden_mclk_table =
5230 &(data->golden_dpm_table.mclk_table);
5231 struct pp_power_state *ps;
5232 struct polaris10_power_state *polaris10_ps;
5233
5234 if (value > 20)
5235 value = 20;
5236
5237 ps = hwmgr->request_ps;
5238
5239 if (ps == NULL)
5240 return -EINVAL;
5241
5242 polaris10_ps = cast_phw_polaris10_power_state(&ps->hardware);
5243
5244 polaris10_ps->performance_levels[polaris10_ps->performance_level_count - 1].memory_clock =
5245 golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value *
5246 value / 100 +
5247 golden_mclk_table->dpm_levels[golden_mclk_table->count - 1].value;
5248
5249 return 0;
5250}
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005251static const struct pp_hwmgr_func polaris10_hwmgr_funcs = {
5252 .backend_init = &polaris10_hwmgr_backend_init,
5253 .backend_fini = &polaris10_hwmgr_backend_fini,
5254 .asic_setup = &polaris10_setup_asic_task,
5255 .dynamic_state_management_enable = &polaris10_enable_dpm_tasks,
5256 .apply_state_adjust_rules = polaris10_apply_state_adjust_rules,
5257 .force_dpm_level = &polaris10_force_dpm_level,
5258 .power_state_set = polaris10_set_power_state_tasks,
5259 .get_power_state_size = polaris10_get_power_state_size,
5260 .get_mclk = polaris10_dpm_get_mclk,
5261 .get_sclk = polaris10_dpm_get_sclk,
5262 .patch_boot_state = polaris10_dpm_patch_boot_state,
5263 .get_pp_table_entry = polaris10_get_pp_table_entry,
Rex Zhua23eefa2015-11-19 18:23:32 +08005264 .get_num_of_pp_table_entries = tonga_get_number_of_powerplay_table_entries,
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005265 .print_current_perforce_level = polaris10_print_current_perforce_level,
5266 .powerdown_uvd = polaris10_phm_powerdown_uvd,
5267 .powergate_uvd = polaris10_phm_powergate_uvd,
5268 .powergate_vce = polaris10_phm_powergate_vce,
5269 .disable_clock_power_gating = polaris10_phm_disable_clock_power_gating,
5270 .update_clock_gatings = polaris10_phm_update_clock_gatings,
5271 .notify_smc_display_config_after_ps_adjustment = polaris10_notify_smc_display_config_after_ps_adjustment,
5272 .display_config_changed = polaris10_display_configuration_changed_task,
5273 .set_max_fan_pwm_output = polaris10_set_max_fan_pwm_output,
5274 .set_max_fan_rpm_output = polaris10_set_max_fan_rpm_output,
5275 .get_temperature = polaris10_thermal_get_temperature,
5276 .stop_thermal_controller = polaris10_thermal_stop_thermal_controller,
5277 .get_fan_speed_info = polaris10_fan_ctrl_get_fan_speed_info,
5278 .get_fan_speed_percent = polaris10_fan_ctrl_get_fan_speed_percent,
5279 .set_fan_speed_percent = polaris10_fan_ctrl_set_fan_speed_percent,
5280 .reset_fan_speed_to_default = polaris10_fan_ctrl_reset_fan_speed_to_default,
5281 .get_fan_speed_rpm = polaris10_fan_ctrl_get_fan_speed_rpm,
5282 .set_fan_speed_rpm = polaris10_fan_ctrl_set_fan_speed_rpm,
5283 .uninitialize_thermal_controller = polaris10_thermal_ctrl_uninitialize_thermal_controller,
5284 .register_internal_thermal_interrupt = polaris10_register_internal_thermal_interrupt,
5285 .check_smc_update_required_for_display_configuration = polaris10_check_smc_update_required_for_display_configuration,
5286 .check_states_equal = polaris10_check_states_equal,
Rex Zhu9e26bbb2016-03-23 18:47:29 +08005287 .set_fan_control_mode = polaris10_set_fan_control_mode,
5288 .get_fan_control_mode = polaris10_get_fan_control_mode,
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005289 .force_clock_level = polaris10_force_clock_level,
5290 .print_clock_levels = polaris10_print_clock_levels,
5291 .enable_per_cu_power_gating = polaris10_phm_enable_per_cu_power_gating,
Eric Huang09a04262016-05-12 15:19:10 -04005292 .get_sclk_od = polaris10_get_sclk_od,
5293 .set_sclk_od = polaris10_set_sclk_od,
Eric Huang0c9e2002016-05-24 16:22:34 -04005294 .get_mclk_od = polaris10_get_mclk_od,
5295 .set_mclk_od = polaris10_set_mclk_od,
Rex Zhua23eefa2015-11-19 18:23:32 +08005296};
5297
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005298int polaris10_hwmgr_init(struct pp_hwmgr *hwmgr)
Rex Zhua23eefa2015-11-19 18:23:32 +08005299{
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005300 hwmgr->hwmgr_func = &polaris10_hwmgr_funcs;
Rex Zhua23eefa2015-11-19 18:23:32 +08005301 hwmgr->pptable_func = &tonga_pptable_funcs;
Flora Cui2cc0c0b2016-03-14 18:33:29 -04005302 pp_polaris10_thermal_initialize(hwmgr);
Rex Zhua23eefa2015-11-19 18:23:32 +08005303
5304 return 0;
5305}