smu_v12_0.c 9.2 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 24 25
/*
 * Copyright 2019 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.
 */

#include <linux/firmware.h>
#include "amdgpu.h"
#include "amdgpu_smu.h"
26
#include "smu_internal.h"
27 28 29 30 31
#include "atomfirmware.h"
#include "amdgpu_atomfirmware.h"
#include "smu_v12_0.h"
#include "soc15_common.h"
#include "atom.h"
32
#include "smu_cmn.h"
33 34 35

#include "asic_reg/mp/mp_12_0_0_offset.h"
#include "asic_reg/mp/mp_12_0_0_sh_mask.h"
36 37
#include "asic_reg/smuio/smuio_12_0_0_offset.h"
#include "asic_reg/smuio/smuio_12_0_0_sh_mask.h"
38

39 40 41 42 43 44 45 46 47 48
/*
 * DO NOT use these for err/warn/info/debug messages.
 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
 * They are more MGPU friendly.
 */
#undef pr_err
#undef pr_warn
#undef pr_info
#undef pr_debug

49 50 51 52
// because some SMU12 based ASICs use older ip offset tables
// we should undefine this register from the smuio12 header
// to prevent confusion down the road
#undef mmPWR_MISC_CNTL_STATUS
53

54
#define smnMP1_FIRMWARE_FLAGS                                0x3010024
55

56
int smu_v12_0_send_msg_without_waiting(struct smu_context *smu,
57 58 59 60 61 62 63 64
					      uint16_t msg)
{
	struct amdgpu_device *adev = smu->adev;

	WREG32_SOC15(MP1, 0, mmMP1_SMN_C2PMSG_66, msg);
	return 0;
}

65
static int smu_v12_0_read_arg(struct smu_context *smu, uint32_t *arg)
66 67 68 69 70 71 72
{
	struct amdgpu_device *adev = smu->adev;

	*arg = RREG32_SOC15(MP1, 0, mmMP1_SMN_C2PMSG_82);
	return 0;
}

73
int smu_v12_0_wait_for_response(struct smu_context *smu)
74 75 76 77 78 79 80
{
	struct amdgpu_device *adev = smu->adev;
	uint32_t cur_value, i;

	for (i = 0; i < adev->usec_timeout; i++) {
		cur_value = RREG32_SOC15(MP1, 0, mmMP1_SMN_C2PMSG_90);
		if ((cur_value & MP1_C2PMSG_90__CONTENT_MASK) != 0)
81 82
			return cur_value == 0x1 ? 0 : -EIO;

83 84 85 86
		udelay(1);
	}

	/* timeout means wrong logic */
87
	return -ETIME;
88 89
}

90
int
91 92
smu_v12_0_send_msg_with_param(struct smu_context *smu,
			      enum smu_message_type msg,
93 94
			      uint32_t param,
			      uint32_t *read_arg)
95 96 97 98
{
	struct amdgpu_device *adev = smu->adev;
	int ret = 0, index = 0;

99 100 101
	index = smu_cmn_to_asic_specific_index(smu,
					       CMN2ASIC_MAPPING_MSG,
					       msg);
102 103 104
	if (index < 0)
		return index;

105
	mutex_lock(&smu->message_lock);
106
	ret = smu_v12_0_wait_for_response(smu);
107
	if (ret) {
108
		dev_err(adev->dev, "Msg issuing pre-check failed and "
109
		       "SMU may be not in the right state!\n");
110
		goto out;
111
	}
112 113 114 115 116 117 118 119

	WREG32_SOC15(MP1, 0, mmMP1_SMN_C2PMSG_90, 0);

	WREG32_SOC15(MP1, 0, mmMP1_SMN_C2PMSG_82, param);

	smu_v12_0_send_msg_without_waiting(smu, (uint16_t)index);

	ret = smu_v12_0_wait_for_response(smu);
120
	if (ret) {
121
		dev_err(adev->dev, "Failed to send message 0x%x, response 0x%x param 0x%x\n",
122
		       index, ret, param);
123
		goto out;
124 125 126 127
	}
	if (read_arg) {
		ret = smu_v12_0_read_arg(smu, read_arg);
		if (ret) {
128
			dev_err(adev->dev, "Failed to read message arg 0x%x, response 0x%x param 0x%x\n",
129
			       index, ret, param);
130
			goto out;
131 132
		}
	}
133 134
out:
	mutex_unlock(&smu->message_lock);
135 136 137
	return ret;
}

138
int smu_v12_0_check_fw_status(struct smu_context *smu)
139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
	struct amdgpu_device *adev = smu->adev;
	uint32_t mp1_fw_flags;

	mp1_fw_flags = RREG32_PCIE(MP1_Public |
		(smnMP1_FIRMWARE_FLAGS & 0xffffffff));

	if ((mp1_fw_flags & MP1_FIRMWARE_FLAGS__INTERRUPTS_ENABLED_MASK) >>
		MP1_FIRMWARE_FLAGS__INTERRUPTS_ENABLED__SHIFT)
		return 0;

	return -EIO;
}

153
int smu_v12_0_check_fw_version(struct smu_context *smu)
154
{
155 156 157
	uint32_t if_version = 0xff, smu_version = 0xff;
	uint16_t smu_major;
	uint8_t smu_minor, smu_debug;
158 159
	int ret = 0;

160
	ret = smu_cmn_get_smc_version(smu, &if_version, &smu_version);
161
	if (ret)
162 163 164 165 166 167 168 169 170 171 172 173 174 175
		return ret;

	smu_major = (smu_version >> 16) & 0xffff;
	smu_minor = (smu_version >> 8) & 0xff;
	smu_debug = (smu_version >> 0) & 0xff;

	/*
	 * 1. if_version mismatch is not critical as our fw is designed
	 * to be backward compatible.
	 * 2. New fw usually brings some optimizations. But that's visible
	 * only on the paired driver.
	 * Considering above, we just leave user a warning message instead
	 * of halt driver loading.
	 */
176
	if (if_version != smu->smc_driver_if_version) {
177
		dev_info(smu->adev->dev, "smu driver if version = 0x%08x, smu fw if version = 0x%08x, "
178
			"smu fw version = 0x%08x (%d.%d.%d)\n",
179
			smu->smc_driver_if_version, if_version,
180
			smu_version, smu_major, smu_minor, smu_debug);
181
		dev_warn(smu->adev->dev, "SMU driver if version not matched\n");
182
	}
183 184 185 186

	return ret;
}

187
int smu_v12_0_powergate_sdma(struct smu_context *smu, bool gate)
188
{
189
	if (!smu->is_apu)
190 191 192
		return 0;

	if (gate)
193
		return smu_send_smc_msg(smu, SMU_MSG_PowerDownSdma, NULL);
194
	else
195
		return smu_send_smc_msg(smu, SMU_MSG_PowerUpSdma, NULL);
196 197
}

198
int smu_v12_0_set_gfx_cgpg(struct smu_context *smu, bool enable)
199 200 201 202 203
{
	if (!(smu->adev->pg_flags & AMD_PG_SUPPORT_GFX_PG))
		return 0;

	return smu_v12_0_send_msg_with_param(smu,
204 205 206
		SMU_MSG_SetGfxCGPG,
		enable ? 1 : 0,
		NULL);
207 208
}

209 210 211 212 213 214 215 216 217 218 219 220
/**
 * smu_v12_0_get_gfxoff_status - get gfxoff status
 *
 * @smu: amdgpu_device pointer
 *
 * This function will be used to get gfxoff status
 *
 * Returns 0=GFXOFF(default).
 * Returns 1=Transition out of GFX State.
 * Returns 2=Not in GFXOFF.
 * Returns 3=Transition into GFXOFF.
 */
221
uint32_t smu_v12_0_get_gfxoff_status(struct smu_context *smu)
222 223
{
	uint32_t reg;
224
	uint32_t gfxOff_Status = 0;
225 226
	struct amdgpu_device *adev = smu->adev;

227 228 229
	reg = RREG32_SOC15(SMUIO, 0, mmSMUIO_GFX_MISC_CNTL);
	gfxOff_Status = (reg & SMUIO_GFX_MISC_CNTL__PWR_GFXOFF_STATUS_MASK)
		>> SMUIO_GFX_MISC_CNTL__PWR_GFXOFF_STATUS__SHIFT;
230

231
	return gfxOff_Status;
232 233
}

234
int smu_v12_0_gfx_off_control(struct smu_context *smu, bool enable)
235
{
236
	int ret = 0, timeout = 500;
237 238

	if (enable) {
239
		ret = smu_send_smc_msg(smu, SMU_MSG_AllowGfxOff, NULL);
240

241
	} else {
242
		ret = smu_send_smc_msg(smu, SMU_MSG_DisallowGfxOff, NULL);
243

244 245
		/* confirm gfx is back to "on" state, timeout is 0.5 second */
		while (!(smu_v12_0_get_gfxoff_status(smu) == 2)) {
246 247 248 249 250 251 252 253 254 255 256 257
			msleep(1);
			timeout--;
			if (timeout == 0) {
				DRM_ERROR("disable gfxoff timeout and failed!\n");
				break;
			}
		}
	}

	return ret;
}

258
int smu_v12_0_fini_smc_tables(struct smu_context *smu)
259 260 261
{
	struct smu_table_context *smu_table = &smu->smu_table;

262 263
	kfree(smu_table->clocks_table);
	smu_table->clocks_table = NULL;
264 265 266 267 268 269

	kfree(smu_table->metrics_table);
	smu_table->metrics_table = NULL;

	kfree(smu_table->watermarks_table);
	smu_table->watermarks_table = NULL;
270 271 272

	return 0;
}
273

274
int smu_v12_0_set_default_dpm_tables(struct smu_context *smu)
275 276 277
{
	struct smu_table_context *smu_table = &smu->smu_table;

278
	return smu_cmn_update_table(smu, SMU_TABLE_DPMCLOCKS, 0, smu_table->clocks_table, false);
279 280
}

281
int smu_v12_0_mode2_reset(struct smu_context *smu){
282
	return smu_v12_0_send_msg_with_param(smu, SMU_MSG_GfxDeviceDriverReset, SMU_RESET_MODE_2, NULL);
283
}
284

285
int smu_v12_0_set_soft_freq_limited_range(struct smu_context *smu, enum smu_clk_type clk_type,
286 287 288 289
			    uint32_t min, uint32_t max)
{
	int ret = 0;

290
	if (!smu_cmn_clk_dpm_is_enabled(smu, clk_type))
291 292
		return 0;

293 294 295
	switch (clk_type) {
	case SMU_GFXCLK:
	case SMU_SCLK:
296
		ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinGfxClk, min, NULL);
297 298 299
		if (ret)
			return ret;

300
		ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMaxGfxClk, max, NULL);
301 302 303 304 305
		if (ret)
			return ret;
	break;
	case SMU_FCLK:
	case SMU_MCLK:
306
		ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinFclkByFreq, min, NULL);
307 308 309
		if (ret)
			return ret;

310
		ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMaxFclkByFreq, max, NULL);
311 312 313 314
		if (ret)
			return ret;
	break;
	case SMU_SOCCLK:
315
		ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinSocclkByFreq, min, NULL);
316 317 318
		if (ret)
			return ret;

319
		ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMaxSocclkByFreq, max, NULL);
320 321 322 323
		if (ret)
			return ret;
	break;
	case SMU_VCLK:
324
		ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinVcn, min, NULL);
325 326 327
		if (ret)
			return ret;

328
		ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMaxVcn, max, NULL);
329 330 331 332 333 334 335 336 337
		if (ret)
			return ret;
	break;
	default:
		return -EINVAL;
	}

	return ret;
}
338 339 340 341 342 343 344 345 346

int smu_v12_0_set_driver_table_location(struct smu_context *smu)
{
	struct smu_table *driver_table = &smu->smu_table.driver_table;
	int ret = 0;

	if (driver_table->mc_address) {
		ret = smu_send_smc_msg_with_param(smu,
				SMU_MSG_SetDriverDramAddrHigh,
347 348
				upper_32_bits(driver_table->mc_address),
				NULL);
349 350 351
		if (!ret)
			ret = smu_send_smc_msg_with_param(smu,
				SMU_MSG_SetDriverDramAddrLow,
352 353
				lower_32_bits(driver_table->mc_address),
				NULL);
354 355 356 357
	}

	return ret;
}