si_ih.c 7.5 KB

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