si_ih.c 7.6 KB

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