fiji_smumgr.c 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2015 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

24
#include "pp_debug.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "smumgr.h"
#include "smu73.h"
#include "smu_ucode_xfer_vi.h"
#include "fiji_smumgr.h"
#include "fiji_ppsmc.h"
#include "smu73_discrete.h"
#include "ppatomctrl.h"
#include "smu/smu_7_1_3_d.h"
#include "smu/smu_7_1_3_sh_mask.h"
#include "gmc/gmc_8_1_d.h"
#include "gmc/gmc_8_1_sh_mask.h"
#include "oss/oss_3_0_d.h"
#include "gca/gfx_8_0_d.h"
#include "bif/bif_5_0_d.h"
#include "bif/bif_5_0_sh_mask.h"
#include "fiji_pwrvirus.h"
41
#include "fiji_smc.h"
42 43 44 45 46 47

#define AVFS_EN_MSB                                        1568
#define AVFS_EN_LSB                                        1568

#define FIJI_SMC_SIZE 0x20000

48
static const struct SMU73_Discrete_GraphicsLevel avfs_graphics_level[8] = {
49 50 51 52 53 54 55 56 57 58 59 60
		/*  Min        Sclk       pcie     DeepSleep Activity  CgSpll      CgSpll    spllSpread  SpllSpread   CcPwr  CcPwr  Sclk   Display     Enabled     Enabled                       Voltage    Power */
		/* Voltage,  Frequency,  DpmLevel,  DivId,    Level,  FuncCntl3,  FuncCntl4,  Spectrum,   Spectrum2,  DynRm, DynRm1  Did, Watermark, ForActivity, ForThrottle, UpHyst, DownHyst, DownHyst, Throttle */
		{ 0x3c0fd047, 0x30750000,   0x00,     0x03,   0x1e00, 0x00200410, 0x87020000, 0x21680000, 0x0c000000,   0,      0,   0x16,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
		{ 0xa00fd047, 0x409c0000,   0x01,     0x04,   0x1e00, 0x00800510, 0x87020000, 0x21680000, 0x11000000,   0,      0,   0x16,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
		{ 0x0410d047, 0x50c30000,   0x01,     0x00,   0x1e00, 0x00600410, 0x87020000, 0x21680000, 0x0d000000,   0,      0,   0x0e,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
		{ 0x6810d047, 0x60ea0000,   0x01,     0x00,   0x1e00, 0x00800410, 0x87020000, 0x21680000, 0x0e000000,   0,      0,   0x0c,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
		{ 0xcc10d047, 0xe8fd0000,   0x01,     0x00,   0x1e00, 0x00e00410, 0x87020000, 0x21680000, 0x0f000000,   0,      0,   0x0c,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
		{ 0x3011d047, 0x70110100,   0x01,     0x00,   0x1e00, 0x00400510, 0x87020000, 0x21680000, 0x10000000,   0,      0,   0x0c,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
		{ 0x9411d047, 0xf8240100,   0x01,     0x00,   0x1e00, 0x00a00510, 0x87020000, 0x21680000, 0x11000000,   0,      0,   0x0c,   0x00,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 },
		{ 0xf811d047, 0x80380100,   0x01,     0x00,   0x1e00, 0x00000610, 0x87020000, 0x21680000, 0x12000000,   0,      0,   0x0c,   0x01,       0x01,        0x01,      0x00,   0x00,      0x00,     0x00 }
};

61
static int fiji_start_smu_in_protection_mode(struct pp_hwmgr *hwmgr)
62 63 64 65
{
	int result = 0;

	/* Wait for smc boot up */
66
	/* PHM_WAIT_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND,
67 68
		RCU_UC_EVENTS, boot_seq_done, 0); */

69
	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
70 71
			SMC_SYSCON_RESET_CNTL, rst_reg, 1);

72
	result = smu7_upload_smu_firmware_image(hwmgr);
73 74 75 76
	if (result)
		return result;

	/* Clear status */
77
	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
78 79
			ixSMU_STATUS, 0);

80
	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
81 82 83
			SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);

	/* De-assert reset */
84
	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
85 86 87
			SMC_SYSCON_RESET_CNTL, rst_reg, 0);

	/* Wait for ROM firmware to initialize interrupt hendler */
88
	/*SMUM_WAIT_VFPF_INDIRECT_REGISTER(hwmgr, SMC_IND,
89 90 91
			SMC_INTR_CNTL_MASK_0, 0x10040, 0xFFFFFFFF); */

	/* Set SMU Auto Start */
92
	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
93 94 95
			SMU_INPUT_DATA, AUTO_START, 1);

	/* Clear firmware interrupt enable flag */
96
	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
97 98
			ixFIRMWARE_FLAGS, 0);

99
	PHM_WAIT_VFPF_INDIRECT_FIELD(hwmgr, SMC_IND, RCU_UC_EVENTS,
100 101
			INTERRUPTS_ENABLED, 1);

102 103
	cgs_write_register(hwmgr->device, mmSMC_MSG_ARG_0, 0x20000);
	cgs_write_register(hwmgr->device, mmSMC_MESSAGE_0, PPSMC_MSG_Test);
104
	PHM_WAIT_FIELD_UNEQUAL(hwmgr, SMC_RESP_0, SMC_RESP, 0);
105 106

	/* Wait for done bit to be set */
107
	PHM_WAIT_VFPF_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND,
108 109 110
			SMU_STATUS, SMU_DONE, 0);

	/* Check pass/failed indicator */
111
	if (SMUM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
112
			SMU_STATUS, SMU_PASS) != 1) {
113 114 115 116 117
		PP_ASSERT_WITH_CODE(false,
				"SMU Firmware start failed!", return -1);
	}

	/* Wait for firmware to initialize */
118
	PHM_WAIT_VFPF_INDIRECT_FIELD(hwmgr, SMC_IND,
119 120 121 122 123
			FIRMWARE_FLAGS, INTERRUPTS_ENABLED, 1);

	return result;
}

124
static int fiji_start_smu_in_non_protection_mode(struct pp_hwmgr *hwmgr)
125 126 127 128
{
	int result = 0;

	/* wait for smc boot up */
129
	PHM_WAIT_VFPF_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND,
130 131 132
			RCU_UC_EVENTS, boot_seq_done, 0);

	/* Clear firmware interrupt enable flag */
133
	cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
134 135 136
			ixFIRMWARE_FLAGS, 0);

	/* Assert reset */
137
	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
138 139
			SMC_SYSCON_RESET_CNTL, rst_reg, 1);

140
	result = smu7_upload_smu_firmware_image(hwmgr);
141 142 143 144
	if (result)
		return result;

	/* Set smc instruct start point at 0x0 */
145
	smu7_program_jump_on_start(hwmgr);
146 147

	/* Enable clock */
148
	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
149 150 151
			SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);

	/* De-assert reset */
152
	PHM_WRITE_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
153 154 155
			SMC_SYSCON_RESET_CNTL, rst_reg, 0);

	/* Wait for firmware to initialize */
156
	PHM_WAIT_VFPF_INDIRECT_FIELD(hwmgr, SMC_IND,
157 158 159 160 161
			FIRMWARE_FLAGS, INTERRUPTS_ENABLED, 1);

	return result;
}

162
static int fiji_setup_pwr_virus(struct pp_hwmgr *hwmgr)
163
{
164 165
	int i;
	int result = -EINVAL;
166 167
	uint32_t reg, data;

168
	const PWR_Command_Table *pvirus = PwrVirusTable;
169
	struct smu7_smumgr *smu_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
170 171 172

	for (i = 0; i < PWR_VIRUS_TABLE_SIZE; i++) {
		switch (pvirus->command) {
173
		case PwrCmdWrite:
174 175
			reg  = pvirus->reg;
			data = pvirus->data;
176
			cgs_write_register(hwmgr->device, reg, data);
177
			break;
178

179 180 181
		case PwrCmdEnd:
			result = 0;
			break;
182

183
		default:
184 185 186
			pr_info("Table Exit with Invalid Command!");
			smu_data->avfs.avfs_btc_status = AVFS_BTC_VIRUS_FAIL;
			result = -EINVAL;
187 188
			break;
		}
189
		pvirus++;
190
	}
191

192 193 194
	return result;
}

195
static int fiji_start_avfs_btc(struct pp_hwmgr *hwmgr)
196 197
{
	int result = 0;
198
	struct smu7_smumgr *smu_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
199

200
	if (0 != smu_data->avfs.avfs_btc_param) {
201
		if (0 != smu7_send_msg_to_smc_with_parameter(hwmgr,
202 203 204
				PPSMC_MSG_PerformBtc, smu_data->avfs.avfs_btc_param)) {
			pr_info("[AVFS][Fiji_PerformBtc] PerformBTC SMU msg failed");
			result = -EINVAL;
205 206 207 208
		}
	}
	/* Soft-Reset to reset the engine before loading uCode */
	 /* halt */
209
	cgs_write_register(hwmgr->device, mmCP_MEC_CNTL, 0x50000000);
210
	/* reset everything */
211
	cgs_write_register(hwmgr->device, mmGRBM_SOFT_RESET, 0xffffffff);
212
	/* clear reset */
213
	cgs_write_register(hwmgr->device, mmGRBM_SOFT_RESET, 0);
214

215
	return result;
216 217
}

218
static int fiji_setup_graphics_level_structure(struct pp_hwmgr *hwmgr)
219 220 221 222 223 224
{
	int32_t vr_config;
	uint32_t table_start;
	uint32_t level_addr, vr_config_addr;
	uint32_t level_size = sizeof(avfs_graphics_level);

225
	PP_ASSERT_WITH_CODE(0 == smu7_read_smc_sram_dword(hwmgr,
226 227 228 229 230 231 232 233 234 235 236 237 238 239
			SMU7_FIRMWARE_HEADER_LOCATION +
			offsetof(SMU73_Firmware_Header, DpmTable),
			&table_start, 0x40000),
			"[AVFS][Fiji_SetupGfxLvlStruct] SMU could not "
			"communicate starting address of DPM table",
			return -1;);

	/* Default value for vr_config =
	 * VR_MERGED_WITH_VDDC + VR_STATIC_VOLTAGE(VDDCI) */
	vr_config = 0x01000500;   /* Real value:0x50001 */

	vr_config_addr = table_start +
			offsetof(SMU73_Discrete_DpmTable, VRConfig);

240
	PP_ASSERT_WITH_CODE(0 == smu7_copy_bytes_to_smc(hwmgr, vr_config_addr,
241 242 243 244 245 246 247
			(uint8_t *)&vr_config, sizeof(int32_t), 0x40000),
			"[AVFS][Fiji_SetupGfxLvlStruct] Problems copying "
			"vr_config value over to SMC",
			return -1;);

	level_addr = table_start + offsetof(SMU73_Discrete_DpmTable, GraphicsLevel);

248
	PP_ASSERT_WITH_CODE(0 == smu7_copy_bytes_to_smc(hwmgr, level_addr,
249 250 251 252 253 254 255
			(uint8_t *)(&avfs_graphics_level), level_size, 0x40000),
			"[AVFS][Fiji_SetupGfxLvlStruct] Copying of DPM table failed!",
			return -1;);

	return 0;
}

256
static int fiji_avfs_event_mgr(struct pp_hwmgr *hwmgr, bool smu_started)
257
{
258
	struct smu7_smumgr *smu_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
259

260
	switch (smu_data->avfs.avfs_btc_status) {
261
	case AVFS_BTC_COMPLETED_PREVIOUSLY:
262
		break;
263

264 265 266
	case AVFS_BTC_BOOT: /*Cold Boot State - Post SMU Start*/
		if (!smu_started)
			break;
267
		smu_data->avfs.avfs_btc_status = AVFS_BTC_FAILED;
268
		PP_ASSERT_WITH_CODE(0 == fiji_setup_graphics_level_structure(hwmgr),
269 270
				"[AVFS][fiji_avfs_event_mgr] Could not Copy Graphics Level"
				" table over to SMU",
271 272
				return -EINVAL;);
		smu_data->avfs.avfs_btc_status = AVFS_BTC_VIRUS_FAIL;
273
		PP_ASSERT_WITH_CODE(0 == fiji_setup_pwr_virus(hwmgr),
274 275
				"[AVFS][fiji_avfs_event_mgr] Could not setup "
				"Pwr Virus for AVFS ",
276 277
				return -EINVAL;);
		smu_data->avfs.avfs_btc_status = AVFS_BTC_FAILED;
278
		PP_ASSERT_WITH_CODE(0 == fiji_start_avfs_btc(hwmgr),
279 280
				"[AVFS][fiji_avfs_event_mgr] Failure at "
				"fiji_start_avfs_btc. AVFS Disabled",
281
				return -EINVAL;);
282

283
		smu_data->avfs.avfs_btc_status = AVFS_BTC_ENABLEAVFS;
284 285 286
		break;
	case AVFS_BTC_DISABLED: /* Do nothing */
	case AVFS_BTC_NOTSUPPORTED: /* Do nothing */
287
	case AVFS_BTC_ENABLEAVFS:
288 289
		break;
	default:
290
		pr_err("AVFS failed status is %x !\n", smu_data->avfs.avfs_btc_status);
291 292 293 294 295
		break;
	}
	return 0;
}

296
static int fiji_start_smu(struct pp_hwmgr *hwmgr)
297 298
{
	int result = 0;
299
	struct fiji_smumgr *priv = (struct fiji_smumgr *)(hwmgr->smu_backend);
300 301

	/* Only start SMC if SMC RAM is not running */
302 303 304
	if (!(smu7_is_smc_ram_running(hwmgr)
		|| cgs_is_virtualization_enabled(hwmgr->device))) {
		fiji_avfs_event_mgr(hwmgr, false);
305 306

		/* Check if SMU is running in protected mode */
307
		if (0 == SMUM_READ_VFPF_INDIRECT_FIELD(hwmgr->device,
308 309
				CGS_IND_REG__SMC,
				SMU_FIRMWARE, SMU_MODE)) {
310
			result = fiji_start_smu_in_non_protection_mode(hwmgr);
311 312 313
			if (result)
				return result;
		} else {
314
			result = fiji_start_smu_in_protection_mode(hwmgr);
315 316 317
			if (result)
				return result;
		}
318
		fiji_avfs_event_mgr(hwmgr, true);
319 320 321
	}

	/* To initialize all clock gating before RLC loaded and running.*/
322
	cgs_set_clockgating_state(hwmgr->device,
323
			AMD_IP_BLOCK_TYPE_GFX, AMD_CG_STATE_GATE);
324
	cgs_set_clockgating_state(hwmgr->device,
325
			AMD_IP_BLOCK_TYPE_GMC, AMD_CG_STATE_GATE);
326
	cgs_set_clockgating_state(hwmgr->device,
327
			AMD_IP_BLOCK_TYPE_SDMA, AMD_CG_STATE_GATE);
328
	cgs_set_clockgating_state(hwmgr->device,
329
			AMD_IP_BLOCK_TYPE_COMMON, AMD_CG_STATE_GATE);
330 331 332 333

	/* Setup SoftRegsStart here for register lookup in case
	 * DummyBackEnd is used and ProcessFirmwareHeader is not executed
	 */
334
	smu7_read_smc_sram_dword(hwmgr,
335 336
			SMU7_FIRMWARE_HEADER_LOCATION +
			offsetof(SMU73_Firmware_Header, SoftRegisters),
337
			&(priv->smu7_data.soft_regs_start), 0x40000);
338

339
	result = smu7_request_smu_load_fw(hwmgr);
340 341 342 343

	return result;
}

344
static bool fiji_is_hw_avfs_present(struct pp_hwmgr *hwmgr)
345 346 347 348 349
{

	uint32_t efuse = 0;
	uint32_t mask = (1 << ((AVFS_EN_MSB - AVFS_EN_LSB) + 1)) - 1;

350
	if (cgs_is_virtualization_enabled(hwmgr->device))
351 352
		return 0;

353
	if (!atomctrl_read_efuse(hwmgr->device, AVFS_EN_LSB, AVFS_EN_MSB,
354 355 356 357 358 359 360 361 362 363 364 365 366 367
			mask, &efuse)) {
		if (efuse)
			return true;
	}
	return false;
}

/**
* Write a 32bit value to the SMC SRAM space.
* ALL PARAMETERS ARE IN HOST BYTE ORDER.
* @param    smumgr  the address of the powerplay hardware manager.
* @param    smc_addr the address in the SMC RAM to access.
* @param    value to write to the SMC SRAM.
*/
368
static int fiji_smu_init(struct pp_hwmgr *hwmgr)
369
{
370
	int i;
R
Rex Zhu 已提交
371 372 373 374 375 376 377
	struct fiji_smumgr *fiji_priv = NULL;

	fiji_priv = kzalloc(sizeof(struct fiji_smumgr), GFP_KERNEL);

	if (fiji_priv == NULL)
		return -ENOMEM;

378
	hwmgr->smu_backend = fiji_priv;
379

380
	if (smu7_init(hwmgr))
381
		return -EINVAL;
382

383
	for (i = 0; i < SMU73_MAX_LEVELS_GRAPHICS; i++)
R
Rex Zhu 已提交
384
		fiji_priv->activity_target[i] = 30;
385

386 387 388 389
	return 0;
}


R
Rex Zhu 已提交
390
const struct pp_smumgr_func fiji_smu_funcs = {
391
	.smu_init = &fiji_smu_init,
392
	.smu_fini = &smu7_smu_fini,
393
	.start_smu = &fiji_start_smu,
394 395 396 397 398
	.check_fw_load_finish = &smu7_check_fw_load_finish,
	.request_smu_load_fw = &smu7_reload_firmware,
	.request_smu_load_specific_fw = NULL,
	.send_msg_to_smc = &smu7_send_msg_to_smc,
	.send_msg_to_smc_with_parameter = &smu7_send_msg_to_smc_with_parameter,
399 400
	.download_pptable_settings = NULL,
	.upload_pptable_settings = NULL,
401 402 403 404 405 406
	.update_smc_table = fiji_update_smc_table,
	.get_offsetof = fiji_get_offsetof,
	.process_firmware_header = fiji_process_firmware_header,
	.init_smc_table = fiji_init_smc_table,
	.update_sclk_threshold = fiji_update_sclk_threshold,
	.thermal_setup_fan_table = fiji_thermal_setup_fan_table,
407
	.thermal_avfs_enable = fiji_thermal_avfs_enable,
408 409 410 411 412
	.populate_all_graphic_levels = fiji_populate_all_graphic_levels,
	.populate_all_memory_levels = fiji_populate_all_memory_levels,
	.get_mac_definition = fiji_get_mac_definition,
	.initialize_mc_reg_table = fiji_initialize_mc_reg_table,
	.is_dpm_running = fiji_is_dpm_running,
413
	.populate_requested_graphic_levels = fiji_populate_requested_graphic_levels,
414
	.is_hw_avfs_present = fiji_is_hw_avfs_present,
415
};