Linux Audio

Check our new training course

Loading...
v6.8
   1/*
   2 * Copyright 2016 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/firmware.h>
  24#include <linux/slab.h>
  25#include <linux/module.h>
  26#include <linux/pci.h>
  27
  28#include <drm/amdgpu_drm.h>
  29
  30#include "amdgpu.h"
  31#include "amdgpu_atombios.h"
  32#include "amdgpu_ih.h"
  33#include "amdgpu_uvd.h"
  34#include "amdgpu_vce.h"
  35#include "amdgpu_ucode.h"
  36#include "amdgpu_psp.h"
  37#include "atom.h"
  38#include "amd_pcie.h"
  39
  40#include "uvd/uvd_7_0_offset.h"
  41#include "gc/gc_9_0_offset.h"
  42#include "gc/gc_9_0_sh_mask.h"
  43#include "sdma0/sdma0_4_0_offset.h"
  44#include "sdma1/sdma1_4_0_offset.h"
  45#include "nbio/nbio_7_0_default.h"
  46#include "nbio/nbio_7_0_offset.h"
  47#include "nbio/nbio_7_0_sh_mask.h"
  48#include "nbio/nbio_7_0_smn.h"
  49#include "mp/mp_9_0_offset.h"
  50
  51#include "soc15.h"
  52#include "soc15_common.h"
  53#include "gfx_v9_0.h"
  54#include "gmc_v9_0.h"
  55#include "gfxhub_v1_0.h"
  56#include "mmhub_v1_0.h"
  57#include "df_v1_7.h"
  58#include "df_v3_6.h"
  59#include "nbio_v6_1.h"
  60#include "nbio_v7_0.h"
  61#include "nbio_v7_4.h"
  62#include "hdp_v4_0.h"
  63#include "vega10_ih.h"
  64#include "vega20_ih.h"
  65#include "navi10_ih.h"
  66#include "sdma_v4_0.h"
  67#include "uvd_v7_0.h"
  68#include "vce_v4_0.h"
  69#include "vcn_v1_0.h"
  70#include "vcn_v2_0.h"
  71#include "jpeg_v2_0.h"
  72#include "vcn_v2_5.h"
  73#include "jpeg_v2_5.h"
  74#include "smuio_v9_0.h"
  75#include "smuio_v11_0.h"
  76#include "smuio_v13_0.h"
  77#include "amdgpu_vkms.h"
  78#include "mxgpu_ai.h"
  79#include "amdgpu_ras.h"
  80#include "amdgpu_xgmi.h"
  81#include <uapi/linux/kfd_ioctl.h>
  82
  83#define mmMP0_MISC_CGTT_CTRL0                                                                   0x01b9
  84#define mmMP0_MISC_CGTT_CTRL0_BASE_IDX                                                          0
  85#define mmMP0_MISC_LIGHT_SLEEP_CTRL                                                             0x01ba
  86#define mmMP0_MISC_LIGHT_SLEEP_CTRL_BASE_IDX                                                    0
  87
  88static const struct amd_ip_funcs soc15_common_ip_funcs;
  89
  90/* Vega, Raven, Arcturus */
  91static const struct amdgpu_video_codec_info vega_video_codecs_encode_array[] =
  92{
  93	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 2304, 0)},
  94	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 4096, 2304, 0)},
  95};
  96
  97static const struct amdgpu_video_codecs vega_video_codecs_encode =
  98{
  99	.codec_count = ARRAY_SIZE(vega_video_codecs_encode_array),
 100	.codec_array = vega_video_codecs_encode_array,
 101};
 102
 103/* Vega */
 104static const struct amdgpu_video_codec_info vega_video_codecs_decode_array[] =
 105{
 106	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2, 4096, 4096, 3)},
 107	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4, 4096, 4096, 5)},
 108	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)},
 109	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1, 4096, 4096, 4)},
 110	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 4096, 4096, 186)},
 111	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
 112};
 113
 114static const struct amdgpu_video_codecs vega_video_codecs_decode =
 115{
 116	.codec_count = ARRAY_SIZE(vega_video_codecs_decode_array),
 117	.codec_array = vega_video_codecs_decode_array,
 118};
 119
 120/* Raven */
 121static const struct amdgpu_video_codec_info rv_video_codecs_decode_array[] =
 122{
 123	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2, 4096, 4096, 3)},
 124	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4, 4096, 4096, 5)},
 125	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)},
 126	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1, 4096, 4096, 4)},
 127	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 4096, 4096, 186)},
 128	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
 129	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 4096, 4096, 0)},
 130};
 131
 132static const struct amdgpu_video_codecs rv_video_codecs_decode =
 133{
 134	.codec_count = ARRAY_SIZE(rv_video_codecs_decode_array),
 135	.codec_array = rv_video_codecs_decode_array,
 136};
 137
 138/* Renoir, Arcturus */
 139static const struct amdgpu_video_codec_info rn_video_codecs_decode_array[] =
 140{
 141	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2, 4096, 4096, 3)},
 142	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4, 4096, 4096, 5)},
 143	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)},
 144	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1, 4096, 4096, 4)},
 145	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)},
 146	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
 147	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)},
 148};
 149
 150static const struct amdgpu_video_codecs rn_video_codecs_decode =
 151{
 152	.codec_count = ARRAY_SIZE(rn_video_codecs_decode_array),
 153	.codec_array = rn_video_codecs_decode_array,
 154};
 155
 156static const struct amdgpu_video_codec_info vcn_4_0_3_video_codecs_decode_array[] = {
 157	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)},
 158	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)},
 159	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
 160	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)},
 161	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1, 8192, 4352, 0)},
 162};
 163
 164static const struct amdgpu_video_codecs vcn_4_0_3_video_codecs_decode = {
 165	.codec_count = ARRAY_SIZE(vcn_4_0_3_video_codecs_decode_array),
 166	.codec_array = vcn_4_0_3_video_codecs_decode_array,
 167};
 168
 169static const struct amdgpu_video_codecs vcn_4_0_3_video_codecs_encode = {
 170	.codec_count = 0,
 171	.codec_array = NULL,
 172};
 173
 174static int soc15_query_video_codecs(struct amdgpu_device *adev, bool encode,
 175				    const struct amdgpu_video_codecs **codecs)
 176{
 177	if (amdgpu_ip_version(adev, VCE_HWIP, 0)) {
 178		switch (amdgpu_ip_version(adev, VCE_HWIP, 0)) {
 179		case IP_VERSION(4, 0, 0):
 180		case IP_VERSION(4, 1, 0):
 181			if (encode)
 182				*codecs = &vega_video_codecs_encode;
 183			else
 184				*codecs = &vega_video_codecs_decode;
 185			return 0;
 186		default:
 187			return -EINVAL;
 188		}
 189	} else {
 190		switch (amdgpu_ip_version(adev, UVD_HWIP, 0)) {
 191		case IP_VERSION(1, 0, 0):
 192		case IP_VERSION(1, 0, 1):
 193			if (encode)
 194				*codecs = &vega_video_codecs_encode;
 195			else
 196				*codecs = &rv_video_codecs_decode;
 197			return 0;
 198		case IP_VERSION(2, 5, 0):
 199		case IP_VERSION(2, 6, 0):
 200		case IP_VERSION(2, 2, 0):
 201			if (encode)
 202				*codecs = &vega_video_codecs_encode;
 203			else
 204				*codecs = &rn_video_codecs_decode;
 205			return 0;
 206		case IP_VERSION(4, 0, 3):
 207			if (encode)
 208				*codecs = &vcn_4_0_3_video_codecs_encode;
 209			else
 210				*codecs = &vcn_4_0_3_video_codecs_decode;
 211			return 0;
 212		default:
 213			return -EINVAL;
 214		}
 215	}
 216}
 217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 218static u32 soc15_uvd_ctx_rreg(struct amdgpu_device *adev, u32 reg)
 219{
 220	unsigned long flags, address, data;
 221	u32 r;
 222
 223	address = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_INDEX);
 224	data = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_DATA);
 225
 226	spin_lock_irqsave(&adev->uvd_ctx_idx_lock, flags);
 227	WREG32(address, ((reg) & 0x1ff));
 228	r = RREG32(data);
 229	spin_unlock_irqrestore(&adev->uvd_ctx_idx_lock, flags);
 230	return r;
 231}
 232
 233static void soc15_uvd_ctx_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 234{
 235	unsigned long flags, address, data;
 236
 237	address = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_INDEX);
 238	data = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_DATA);
 239
 240	spin_lock_irqsave(&adev->uvd_ctx_idx_lock, flags);
 241	WREG32(address, ((reg) & 0x1ff));
 242	WREG32(data, (v));
 243	spin_unlock_irqrestore(&adev->uvd_ctx_idx_lock, flags);
 244}
 245
 246static u32 soc15_didt_rreg(struct amdgpu_device *adev, u32 reg)
 247{
 248	unsigned long flags, address, data;
 249	u32 r;
 250
 251	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
 252	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
 253
 254	spin_lock_irqsave(&adev->didt_idx_lock, flags);
 255	WREG32(address, (reg));
 256	r = RREG32(data);
 257	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
 258	return r;
 259}
 260
 261static void soc15_didt_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 262{
 263	unsigned long flags, address, data;
 264
 265	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
 266	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
 267
 268	spin_lock_irqsave(&adev->didt_idx_lock, flags);
 269	WREG32(address, (reg));
 270	WREG32(data, (v));
 271	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
 272}
 273
 274static u32 soc15_gc_cac_rreg(struct amdgpu_device *adev, u32 reg)
 275{
 276	unsigned long flags;
 277	u32 r;
 278
 279	spin_lock_irqsave(&adev->gc_cac_idx_lock, flags);
 280	WREG32_SOC15(GC, 0, mmGC_CAC_IND_INDEX, (reg));
 281	r = RREG32_SOC15(GC, 0, mmGC_CAC_IND_DATA);
 282	spin_unlock_irqrestore(&adev->gc_cac_idx_lock, flags);
 283	return r;
 284}
 285
 286static void soc15_gc_cac_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 287{
 288	unsigned long flags;
 289
 290	spin_lock_irqsave(&adev->gc_cac_idx_lock, flags);
 291	WREG32_SOC15(GC, 0, mmGC_CAC_IND_INDEX, (reg));
 292	WREG32_SOC15(GC, 0, mmGC_CAC_IND_DATA, (v));
 293	spin_unlock_irqrestore(&adev->gc_cac_idx_lock, flags);
 294}
 295
 296static u32 soc15_se_cac_rreg(struct amdgpu_device *adev, u32 reg)
 297{
 298	unsigned long flags;
 299	u32 r;
 300
 301	spin_lock_irqsave(&adev->se_cac_idx_lock, flags);
 302	WREG32_SOC15(GC, 0, mmSE_CAC_IND_INDEX, (reg));
 303	r = RREG32_SOC15(GC, 0, mmSE_CAC_IND_DATA);
 304	spin_unlock_irqrestore(&adev->se_cac_idx_lock, flags);
 305	return r;
 306}
 307
 308static void soc15_se_cac_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 309{
 310	unsigned long flags;
 311
 312	spin_lock_irqsave(&adev->se_cac_idx_lock, flags);
 313	WREG32_SOC15(GC, 0, mmSE_CAC_IND_INDEX, (reg));
 314	WREG32_SOC15(GC, 0, mmSE_CAC_IND_DATA, (v));
 315	spin_unlock_irqrestore(&adev->se_cac_idx_lock, flags);
 316}
 317
 318static u32 soc15_get_config_memsize(struct amdgpu_device *adev)
 319{
 320	return adev->nbio.funcs->get_memsize(adev);
 321}
 322
 323static u32 soc15_get_xclk(struct amdgpu_device *adev)
 324{
 325	u32 reference_clock = adev->clock.spll.reference_freq;
 326
 327	if (amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(12, 0, 0) ||
 328	    amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(12, 0, 1) ||
 329	    amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 6))
 330		return 10000;
 331	if (amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(10, 0, 0) ||
 332	    amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(10, 0, 1))
 333		return reference_clock / 4;
 334
 335	return reference_clock;
 336}
 337
 338
 339void soc15_grbm_select(struct amdgpu_device *adev,
 340		     u32 me, u32 pipe, u32 queue, u32 vmid, int xcc_id)
 341{
 342	u32 grbm_gfx_cntl = 0;
 343	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, PIPEID, pipe);
 344	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, MEID, me);
 345	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, VMID, vmid);
 346	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, QUEUEID, queue);
 347
 348	WREG32_SOC15_RLC_SHADOW(GC, xcc_id, mmGRBM_GFX_CNTL, grbm_gfx_cntl);
 
 
 
 
 
 349}
 350
 351static bool soc15_read_disabled_bios(struct amdgpu_device *adev)
 352{
 353	/* todo */
 354	return false;
 355}
 356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 357static struct soc15_allowed_register_entry soc15_allowed_read_registers[] = {
 358	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS)},
 359	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS2)},
 360	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE0)},
 361	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE1)},
 362	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE2)},
 363	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE3)},
 364	{ SOC15_REG_ENTRY(SDMA0, 0, mmSDMA0_STATUS_REG)},
 365	{ SOC15_REG_ENTRY(SDMA1, 0, mmSDMA1_STATUS_REG)},
 366	{ SOC15_REG_ENTRY(GC, 0, mmCP_STAT)},
 367	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT1)},
 368	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT2)},
 369	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT3)},
 370	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_BUSY_STAT)},
 371	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STALLED_STAT1)},
 372	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STATUS)},
 373	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_BUSY_STAT)},
 374	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STALLED_STAT1)},
 375	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STATUS)},
 376	{ SOC15_REG_ENTRY(GC, 0, mmGB_ADDR_CONFIG)},
 377	{ SOC15_REG_ENTRY(GC, 0, mmDB_DEBUG2)},
 378};
 379
 380static uint32_t soc15_read_indexed_register(struct amdgpu_device *adev, u32 se_num,
 381					 u32 sh_num, u32 reg_offset)
 382{
 383	uint32_t val;
 384
 385	mutex_lock(&adev->grbm_idx_mutex);
 386	if (se_num != 0xffffffff || sh_num != 0xffffffff)
 387		amdgpu_gfx_select_se_sh(adev, se_num, sh_num, 0xffffffff, 0);
 388
 389	val = RREG32(reg_offset);
 390
 391	if (se_num != 0xffffffff || sh_num != 0xffffffff)
 392		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0);
 393	mutex_unlock(&adev->grbm_idx_mutex);
 394	return val;
 395}
 396
 397static uint32_t soc15_get_register_value(struct amdgpu_device *adev,
 398					 bool indexed, u32 se_num,
 399					 u32 sh_num, u32 reg_offset)
 400{
 401	if (indexed) {
 402		return soc15_read_indexed_register(adev, se_num, sh_num, reg_offset);
 403	} else {
 404		if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmGB_ADDR_CONFIG))
 405			return adev->gfx.config.gb_addr_config;
 406		else if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmDB_DEBUG2))
 407			return adev->gfx.config.db_debug2;
 408		return RREG32(reg_offset);
 409	}
 410}
 411
 412static int soc15_read_register(struct amdgpu_device *adev, u32 se_num,
 413			    u32 sh_num, u32 reg_offset, u32 *value)
 414{
 415	uint32_t i;
 416	struct soc15_allowed_register_entry  *en;
 417
 418	*value = 0;
 419	for (i = 0; i < ARRAY_SIZE(soc15_allowed_read_registers); i++) {
 420		en = &soc15_allowed_read_registers[i];
 421		if (!adev->reg_offset[en->hwip][en->inst])
 422			continue;
 423		else if (reg_offset != (adev->reg_offset[en->hwip][en->inst][en->seg]
 424					+ en->reg_offset))
 425			continue;
 426
 427		*value = soc15_get_register_value(adev,
 428						  soc15_allowed_read_registers[i].grbm_indexed,
 429						  se_num, sh_num, reg_offset);
 430		return 0;
 431	}
 432	return -EINVAL;
 433}
 434
 435
 436/**
 437 * soc15_program_register_sequence - program an array of registers.
 438 *
 439 * @adev: amdgpu_device pointer
 440 * @regs: pointer to the register array
 441 * @array_size: size of the register array
 442 *
 443 * Programs an array or registers with and and or masks.
 444 * This is a helper for setting golden registers.
 445 */
 446
 447void soc15_program_register_sequence(struct amdgpu_device *adev,
 448					     const struct soc15_reg_golden *regs,
 449					     const u32 array_size)
 450{
 451	const struct soc15_reg_golden *entry;
 452	u32 tmp, reg;
 453	int i;
 454
 455	for (i = 0; i < array_size; ++i) {
 456		entry = &regs[i];
 457		reg =  adev->reg_offset[entry->hwip][entry->instance][entry->segment] + entry->reg;
 458
 459		if (entry->and_mask == 0xffffffff) {
 460			tmp = entry->or_mask;
 461		} else {
 462			tmp = (entry->hwip == GC_HWIP) ?
 463				RREG32_SOC15_IP(GC, reg) : RREG32(reg);
 464
 465			tmp &= ~(entry->and_mask);
 466			tmp |= (entry->or_mask & entry->and_mask);
 467		}
 468
 469		if (reg == SOC15_REG_OFFSET(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_3) ||
 470			reg == SOC15_REG_OFFSET(GC, 0, mmPA_SC_ENHANCE) ||
 471			reg == SOC15_REG_OFFSET(GC, 0, mmPA_SC_ENHANCE_1) ||
 472			reg == SOC15_REG_OFFSET(GC, 0, mmSH_MEM_CONFIG))
 473			WREG32_RLC(reg, tmp);
 474		else
 475			(entry->hwip == GC_HWIP) ?
 476				WREG32_SOC15_IP(GC, reg, tmp) : WREG32(reg, tmp);
 477
 478	}
 479
 480}
 481
 482static int soc15_asic_baco_reset(struct amdgpu_device *adev)
 483{
 484	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
 485	int ret = 0;
 486
 487	/* avoid NBIF got stuck when do RAS recovery in BACO reset */
 488	if (ras && adev->ras_enabled)
 489		adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
 490
 491	ret = amdgpu_dpm_baco_reset(adev);
 492	if (ret)
 493		return ret;
 494
 495	/* re-enable doorbell interrupt after BACO exit */
 496	if (ras && adev->ras_enabled)
 497		adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
 498
 499	return 0;
 500}
 501
 502static enum amd_reset_method
 503soc15_asic_reset_method(struct amdgpu_device *adev)
 504{
 505	bool baco_reset = false;
 506	bool connected_to_cpu = false;
 507	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
 508
 509        if (adev->gmc.xgmi.supported && adev->gmc.xgmi.connected_to_cpu)
 510                connected_to_cpu = true;
 511
 512	if (amdgpu_reset_method == AMD_RESET_METHOD_MODE1 ||
 513	    amdgpu_reset_method == AMD_RESET_METHOD_MODE2 ||
 514	    amdgpu_reset_method == AMD_RESET_METHOD_BACO ||
 515	    amdgpu_reset_method == AMD_RESET_METHOD_PCI) {
 516		/* If connected to cpu, driver only support mode2 */
 517                if (connected_to_cpu)
 518                        return AMD_RESET_METHOD_MODE2;
 519                return amdgpu_reset_method;
 520        }
 521
 522	if (amdgpu_reset_method != -1)
 523		dev_warn(adev->dev, "Specified reset method:%d isn't supported, using AUTO instead.\n",
 524				  amdgpu_reset_method);
 525
 526	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
 527	case IP_VERSION(10, 0, 0):
 528	case IP_VERSION(10, 0, 1):
 529	case IP_VERSION(12, 0, 0):
 530	case IP_VERSION(12, 0, 1):
 531		return AMD_RESET_METHOD_MODE2;
 532	case IP_VERSION(9, 0, 0):
 533	case IP_VERSION(11, 0, 2):
 534		if (adev->asic_type == CHIP_VEGA20) {
 535			if (adev->psp.sos.fw_version >= 0x80067)
 536				baco_reset = amdgpu_dpm_is_baco_supported(adev);
 537			/*
 538			 * 1. PMFW version > 0x284300: all cases use baco
 539			 * 2. PMFW version <= 0x284300: only sGPU w/o RAS use baco
 540			 */
 541			if (ras && adev->ras_enabled &&
 542			    adev->pm.fw_version <= 0x283400)
 543				baco_reset = false;
 544		} else {
 545			baco_reset = amdgpu_dpm_is_baco_supported(adev);
 546		}
 
 
 
 
 
 
 
 547		break;
 548	case IP_VERSION(13, 0, 2):
 549		 /*
 550		 * 1.connected to cpu: driver issue mode2 reset
 551		 * 2.discret gpu: driver issue mode1 reset
 552		 */
 553		if (connected_to_cpu)
 554			return AMD_RESET_METHOD_MODE2;
 555		break;
 556	case IP_VERSION(13, 0, 6):
 557		/* Use gpu_recovery param to target a reset method.
 558		 * Enable triggering of GPU reset only if specified
 559		 * by module parameter.
 560		 */
 561		if (amdgpu_gpu_recovery == 4 || amdgpu_gpu_recovery == 5)
 562			return AMD_RESET_METHOD_MODE2;
 563		else if (!(adev->flags & AMD_IS_APU))
 564			return AMD_RESET_METHOD_MODE1;
 565		else
 566			return AMD_RESET_METHOD_MODE2;
 567	default:
 568		break;
 569	}
 570
 571	if (baco_reset)
 572		return AMD_RESET_METHOD_BACO;
 573	else
 574		return AMD_RESET_METHOD_MODE1;
 575}
 576
 577static bool soc15_need_reset_on_resume(struct amdgpu_device *adev)
 578{
 579	u32 sol_reg;
 580
 581	sol_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_81);
 582
 583	/* Will reset for the following suspend abort cases.
 584	 * 1) Only reset limit on APU side, dGPU hasn't checked yet.
 585	 * 2) S3 suspend abort and TOS already launched.
 586	 */
 587	if (adev->flags & AMD_IS_APU && adev->in_s3 &&
 588			!adev->suspend_complete &&
 589			sol_reg)
 590		return true;
 591
 592	return false;
 593}
 594
 595static int soc15_asic_reset(struct amdgpu_device *adev)
 596{
 597	/* original raven doesn't have full asic reset */
 598	/* On the latest Raven, the GPU reset can be performed
 599	 * successfully. So now, temporarily enable it for the
 600	 * S3 suspend abort case.
 601	 */
 602	if (((adev->apu_flags & AMD_APU_IS_RAVEN) ||
 603	    (adev->apu_flags & AMD_APU_IS_RAVEN2)) &&
 604		!soc15_need_reset_on_resume(adev))
 605		return 0;
 606
 607	switch (soc15_asic_reset_method(adev)) {
 608	case AMD_RESET_METHOD_PCI:
 609		dev_info(adev->dev, "PCI reset\n");
 610		return amdgpu_device_pci_reset(adev);
 611	case AMD_RESET_METHOD_BACO:
 612		dev_info(adev->dev, "BACO reset\n");
 613		return soc15_asic_baco_reset(adev);
 614	case AMD_RESET_METHOD_MODE2:
 615		dev_info(adev->dev, "MODE2 reset\n");
 616		return amdgpu_dpm_mode2_reset(adev);
 617	default:
 618		dev_info(adev->dev, "MODE1 reset\n");
 619		return amdgpu_device_mode1_reset(adev);
 620	}
 621}
 622
 623static bool soc15_supports_baco(struct amdgpu_device *adev)
 624{
 625	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
 626	case IP_VERSION(9, 0, 0):
 627	case IP_VERSION(11, 0, 2):
 628		if (adev->asic_type == CHIP_VEGA20) {
 629			if (adev->psp.sos.fw_version >= 0x80067)
 630				return amdgpu_dpm_is_baco_supported(adev);
 631			return false;
 632		} else {
 633			return amdgpu_dpm_is_baco_supported(adev);
 634		}
 635		break;
 636	default:
 637		return false;
 638	}
 639}
 640
 641/*static int soc15_set_uvd_clock(struct amdgpu_device *adev, u32 clock,
 642			u32 cntl_reg, u32 status_reg)
 643{
 644	return 0;
 645}*/
 646
 647static int soc15_set_uvd_clocks(struct amdgpu_device *adev, u32 vclk, u32 dclk)
 648{
 649	/*int r;
 650
 651	r = soc15_set_uvd_clock(adev, vclk, ixCG_VCLK_CNTL, ixCG_VCLK_STATUS);
 652	if (r)
 653		return r;
 654
 655	r = soc15_set_uvd_clock(adev, dclk, ixCG_DCLK_CNTL, ixCG_DCLK_STATUS);
 656	*/
 657	return 0;
 658}
 659
 660static int soc15_set_vce_clocks(struct amdgpu_device *adev, u32 evclk, u32 ecclk)
 661{
 662	/* todo */
 663
 664	return 0;
 665}
 666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 667static void soc15_program_aspm(struct amdgpu_device *adev)
 668{
 669	if (!amdgpu_device_should_use_aspm(adev))
 670		return;
 671
 672	if (adev->nbio.funcs->program_aspm)
 
 673		adev->nbio.funcs->program_aspm(adev);
 674}
 675
 676const struct amdgpu_ip_block_version vega10_common_ip_block =
 
 
 
 
 
 
 
 677{
 678	.type = AMD_IP_BLOCK_TYPE_COMMON,
 679	.major = 2,
 680	.minor = 0,
 681	.rev = 0,
 682	.funcs = &soc15_common_ip_funcs,
 683};
 684
 
 
 
 
 
 685static void soc15_reg_base_init(struct amdgpu_device *adev)
 686{
 
 
 687	/* Set IP register base before any HW register access */
 688	switch (adev->asic_type) {
 689	case CHIP_VEGA10:
 690	case CHIP_VEGA12:
 691	case CHIP_RAVEN:
 
 
 692	case CHIP_RENOIR:
 
 
 
 
 
 
 
 
 
 693		vega10_reg_base_init(adev);
 694		break;
 695	case CHIP_VEGA20:
 696		vega20_reg_base_init(adev);
 697		break;
 698	case CHIP_ARCTURUS:
 699		arct_reg_base_init(adev);
 700		break;
 701	case CHIP_ALDEBARAN:
 702		aldebaran_reg_base_init(adev);
 703		break;
 704	default:
 705		DRM_ERROR("Unsupported asic type: %d!\n", adev->asic_type);
 706		break;
 707	}
 708}
 709
 710void soc15_set_virt_ops(struct amdgpu_device *adev)
 711{
 712	adev->virt.ops = &xgpu_ai_virt_ops;
 713
 714	/* init soc15 reg base early enough so we can
 715	 * request request full access for sriov before
 716	 * set_ip_blocks. */
 717	soc15_reg_base_init(adev);
 718}
 719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 720static bool soc15_need_full_reset(struct amdgpu_device *adev)
 721{
 722	/* change this when we implement soft reset */
 723	return true;
 724}
 725
 726static void soc15_get_pcie_usage(struct amdgpu_device *adev, uint64_t *count0,
 727				 uint64_t *count1)
 728{
 729	uint32_t perfctr = 0;
 730	uint64_t cnt0_of, cnt1_of;
 731	int tmp;
 732
 733	/* This reports 0 on APUs, so return to avoid writing/reading registers
 734	 * that may or may not be different from their GPU counterparts
 735	 */
 736	if (adev->flags & AMD_IS_APU)
 737		return;
 738
 739	/* Set the 2 events that we wish to watch, defined above */
 740	/* Reg 40 is # received msgs */
 741	/* Reg 104 is # of posted requests sent */
 742	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK, EVENT0_SEL, 40);
 743	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK, EVENT1_SEL, 104);
 744
 745	/* Write to enable desired perf counters */
 746	WREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK, perfctr);
 747	/* Zero out and enable the perf counters
 748	 * Write 0x5:
 749	 * Bit 0 = Start all counters(1)
 750	 * Bit 2 = Global counter reset enable(1)
 751	 */
 752	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000005);
 753
 754	msleep(1000);
 755
 756	/* Load the shadow and disable the perf counters
 757	 * Write 0x2:
 758	 * Bit 0 = Stop counters(0)
 759	 * Bit 1 = Load the shadow counters(1)
 760	 */
 761	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000002);
 762
 763	/* Read register values to get any >32bit overflow */
 764	tmp = RREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK);
 765	cnt0_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK, COUNTER0_UPPER);
 766	cnt1_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK, COUNTER1_UPPER);
 767
 768	/* Get the values and add the overflow */
 769	*count0 = RREG32_PCIE(smnPCIE_PERF_COUNT0_TXCLK) | (cnt0_of << 32);
 770	*count1 = RREG32_PCIE(smnPCIE_PERF_COUNT1_TXCLK) | (cnt1_of << 32);
 771}
 772
 773static void vega20_get_pcie_usage(struct amdgpu_device *adev, uint64_t *count0,
 774				 uint64_t *count1)
 775{
 776	uint32_t perfctr = 0;
 777	uint64_t cnt0_of, cnt1_of;
 778	int tmp;
 779
 780	/* This reports 0 on APUs, so return to avoid writing/reading registers
 781	 * that may or may not be different from their GPU counterparts
 782	 */
 783	if (adev->flags & AMD_IS_APU)
 784		return;
 785
 786	/* Set the 2 events that we wish to watch, defined above */
 787	/* Reg 40 is # received msgs */
 788	/* Reg 108 is # of posted requests sent on VG20 */
 789	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK3,
 790				EVENT0_SEL, 40);
 791	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK3,
 792				EVENT1_SEL, 108);
 793
 794	/* Write to enable desired perf counters */
 795	WREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK3, perfctr);
 796	/* Zero out and enable the perf counters
 797	 * Write 0x5:
 798	 * Bit 0 = Start all counters(1)
 799	 * Bit 2 = Global counter reset enable(1)
 800	 */
 801	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000005);
 802
 803	msleep(1000);
 804
 805	/* Load the shadow and disable the perf counters
 806	 * Write 0x2:
 807	 * Bit 0 = Stop counters(0)
 808	 * Bit 1 = Load the shadow counters(1)
 809	 */
 810	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000002);
 811
 812	/* Read register values to get any >32bit overflow */
 813	tmp = RREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK3);
 814	cnt0_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK3, COUNTER0_UPPER);
 815	cnt1_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK3, COUNTER1_UPPER);
 816
 817	/* Get the values and add the overflow */
 818	*count0 = RREG32_PCIE(smnPCIE_PERF_COUNT0_TXCLK3) | (cnt0_of << 32);
 819	*count1 = RREG32_PCIE(smnPCIE_PERF_COUNT1_TXCLK3) | (cnt1_of << 32);
 820}
 821
 822static bool soc15_need_reset_on_init(struct amdgpu_device *adev)
 823{
 824	u32 sol_reg;
 825
 826	/* CP hangs in IGT reloading test on RN, reset to WA */
 827	if (adev->asic_type == CHIP_RENOIR)
 828		return true;
 829
 830	/* Just return false for soc15 GPUs.  Reset does not seem to
 831	 * be necessary.
 832	 */
 833	if (!amdgpu_passthrough(adev))
 834		return false;
 835
 836	if (adev->flags & AMD_IS_APU)
 837		return false;
 838
 839	/* Check sOS sign of life register to confirm sys driver and sOS
 840	 * are already been loaded.
 841	 */
 842	sol_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_81);
 843	if (sol_reg)
 844		return true;
 845
 846	return false;
 847}
 848
 849static uint64_t soc15_get_pcie_replay_count(struct amdgpu_device *adev)
 850{
 851	uint64_t nak_r, nak_g;
 852
 853	/* Get the number of NAKs received and generated */
 854	nak_r = RREG32_PCIE(smnPCIE_RX_NUM_NAK);
 855	nak_g = RREG32_PCIE(smnPCIE_RX_NUM_NAK_GENERATED);
 856
 857	/* Add the total number of NAKs, i.e the number of replays */
 858	return (nak_r + nak_g);
 859}
 860
 861static void soc15_pre_asic_init(struct amdgpu_device *adev)
 862{
 863	gmc_v9_0_restore_registers(adev);
 864}
 865
 866static const struct amdgpu_asic_funcs soc15_asic_funcs =
 867{
 868	.read_disabled_bios = &soc15_read_disabled_bios,
 869	.read_bios_from_rom = &amdgpu_soc15_read_bios_from_rom,
 870	.read_register = &soc15_read_register,
 871	.reset = &soc15_asic_reset,
 872	.reset_method = &soc15_asic_reset_method,
 
 873	.get_xclk = &soc15_get_xclk,
 874	.set_uvd_clocks = &soc15_set_uvd_clocks,
 875	.set_vce_clocks = &soc15_set_vce_clocks,
 876	.get_config_memsize = &soc15_get_config_memsize,
 877	.need_full_reset = &soc15_need_full_reset,
 878	.init_doorbell_index = &vega10_doorbell_index_init,
 879	.get_pcie_usage = &soc15_get_pcie_usage,
 880	.need_reset_on_init = &soc15_need_reset_on_init,
 881	.get_pcie_replay_count = &soc15_get_pcie_replay_count,
 882	.supports_baco = &soc15_supports_baco,
 883	.pre_asic_init = &soc15_pre_asic_init,
 884	.query_video_codecs = &soc15_query_video_codecs,
 885};
 886
 887static const struct amdgpu_asic_funcs vega20_asic_funcs =
 888{
 889	.read_disabled_bios = &soc15_read_disabled_bios,
 890	.read_bios_from_rom = &amdgpu_soc15_read_bios_from_rom,
 891	.read_register = &soc15_read_register,
 892	.reset = &soc15_asic_reset,
 893	.reset_method = &soc15_asic_reset_method,
 
 894	.get_xclk = &soc15_get_xclk,
 895	.set_uvd_clocks = &soc15_set_uvd_clocks,
 896	.set_vce_clocks = &soc15_set_vce_clocks,
 897	.get_config_memsize = &soc15_get_config_memsize,
 898	.need_full_reset = &soc15_need_full_reset,
 899	.init_doorbell_index = &vega20_doorbell_index_init,
 900	.get_pcie_usage = &vega20_get_pcie_usage,
 901	.need_reset_on_init = &soc15_need_reset_on_init,
 902	.get_pcie_replay_count = &soc15_get_pcie_replay_count,
 903	.supports_baco = &soc15_supports_baco,
 904	.pre_asic_init = &soc15_pre_asic_init,
 905	.query_video_codecs = &soc15_query_video_codecs,
 906};
 907
 908static const struct amdgpu_asic_funcs aqua_vanjaram_asic_funcs =
 909{
 910	.read_disabled_bios = &soc15_read_disabled_bios,
 911	.read_bios_from_rom = &amdgpu_soc15_read_bios_from_rom,
 912	.read_register = &soc15_read_register,
 913	.reset = &soc15_asic_reset,
 914	.reset_method = &soc15_asic_reset_method,
 915	.get_xclk = &soc15_get_xclk,
 916	.set_uvd_clocks = &soc15_set_uvd_clocks,
 917	.set_vce_clocks = &soc15_set_vce_clocks,
 918	.get_config_memsize = &soc15_get_config_memsize,
 919	.need_full_reset = &soc15_need_full_reset,
 920	.init_doorbell_index = &aqua_vanjaram_doorbell_index_init,
 921	.get_pcie_usage = &amdgpu_nbio_get_pcie_usage,
 922	.need_reset_on_init = &soc15_need_reset_on_init,
 923	.get_pcie_replay_count = &amdgpu_nbio_get_pcie_replay_count,
 924	.supports_baco = &soc15_supports_baco,
 925	.pre_asic_init = &soc15_pre_asic_init,
 926	.query_video_codecs = &soc15_query_video_codecs,
 927	.encode_ext_smn_addressing = &aqua_vanjaram_encode_ext_smn_addressing,
 928	.get_reg_state = &aqua_vanjaram_get_reg_state,
 929};
 930
 931static int soc15_common_early_init(void *handle)
 932{
 933#define MMIO_REG_HOLE_OFFSET (0x80000 - PAGE_SIZE)
 934	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 935
 936	if (!amdgpu_sriov_vf(adev)) {
 937		adev->rmmio_remap.reg_offset = MMIO_REG_HOLE_OFFSET;
 938		adev->rmmio_remap.bus_addr = adev->rmmio_base + MMIO_REG_HOLE_OFFSET;
 939	}
 940	adev->smc_rreg = NULL;
 941	adev->smc_wreg = NULL;
 942	adev->pcie_rreg = &amdgpu_device_indirect_rreg;
 943	adev->pcie_wreg = &amdgpu_device_indirect_wreg;
 944	adev->pcie_rreg_ext = &amdgpu_device_indirect_rreg_ext;
 945	adev->pcie_wreg_ext = &amdgpu_device_indirect_wreg_ext;
 946	adev->pcie_rreg64 = &amdgpu_device_indirect_rreg64;
 947	adev->pcie_wreg64 = &amdgpu_device_indirect_wreg64;
 948	adev->pcie_rreg64_ext = &amdgpu_device_indirect_rreg64_ext;
 949	adev->pcie_wreg64_ext = &amdgpu_device_indirect_wreg64_ext;
 950	adev->uvd_ctx_rreg = &soc15_uvd_ctx_rreg;
 951	adev->uvd_ctx_wreg = &soc15_uvd_ctx_wreg;
 952	adev->didt_rreg = &soc15_didt_rreg;
 953	adev->didt_wreg = &soc15_didt_wreg;
 954	adev->gc_cac_rreg = &soc15_gc_cac_rreg;
 955	adev->gc_cac_wreg = &soc15_gc_cac_wreg;
 956	adev->se_cac_rreg = &soc15_se_cac_rreg;
 957	adev->se_cac_wreg = &soc15_se_cac_wreg;
 958
 959	adev->rev_id = amdgpu_device_get_rev_id(adev);
 960	adev->external_rev_id = 0xFF;
 961	/* TODO: split the GC and PG flags based on the relevant IP version for which
 962	 * they are relevant.
 963	 */
 964	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
 965	case IP_VERSION(9, 0, 1):
 966		adev->asic_funcs = &soc15_asic_funcs;
 967		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
 968			AMD_CG_SUPPORT_GFX_MGLS |
 969			AMD_CG_SUPPORT_GFX_RLC_LS |
 970			AMD_CG_SUPPORT_GFX_CP_LS |
 971			AMD_CG_SUPPORT_GFX_3D_CGCG |
 972			AMD_CG_SUPPORT_GFX_3D_CGLS |
 973			AMD_CG_SUPPORT_GFX_CGCG |
 974			AMD_CG_SUPPORT_GFX_CGLS |
 975			AMD_CG_SUPPORT_BIF_MGCG |
 976			AMD_CG_SUPPORT_BIF_LS |
 977			AMD_CG_SUPPORT_HDP_LS |
 978			AMD_CG_SUPPORT_DRM_MGCG |
 979			AMD_CG_SUPPORT_DRM_LS |
 980			AMD_CG_SUPPORT_ROM_MGCG |
 981			AMD_CG_SUPPORT_DF_MGCG |
 982			AMD_CG_SUPPORT_SDMA_MGCG |
 983			AMD_CG_SUPPORT_SDMA_LS |
 984			AMD_CG_SUPPORT_MC_MGCG |
 985			AMD_CG_SUPPORT_MC_LS;
 986		adev->pg_flags = 0;
 987		adev->external_rev_id = 0x1;
 988		break;
 989	case IP_VERSION(9, 2, 1):
 990		adev->asic_funcs = &soc15_asic_funcs;
 991		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
 992			AMD_CG_SUPPORT_GFX_MGLS |
 993			AMD_CG_SUPPORT_GFX_CGCG |
 994			AMD_CG_SUPPORT_GFX_CGLS |
 995			AMD_CG_SUPPORT_GFX_3D_CGCG |
 996			AMD_CG_SUPPORT_GFX_3D_CGLS |
 997			AMD_CG_SUPPORT_GFX_CP_LS |
 998			AMD_CG_SUPPORT_MC_LS |
 999			AMD_CG_SUPPORT_MC_MGCG |
1000			AMD_CG_SUPPORT_SDMA_MGCG |
1001			AMD_CG_SUPPORT_SDMA_LS |
1002			AMD_CG_SUPPORT_BIF_MGCG |
1003			AMD_CG_SUPPORT_BIF_LS |
1004			AMD_CG_SUPPORT_HDP_MGCG |
1005			AMD_CG_SUPPORT_HDP_LS |
1006			AMD_CG_SUPPORT_ROM_MGCG |
1007			AMD_CG_SUPPORT_VCE_MGCG |
1008			AMD_CG_SUPPORT_UVD_MGCG;
1009		adev->pg_flags = 0;
1010		adev->external_rev_id = adev->rev_id + 0x14;
1011		break;
1012	case IP_VERSION(9, 4, 0):
1013		adev->asic_funcs = &vega20_asic_funcs;
1014		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1015			AMD_CG_SUPPORT_GFX_MGLS |
1016			AMD_CG_SUPPORT_GFX_CGCG |
1017			AMD_CG_SUPPORT_GFX_CGLS |
1018			AMD_CG_SUPPORT_GFX_3D_CGCG |
1019			AMD_CG_SUPPORT_GFX_3D_CGLS |
1020			AMD_CG_SUPPORT_GFX_CP_LS |
1021			AMD_CG_SUPPORT_MC_LS |
1022			AMD_CG_SUPPORT_MC_MGCG |
1023			AMD_CG_SUPPORT_SDMA_MGCG |
1024			AMD_CG_SUPPORT_SDMA_LS |
1025			AMD_CG_SUPPORT_BIF_MGCG |
1026			AMD_CG_SUPPORT_BIF_LS |
1027			AMD_CG_SUPPORT_HDP_MGCG |
1028			AMD_CG_SUPPORT_HDP_LS |
1029			AMD_CG_SUPPORT_ROM_MGCG |
1030			AMD_CG_SUPPORT_VCE_MGCG |
1031			AMD_CG_SUPPORT_UVD_MGCG;
1032		adev->pg_flags = 0;
1033		adev->external_rev_id = adev->rev_id + 0x28;
1034		break;
1035	case IP_VERSION(9, 1, 0):
1036	case IP_VERSION(9, 2, 2):
1037		adev->asic_funcs = &soc15_asic_funcs;
1038
1039		if (adev->rev_id >= 0x8)
1040			adev->apu_flags |= AMD_APU_IS_RAVEN2;
1041
1042		if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1043			adev->external_rev_id = adev->rev_id + 0x79;
1044		else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1045			adev->external_rev_id = adev->rev_id + 0x41;
1046		else if (adev->rev_id == 1)
1047			adev->external_rev_id = adev->rev_id + 0x20;
1048		else
1049			adev->external_rev_id = adev->rev_id + 0x01;
1050
1051		if (adev->apu_flags & AMD_APU_IS_RAVEN2) {
1052			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1053				AMD_CG_SUPPORT_GFX_MGLS |
1054				AMD_CG_SUPPORT_GFX_CP_LS |
1055				AMD_CG_SUPPORT_GFX_3D_CGCG |
1056				AMD_CG_SUPPORT_GFX_3D_CGLS |
1057				AMD_CG_SUPPORT_GFX_CGCG |
1058				AMD_CG_SUPPORT_GFX_CGLS |
1059				AMD_CG_SUPPORT_BIF_LS |
1060				AMD_CG_SUPPORT_HDP_LS |
1061				AMD_CG_SUPPORT_MC_MGCG |
1062				AMD_CG_SUPPORT_MC_LS |
1063				AMD_CG_SUPPORT_SDMA_MGCG |
1064				AMD_CG_SUPPORT_SDMA_LS |
1065				AMD_CG_SUPPORT_VCN_MGCG;
1066
1067			adev->pg_flags = AMD_PG_SUPPORT_SDMA | AMD_PG_SUPPORT_VCN;
1068		} else if (adev->apu_flags & AMD_APU_IS_PICASSO) {
1069			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1070				AMD_CG_SUPPORT_GFX_MGLS |
1071				AMD_CG_SUPPORT_GFX_CP_LS |
1072				AMD_CG_SUPPORT_GFX_3D_CGLS |
1073				AMD_CG_SUPPORT_GFX_CGCG |
1074				AMD_CG_SUPPORT_GFX_CGLS |
1075				AMD_CG_SUPPORT_BIF_LS |
1076				AMD_CG_SUPPORT_HDP_LS |
1077				AMD_CG_SUPPORT_MC_MGCG |
1078				AMD_CG_SUPPORT_MC_LS |
1079				AMD_CG_SUPPORT_SDMA_MGCG |
1080				AMD_CG_SUPPORT_SDMA_LS |
1081				AMD_CG_SUPPORT_VCN_MGCG;
1082
1083			/*
1084			 * MMHUB PG needs to be disabled for Picasso for
1085			 * stability reasons.
1086			 */
1087			adev->pg_flags = AMD_PG_SUPPORT_SDMA |
 
1088				AMD_PG_SUPPORT_VCN;
1089		} else {
1090			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1091				AMD_CG_SUPPORT_GFX_MGLS |
1092				AMD_CG_SUPPORT_GFX_RLC_LS |
1093				AMD_CG_SUPPORT_GFX_CP_LS |
1094				AMD_CG_SUPPORT_GFX_3D_CGLS |
1095				AMD_CG_SUPPORT_GFX_CGCG |
1096				AMD_CG_SUPPORT_GFX_CGLS |
1097				AMD_CG_SUPPORT_BIF_MGCG |
1098				AMD_CG_SUPPORT_BIF_LS |
1099				AMD_CG_SUPPORT_HDP_MGCG |
1100				AMD_CG_SUPPORT_HDP_LS |
1101				AMD_CG_SUPPORT_DRM_MGCG |
1102				AMD_CG_SUPPORT_DRM_LS |
1103				AMD_CG_SUPPORT_MC_MGCG |
1104				AMD_CG_SUPPORT_MC_LS |
1105				AMD_CG_SUPPORT_SDMA_MGCG |
1106				AMD_CG_SUPPORT_SDMA_LS |
1107				AMD_CG_SUPPORT_VCN_MGCG;
1108
1109			adev->pg_flags = AMD_PG_SUPPORT_SDMA | AMD_PG_SUPPORT_VCN;
1110		}
1111		break;
1112	case IP_VERSION(9, 4, 1):
1113		adev->asic_funcs = &vega20_asic_funcs;
1114		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1115			AMD_CG_SUPPORT_GFX_MGLS |
1116			AMD_CG_SUPPORT_GFX_CGCG |
1117			AMD_CG_SUPPORT_GFX_CGLS |
1118			AMD_CG_SUPPORT_GFX_CP_LS |
1119			AMD_CG_SUPPORT_HDP_MGCG |
1120			AMD_CG_SUPPORT_HDP_LS |
1121			AMD_CG_SUPPORT_SDMA_MGCG |
1122			AMD_CG_SUPPORT_SDMA_LS |
1123			AMD_CG_SUPPORT_MC_MGCG |
1124			AMD_CG_SUPPORT_MC_LS |
1125			AMD_CG_SUPPORT_IH_CG |
1126			AMD_CG_SUPPORT_VCN_MGCG |
1127			AMD_CG_SUPPORT_JPEG_MGCG;
1128		adev->pg_flags = AMD_PG_SUPPORT_VCN | AMD_PG_SUPPORT_VCN_DPG;
1129		adev->external_rev_id = adev->rev_id + 0x32;
1130		break;
1131	case IP_VERSION(9, 3, 0):
1132		adev->asic_funcs = &soc15_asic_funcs;
1133
1134		if (adev->apu_flags & AMD_APU_IS_RENOIR)
1135			adev->external_rev_id = adev->rev_id + 0x91;
1136		else
1137			adev->external_rev_id = adev->rev_id + 0xa1;
1138		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1139				 AMD_CG_SUPPORT_GFX_MGLS |
1140				 AMD_CG_SUPPORT_GFX_3D_CGCG |
1141				 AMD_CG_SUPPORT_GFX_3D_CGLS |
1142				 AMD_CG_SUPPORT_GFX_CGCG |
1143				 AMD_CG_SUPPORT_GFX_CGLS |
1144				 AMD_CG_SUPPORT_GFX_CP_LS |
1145				 AMD_CG_SUPPORT_MC_MGCG |
1146				 AMD_CG_SUPPORT_MC_LS |
1147				 AMD_CG_SUPPORT_SDMA_MGCG |
1148				 AMD_CG_SUPPORT_SDMA_LS |
1149				 AMD_CG_SUPPORT_BIF_LS |
1150				 AMD_CG_SUPPORT_HDP_LS |
1151				 AMD_CG_SUPPORT_VCN_MGCG |
1152				 AMD_CG_SUPPORT_JPEG_MGCG |
1153				 AMD_CG_SUPPORT_IH_CG |
1154				 AMD_CG_SUPPORT_ATHUB_LS |
1155				 AMD_CG_SUPPORT_ATHUB_MGCG |
1156				 AMD_CG_SUPPORT_DF_MGCG;
1157		adev->pg_flags = AMD_PG_SUPPORT_SDMA |
1158				 AMD_PG_SUPPORT_VCN |
1159				 AMD_PG_SUPPORT_JPEG |
1160				 AMD_PG_SUPPORT_VCN_DPG;
1161		break;
1162	case IP_VERSION(9, 4, 2):
1163		adev->asic_funcs = &vega20_asic_funcs;
1164		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1165			AMD_CG_SUPPORT_GFX_MGLS |
 
 
1166			AMD_CG_SUPPORT_GFX_CP_LS |
1167			AMD_CG_SUPPORT_HDP_LS |
1168			AMD_CG_SUPPORT_SDMA_MGCG |
1169			AMD_CG_SUPPORT_SDMA_LS |
1170			AMD_CG_SUPPORT_IH_CG |
1171			AMD_CG_SUPPORT_VCN_MGCG | AMD_CG_SUPPORT_JPEG_MGCG;
1172		adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG;
1173		adev->external_rev_id = adev->rev_id + 0x3c;
1174		break;
1175	case IP_VERSION(9, 4, 3):
1176		adev->asic_funcs = &aqua_vanjaram_asic_funcs;
1177		adev->cg_flags =
1178			AMD_CG_SUPPORT_GFX_MGCG | AMD_CG_SUPPORT_GFX_CGCG |
1179			AMD_CG_SUPPORT_GFX_CGLS | AMD_CG_SUPPORT_SDMA_MGCG |
1180			AMD_CG_SUPPORT_GFX_FGCG | AMD_CG_SUPPORT_REPEATER_FGCG |
1181			AMD_CG_SUPPORT_VCN_MGCG | AMD_CG_SUPPORT_JPEG_MGCG |
1182			AMD_CG_SUPPORT_IH_CG;
1183		adev->pg_flags =
1184			AMD_PG_SUPPORT_VCN |
1185			AMD_PG_SUPPORT_VCN_DPG |
1186			AMD_PG_SUPPORT_JPEG;
1187		adev->external_rev_id = adev->rev_id + 0x46;
1188		/* GC 9.4.3 uses MMIO register region hole at a different offset */
1189		if (!amdgpu_sriov_vf(adev)) {
1190			adev->rmmio_remap.reg_offset = 0x1A000;
1191			adev->rmmio_remap.bus_addr = adev->rmmio_base + 0x1A000;
1192		}
1193		break;
1194	default:
1195		/* FIXME: not supported yet */
1196		return -EINVAL;
1197	}
1198
1199	if (amdgpu_sriov_vf(adev)) {
1200		amdgpu_virt_init_setting(adev);
1201		xgpu_ai_mailbox_set_irq_funcs(adev);
1202	}
1203
1204	return 0;
1205}
1206
1207static int soc15_common_late_init(void *handle)
1208{
1209	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
1210
1211	if (amdgpu_sriov_vf(adev))
1212		xgpu_ai_mailbox_get_irq(adev);
1213
1214	/* Enable selfring doorbell aperture late because doorbell BAR
1215	 * aperture will change if resize BAR successfully in gmc sw_init.
1216	 */
1217	adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, true);
1218
1219	return 0;
1220}
1221
1222static int soc15_common_sw_init(void *handle)
1223{
1224	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1225
1226	if (amdgpu_sriov_vf(adev))
1227		xgpu_ai_mailbox_add_irq_id(adev);
1228
1229	if (adev->df.funcs &&
1230	    adev->df.funcs->sw_init)
1231		adev->df.funcs->sw_init(adev);
1232
1233	return 0;
1234}
1235
1236static int soc15_common_sw_fini(void *handle)
1237{
1238	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1239
1240	if (adev->df.funcs &&
1241	    adev->df.funcs->sw_fini)
1242		adev->df.funcs->sw_fini(adev);
 
1243	return 0;
1244}
1245
1246static void soc15_sdma_doorbell_range_init(struct amdgpu_device *adev)
1247{
1248	int i;
 
1249
1250	/* sdma doorbell range is programed by hypervisor */
1251	if (!amdgpu_sriov_vf(adev)) {
1252		for (i = 0; i < adev->sdma.num_instances; i++) {
 
1253			adev->nbio.funcs->sdma_doorbell_range(adev, i,
1254				true, adev->doorbell_index.sdma_engine[i] << 1,
1255				adev->doorbell_index.sdma_doorbell_range);
1256		}
 
 
 
1257	}
1258}
1259
1260static int soc15_common_hw_init(void *handle)
1261{
1262	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1263
 
 
1264	/* enable aspm */
1265	soc15_program_aspm(adev);
1266	/* setup nbio registers */
1267	adev->nbio.funcs->init_registers(adev);
1268	/* remap HDP registers to a hole in mmio space,
1269	 * for the purpose of expose those registers
1270	 * to process space
1271	 */
1272	if (adev->nbio.funcs->remap_hdp_registers && !amdgpu_sriov_vf(adev))
1273		adev->nbio.funcs->remap_hdp_registers(adev);
1274
1275	/* enable the doorbell aperture */
1276	adev->nbio.funcs->enable_doorbell_aperture(adev, true);
1277
1278	/* HW doorbell routing policy: doorbell writing not
1279	 * in SDMA/IH/MM/ACV range will be routed to CP. So
1280	 * we need to init SDMA doorbell range prior
1281	 * to CP ip block init and ring test.  IH already
1282	 * happens before CP.
1283	 */
1284	soc15_sdma_doorbell_range_init(adev);
1285
1286	return 0;
1287}
1288
1289static int soc15_common_hw_fini(void *handle)
1290{
1291	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1292
1293	/* Disable the doorbell aperture and selfring doorbell aperture
1294	 * separately in hw_fini because soc15_enable_doorbell_aperture
1295	 * has been removed and there is no need to delay disabling
1296	 * selfring doorbell.
1297	 */
1298	adev->nbio.funcs->enable_doorbell_aperture(adev, false);
1299	adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, false);
1300
1301	if (amdgpu_sriov_vf(adev))
1302		xgpu_ai_mailbox_put_irq(adev);
1303
1304	if (adev->nbio.ras_if &&
1305	    amdgpu_ras_is_supported(adev, adev->nbio.ras_if->block)) {
1306		if (adev->nbio.ras &&
1307		    adev->nbio.ras->init_ras_controller_interrupt)
1308			amdgpu_irq_put(adev, &adev->nbio.ras_controller_irq, 0);
1309		if (adev->nbio.ras &&
1310		    adev->nbio.ras->init_ras_err_event_athub_interrupt)
1311			amdgpu_irq_put(adev, &adev->nbio.ras_err_event_athub_irq, 0);
1312	}
1313
1314	return 0;
1315}
1316
1317static int soc15_common_suspend(void *handle)
1318{
1319	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1320
1321	return soc15_common_hw_fini(adev);
1322}
1323
1324static int soc15_common_resume(void *handle)
1325{
1326	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1327
1328	if (soc15_need_reset_on_resume(adev)) {
1329		dev_info(adev->dev, "S3 suspend abort case, let's reset ASIC.\n");
1330		soc15_asic_reset(adev);
1331	}
1332	return soc15_common_hw_init(adev);
1333}
1334
1335static bool soc15_common_is_idle(void *handle)
1336{
1337	return true;
1338}
1339
1340static int soc15_common_wait_for_idle(void *handle)
1341{
1342	return 0;
1343}
1344
1345static int soc15_common_soft_reset(void *handle)
1346{
1347	return 0;
1348}
1349
1350static void soc15_update_drm_clock_gating(struct amdgpu_device *adev, bool enable)
1351{
1352	uint32_t def, data;
1353
1354	def = data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0));
1355
1356	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_DRM_MGCG))
1357		data &= ~(0x01000000 |
1358			  0x02000000 |
1359			  0x04000000 |
1360			  0x08000000 |
1361			  0x10000000 |
1362			  0x20000000 |
1363			  0x40000000 |
1364			  0x80000000);
1365	else
1366		data |= (0x01000000 |
1367			 0x02000000 |
1368			 0x04000000 |
1369			 0x08000000 |
1370			 0x10000000 |
1371			 0x20000000 |
1372			 0x40000000 |
1373			 0x80000000);
1374
1375	if (def != data)
1376		WREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0), data);
1377}
1378
1379static void soc15_update_drm_light_sleep(struct amdgpu_device *adev, bool enable)
1380{
1381	uint32_t def, data;
1382
1383	def = data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL));
1384
1385	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_DRM_LS))
1386		data |= 1;
1387	else
1388		data &= ~1;
1389
1390	if (def != data)
1391		WREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL), data);
1392}
1393
1394static int soc15_common_set_clockgating_state(void *handle,
1395					    enum amd_clockgating_state state)
1396{
1397	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1398
1399	if (amdgpu_sriov_vf(adev))
1400		return 0;
1401
1402	switch (amdgpu_ip_version(adev, NBIO_HWIP, 0)) {
1403	case IP_VERSION(6, 1, 0):
1404	case IP_VERSION(6, 2, 0):
1405	case IP_VERSION(7, 4, 0):
1406		adev->nbio.funcs->update_medium_grain_clock_gating(adev,
1407				state == AMD_CG_STATE_GATE);
1408		adev->nbio.funcs->update_medium_grain_light_sleep(adev,
1409				state == AMD_CG_STATE_GATE);
1410		adev->hdp.funcs->update_clock_gating(adev,
1411				state == AMD_CG_STATE_GATE);
1412		soc15_update_drm_clock_gating(adev,
1413				state == AMD_CG_STATE_GATE);
1414		soc15_update_drm_light_sleep(adev,
1415				state == AMD_CG_STATE_GATE);
1416		adev->smuio.funcs->update_rom_clock_gating(adev,
1417				state == AMD_CG_STATE_GATE);
1418		adev->df.funcs->update_medium_grain_clock_gating(adev,
1419				state == AMD_CG_STATE_GATE);
1420		break;
1421	case IP_VERSION(7, 0, 0):
1422	case IP_VERSION(7, 0, 1):
1423	case IP_VERSION(2, 5, 0):
1424		adev->nbio.funcs->update_medium_grain_clock_gating(adev,
1425				state == AMD_CG_STATE_GATE);
1426		adev->nbio.funcs->update_medium_grain_light_sleep(adev,
1427				state == AMD_CG_STATE_GATE);
1428		adev->hdp.funcs->update_clock_gating(adev,
1429				state == AMD_CG_STATE_GATE);
1430		soc15_update_drm_clock_gating(adev,
1431				state == AMD_CG_STATE_GATE);
1432		soc15_update_drm_light_sleep(adev,
1433				state == AMD_CG_STATE_GATE);
1434		break;
1435	case IP_VERSION(7, 4, 1):
1436	case IP_VERSION(7, 4, 4):
1437		adev->hdp.funcs->update_clock_gating(adev,
1438				state == AMD_CG_STATE_GATE);
1439		break;
1440	default:
1441		break;
1442	}
1443	return 0;
1444}
1445
1446static void soc15_common_get_clockgating_state(void *handle, u64 *flags)
1447{
1448	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1449	int data;
1450
1451	if (amdgpu_sriov_vf(adev))
1452		*flags = 0;
1453
1454	if (adev->nbio.funcs && adev->nbio.funcs->get_clockgating_state)
1455		adev->nbio.funcs->get_clockgating_state(adev, flags);
 
1456
1457	if (adev->hdp.funcs && adev->hdp.funcs->get_clock_gating_state)
1458		adev->hdp.funcs->get_clock_gating_state(adev, flags);
1459
1460	if ((amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(13, 0, 2)) &&
1461	    (amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(13, 0, 6))) {
1462		/* AMD_CG_SUPPORT_DRM_MGCG */
1463		data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0));
1464		if (!(data & 0x01000000))
1465			*flags |= AMD_CG_SUPPORT_DRM_MGCG;
1466
1467		/* AMD_CG_SUPPORT_DRM_LS */
1468		data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL));
1469		if (data & 0x1)
1470			*flags |= AMD_CG_SUPPORT_DRM_LS;
1471	}
1472
1473	/* AMD_CG_SUPPORT_ROM_MGCG */
1474	if (adev->smuio.funcs && adev->smuio.funcs->get_clock_gating_state)
1475		adev->smuio.funcs->get_clock_gating_state(adev, flags);
1476
1477	if (adev->df.funcs && adev->df.funcs->get_clockgating_state)
1478		adev->df.funcs->get_clockgating_state(adev, flags);
1479}
1480
1481static int soc15_common_set_powergating_state(void *handle,
1482					    enum amd_powergating_state state)
1483{
1484	/* todo */
1485	return 0;
1486}
1487
1488static const struct amd_ip_funcs soc15_common_ip_funcs = {
1489	.name = "soc15_common",
1490	.early_init = soc15_common_early_init,
1491	.late_init = soc15_common_late_init,
1492	.sw_init = soc15_common_sw_init,
1493	.sw_fini = soc15_common_sw_fini,
1494	.hw_init = soc15_common_hw_init,
1495	.hw_fini = soc15_common_hw_fini,
1496	.suspend = soc15_common_suspend,
1497	.resume = soc15_common_resume,
1498	.is_idle = soc15_common_is_idle,
1499	.wait_for_idle = soc15_common_wait_for_idle,
1500	.soft_reset = soc15_common_soft_reset,
1501	.set_clockgating_state = soc15_common_set_clockgating_state,
1502	.set_powergating_state = soc15_common_set_powergating_state,
1503	.get_clockgating_state= soc15_common_get_clockgating_state,
1504};
v5.14.15
   1/*
   2 * Copyright 2016 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/firmware.h>
  24#include <linux/slab.h>
  25#include <linux/module.h>
  26#include <linux/pci.h>
  27
  28#include <drm/amdgpu_drm.h>
  29
  30#include "amdgpu.h"
  31#include "amdgpu_atombios.h"
  32#include "amdgpu_ih.h"
  33#include "amdgpu_uvd.h"
  34#include "amdgpu_vce.h"
  35#include "amdgpu_ucode.h"
  36#include "amdgpu_psp.h"
  37#include "atom.h"
  38#include "amd_pcie.h"
  39
  40#include "uvd/uvd_7_0_offset.h"
  41#include "gc/gc_9_0_offset.h"
  42#include "gc/gc_9_0_sh_mask.h"
  43#include "sdma0/sdma0_4_0_offset.h"
  44#include "sdma1/sdma1_4_0_offset.h"
  45#include "nbio/nbio_7_0_default.h"
  46#include "nbio/nbio_7_0_offset.h"
  47#include "nbio/nbio_7_0_sh_mask.h"
  48#include "nbio/nbio_7_0_smn.h"
  49#include "mp/mp_9_0_offset.h"
  50
  51#include "soc15.h"
  52#include "soc15_common.h"
  53#include "gfx_v9_0.h"
  54#include "gmc_v9_0.h"
  55#include "gfxhub_v1_0.h"
  56#include "mmhub_v1_0.h"
  57#include "df_v1_7.h"
  58#include "df_v3_6.h"
  59#include "nbio_v6_1.h"
  60#include "nbio_v7_0.h"
  61#include "nbio_v7_4.h"
  62#include "hdp_v4_0.h"
  63#include "vega10_ih.h"
  64#include "vega20_ih.h"
  65#include "navi10_ih.h"
  66#include "sdma_v4_0.h"
  67#include "uvd_v7_0.h"
  68#include "vce_v4_0.h"
  69#include "vcn_v1_0.h"
  70#include "vcn_v2_0.h"
  71#include "jpeg_v2_0.h"
  72#include "vcn_v2_5.h"
  73#include "jpeg_v2_5.h"
  74#include "smuio_v9_0.h"
  75#include "smuio_v11_0.h"
  76#include "smuio_v13_0.h"
  77#include "dce_virtual.h"
  78#include "mxgpu_ai.h"
  79#include "amdgpu_ras.h"
  80#include "amdgpu_xgmi.h"
  81#include <uapi/linux/kfd_ioctl.h>
  82
  83#define mmMP0_MISC_CGTT_CTRL0                                                                   0x01b9
  84#define mmMP0_MISC_CGTT_CTRL0_BASE_IDX                                                          0
  85#define mmMP0_MISC_LIGHT_SLEEP_CTRL                                                             0x01ba
  86#define mmMP0_MISC_LIGHT_SLEEP_CTRL_BASE_IDX                                                    0
  87
 
 
  88/* Vega, Raven, Arcturus */
  89static const struct amdgpu_video_codec_info vega_video_codecs_encode_array[] =
  90{
  91	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 2304, 0)},
  92	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 4096, 2304, 0)},
  93};
  94
  95static const struct amdgpu_video_codecs vega_video_codecs_encode =
  96{
  97	.codec_count = ARRAY_SIZE(vega_video_codecs_encode_array),
  98	.codec_array = vega_video_codecs_encode_array,
  99};
 100
 101/* Vega */
 102static const struct amdgpu_video_codec_info vega_video_codecs_decode_array[] =
 103{
 104	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2, 4096, 4906, 3)},
 105	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4, 4096, 4906, 5)},
 106	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4906, 52)},
 107	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1, 4096, 4906, 4)},
 108	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 4096, 4096, 186)},
 109	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
 110};
 111
 112static const struct amdgpu_video_codecs vega_video_codecs_decode =
 113{
 114	.codec_count = ARRAY_SIZE(vega_video_codecs_decode_array),
 115	.codec_array = vega_video_codecs_decode_array,
 116};
 117
 118/* Raven */
 119static const struct amdgpu_video_codec_info rv_video_codecs_decode_array[] =
 120{
 121	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2, 4096, 4906, 3)},
 122	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4, 4096, 4906, 5)},
 123	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4906, 52)},
 124	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1, 4096, 4906, 4)},
 125	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 4096, 4096, 186)},
 126	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
 127	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 4096, 4096, 0)},
 128};
 129
 130static const struct amdgpu_video_codecs rv_video_codecs_decode =
 131{
 132	.codec_count = ARRAY_SIZE(rv_video_codecs_decode_array),
 133	.codec_array = rv_video_codecs_decode_array,
 134};
 135
 136/* Renoir, Arcturus */
 137static const struct amdgpu_video_codec_info rn_video_codecs_decode_array[] =
 138{
 139	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2, 4096, 4906, 3)},
 140	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4, 4096, 4906, 5)},
 141	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4906, 52)},
 142	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1, 4096, 4906, 4)},
 143	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)},
 144	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
 145	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)},
 146};
 147
 148static const struct amdgpu_video_codecs rn_video_codecs_decode =
 149{
 150	.codec_count = ARRAY_SIZE(rn_video_codecs_decode_array),
 151	.codec_array = rn_video_codecs_decode_array,
 152};
 153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 154static int soc15_query_video_codecs(struct amdgpu_device *adev, bool encode,
 155				    const struct amdgpu_video_codecs **codecs)
 156{
 157	switch (adev->asic_type) {
 158	case CHIP_VEGA20:
 159	case CHIP_VEGA10:
 160	case CHIP_VEGA12:
 161		if (encode)
 162			*codecs = &vega_video_codecs_encode;
 163		else
 164			*codecs = &vega_video_codecs_decode;
 165		return 0;
 166	case CHIP_RAVEN:
 167		if (encode)
 168			*codecs = &vega_video_codecs_encode;
 169		else
 170			*codecs = &rv_video_codecs_decode;
 171		return 0;
 172	case CHIP_ARCTURUS:
 173	case CHIP_ALDEBARAN:
 174	case CHIP_RENOIR:
 175		if (encode)
 176			*codecs = &vega_video_codecs_encode;
 177		else
 178			*codecs = &rn_video_codecs_decode;
 179		return 0;
 180	default:
 181		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 182	}
 183}
 184
 185/*
 186 * Indirect registers accessor
 187 */
 188static u32 soc15_pcie_rreg(struct amdgpu_device *adev, u32 reg)
 189{
 190	unsigned long address, data;
 191	address = adev->nbio.funcs->get_pcie_index_offset(adev);
 192	data = adev->nbio.funcs->get_pcie_data_offset(adev);
 193
 194	return amdgpu_device_indirect_rreg(adev, address, data, reg);
 195}
 196
 197static void soc15_pcie_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 198{
 199	unsigned long address, data;
 200
 201	address = adev->nbio.funcs->get_pcie_index_offset(adev);
 202	data = adev->nbio.funcs->get_pcie_data_offset(adev);
 203
 204	amdgpu_device_indirect_wreg(adev, address, data, reg, v);
 205}
 206
 207static u64 soc15_pcie_rreg64(struct amdgpu_device *adev, u32 reg)
 208{
 209	unsigned long address, data;
 210	address = adev->nbio.funcs->get_pcie_index_offset(adev);
 211	data = adev->nbio.funcs->get_pcie_data_offset(adev);
 212
 213	return amdgpu_device_indirect_rreg64(adev, address, data, reg);
 214}
 215
 216static void soc15_pcie_wreg64(struct amdgpu_device *adev, u32 reg, u64 v)
 217{
 218	unsigned long address, data;
 219
 220	address = adev->nbio.funcs->get_pcie_index_offset(adev);
 221	data = adev->nbio.funcs->get_pcie_data_offset(adev);
 222
 223	amdgpu_device_indirect_wreg64(adev, address, data, reg, v);
 224}
 225
 226static u32 soc15_uvd_ctx_rreg(struct amdgpu_device *adev, u32 reg)
 227{
 228	unsigned long flags, address, data;
 229	u32 r;
 230
 231	address = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_INDEX);
 232	data = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_DATA);
 233
 234	spin_lock_irqsave(&adev->uvd_ctx_idx_lock, flags);
 235	WREG32(address, ((reg) & 0x1ff));
 236	r = RREG32(data);
 237	spin_unlock_irqrestore(&adev->uvd_ctx_idx_lock, flags);
 238	return r;
 239}
 240
 241static void soc15_uvd_ctx_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 242{
 243	unsigned long flags, address, data;
 244
 245	address = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_INDEX);
 246	data = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_DATA);
 247
 248	spin_lock_irqsave(&adev->uvd_ctx_idx_lock, flags);
 249	WREG32(address, ((reg) & 0x1ff));
 250	WREG32(data, (v));
 251	spin_unlock_irqrestore(&adev->uvd_ctx_idx_lock, flags);
 252}
 253
 254static u32 soc15_didt_rreg(struct amdgpu_device *adev, u32 reg)
 255{
 256	unsigned long flags, address, data;
 257	u32 r;
 258
 259	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
 260	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
 261
 262	spin_lock_irqsave(&adev->didt_idx_lock, flags);
 263	WREG32(address, (reg));
 264	r = RREG32(data);
 265	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
 266	return r;
 267}
 268
 269static void soc15_didt_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 270{
 271	unsigned long flags, address, data;
 272
 273	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
 274	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
 275
 276	spin_lock_irqsave(&adev->didt_idx_lock, flags);
 277	WREG32(address, (reg));
 278	WREG32(data, (v));
 279	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
 280}
 281
 282static u32 soc15_gc_cac_rreg(struct amdgpu_device *adev, u32 reg)
 283{
 284	unsigned long flags;
 285	u32 r;
 286
 287	spin_lock_irqsave(&adev->gc_cac_idx_lock, flags);
 288	WREG32_SOC15(GC, 0, mmGC_CAC_IND_INDEX, (reg));
 289	r = RREG32_SOC15(GC, 0, mmGC_CAC_IND_DATA);
 290	spin_unlock_irqrestore(&adev->gc_cac_idx_lock, flags);
 291	return r;
 292}
 293
 294static void soc15_gc_cac_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 295{
 296	unsigned long flags;
 297
 298	spin_lock_irqsave(&adev->gc_cac_idx_lock, flags);
 299	WREG32_SOC15(GC, 0, mmGC_CAC_IND_INDEX, (reg));
 300	WREG32_SOC15(GC, 0, mmGC_CAC_IND_DATA, (v));
 301	spin_unlock_irqrestore(&adev->gc_cac_idx_lock, flags);
 302}
 303
 304static u32 soc15_se_cac_rreg(struct amdgpu_device *adev, u32 reg)
 305{
 306	unsigned long flags;
 307	u32 r;
 308
 309	spin_lock_irqsave(&adev->se_cac_idx_lock, flags);
 310	WREG32_SOC15(GC, 0, mmSE_CAC_IND_INDEX, (reg));
 311	r = RREG32_SOC15(GC, 0, mmSE_CAC_IND_DATA);
 312	spin_unlock_irqrestore(&adev->se_cac_idx_lock, flags);
 313	return r;
 314}
 315
 316static void soc15_se_cac_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 317{
 318	unsigned long flags;
 319
 320	spin_lock_irqsave(&adev->se_cac_idx_lock, flags);
 321	WREG32_SOC15(GC, 0, mmSE_CAC_IND_INDEX, (reg));
 322	WREG32_SOC15(GC, 0, mmSE_CAC_IND_DATA, (v));
 323	spin_unlock_irqrestore(&adev->se_cac_idx_lock, flags);
 324}
 325
 326static u32 soc15_get_config_memsize(struct amdgpu_device *adev)
 327{
 328	return adev->nbio.funcs->get_memsize(adev);
 329}
 330
 331static u32 soc15_get_xclk(struct amdgpu_device *adev)
 332{
 333	u32 reference_clock = adev->clock.spll.reference_freq;
 334
 335	if (adev->asic_type == CHIP_RENOIR)
 
 
 336		return 10000;
 337	if (adev->asic_type == CHIP_RAVEN)
 
 338		return reference_clock / 4;
 339
 340	return reference_clock;
 341}
 342
 343
 344void soc15_grbm_select(struct amdgpu_device *adev,
 345		     u32 me, u32 pipe, u32 queue, u32 vmid)
 346{
 347	u32 grbm_gfx_cntl = 0;
 348	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, PIPEID, pipe);
 349	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, MEID, me);
 350	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, VMID, vmid);
 351	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, QUEUEID, queue);
 352
 353	WREG32_SOC15_RLC_SHADOW(GC, 0, mmGRBM_GFX_CNTL, grbm_gfx_cntl);
 354}
 355
 356static void soc15_vga_set_state(struct amdgpu_device *adev, bool state)
 357{
 358	/* todo */
 359}
 360
 361static bool soc15_read_disabled_bios(struct amdgpu_device *adev)
 362{
 363	/* todo */
 364	return false;
 365}
 366
 367static bool soc15_read_bios_from_rom(struct amdgpu_device *adev,
 368				     u8 *bios, u32 length_bytes)
 369{
 370	u32 *dw_ptr;
 371	u32 i, length_dw;
 372	uint32_t rom_index_offset;
 373	uint32_t rom_data_offset;
 374
 375	if (bios == NULL)
 376		return false;
 377	if (length_bytes == 0)
 378		return false;
 379	/* APU vbios image is part of sbios image */
 380	if (adev->flags & AMD_IS_APU)
 381		return false;
 382
 383	dw_ptr = (u32 *)bios;
 384	length_dw = ALIGN(length_bytes, 4) / 4;
 385
 386	rom_index_offset =
 387		adev->smuio.funcs->get_rom_index_offset(adev);
 388	rom_data_offset =
 389		adev->smuio.funcs->get_rom_data_offset(adev);
 390
 391	/* set rom index to 0 */
 392	WREG32(rom_index_offset, 0);
 393	/* read out the rom data */
 394	for (i = 0; i < length_dw; i++)
 395		dw_ptr[i] = RREG32(rom_data_offset);
 396
 397	return true;
 398}
 399
 400static struct soc15_allowed_register_entry soc15_allowed_read_registers[] = {
 401	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS)},
 402	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS2)},
 403	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE0)},
 404	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE1)},
 405	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE2)},
 406	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE3)},
 407	{ SOC15_REG_ENTRY(SDMA0, 0, mmSDMA0_STATUS_REG)},
 408	{ SOC15_REG_ENTRY(SDMA1, 0, mmSDMA1_STATUS_REG)},
 409	{ SOC15_REG_ENTRY(GC, 0, mmCP_STAT)},
 410	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT1)},
 411	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT2)},
 412	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT3)},
 413	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_BUSY_STAT)},
 414	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STALLED_STAT1)},
 415	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STATUS)},
 416	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_BUSY_STAT)},
 417	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STALLED_STAT1)},
 418	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STATUS)},
 419	{ SOC15_REG_ENTRY(GC, 0, mmGB_ADDR_CONFIG)},
 420	{ SOC15_REG_ENTRY(GC, 0, mmDB_DEBUG2)},
 421};
 422
 423static uint32_t soc15_read_indexed_register(struct amdgpu_device *adev, u32 se_num,
 424					 u32 sh_num, u32 reg_offset)
 425{
 426	uint32_t val;
 427
 428	mutex_lock(&adev->grbm_idx_mutex);
 429	if (se_num != 0xffffffff || sh_num != 0xffffffff)
 430		amdgpu_gfx_select_se_sh(adev, se_num, sh_num, 0xffffffff);
 431
 432	val = RREG32(reg_offset);
 433
 434	if (se_num != 0xffffffff || sh_num != 0xffffffff)
 435		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
 436	mutex_unlock(&adev->grbm_idx_mutex);
 437	return val;
 438}
 439
 440static uint32_t soc15_get_register_value(struct amdgpu_device *adev,
 441					 bool indexed, u32 se_num,
 442					 u32 sh_num, u32 reg_offset)
 443{
 444	if (indexed) {
 445		return soc15_read_indexed_register(adev, se_num, sh_num, reg_offset);
 446	} else {
 447		if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmGB_ADDR_CONFIG))
 448			return adev->gfx.config.gb_addr_config;
 449		else if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmDB_DEBUG2))
 450			return adev->gfx.config.db_debug2;
 451		return RREG32(reg_offset);
 452	}
 453}
 454
 455static int soc15_read_register(struct amdgpu_device *adev, u32 se_num,
 456			    u32 sh_num, u32 reg_offset, u32 *value)
 457{
 458	uint32_t i;
 459	struct soc15_allowed_register_entry  *en;
 460
 461	*value = 0;
 462	for (i = 0; i < ARRAY_SIZE(soc15_allowed_read_registers); i++) {
 463		en = &soc15_allowed_read_registers[i];
 464		if (adev->reg_offset[en->hwip][en->inst] &&
 465			reg_offset != (adev->reg_offset[en->hwip][en->inst][en->seg]
 
 466					+ en->reg_offset))
 467			continue;
 468
 469		*value = soc15_get_register_value(adev,
 470						  soc15_allowed_read_registers[i].grbm_indexed,
 471						  se_num, sh_num, reg_offset);
 472		return 0;
 473	}
 474	return -EINVAL;
 475}
 476
 477
 478/**
 479 * soc15_program_register_sequence - program an array of registers.
 480 *
 481 * @adev: amdgpu_device pointer
 482 * @regs: pointer to the register array
 483 * @array_size: size of the register array
 484 *
 485 * Programs an array or registers with and and or masks.
 486 * This is a helper for setting golden registers.
 487 */
 488
 489void soc15_program_register_sequence(struct amdgpu_device *adev,
 490					     const struct soc15_reg_golden *regs,
 491					     const u32 array_size)
 492{
 493	const struct soc15_reg_golden *entry;
 494	u32 tmp, reg;
 495	int i;
 496
 497	for (i = 0; i < array_size; ++i) {
 498		entry = &regs[i];
 499		reg =  adev->reg_offset[entry->hwip][entry->instance][entry->segment] + entry->reg;
 500
 501		if (entry->and_mask == 0xffffffff) {
 502			tmp = entry->or_mask;
 503		} else {
 504			tmp = (entry->hwip == GC_HWIP) ?
 505				RREG32_SOC15_IP(GC, reg) : RREG32(reg);
 506
 507			tmp &= ~(entry->and_mask);
 508			tmp |= (entry->or_mask & entry->and_mask);
 509		}
 510
 511		if (reg == SOC15_REG_OFFSET(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_3) ||
 512			reg == SOC15_REG_OFFSET(GC, 0, mmPA_SC_ENHANCE) ||
 513			reg == SOC15_REG_OFFSET(GC, 0, mmPA_SC_ENHANCE_1) ||
 514			reg == SOC15_REG_OFFSET(GC, 0, mmSH_MEM_CONFIG))
 515			WREG32_RLC(reg, tmp);
 516		else
 517			(entry->hwip == GC_HWIP) ?
 518				WREG32_SOC15_IP(GC, reg, tmp) : WREG32(reg, tmp);
 519
 520	}
 521
 522}
 523
 524static int soc15_asic_baco_reset(struct amdgpu_device *adev)
 525{
 526	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
 527	int ret = 0;
 528
 529	/* avoid NBIF got stuck when do RAS recovery in BACO reset */
 530	if (ras && adev->ras_enabled)
 531		adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
 532
 533	ret = amdgpu_dpm_baco_reset(adev);
 534	if (ret)
 535		return ret;
 536
 537	/* re-enable doorbell interrupt after BACO exit */
 538	if (ras && adev->ras_enabled)
 539		adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
 540
 541	return 0;
 542}
 543
 544static enum amd_reset_method
 545soc15_asic_reset_method(struct amdgpu_device *adev)
 546{
 547	bool baco_reset = false;
 548	bool connected_to_cpu = false;
 549	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
 550
 551        if (adev->gmc.xgmi.supported && adev->gmc.xgmi.connected_to_cpu)
 552                connected_to_cpu = true;
 553
 554	if (amdgpu_reset_method == AMD_RESET_METHOD_MODE1 ||
 555	    amdgpu_reset_method == AMD_RESET_METHOD_MODE2 ||
 556	    amdgpu_reset_method == AMD_RESET_METHOD_BACO ||
 557	    amdgpu_reset_method == AMD_RESET_METHOD_PCI) {
 558		/* If connected to cpu, driver only support mode2 */
 559                if (connected_to_cpu)
 560                        return AMD_RESET_METHOD_MODE2;
 561                return amdgpu_reset_method;
 562        }
 563
 564	if (amdgpu_reset_method != -1)
 565		dev_warn(adev->dev, "Specified reset method:%d isn't supported, using AUTO instead.\n",
 566				  amdgpu_reset_method);
 567
 568	switch (adev->asic_type) {
 569	case CHIP_RAVEN:
 570	case CHIP_RENOIR:
 
 
 571		return AMD_RESET_METHOD_MODE2;
 572	case CHIP_VEGA10:
 573	case CHIP_VEGA12:
 574	case CHIP_ARCTURUS:
 575		baco_reset = amdgpu_dpm_is_baco_supported(adev);
 576		break;
 577	case CHIP_VEGA20:
 578		if (adev->psp.sos_fw_version >= 0x80067)
 
 
 
 
 
 
 579			baco_reset = amdgpu_dpm_is_baco_supported(adev);
 580
 581		/*
 582		 * 1. PMFW version > 0x284300: all cases use baco
 583		 * 2. PMFW version <= 0x284300: only sGPU w/o RAS use baco
 584		 */
 585		if (ras && adev->ras_enabled &&
 586		    adev->pm.fw_version <= 0x283400)
 587			baco_reset = false;
 588		break;
 589	case CHIP_ALDEBARAN:
 590		 /*
 591		 * 1.connected to cpu: driver issue mode2 reset
 592		 * 2.discret gpu: driver issue mode1 reset
 593		 */
 594		if (connected_to_cpu)
 595			return AMD_RESET_METHOD_MODE2;
 596		break;
 
 
 
 
 
 
 
 
 
 
 
 597	default:
 598		break;
 599	}
 600
 601	if (baco_reset)
 602		return AMD_RESET_METHOD_BACO;
 603	else
 604		return AMD_RESET_METHOD_MODE1;
 605}
 606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 607static int soc15_asic_reset(struct amdgpu_device *adev)
 608{
 609	/* original raven doesn't have full asic reset */
 610	if ((adev->apu_flags & AMD_APU_IS_RAVEN) &&
 611	    !(adev->apu_flags & AMD_APU_IS_RAVEN2))
 
 
 
 
 
 612		return 0;
 613
 614	switch (soc15_asic_reset_method(adev)) {
 615	case AMD_RESET_METHOD_PCI:
 616		dev_info(adev->dev, "PCI reset\n");
 617		return amdgpu_device_pci_reset(adev);
 618	case AMD_RESET_METHOD_BACO:
 619		dev_info(adev->dev, "BACO reset\n");
 620		return soc15_asic_baco_reset(adev);
 621	case AMD_RESET_METHOD_MODE2:
 622		dev_info(adev->dev, "MODE2 reset\n");
 623		return amdgpu_dpm_mode2_reset(adev);
 624	default:
 625		dev_info(adev->dev, "MODE1 reset\n");
 626		return amdgpu_device_mode1_reset(adev);
 627	}
 628}
 629
 630static bool soc15_supports_baco(struct amdgpu_device *adev)
 631{
 632	switch (adev->asic_type) {
 633	case CHIP_VEGA10:
 634	case CHIP_VEGA12:
 635	case CHIP_ARCTURUS:
 636		return amdgpu_dpm_is_baco_supported(adev);
 637	case CHIP_VEGA20:
 638		if (adev->psp.sos_fw_version >= 0x80067)
 
 639			return amdgpu_dpm_is_baco_supported(adev);
 640		return false;
 
 641	default:
 642		return false;
 643	}
 644}
 645
 646/*static int soc15_set_uvd_clock(struct amdgpu_device *adev, u32 clock,
 647			u32 cntl_reg, u32 status_reg)
 648{
 649	return 0;
 650}*/
 651
 652static int soc15_set_uvd_clocks(struct amdgpu_device *adev, u32 vclk, u32 dclk)
 653{
 654	/*int r;
 655
 656	r = soc15_set_uvd_clock(adev, vclk, ixCG_VCLK_CNTL, ixCG_VCLK_STATUS);
 657	if (r)
 658		return r;
 659
 660	r = soc15_set_uvd_clock(adev, dclk, ixCG_DCLK_CNTL, ixCG_DCLK_STATUS);
 661	*/
 662	return 0;
 663}
 664
 665static int soc15_set_vce_clocks(struct amdgpu_device *adev, u32 evclk, u32 ecclk)
 666{
 667	/* todo */
 668
 669	return 0;
 670}
 671
 672static void soc15_pcie_gen3_enable(struct amdgpu_device *adev)
 673{
 674	if (pci_is_root_bus(adev->pdev->bus))
 675		return;
 676
 677	if (amdgpu_pcie_gen2 == 0)
 678		return;
 679
 680	if (adev->flags & AMD_IS_APU)
 681		return;
 682
 683	if (!(adev->pm.pcie_gen_mask & (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
 684					CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)))
 685		return;
 686
 687	/* todo */
 688}
 689
 690static void soc15_program_aspm(struct amdgpu_device *adev)
 691{
 692	if (!amdgpu_aspm)
 693		return;
 694
 695	if (!(adev->flags & AMD_IS_APU) &&
 696	    (adev->nbio.funcs->program_aspm))
 697		adev->nbio.funcs->program_aspm(adev);
 698}
 699
 700static void soc15_enable_doorbell_aperture(struct amdgpu_device *adev,
 701					   bool enable)
 702{
 703	adev->nbio.funcs->enable_doorbell_aperture(adev, enable);
 704	adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, enable);
 705}
 706
 707static const struct amdgpu_ip_block_version vega10_common_ip_block =
 708{
 709	.type = AMD_IP_BLOCK_TYPE_COMMON,
 710	.major = 2,
 711	.minor = 0,
 712	.rev = 0,
 713	.funcs = &soc15_common_ip_funcs,
 714};
 715
 716static uint32_t soc15_get_rev_id(struct amdgpu_device *adev)
 717{
 718	return adev->nbio.funcs->get_rev_id(adev);
 719}
 720
 721static void soc15_reg_base_init(struct amdgpu_device *adev)
 722{
 723	int r;
 724
 725	/* Set IP register base before any HW register access */
 726	switch (adev->asic_type) {
 727	case CHIP_VEGA10:
 728	case CHIP_VEGA12:
 729	case CHIP_RAVEN:
 730		vega10_reg_base_init(adev);
 731		break;
 732	case CHIP_RENOIR:
 733		/* It's safe to do ip discovery here for Renior,
 734		 * it doesn't support SRIOV. */
 735		if (amdgpu_discovery) {
 736			r = amdgpu_discovery_reg_base_init(adev);
 737			if (r == 0)
 738				break;
 739			DRM_WARN("failed to init reg base from ip discovery table, "
 740				 "fallback to legacy init method\n");
 741		}
 742		vega10_reg_base_init(adev);
 743		break;
 744	case CHIP_VEGA20:
 745		vega20_reg_base_init(adev);
 746		break;
 747	case CHIP_ARCTURUS:
 748		arct_reg_base_init(adev);
 749		break;
 750	case CHIP_ALDEBARAN:
 751		aldebaran_reg_base_init(adev);
 752		break;
 753	default:
 754		DRM_ERROR("Unsupported asic type: %d!\n", adev->asic_type);
 755		break;
 756	}
 757}
 758
 759void soc15_set_virt_ops(struct amdgpu_device *adev)
 760{
 761	adev->virt.ops = &xgpu_ai_virt_ops;
 762
 763	/* init soc15 reg base early enough so we can
 764	 * request request full access for sriov before
 765	 * set_ip_blocks. */
 766	soc15_reg_base_init(adev);
 767}
 768
 769int soc15_set_ip_blocks(struct amdgpu_device *adev)
 770{
 771	/* for bare metal case */
 772	if (!amdgpu_sriov_vf(adev))
 773		soc15_reg_base_init(adev);
 774
 775	if (adev->flags & AMD_IS_APU) {
 776		adev->nbio.funcs = &nbio_v7_0_funcs;
 777		adev->nbio.hdp_flush_reg = &nbio_v7_0_hdp_flush_reg;
 778	} else if (adev->asic_type == CHIP_VEGA20 ||
 779		   adev->asic_type == CHIP_ARCTURUS ||
 780		   adev->asic_type == CHIP_ALDEBARAN) {
 781		adev->nbio.funcs = &nbio_v7_4_funcs;
 782		adev->nbio.hdp_flush_reg = &nbio_v7_4_hdp_flush_reg;
 783	} else {
 784		adev->nbio.funcs = &nbio_v6_1_funcs;
 785		adev->nbio.hdp_flush_reg = &nbio_v6_1_hdp_flush_reg;
 786	}
 787	adev->hdp.funcs = &hdp_v4_0_funcs;
 788
 789	if (adev->asic_type == CHIP_VEGA20 ||
 790	    adev->asic_type == CHIP_ARCTURUS ||
 791	    adev->asic_type == CHIP_ALDEBARAN)
 792		adev->df.funcs = &df_v3_6_funcs;
 793	else
 794		adev->df.funcs = &df_v1_7_funcs;
 795
 796	if (adev->asic_type == CHIP_VEGA20 ||
 797	    adev->asic_type == CHIP_ARCTURUS)
 798		adev->smuio.funcs = &smuio_v11_0_funcs;
 799	else if (adev->asic_type == CHIP_ALDEBARAN)
 800		adev->smuio.funcs = &smuio_v13_0_funcs;
 801	else
 802		adev->smuio.funcs = &smuio_v9_0_funcs;
 803
 804	adev->rev_id = soc15_get_rev_id(adev);
 805
 806	switch (adev->asic_type) {
 807	case CHIP_VEGA10:
 808	case CHIP_VEGA12:
 809	case CHIP_VEGA20:
 810		amdgpu_device_ip_block_add(adev, &vega10_common_ip_block);
 811		amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block);
 812
 813		/* For Vega10 SR-IOV, PSP need to be initialized before IH */
 814		if (amdgpu_sriov_vf(adev)) {
 815			if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) {
 816				if (adev->asic_type == CHIP_VEGA20)
 817					amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
 818				else
 819					amdgpu_device_ip_block_add(adev, &psp_v3_1_ip_block);
 820			}
 821			if (adev->asic_type == CHIP_VEGA20)
 822				amdgpu_device_ip_block_add(adev, &vega20_ih_ip_block);
 823			else
 824				amdgpu_device_ip_block_add(adev, &vega10_ih_ip_block);
 825		} else {
 826			if (adev->asic_type == CHIP_VEGA20)
 827				amdgpu_device_ip_block_add(adev, &vega20_ih_ip_block);
 828			else
 829				amdgpu_device_ip_block_add(adev, &vega10_ih_ip_block);
 830			if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) {
 831				if (adev->asic_type == CHIP_VEGA20)
 832					amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
 833				else
 834					amdgpu_device_ip_block_add(adev, &psp_v3_1_ip_block);
 835			}
 836		}
 837		amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block);
 838		amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block);
 839		if (is_support_sw_smu(adev)) {
 840			if (!amdgpu_sriov_vf(adev))
 841				amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
 842		} else {
 843			amdgpu_device_ip_block_add(adev, &pp_smu_ip_block);
 844		}
 845		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
 846			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
 847#if defined(CONFIG_DRM_AMD_DC)
 848		else if (amdgpu_device_has_dc_support(adev))
 849			amdgpu_device_ip_block_add(adev, &dm_ip_block);
 850#endif
 851		if (!(adev->asic_type == CHIP_VEGA20 && amdgpu_sriov_vf(adev))) {
 852			amdgpu_device_ip_block_add(adev, &uvd_v7_0_ip_block);
 853			amdgpu_device_ip_block_add(adev, &vce_v4_0_ip_block);
 854		}
 855		break;
 856	case CHIP_RAVEN:
 857		amdgpu_device_ip_block_add(adev, &vega10_common_ip_block);
 858		amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block);
 859		amdgpu_device_ip_block_add(adev, &vega10_ih_ip_block);
 860		if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
 861			amdgpu_device_ip_block_add(adev, &psp_v10_0_ip_block);
 862		amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block);
 863		amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block);
 864		amdgpu_device_ip_block_add(adev, &pp_smu_ip_block);
 865		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
 866			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
 867#if defined(CONFIG_DRM_AMD_DC)
 868		else if (amdgpu_device_has_dc_support(adev))
 869			amdgpu_device_ip_block_add(adev, &dm_ip_block);
 870#endif
 871		amdgpu_device_ip_block_add(adev, &vcn_v1_0_ip_block);
 872		break;
 873	case CHIP_ARCTURUS:
 874		amdgpu_device_ip_block_add(adev, &vega10_common_ip_block);
 875		amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block);
 876
 877		if (amdgpu_sriov_vf(adev)) {
 878			if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
 879				amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
 880			amdgpu_device_ip_block_add(adev, &vega20_ih_ip_block);
 881		} else {
 882			amdgpu_device_ip_block_add(adev, &vega20_ih_ip_block);
 883			if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
 884				amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
 885		}
 886
 887		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
 888			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
 889		amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block);
 890		amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block);
 891		amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
 892
 893		if (amdgpu_sriov_vf(adev)) {
 894			if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
 895				amdgpu_device_ip_block_add(adev, &vcn_v2_5_ip_block);
 896		} else {
 897			amdgpu_device_ip_block_add(adev, &vcn_v2_5_ip_block);
 898		}
 899		if (!amdgpu_sriov_vf(adev))
 900			amdgpu_device_ip_block_add(adev, &jpeg_v2_5_ip_block);
 901		break;
 902	case CHIP_RENOIR:
 903		amdgpu_device_ip_block_add(adev, &vega10_common_ip_block);
 904		amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block);
 905		amdgpu_device_ip_block_add(adev, &vega10_ih_ip_block);
 906		if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
 907			amdgpu_device_ip_block_add(adev, &psp_v12_0_ip_block);
 908		amdgpu_device_ip_block_add(adev, &smu_v12_0_ip_block);
 909		amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block);
 910		amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block);
 911		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
 912			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
 913#if defined(CONFIG_DRM_AMD_DC)
 914                else if (amdgpu_device_has_dc_support(adev))
 915			amdgpu_device_ip_block_add(adev, &dm_ip_block);
 916#endif
 917		amdgpu_device_ip_block_add(adev, &vcn_v2_0_ip_block);
 918		amdgpu_device_ip_block_add(adev, &jpeg_v2_0_ip_block);
 919		break;
 920	case CHIP_ALDEBARAN:
 921		amdgpu_device_ip_block_add(adev, &vega10_common_ip_block);
 922		amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block);
 923
 924		if (amdgpu_sriov_vf(adev)) {
 925			if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
 926				amdgpu_device_ip_block_add(adev, &psp_v13_0_ip_block);
 927			amdgpu_device_ip_block_add(adev, &vega20_ih_ip_block);
 928		} else {
 929			amdgpu_device_ip_block_add(adev, &vega20_ih_ip_block);
 930			if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
 931				amdgpu_device_ip_block_add(adev, &psp_v13_0_ip_block);
 932		}
 933
 934		amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block);
 935		amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block);
 936
 937		amdgpu_device_ip_block_add(adev, &smu_v13_0_ip_block);
 938		amdgpu_device_ip_block_add(adev, &vcn_v2_6_ip_block);
 939		amdgpu_device_ip_block_add(adev, &jpeg_v2_6_ip_block);
 940		break;
 941	default:
 942		return -EINVAL;
 943	}
 944
 945	return 0;
 946}
 947
 948static bool soc15_need_full_reset(struct amdgpu_device *adev)
 949{
 950	/* change this when we implement soft reset */
 951	return true;
 952}
 953
 954static void soc15_get_pcie_usage(struct amdgpu_device *adev, uint64_t *count0,
 955				 uint64_t *count1)
 956{
 957	uint32_t perfctr = 0;
 958	uint64_t cnt0_of, cnt1_of;
 959	int tmp;
 960
 961	/* This reports 0 on APUs, so return to avoid writing/reading registers
 962	 * that may or may not be different from their GPU counterparts
 963	 */
 964	if (adev->flags & AMD_IS_APU)
 965		return;
 966
 967	/* Set the 2 events that we wish to watch, defined above */
 968	/* Reg 40 is # received msgs */
 969	/* Reg 104 is # of posted requests sent */
 970	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK, EVENT0_SEL, 40);
 971	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK, EVENT1_SEL, 104);
 972
 973	/* Write to enable desired perf counters */
 974	WREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK, perfctr);
 975	/* Zero out and enable the perf counters
 976	 * Write 0x5:
 977	 * Bit 0 = Start all counters(1)
 978	 * Bit 2 = Global counter reset enable(1)
 979	 */
 980	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000005);
 981
 982	msleep(1000);
 983
 984	/* Load the shadow and disable the perf counters
 985	 * Write 0x2:
 986	 * Bit 0 = Stop counters(0)
 987	 * Bit 1 = Load the shadow counters(1)
 988	 */
 989	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000002);
 990
 991	/* Read register values to get any >32bit overflow */
 992	tmp = RREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK);
 993	cnt0_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK, COUNTER0_UPPER);
 994	cnt1_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK, COUNTER1_UPPER);
 995
 996	/* Get the values and add the overflow */
 997	*count0 = RREG32_PCIE(smnPCIE_PERF_COUNT0_TXCLK) | (cnt0_of << 32);
 998	*count1 = RREG32_PCIE(smnPCIE_PERF_COUNT1_TXCLK) | (cnt1_of << 32);
 999}
1000
1001static void vega20_get_pcie_usage(struct amdgpu_device *adev, uint64_t *count0,
1002				 uint64_t *count1)
1003{
1004	uint32_t perfctr = 0;
1005	uint64_t cnt0_of, cnt1_of;
1006	int tmp;
1007
1008	/* This reports 0 on APUs, so return to avoid writing/reading registers
1009	 * that may or may not be different from their GPU counterparts
1010	 */
1011	if (adev->flags & AMD_IS_APU)
1012		return;
1013
1014	/* Set the 2 events that we wish to watch, defined above */
1015	/* Reg 40 is # received msgs */
1016	/* Reg 108 is # of posted requests sent on VG20 */
1017	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK3,
1018				EVENT0_SEL, 40);
1019	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK3,
1020				EVENT1_SEL, 108);
1021
1022	/* Write to enable desired perf counters */
1023	WREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK3, perfctr);
1024	/* Zero out and enable the perf counters
1025	 * Write 0x5:
1026	 * Bit 0 = Start all counters(1)
1027	 * Bit 2 = Global counter reset enable(1)
1028	 */
1029	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000005);
1030
1031	msleep(1000);
1032
1033	/* Load the shadow and disable the perf counters
1034	 * Write 0x2:
1035	 * Bit 0 = Stop counters(0)
1036	 * Bit 1 = Load the shadow counters(1)
1037	 */
1038	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000002);
1039
1040	/* Read register values to get any >32bit overflow */
1041	tmp = RREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK3);
1042	cnt0_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK3, COUNTER0_UPPER);
1043	cnt1_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK3, COUNTER1_UPPER);
1044
1045	/* Get the values and add the overflow */
1046	*count0 = RREG32_PCIE(smnPCIE_PERF_COUNT0_TXCLK3) | (cnt0_of << 32);
1047	*count1 = RREG32_PCIE(smnPCIE_PERF_COUNT1_TXCLK3) | (cnt1_of << 32);
1048}
1049
1050static bool soc15_need_reset_on_init(struct amdgpu_device *adev)
1051{
1052	u32 sol_reg;
1053
 
 
 
 
1054	/* Just return false for soc15 GPUs.  Reset does not seem to
1055	 * be necessary.
1056	 */
1057	if (!amdgpu_passthrough(adev))
1058		return false;
1059
1060	if (adev->flags & AMD_IS_APU)
1061		return false;
1062
1063	/* Check sOS sign of life register to confirm sys driver and sOS
1064	 * are already been loaded.
1065	 */
1066	sol_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_81);
1067	if (sol_reg)
1068		return true;
1069
1070	return false;
1071}
1072
1073static uint64_t soc15_get_pcie_replay_count(struct amdgpu_device *adev)
1074{
1075	uint64_t nak_r, nak_g;
1076
1077	/* Get the number of NAKs received and generated */
1078	nak_r = RREG32_PCIE(smnPCIE_RX_NUM_NAK);
1079	nak_g = RREG32_PCIE(smnPCIE_RX_NUM_NAK_GENERATED);
1080
1081	/* Add the total number of NAKs, i.e the number of replays */
1082	return (nak_r + nak_g);
1083}
1084
1085static void soc15_pre_asic_init(struct amdgpu_device *adev)
1086{
1087	gmc_v9_0_restore_registers(adev);
1088}
1089
1090static const struct amdgpu_asic_funcs soc15_asic_funcs =
1091{
1092	.read_disabled_bios = &soc15_read_disabled_bios,
1093	.read_bios_from_rom = &soc15_read_bios_from_rom,
1094	.read_register = &soc15_read_register,
1095	.reset = &soc15_asic_reset,
1096	.reset_method = &soc15_asic_reset_method,
1097	.set_vga_state = &soc15_vga_set_state,
1098	.get_xclk = &soc15_get_xclk,
1099	.set_uvd_clocks = &soc15_set_uvd_clocks,
1100	.set_vce_clocks = &soc15_set_vce_clocks,
1101	.get_config_memsize = &soc15_get_config_memsize,
1102	.need_full_reset = &soc15_need_full_reset,
1103	.init_doorbell_index = &vega10_doorbell_index_init,
1104	.get_pcie_usage = &soc15_get_pcie_usage,
1105	.need_reset_on_init = &soc15_need_reset_on_init,
1106	.get_pcie_replay_count = &soc15_get_pcie_replay_count,
1107	.supports_baco = &soc15_supports_baco,
1108	.pre_asic_init = &soc15_pre_asic_init,
1109	.query_video_codecs = &soc15_query_video_codecs,
1110};
1111
1112static const struct amdgpu_asic_funcs vega20_asic_funcs =
1113{
1114	.read_disabled_bios = &soc15_read_disabled_bios,
1115	.read_bios_from_rom = &soc15_read_bios_from_rom,
1116	.read_register = &soc15_read_register,
1117	.reset = &soc15_asic_reset,
1118	.reset_method = &soc15_asic_reset_method,
1119	.set_vga_state = &soc15_vga_set_state,
1120	.get_xclk = &soc15_get_xclk,
1121	.set_uvd_clocks = &soc15_set_uvd_clocks,
1122	.set_vce_clocks = &soc15_set_vce_clocks,
1123	.get_config_memsize = &soc15_get_config_memsize,
1124	.need_full_reset = &soc15_need_full_reset,
1125	.init_doorbell_index = &vega20_doorbell_index_init,
1126	.get_pcie_usage = &vega20_get_pcie_usage,
1127	.need_reset_on_init = &soc15_need_reset_on_init,
1128	.get_pcie_replay_count = &soc15_get_pcie_replay_count,
1129	.supports_baco = &soc15_supports_baco,
1130	.pre_asic_init = &soc15_pre_asic_init,
1131	.query_video_codecs = &soc15_query_video_codecs,
1132};
1133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1134static int soc15_common_early_init(void *handle)
1135{
1136#define MMIO_REG_HOLE_OFFSET (0x80000 - PAGE_SIZE)
1137	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1138
1139	adev->rmmio_remap.reg_offset = MMIO_REG_HOLE_OFFSET;
1140	adev->rmmio_remap.bus_addr = adev->rmmio_base + MMIO_REG_HOLE_OFFSET;
 
 
1141	adev->smc_rreg = NULL;
1142	adev->smc_wreg = NULL;
1143	adev->pcie_rreg = &soc15_pcie_rreg;
1144	adev->pcie_wreg = &soc15_pcie_wreg;
1145	adev->pcie_rreg64 = &soc15_pcie_rreg64;
1146	adev->pcie_wreg64 = &soc15_pcie_wreg64;
 
 
 
 
1147	adev->uvd_ctx_rreg = &soc15_uvd_ctx_rreg;
1148	adev->uvd_ctx_wreg = &soc15_uvd_ctx_wreg;
1149	adev->didt_rreg = &soc15_didt_rreg;
1150	adev->didt_wreg = &soc15_didt_wreg;
1151	adev->gc_cac_rreg = &soc15_gc_cac_rreg;
1152	adev->gc_cac_wreg = &soc15_gc_cac_wreg;
1153	adev->se_cac_rreg = &soc15_se_cac_rreg;
1154	adev->se_cac_wreg = &soc15_se_cac_wreg;
1155
1156
1157	adev->external_rev_id = 0xFF;
1158	switch (adev->asic_type) {
1159	case CHIP_VEGA10:
 
 
 
1160		adev->asic_funcs = &soc15_asic_funcs;
1161		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1162			AMD_CG_SUPPORT_GFX_MGLS |
1163			AMD_CG_SUPPORT_GFX_RLC_LS |
1164			AMD_CG_SUPPORT_GFX_CP_LS |
1165			AMD_CG_SUPPORT_GFX_3D_CGCG |
1166			AMD_CG_SUPPORT_GFX_3D_CGLS |
1167			AMD_CG_SUPPORT_GFX_CGCG |
1168			AMD_CG_SUPPORT_GFX_CGLS |
1169			AMD_CG_SUPPORT_BIF_MGCG |
1170			AMD_CG_SUPPORT_BIF_LS |
1171			AMD_CG_SUPPORT_HDP_LS |
1172			AMD_CG_SUPPORT_DRM_MGCG |
1173			AMD_CG_SUPPORT_DRM_LS |
1174			AMD_CG_SUPPORT_ROM_MGCG |
1175			AMD_CG_SUPPORT_DF_MGCG |
1176			AMD_CG_SUPPORT_SDMA_MGCG |
1177			AMD_CG_SUPPORT_SDMA_LS |
1178			AMD_CG_SUPPORT_MC_MGCG |
1179			AMD_CG_SUPPORT_MC_LS;
1180		adev->pg_flags = 0;
1181		adev->external_rev_id = 0x1;
1182		break;
1183	case CHIP_VEGA12:
1184		adev->asic_funcs = &soc15_asic_funcs;
1185		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1186			AMD_CG_SUPPORT_GFX_MGLS |
1187			AMD_CG_SUPPORT_GFX_CGCG |
1188			AMD_CG_SUPPORT_GFX_CGLS |
1189			AMD_CG_SUPPORT_GFX_3D_CGCG |
1190			AMD_CG_SUPPORT_GFX_3D_CGLS |
1191			AMD_CG_SUPPORT_GFX_CP_LS |
1192			AMD_CG_SUPPORT_MC_LS |
1193			AMD_CG_SUPPORT_MC_MGCG |
1194			AMD_CG_SUPPORT_SDMA_MGCG |
1195			AMD_CG_SUPPORT_SDMA_LS |
1196			AMD_CG_SUPPORT_BIF_MGCG |
1197			AMD_CG_SUPPORT_BIF_LS |
1198			AMD_CG_SUPPORT_HDP_MGCG |
1199			AMD_CG_SUPPORT_HDP_LS |
1200			AMD_CG_SUPPORT_ROM_MGCG |
1201			AMD_CG_SUPPORT_VCE_MGCG |
1202			AMD_CG_SUPPORT_UVD_MGCG;
1203		adev->pg_flags = 0;
1204		adev->external_rev_id = adev->rev_id + 0x14;
1205		break;
1206	case CHIP_VEGA20:
1207		adev->asic_funcs = &vega20_asic_funcs;
1208		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1209			AMD_CG_SUPPORT_GFX_MGLS |
1210			AMD_CG_SUPPORT_GFX_CGCG |
1211			AMD_CG_SUPPORT_GFX_CGLS |
1212			AMD_CG_SUPPORT_GFX_3D_CGCG |
1213			AMD_CG_SUPPORT_GFX_3D_CGLS |
1214			AMD_CG_SUPPORT_GFX_CP_LS |
1215			AMD_CG_SUPPORT_MC_LS |
1216			AMD_CG_SUPPORT_MC_MGCG |
1217			AMD_CG_SUPPORT_SDMA_MGCG |
1218			AMD_CG_SUPPORT_SDMA_LS |
1219			AMD_CG_SUPPORT_BIF_MGCG |
1220			AMD_CG_SUPPORT_BIF_LS |
1221			AMD_CG_SUPPORT_HDP_MGCG |
1222			AMD_CG_SUPPORT_HDP_LS |
1223			AMD_CG_SUPPORT_ROM_MGCG |
1224			AMD_CG_SUPPORT_VCE_MGCG |
1225			AMD_CG_SUPPORT_UVD_MGCG;
1226		adev->pg_flags = 0;
1227		adev->external_rev_id = adev->rev_id + 0x28;
1228		break;
1229	case CHIP_RAVEN:
 
1230		adev->asic_funcs = &soc15_asic_funcs;
1231
1232		if (adev->rev_id >= 0x8)
1233			adev->apu_flags |= AMD_APU_IS_RAVEN2;
1234
1235		if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1236			adev->external_rev_id = adev->rev_id + 0x79;
1237		else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1238			adev->external_rev_id = adev->rev_id + 0x41;
1239		else if (adev->rev_id == 1)
1240			adev->external_rev_id = adev->rev_id + 0x20;
1241		else
1242			adev->external_rev_id = adev->rev_id + 0x01;
1243
1244		if (adev->apu_flags & AMD_APU_IS_RAVEN2) {
1245			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1246				AMD_CG_SUPPORT_GFX_MGLS |
1247				AMD_CG_SUPPORT_GFX_CP_LS |
1248				AMD_CG_SUPPORT_GFX_3D_CGCG |
1249				AMD_CG_SUPPORT_GFX_3D_CGLS |
1250				AMD_CG_SUPPORT_GFX_CGCG |
1251				AMD_CG_SUPPORT_GFX_CGLS |
1252				AMD_CG_SUPPORT_BIF_LS |
1253				AMD_CG_SUPPORT_HDP_LS |
1254				AMD_CG_SUPPORT_MC_MGCG |
1255				AMD_CG_SUPPORT_MC_LS |
1256				AMD_CG_SUPPORT_SDMA_MGCG |
1257				AMD_CG_SUPPORT_SDMA_LS |
1258				AMD_CG_SUPPORT_VCN_MGCG;
1259
1260			adev->pg_flags = AMD_PG_SUPPORT_SDMA | AMD_PG_SUPPORT_VCN;
1261		} else if (adev->apu_flags & AMD_APU_IS_PICASSO) {
1262			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1263				AMD_CG_SUPPORT_GFX_MGLS |
1264				AMD_CG_SUPPORT_GFX_CP_LS |
1265				AMD_CG_SUPPORT_GFX_3D_CGLS |
1266				AMD_CG_SUPPORT_GFX_CGCG |
1267				AMD_CG_SUPPORT_GFX_CGLS |
1268				AMD_CG_SUPPORT_BIF_LS |
1269				AMD_CG_SUPPORT_HDP_LS |
1270				AMD_CG_SUPPORT_MC_MGCG |
1271				AMD_CG_SUPPORT_MC_LS |
1272				AMD_CG_SUPPORT_SDMA_MGCG |
1273				AMD_CG_SUPPORT_SDMA_LS |
1274				AMD_CG_SUPPORT_VCN_MGCG;
1275
 
 
 
 
1276			adev->pg_flags = AMD_PG_SUPPORT_SDMA |
1277				AMD_PG_SUPPORT_MMHUB |
1278				AMD_PG_SUPPORT_VCN;
1279		} else {
1280			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1281				AMD_CG_SUPPORT_GFX_MGLS |
1282				AMD_CG_SUPPORT_GFX_RLC_LS |
1283				AMD_CG_SUPPORT_GFX_CP_LS |
1284				AMD_CG_SUPPORT_GFX_3D_CGLS |
1285				AMD_CG_SUPPORT_GFX_CGCG |
1286				AMD_CG_SUPPORT_GFX_CGLS |
1287				AMD_CG_SUPPORT_BIF_MGCG |
1288				AMD_CG_SUPPORT_BIF_LS |
1289				AMD_CG_SUPPORT_HDP_MGCG |
1290				AMD_CG_SUPPORT_HDP_LS |
1291				AMD_CG_SUPPORT_DRM_MGCG |
1292				AMD_CG_SUPPORT_DRM_LS |
1293				AMD_CG_SUPPORT_MC_MGCG |
1294				AMD_CG_SUPPORT_MC_LS |
1295				AMD_CG_SUPPORT_SDMA_MGCG |
1296				AMD_CG_SUPPORT_SDMA_LS |
1297				AMD_CG_SUPPORT_VCN_MGCG;
1298
1299			adev->pg_flags = AMD_PG_SUPPORT_SDMA | AMD_PG_SUPPORT_VCN;
1300		}
1301		break;
1302	case CHIP_ARCTURUS:
1303		adev->asic_funcs = &vega20_asic_funcs;
1304		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1305			AMD_CG_SUPPORT_GFX_MGLS |
1306			AMD_CG_SUPPORT_GFX_CGCG |
1307			AMD_CG_SUPPORT_GFX_CGLS |
1308			AMD_CG_SUPPORT_GFX_CP_LS |
1309			AMD_CG_SUPPORT_HDP_MGCG |
1310			AMD_CG_SUPPORT_HDP_LS |
1311			AMD_CG_SUPPORT_SDMA_MGCG |
1312			AMD_CG_SUPPORT_SDMA_LS |
1313			AMD_CG_SUPPORT_MC_MGCG |
1314			AMD_CG_SUPPORT_MC_LS |
1315			AMD_CG_SUPPORT_IH_CG |
1316			AMD_CG_SUPPORT_VCN_MGCG |
1317			AMD_CG_SUPPORT_JPEG_MGCG;
1318		adev->pg_flags = AMD_PG_SUPPORT_VCN | AMD_PG_SUPPORT_VCN_DPG;
1319		adev->external_rev_id = adev->rev_id + 0x32;
1320		break;
1321	case CHIP_RENOIR:
1322		adev->asic_funcs = &soc15_asic_funcs;
1323
1324		if (adev->apu_flags & AMD_APU_IS_RENOIR)
1325			adev->external_rev_id = adev->rev_id + 0x91;
1326		else
1327			adev->external_rev_id = adev->rev_id + 0xa1;
1328		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1329				 AMD_CG_SUPPORT_GFX_MGLS |
1330				 AMD_CG_SUPPORT_GFX_3D_CGCG |
1331				 AMD_CG_SUPPORT_GFX_3D_CGLS |
1332				 AMD_CG_SUPPORT_GFX_CGCG |
1333				 AMD_CG_SUPPORT_GFX_CGLS |
1334				 AMD_CG_SUPPORT_GFX_CP_LS |
1335				 AMD_CG_SUPPORT_MC_MGCG |
1336				 AMD_CG_SUPPORT_MC_LS |
1337				 AMD_CG_SUPPORT_SDMA_MGCG |
1338				 AMD_CG_SUPPORT_SDMA_LS |
1339				 AMD_CG_SUPPORT_BIF_LS |
1340				 AMD_CG_SUPPORT_HDP_LS |
1341				 AMD_CG_SUPPORT_VCN_MGCG |
1342				 AMD_CG_SUPPORT_JPEG_MGCG |
1343				 AMD_CG_SUPPORT_IH_CG |
1344				 AMD_CG_SUPPORT_ATHUB_LS |
1345				 AMD_CG_SUPPORT_ATHUB_MGCG |
1346				 AMD_CG_SUPPORT_DF_MGCG;
1347		adev->pg_flags = AMD_PG_SUPPORT_SDMA |
1348				 AMD_PG_SUPPORT_VCN |
1349				 AMD_PG_SUPPORT_JPEG |
1350				 AMD_PG_SUPPORT_VCN_DPG;
1351		break;
1352	case CHIP_ALDEBARAN:
1353		adev->asic_funcs = &vega20_asic_funcs;
1354		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1355			AMD_CG_SUPPORT_GFX_MGLS |
1356			AMD_CG_SUPPORT_GFX_CGCG |
1357			AMD_CG_SUPPORT_GFX_CGLS |
1358			AMD_CG_SUPPORT_GFX_CP_LS |
1359			AMD_CG_SUPPORT_HDP_LS |
1360			AMD_CG_SUPPORT_SDMA_MGCG |
1361			AMD_CG_SUPPORT_SDMA_LS |
1362			AMD_CG_SUPPORT_IH_CG |
1363			AMD_CG_SUPPORT_VCN_MGCG | AMD_CG_SUPPORT_JPEG_MGCG;
1364		adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG;
1365		adev->external_rev_id = adev->rev_id + 0x3c;
1366		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1367	default:
1368		/* FIXME: not supported yet */
1369		return -EINVAL;
1370	}
1371
1372	if (amdgpu_sriov_vf(adev)) {
1373		amdgpu_virt_init_setting(adev);
1374		xgpu_ai_mailbox_set_irq_funcs(adev);
1375	}
1376
1377	return 0;
1378}
1379
1380static int soc15_common_late_init(void *handle)
1381{
1382	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1383	int r = 0;
1384
1385	if (amdgpu_sriov_vf(adev))
1386		xgpu_ai_mailbox_get_irq(adev);
1387
1388	if (adev->nbio.ras_funcs &&
1389	    adev->nbio.ras_funcs->ras_late_init)
1390		r = adev->nbio.ras_funcs->ras_late_init(adev);
 
1391
1392	return r;
1393}
1394
1395static int soc15_common_sw_init(void *handle)
1396{
1397	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1398
1399	if (amdgpu_sriov_vf(adev))
1400		xgpu_ai_mailbox_add_irq_id(adev);
1401
1402	adev->df.funcs->sw_init(adev);
 
 
1403
1404	return 0;
1405}
1406
1407static int soc15_common_sw_fini(void *handle)
1408{
1409	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1410
1411	if (adev->nbio.ras_funcs &&
1412	    adev->nbio.ras_funcs->ras_fini)
1413		adev->nbio.ras_funcs->ras_fini(adev);
1414	adev->df.funcs->sw_fini(adev);
1415	return 0;
1416}
1417
1418static void soc15_doorbell_range_init(struct amdgpu_device *adev)
1419{
1420	int i;
1421	struct amdgpu_ring *ring;
1422
1423	/* sdma/ih doorbell range are programed by hypervisor */
1424	if (!amdgpu_sriov_vf(adev)) {
1425		for (i = 0; i < adev->sdma.num_instances; i++) {
1426			ring = &adev->sdma.instance[i].ring;
1427			adev->nbio.funcs->sdma_doorbell_range(adev, i,
1428				ring->use_doorbell, ring->doorbell_index,
1429				adev->doorbell_index.sdma_doorbell_range);
1430		}
1431
1432		adev->nbio.funcs->ih_doorbell_range(adev, adev->irq.ih.use_doorbell,
1433						adev->irq.ih.doorbell_index);
1434	}
1435}
1436
1437static int soc15_common_hw_init(void *handle)
1438{
1439	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1440
1441	/* enable pcie gen2/3 link */
1442	soc15_pcie_gen3_enable(adev);
1443	/* enable aspm */
1444	soc15_program_aspm(adev);
1445	/* setup nbio registers */
1446	adev->nbio.funcs->init_registers(adev);
1447	/* remap HDP registers to a hole in mmio space,
1448	 * for the purpose of expose those registers
1449	 * to process space
1450	 */
1451	if (adev->nbio.funcs->remap_hdp_registers)
1452		adev->nbio.funcs->remap_hdp_registers(adev);
1453
1454	/* enable the doorbell aperture */
1455	soc15_enable_doorbell_aperture(adev, true);
 
1456	/* HW doorbell routing policy: doorbell writing not
1457	 * in SDMA/IH/MM/ACV range will be routed to CP. So
1458	 * we need to init SDMA/IH/MM/ACV doorbell range prior
1459	 * to CP ip block init and ring test.
 
1460	 */
1461	soc15_doorbell_range_init(adev);
1462
1463	return 0;
1464}
1465
1466static int soc15_common_hw_fini(void *handle)
1467{
1468	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1469
1470	/* disable the doorbell aperture */
1471	soc15_enable_doorbell_aperture(adev, false);
 
 
 
 
 
 
1472	if (amdgpu_sriov_vf(adev))
1473		xgpu_ai_mailbox_put_irq(adev);
1474
1475	if (adev->nbio.ras_if &&
1476	    amdgpu_ras_is_supported(adev, adev->nbio.ras_if->block)) {
1477		if (adev->nbio.ras_funcs &&
1478		    adev->nbio.ras_funcs->init_ras_controller_interrupt)
1479			amdgpu_irq_put(adev, &adev->nbio.ras_controller_irq, 0);
1480		if (adev->nbio.ras_funcs &&
1481		    adev->nbio.ras_funcs->init_ras_err_event_athub_interrupt)
1482			amdgpu_irq_put(adev, &adev->nbio.ras_err_event_athub_irq, 0);
1483	}
1484
1485	return 0;
1486}
1487
1488static int soc15_common_suspend(void *handle)
1489{
1490	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1491
1492	return soc15_common_hw_fini(adev);
1493}
1494
1495static int soc15_common_resume(void *handle)
1496{
1497	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1498
 
 
 
 
1499	return soc15_common_hw_init(adev);
1500}
1501
1502static bool soc15_common_is_idle(void *handle)
1503{
1504	return true;
1505}
1506
1507static int soc15_common_wait_for_idle(void *handle)
1508{
1509	return 0;
1510}
1511
1512static int soc15_common_soft_reset(void *handle)
1513{
1514	return 0;
1515}
1516
1517static void soc15_update_drm_clock_gating(struct amdgpu_device *adev, bool enable)
1518{
1519	uint32_t def, data;
1520
1521	def = data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0));
1522
1523	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_DRM_MGCG))
1524		data &= ~(0x01000000 |
1525			  0x02000000 |
1526			  0x04000000 |
1527			  0x08000000 |
1528			  0x10000000 |
1529			  0x20000000 |
1530			  0x40000000 |
1531			  0x80000000);
1532	else
1533		data |= (0x01000000 |
1534			 0x02000000 |
1535			 0x04000000 |
1536			 0x08000000 |
1537			 0x10000000 |
1538			 0x20000000 |
1539			 0x40000000 |
1540			 0x80000000);
1541
1542	if (def != data)
1543		WREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0), data);
1544}
1545
1546static void soc15_update_drm_light_sleep(struct amdgpu_device *adev, bool enable)
1547{
1548	uint32_t def, data;
1549
1550	def = data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL));
1551
1552	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_DRM_LS))
1553		data |= 1;
1554	else
1555		data &= ~1;
1556
1557	if (def != data)
1558		WREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL), data);
1559}
1560
1561static int soc15_common_set_clockgating_state(void *handle,
1562					    enum amd_clockgating_state state)
1563{
1564	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1565
1566	if (amdgpu_sriov_vf(adev))
1567		return 0;
1568
1569	switch (adev->asic_type) {
1570	case CHIP_VEGA10:
1571	case CHIP_VEGA12:
1572	case CHIP_VEGA20:
1573		adev->nbio.funcs->update_medium_grain_clock_gating(adev,
1574				state == AMD_CG_STATE_GATE);
1575		adev->nbio.funcs->update_medium_grain_light_sleep(adev,
1576				state == AMD_CG_STATE_GATE);
1577		adev->hdp.funcs->update_clock_gating(adev,
1578				state == AMD_CG_STATE_GATE);
1579		soc15_update_drm_clock_gating(adev,
1580				state == AMD_CG_STATE_GATE);
1581		soc15_update_drm_light_sleep(adev,
1582				state == AMD_CG_STATE_GATE);
1583		adev->smuio.funcs->update_rom_clock_gating(adev,
1584				state == AMD_CG_STATE_GATE);
1585		adev->df.funcs->update_medium_grain_clock_gating(adev,
1586				state == AMD_CG_STATE_GATE);
1587		break;
1588	case CHIP_RAVEN:
1589	case CHIP_RENOIR:
 
1590		adev->nbio.funcs->update_medium_grain_clock_gating(adev,
1591				state == AMD_CG_STATE_GATE);
1592		adev->nbio.funcs->update_medium_grain_light_sleep(adev,
1593				state == AMD_CG_STATE_GATE);
1594		adev->hdp.funcs->update_clock_gating(adev,
1595				state == AMD_CG_STATE_GATE);
1596		soc15_update_drm_clock_gating(adev,
1597				state == AMD_CG_STATE_GATE);
1598		soc15_update_drm_light_sleep(adev,
1599				state == AMD_CG_STATE_GATE);
1600		break;
1601	case CHIP_ARCTURUS:
1602	case CHIP_ALDEBARAN:
1603		adev->hdp.funcs->update_clock_gating(adev,
1604				state == AMD_CG_STATE_GATE);
1605		break;
1606	default:
1607		break;
1608	}
1609	return 0;
1610}
1611
1612static void soc15_common_get_clockgating_state(void *handle, u32 *flags)
1613{
1614	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1615	int data;
1616
1617	if (amdgpu_sriov_vf(adev))
1618		*flags = 0;
1619
1620	adev->nbio.funcs->get_clockgating_state(adev, flags);
1621
1622	adev->hdp.funcs->get_clock_gating_state(adev, flags);
1623
1624	if (adev->asic_type != CHIP_ALDEBARAN) {
 
1625
 
 
1626		/* AMD_CG_SUPPORT_DRM_MGCG */
1627		data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0));
1628		if (!(data & 0x01000000))
1629			*flags |= AMD_CG_SUPPORT_DRM_MGCG;
1630
1631		/* AMD_CG_SUPPORT_DRM_LS */
1632		data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL));
1633		if (data & 0x1)
1634			*flags |= AMD_CG_SUPPORT_DRM_LS;
1635	}
1636
1637	/* AMD_CG_SUPPORT_ROM_MGCG */
1638	adev->smuio.funcs->get_clock_gating_state(adev, flags);
 
1639
1640	adev->df.funcs->get_clockgating_state(adev, flags);
 
1641}
1642
1643static int soc15_common_set_powergating_state(void *handle,
1644					    enum amd_powergating_state state)
1645{
1646	/* todo */
1647	return 0;
1648}
1649
1650const struct amd_ip_funcs soc15_common_ip_funcs = {
1651	.name = "soc15_common",
1652	.early_init = soc15_common_early_init,
1653	.late_init = soc15_common_late_init,
1654	.sw_init = soc15_common_sw_init,
1655	.sw_fini = soc15_common_sw_fini,
1656	.hw_init = soc15_common_hw_init,
1657	.hw_fini = soc15_common_hw_fini,
1658	.suspend = soc15_common_suspend,
1659	.resume = soc15_common_resume,
1660	.is_idle = soc15_common_is_idle,
1661	.wait_for_idle = soc15_common_wait_for_idle,
1662	.soft_reset = soc15_common_soft_reset,
1663	.set_clockgating_state = soc15_common_set_clockgating_state,
1664	.set_powergating_state = soc15_common_set_powergating_state,
1665	.get_clockgating_state= soc15_common_get_clockgating_state,
1666};