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 24 #include <linux/pci.h> 25 26 #include "amdgpu.h" 27 #include "amdgpu_ih.h" 28 29 #include "oss/osssys_5_0_0_offset.h" 30 #include "oss/osssys_5_0_0_sh_mask.h" 31 32 #include "soc15_common.h" 33 #include "navi10_ih.h" 34 35 #define MAX_REARM_RETRY 10 36 37 #define mmIH_CHICKEN_Sienna_Cichlid 0x018d 38 #define mmIH_CHICKEN_Sienna_Cichlid_BASE_IDX 0 39 40 static void navi10_ih_set_interrupt_funcs(struct amdgpu_device *adev); 41 42 /** 43 * navi10_ih_init_register_offset - Initialize register offset for ih rings 44 * 45 * @adev: amdgpu_device pointer 46 * 47 * Initialize register offset ih rings (NAVI10). 48 */ 49 static void navi10_ih_init_register_offset(struct amdgpu_device *adev) 50 { 51 struct amdgpu_ih_regs *ih_regs; 52 53 if (adev->irq.ih.ring_size) { 54 ih_regs = &adev->irq.ih.ih_regs; 55 ih_regs->ih_rb_base = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE); 56 ih_regs->ih_rb_base_hi = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_HI); 57 ih_regs->ih_rb_cntl = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL); 58 ih_regs->ih_rb_wptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR); 59 ih_regs->ih_rb_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_RPTR); 60 ih_regs->ih_doorbell_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_DOORBELL_RPTR); 61 ih_regs->ih_rb_wptr_addr_lo = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_ADDR_LO); 62 ih_regs->ih_rb_wptr_addr_hi = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_ADDR_HI); 63 ih_regs->psp_reg_id = PSP_REG_IH_RB_CNTL; 64 } 65 66 if (adev->irq.ih1.ring_size) { 67 ih_regs = &adev->irq.ih1.ih_regs; 68 ih_regs->ih_rb_base = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_RING1); 69 ih_regs->ih_rb_base_hi = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_HI_RING1); 70 ih_regs->ih_rb_cntl = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL_RING1); 71 ih_regs->ih_rb_wptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_RING1); 72 ih_regs->ih_rb_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_RPTR_RING1); 73 ih_regs->ih_doorbell_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_DOORBELL_RPTR_RING1); 74 ih_regs->psp_reg_id = PSP_REG_IH_RB_CNTL_RING1; 75 } 76 77 if (adev->irq.ih2.ring_size) { 78 ih_regs = &adev->irq.ih2.ih_regs; 79 ih_regs->ih_rb_base = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_RING2); 80 ih_regs->ih_rb_base_hi = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_HI_RING2); 81 ih_regs->ih_rb_cntl = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL_RING2); 82 ih_regs->ih_rb_wptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_RING2); 83 ih_regs->ih_rb_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_RPTR_RING2); 84 ih_regs->ih_doorbell_rptr = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_DOORBELL_RPTR_RING2); 85 ih_regs->psp_reg_id = PSP_REG_IH_RB_CNTL_RING2; 86 } 87 } 88 89 /** 90 * force_update_wptr_for_self_int - Force update the wptr for self interrupt 91 * 92 * @adev: amdgpu_device pointer 93 * @threshold: threshold to trigger the wptr reporting 94 * @timeout: timeout to trigger the wptr reporting 95 * @enabled: Enable/disable timeout flush mechanism 96 * 97 * threshold input range: 0 ~ 15, default 0, 98 * real_threshold = 2^threshold 99 * timeout input range: 0 ~ 20, default 8, 100 * real_timeout = (2^timeout) * 1024 / (socclk_freq) 101 * 102 * Force update wptr for self interrupt ( >= SIENNA_CICHLID). 103 */ 104 static void 105 force_update_wptr_for_self_int(struct amdgpu_device *adev, 106 u32 threshold, u32 timeout, bool enabled) 107 { 108 u32 ih_cntl, ih_rb_cntl; 109 110 if (amdgpu_ip_version(adev, OSSSYS_HWIP, 0) < IP_VERSION(5, 0, 3)) 111 return; 112 113 ih_cntl = RREG32_SOC15(OSSSYS, 0, mmIH_CNTL2); 114 ih_rb_cntl = RREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL_RING1); 115 116 ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL2, 117 SELF_IV_FORCE_WPTR_UPDATE_TIMEOUT, timeout); 118 ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL2, 119 SELF_IV_FORCE_WPTR_UPDATE_ENABLE, enabled); 120 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL_RING1, 121 RB_USED_INT_THRESHOLD, threshold); 122 123 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) { 124 if (psp_reg_program(&adev->psp, PSP_REG_IH_RB_CNTL_RING1, ih_rb_cntl)) 125 return; 126 } else { 127 WREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL_RING1, ih_rb_cntl); 128 } 129 130 ih_rb_cntl = RREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL_RING2); 131 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL_RING2, 132 RB_USED_INT_THRESHOLD, threshold); 133 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) { 134 if (psp_reg_program(&adev->psp, PSP_REG_IH_RB_CNTL_RING2, ih_rb_cntl)) 135 return; 136 } else { 137 WREG32_SOC15(OSSSYS, 0, mmIH_RB_CNTL_RING2, ih_rb_cntl); 138 } 139 140 WREG32_SOC15(OSSSYS, 0, mmIH_CNTL2, ih_cntl); 141 } 142 143 /** 144 * navi10_ih_toggle_ring_interrupts - toggle the interrupt ring buffer 145 * 146 * @adev: amdgpu_device pointer 147 * @ih: amdgpu_ih_ring pointet 148 * @enable: true - enable the interrupts, false - disable the interrupts 149 * 150 * Toggle the interrupt ring buffer (NAVI10) 151 */ 152 static int navi10_ih_toggle_ring_interrupts(struct amdgpu_device *adev, 153 struct amdgpu_ih_ring *ih, 154 bool enable) 155 { 156 struct amdgpu_ih_regs *ih_regs; 157 uint32_t tmp; 158 159 ih_regs = &ih->ih_regs; 160 161 tmp = RREG32(ih_regs->ih_rb_cntl); 162 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RB_ENABLE, (enable ? 1 : 0)); 163 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RB_GPU_TS_ENABLE, 1); 164 165 if (enable) { 166 /* Unset the CLEAR_OVERFLOW bit to make sure the next step 167 * is switching the bit from 0 to 1 168 */ 169 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 0); 170 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) { 171 if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp)) 172 return -ETIMEDOUT; 173 } else { 174 WREG32_NO_KIQ(ih_regs->ih_rb_cntl, tmp); 175 } 176 177 /* Clear RB_OVERFLOW bit */ 178 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1); 179 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) { 180 if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp)) 181 return -ETIMEDOUT; 182 } else { 183 WREG32_NO_KIQ(ih_regs->ih_rb_cntl, tmp); 184 } 185 186 /* Unset the CLEAR_OVERFLOW bit immediately so new overflows 187 * can be detected. 188 */ 189 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 0); 190 } 191 192 /* enable_intr field is only valid in ring0 */ 193 if (ih == &adev->irq.ih) 194 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, ENABLE_INTR, (enable ? 1 : 0)); 195 196 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) { 197 /* 198 * PSP programming of IH_RB_CNTL is slow enough for MSI to 199 * arrive before this call returns. Publish ih->enabled first 200 * so amdgpu_ih_process() consumes that IV and rearms. 201 */ 202 if (enable) { 203 ih->enabled = true; 204 smp_wmb(); 205 } 206 if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp)) { 207 ih->enabled = false; 208 return -ETIMEDOUT; 209 } 210 } else { 211 WREG32(ih_regs->ih_rb_cntl, tmp); 212 } 213 214 if (enable) { 215 ih->enabled = true; 216 } else { 217 /* set rptr, wptr to 0 */ 218 WREG32(ih_regs->ih_rb_rptr, 0); 219 WREG32(ih_regs->ih_rb_wptr, 0); 220 ih->enabled = false; 221 ih->rptr = 0; 222 } 223 224 return 0; 225 } 226 227 /** 228 * navi10_ih_toggle_interrupts - Toggle all the available interrupt ring buffers 229 * 230 * @adev: amdgpu_device pointer 231 * @enable: enable or disable interrupt ring buffers 232 * 233 * Toggle all the available interrupt ring buffers (NAVI10). 234 */ 235 static int navi10_ih_toggle_interrupts(struct amdgpu_device *adev, bool enable) 236 { 237 struct amdgpu_ih_ring *ih[] = {&adev->irq.ih, &adev->irq.ih1, &adev->irq.ih2}; 238 int i; 239 int r; 240 241 for (i = 0; i < ARRAY_SIZE(ih); i++) { 242 if (ih[i]->ring_size) { 243 r = navi10_ih_toggle_ring_interrupts(adev, ih[i], enable); 244 if (r) 245 return r; 246 } 247 } 248 249 return 0; 250 } 251 252 static uint32_t navi10_ih_rb_cntl(struct amdgpu_ih_ring *ih, uint32_t ih_rb_cntl) 253 { 254 int rb_bufsz = order_base_2(ih->ring_size / 4); 255 256 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, 257 MC_SPACE, ih->use_bus_addr ? 1 : 4); 258 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, 259 WPTR_OVERFLOW_CLEAR, 1); 260 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, 261 WPTR_OVERFLOW_ENABLE, 1); 262 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz); 263 /* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register 264 * value is written to memory 265 */ 266 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, 267 WPTR_WRITEBACK_ENABLE, 1); 268 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_SNOOP, 1); 269 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_RO, 0); 270 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0); 271 272 return ih_rb_cntl; 273 } 274 275 static uint32_t navi10_ih_doorbell_rptr(struct amdgpu_ih_ring *ih) 276 { 277 u32 ih_doorbell_rtpr = 0; 278 279 if (ih->use_doorbell) { 280 ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, 281 IH_DOORBELL_RPTR, OFFSET, 282 ih->doorbell_index); 283 ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, 284 IH_DOORBELL_RPTR, 285 ENABLE, 1); 286 } else { 287 ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, 288 IH_DOORBELL_RPTR, 289 ENABLE, 0); 290 } 291 return ih_doorbell_rtpr; 292 } 293 294 /** 295 * navi10_ih_enable_ring - enable an ih ring buffer 296 * 297 * @adev: amdgpu_device pointer 298 * @ih: amdgpu_ih_ring pointer 299 * 300 * Enable an ih ring buffer (NAVI10) 301 */ 302 static int navi10_ih_enable_ring(struct amdgpu_device *adev, 303 struct amdgpu_ih_ring *ih) 304 { 305 struct amdgpu_ih_regs *ih_regs; 306 uint32_t tmp; 307 308 ih_regs = &ih->ih_regs; 309 310 /* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/ 311 WREG32(ih_regs->ih_rb_base, ih->gpu_addr >> 8); 312 WREG32(ih_regs->ih_rb_base_hi, (ih->gpu_addr >> 40) & 0xff); 313 314 tmp = RREG32(ih_regs->ih_rb_cntl); 315 tmp = navi10_ih_rb_cntl(ih, tmp); 316 if (ih == &adev->irq.ih) 317 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RPTR_REARM, !!adev->irq.msi_enabled); 318 if (ih == &adev->irq.ih1) 319 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, RB_FULL_DRAIN_ENABLE, 1); 320 321 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_reg_indirect_ih(adev)) { 322 if (psp_reg_program(&adev->psp, ih_regs->psp_reg_id, tmp)) { 323 DRM_ERROR("PSP program IH_RB_CNTL failed!\n"); 324 return -ETIMEDOUT; 325 } 326 } else { 327 WREG32(ih_regs->ih_rb_cntl, tmp); 328 } 329 330 if (ih == &adev->irq.ih) { 331 /* set the ih ring 0 writeback address whether it's enabled or not */ 332 WREG32(ih_regs->ih_rb_wptr_addr_lo, lower_32_bits(ih->wptr_addr)); 333 WREG32(ih_regs->ih_rb_wptr_addr_hi, upper_32_bits(ih->wptr_addr) & 0xFFFF); 334 } 335 336 /* set rptr, wptr to 0 */ 337 WREG32(ih_regs->ih_rb_wptr, 0); 338 WREG32(ih_regs->ih_rb_rptr, 0); 339 340 WREG32(ih_regs->ih_doorbell_rptr, navi10_ih_doorbell_rptr(ih)); 341 342 return 0; 343 } 344 345 /** 346 * navi10_ih_irq_init - init and enable the interrupt ring 347 * 348 * @adev: amdgpu_device pointer 349 * 350 * Allocate a ring buffer for the interrupt controller, 351 * enable the RLC, disable interrupts, enable the IH 352 * ring buffer and enable it (NAVI). 353 * Called at device load and reume. 354 * Returns 0 for success, errors for failure. 355 */ 356 static int navi10_ih_irq_init(struct amdgpu_device *adev) 357 { 358 struct amdgpu_ih_ring *ih[] = {&adev->irq.ih, &adev->irq.ih1, &adev->irq.ih2}; 359 u32 ih_chicken; 360 int ret; 361 int i; 362 363 /* disable irqs */ 364 ret = navi10_ih_toggle_interrupts(adev, false); 365 if (ret) 366 return ret; 367 368 adev->nbio.funcs->ih_control(adev); 369 370 if (unlikely(adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT)) { 371 if (ih[0]->use_bus_addr) { 372 switch (amdgpu_ip_version(adev, OSSSYS_HWIP, 0)) { 373 case IP_VERSION(5, 0, 3): 374 case IP_VERSION(5, 2, 0): 375 case IP_VERSION(5, 2, 1): 376 ih_chicken = RREG32_SOC15(OSSSYS, 0, mmIH_CHICKEN_Sienna_Cichlid); 377 ih_chicken = REG_SET_FIELD(ih_chicken, 378 IH_CHICKEN, MC_SPACE_GPA_ENABLE, 1); 379 WREG32_SOC15(OSSSYS, 0, mmIH_CHICKEN_Sienna_Cichlid, ih_chicken); 380 break; 381 default: 382 ih_chicken = RREG32_SOC15(OSSSYS, 0, mmIH_CHICKEN); 383 ih_chicken = REG_SET_FIELD(ih_chicken, 384 IH_CHICKEN, MC_SPACE_GPA_ENABLE, 1); 385 WREG32_SOC15(OSSSYS, 0, mmIH_CHICKEN, ih_chicken); 386 break; 387 } 388 } 389 } 390 391 for (i = 0; i < ARRAY_SIZE(ih); i++) { 392 if (ih[i]->ring_size) { 393 ret = navi10_ih_enable_ring(adev, ih[i]); 394 if (ret) 395 return ret; 396 } 397 } 398 399 /* update doorbell range for ih ring 0*/ 400 adev->nbio.funcs->ih_doorbell_range(adev, ih[0]->use_doorbell, 401 ih[0]->doorbell_index); 402 403 pci_set_master(adev->pdev); 404 405 /* enable interrupts */ 406 ret = navi10_ih_toggle_interrupts(adev, true); 407 if (ret) 408 return ret; 409 /* enable wptr force update for self int */ 410 force_update_wptr_for_self_int(adev, 0, 8, true); 411 412 if (adev->irq.ih_soft.ring_size) 413 adev->irq.ih_soft.enabled = true; 414 415 return 0; 416 } 417 418 /** 419 * navi10_ih_irq_disable - disable interrupts 420 * 421 * @adev: amdgpu_device pointer 422 * 423 * Disable interrupts on the hw (NAVI10). 424 */ 425 static void navi10_ih_irq_disable(struct amdgpu_device *adev) 426 { 427 force_update_wptr_for_self_int(adev, 0, 8, false); 428 navi10_ih_toggle_interrupts(adev, false); 429 430 /* Wait and acknowledge irq */ 431 mdelay(1); 432 } 433 434 /** 435 * navi10_ih_get_wptr - get the IH ring buffer wptr 436 * 437 * @adev: amdgpu_device pointer 438 * @ih: IH ring buffer to fetch wptr 439 * 440 * Get the IH ring buffer wptr from either the register 441 * or the writeback memory buffer (NAVI10). Also check for 442 * ring buffer overflow and deal with it. 443 * Returns the value of the wptr. 444 */ 445 static u32 navi10_ih_get_wptr(struct amdgpu_device *adev, 446 struct amdgpu_ih_ring *ih) 447 { 448 u32 wptr, tmp; 449 struct amdgpu_ih_regs *ih_regs; 450 451 if (ih == &adev->irq.ih || ih == &adev->irq.ih_soft) { 452 /* Only ring0 supports writeback. On other rings fall back 453 * to register-based code with overflow checking below. 454 * ih_soft ring doesn't have any backing hardware registers, 455 * update wptr and return. 456 */ 457 wptr = le32_to_cpu(*ih->wptr_cpu); 458 459 if (ih == &adev->irq.ih_soft) 460 goto out; 461 462 463 if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW)) 464 goto out; 465 } 466 467 ih_regs = &ih->ih_regs; 468 469 /* Double check that the overflow wasn't already cleared. */ 470 wptr = RREG32_NO_KIQ(ih_regs->ih_rb_wptr); 471 if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW)) 472 goto out; 473 wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0); 474 475 /* When a ring buffer overflow happen start parsing interrupt 476 * from the last not overwritten vector (wptr + 32). Hopefully 477 * this should allow us to catch up. 478 */ 479 tmp = (wptr + 32) & ih->ptr_mask; 480 dev_warn(adev->dev, "%s ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n", 481 amdgpu_ih_ring_name(adev, ih), wptr, ih->rptr, tmp); 482 ih->rptr = tmp; 483 484 tmp = RREG32_NO_KIQ(ih_regs->ih_rb_cntl); 485 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1); 486 WREG32_NO_KIQ(ih_regs->ih_rb_cntl, tmp); 487 488 /* Unset the CLEAR_OVERFLOW bit immediately so new overflows 489 * can be detected. 490 */ 491 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 0); 492 WREG32_NO_KIQ(ih_regs->ih_rb_cntl, tmp); 493 out: 494 return (wptr & ih->ptr_mask); 495 } 496 497 /** 498 * navi10_ih_irq_rearm - rearm IRQ if lost 499 * 500 * @adev: amdgpu_device pointer 501 * @ih: IH ring to match 502 * 503 */ 504 static void navi10_ih_irq_rearm(struct amdgpu_device *adev, 505 struct amdgpu_ih_ring *ih) 506 { 507 uint32_t v = 0; 508 uint32_t i = 0; 509 struct amdgpu_ih_regs *ih_regs; 510 511 ih_regs = &ih->ih_regs; 512 513 /* Rearm IRQ / re-write doorbell if doorbell write is lost */ 514 for (i = 0; i < MAX_REARM_RETRY; i++) { 515 v = RREG32_NO_KIQ(ih_regs->ih_rb_rptr); 516 if ((v < ih->ring_size) && (v != ih->rptr)) 517 WDOORBELL32(ih->doorbell_index, ih->rptr); 518 else 519 break; 520 } 521 } 522 523 /** 524 * navi10_ih_set_rptr - set the IH ring buffer rptr 525 * 526 * @adev: amdgpu_device pointer 527 * 528 * @ih: IH ring buffer to set rptr 529 * Set the IH ring buffer rptr. 530 */ 531 static void navi10_ih_set_rptr(struct amdgpu_device *adev, 532 struct amdgpu_ih_ring *ih) 533 { 534 struct amdgpu_ih_regs *ih_regs; 535 536 if (ih == &adev->irq.ih_soft) 537 return; 538 539 if (ih->use_doorbell) { 540 /* XXX check if swapping is necessary on BE */ 541 *ih->rptr_cpu = ih->rptr; 542 WDOORBELL32(ih->doorbell_index, ih->rptr); 543 544 if (amdgpu_sriov_vf(adev)) 545 navi10_ih_irq_rearm(adev, ih); 546 } else { 547 ih_regs = &ih->ih_regs; 548 WREG32(ih_regs->ih_rb_rptr, ih->rptr); 549 } 550 } 551 552 /** 553 * navi10_ih_self_irq - dispatch work for ring 1 and 2 554 * 555 * @adev: amdgpu_device pointer 556 * @source: irq source 557 * @entry: IV with WPTR update 558 * 559 * Update the WPTR from the IV and schedule work to handle the entries. 560 */ 561 static int navi10_ih_self_irq(struct amdgpu_device *adev, 562 struct amdgpu_irq_src *source, 563 struct amdgpu_iv_entry *entry) 564 { 565 switch (entry->ring_id) { 566 case 1: 567 schedule_work(&adev->irq.ih1_work); 568 break; 569 case 2: 570 schedule_work(&adev->irq.ih2_work); 571 break; 572 default: break; 573 } 574 return 0; 575 } 576 577 static const struct amdgpu_irq_src_funcs navi10_ih_self_irq_funcs = { 578 .process = navi10_ih_self_irq, 579 }; 580 581 static void navi10_ih_set_self_irq_funcs(struct amdgpu_device *adev) 582 { 583 adev->irq.self_irq.num_types = 0; 584 adev->irq.self_irq.funcs = &navi10_ih_self_irq_funcs; 585 } 586 587 static int navi10_ih_early_init(struct amdgpu_ip_block *ip_block) 588 { 589 struct amdgpu_device *adev = ip_block->adev; 590 591 navi10_ih_set_interrupt_funcs(adev); 592 navi10_ih_set_self_irq_funcs(adev); 593 return 0; 594 } 595 596 static int navi10_ih_sw_init(struct amdgpu_ip_block *ip_block) 597 { 598 int r; 599 struct amdgpu_device *adev = ip_block->adev; 600 bool use_bus_addr; 601 602 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_IH, 0, 603 &adev->irq.self_irq); 604 605 if (r) 606 return r; 607 608 /* use gpu virtual address for ih ring 609 * until ih_checken is programmed to allow 610 * use bus address for ih ring by psp bl */ 611 if ((adev->flags & AMD_IS_APU) || 612 (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) 613 use_bus_addr = false; 614 else 615 use_bus_addr = true; 616 r = amdgpu_ih_ring_init(adev, &adev->irq.ih, IH_RING_SIZE, use_bus_addr); 617 if (r) 618 return r; 619 620 adev->irq.ih.use_doorbell = true; 621 adev->irq.ih.doorbell_index = adev->doorbell_index.ih << 1; 622 623 adev->irq.ih1.ring_size = 0; 624 adev->irq.ih2.ring_size = 0; 625 626 /* initialize ih control registers offset */ 627 navi10_ih_init_register_offset(adev); 628 629 r = amdgpu_ih_ring_init(adev, &adev->irq.ih_soft, IH_SW_RING_SIZE, true); 630 if (r) 631 return r; 632 633 r = amdgpu_irq_init(adev); 634 635 return r; 636 } 637 638 static int navi10_ih_sw_fini(struct amdgpu_ip_block *ip_block) 639 { 640 struct amdgpu_device *adev = ip_block->adev; 641 642 amdgpu_irq_fini_sw(adev); 643 644 return 0; 645 } 646 647 static int navi10_ih_hw_init(struct amdgpu_ip_block *ip_block) 648 { 649 struct amdgpu_device *adev = ip_block->adev; 650 651 return navi10_ih_irq_init(adev); 652 } 653 654 static int navi10_ih_hw_fini(struct amdgpu_ip_block *ip_block) 655 { 656 navi10_ih_irq_disable(ip_block->adev); 657 658 return 0; 659 } 660 661 static int navi10_ih_suspend(struct amdgpu_ip_block *ip_block) 662 { 663 return navi10_ih_hw_fini(ip_block); 664 } 665 666 static int navi10_ih_resume(struct amdgpu_ip_block *ip_block) 667 { 668 return navi10_ih_hw_init(ip_block); 669 } 670 671 static int navi10_ih_wait_for_idle(struct amdgpu_ip_block *ip_block) 672 { 673 /* todo */ 674 return -ETIMEDOUT; 675 } 676 677 static void navi10_ih_update_clockgating_state(struct amdgpu_device *adev, 678 bool enable) 679 { 680 uint32_t data, def, field_val; 681 682 if (adev->cg_flags & AMD_CG_SUPPORT_IH_CG) { 683 def = data = RREG32_SOC15(OSSSYS, 0, mmIH_CLK_CTRL); 684 field_val = enable ? 0 : 1; 685 data = REG_SET_FIELD(data, IH_CLK_CTRL, 686 DBUS_MUX_CLK_SOFT_OVERRIDE, field_val); 687 data = REG_SET_FIELD(data, IH_CLK_CTRL, 688 OSSSYS_SHARE_CLK_SOFT_OVERRIDE, field_val); 689 data = REG_SET_FIELD(data, IH_CLK_CTRL, 690 LIMIT_SMN_CLK_SOFT_OVERRIDE, field_val); 691 data = REG_SET_FIELD(data, IH_CLK_CTRL, 692 DYN_CLK_SOFT_OVERRIDE, field_val); 693 data = REG_SET_FIELD(data, IH_CLK_CTRL, 694 REG_CLK_SOFT_OVERRIDE, field_val); 695 if (def != data) 696 WREG32_SOC15(OSSSYS, 0, mmIH_CLK_CTRL, data); 697 } 698 } 699 700 static int navi10_ih_set_clockgating_state(struct amdgpu_ip_block *ip_block, 701 enum amd_clockgating_state state) 702 { 703 struct amdgpu_device *adev = ip_block->adev; 704 705 navi10_ih_update_clockgating_state(adev, 706 state == AMD_CG_STATE_GATE); 707 return 0; 708 } 709 710 static int navi10_ih_set_powergating_state(struct amdgpu_ip_block *ip_block, 711 enum amd_powergating_state state) 712 { 713 return 0; 714 } 715 716 static void navi10_ih_get_clockgating_state(struct amdgpu_ip_block *ip_block, u64 *flags) 717 { 718 struct amdgpu_device *adev = ip_block->adev; 719 720 if (!RREG32_SOC15(OSSSYS, 0, mmIH_CLK_CTRL)) 721 *flags |= AMD_CG_SUPPORT_IH_CG; 722 } 723 724 static const struct amd_ip_funcs navi10_ih_ip_funcs = { 725 .name = "navi10_ih", 726 .early_init = navi10_ih_early_init, 727 .sw_init = navi10_ih_sw_init, 728 .sw_fini = navi10_ih_sw_fini, 729 .hw_init = navi10_ih_hw_init, 730 .hw_fini = navi10_ih_hw_fini, 731 .suspend = navi10_ih_suspend, 732 .resume = navi10_ih_resume, 733 .wait_for_idle = navi10_ih_wait_for_idle, 734 .set_clockgating_state = navi10_ih_set_clockgating_state, 735 .set_powergating_state = navi10_ih_set_powergating_state, 736 .get_clockgating_state = navi10_ih_get_clockgating_state, 737 }; 738 739 static const struct amdgpu_ih_funcs navi10_ih_funcs = { 740 .get_wptr = navi10_ih_get_wptr, 741 .decode_iv = amdgpu_ih_decode_iv_helper, 742 .decode_iv_ts = amdgpu_ih_decode_iv_ts_helper, 743 .set_rptr = navi10_ih_set_rptr 744 }; 745 746 static void navi10_ih_set_interrupt_funcs(struct amdgpu_device *adev) 747 { 748 if (adev->irq.ih_funcs == NULL) 749 adev->irq.ih_funcs = &navi10_ih_funcs; 750 } 751 752 const struct amdgpu_ip_block_version navi10_ih_ip_block = { 753 .type = AMD_IP_BLOCK_TYPE_IH, 754 .major = 5, 755 .minor = 0, 756 .rev = 0, 757 .funcs = &navi10_ih_ip_funcs, 758 }; 759