0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 #include <linux/pci.h>
0025
0026 #include "amdgpu.h"
0027 #include "amdgpu_ih.h"
0028 #include "vid.h"
0029
0030 #include "oss/oss_3_0_d.h"
0031 #include "oss/oss_3_0_sh_mask.h"
0032
0033 #include "bif/bif_5_1_d.h"
0034 #include "bif/bif_5_1_sh_mask.h"
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 static void tonga_ih_set_interrupt_funcs(struct amdgpu_device *adev);
0052
0053
0054
0055
0056
0057
0058
0059
0060 static void tonga_ih_enable_interrupts(struct amdgpu_device *adev)
0061 {
0062 u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
0063
0064 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 1);
0065 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 1);
0066 WREG32(mmIH_RB_CNTL, ih_rb_cntl);
0067 adev->irq.ih.enabled = true;
0068 }
0069
0070
0071
0072
0073
0074
0075
0076
0077 static void tonga_ih_disable_interrupts(struct amdgpu_device *adev)
0078 {
0079 u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
0080
0081 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 0);
0082 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 0);
0083 WREG32(mmIH_RB_CNTL, ih_rb_cntl);
0084
0085 WREG32(mmIH_RB_RPTR, 0);
0086 WREG32(mmIH_RB_WPTR, 0);
0087 adev->irq.ih.enabled = false;
0088 adev->irq.ih.rptr = 0;
0089 }
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 static int tonga_ih_irq_init(struct amdgpu_device *adev)
0103 {
0104 u32 interrupt_cntl, ih_rb_cntl, ih_doorbell_rtpr;
0105 struct amdgpu_ih_ring *ih = &adev->irq.ih;
0106 int rb_bufsz;
0107
0108
0109 tonga_ih_disable_interrupts(adev);
0110
0111
0112 WREG32(mmINTERRUPT_CNTL2, adev->dummy_page_addr >> 8);
0113 interrupt_cntl = RREG32(mmINTERRUPT_CNTL);
0114
0115
0116
0117 interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_DUMMY_RD_OVERRIDE, 0);
0118
0119 interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_REQ_NONSNOOP_EN, 0);
0120 WREG32(mmINTERRUPT_CNTL, interrupt_cntl);
0121
0122
0123 WREG32(mmIH_RB_BASE, ih->gpu_addr >> 8);
0124
0125 rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
0126 ih_rb_cntl = REG_SET_FIELD(0, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
0127 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
0128
0129 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_WRITEBACK_ENABLE, 1);
0130 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0);
0131
0132 if (adev->irq.msi_enabled)
0133 ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RPTR_REARM, 1);
0134
0135 WREG32(mmIH_RB_CNTL, ih_rb_cntl);
0136
0137
0138 WREG32(mmIH_RB_WPTR_ADDR_LO, lower_32_bits(ih->wptr_addr));
0139 WREG32(mmIH_RB_WPTR_ADDR_HI, upper_32_bits(ih->wptr_addr) & 0xFF);
0140
0141
0142 WREG32(mmIH_RB_RPTR, 0);
0143 WREG32(mmIH_RB_WPTR, 0);
0144
0145 ih_doorbell_rtpr = RREG32(mmIH_DOORBELL_RPTR);
0146 if (adev->irq.ih.use_doorbell) {
0147 ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
0148 OFFSET, adev->irq.ih.doorbell_index);
0149 ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
0150 ENABLE, 1);
0151 } else {
0152 ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
0153 ENABLE, 0);
0154 }
0155 WREG32(mmIH_DOORBELL_RPTR, ih_doorbell_rtpr);
0156
0157 pci_set_master(adev->pdev);
0158
0159
0160 tonga_ih_enable_interrupts(adev);
0161
0162 return 0;
0163 }
0164
0165
0166
0167
0168
0169
0170
0171
0172 static void tonga_ih_irq_disable(struct amdgpu_device *adev)
0173 {
0174 tonga_ih_disable_interrupts(adev);
0175
0176
0177 mdelay(1);
0178 }
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192 static u32 tonga_ih_get_wptr(struct amdgpu_device *adev,
0193 struct amdgpu_ih_ring *ih)
0194 {
0195 u32 wptr, tmp;
0196
0197 wptr = le32_to_cpu(*ih->wptr_cpu);
0198
0199 if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW))
0200 goto out;
0201
0202
0203 wptr = RREG32(mmIH_RB_WPTR);
0204
0205 if (!REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW))
0206 goto out;
0207
0208 wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
0209
0210
0211
0212
0213
0214
0215 dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
0216 wptr, ih->rptr, (wptr + 16) & ih->ptr_mask);
0217 ih->rptr = (wptr + 16) & ih->ptr_mask;
0218 tmp = RREG32(mmIH_RB_CNTL);
0219 tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
0220 WREG32(mmIH_RB_CNTL, tmp);
0221
0222 out:
0223 return (wptr & ih->ptr_mask);
0224 }
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 static void tonga_ih_decode_iv(struct amdgpu_device *adev,
0237 struct amdgpu_ih_ring *ih,
0238 struct amdgpu_iv_entry *entry)
0239 {
0240
0241 u32 ring_index = ih->rptr >> 2;
0242 uint32_t dw[4];
0243
0244 dw[0] = le32_to_cpu(ih->ring[ring_index + 0]);
0245 dw[1] = le32_to_cpu(ih->ring[ring_index + 1]);
0246 dw[2] = le32_to_cpu(ih->ring[ring_index + 2]);
0247 dw[3] = le32_to_cpu(ih->ring[ring_index + 3]);
0248
0249 entry->client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
0250 entry->src_id = dw[0] & 0xff;
0251 entry->src_data[0] = dw[1] & 0xfffffff;
0252 entry->ring_id = dw[2] & 0xff;
0253 entry->vmid = (dw[2] >> 8) & 0xff;
0254 entry->pasid = (dw[2] >> 16) & 0xffff;
0255
0256
0257 ih->rptr += 16;
0258 }
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268 static void tonga_ih_set_rptr(struct amdgpu_device *adev,
0269 struct amdgpu_ih_ring *ih)
0270 {
0271 if (ih->use_doorbell) {
0272
0273 *ih->rptr_cpu = ih->rptr;
0274 WDOORBELL32(ih->doorbell_index, ih->rptr);
0275 } else {
0276 WREG32(mmIH_RB_RPTR, ih->rptr);
0277 }
0278 }
0279
0280 static int tonga_ih_early_init(void *handle)
0281 {
0282 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0283 int ret;
0284
0285 ret = amdgpu_irq_add_domain(adev);
0286 if (ret)
0287 return ret;
0288
0289 tonga_ih_set_interrupt_funcs(adev);
0290
0291 return 0;
0292 }
0293
0294 static int tonga_ih_sw_init(void *handle)
0295 {
0296 int r;
0297 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0298
0299 r = amdgpu_ih_ring_init(adev, &adev->irq.ih, 64 * 1024, true);
0300 if (r)
0301 return r;
0302
0303 adev->irq.ih.use_doorbell = true;
0304 adev->irq.ih.doorbell_index = adev->doorbell_index.ih;
0305
0306 r = amdgpu_irq_init(adev);
0307
0308 return r;
0309 }
0310
0311 static int tonga_ih_sw_fini(void *handle)
0312 {
0313 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0314
0315 amdgpu_irq_fini_sw(adev);
0316 amdgpu_irq_remove_domain(adev);
0317
0318 return 0;
0319 }
0320
0321 static int tonga_ih_hw_init(void *handle)
0322 {
0323 int r;
0324 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0325
0326 r = tonga_ih_irq_init(adev);
0327 if (r)
0328 return r;
0329
0330 return 0;
0331 }
0332
0333 static int tonga_ih_hw_fini(void *handle)
0334 {
0335 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0336
0337 tonga_ih_irq_disable(adev);
0338
0339 return 0;
0340 }
0341
0342 static int tonga_ih_suspend(void *handle)
0343 {
0344 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0345
0346 return tonga_ih_hw_fini(adev);
0347 }
0348
0349 static int tonga_ih_resume(void *handle)
0350 {
0351 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0352
0353 return tonga_ih_hw_init(adev);
0354 }
0355
0356 static bool tonga_ih_is_idle(void *handle)
0357 {
0358 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0359 u32 tmp = RREG32(mmSRBM_STATUS);
0360
0361 if (REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
0362 return false;
0363
0364 return true;
0365 }
0366
0367 static int tonga_ih_wait_for_idle(void *handle)
0368 {
0369 unsigned i;
0370 u32 tmp;
0371 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0372
0373 for (i = 0; i < adev->usec_timeout; i++) {
0374
0375 tmp = RREG32(mmSRBM_STATUS);
0376 if (!REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
0377 return 0;
0378 udelay(1);
0379 }
0380 return -ETIMEDOUT;
0381 }
0382
0383 static bool tonga_ih_check_soft_reset(void *handle)
0384 {
0385 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0386 u32 srbm_soft_reset = 0;
0387 u32 tmp = RREG32(mmSRBM_STATUS);
0388
0389 if (tmp & SRBM_STATUS__IH_BUSY_MASK)
0390 srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET,
0391 SOFT_RESET_IH, 1);
0392
0393 if (srbm_soft_reset) {
0394 adev->irq.srbm_soft_reset = srbm_soft_reset;
0395 return true;
0396 } else {
0397 adev->irq.srbm_soft_reset = 0;
0398 return false;
0399 }
0400 }
0401
0402 static int tonga_ih_pre_soft_reset(void *handle)
0403 {
0404 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0405
0406 if (!adev->irq.srbm_soft_reset)
0407 return 0;
0408
0409 return tonga_ih_hw_fini(adev);
0410 }
0411
0412 static int tonga_ih_post_soft_reset(void *handle)
0413 {
0414 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0415
0416 if (!adev->irq.srbm_soft_reset)
0417 return 0;
0418
0419 return tonga_ih_hw_init(adev);
0420 }
0421
0422 static int tonga_ih_soft_reset(void *handle)
0423 {
0424 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
0425 u32 srbm_soft_reset;
0426
0427 if (!adev->irq.srbm_soft_reset)
0428 return 0;
0429 srbm_soft_reset = adev->irq.srbm_soft_reset;
0430
0431 if (srbm_soft_reset) {
0432 u32 tmp;
0433
0434 tmp = RREG32(mmSRBM_SOFT_RESET);
0435 tmp |= srbm_soft_reset;
0436 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
0437 WREG32(mmSRBM_SOFT_RESET, tmp);
0438 tmp = RREG32(mmSRBM_SOFT_RESET);
0439
0440 udelay(50);
0441
0442 tmp &= ~srbm_soft_reset;
0443 WREG32(mmSRBM_SOFT_RESET, tmp);
0444 tmp = RREG32(mmSRBM_SOFT_RESET);
0445
0446
0447 udelay(50);
0448 }
0449
0450 return 0;
0451 }
0452
0453 static int tonga_ih_set_clockgating_state(void *handle,
0454 enum amd_clockgating_state state)
0455 {
0456 return 0;
0457 }
0458
0459 static int tonga_ih_set_powergating_state(void *handle,
0460 enum amd_powergating_state state)
0461 {
0462 return 0;
0463 }
0464
0465 static const struct amd_ip_funcs tonga_ih_ip_funcs = {
0466 .name = "tonga_ih",
0467 .early_init = tonga_ih_early_init,
0468 .late_init = NULL,
0469 .sw_init = tonga_ih_sw_init,
0470 .sw_fini = tonga_ih_sw_fini,
0471 .hw_init = tonga_ih_hw_init,
0472 .hw_fini = tonga_ih_hw_fini,
0473 .suspend = tonga_ih_suspend,
0474 .resume = tonga_ih_resume,
0475 .is_idle = tonga_ih_is_idle,
0476 .wait_for_idle = tonga_ih_wait_for_idle,
0477 .check_soft_reset = tonga_ih_check_soft_reset,
0478 .pre_soft_reset = tonga_ih_pre_soft_reset,
0479 .soft_reset = tonga_ih_soft_reset,
0480 .post_soft_reset = tonga_ih_post_soft_reset,
0481 .set_clockgating_state = tonga_ih_set_clockgating_state,
0482 .set_powergating_state = tonga_ih_set_powergating_state,
0483 };
0484
0485 static const struct amdgpu_ih_funcs tonga_ih_funcs = {
0486 .get_wptr = tonga_ih_get_wptr,
0487 .decode_iv = tonga_ih_decode_iv,
0488 .set_rptr = tonga_ih_set_rptr
0489 };
0490
0491 static void tonga_ih_set_interrupt_funcs(struct amdgpu_device *adev)
0492 {
0493 adev->irq.ih_funcs = &tonga_ih_funcs;
0494 }
0495
0496 const struct amdgpu_ip_block_version tonga_ih_ip_block =
0497 {
0498 .type = AMD_IP_BLOCK_TYPE_IH,
0499 .major = 3,
0500 .minor = 0,
0501 .rev = 0,
0502 .funcs = &tonga_ih_ip_funcs,
0503 };