1 /* 2 * Copyright 2025 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 "amdgpu.h" 24 #include "amdgpu_discovery.h" 25 #include "soc15.h" 26 #include "soc15_common.h" 27 #include "soc_v1_0.h" 28 #include "amdgpu_ip.h" 29 #include "amdgpu_imu.h" 30 #include "gfxhub_v12_1.h" 31 #include "sdma_v7_1.h" 32 #include "gfx_v12_1.h" 33 #include "amdgpu_video_codecs.h" 34 #include "amdgpu_reset.h" 35 36 #include "gc/gc_12_1_0_offset.h" 37 #include "gc/gc_12_1_0_sh_mask.h" 38 #include "mp/mp_15_0_8_offset.h" 39 40 #define XCC_REG_RANGE_0_LOW 0x1260 /* XCC gfxdec0 lower Bound */ 41 #define XCC_REG_RANGE_0_HIGH 0x3C00 /* XCC gfxdec0 upper Bound */ 42 #define XCC_REG_RANGE_1_LOW 0xA000 /* XCC gfxdec1 lower Bound */ 43 #define XCC_REG_RANGE_1_HIGH 0x10000 /* XCC gfxdec1 upper Bound */ 44 #define NORMALIZE_XCC_REG_OFFSET(offset) \ 45 (offset & 0xFFFF) 46 47 #define MID1_REG_RANGE_0_LOW 0x40000 48 #define MID1_REG_RANGE_0_HIGH 0x80000 49 #define NORMALIZE_MID_REG_OFFSET(offset) \ 50 (offset & 0x3FFFF) 51 52 static const struct amdgpu_video_codecs vcn_5_0_2_video_codecs_encode_vcn0 = { 53 .codec_count = 0, 54 .codec_array = NULL, 55 }; 56 57 static const struct amdgpu_video_codec_info vcn_5_0_2_video_codecs_decode_array_vcn0[] = { 58 {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)}, 59 {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)}, 60 {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 16384, 16384, 0)}, 61 {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)}, 62 {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1, 8192, 4352, 0)}, 63 }; 64 65 static const struct amdgpu_video_codecs vcn_5_0_2_video_codecs_decode_vcn0 = { 66 .codec_count = ARRAY_SIZE(vcn_5_0_2_video_codecs_decode_array_vcn0), 67 .codec_array = vcn_5_0_2_video_codecs_decode_array_vcn0, 68 }; 69 70 static int soc_v1_0_query_video_codecs(struct amdgpu_device *adev, bool encode, 71 const struct amdgpu_video_codecs **codecs) 72 { 73 switch (amdgpu_ip_version(adev, UVD_HWIP, 0)) { 74 case IP_VERSION(5, 0, 2): 75 if (encode) 76 *codecs = &vcn_5_0_2_video_codecs_encode_vcn0; 77 else 78 *codecs = &vcn_5_0_2_video_codecs_decode_vcn0; 79 return 0; 80 default: 81 return -EINVAL; 82 } 83 } 84 85 /* Initialized doorbells for amdgpu including multimedia 86 * KFD can use all the rest in 2M doorbell bar */ 87 static void soc_v1_0_doorbell_index_init(struct amdgpu_device *adev) 88 { 89 int i; 90 91 adev->doorbell_index.kiq = AMDGPU_SOC_V1_0_DOORBELL_KIQ_START; 92 93 adev->doorbell_index.mec_ring0 = AMDGPU_SOC_V1_0_DOORBELL_MEC_RING_START; 94 adev->doorbell_index.mes_ring0 = AMDGPU_SOC_V1_0_DOORBELL_MES_RING0; 95 adev->doorbell_index.mes_ring1 = AMDGPU_SOC_V1_0_DOORBELL_MES_RING1; 96 97 adev->doorbell_index.userqueue_start = AMDGPU_SOC_V1_0_DOORBELL_USERQUEUE_START; 98 adev->doorbell_index.userqueue_end = AMDGPU_SOC_V1_0_DOORBELL_USERQUEUE_END; 99 adev->doorbell_index.xcc_doorbell_range = AMDGPU_SOC_V1_0_DOORBELL_XCC_RANGE; 100 101 adev->doorbell_index.sdma_doorbell_range = 14; 102 for (i = 0; i < adev->sdma.num_instances; i++) 103 adev->doorbell_index.sdma_engine[i] = 104 AMDGPU_SOC_V1_0_DOORBELL_sDMA_ENGINE_START + 105 i * (adev->doorbell_index.sdma_doorbell_range >> 1); 106 107 adev->doorbell_index.ih = AMDGPU_SOC_V1_0_DOORBELL_IH; 108 adev->doorbell_index.vcn.vcn_ring0_1 = AMDGPU_SOC_V1_0_DOORBELL_VCN_START; 109 110 adev->doorbell_index.first_non_cp = AMDGPU_SOC_V1_0_DOORBELL_FIRST_NON_CP; 111 adev->doorbell_index.last_non_cp = AMDGPU_SOC_V1_0_DOORBELL_LAST_NON_CP; 112 113 adev->doorbell_index.max_assignment = AMDGPU_SOC_V1_0_DOORBELL_MAX_ASSIGNMENT << 1; 114 } 115 116 /* Fixed pattern for upper 32bits smn addressing. 117 * bit[47:40]: Socket ID 118 * bit[39:34]: Die ID 119 * bit[32]: local or remote die in same socket 120 * The ext_id is comprised of socket_id and die_id. 121 * ext_id = (socket_id << 6) | (die_id) 122 */ 123 u64 soc_v1_0_encode_ext_smn_addressing(int ext_id) 124 { 125 u64 ext_offset; 126 int socket_id, die_id; 127 128 /* local die routing for MID0 on local socket */ 129 if (ext_id == 0) 130 return 0; 131 132 die_id = ext_id & 0x3; 133 socket_id = (ext_id >> 6) & 0xff; 134 135 /* Initiated from host, accessing to non-MID0 is cross-die traffic */ 136 if (socket_id == 0) 137 ext_offset = ((u64)die_id << 34) | (1ULL << 32); 138 else if (socket_id != 0 && die_id != 0) 139 ext_offset = ((u64)socket_id << 40) | ((u64)die_id << 34) | 140 (3ULL << 32); 141 else 142 ext_offset = ((u64)socket_id << 40) | (1ULL << 33); 143 144 return ext_offset; 145 } 146 147 static u32 soc_v1_0_get_config_memsize(struct amdgpu_device *adev) 148 { 149 return adev->nbio.funcs->get_memsize(adev); 150 } 151 152 static u32 soc_v1_0_get_xclk(struct amdgpu_device *adev) 153 { 154 /* 100MHz is the default */ 155 if (!adev->bios) 156 return 10000; 157 else 158 return adev->clock.spll.reference_freq; 159 } 160 161 void soc_v1_0_grbm_select(struct amdgpu_device *adev, 162 u32 me, u32 pipe, 163 u32 queue, u32 vmid, 164 int xcc_id) 165 { 166 u32 grbm_gfx_cntl = 0; 167 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, PIPEID, pipe); 168 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, MEID, me); 169 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, VMID, vmid); 170 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, QUEUEID, queue); 171 172 WREG32_SOC15_RLC_SHADOW(GC, xcc_id, regGRBM_GFX_CNTL, grbm_gfx_cntl); 173 } 174 175 static struct soc15_allowed_register_entry soc_v1_0_allowed_read_registers[] = { 176 { SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS) }, 177 { SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS2) }, 178 { SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS3) }, 179 { SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS_SE0) }, 180 { SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS_SE1) }, 181 { SOC15_REG_ENTRY(GC, 0, regCP_STAT) }, 182 { SOC15_REG_ENTRY(GC, 0, regCP_STALLED_STAT1) }, 183 { SOC15_REG_ENTRY(GC, 0, regCP_STALLED_STAT2) }, 184 { SOC15_REG_ENTRY(GC, 0, regCP_STALLED_STAT3) }, 185 { SOC15_REG_ENTRY(GC, 0, regCP_CPF_BUSY_STAT) }, 186 { SOC15_REG_ENTRY(GC, 0, regCP_CPF_STALLED_STAT1) }, 187 { SOC15_REG_ENTRY(GC, 0, regCP_CPF_STATUS) }, 188 { SOC15_REG_ENTRY(GC, 0, regCP_CPC_BUSY_STAT) }, 189 { SOC15_REG_ENTRY(GC, 0, regCP_CPC_STALLED_STAT1) }, 190 { SOC15_REG_ENTRY(GC, 0, regCP_CPC_STATUS) }, 191 { SOC15_REG_ENTRY(GC, 0, regGB_ADDR_CONFIG_1) }, 192 }; 193 194 195 static uint32_t soc_v1_0_get_register_value(struct amdgpu_device *adev, 196 bool indexed, u32 se_num, 197 u32 sh_num, u32 reg_offset) 198 { 199 if (indexed) { 200 return amdgpu_read_indexed_register(adev, se_num, sh_num, reg_offset); 201 } else { 202 if (reg_offset == SOC15_REG_OFFSET(GC, 0, regGB_ADDR_CONFIG_1) && 203 adev->gfx.config.gb_addr_config) 204 return adev->gfx.config.gb_addr_config; 205 return RREG32(reg_offset); 206 } 207 } 208 209 static int soc_v1_0_read_register(struct amdgpu_device *adev, 210 u32 se_num, u32 sh_num, 211 u32 reg_offset, u32 *value) 212 { 213 uint32_t i; 214 struct soc15_allowed_register_entry *en; 215 216 *value = 0; 217 for (i = 0; i < ARRAY_SIZE(soc_v1_0_allowed_read_registers); i++) { 218 en = &soc_v1_0_allowed_read_registers[i]; 219 if (!adev->reg_offset[en->hwip][en->inst]) 220 continue; 221 else if (reg_offset != (adev->reg_offset[en->hwip][en->inst][en->seg] 222 + en->reg_offset)) 223 continue; 224 225 *value = soc_v1_0_get_register_value(adev, 226 soc_v1_0_allowed_read_registers[i].grbm_indexed, 227 se_num, sh_num, reg_offset); 228 return 0; 229 } 230 return -EINVAL; 231 } 232 233 static bool soc_v1_0_need_reset_on_init(struct amdgpu_device *adev) 234 { 235 236 return false; 237 } 238 239 static enum amd_reset_method 240 soc_v1_0_asic_reset_method(struct amdgpu_device *adev) 241 { 242 if ((adev->gmc.xgmi.supported && adev->gmc.xgmi.connected_to_cpu) || 243 (amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(15, 0, 8))) { 244 if (amdgpu_reset_method != -1) 245 dev_warn_once(adev->dev, "Reset override isn't supported, using Mode2 instead.\n"); 246 247 return AMD_RESET_METHOD_MODE2; 248 } 249 250 return amdgpu_reset_method; 251 } 252 253 static int soc_v1_0_asic_reset(struct amdgpu_device *adev) 254 { 255 switch (soc_v1_0_asic_reset_method(adev)) { 256 case AMD_RESET_METHOD_MODE2: 257 dev_info(adev->dev, "MODE2 reset\n"); 258 return amdgpu_dpm_mode2_reset(adev); 259 default: 260 dev_info(adev->dev, "Invalid reset method Not supported\n"); 261 return -EOPNOTSUPP; 262 } 263 264 return 0; 265 } 266 267 /* 268 * Function returns a pair of (size, offset_within_vram) to 269 * tell caller to skip the *reserve_size bytes starting at *offset inside VRAM. 270 */ 271 static void soc_v1_0_get_fw_reserved_info(struct amdgpu_device *adev, 272 u64 *reserve_size, 273 u64 *offset) 274 { 275 struct mem_reserved_info umf_info, vram_info; 276 u64 vram_base; 277 278 if (amdgpu_discovery_get_mem_reserved_region_by_id(adev, 279 MASTER_DIE_UMF_REGION_ID, &umf_info)) { 280 dev_warn(adev->dev, 281 "MASTER_DIE_UMF region not found in discovery\n"); 282 return; 283 } 284 /* 285 * If SPECIFIC_PURPOSE_REGION exists and non-zero, use it as the VRAM base. 286 * In multi-die layouts, VRAM may be placed at non-default bases. 287 * Prefer to query SPECIFIC_PURPOSE_REGION to get the vram_base. 288 */ 289 if (!amdgpu_discovery_get_mem_reserved_region_by_id(adev, 290 SPECIFIC_PURPOSE_REGION_ID, &vram_info) && 291 vram_info.reserved_region_size) 292 vram_base = vram_info.reserved_region_start; 293 else 294 vram_base = adev->gmc.vram_start; 295 dev_dbg(adev->dev, "%s: vram_base=0x%llx\n", __func__, vram_base); 296 297 if (umf_info.reserved_region_size == 0 || 298 umf_info.reserved_region_start < vram_base || 299 umf_info.reserved_region_start + umf_info.reserved_region_size > 300 vram_base + adev->gmc.real_vram_size) { 301 dev_warn(adev->dev, 302 "MASTER_DIE_UMF region out of VRAM: start=0x%llx size=0x%llx vram=[0x%llx,+0x%llx)\n", 303 umf_info.reserved_region_start, umf_info.reserved_region_size, 304 vram_base, adev->gmc.real_vram_size); 305 return; 306 } 307 /* 308 * *offset is the distance from the start of VRAM to the start of the 309 * UMF carveout. 310 */ 311 *reserve_size = umf_info.reserved_region_size; 312 *offset = umf_info.reserved_region_start - vram_base; 313 } 314 315 static const struct amdgpu_asic_funcs soc_v1_0_asic_funcs = { 316 .read_bios_from_rom = &amdgpu_soc15_read_bios_from_rom, 317 .read_register = &soc_v1_0_read_register, 318 .get_config_memsize = &soc_v1_0_get_config_memsize, 319 .get_xclk = &soc_v1_0_get_xclk, 320 .init_doorbell_index = &soc_v1_0_doorbell_index_init, 321 .need_reset_on_init = &soc_v1_0_need_reset_on_init, 322 .encode_ext_smn_addressing = &soc_v1_0_encode_ext_smn_addressing, 323 .reset = soc_v1_0_asic_reset, 324 .reset_method = &soc_v1_0_asic_reset_method, 325 .query_video_codecs = &soc_v1_0_query_video_codecs, 326 .get_fw_reserved_info = &soc_v1_0_get_fw_reserved_info, 327 }; 328 329 enum soc_v1_0_external_rev_id { 330 SOC_V1_0_MID_A0_AID_A0_XCD_A0 = 0x1, 331 SOC_V1_0_MID_A0_AID_A0_XCD_B0 = 0x2, 332 SOC_V1_0_MID_A0_AID_A1_XCD_A0 = 0x3, 333 SOC_V1_0_MID_A0_AID_A1_XCD_B0 = 0x4, 334 }; 335 336 static int soc_v1_0_set_rev_id(struct amdgpu_device *adev) 337 { 338 u16 die_rev_id = 0, xcd_rev_id; 339 int r; 340 341 xcd_rev_id = amdgpu_device_get_rev_id(adev); 342 r = amdgpu_discovery_get_die_rev_id(adev, &die_rev_id); 343 if (r) { 344 die_rev_id = 0; 345 dev_warn(adev->dev, 346 "missing die rev id from ip discovery, assuming 0\n"); 347 } 348 349 /* 350 * FIXME: XCD revision is not yet populated in die_info[0].die_id by 351 * ASP firmware. Use the PCI revision ID register as a temporary 352 * fallback until firmware support is available. 353 */ 354 die_rev_id |= xcd_rev_id << SOC_V1_0_DIE_REV_XCD__SHIFT; 355 adev->rev_id = die_rev_id; 356 357 /* SOC_V1_0_DIE_REV(mid_rev, aid_rev, xcd_rev) */ 358 switch (die_rev_id) { 359 case SOC_V1_0_DIE_REV(0, 0, 0): 360 adev->external_rev_id = SOC_V1_0_MID_A0_AID_A0_XCD_A0; 361 break; 362 case SOC_V1_0_DIE_REV(0, 0, 1): 363 adev->external_rev_id = SOC_V1_0_MID_A0_AID_A0_XCD_B0; 364 break; 365 case SOC_V1_0_DIE_REV(0, 1, 0): 366 adev->external_rev_id = SOC_V1_0_MID_A0_AID_A1_XCD_A0; 367 break; 368 case SOC_V1_0_DIE_REV(0, 1, 1): 369 adev->external_rev_id = SOC_V1_0_MID_A0_AID_A1_XCD_B0; 370 break; 371 default: 372 dev_warn(adev->dev, "unknown die rev id 0x%x\n", die_rev_id); 373 break; 374 } 375 376 return 0; 377 } 378 379 static int soc_v1_0_common_early_init(struct amdgpu_ip_block *ip_block) 380 { 381 struct amdgpu_device *adev = ip_block->adev; 382 int r; 383 384 adev->reg.pcie.rreg = &amdgpu_device_indirect_rreg; 385 adev->reg.pcie.wreg = &amdgpu_device_indirect_wreg; 386 adev->reg.pcie.rreg_ext = &amdgpu_device_indirect_rreg_ext; 387 adev->reg.pcie.wreg_ext = &amdgpu_device_indirect_wreg_ext; 388 adev->reg.pcie.rreg64 = &amdgpu_device_indirect_rreg64; 389 adev->reg.pcie.wreg64 = &amdgpu_device_indirect_wreg64; 390 adev->reg.pcie.port_rreg = &amdgpu_device_pcie_port_rreg; 391 adev->reg.pcie.port_wreg = &amdgpu_device_pcie_port_wreg; 392 adev->reg.pcie.rreg64_ext = &amdgpu_device_indirect_rreg64_ext; 393 adev->reg.pcie.wreg64_ext = &amdgpu_device_indirect_wreg64_ext; 394 395 adev->asic_funcs = &soc_v1_0_asic_funcs; 396 397 r = soc_v1_0_set_rev_id(adev); 398 if (r) 399 return r; 400 401 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 402 case IP_VERSION(12, 1, 0): 403 adev->cg_flags = 0; 404 adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG; 405 break; 406 default: 407 /* FIXME: not supported yet */ 408 return -EINVAL; 409 } 410 411 adev->nbio.funcs->init_registers(adev); 412 413 return 0; 414 } 415 416 static int soc_v1_0_common_late_init(struct amdgpu_ip_block *ip_block) 417 { 418 struct amdgpu_device *adev = ip_block->adev; 419 420 /* Enable selfring doorbell aperture late because doorbell BAR 421 * aperture will change if resize BAR successfully in gmc sw_init. 422 */ 423 adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, true); 424 425 return 0; 426 } 427 428 static int soc_v1_0_common_hw_init(struct amdgpu_ip_block *ip_block) 429 { 430 struct amdgpu_device *adev = ip_block->adev; 431 432 /* enable the doorbell aperture */ 433 adev->nbio.funcs->enable_doorbell_aperture(adev, true); 434 435 return 0; 436 } 437 438 static int soc_v1_0_common_hw_fini(struct amdgpu_ip_block *ip_block) 439 { 440 struct amdgpu_device *adev = ip_block->adev; 441 442 adev->nbio.funcs->enable_doorbell_aperture(adev, false); 443 adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, false); 444 445 return 0; 446 } 447 448 static int soc_v1_0_common_suspend(struct amdgpu_ip_block *ip_block) 449 { 450 return soc_v1_0_common_hw_fini(ip_block); 451 } 452 453 static int soc_v1_0_common_resume(struct amdgpu_ip_block *ip_block) 454 { 455 return soc_v1_0_common_hw_init(ip_block); 456 } 457 458 static int soc_v1_0_common_set_clockgating_state(struct amdgpu_ip_block *ip_block, 459 enum amd_clockgating_state state) 460 { 461 return 0; 462 } 463 464 static int soc_v1_0_common_set_powergating_state(struct amdgpu_ip_block *ip_block, 465 enum amd_powergating_state state) 466 { 467 return 0; 468 } 469 470 static void soc_v1_0_common_get_clockgating_state(struct amdgpu_ip_block *ip_block, 471 u64 *flags) 472 { 473 return; 474 } 475 476 static const struct amd_ip_funcs soc_v1_0_common_ip_funcs = { 477 .name = "soc_v1_0_common", 478 .early_init = soc_v1_0_common_early_init, 479 .late_init = soc_v1_0_common_late_init, 480 .hw_init = soc_v1_0_common_hw_init, 481 .hw_fini = soc_v1_0_common_hw_fini, 482 .suspend = soc_v1_0_common_suspend, 483 .resume = soc_v1_0_common_resume, 484 .set_clockgating_state = soc_v1_0_common_set_clockgating_state, 485 .set_powergating_state = soc_v1_0_common_set_powergating_state, 486 .get_clockgating_state = soc_v1_0_common_get_clockgating_state, 487 }; 488 489 const struct amdgpu_ip_block_version soc_v1_0_common_ip_block = { 490 .type = AMD_IP_BLOCK_TYPE_COMMON, 491 .major = 1, 492 .minor = 0, 493 .rev = 0, 494 .funcs = &soc_v1_0_common_ip_funcs, 495 }; 496 497 static struct amdgpu_reset_handler * 498 soc_v1_0_get_reset_handler(struct amdgpu_reset_control *reset_ctl, 499 struct amdgpu_reset_context *reset_context) 500 { 501 struct amdgpu_device *adev = (struct amdgpu_device *)reset_ctl->handle; 502 struct amdgpu_reset_handler *handler; 503 enum amd_reset_method method; 504 int i; 505 506 method = (reset_context->method == AMD_RESET_METHOD_NONE) ? 507 amdgpu_asic_reset_method(adev) : reset_context->method; 508 for_each_handler(i, handler, reset_ctl) { 509 if (handler->reset_method == method) 510 return handler; 511 } 512 513 return NULL; 514 } 515 516 static inline u32 soc_v1_0_get_ip_block_mask(struct amdgpu_device *adev) 517 { 518 u32 ip_block_mask = BIT(AMD_IP_BLOCK_TYPE_GFX) | 519 BIT(AMD_IP_BLOCK_TYPE_MES) | 520 BIT(AMD_IP_BLOCK_TYPE_SDMA) | 521 BIT(AMD_IP_BLOCK_TYPE_IH); 522 523 return ip_block_mask; 524 } 525 526 static int soc_v1_0_mode2_suspend_ip(struct amdgpu_device *adev) 527 { 528 u32 ip_block_mask = soc_v1_0_get_ip_block_mask(adev); 529 u32 ip_block; 530 int r, i; 531 532 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE); 533 534 /* SDMA suspend not required */ 535 ip_block_mask &= ~BIT(AMD_IP_BLOCK_TYPE_SDMA); 536 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 537 if (!adev->ip_blocks[i].status.valid) 538 continue; 539 ip_block = BIT(adev->ip_blocks[i].version->type); 540 if (!(ip_block_mask & ip_block)) 541 continue; 542 543 r = amdgpu_ip_block_suspend(&adev->ip_blocks[i]); 544 if (r) 545 return r; 546 } 547 548 return 0; 549 } 550 551 static int 552 soc_v1_0_mode2_prepare_hwcontext(struct amdgpu_reset_control *reset_ctl, 553 struct amdgpu_reset_context *reset_context) 554 { 555 struct amdgpu_device *adev = (struct amdgpu_device *)reset_ctl->handle; 556 557 return soc_v1_0_mode2_suspend_ip(adev); 558 } 559 560 static int soc_v1_0_mode2_reset(struct amdgpu_device *adev) 561 { 562 adev->asic_reset_res = amdgpu_dpm_mode2_reset(adev); 563 return adev->asic_reset_res; 564 } 565 566 static void soc_v1_0_async_reset(struct work_struct *work) 567 { 568 struct amdgpu_reset_handler *handler; 569 struct amdgpu_reset_control *reset_ctl = 570 container_of(work, struct amdgpu_reset_control, reset_work); 571 struct amdgpu_device *adev = (struct amdgpu_device *)reset_ctl->handle; 572 int i; 573 574 for_each_handler(i, handler, reset_ctl) { 575 if (handler->reset_method == reset_ctl->active_reset) { 576 dev_dbg(adev->dev, "Resetting device\n"); 577 handler->do_reset(adev); 578 break; 579 } 580 } 581 } 582 583 static int 584 soc_v1_0_mode2_perform_reset(struct amdgpu_reset_control *reset_ctl, 585 struct amdgpu_reset_context *reset_context) 586 { 587 struct amdgpu_device *adev = (struct amdgpu_device *)reset_ctl->handle; 588 struct list_head *reset_device_list = reset_context->reset_device_list; 589 struct amdgpu_device *tmp_adev = NULL; 590 int r = 0; 591 592 dev_dbg(adev->dev, "soc_v1_0 perform hw reset\n"); 593 594 if (!reset_device_list) 595 return -EINVAL; 596 597 list_for_each_entry(tmp_adev, reset_device_list, reset_list) { 598 mutex_lock(&tmp_adev->reset_cntl->reset_lock); 599 tmp_adev->reset_cntl->active_reset = AMD_RESET_METHOD_MODE2; 600 } 601 602 list_for_each_entry(tmp_adev, reset_device_list, reset_list) { 603 r = soc_v1_0_mode2_reset(tmp_adev); 604 if (r) { 605 dev_err(tmp_adev->dev, 606 "ASIC reset failed with error, %d for drm dev, %s", 607 r, adev_to_drm(tmp_adev)->unique); 608 break; 609 } 610 } 611 612 list_for_each_entry(tmp_adev, reset_device_list, reset_list) { 613 mutex_unlock(&tmp_adev->reset_cntl->reset_lock); 614 tmp_adev->reset_cntl->active_reset = AMD_RESET_METHOD_NONE; 615 } 616 617 return r; 618 } 619 620 static int soc_v1_0_mode2_restore_ip(struct amdgpu_device *adev) 621 { 622 u32 ip_block_mask = soc_v1_0_get_ip_block_mask(adev); 623 struct amdgpu_ip_block *ih_block; 624 u32 ip_block; 625 int i, r; 626 627 if (ip_block_mask & BIT(AMD_IP_BLOCK_TYPE_IH)) { 628 ih_block = amdgpu_device_ip_get_ip_block(adev, 629 AMD_IP_BLOCK_TYPE_IH); 630 if (unlikely(!ih_block)) { 631 dev_err(adev->dev, "Failed to get IH handle\n"); 632 return -EINVAL; 633 } 634 r = amdgpu_ip_block_resume(ih_block); 635 if (r) 636 return r; 637 } 638 639 /* IH Block resume completed can be removed from ip_block_mask */ 640 ip_block_mask &= ~BIT(AMD_IP_BLOCK_TYPE_IH); 641 642 /* Reinit GFXHUB */ 643 adev->gfxhub.funcs->init(adev); 644 r = adev->gfxhub.funcs->gart_enable(adev); 645 if (r) { 646 dev_err(adev->dev, "GFXHUB gart reenable failed after reset\n"); 647 return r; 648 } 649 650 for (i = 0; i < adev->num_ip_blocks; i++) { 651 if (!adev->ip_blocks[i].status.valid) 652 continue; 653 ip_block = BIT(adev->ip_blocks[i].version->type); 654 if (!(ip_block_mask & ip_block)) 655 continue; 656 r = amdgpu_ip_block_resume(&adev->ip_blocks[i]); 657 if (r) 658 return r; 659 } 660 661 for (i = 0; i < adev->num_ip_blocks; i++) { 662 if (!adev->ip_blocks[i].status.valid) 663 continue; 664 ip_block = BIT(adev->ip_blocks[i].version->type); 665 if (!(ip_block_mask & ip_block)) 666 continue; 667 668 if (adev->ip_blocks[i].version->funcs->late_init) { 669 r = adev->ip_blocks[i].version->funcs->late_init(&adev->ip_blocks[i]); 670 if (r) { 671 dev_err(adev->dev, 672 "late_init of IP block <%s> failed %d after reset\n", 673 adev->ip_blocks[i].version->funcs->name, 674 r); 675 return r; 676 } 677 } 678 adev->ip_blocks[i].status.late_initialized = true; 679 } 680 681 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE); 682 683 return r; 684 } 685 686 static int 687 soc_v1_0_mode2_restore_hwcontext(struct amdgpu_reset_control *reset_ctl, 688 struct amdgpu_reset_context *reset_context) 689 { 690 struct list_head *reset_device_list = reset_context->reset_device_list; 691 struct amdgpu_device *tmp_adev = NULL; 692 int r; 693 694 if (!reset_device_list) 695 return -EINVAL; 696 697 list_for_each_entry(tmp_adev, reset_device_list, reset_list) { 698 amdgpu_set_init_level(tmp_adev, 699 AMDGPU_INIT_LEVEL_RESET_RECOVERY); 700 dev_info(tmp_adev->dev, 701 "GPU reset succeeded, trying to resume\n"); 702 amdgpu_ras_clear_err_state(tmp_adev); 703 r = soc_v1_0_mode2_restore_ip(tmp_adev); 704 if (r) 705 goto end; 706 707 /* 708 * Add this ASIC as tracked as reset was already 709 * complete successfully. 710 */ 711 amdgpu_register_gpu_instance(tmp_adev); 712 713 amdgpu_ras_resume(tmp_adev); 714 715 if (!r) { 716 amdgpu_set_init_level(tmp_adev, 717 AMDGPU_INIT_LEVEL_DEFAULT); 718 amdgpu_irq_gpu_reset_resume_helper(tmp_adev); 719 720 r = amdgpu_ib_ring_tests(tmp_adev); 721 if (r) { 722 dev_err(tmp_adev->dev, 723 "ib ring test failed (%d).\n", r); 724 r = -EAGAIN; 725 tmp_adev->asic_reset_res = r; 726 goto end; 727 } 728 } 729 } 730 731 end: 732 return r; 733 } 734 735 static struct amdgpu_reset_handler soc_v1_0_mode2_handler = { 736 .reset_method = AMD_RESET_METHOD_MODE2, 737 .prepare_env = NULL, 738 .prepare_hwcontext = soc_v1_0_mode2_prepare_hwcontext, 739 .perform_reset = soc_v1_0_mode2_perform_reset, 740 .restore_hwcontext = soc_v1_0_mode2_restore_hwcontext, 741 .restore_env = NULL, 742 .do_reset = soc_v1_0_mode2_reset, 743 }; 744 745 static struct amdgpu_reset_handler 746 *soc_v1_0_rst_handlers[AMDGPU_RESET_MAX_HANDLERS] = { 747 &soc_v1_0_mode2_handler, 748 }; 749 750 int soc_v1_0_reset_init(struct amdgpu_device *adev) 751 { 752 struct amdgpu_reset_control *reset_ctl; 753 754 reset_ctl = kzalloc_obj(*reset_ctl, GFP_KERNEL); 755 if (!reset_ctl) 756 return -ENOMEM; 757 758 reset_ctl->handle = adev; 759 reset_ctl->async_reset = soc_v1_0_async_reset; 760 reset_ctl->active_reset = AMD_RESET_METHOD_NONE; 761 reset_ctl->get_reset_handler = soc_v1_0_get_reset_handler; 762 763 INIT_WORK(&reset_ctl->reset_work, reset_ctl->async_reset); 764 /* Only mode2 is handled through reset control now */ 765 reset_ctl->reset_handlers = &soc_v1_0_rst_handlers; 766 767 adev->reset_cntl = reset_ctl; 768 769 return 0; 770 } 771 772 int soc_v1_0_reset_fini(struct amdgpu_device *adev) 773 { 774 kfree(adev->reset_cntl); 775 adev->reset_cntl = NULL; 776 return 0; 777 } 778 779 static enum amdgpu_gfx_partition __soc_v1_0_calc_xcp_mode(struct amdgpu_xcp_mgr *xcp_mgr) 780 { 781 struct amdgpu_device *adev = xcp_mgr->adev; 782 int num_xcc, num_xcc_per_xcp = 0, mode = 0; 783 784 num_xcc = NUM_XCC(xcp_mgr->adev->gfx.xcc_mask); 785 if (adev->gfx.funcs && 786 adev->gfx.funcs->get_xccs_per_xcp) 787 num_xcc_per_xcp = adev->gfx.funcs->get_xccs_per_xcp(adev); 788 if ((num_xcc_per_xcp) && (num_xcc % num_xcc_per_xcp == 0)) 789 mode = num_xcc / num_xcc_per_xcp; 790 791 if (num_xcc_per_xcp == 1) 792 return AMDGPU_CPX_PARTITION_MODE; 793 794 switch (mode) { 795 case 1: 796 return AMDGPU_SPX_PARTITION_MODE; 797 case 2: 798 return AMDGPU_DPX_PARTITION_MODE; 799 case 3: 800 return AMDGPU_TPX_PARTITION_MODE; 801 case 4: 802 return AMDGPU_QPX_PARTITION_MODE; 803 default: 804 return AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE; 805 } 806 807 return AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE; 808 } 809 810 static int soc_v1_0_query_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr) 811 { 812 enum amdgpu_gfx_partition derv_mode, mode; 813 struct amdgpu_device *adev = xcp_mgr->adev; 814 815 mode = AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE; 816 derv_mode = __soc_v1_0_calc_xcp_mode(xcp_mgr); 817 818 if (amdgpu_sriov_vf(adev) || !adev->psp.funcs) 819 return derv_mode; 820 821 if (adev->nbio.funcs && 822 adev->nbio.funcs->get_compute_partition_mode) { 823 mode = adev->nbio.funcs->get_compute_partition_mode(adev); 824 if (mode != derv_mode) 825 dev_warn(adev->dev, 826 "Mismatch in compute partition mode - reported : %d derived : %d", 827 mode, derv_mode); 828 } 829 830 return mode; 831 } 832 833 static int __soc_v1_0_get_xcc_per_xcp(struct amdgpu_xcp_mgr *xcp_mgr, int mode) 834 { 835 int num_xcc, num_xcc_per_xcp = 0; 836 837 num_xcc = NUM_XCC(xcp_mgr->adev->gfx.xcc_mask); 838 839 switch (mode) { 840 case AMDGPU_SPX_PARTITION_MODE: 841 num_xcc_per_xcp = num_xcc; 842 break; 843 case AMDGPU_DPX_PARTITION_MODE: 844 num_xcc_per_xcp = num_xcc / 2; 845 break; 846 case AMDGPU_TPX_PARTITION_MODE: 847 num_xcc_per_xcp = num_xcc / 3; 848 break; 849 case AMDGPU_QPX_PARTITION_MODE: 850 num_xcc_per_xcp = num_xcc / 4; 851 break; 852 case AMDGPU_CPX_PARTITION_MODE: 853 num_xcc_per_xcp = 1; 854 break; 855 } 856 857 return num_xcc_per_xcp; 858 } 859 860 static int __soc_v1_0_get_xcp_ip_info(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id, 861 enum AMDGPU_XCP_IP_BLOCK ip_id, 862 struct amdgpu_xcp_ip *ip) 863 { 864 struct amdgpu_device *adev = xcp_mgr->adev; 865 int num_sdma, num_vcn, num_shared_vcn, num_xcp; 866 int num_xcc_xcp, num_sdma_xcp, num_vcn_xcp; 867 868 num_sdma = adev->sdma.num_instances; 869 num_vcn = adev->vcn.num_vcn_inst; 870 num_shared_vcn = 1; 871 872 num_xcc_xcp = adev->gfx.num_xcc_per_xcp; 873 num_xcp = NUM_XCC(adev->gfx.xcc_mask) / num_xcc_xcp; 874 875 switch (xcp_mgr->mode) { 876 case AMDGPU_SPX_PARTITION_MODE: 877 case AMDGPU_DPX_PARTITION_MODE: 878 case AMDGPU_TPX_PARTITION_MODE: 879 case AMDGPU_QPX_PARTITION_MODE: 880 case AMDGPU_CPX_PARTITION_MODE: 881 num_sdma_xcp = DIV_ROUND_UP(num_sdma, num_xcp); 882 num_vcn_xcp = DIV_ROUND_UP(num_vcn, num_xcp); 883 break; 884 default: 885 return -EINVAL; 886 } 887 888 if (num_vcn && num_xcp > num_vcn) 889 num_shared_vcn = num_xcp / num_vcn; 890 891 switch (ip_id) { 892 case AMDGPU_XCP_GFXHUB: 893 ip->inst_mask = XCP_INST_MASK(num_xcc_xcp, xcp_id); 894 ip->ip_funcs = &gfxhub_v12_1_xcp_funcs; 895 break; 896 case AMDGPU_XCP_GFX: 897 ip->inst_mask = XCP_INST_MASK(num_xcc_xcp, xcp_id); 898 ip->ip_funcs = &gfx_v12_1_xcp_funcs; 899 break; 900 case AMDGPU_XCP_SDMA: 901 ip->inst_mask = XCP_INST_MASK(num_sdma_xcp, xcp_id); 902 ip->ip_funcs = &sdma_v7_1_xcp_funcs; 903 break; 904 case AMDGPU_XCP_VCN: 905 ip->inst_mask = 906 XCP_INST_MASK(num_vcn_xcp, xcp_id / num_shared_vcn); 907 /* TODO : Assign IP funcs */ 908 break; 909 default: 910 return -EINVAL; 911 } 912 913 ip->ip_id = ip_id; 914 915 return 0; 916 } 917 918 static int soc_v1_0_get_xcp_res_info(struct amdgpu_xcp_mgr *xcp_mgr, 919 int mode, 920 struct amdgpu_xcp_cfg *xcp_cfg) 921 { 922 struct amdgpu_device *adev = xcp_mgr->adev; 923 int max_res[AMDGPU_XCP_RES_MAX] = {}; 924 bool res_lt_xcp; 925 int num_xcp, i; 926 u16 nps_modes; 927 928 if (!(xcp_mgr->supp_xcp_modes & BIT(mode))) 929 return -EINVAL; 930 931 max_res[AMDGPU_XCP_RES_XCC] = NUM_XCC(adev->gfx.xcc_mask); 932 max_res[AMDGPU_XCP_RES_DMA] = adev->sdma.num_instances; 933 max_res[AMDGPU_XCP_RES_DEC] = adev->vcn.num_vcn_inst; 934 max_res[AMDGPU_XCP_RES_JPEG] = adev->jpeg.num_jpeg_inst; 935 936 switch (mode) { 937 case AMDGPU_SPX_PARTITION_MODE: 938 num_xcp = 1; 939 nps_modes = BIT(AMDGPU_NPS1_PARTITION_MODE); 940 break; 941 case AMDGPU_DPX_PARTITION_MODE: 942 num_xcp = 2; 943 nps_modes = BIT(AMDGPU_NPS1_PARTITION_MODE) | 944 BIT(AMDGPU_NPS2_PARTITION_MODE); 945 break; 946 case AMDGPU_TPX_PARTITION_MODE: 947 num_xcp = 3; 948 nps_modes = BIT(AMDGPU_NPS1_PARTITION_MODE) | 949 BIT(AMDGPU_NPS4_PARTITION_MODE); 950 break; 951 case AMDGPU_QPX_PARTITION_MODE: 952 num_xcp = 4; 953 nps_modes = BIT(AMDGPU_NPS1_PARTITION_MODE) | 954 BIT(AMDGPU_NPS4_PARTITION_MODE); 955 break; 956 case AMDGPU_CPX_PARTITION_MODE: 957 num_xcp = NUM_XCC(adev->gfx.xcc_mask); 958 nps_modes = BIT(AMDGPU_NPS1_PARTITION_MODE) | 959 BIT(AMDGPU_NPS4_PARTITION_MODE); 960 break; 961 default: 962 return -EINVAL; 963 } 964 965 xcp_cfg->compatible_nps_modes = 966 (adev->gmc.supported_nps_modes & nps_modes); 967 xcp_cfg->num_res = ARRAY_SIZE(max_res); 968 969 for (i = 0; i < xcp_cfg->num_res; i++) { 970 xcp_cfg->xcp_res[i].id = i; 971 if (!max_res[i]) 972 continue; 973 res_lt_xcp = max_res[i] < num_xcp; 974 xcp_cfg->xcp_res[i].num_inst = 975 res_lt_xcp ? 1 : max_res[i] / num_xcp; 976 xcp_cfg->xcp_res[i].num_inst = 977 i == AMDGPU_XCP_RES_JPEG ? 978 xcp_cfg->xcp_res[i].num_inst * 979 adev->jpeg.num_jpeg_rings : xcp_cfg->xcp_res[i].num_inst; 980 xcp_cfg->xcp_res[i].num_shared = 981 res_lt_xcp ? num_xcp / max_res[i] : 1; 982 } 983 984 return 0; 985 } 986 987 static enum amdgpu_gfx_partition __soc_v1_0_get_auto_mode(struct amdgpu_xcp_mgr *xcp_mgr) 988 { 989 struct amdgpu_device *adev = xcp_mgr->adev; 990 int num_xcc; 991 992 num_xcc = NUM_XCC(xcp_mgr->adev->gfx.xcc_mask); 993 994 if (adev->gmc.num_mem_partitions == 1) 995 return AMDGPU_SPX_PARTITION_MODE; 996 997 if (adev->gmc.num_mem_partitions == num_xcc) 998 return AMDGPU_CPX_PARTITION_MODE; 999 1000 if (adev->gmc.num_mem_partitions == 2) 1001 return AMDGPU_DPX_PARTITION_MODE; 1002 1003 return AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE; 1004 } 1005 1006 static bool __soc_v1_0_is_valid_mode(struct amdgpu_xcp_mgr *xcp_mgr, 1007 enum amdgpu_gfx_partition mode) 1008 { 1009 struct amdgpu_device *adev = xcp_mgr->adev; 1010 int num_xcc, num_xccs_per_xcp; 1011 1012 num_xcc = NUM_XCC(adev->gfx.xcc_mask); 1013 switch (mode) { 1014 case AMDGPU_SPX_PARTITION_MODE: 1015 return adev->gmc.num_mem_partitions == 1 && num_xcc > 0; 1016 case AMDGPU_DPX_PARTITION_MODE: 1017 return adev->gmc.num_mem_partitions <= 2 && (num_xcc % 2) == 0; 1018 case AMDGPU_TPX_PARTITION_MODE: 1019 return (adev->gmc.num_mem_partitions == 1 || 1020 adev->gmc.num_mem_partitions == 3) && 1021 ((num_xcc % 3) == 0); 1022 case AMDGPU_QPX_PARTITION_MODE: 1023 num_xccs_per_xcp = num_xcc / 4; 1024 return (adev->gmc.num_mem_partitions == 1 || 1025 adev->gmc.num_mem_partitions == 4) && 1026 (num_xccs_per_xcp >= 2); 1027 case AMDGPU_CPX_PARTITION_MODE: 1028 /* (num_xcc > 1) because 1 XCC is considered SPX, not CPX. 1029 * (num_xcc % adev->gmc.num_mem_partitions) == 0 because 1030 * num_compute_partitions can't be less than num_mem_partitions 1031 */ 1032 return (adev->gmc.num_mem_partitions == 1 || 1033 adev->gmc.num_mem_partitions == 4) && 1034 (num_xcc > 1) && 1035 (num_xcc % adev->gmc.num_mem_partitions) == 0; 1036 default: 1037 return false; 1038 } 1039 1040 return false; 1041 } 1042 1043 static void __soc_v1_0_update_available_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr) 1044 { 1045 int mode; 1046 1047 xcp_mgr->avail_xcp_modes = 0; 1048 1049 for_each_inst(mode, xcp_mgr->supp_xcp_modes) { 1050 if (__soc_v1_0_is_valid_mode(xcp_mgr, mode)) 1051 xcp_mgr->avail_xcp_modes |= BIT(mode); 1052 } 1053 } 1054 1055 static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, 1056 int mode, int *num_xcps) 1057 { 1058 int num_xcc_per_xcp, num_xcc, ret; 1059 struct amdgpu_device *adev; 1060 u32 flags = 0; 1061 1062 adev = xcp_mgr->adev; 1063 num_xcc = NUM_XCC(adev->gfx.xcc_mask); 1064 1065 if (mode == AMDGPU_AUTO_COMPUTE_PARTITION_MODE) { 1066 mode = __soc_v1_0_get_auto_mode(xcp_mgr); 1067 if (mode == AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE) { 1068 dev_err(adev->dev, 1069 "Invalid config, no compatible compute partition mode found, available memory partitions: %d", 1070 adev->gmc.num_mem_partitions); 1071 return -EINVAL; 1072 } 1073 } else if (!__soc_v1_0_is_valid_mode(xcp_mgr, mode)) { 1074 dev_err(adev->dev, 1075 "Invalid compute partition mode requested, requested: %s, available memory partitions: %d", 1076 amdgpu_gfx_compute_mode_desc(mode), adev->gmc.num_mem_partitions); 1077 return -EINVAL; 1078 } 1079 1080 if (adev->kfd.init_complete && !amdgpu_in_reset(adev)) 1081 flags |= AMDGPU_XCP_OPS_KFD; 1082 1083 ret = amdgpu_xcp_pre_partition_switch(xcp_mgr, flags); 1084 if (ret) 1085 goto out; 1086 1087 num_xcc_per_xcp = __soc_v1_0_get_xcc_per_xcp(xcp_mgr, mode); 1088 if (adev->gfx.imu.funcs && 1089 adev->gfx.imu.funcs->switch_compute_partition) { 1090 ret = adev->gfx.imu.funcs->switch_compute_partition(xcp_mgr->adev, num_xcc_per_xcp, mode); 1091 if (ret) 1092 goto out; 1093 } 1094 if (adev->gfx.imu.funcs && 1095 adev->gfx.imu.funcs->init_mcm_addr_lut && 1096 amdgpu_emu_mode) 1097 adev->gfx.imu.funcs->init_mcm_addr_lut(adev); 1098 1099 /* Init info about new xcps */ 1100 *num_xcps = num_xcc / num_xcc_per_xcp; 1101 amdgpu_xcp_init(xcp_mgr, *num_xcps, mode); 1102 1103 ret = amdgpu_xcp_post_partition_switch(xcp_mgr, flags); 1104 if (!ret) 1105 __soc_v1_0_update_available_partition_mode(xcp_mgr); 1106 out: 1107 return ret; 1108 } 1109 1110 #ifdef HAVE_ACPI_DEV_GET_FIRST_MATCH_DEV 1111 static int __soc_v1_0_get_xcp_mem_id(struct amdgpu_device *adev, 1112 int xcc_id, uint8_t *mem_id) 1113 { 1114 /* memory/spatial modes validation check is already done */ 1115 *mem_id = xcc_id / adev->gfx.num_xcc_per_xcp; 1116 *mem_id /= adev->xcp_mgr->num_xcp_per_mem_partition; 1117 1118 return 0; 1119 } 1120 1121 static int soc_v1_0_get_xcp_mem_id(struct amdgpu_xcp_mgr *xcp_mgr, 1122 struct amdgpu_xcp *xcp, uint8_t *mem_id) 1123 { 1124 struct amdgpu_numa_info numa_info; 1125 struct amdgpu_device *adev; 1126 uint32_t xcc_mask; 1127 int r, i, xcc_id; 1128 1129 adev = xcp_mgr->adev; 1130 /* TODO: BIOS is not returning the right info now 1131 * Check on this later 1132 */ 1133 /* 1134 if (adev->gmc.gmc_funcs->query_mem_partition_mode) 1135 mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev); 1136 */ 1137 if (adev->gmc.num_mem_partitions == 1) { 1138 /* Only one range */ 1139 *mem_id = 0; 1140 return 0; 1141 } 1142 1143 r = amdgpu_xcp_get_inst_details(xcp, AMDGPU_XCP_GFX, &xcc_mask); 1144 if (r || !xcc_mask) 1145 return -EINVAL; 1146 1147 xcc_id = ffs(xcc_mask) - 1; 1148 if (!adev->gmc.is_app_apu) 1149 return __soc_v1_0_get_xcp_mem_id(adev, xcc_id, mem_id); 1150 1151 r = amdgpu_acpi_get_mem_info(adev, xcc_id, &numa_info); 1152 1153 if (r) 1154 return r; 1155 1156 r = -EINVAL; 1157 for (i = 0; i < adev->gmc.num_mem_partitions; ++i) { 1158 if (adev->gmc.mem_partitions[i].numa.node == numa_info.nid) { 1159 *mem_id = i; 1160 r = 0; 1161 break; 1162 } 1163 } 1164 1165 return r; 1166 } 1167 #endif 1168 1169 static int soc_v1_0_get_xcp_ip_details(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id, 1170 enum AMDGPU_XCP_IP_BLOCK ip_id, 1171 struct amdgpu_xcp_ip *ip) 1172 { 1173 if (!ip) 1174 return -EINVAL; 1175 1176 return __soc_v1_0_get_xcp_ip_info(xcp_mgr, xcp_id, ip_id, ip); 1177 } 1178 1179 struct amdgpu_xcp_mgr_funcs soc_v1_0_xcp_funcs = { 1180 .switch_partition_mode = &soc_v1_0_switch_partition_mode, 1181 .query_partition_mode = &soc_v1_0_query_partition_mode, 1182 .get_ip_details = &soc_v1_0_get_xcp_ip_details, 1183 .get_xcp_res_info = &soc_v1_0_get_xcp_res_info, 1184 #ifdef HAVE_ACPI_DEV_GET_FIRST_MATCH_DEV 1185 .get_xcp_mem_id = &soc_v1_0_get_xcp_mem_id, 1186 #endif 1187 }; 1188 1189 static int soc_v1_0_xcp_mgr_init(struct amdgpu_device *adev) 1190 { 1191 int ret; 1192 1193 if (amdgpu_sriov_vf(adev)) 1194 soc_v1_0_xcp_funcs.switch_partition_mode = NULL; 1195 1196 ret = amdgpu_xcp_mgr_init(adev, AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE, 1197 1, &soc_v1_0_xcp_funcs); 1198 if (ret) 1199 return ret; 1200 1201 amdgpu_xcp_update_supported_modes(adev->xcp_mgr); 1202 /* TODO: Default memory node affinity init */ 1203 1204 return ret; 1205 } 1206 1207 uint32_t soc_v1_0_get_aid_mask(uint16_t xcc_mask) 1208 { 1209 int xcc_inst_per_aid = 4; 1210 uint32_t aid_mask = 0; 1211 int i; 1212 1213 for (i = 0; xcc_mask; xcc_mask >>= xcc_inst_per_aid, i++) { 1214 if (xcc_mask & GENMASK(xcc_inst_per_aid - 1, 0)) 1215 aid_mask |= BIT(i); 1216 } 1217 1218 return aid_mask; 1219 } 1220 1221 int soc_v1_0_init_soc_config(struct amdgpu_device *adev) 1222 { 1223 int ret, i; 1224 uint16_t sdma_mask = 0; 1225 1226 adev->aid_mask = soc_v1_0_get_aid_mask(adev->gfx.xcc_mask); 1227 1228 adev->sdma.num_inst_per_xcc = 2; 1229 for_each_inst(i, adev->gfx.xcc_mask) 1230 sdma_mask |= 1231 GENMASK(adev->sdma.num_inst_per_xcc - 1, 0) << 1232 (i * adev->sdma.num_inst_per_xcc); 1233 adev->sdma.sdma_mask = sdma_mask; 1234 adev->sdma.num_instances = NUM_XCC(adev->sdma.sdma_mask); 1235 1236 adev->vcn.harvest_config = 0; 1237 adev->vcn.num_inst_per_aid = 2; 1238 adev->vcn.num_vcn_inst = hweight32(adev->vcn.inst_mask); 1239 1240 adev->jpeg.harvest_config = 0; 1241 adev->jpeg.num_inst_per_aid = 2; 1242 adev->jpeg.num_jpeg_inst = hweight32(adev->jpeg.inst_mask); 1243 1244 ret = soc_v1_0_xcp_mgr_init(adev); 1245 if (ret) 1246 return ret; 1247 1248 amdgpu_ip_map_init(adev); 1249 1250 return 0; 1251 } 1252 1253 bool soc_v1_0_normalize_xcc_reg_range(uint32_t reg) 1254 { 1255 if (((reg >= XCC_REG_RANGE_0_LOW) && (reg < XCC_REG_RANGE_0_HIGH)) || 1256 ((reg >= XCC_REG_RANGE_1_LOW) && (reg < XCC_REG_RANGE_1_HIGH))) 1257 return true; 1258 else 1259 return false; 1260 } 1261 1262 uint32_t soc_v1_0_normalize_xcc_reg_offset(uint32_t reg) 1263 { 1264 uint32_t normalized_reg = NORMALIZE_XCC_REG_OFFSET(reg); 1265 1266 /* If it is an XCC reg, normalize the reg to keep 1267 * lower 16 bits in local xcc */ 1268 1269 if (soc_v1_0_normalize_xcc_reg_range(normalized_reg)) 1270 return normalized_reg; 1271 else 1272 return reg; 1273 } 1274 1275 bool soc_v1_0_mid1_reg_range(uint32_t reg) 1276 { 1277 uint32_t normalized_reg = soc_v1_0_normalize_xcc_reg_offset(reg); 1278 1279 if (soc_v1_0_normalize_xcc_reg_range(normalized_reg)) 1280 return false; 1281 1282 if ((reg >= MID1_REG_RANGE_0_LOW) && (reg < MID1_REG_RANGE_0_HIGH)) 1283 return true; 1284 else 1285 return false; 1286 } 1287 1288 uint32_t soc_v1_0_normalize_reg_offset(uint32_t reg) 1289 { 1290 uint32_t normalized_reg = soc_v1_0_normalize_xcc_reg_offset(reg); 1291 1292 if (soc_v1_0_normalize_xcc_reg_range(normalized_reg)) 1293 return soc_v1_0_normalize_xcc_reg_offset(reg); 1294 1295 /* check if the reg offset is inside MID1. */ 1296 if (soc_v1_0_mid1_reg_range(reg)) 1297 return NORMALIZE_MID_REG_OFFSET(reg); 1298 1299 return reg; 1300 } 1301 1302