1 /* 2 * Copyright 2019 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 "amdgpu.h" 29 #include "amdgpu_atombios.h" 30 #include "amdgpu_ih.h" 31 #include "amdgpu_uvd.h" 32 #include "amdgpu_vce.h" 33 #include "amdgpu_ucode.h" 34 #include "amdgpu_psp.h" 35 #include "amdgpu_smu.h" 36 #include "atom.h" 37 #include "amd_pcie.h" 38 39 #include "gc/gc_10_1_0_offset.h" 40 #include "gc/gc_10_1_0_sh_mask.h" 41 #include "hdp/hdp_5_0_0_offset.h" 42 #include "hdp/hdp_5_0_0_sh_mask.h" 43 44 #include "soc15.h" 45 #include "soc15_common.h" 46 #include "gmc_v10_0.h" 47 #include "gfxhub_v2_0.h" 48 #include "mmhub_v2_0.h" 49 #include "nbio_v2_3.h" 50 #include "nv.h" 51 #include "navi10_ih.h" 52 #include "gfx_v10_0.h" 53 #include "sdma_v5_0.h" 54 #include "vcn_v2_0.h" 55 #include "dce_virtual.h" 56 #include "mes_v10_1.h" 57 #include "mxgpu_nv.h" 58 59 static const struct amd_ip_funcs nv_common_ip_funcs; 60 61 /* 62 * Indirect registers accessor 63 */ 64 static u32 nv_pcie_rreg(struct amdgpu_device *adev, u32 reg) 65 { 66 unsigned long flags, address, data; 67 u32 r; 68 address = adev->nbio.funcs->get_pcie_index_offset(adev); 69 data = adev->nbio.funcs->get_pcie_data_offset(adev); 70 71 spin_lock_irqsave(&adev->pcie_idx_lock, flags); 72 WREG32(address, reg); 73 (void)RREG32(address); 74 r = RREG32(data); 75 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags); 76 return r; 77 } 78 79 static void nv_pcie_wreg(struct amdgpu_device *adev, u32 reg, u32 v) 80 { 81 unsigned long flags, address, data; 82 83 address = adev->nbio.funcs->get_pcie_index_offset(adev); 84 data = adev->nbio.funcs->get_pcie_data_offset(adev); 85 86 spin_lock_irqsave(&adev->pcie_idx_lock, flags); 87 WREG32(address, reg); 88 (void)RREG32(address); 89 WREG32(data, v); 90 (void)RREG32(data); 91 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags); 92 } 93 94 static u32 nv_didt_rreg(struct amdgpu_device *adev, u32 reg) 95 { 96 unsigned long flags, address, data; 97 u32 r; 98 99 address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX); 100 data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA); 101 102 spin_lock_irqsave(&adev->didt_idx_lock, flags); 103 WREG32(address, (reg)); 104 r = RREG32(data); 105 spin_unlock_irqrestore(&adev->didt_idx_lock, flags); 106 return r; 107 } 108 109 static void nv_didt_wreg(struct amdgpu_device *adev, u32 reg, u32 v) 110 { 111 unsigned long flags, address, data; 112 113 address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX); 114 data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA); 115 116 spin_lock_irqsave(&adev->didt_idx_lock, flags); 117 WREG32(address, (reg)); 118 WREG32(data, (v)); 119 spin_unlock_irqrestore(&adev->didt_idx_lock, flags); 120 } 121 122 static u32 nv_get_config_memsize(struct amdgpu_device *adev) 123 { 124 return adev->nbio.funcs->get_memsize(adev); 125 } 126 127 static u32 nv_get_xclk(struct amdgpu_device *adev) 128 { 129 return adev->clock.spll.reference_freq; 130 } 131 132 133 void nv_grbm_select(struct amdgpu_device *adev, 134 u32 me, u32 pipe, u32 queue, u32 vmid) 135 { 136 u32 grbm_gfx_cntl = 0; 137 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, PIPEID, pipe); 138 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, MEID, me); 139 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, VMID, vmid); 140 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, QUEUEID, queue); 141 142 WREG32(SOC15_REG_OFFSET(GC, 0, mmGRBM_GFX_CNTL), grbm_gfx_cntl); 143 } 144 145 static void nv_vga_set_state(struct amdgpu_device *adev, bool state) 146 { 147 /* todo */ 148 } 149 150 static bool nv_read_disabled_bios(struct amdgpu_device *adev) 151 { 152 /* todo */ 153 return false; 154 } 155 156 static bool nv_read_bios_from_rom(struct amdgpu_device *adev, 157 u8 *bios, u32 length_bytes) 158 { 159 /* TODO: will implement it when SMU header is available */ 160 return false; 161 } 162 163 static struct soc15_allowed_register_entry nv_allowed_read_registers[] = { 164 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS)}, 165 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS2)}, 166 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE0)}, 167 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE1)}, 168 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE2)}, 169 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE3)}, 170 #if 0 /* TODO: will set it when SDMA header is available */ 171 { SOC15_REG_ENTRY(SDMA0, 0, mmSDMA0_STATUS_REG)}, 172 { SOC15_REG_ENTRY(SDMA1, 0, mmSDMA1_STATUS_REG)}, 173 #endif 174 { SOC15_REG_ENTRY(GC, 0, mmCP_STAT)}, 175 { SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT1)}, 176 { SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT2)}, 177 { SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT3)}, 178 { SOC15_REG_ENTRY(GC, 0, mmCP_CPF_BUSY_STAT)}, 179 { SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STALLED_STAT1)}, 180 { SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STATUS)}, 181 { SOC15_REG_ENTRY(GC, 0, mmCP_CPC_BUSY_STAT)}, 182 { SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STALLED_STAT1)}, 183 { SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STATUS)}, 184 { SOC15_REG_ENTRY(GC, 0, mmGB_ADDR_CONFIG)}, 185 }; 186 187 static uint32_t nv_read_indexed_register(struct amdgpu_device *adev, u32 se_num, 188 u32 sh_num, u32 reg_offset) 189 { 190 uint32_t val; 191 192 mutex_lock(&adev->grbm_idx_mutex); 193 if (se_num != 0xffffffff || sh_num != 0xffffffff) 194 amdgpu_gfx_select_se_sh(adev, se_num, sh_num, 0xffffffff); 195 196 val = RREG32(reg_offset); 197 198 if (se_num != 0xffffffff || sh_num != 0xffffffff) 199 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff); 200 mutex_unlock(&adev->grbm_idx_mutex); 201 return val; 202 } 203 204 static uint32_t nv_get_register_value(struct amdgpu_device *adev, 205 bool indexed, u32 se_num, 206 u32 sh_num, u32 reg_offset) 207 { 208 if (indexed) { 209 return nv_read_indexed_register(adev, se_num, sh_num, reg_offset); 210 } else { 211 if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmGB_ADDR_CONFIG)) 212 return adev->gfx.config.gb_addr_config; 213 return RREG32(reg_offset); 214 } 215 } 216 217 static int nv_read_register(struct amdgpu_device *adev, u32 se_num, 218 u32 sh_num, u32 reg_offset, u32 *value) 219 { 220 uint32_t i; 221 struct soc15_allowed_register_entry *en; 222 223 *value = 0; 224 for (i = 0; i < ARRAY_SIZE(nv_allowed_read_registers); i++) { 225 en = &nv_allowed_read_registers[i]; 226 if (reg_offset != 227 (adev->reg_offset[en->hwip][en->inst][en->seg] + en->reg_offset)) 228 continue; 229 230 *value = nv_get_register_value(adev, 231 nv_allowed_read_registers[i].grbm_indexed, 232 se_num, sh_num, reg_offset); 233 return 0; 234 } 235 return -EINVAL; 236 } 237 238 #if 0 239 static void nv_gpu_pci_config_reset(struct amdgpu_device *adev) 240 { 241 u32 i; 242 243 dev_info(adev->dev, "GPU pci config reset\n"); 244 245 /* disable BM */ 246 pci_clear_master(adev->pdev); 247 /* reset */ 248 amdgpu_pci_config_reset(adev); 249 250 udelay(100); 251 252 /* wait for asic to come out of reset */ 253 for (i = 0; i < adev->usec_timeout; i++) { 254 u32 memsize = nbio_v2_3_get_memsize(adev); 255 if (memsize != 0xffffffff) 256 break; 257 udelay(1); 258 } 259 260 } 261 #endif 262 263 static int nv_asic_mode1_reset(struct amdgpu_device *adev) 264 { 265 u32 i; 266 int ret = 0; 267 268 amdgpu_atombios_scratch_regs_engine_hung(adev, true); 269 270 dev_info(adev->dev, "GPU mode1 reset\n"); 271 272 /* disable BM */ 273 pci_clear_master(adev->pdev); 274 275 pci_save_state(adev->pdev); 276 277 ret = psp_gpu_reset(adev); 278 if (ret) 279 dev_err(adev->dev, "GPU mode1 reset failed\n"); 280 281 pci_restore_state(adev->pdev); 282 283 /* wait for asic to come out of reset */ 284 for (i = 0; i < adev->usec_timeout; i++) { 285 u32 memsize = adev->nbio.funcs->get_memsize(adev); 286 287 if (memsize != 0xffffffff) 288 break; 289 udelay(1); 290 } 291 292 amdgpu_atombios_scratch_regs_engine_hung(adev, false); 293 294 return ret; 295 } 296 297 static enum amd_reset_method 298 nv_asic_reset_method(struct amdgpu_device *adev) 299 { 300 struct smu_context *smu = &adev->smu; 301 302 if (!amdgpu_sriov_vf(adev) && smu_baco_is_support(smu)) 303 return AMD_RESET_METHOD_BACO; 304 else 305 return AMD_RESET_METHOD_MODE1; 306 } 307 308 static int nv_asic_reset(struct amdgpu_device *adev) 309 { 310 311 /* FIXME: it doesn't work since vega10 */ 312 #if 0 313 amdgpu_atombios_scratch_regs_engine_hung(adev, true); 314 315 nv_gpu_pci_config_reset(adev); 316 317 amdgpu_atombios_scratch_regs_engine_hung(adev, false); 318 #endif 319 int ret = 0; 320 struct smu_context *smu = &adev->smu; 321 322 if (nv_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) { 323 if (!adev->in_suspend) 324 amdgpu_inc_vram_lost(adev); 325 ret = smu_baco_reset(smu); 326 } else { 327 if (!adev->in_suspend) 328 amdgpu_inc_vram_lost(adev); 329 ret = nv_asic_mode1_reset(adev); 330 } 331 332 return ret; 333 } 334 335 static int nv_set_uvd_clocks(struct amdgpu_device *adev, u32 vclk, u32 dclk) 336 { 337 /* todo */ 338 return 0; 339 } 340 341 static int nv_set_vce_clocks(struct amdgpu_device *adev, u32 evclk, u32 ecclk) 342 { 343 /* todo */ 344 return 0; 345 } 346 347 static void nv_pcie_gen3_enable(struct amdgpu_device *adev) 348 { 349 if (pci_is_root_bus(adev->pdev->bus)) 350 return; 351 352 if (amdgpu_pcie_gen2 == 0) 353 return; 354 355 if (!(adev->pm.pcie_gen_mask & (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 | 356 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3))) 357 return; 358 359 /* todo */ 360 } 361 362 static void nv_program_aspm(struct amdgpu_device *adev) 363 { 364 365 if (amdgpu_aspm == 0) 366 return; 367 368 /* todo */ 369 } 370 371 static void nv_enable_doorbell_aperture(struct amdgpu_device *adev, 372 bool enable) 373 { 374 adev->nbio.funcs->enable_doorbell_aperture(adev, enable); 375 adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, enable); 376 } 377 378 static const struct amdgpu_ip_block_version nv_common_ip_block = 379 { 380 .type = AMD_IP_BLOCK_TYPE_COMMON, 381 .major = 1, 382 .minor = 0, 383 .rev = 0, 384 .funcs = &nv_common_ip_funcs, 385 }; 386 387 static int nv_reg_base_init(struct amdgpu_device *adev) 388 { 389 int r; 390 391 if (amdgpu_discovery) { 392 r = amdgpu_discovery_reg_base_init(adev); 393 if (r) { 394 DRM_WARN("failed to init reg base from ip discovery table, " 395 "fallback to legacy init method\n"); 396 goto legacy_init; 397 } 398 399 return 0; 400 } 401 402 legacy_init: 403 switch (adev->asic_type) { 404 case CHIP_NAVI10: 405 navi10_reg_base_init(adev); 406 break; 407 case CHIP_NAVI14: 408 navi14_reg_base_init(adev); 409 break; 410 case CHIP_NAVI12: 411 navi12_reg_base_init(adev); 412 break; 413 default: 414 return -EINVAL; 415 } 416 417 return 0; 418 } 419 420 int nv_set_ip_blocks(struct amdgpu_device *adev) 421 { 422 int r; 423 424 /* Set IP register base before any HW register access */ 425 r = nv_reg_base_init(adev); 426 if (r) 427 return r; 428 429 adev->nbio.funcs = &nbio_v2_3_funcs; 430 adev->nbio.hdp_flush_reg = &nbio_v2_3_hdp_flush_reg; 431 432 adev->nbio.funcs->detect_hw_virt(adev); 433 434 if (amdgpu_sriov_vf(adev)) 435 adev->virt.ops = &xgpu_nv_virt_ops; 436 437 switch (adev->asic_type) { 438 case CHIP_NAVI10: 439 case CHIP_NAVI14: 440 amdgpu_device_ip_block_add(adev, &nv_common_ip_block); 441 amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block); 442 amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block); 443 amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block); 444 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP && 445 is_support_sw_smu(adev) && !amdgpu_sriov_vf(adev)) 446 amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block); 447 if (adev->enable_virtual_display || amdgpu_sriov_vf(adev)) 448 amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block); 449 #if defined(CONFIG_DRM_AMD_DC) 450 else if (amdgpu_device_has_dc_support(adev)) 451 amdgpu_device_ip_block_add(adev, &dm_ip_block); 452 #endif 453 amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block); 454 amdgpu_device_ip_block_add(adev, &sdma_v5_0_ip_block); 455 if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT && 456 is_support_sw_smu(adev) && !amdgpu_sriov_vf(adev)) 457 amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block); 458 amdgpu_device_ip_block_add(adev, &vcn_v2_0_ip_block); 459 if (adev->enable_mes) 460 amdgpu_device_ip_block_add(adev, &mes_v10_1_ip_block); 461 break; 462 case CHIP_NAVI12: 463 amdgpu_device_ip_block_add(adev, &nv_common_ip_block); 464 amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block); 465 amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block); 466 amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block); 467 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP && 468 is_support_sw_smu(adev) && !amdgpu_sriov_vf(adev)) 469 amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block); 470 if (adev->enable_virtual_display || amdgpu_sriov_vf(adev)) 471 amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block); 472 #if defined(CONFIG_DRM_AMD_DC) 473 else if (amdgpu_device_has_dc_support(adev)) 474 amdgpu_device_ip_block_add(adev, &dm_ip_block); 475 #endif 476 amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block); 477 amdgpu_device_ip_block_add(adev, &sdma_v5_0_ip_block); 478 if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT && 479 is_support_sw_smu(adev) && !amdgpu_sriov_vf(adev)) 480 amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block); 481 amdgpu_device_ip_block_add(adev, &vcn_v2_0_ip_block); 482 break; 483 default: 484 return -EINVAL; 485 } 486 487 return 0; 488 } 489 490 static uint32_t nv_get_rev_id(struct amdgpu_device *adev) 491 { 492 return adev->nbio.funcs->get_rev_id(adev); 493 } 494 495 static void nv_flush_hdp(struct amdgpu_device *adev, struct amdgpu_ring *ring) 496 { 497 adev->nbio.funcs->hdp_flush(adev, ring); 498 } 499 500 static void nv_invalidate_hdp(struct amdgpu_device *adev, 501 struct amdgpu_ring *ring) 502 { 503 if (!ring || !ring->funcs->emit_wreg) { 504 WREG32_SOC15_NO_KIQ(NBIO, 0, mmHDP_READ_CACHE_INVALIDATE, 1); 505 } else { 506 amdgpu_ring_emit_wreg(ring, SOC15_REG_OFFSET( 507 HDP, 0, mmHDP_READ_CACHE_INVALIDATE), 1); 508 } 509 } 510 511 static bool nv_need_full_reset(struct amdgpu_device *adev) 512 { 513 return true; 514 } 515 516 static void nv_get_pcie_usage(struct amdgpu_device *adev, 517 uint64_t *count0, 518 uint64_t *count1) 519 { 520 /*TODO*/ 521 } 522 523 static bool nv_need_reset_on_init(struct amdgpu_device *adev) 524 { 525 #if 0 526 u32 sol_reg; 527 528 if (adev->flags & AMD_IS_APU) 529 return false; 530 531 /* Check sOS sign of life register to confirm sys driver and sOS 532 * are already been loaded. 533 */ 534 sol_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_81); 535 if (sol_reg) 536 return true; 537 #endif 538 /* TODO: re-enable it when mode1 reset is functional */ 539 return false; 540 } 541 542 static void nv_init_doorbell_index(struct amdgpu_device *adev) 543 { 544 adev->doorbell_index.kiq = AMDGPU_NAVI10_DOORBELL_KIQ; 545 adev->doorbell_index.mec_ring0 = AMDGPU_NAVI10_DOORBELL_MEC_RING0; 546 adev->doorbell_index.mec_ring1 = AMDGPU_NAVI10_DOORBELL_MEC_RING1; 547 adev->doorbell_index.mec_ring2 = AMDGPU_NAVI10_DOORBELL_MEC_RING2; 548 adev->doorbell_index.mec_ring3 = AMDGPU_NAVI10_DOORBELL_MEC_RING3; 549 adev->doorbell_index.mec_ring4 = AMDGPU_NAVI10_DOORBELL_MEC_RING4; 550 adev->doorbell_index.mec_ring5 = AMDGPU_NAVI10_DOORBELL_MEC_RING5; 551 adev->doorbell_index.mec_ring6 = AMDGPU_NAVI10_DOORBELL_MEC_RING6; 552 adev->doorbell_index.mec_ring7 = AMDGPU_NAVI10_DOORBELL_MEC_RING7; 553 adev->doorbell_index.userqueue_start = AMDGPU_NAVI10_DOORBELL_USERQUEUE_START; 554 adev->doorbell_index.userqueue_end = AMDGPU_NAVI10_DOORBELL_USERQUEUE_END; 555 adev->doorbell_index.gfx_ring0 = AMDGPU_NAVI10_DOORBELL_GFX_RING0; 556 adev->doorbell_index.gfx_ring1 = AMDGPU_NAVI10_DOORBELL_GFX_RING1; 557 adev->doorbell_index.sdma_engine[0] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE0; 558 adev->doorbell_index.sdma_engine[1] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE1; 559 adev->doorbell_index.ih = AMDGPU_NAVI10_DOORBELL_IH; 560 adev->doorbell_index.vcn.vcn_ring0_1 = AMDGPU_NAVI10_DOORBELL64_VCN0_1; 561 adev->doorbell_index.vcn.vcn_ring2_3 = AMDGPU_NAVI10_DOORBELL64_VCN2_3; 562 adev->doorbell_index.vcn.vcn_ring4_5 = AMDGPU_NAVI10_DOORBELL64_VCN4_5; 563 adev->doorbell_index.vcn.vcn_ring6_7 = AMDGPU_NAVI10_DOORBELL64_VCN6_7; 564 adev->doorbell_index.first_non_cp = AMDGPU_NAVI10_DOORBELL64_FIRST_NON_CP; 565 adev->doorbell_index.last_non_cp = AMDGPU_NAVI10_DOORBELL64_LAST_NON_CP; 566 567 adev->doorbell_index.max_assignment = AMDGPU_NAVI10_DOORBELL_MAX_ASSIGNMENT << 1; 568 adev->doorbell_index.sdma_doorbell_range = 20; 569 } 570 571 static const struct amdgpu_asic_funcs nv_asic_funcs = 572 { 573 .read_disabled_bios = &nv_read_disabled_bios, 574 .read_bios_from_rom = &nv_read_bios_from_rom, 575 .read_register = &nv_read_register, 576 .reset = &nv_asic_reset, 577 .reset_method = &nv_asic_reset_method, 578 .set_vga_state = &nv_vga_set_state, 579 .get_xclk = &nv_get_xclk, 580 .set_uvd_clocks = &nv_set_uvd_clocks, 581 .set_vce_clocks = &nv_set_vce_clocks, 582 .get_config_memsize = &nv_get_config_memsize, 583 .flush_hdp = &nv_flush_hdp, 584 .invalidate_hdp = &nv_invalidate_hdp, 585 .init_doorbell_index = &nv_init_doorbell_index, 586 .need_full_reset = &nv_need_full_reset, 587 .get_pcie_usage = &nv_get_pcie_usage, 588 .need_reset_on_init = &nv_need_reset_on_init, 589 }; 590 591 static int nv_common_early_init(void *handle) 592 { 593 #define MMIO_REG_HOLE_OFFSET (0x80000 - PAGE_SIZE) 594 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 595 596 adev->rmmio_remap.reg_offset = MMIO_REG_HOLE_OFFSET; 597 adev->rmmio_remap.bus_addr = adev->rmmio_base + MMIO_REG_HOLE_OFFSET; 598 adev->smc_rreg = NULL; 599 adev->smc_wreg = NULL; 600 adev->pcie_rreg = &nv_pcie_rreg; 601 adev->pcie_wreg = &nv_pcie_wreg; 602 603 /* TODO: will add them during VCN v2 implementation */ 604 adev->uvd_ctx_rreg = NULL; 605 adev->uvd_ctx_wreg = NULL; 606 607 adev->didt_rreg = &nv_didt_rreg; 608 adev->didt_wreg = &nv_didt_wreg; 609 610 adev->asic_funcs = &nv_asic_funcs; 611 612 adev->rev_id = nv_get_rev_id(adev); 613 adev->external_rev_id = 0xff; 614 switch (adev->asic_type) { 615 case CHIP_NAVI10: 616 adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG | 617 AMD_CG_SUPPORT_GFX_CGCG | 618 AMD_CG_SUPPORT_IH_CG | 619 AMD_CG_SUPPORT_HDP_MGCG | 620 AMD_CG_SUPPORT_HDP_LS | 621 AMD_CG_SUPPORT_SDMA_MGCG | 622 AMD_CG_SUPPORT_SDMA_LS | 623 AMD_CG_SUPPORT_MC_MGCG | 624 AMD_CG_SUPPORT_MC_LS | 625 AMD_CG_SUPPORT_ATHUB_MGCG | 626 AMD_CG_SUPPORT_ATHUB_LS | 627 AMD_CG_SUPPORT_VCN_MGCG | 628 AMD_CG_SUPPORT_BIF_MGCG | 629 AMD_CG_SUPPORT_BIF_LS; 630 adev->pg_flags = AMD_PG_SUPPORT_VCN | 631 AMD_PG_SUPPORT_VCN_DPG | 632 AMD_PG_SUPPORT_ATHUB; 633 adev->external_rev_id = adev->rev_id + 0x1; 634 break; 635 case CHIP_NAVI14: 636 adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG | 637 AMD_CG_SUPPORT_GFX_CGCG | 638 AMD_CG_SUPPORT_IH_CG | 639 AMD_CG_SUPPORT_HDP_MGCG | 640 AMD_CG_SUPPORT_HDP_LS | 641 AMD_CG_SUPPORT_SDMA_MGCG | 642 AMD_CG_SUPPORT_SDMA_LS | 643 AMD_CG_SUPPORT_MC_MGCG | 644 AMD_CG_SUPPORT_MC_LS | 645 AMD_CG_SUPPORT_ATHUB_MGCG | 646 AMD_CG_SUPPORT_ATHUB_LS | 647 AMD_CG_SUPPORT_VCN_MGCG | 648 AMD_CG_SUPPORT_BIF_MGCG | 649 AMD_CG_SUPPORT_BIF_LS; 650 adev->pg_flags = AMD_PG_SUPPORT_VCN | 651 AMD_PG_SUPPORT_VCN_DPG; 652 adev->external_rev_id = adev->rev_id + 20; 653 break; 654 case CHIP_NAVI12: 655 adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG | 656 AMD_CG_SUPPORT_GFX_MGLS | 657 AMD_CG_SUPPORT_GFX_CGCG | 658 AMD_CG_SUPPORT_GFX_CP_LS | 659 AMD_CG_SUPPORT_GFX_RLC_LS | 660 AMD_CG_SUPPORT_IH_CG | 661 AMD_CG_SUPPORT_HDP_MGCG | 662 AMD_CG_SUPPORT_HDP_LS | 663 AMD_CG_SUPPORT_SDMA_MGCG | 664 AMD_CG_SUPPORT_SDMA_LS | 665 AMD_CG_SUPPORT_MC_MGCG | 666 AMD_CG_SUPPORT_MC_LS | 667 AMD_CG_SUPPORT_ATHUB_MGCG | 668 AMD_CG_SUPPORT_ATHUB_LS | 669 AMD_CG_SUPPORT_VCN_MGCG; 670 adev->pg_flags = AMD_PG_SUPPORT_VCN | 671 AMD_PG_SUPPORT_VCN_DPG | 672 AMD_PG_SUPPORT_ATHUB; 673 adev->external_rev_id = adev->rev_id + 0xa; 674 break; 675 default: 676 /* FIXME: not supported yet */ 677 return -EINVAL; 678 } 679 680 if (amdgpu_sriov_vf(adev)) { 681 amdgpu_virt_init_setting(adev); 682 xgpu_nv_mailbox_set_irq_funcs(adev); 683 } 684 685 return 0; 686 } 687 688 static int nv_common_late_init(void *handle) 689 { 690 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 691 692 if (amdgpu_sriov_vf(adev)) 693 xgpu_nv_mailbox_get_irq(adev); 694 695 return 0; 696 } 697 698 static int nv_common_sw_init(void *handle) 699 { 700 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 701 702 if (amdgpu_sriov_vf(adev)) 703 xgpu_nv_mailbox_add_irq_id(adev); 704 705 return 0; 706 } 707 708 static int nv_common_sw_fini(void *handle) 709 { 710 return 0; 711 } 712 713 static int nv_common_hw_init(void *handle) 714 { 715 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 716 717 /* enable pcie gen2/3 link */ 718 nv_pcie_gen3_enable(adev); 719 /* enable aspm */ 720 nv_program_aspm(adev); 721 /* setup nbio registers */ 722 adev->nbio.funcs->init_registers(adev); 723 /* remap HDP registers to a hole in mmio space, 724 * for the purpose of expose those registers 725 * to process space 726 */ 727 if (adev->nbio.funcs->remap_hdp_registers) 728 adev->nbio.funcs->remap_hdp_registers(adev); 729 /* enable the doorbell aperture */ 730 nv_enable_doorbell_aperture(adev, true); 731 732 return 0; 733 } 734 735 static int nv_common_hw_fini(void *handle) 736 { 737 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 738 739 /* disable the doorbell aperture */ 740 nv_enable_doorbell_aperture(adev, false); 741 742 return 0; 743 } 744 745 static int nv_common_suspend(void *handle) 746 { 747 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 748 749 return nv_common_hw_fini(adev); 750 } 751 752 static int nv_common_resume(void *handle) 753 { 754 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 755 756 return nv_common_hw_init(adev); 757 } 758 759 static bool nv_common_is_idle(void *handle) 760 { 761 return true; 762 } 763 764 static int nv_common_wait_for_idle(void *handle) 765 { 766 return 0; 767 } 768 769 static int nv_common_soft_reset(void *handle) 770 { 771 return 0; 772 } 773 774 static void nv_update_hdp_mem_power_gating(struct amdgpu_device *adev, 775 bool enable) 776 { 777 uint32_t hdp_clk_cntl, hdp_clk_cntl1; 778 uint32_t hdp_mem_pwr_cntl; 779 780 if (!(adev->cg_flags & (AMD_CG_SUPPORT_HDP_LS | 781 AMD_CG_SUPPORT_HDP_DS | 782 AMD_CG_SUPPORT_HDP_SD))) 783 return; 784 785 hdp_clk_cntl = hdp_clk_cntl1 = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL); 786 hdp_mem_pwr_cntl = RREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL); 787 788 /* Before doing clock/power mode switch, 789 * forced on IPH & RC clock */ 790 hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL, 791 IPH_MEM_CLK_SOFT_OVERRIDE, 1); 792 hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL, 793 RC_MEM_CLK_SOFT_OVERRIDE, 1); 794 WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl); 795 796 /* HDP 5.0 doesn't support dynamic power mode switch, 797 * disable clock and power gating before any changing */ 798 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL, 799 IPH_MEM_POWER_CTRL_EN, 0); 800 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL, 801 IPH_MEM_POWER_LS_EN, 0); 802 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL, 803 IPH_MEM_POWER_DS_EN, 0); 804 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL, 805 IPH_MEM_POWER_SD_EN, 0); 806 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL, 807 RC_MEM_POWER_CTRL_EN, 0); 808 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL, 809 RC_MEM_POWER_LS_EN, 0); 810 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL, 811 RC_MEM_POWER_DS_EN, 0); 812 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL, 813 RC_MEM_POWER_SD_EN, 0); 814 WREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL, hdp_mem_pwr_cntl); 815 816 /* only one clock gating mode (LS/DS/SD) can be enabled */ 817 if (adev->cg_flags & AMD_CG_SUPPORT_HDP_LS) { 818 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, 819 HDP_MEM_POWER_CTRL, 820 IPH_MEM_POWER_LS_EN, enable); 821 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, 822 HDP_MEM_POWER_CTRL, 823 RC_MEM_POWER_LS_EN, enable); 824 } else if (adev->cg_flags & AMD_CG_SUPPORT_HDP_DS) { 825 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, 826 HDP_MEM_POWER_CTRL, 827 IPH_MEM_POWER_DS_EN, enable); 828 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, 829 HDP_MEM_POWER_CTRL, 830 RC_MEM_POWER_DS_EN, enable); 831 } else if (adev->cg_flags & AMD_CG_SUPPORT_HDP_SD) { 832 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, 833 HDP_MEM_POWER_CTRL, 834 IPH_MEM_POWER_SD_EN, enable); 835 /* RC should not use shut down mode, fallback to ds */ 836 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, 837 HDP_MEM_POWER_CTRL, 838 RC_MEM_POWER_DS_EN, enable); 839 } 840 841 WREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL, hdp_mem_pwr_cntl); 842 843 /* restore IPH & RC clock override after clock/power mode changing */ 844 WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl1); 845 } 846 847 static void nv_update_hdp_clock_gating(struct amdgpu_device *adev, 848 bool enable) 849 { 850 uint32_t hdp_clk_cntl; 851 852 if (!(adev->cg_flags & AMD_CG_SUPPORT_HDP_MGCG)) 853 return; 854 855 hdp_clk_cntl = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL); 856 857 if (enable) { 858 hdp_clk_cntl &= 859 ~(uint32_t) 860 (HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK | 861 HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK | 862 HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK | 863 HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK | 864 HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK | 865 HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK); 866 } else { 867 hdp_clk_cntl |= HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK | 868 HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK | 869 HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK | 870 HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK | 871 HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK | 872 HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK; 873 } 874 875 WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl); 876 } 877 878 static int nv_common_set_clockgating_state(void *handle, 879 enum amd_clockgating_state state) 880 { 881 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 882 883 if (amdgpu_sriov_vf(adev)) 884 return 0; 885 886 switch (adev->asic_type) { 887 case CHIP_NAVI10: 888 case CHIP_NAVI14: 889 case CHIP_NAVI12: 890 adev->nbio.funcs->update_medium_grain_clock_gating(adev, 891 state == AMD_CG_STATE_GATE ? true : false); 892 adev->nbio.funcs->update_medium_grain_light_sleep(adev, 893 state == AMD_CG_STATE_GATE ? true : false); 894 nv_update_hdp_mem_power_gating(adev, 895 state == AMD_CG_STATE_GATE ? true : false); 896 nv_update_hdp_clock_gating(adev, 897 state == AMD_CG_STATE_GATE ? true : false); 898 break; 899 default: 900 break; 901 } 902 return 0; 903 } 904 905 static int nv_common_set_powergating_state(void *handle, 906 enum amd_powergating_state state) 907 { 908 /* TODO */ 909 return 0; 910 } 911 912 static void nv_common_get_clockgating_state(void *handle, u32 *flags) 913 { 914 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 915 uint32_t tmp; 916 917 if (amdgpu_sriov_vf(adev)) 918 *flags = 0; 919 920 adev->nbio.funcs->get_clockgating_state(adev, flags); 921 922 /* AMD_CG_SUPPORT_HDP_MGCG */ 923 tmp = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL); 924 if (!(tmp & (HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK | 925 HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK | 926 HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK | 927 HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK | 928 HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK | 929 HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK))) 930 *flags |= AMD_CG_SUPPORT_HDP_MGCG; 931 932 /* AMD_CG_SUPPORT_HDP_LS/DS/SD */ 933 tmp = RREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL); 934 if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_LS_EN_MASK) 935 *flags |= AMD_CG_SUPPORT_HDP_LS; 936 else if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_DS_EN_MASK) 937 *flags |= AMD_CG_SUPPORT_HDP_DS; 938 else if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_SD_EN_MASK) 939 *flags |= AMD_CG_SUPPORT_HDP_SD; 940 941 return; 942 } 943 944 static const struct amd_ip_funcs nv_common_ip_funcs = { 945 .name = "nv_common", 946 .early_init = nv_common_early_init, 947 .late_init = nv_common_late_init, 948 .sw_init = nv_common_sw_init, 949 .sw_fini = nv_common_sw_fini, 950 .hw_init = nv_common_hw_init, 951 .hw_fini = nv_common_hw_fini, 952 .suspend = nv_common_suspend, 953 .resume = nv_common_resume, 954 .is_idle = nv_common_is_idle, 955 .wait_for_idle = nv_common_wait_for_idle, 956 .soft_reset = nv_common_soft_reset, 957 .set_clockgating_state = nv_common_set_clockgating_state, 958 .set_powergating_state = nv_common_set_powergating_state, 959 .get_clockgating_state = nv_common_get_clockgating_state, 960 }; 961