si_ih.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2015 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 "drmP.h"
  24. #include "amdgpu.h"
  25. #include "amdgpu_ih.h"
  26. #include "si/sid.h"
  27. #include "si_ih.h"
  28. static void si_ih_set_interrupt_funcs(struct amdgpu_device *adev);
  29. static void si_ih_enable_interrupts(struct amdgpu_device *adev)
  30. {
  31. u32 ih_cntl = RREG32(IH_CNTL);
  32. u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
  33. ih_cntl |= ENABLE_INTR;
  34. ih_rb_cntl |= IH_RB_ENABLE;
  35. WREG32(IH_CNTL, ih_cntl);
  36. WREG32(IH_RB_CNTL, ih_rb_cntl);
  37. adev->irq.ih.enabled = true;
  38. }
  39. static void si_ih_disable_interrupts(struct amdgpu_device *adev)
  40. {
  41. u32 ih_rb_cntl = RREG32(IH_RB_CNTL);
  42. u32 ih_cntl = RREG32(IH_CNTL);
  43. ih_rb_cntl &= ~IH_RB_ENABLE;
  44. ih_cntl &= ~ENABLE_INTR;
  45. WREG32(IH_RB_CNTL, ih_rb_cntl);
  46. WREG32(IH_CNTL, ih_cntl);
  47. WREG32(IH_RB_RPTR, 0);
  48. WREG32(IH_RB_WPTR, 0);
  49. adev->irq.ih.enabled = false;
  50. adev->irq.ih.rptr = 0;
  51. }
  52. static int si_ih_irq_init(struct amdgpu_device *adev)
  53. {
  54. int rb_bufsz;
  55. u32 interrupt_cntl, ih_cntl, ih_rb_cntl;
  56. u64 wptr_off;
  57. si_ih_disable_interrupts(adev);
  58. WREG32(INTERRUPT_CNTL2, adev->irq.ih.gpu_addr >> 8);
  59. interrupt_cntl = RREG32(INTERRUPT_CNTL);
  60. interrupt_cntl &= ~IH_DUMMY_RD_OVERRIDE;
  61. interrupt_cntl &= ~IH_REQ_NONSNOOP_EN;
  62. WREG32(INTERRUPT_CNTL, interrupt_cntl);
  63. WREG32(IH_RB_BASE, adev->irq.ih.gpu_addr >> 8);
  64. rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
  65. ih_rb_cntl = IH_WPTR_OVERFLOW_ENABLE |
  66. IH_WPTR_OVERFLOW_CLEAR |
  67. (rb_bufsz << 1) |
  68. IH_WPTR_WRITEBACK_ENABLE;
  69. wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
  70. WREG32(IH_RB_WPTR_ADDR_LO, lower_32_bits(wptr_off));
  71. WREG32(IH_RB_WPTR_ADDR_HI, upper_32_bits(wptr_off) & 0xFF);
  72. WREG32(IH_RB_CNTL, ih_rb_cntl);
  73. WREG32(IH_RB_RPTR, 0);
  74. WREG32(IH_RB_WPTR, 0);
  75. ih_cntl = MC_WRREQ_CREDIT(0x10) | MC_WR_CLEAN_CNT(0x10) | MC_VMID(0);
  76. if (adev->irq.msi_enabled)
  77. ih_cntl |= RPTR_REARM;
  78. WREG32(IH_CNTL, ih_cntl);
  79. pci_set_master(adev->pdev);
  80. si_ih_enable_interrupts(adev);
  81. return 0;
  82. }
  83. static void si_ih_irq_disable(struct amdgpu_device *adev)
  84. {
  85. si_ih_disable_interrupts(adev);
  86. mdelay(1);
  87. }
  88. static u32 si_ih_get_wptr(struct amdgpu_device *adev)
  89. {
  90. u32 wptr, tmp;
  91. wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
  92. if (wptr & IH_RB_WPTR__RB_OVERFLOW_MASK) {
  93. wptr &= ~IH_RB_WPTR__RB_OVERFLOW_MASK;
  94. dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
  95. wptr, adev->irq.ih.rptr, (wptr + 16) & adev->irq.ih.ptr_mask);
  96. adev->irq.ih.rptr = (wptr + 16) & adev->irq.ih.ptr_mask;
  97. tmp = RREG32(IH_RB_CNTL);
  98. tmp |= IH_RB_CNTL__WPTR_OVERFLOW_CLEAR_MASK;
  99. WREG32(IH_RB_CNTL, tmp);
  100. }
  101. return (wptr & adev->irq.ih.ptr_mask);
  102. }
  103. static void si_ih_decode_iv(struct amdgpu_device *adev,
  104. struct amdgpu_iv_entry *entry)
  105. {
  106. u32 ring_index = adev->irq.ih.rptr >> 2;
  107. uint32_t dw[4];
  108. dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
  109. dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
  110. dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
  111. dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
  112. entry->src_id = dw[0] & 0xff;
  113. entry->src_data = dw[1] & 0xfffffff;
  114. entry->ring_id = dw[2] & 0xff;
  115. entry->vm_id = (dw[2] >> 8) & 0xff;
  116. adev->irq.ih.rptr += 16;
  117. }
  118. static void si_ih_set_rptr(struct amdgpu_device *adev)
  119. {
  120. WREG32(IH_RB_RPTR, adev->irq.ih.rptr);
  121. }
  122. static int si_ih_early_init(void *handle)
  123. {
  124. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  125. si_ih_set_interrupt_funcs(adev);
  126. return 0;
  127. }
  128. static int si_ih_sw_init(void *handle)
  129. {
  130. int r;
  131. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  132. r = amdgpu_ih_ring_init(adev, 64 * 1024, false);
  133. if (r)
  134. return r;
  135. return amdgpu_irq_init(adev);
  136. }
  137. static int si_ih_sw_fini(void *handle)
  138. {
  139. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  140. amdgpu_irq_fini(adev);
  141. amdgpu_ih_ring_fini(adev);
  142. return 0;
  143. }
  144. static int si_ih_hw_init(void *handle)
  145. {
  146. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  147. return si_ih_irq_init(adev);
  148. }
  149. static int si_ih_hw_fini(void *handle)
  150. {
  151. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  152. si_ih_irq_disable(adev);
  153. return 0;
  154. }
  155. static int si_ih_suspend(void *handle)
  156. {
  157. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  158. return si_ih_hw_fini(adev);
  159. }
  160. static int si_ih_resume(void *handle)
  161. {
  162. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  163. return si_ih_hw_init(adev);
  164. }
  165. static bool si_ih_is_idle(void *handle)
  166. {
  167. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  168. u32 tmp = RREG32(SRBM_STATUS);
  169. if (tmp & SRBM_STATUS__IH_BUSY_MASK)
  170. return false;
  171. return true;
  172. }
  173. static int si_ih_wait_for_idle(void *handle)
  174. {
  175. unsigned i;
  176. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  177. for (i = 0; i < adev->usec_timeout; i++) {
  178. if (si_ih_is_idle(handle))
  179. return 0;
  180. udelay(1);
  181. }
  182. return -ETIMEDOUT;
  183. }
  184. static int si_ih_soft_reset(void *handle)
  185. {
  186. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  187. u32 srbm_soft_reset = 0;
  188. u32 tmp = RREG32(SRBM_STATUS);
  189. if (tmp & SRBM_STATUS__IH_BUSY_MASK)
  190. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_IH_MASK;
  191. if (srbm_soft_reset) {
  192. tmp = RREG32(SRBM_SOFT_RESET);
  193. tmp |= srbm_soft_reset;
  194. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  195. WREG32(SRBM_SOFT_RESET, tmp);
  196. tmp = RREG32(SRBM_SOFT_RESET);
  197. udelay(50);
  198. tmp &= ~srbm_soft_reset;
  199. WREG32(SRBM_SOFT_RESET, tmp);
  200. tmp = RREG32(SRBM_SOFT_RESET);
  201. udelay(50);
  202. }
  203. return 0;
  204. }
  205. static int si_ih_set_clockgating_state(void *handle,
  206. enum amd_clockgating_state state)
  207. {
  208. return 0;
  209. }
  210. static int si_ih_set_powergating_state(void *handle,
  211. enum amd_powergating_state state)
  212. {
  213. return 0;
  214. }
  215. static const struct amd_ip_funcs si_ih_ip_funcs = {
  216. .name = "si_ih",
  217. .early_init = si_ih_early_init,
  218. .late_init = NULL,
  219. .sw_init = si_ih_sw_init,
  220. .sw_fini = si_ih_sw_fini,
  221. .hw_init = si_ih_hw_init,
  222. .hw_fini = si_ih_hw_fini,
  223. .suspend = si_ih_suspend,
  224. .resume = si_ih_resume,
  225. .is_idle = si_ih_is_idle,
  226. .wait_for_idle = si_ih_wait_for_idle,
  227. .soft_reset = si_ih_soft_reset,
  228. .set_clockgating_state = si_ih_set_clockgating_state,
  229. .set_powergating_state = si_ih_set_powergating_state,
  230. };
  231. static const struct amdgpu_ih_funcs si_ih_funcs = {
  232. .get_wptr = si_ih_get_wptr,
  233. .decode_iv = si_ih_decode_iv,
  234. .set_rptr = si_ih_set_rptr
  235. };
  236. static void si_ih_set_interrupt_funcs(struct amdgpu_device *adev)
  237. {
  238. if (adev->irq.ih_funcs == NULL)
  239. adev->irq.ih_funcs = &si_ih_funcs;
  240. }
  241. const struct amdgpu_ip_block_version si_ih_ip_block =
  242. {
  243. .type = AMD_IP_BLOCK_TYPE_IH,
  244. .major = 1,
  245. .minor = 0,
  246. .rev = 0,
  247. .funcs = &si_ih_ip_funcs,
  248. };