smu8_smumgr.c 24.8 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
/*
 * 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.
 *
 */
23 24 25

#include <linux/delay.h>
#include <linux/gfp.h>
26 27
#include <linux/kernel.h>
#include <linux/slab.h>
28 29
#include <linux/types.h>

30 31 32 33 34
#include "cgs_common.h"
#include "smu/smu_8_0_d.h"
#include "smu/smu_8_0_sh_mask.h"
#include "smu8.h"
#include "smu8_fusion.h"
35
#include "smu8_smumgr.h"
36 37 38 39 40 41 42 43
#include "cz_ppsmc.h"
#include "smu_ucode_xfer_cz.h"
#include "gca/gfx_8_0_d.h"
#include "gca/gfx_8_0_sh_mask.h"
#include "smumgr.h"

#define SIZE_ALIGN_32(x)    (((x) + 31) / 32 * 32)

44 45 46 47 48 49 50 51 52
static const enum smu8_scratch_entry firmware_list[] = {
	SMU8_SCRATCH_ENTRY_UCODE_ID_SDMA0,
	SMU8_SCRATCH_ENTRY_UCODE_ID_SDMA1,
	SMU8_SCRATCH_ENTRY_UCODE_ID_CP_CE,
	SMU8_SCRATCH_ENTRY_UCODE_ID_CP_PFP,
	SMU8_SCRATCH_ENTRY_UCODE_ID_CP_ME,
	SMU8_SCRATCH_ENTRY_UCODE_ID_CP_MEC_JT1,
	SMU8_SCRATCH_ENTRY_UCODE_ID_CP_MEC_JT2,
	SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_G,
53 54
};

55
static uint32_t smu8_get_argument(struct pp_hwmgr *hwmgr)
56
{
57
	if (hwmgr == NULL || hwmgr->device == NULL)
58
		return 0;
59

60
	return cgs_read_register(hwmgr->device,
61 62 63
					mmSMU_MP1_SRBM2P_ARG_0);
}

64
static int smu8_send_msg_to_smc_async(struct pp_hwmgr *hwmgr, uint16_t msg)
65 66 67
{
	int result = 0;

68
	if (hwmgr == NULL || hwmgr->device == NULL)
69 70
		return -EINVAL;

71
	result = PHM_WAIT_FIELD_UNEQUAL(hwmgr,
72 73
					SMU_MP1_SRBM2P_RESP_0, CONTENT, 0);
	if (result != 0) {
74
		pr_err("smu8_send_msg_to_smc_async (0x%04x) failed\n", msg);
75 76 77
		return result;
	}

78 79
	cgs_write_register(hwmgr->device, mmSMU_MP1_SRBM2P_RESP_0, 0);
	cgs_write_register(hwmgr->device, mmSMU_MP1_SRBM2P_MSG_0, msg);
80 81 82 83 84

	return 0;
}

/* Send a message to the SMC, and wait for its response.*/
85
static int smu8_send_msg_to_smc(struct pp_hwmgr *hwmgr, uint16_t msg)
86 87 88
{
	int result = 0;

89
	result = smu8_send_msg_to_smc_async(hwmgr, msg);
90 91 92
	if (result != 0)
		return result;

93
	return PHM_WAIT_FIELD_UNEQUAL(hwmgr,
94 95 96
					SMU_MP1_SRBM2P_RESP_0, CONTENT, 0);
}

97
static int smu8_set_smc_sram_address(struct pp_hwmgr *hwmgr,
98 99
				     uint32_t smc_address, uint32_t limit)
{
100
	if (hwmgr == NULL || hwmgr->device == NULL)
101 102 103
		return -EINVAL;

	if (0 != (3 & smc_address)) {
104
		pr_err("SMC address must be 4 byte aligned\n");
105
		return -EINVAL;
106 107 108
	}

	if (limit <= (smc_address + 3)) {
109
		pr_err("SMC address beyond the SMC RAM area\n");
110
		return -EINVAL;
111 112
	}

113
	cgs_write_register(hwmgr->device, mmMP0PUB_IND_INDEX_0,
114 115 116 117 118
				SMN_MP1_SRAM_START_ADDR + smc_address);

	return 0;
}

119
static int smu8_write_smc_sram_dword(struct pp_hwmgr *hwmgr,
120 121 122 123
		uint32_t smc_address, uint32_t value, uint32_t limit)
{
	int result;

124
	if (hwmgr == NULL || hwmgr->device == NULL)
125 126
		return -EINVAL;

127
	result = smu8_set_smc_sram_address(hwmgr, smc_address, limit);
128
	if (!result)
129
		cgs_write_register(hwmgr->device, mmMP0PUB_IND_DATA_0, value);
130

131
	return result;
132 133
}

134
static int smu8_send_msg_to_smc_with_parameter(struct pp_hwmgr *hwmgr,
135 136
					  uint16_t msg, uint32_t parameter)
{
137
	if (hwmgr == NULL || hwmgr->device == NULL)
138 139
		return -EINVAL;

140
	cgs_write_register(hwmgr->device, mmSMU_MP1_SRBM2P_ARG_0, parameter);
141

142
	return smu8_send_msg_to_smc(hwmgr, msg);
143 144
}

145
static int smu8_check_fw_load_finish(struct pp_hwmgr *hwmgr,
146 147 148 149 150 151 152
				   uint32_t firmware)
{
	int i;
	uint32_t index = SMN_MP1_SRAM_START_ADDR +
			 SMU8_FIRMWARE_HEADER_LOCATION +
			 offsetof(struct SMU8_Firmware_Header, UcodeLoadStatus);

153
	if (hwmgr == NULL || hwmgr->device == NULL)
154 155
		return -EINVAL;

156
	cgs_write_register(hwmgr->device, mmMP0PUB_IND_INDEX, index);
157

158
	for (i = 0; i < hwmgr->usec_timeout; i++) {
159
		if (firmware ==
160
			(cgs_read_register(hwmgr->device, mmMP0PUB_IND_DATA) & firmware))
161 162 163 164
			break;
		udelay(1);
	}

165
	if (i >= hwmgr->usec_timeout) {
166
		pr_err("SMU check loaded firmware failed.\n");
167 168 169 170 171 172
		return -EINVAL;
	}

	return 0;
}

173
static int smu8_load_mec_firmware(struct pp_hwmgr *hwmgr)
174 175 176 177 178
{
	uint32_t reg_data;
	uint32_t tmp;
	int ret = 0;
	struct cgs_firmware_info info = {0};
179
	struct smu8_smumgr *smu8_smu;
180

181
	if (hwmgr == NULL || hwmgr->device == NULL)
182 183
		return -EINVAL;

184
	smu8_smu = hwmgr->smu_backend;
185
	ret = cgs_get_firmware_info(hwmgr->device,
186 187 188 189 190 191
						CGS_UCODE_ID_CP_MEC, &info);

	if (ret)
		return -EINVAL;

	/* Disable MEC parsing/prefetching */
192
	tmp = cgs_read_register(hwmgr->device,
193
					mmCP_MEC_CNTL);
194 195
	tmp = PHM_SET_FIELD(tmp, CP_MEC_CNTL, MEC_ME1_HALT, 1);
	tmp = PHM_SET_FIELD(tmp, CP_MEC_CNTL, MEC_ME2_HALT, 1);
196
	cgs_write_register(hwmgr->device, mmCP_MEC_CNTL, tmp);
197

198
	tmp = cgs_read_register(hwmgr->device,
199 200
					mmCP_CPC_IC_BASE_CNTL);

201 202 203 204
	tmp = PHM_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, VMID, 0);
	tmp = PHM_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, ATC, 0);
	tmp = PHM_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, CACHE_POLICY, 0);
	tmp = PHM_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, MTYPE, 1);
205
	cgs_write_register(hwmgr->device, mmCP_CPC_IC_BASE_CNTL, tmp);
206

207
	reg_data = lower_32_bits(info.mc_addr) &
208
			PHM_FIELD_MASK(CP_CPC_IC_BASE_LO, IC_BASE_LO);
209
	cgs_write_register(hwmgr->device, mmCP_CPC_IC_BASE_LO, reg_data);
210

211
	reg_data = upper_32_bits(info.mc_addr) &
212
			PHM_FIELD_MASK(CP_CPC_IC_BASE_HI, IC_BASE_HI);
213
	cgs_write_register(hwmgr->device, mmCP_CPC_IC_BASE_HI, reg_data);
214 215 216 217

	return 0;
}

218 219
static uint8_t smu8_translate_firmware_enum_to_arg(struct pp_hwmgr *hwmgr,
			enum smu8_scratch_entry firmware_enum)
220 221 222 223
{
	uint8_t ret = 0;

	switch (firmware_enum) {
224
	case SMU8_SCRATCH_ENTRY_UCODE_ID_SDMA0:
225 226
		ret = UCODE_ID_SDMA0;
		break;
227
	case SMU8_SCRATCH_ENTRY_UCODE_ID_SDMA1:
228
		if (hwmgr->chip_id == CHIP_STONEY)
229 230 231
			ret = UCODE_ID_SDMA0;
		else
			ret = UCODE_ID_SDMA1;
232
		break;
233
	case SMU8_SCRATCH_ENTRY_UCODE_ID_CP_CE:
234 235
		ret = UCODE_ID_CP_CE;
		break;
236
	case SMU8_SCRATCH_ENTRY_UCODE_ID_CP_PFP:
237 238
		ret = UCODE_ID_CP_PFP;
		break;
239
	case SMU8_SCRATCH_ENTRY_UCODE_ID_CP_ME:
240 241
		ret = UCODE_ID_CP_ME;
		break;
242
	case SMU8_SCRATCH_ENTRY_UCODE_ID_CP_MEC_JT1:
243 244
		ret = UCODE_ID_CP_MEC_JT1;
		break;
245
	case SMU8_SCRATCH_ENTRY_UCODE_ID_CP_MEC_JT2:
246
		if (hwmgr->chip_id == CHIP_STONEY)
247 248 249
			ret = UCODE_ID_CP_MEC_JT1;
		else
			ret = UCODE_ID_CP_MEC_JT2;
250
		break;
251
	case SMU8_SCRATCH_ENTRY_UCODE_ID_GMCON_RENG:
252 253
		ret = UCODE_ID_GMCON_RENG;
		break;
254
	case SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_G:
255 256
		ret = UCODE_ID_RLC_G;
		break;
257
	case SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SCRATCH:
258 259
		ret = UCODE_ID_RLC_SCRATCH;
		break;
260
	case SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SRM_ARAM:
261 262
		ret = UCODE_ID_RLC_SRM_ARAM;
		break;
263
	case SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SRM_DRAM:
264 265
		ret = UCODE_ID_RLC_SRM_DRAM;
		break;
266
	case SMU8_SCRATCH_ENTRY_UCODE_ID_DMCU_ERAM:
267 268
		ret = UCODE_ID_DMCU_ERAM;
		break;
269
	case SMU8_SCRATCH_ENTRY_UCODE_ID_DMCU_IRAM:
270 271
		ret = UCODE_ID_DMCU_IRAM;
		break;
272
	case SMU8_SCRATCH_ENTRY_UCODE_ID_POWER_PROFILING:
273 274
		ret = TASK_ARG_INIT_MM_PWR_LOG;
		break;
275 276 277 278 279 280
	case SMU8_SCRATCH_ENTRY_DATA_ID_SDMA_HALT:
	case SMU8_SCRATCH_ENTRY_DATA_ID_SYS_CLOCKGATING:
	case SMU8_SCRATCH_ENTRY_DATA_ID_SDMA_RING_REGS:
	case SMU8_SCRATCH_ENTRY_DATA_ID_NONGFX_REINIT:
	case SMU8_SCRATCH_ENTRY_DATA_ID_SDMA_START:
	case SMU8_SCRATCH_ENTRY_DATA_ID_IH_REGISTERS:
281 282
		ret = TASK_ARG_REG_MMIO;
		break;
283
	case SMU8_SCRATCH_ENTRY_SMU8_FUSION_CLKTABLE:
284 285 286 287 288 289 290
		ret = TASK_ARG_INIT_CLK_TABLE;
		break;
	}

	return ret;
}

291
static enum cgs_ucode_id smu8_convert_fw_type_to_cgs(uint32_t fw_type)
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
{
	enum cgs_ucode_id result = CGS_UCODE_ID_MAXIMUM;

	switch (fw_type) {
	case UCODE_ID_SDMA0:
		result = CGS_UCODE_ID_SDMA0;
		break;
	case UCODE_ID_SDMA1:
		result = CGS_UCODE_ID_SDMA1;
		break;
	case UCODE_ID_CP_CE:
		result = CGS_UCODE_ID_CP_CE;
		break;
	case UCODE_ID_CP_PFP:
		result = CGS_UCODE_ID_CP_PFP;
		break;
	case UCODE_ID_CP_ME:
		result = CGS_UCODE_ID_CP_ME;
		break;
	case UCODE_ID_CP_MEC_JT1:
		result = CGS_UCODE_ID_CP_MEC_JT1;
		break;
	case UCODE_ID_CP_MEC_JT2:
		result = CGS_UCODE_ID_CP_MEC_JT2;
		break;
	case UCODE_ID_RLC_G:
		result = CGS_UCODE_ID_RLC_G;
		break;
	default:
		break;
	}

	return result;
}

327
static int smu8_smu_populate_single_scratch_task(
328
			struct pp_hwmgr *hwmgr,
329
			enum smu8_scratch_entry fw_enum,
330 331 332
			uint8_t type, bool is_last)
{
	uint8_t i;
333 334 335
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
	struct TOC *toc = (struct TOC *)smu8_smu->toc_buffer.kaddr;
	struct SMU_Task *task = &toc->tasks[smu8_smu->toc_entry_used_count++];
336 337

	task->type = type;
338 339
	task->arg = smu8_translate_firmware_enum_to_arg(hwmgr, fw_enum);
	task->next = is_last ? END_OF_TASK_LIST : smu8_smu->toc_entry_used_count;
340

341 342
	for (i = 0; i < smu8_smu->scratch_buffer_length; i++)
		if (smu8_smu->scratch_buffer[i].firmware_ID == fw_enum)
343 344
			break;

345
	if (i >= smu8_smu->scratch_buffer_length) {
346
		pr_err("Invalid Firmware Type\n");
347 348 349
		return -EINVAL;
	}

350 351 352
	task->addr.low = lower_32_bits(smu8_smu->scratch_buffer[i].mc_addr);
	task->addr.high = upper_32_bits(smu8_smu->scratch_buffer[i].mc_addr);
	task->size_bytes = smu8_smu->scratch_buffer[i].data_size;
353

354 355 356
	if (SMU8_SCRATCH_ENTRY_DATA_ID_IH_REGISTERS == fw_enum) {
		struct smu8_ih_meta_data *pIHReg_restore =
		     (struct smu8_ih_meta_data *)smu8_smu->scratch_buffer[i].kaddr;
357 358 359 360 361 362 363
		pIHReg_restore->command =
			METADATA_CMD_MODE0 | METADATA_PERFORM_ON_LOAD;
	}

	return 0;
}

364
static int smu8_smu_populate_single_ucode_load_task(
365
					struct pp_hwmgr *hwmgr,
366
					enum smu8_scratch_entry fw_enum,
367 368 369
					bool is_last)
{
	uint8_t i;
370 371 372
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
	struct TOC *toc = (struct TOC *)smu8_smu->toc_buffer.kaddr;
	struct SMU_Task *task = &toc->tasks[smu8_smu->toc_entry_used_count++];
373 374

	task->type = TASK_TYPE_UCODE_LOAD;
375 376
	task->arg = smu8_translate_firmware_enum_to_arg(hwmgr, fw_enum);
	task->next = is_last ? END_OF_TASK_LIST : smu8_smu->toc_entry_used_count;
377

378 379
	for (i = 0; i < smu8_smu->driver_buffer_length; i++)
		if (smu8_smu->driver_buffer[i].firmware_ID == fw_enum)
380 381
			break;

382
	if (i >= smu8_smu->driver_buffer_length) {
383
		pr_err("Invalid Firmware Type\n");
384 385 386
		return -EINVAL;
	}

387 388 389
	task->addr.low = lower_32_bits(smu8_smu->driver_buffer[i].mc_addr);
	task->addr.high = upper_32_bits(smu8_smu->driver_buffer[i].mc_addr);
	task->size_bytes = smu8_smu->driver_buffer[i].data_size;
390 391 392 393

	return 0;
}

394
static int smu8_smu_construct_toc_for_rlc_aram_save(struct pp_hwmgr *hwmgr)
395
{
396
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
397

398 399 400
	smu8_smu->toc_entry_aram = smu8_smu->toc_entry_used_count;
	smu8_smu_populate_single_scratch_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SRM_ARAM,
401 402 403 404 405
				TASK_TYPE_UCODE_SAVE, true);

	return 0;
}

406
static int smu8_smu_initialize_toc_empty_job_list(struct pp_hwmgr *hwmgr)
407 408
{
	int i;
409 410
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
	struct TOC *toc = (struct TOC *)smu8_smu->toc_buffer.kaddr;
411 412 413 414 415 416 417

	for (i = 0; i < NUM_JOBLIST_ENTRIES; i++)
		toc->JobList[i] = (uint8_t)IGNORE_JOB;

	return 0;
}

418
static int smu8_smu_construct_toc_for_vddgfx_enter(struct pp_hwmgr *hwmgr)
419
{
420 421
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
	struct TOC *toc = (struct TOC *)smu8_smu->toc_buffer.kaddr;
422

423 424 425
	toc->JobList[JOB_GFX_SAVE] = (uint8_t)smu8_smu->toc_entry_used_count;
	smu8_smu_populate_single_scratch_task(hwmgr,
				    SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SCRATCH,
426 427
				    TASK_TYPE_UCODE_SAVE, false);

428 429
	smu8_smu_populate_single_scratch_task(hwmgr,
				    SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SRM_DRAM,
430 431 432 433 434 435
				    TASK_TYPE_UCODE_SAVE, true);

	return 0;
}


436
static int smu8_smu_construct_toc_for_vddgfx_exit(struct pp_hwmgr *hwmgr)
437
{
438 439
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
	struct TOC *toc = (struct TOC *)smu8_smu->toc_buffer.kaddr;
440

441
	toc->JobList[JOB_GFX_RESTORE] = (uint8_t)smu8_smu->toc_entry_used_count;
442

443 444 445 446 447 448 449 450
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_CE, false);
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_PFP, false);
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_ME, false);
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_MEC_JT1, false);
451

452
	if (hwmgr->chip_id == CHIP_STONEY)
453 454
		smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_MEC_JT1, false);
455
	else
456 457
		smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_MEC_JT2, false);
458

459 460
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_G, false);
461 462

	/* populate scratch */
463 464
	smu8_smu_populate_single_scratch_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SCRATCH,
465 466
				TASK_TYPE_UCODE_LOAD, false);

467 468
	smu8_smu_populate_single_scratch_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SRM_ARAM,
469 470
				TASK_TYPE_UCODE_LOAD, false);

471 472
	smu8_smu_populate_single_scratch_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SRM_DRAM,
473 474 475 476 477
				TASK_TYPE_UCODE_LOAD, true);

	return 0;
}

478
static int smu8_smu_construct_toc_for_power_profiling(struct pp_hwmgr *hwmgr)
479
{
480
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
481

482
	smu8_smu->toc_entry_power_profiling_index = smu8_smu->toc_entry_used_count;
483

484 485
	smu8_smu_populate_single_scratch_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_POWER_PROFILING,
486 487 488 489
				TASK_TYPE_INITIALIZE, true);
	return 0;
}

490
static int smu8_smu_construct_toc_for_bootup(struct pp_hwmgr *hwmgr)
491
{
492
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
493

494
	smu8_smu->toc_entry_initialize_index = smu8_smu->toc_entry_used_count;
495

496 497
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_SDMA0, false);
498
	if (hwmgr->chip_id != CHIP_STONEY)
499 500 501 502 503 504 505 506 507 508
		smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_SDMA1, false);
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_CE, false);
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_PFP, false);
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_ME, false);
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_MEC_JT1, false);
509
	if (hwmgr->chip_id != CHIP_STONEY)
510 511 512 513
		smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_CP_MEC_JT2, false);
	smu8_smu_populate_single_ucode_load_task(hwmgr,
				SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_G, true);
514 515 516 517

	return 0;
}

518
static int smu8_smu_construct_toc_for_clock_table(struct pp_hwmgr *hwmgr)
519
{
520
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
521

522
	smu8_smu->toc_entry_clock_table = smu8_smu->toc_entry_used_count;
523

524 525
	smu8_smu_populate_single_scratch_task(hwmgr,
				SMU8_SCRATCH_ENTRY_SMU8_FUSION_CLKTABLE,
526 527 528 529 530
				TASK_TYPE_INITIALIZE, true);

	return 0;
}

531
static int smu8_smu_construct_toc(struct pp_hwmgr *hwmgr)
532
{
533
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
534

535 536 537 538 539 540 541 542
	smu8_smu->toc_entry_used_count = 0;
	smu8_smu_initialize_toc_empty_job_list(hwmgr);
	smu8_smu_construct_toc_for_rlc_aram_save(hwmgr);
	smu8_smu_construct_toc_for_vddgfx_enter(hwmgr);
	smu8_smu_construct_toc_for_vddgfx_exit(hwmgr);
	smu8_smu_construct_toc_for_power_profiling(hwmgr);
	smu8_smu_construct_toc_for_bootup(hwmgr);
	smu8_smu_construct_toc_for_clock_table(hwmgr);
543 544 545 546

	return 0;
}

547
static int smu8_smu_populate_firmware_entries(struct pp_hwmgr *hwmgr)
548
{
549
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
550 551 552 553 554 555
	uint32_t firmware_type;
	uint32_t i;
	int ret;
	enum cgs_ucode_id ucode_id;
	struct cgs_firmware_info info = {0};

556
	smu8_smu->driver_buffer_length = 0;
557

558
	for (i = 0; i < ARRAY_SIZE(firmware_list); i++) {
559

560
		firmware_type = smu8_translate_firmware_enum_to_arg(hwmgr,
561 562
					firmware_list[i]);

563
		ucode_id = smu8_convert_fw_type_to_cgs(firmware_type);
564

565
		ret = cgs_get_firmware_info(hwmgr->device,
566 567 568
							ucode_id, &info);

		if (ret == 0) {
569
			smu8_smu->driver_buffer[i].mc_addr = info.mc_addr;
570

571
			smu8_smu->driver_buffer[i].data_size = info.image_size;
572

573 574
			smu8_smu->driver_buffer[i].firmware_ID = firmware_list[i];
			smu8_smu->driver_buffer_length++;
575 576 577 578 579 580
		}
	}

	return 0;
}

581
static int smu8_smu_populate_single_scratch_entry(
582
				struct pp_hwmgr *hwmgr,
583
				enum smu8_scratch_entry scratch_type,
584
				uint32_t ulsize_byte,
585
				struct smu8_buffer_entry *entry)
586
{
587
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
588 589 590
	uint32_t ulsize_aligned = SIZE_ALIGN_32(ulsize_byte);

	entry->data_size = ulsize_byte;
591 592 593
	entry->kaddr = (char *) smu8_smu->smu_buffer.kaddr +
				smu8_smu->smu_buffer_used_bytes;
	entry->mc_addr = smu8_smu->smu_buffer.mc_addr + smu8_smu->smu_buffer_used_bytes;
594 595
	entry->firmware_ID = scratch_type;

596
	smu8_smu->smu_buffer_used_bytes += ulsize_aligned;
597 598 599 600

	return 0;
}

601
static int smu8_download_pptable_settings(struct pp_hwmgr *hwmgr, void **table)
602
{
603
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
604 605
	unsigned long i;

606 607 608
	for (i = 0; i < smu8_smu->scratch_buffer_length; i++) {
		if (smu8_smu->scratch_buffer[i].firmware_ID
			== SMU8_SCRATCH_ENTRY_SMU8_FUSION_CLKTABLE)
609 610 611
			break;
	}

612
	*table = (struct SMU8_Fusion_ClkTable *)smu8_smu->scratch_buffer[i].kaddr;
613

614
	smu8_send_msg_to_smc_with_parameter(hwmgr,
615
				PPSMC_MSG_SetClkTableAddrHi,
616
				upper_32_bits(smu8_smu->scratch_buffer[i].mc_addr));
617

618
	smu8_send_msg_to_smc_with_parameter(hwmgr,
619
				PPSMC_MSG_SetClkTableAddrLo,
620
				lower_32_bits(smu8_smu->scratch_buffer[i].mc_addr));
621

622 623
	smu8_send_msg_to_smc_with_parameter(hwmgr, PPSMC_MSG_ExecuteJob,
				smu8_smu->toc_entry_clock_table);
624

625
	smu8_send_msg_to_smc(hwmgr, PPSMC_MSG_ClkTableXferToDram);
626 627 628 629

	return 0;
}

630
static int smu8_upload_pptable_settings(struct pp_hwmgr *hwmgr)
631
{
632
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
633 634
	unsigned long i;

635 636 637
	for (i = 0; i < smu8_smu->scratch_buffer_length; i++) {
		if (smu8_smu->scratch_buffer[i].firmware_ID
				== SMU8_SCRATCH_ENTRY_SMU8_FUSION_CLKTABLE)
638 639 640
			break;
	}

641
	smu8_send_msg_to_smc_with_parameter(hwmgr,
642
				PPSMC_MSG_SetClkTableAddrHi,
643
				upper_32_bits(smu8_smu->scratch_buffer[i].mc_addr));
644

645
	smu8_send_msg_to_smc_with_parameter(hwmgr,
646
				PPSMC_MSG_SetClkTableAddrLo,
647
				lower_32_bits(smu8_smu->scratch_buffer[i].mc_addr));
648

649 650
	smu8_send_msg_to_smc_with_parameter(hwmgr, PPSMC_MSG_ExecuteJob,
				smu8_smu->toc_entry_clock_table);
651

652
	smu8_send_msg_to_smc(hwmgr, PPSMC_MSG_ClkTableXferToSmu);
653 654 655 656

	return 0;
}

657
static int smu8_request_smu_load_fw(struct pp_hwmgr *hwmgr)
658
{
659
	struct smu8_smumgr *smu8_smu = hwmgr->smu_backend;
660 661
	uint32_t smc_address;

662
	if (!hwmgr->reload_fw) {
663 664 665 666
		pr_info("skip reloading...\n");
		return 0;
	}

667
	smu8_smu_populate_firmware_entries(hwmgr);
668

669
	smu8_smu_construct_toc(hwmgr);
670 671 672 673

	smc_address = SMU8_FIRMWARE_HEADER_LOCATION +
		offsetof(struct SMU8_Firmware_Header, UcodeLoadStatus);

674
	smu8_write_smc_sram_dword(hwmgr, smc_address, 0, smc_address+4);
675

676
	smu8_send_msg_to_smc_with_parameter(hwmgr,
677
					PPSMC_MSG_DriverDramAddrHi,
678
					upper_32_bits(smu8_smu->toc_buffer.mc_addr));
679

680
	smu8_send_msg_to_smc_with_parameter(hwmgr,
681
					PPSMC_MSG_DriverDramAddrLo,
682
					lower_32_bits(smu8_smu->toc_buffer.mc_addr));
683

684
	smu8_send_msg_to_smc(hwmgr, PPSMC_MSG_InitJobs);
685

686
	smu8_send_msg_to_smc_with_parameter(hwmgr,
687
					PPSMC_MSG_ExecuteJob,
688 689 690
					smu8_smu->toc_entry_aram);
	smu8_send_msg_to_smc_with_parameter(hwmgr, PPSMC_MSG_ExecuteJob,
				smu8_smu->toc_entry_power_profiling_index);
691

692
	return smu8_send_msg_to_smc_with_parameter(hwmgr,
693
					PPSMC_MSG_ExecuteJob,
694
					smu8_smu->toc_entry_initialize_index);
695 696
}

697
static int smu8_start_smu(struct pp_hwmgr *hwmgr)
698 699 700
{
	int ret = 0;
	uint32_t fw_to_check = 0;
701 702
	struct amdgpu_device *adev = hwmgr->adev;

703 704 705 706 707 708 709 710 711 712
	uint32_t index = SMN_MP1_SRAM_START_ADDR +
			 SMU8_FIRMWARE_HEADER_LOCATION +
			 offsetof(struct SMU8_Firmware_Header, Version);


	if (hwmgr == NULL || hwmgr->device == NULL)
		return -EINVAL;

	cgs_write_register(hwmgr->device, mmMP0PUB_IND_INDEX, index);
	hwmgr->smu_version = cgs_read_register(hwmgr->device, mmMP0PUB_IND_DATA);
713
	adev->pm.fw_version = hwmgr->smu_version >> 8;
714 715 716 717 718 719 720 721 722 723

	fw_to_check = UCODE_ID_RLC_G_MASK |
			UCODE_ID_SDMA0_MASK |
			UCODE_ID_SDMA1_MASK |
			UCODE_ID_CP_CE_MASK |
			UCODE_ID_CP_ME_MASK |
			UCODE_ID_CP_PFP_MASK |
			UCODE_ID_CP_MEC_JT1_MASK |
			UCODE_ID_CP_MEC_JT2_MASK;

724
	if (hwmgr->chip_id == CHIP_STONEY)
725 726
		fw_to_check &= ~(UCODE_ID_SDMA1_MASK | UCODE_ID_CP_MEC_JT2_MASK);

727
	ret = smu8_request_smu_load_fw(hwmgr);
728 729 730
	if (ret)
		pr_err("SMU firmware load failed\n");

731
	smu8_check_fw_load_finish(hwmgr, fw_to_check);
732

733
	ret = smu8_load_mec_firmware(hwmgr);
734 735 736 737 738 739
	if (ret)
		pr_err("Mec Firmware load failed\n");

	return ret;
}

740
static int smu8_smu_init(struct pp_hwmgr *hwmgr)
741 742
{
	int ret = 0;
743
	struct smu8_smumgr *smu8_smu;
R
Rex Zhu 已提交
744

745 746
	smu8_smu = kzalloc(sizeof(struct smu8_smumgr), GFP_KERNEL);
	if (smu8_smu == NULL)
R
Rex Zhu 已提交
747 748
		return -ENOMEM;

749
	hwmgr->smu_backend = smu8_smu;
750

751 752
	smu8_smu->toc_buffer.data_size = 4096;
	smu8_smu->smu_buffer.data_size =
753 754 755 756 757 758
		ALIGN(UCODE_ID_RLC_SCRATCH_SIZE_BYTE, 32) +
		ALIGN(UCODE_ID_RLC_SRM_ARAM_SIZE_BYTE, 32) +
		ALIGN(UCODE_ID_RLC_SRM_DRAM_SIZE_BYTE, 32) +
		ALIGN(sizeof(struct SMU8_MultimediaPowerLogData), 32) +
		ALIGN(sizeof(struct SMU8_Fusion_ClkTable), 32);

759
	ret = amdgpu_bo_create_kernel((struct amdgpu_device *)hwmgr->adev,
760
				smu8_smu->toc_buffer.data_size,
761
				PAGE_SIZE,
762
				AMDGPU_GEM_DOMAIN_VRAM,
763 764 765
				&smu8_smu->toc_buffer.handle,
				&smu8_smu->toc_buffer.mc_addr,
				&smu8_smu->toc_buffer.kaddr);
766
	if (ret)
767
		goto err2;
768

769
	ret = amdgpu_bo_create_kernel((struct amdgpu_device *)hwmgr->adev,
770
				smu8_smu->smu_buffer.data_size,
771
				PAGE_SIZE,
772
				AMDGPU_GEM_DOMAIN_VRAM,
773 774 775
				&smu8_smu->smu_buffer.handle,
				&smu8_smu->smu_buffer.mc_addr,
				&smu8_smu->smu_buffer.kaddr);
776 777
	if (ret)
		goto err1;
778

779 780
	if (0 != smu8_smu_populate_single_scratch_entry(hwmgr,
		SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SCRATCH,
781
		UCODE_ID_RLC_SCRATCH_SIZE_BYTE,
782
		&smu8_smu->scratch_buffer[smu8_smu->scratch_buffer_length++])) {
783
		pr_err("Error when Populate Firmware Entry.\n");
784
		goto err0;
785 786
	}

787 788
	if (0 != smu8_smu_populate_single_scratch_entry(hwmgr,
		SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SRM_ARAM,
789
		UCODE_ID_RLC_SRM_ARAM_SIZE_BYTE,
790
		&smu8_smu->scratch_buffer[smu8_smu->scratch_buffer_length++])) {
791
		pr_err("Error when Populate Firmware Entry.\n");
792
		goto err0;
793
	}
794 795
	if (0 != smu8_smu_populate_single_scratch_entry(hwmgr,
		SMU8_SCRATCH_ENTRY_UCODE_ID_RLC_SRM_DRAM,
796
		UCODE_ID_RLC_SRM_DRAM_SIZE_BYTE,
797
		&smu8_smu->scratch_buffer[smu8_smu->scratch_buffer_length++])) {
798
		pr_err("Error when Populate Firmware Entry.\n");
799
		goto err0;
800 801
	}

802 803
	if (0 != smu8_smu_populate_single_scratch_entry(hwmgr,
		SMU8_SCRATCH_ENTRY_UCODE_ID_POWER_PROFILING,
804
		sizeof(struct SMU8_MultimediaPowerLogData),
805
		&smu8_smu->scratch_buffer[smu8_smu->scratch_buffer_length++])) {
806
		pr_err("Error when Populate Firmware Entry.\n");
807
		goto err0;
808 809
	}

810 811
	if (0 != smu8_smu_populate_single_scratch_entry(hwmgr,
		SMU8_SCRATCH_ENTRY_SMU8_FUSION_CLKTABLE,
812
		sizeof(struct SMU8_Fusion_ClkTable),
813
		&smu8_smu->scratch_buffer[smu8_smu->scratch_buffer_length++])) {
814
		pr_err("Error when Populate Firmware Entry.\n");
815
		goto err0;
816 817 818
	}

	return 0;
819 820

err0:
821 822 823
	amdgpu_bo_free_kernel(&smu8_smu->smu_buffer.handle,
				&smu8_smu->smu_buffer.mc_addr,
				&smu8_smu->smu_buffer.kaddr);
824
err1:
825 826 827
	amdgpu_bo_free_kernel(&smu8_smu->toc_buffer.handle,
				&smu8_smu->toc_buffer.mc_addr,
				&smu8_smu->toc_buffer.kaddr);
828
err2:
829
	kfree(smu8_smu);
830
	return -EINVAL;
831 832
}

833
static int smu8_smu_fini(struct pp_hwmgr *hwmgr)
834
{
835
	struct smu8_smumgr *smu8_smu;
836

837
	if (hwmgr == NULL || hwmgr->device == NULL)
838 839
		return -EINVAL;

840 841 842 843 844 845 846 847 848
	smu8_smu = hwmgr->smu_backend;
	if (smu8_smu) {
		amdgpu_bo_free_kernel(&smu8_smu->toc_buffer.handle,
					&smu8_smu->toc_buffer.mc_addr,
					&smu8_smu->toc_buffer.kaddr);
		amdgpu_bo_free_kernel(&smu8_smu->smu_buffer.handle,
					&smu8_smu->smu_buffer.mc_addr,
					&smu8_smu->smu_buffer.kaddr);
		kfree(smu8_smu);
849 850 851 852 853
	}

	return 0;
}

854
static bool smu8_dpm_check_smu_features(struct pp_hwmgr *hwmgr,
855 856 857 858 859
				unsigned long check_feature)
{
	int result;
	unsigned long features;

860
	result = smu8_send_msg_to_smc_with_parameter(hwmgr, PPSMC_MSG_GetFeatureStatus, 0);
861 862 863 864 865 866 867 868 869
	if (result == 0) {
		features = smum_get_argument(hwmgr);
		if (features & check_feature)
			return true;
	}

	return false;
}

870
static bool smu8_is_dpm_running(struct pp_hwmgr *hwmgr)
871
{
872
	if (smu8_dpm_check_smu_features(hwmgr, SMU_EnabledFeatureScoreboard_SclkDpmOn))
873 874 875 876
		return true;
	return false;
}

877 878 879 880 881
const struct pp_smumgr_func smu8_smu_funcs = {
	.smu_init = smu8_smu_init,
	.smu_fini = smu8_smu_fini,
	.start_smu = smu8_start_smu,
	.check_fw_load_finish = smu8_check_fw_load_finish,
882 883
	.request_smu_load_fw = NULL,
	.request_smu_load_specific_fw = NULL,
884
	.get_argument = smu8_get_argument,
885 886 887 888 889
	.send_msg_to_smc = smu8_send_msg_to_smc,
	.send_msg_to_smc_with_parameter = smu8_send_msg_to_smc_with_parameter,
	.download_pptable_settings = smu8_download_pptable_settings,
	.upload_pptable_settings = smu8_upload_pptable_settings,
	.is_dpm_running = smu8_is_dpm_running,
890 891
};