si_ih.c 7.9 KB

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