si_ih.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <drm/drmP.h>
  24. #include "amdgpu.h"
  25. #include "amdgpu_ih.h"
  26. #include "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. /**
  104. * si_ih_prescreen_iv - prescreen an interrupt vector
  105. *
  106. * @adev: amdgpu_device pointer
  107. *
  108. * Returns true if the interrupt vector should be further processed.
  109. */
  110. static bool si_ih_prescreen_iv(struct amdgpu_device *adev)
  111. {
  112. /* Process all interrupts */
  113. return true;
  114. }
  115. static void si_ih_decode_iv(struct amdgpu_device *adev,
  116. struct amdgpu_iv_entry *entry)
  117. {
  118. u32 ring_index = adev->irq.ih.rptr >> 2;
  119. uint32_t dw[4];
  120. dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
  121. dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
  122. dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
  123. dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
  124. entry->client_id = AMDGPU_IH_CLIENTID_LEGACY;
  125. entry->src_id = dw[0] & 0xff;
  126. entry->src_data[0] = dw[1] & 0xfffffff;
  127. entry->ring_id = dw[2] & 0xff;
  128. entry->vm_id = (dw[2] >> 8) & 0xff;
  129. adev->irq.ih.rptr += 16;
  130. }
  131. static void si_ih_set_rptr(struct amdgpu_device *adev)
  132. {
  133. WREG32(IH_RB_RPTR, adev->irq.ih.rptr);
  134. }
  135. static int si_ih_early_init(void *handle)
  136. {
  137. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  138. si_ih_set_interrupt_funcs(adev);
  139. return 0;
  140. }
  141. static int si_ih_sw_init(void *handle)
  142. {
  143. int r;
  144. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  145. r = amdgpu_ih_ring_init(adev, 64 * 1024, false);
  146. if (r)
  147. return r;
  148. return amdgpu_irq_init(adev);
  149. }
  150. static int si_ih_sw_fini(void *handle)
  151. {
  152. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  153. amdgpu_irq_fini(adev);
  154. amdgpu_ih_ring_fini(adev);
  155. return 0;
  156. }
  157. static int si_ih_hw_init(void *handle)
  158. {
  159. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  160. return si_ih_irq_init(adev);
  161. }
  162. static int si_ih_hw_fini(void *handle)
  163. {
  164. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  165. si_ih_irq_disable(adev);
  166. return 0;
  167. }
  168. static int si_ih_suspend(void *handle)
  169. {
  170. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  171. return si_ih_hw_fini(adev);
  172. }
  173. static int si_ih_resume(void *handle)
  174. {
  175. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  176. return si_ih_hw_init(adev);
  177. }
  178. static bool si_ih_is_idle(void *handle)
  179. {
  180. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  181. u32 tmp = RREG32(SRBM_STATUS);
  182. if (tmp & SRBM_STATUS__IH_BUSY_MASK)
  183. return false;
  184. return true;
  185. }
  186. static int si_ih_wait_for_idle(void *handle)
  187. {
  188. unsigned i;
  189. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  190. for (i = 0; i < adev->usec_timeout; i++) {
  191. if (si_ih_is_idle(handle))
  192. return 0;
  193. udelay(1);
  194. }
  195. return -ETIMEDOUT;
  196. }
  197. static int si_ih_soft_reset(void *handle)
  198. {
  199. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  200. u32 srbm_soft_reset = 0;
  201. u32 tmp = RREG32(SRBM_STATUS);
  202. if (tmp & SRBM_STATUS__IH_BUSY_MASK)
  203. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_IH_MASK;
  204. if (srbm_soft_reset) {
  205. tmp = RREG32(SRBM_SOFT_RESET);
  206. tmp |= srbm_soft_reset;
  207. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  208. WREG32(SRBM_SOFT_RESET, tmp);
  209. tmp = RREG32(SRBM_SOFT_RESET);
  210. udelay(50);
  211. tmp &= ~srbm_soft_reset;
  212. WREG32(SRBM_SOFT_RESET, tmp);
  213. tmp = RREG32(SRBM_SOFT_RESET);
  214. udelay(50);
  215. }
  216. return 0;
  217. }
  218. static int si_ih_set_clockgating_state(void *handle,
  219. enum amd_clockgating_state state)
  220. {
  221. return 0;
  222. }
  223. static int si_ih_set_powergating_state(void *handle,
  224. enum amd_powergating_state state)
  225. {
  226. return 0;
  227. }
  228. static const struct amd_ip_funcs si_ih_ip_funcs = {
  229. .name = "si_ih",
  230. .early_init = si_ih_early_init,
  231. .late_init = NULL,
  232. .sw_init = si_ih_sw_init,
  233. .sw_fini = si_ih_sw_fini,
  234. .hw_init = si_ih_hw_init,
  235. .hw_fini = si_ih_hw_fini,
  236. .suspend = si_ih_suspend,
  237. .resume = si_ih_resume,
  238. .is_idle = si_ih_is_idle,
  239. .wait_for_idle = si_ih_wait_for_idle,
  240. .soft_reset = si_ih_soft_reset,
  241. .set_clockgating_state = si_ih_set_clockgating_state,
  242. .set_powergating_state = si_ih_set_powergating_state,
  243. };
  244. static const struct amdgpu_ih_funcs si_ih_funcs = {
  245. .get_wptr = si_ih_get_wptr,
  246. .prescreen_iv = si_ih_prescreen_iv,
  247. .decode_iv = si_ih_decode_iv,
  248. .set_rptr = si_ih_set_rptr
  249. };
  250. static void si_ih_set_interrupt_funcs(struct amdgpu_device *adev)
  251. {
  252. if (adev->irq.ih_funcs == NULL)
  253. adev->irq.ih_funcs = &si_ih_funcs;
  254. }
  255. const struct amdgpu_ip_block_version si_ih_ip_block =
  256. {
  257. .type = AMD_IP_BLOCK_TYPE_IH,
  258. .major = 1,
  259. .minor = 0,
  260. .rev = 0,
  261. .funcs = &si_ih_ip_funcs,
  262. };