amdgpu_powerplay.c 8.0 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 26 27 28 29 30 31 32
/*
 * 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.
 *
 * Authors: AMD
 *
 */
#include "atom.h"
#include "amdgpu.h"
#include "amd_shared.h"
#include <linux/module.h>
#include <linux/moduleparam.h>
#include "amdgpu_pm.h"
#include <drm/amdgpu_drm.h>
#include "amdgpu_powerplay.h"
33
#include "si_dpm.h"
34 35 36
#include "cik_dpm.h"
#include "vi_dpm.h"

37
static int amdgpu_create_pp_handle(struct amdgpu_device *adev)
38
{
39
	struct amd_pp_init pp_init;
40
	struct amd_powerplay *amd_pp;
41
	int ret;
42 43

	amd_pp = &(adev->powerplay);
44 45
	pp_init.chip_family = adev->family;
	pp_init.chip_id = adev->asic_type;
M
Monk Liu 已提交
46
	pp_init.pm_en = (amdgpu_dpm != 0 && !amdgpu_sriov_vf(adev)) ? true : false;
47 48 49 50 51 52
	pp_init.feature_mask = amdgpu_pp_feature_mask;
	pp_init.device = amdgpu_cgs_create_device(adev);
	ret = amd_powerplay_create(&pp_init, &(amd_pp->pp_handle));
	if (ret)
		return -EINVAL;
	return 0;
53 54 55 56 57
}

static int amdgpu_pp_early_init(void *handle)
{
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
58
	struct amd_powerplay *amd_pp;
59 60
	int ret = 0;

61 62 63 64
	amd_pp = &(adev->powerplay);
	adev->pp_enabled = false;
	amd_pp->pp_handle = (void *)adev;

65
	switch (adev->asic_type) {
66 67
	case CHIP_POLARIS11:
	case CHIP_POLARIS10:
68
	case CHIP_POLARIS12:
69 70
	case CHIP_TONGA:
	case CHIP_FIJI:
71
	case CHIP_TOPAZ:
72 73
	case CHIP_CARRIZO:
	case CHIP_STONEY:
74
		adev->pp_enabled = true;
75 76 77 78
		if (amdgpu_create_pp_handle(adev))
			return -EINVAL;
		amd_pp->ip_funcs = &pp_ip_funcs;
		amd_pp->pp_funcs = &pp_dpm_funcs;
79 80
		break;
	/* These chips don't have powerplay implemenations */
81 82 83 84 85 86 87 88 89 90
#ifdef CONFIG_DRM_AMDGPU_SI
	case CHIP_TAHITI:
	case CHIP_PITCAIRN:
	case CHIP_VERDE:
	case CHIP_OLAND:
	case CHIP_HAINAN:
		amd_pp->ip_funcs = &si_dpm_ip_funcs;
	break;
#endif
#ifdef CONFIG_DRM_AMDGPU_CIK
91 92
	case CHIP_BONAIRE:
	case CHIP_HAWAII:
93 94
		amd_pp->ip_funcs = &ci_dpm_ip_funcs;
		break;
95 96 97
	case CHIP_KABINI:
	case CHIP_MULLINS:
	case CHIP_KAVERI:
98 99 100
		amd_pp->ip_funcs = &kv_dpm_ip_funcs;
		break;
#endif
101
	default:
102
		ret = -EINVAL;
103
		break;
104 105
	}

106 107 108
	if (adev->powerplay.ip_funcs->early_init)
		ret = adev->powerplay.ip_funcs->early_init(
					adev->powerplay.pp_handle);
109 110 111 112 113

	if (ret == PP_DPM_DISABLED) {
		adev->pm.dpm_enabled = false;
		return 0;
	}
114 115 116
	return ret;
}

117 118 119 120 121 122 123 124 125 126

static int amdgpu_pp_late_init(void *handle)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->late_init)
		ret = adev->powerplay.ip_funcs->late_init(
					adev->powerplay.pp_handle);

127
	if (adev->pp_enabled && adev->pm.dpm_enabled) {
128
		amdgpu_pm_sysfs_init(adev);
129 130
		amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_COMPLETE_INIT, NULL, NULL);
	}
131

132 133 134
	return ret;
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
static int amdgpu_pp_sw_init(void *handle)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->sw_init)
		ret = adev->powerplay.ip_funcs->sw_init(
					adev->powerplay.pp_handle);

	return ret;
}

static int amdgpu_pp_sw_fini(void *handle)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->sw_fini)
		ret = adev->powerplay.ip_funcs->sw_fini(
					adev->powerplay.pp_handle);
	if (ret)
		return ret;

	return ret;
}

static int amdgpu_pp_hw_init(void *handle)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

166
	if (adev->pp_enabled && adev->firmware.load_type == AMDGPU_FW_LOAD_SMU)
167 168 169 170 171 172
		amdgpu_ucode_init_bo(adev);

	if (adev->powerplay.ip_funcs->hw_init)
		ret = adev->powerplay.ip_funcs->hw_init(
					adev->powerplay.pp_handle);

173 174 175 176 177
	if (ret == PP_DPM_DISABLED) {
		adev->pm.dpm_enabled = false;
		return 0;
	}

178
	if ((amdgpu_dpm != 0) && !amdgpu_sriov_vf(adev))
179 180
		adev->pm.dpm_enabled = true;

181 182 183 184 185 186 187 188 189 190 191 192
	return ret;
}

static int amdgpu_pp_hw_fini(void *handle)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->hw_fini)
		ret = adev->powerplay.ip_funcs->hw_fini(
					adev->powerplay.pp_handle);

193
	if (adev->pp_enabled && adev->firmware.load_type == AMDGPU_FW_LOAD_SMU)
194 195 196 197 198
		amdgpu_ucode_fini_bo(adev);

	return ret;
}

199 200 201 202 203 204 205
static void amdgpu_pp_late_fini(void *handle)
{
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->late_fini)
		adev->powerplay.ip_funcs->late_fini(
			  adev->powerplay.pp_handle);
206 207 208 209 210

	if (adev->pp_enabled && adev->pm.dpm_enabled)
		amdgpu_pm_sysfs_fini(adev);

	amd_powerplay_destroy(adev->powerplay.pp_handle);
211 212
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static int amdgpu_pp_suspend(void *handle)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->suspend)
		ret = adev->powerplay.ip_funcs->suspend(
					 adev->powerplay.pp_handle);
	return ret;
}

static int amdgpu_pp_resume(void *handle)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->resume)
		ret = adev->powerplay.ip_funcs->resume(
					adev->powerplay.pp_handle);
	return ret;
}

static int amdgpu_pp_set_clockgating_state(void *handle,
					enum amd_clockgating_state state)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->set_clockgating_state)
		ret = adev->powerplay.ip_funcs->set_clockgating_state(
				adev->powerplay.pp_handle, state);
	return ret;
}

static int amdgpu_pp_set_powergating_state(void *handle,
					enum amd_powergating_state state)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->set_powergating_state)
		ret = adev->powerplay.ip_funcs->set_powergating_state(
				 adev->powerplay.pp_handle, state);
	return ret;
}


static bool amdgpu_pp_is_idle(void *handle)
{
	bool ret = true;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->is_idle)
		ret = adev->powerplay.ip_funcs->is_idle(
					adev->powerplay.pp_handle);
	return ret;
}

static int amdgpu_pp_wait_for_idle(void *handle)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->wait_for_idle)
		ret = adev->powerplay.ip_funcs->wait_for_idle(
					adev->powerplay.pp_handle);
	return ret;
}

static int amdgpu_pp_soft_reset(void *handle)
{
	int ret = 0;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	if (adev->powerplay.ip_funcs->soft_reset)
		ret = adev->powerplay.ip_funcs->soft_reset(
					adev->powerplay.pp_handle);
	return ret;
}

293
static const struct amd_ip_funcs amdgpu_pp_ip_funcs = {
294
	.name = "amdgpu_powerplay",
295
	.early_init = amdgpu_pp_early_init,
296
	.late_init = amdgpu_pp_late_init,
297 298 299 300
	.sw_init = amdgpu_pp_sw_init,
	.sw_fini = amdgpu_pp_sw_fini,
	.hw_init = amdgpu_pp_hw_init,
	.hw_fini = amdgpu_pp_hw_fini,
301
	.late_fini = amdgpu_pp_late_fini,
302 303 304 305 306 307 308 309
	.suspend = amdgpu_pp_suspend,
	.resume = amdgpu_pp_resume,
	.is_idle = amdgpu_pp_is_idle,
	.wait_for_idle = amdgpu_pp_wait_for_idle,
	.soft_reset = amdgpu_pp_soft_reset,
	.set_clockgating_state = amdgpu_pp_set_clockgating_state,
	.set_powergating_state = amdgpu_pp_set_powergating_state,
};
310 311 312 313 314 315 316 317 318

const struct amdgpu_ip_block_version amdgpu_pp_ip_block =
{
	.type = AMD_IP_BLOCK_TYPE_SMC,
	.major = 1,
	.minor = 0,
	.rev = 0,
	.funcs = &amdgpu_pp_ip_funcs,
};