vega10_ih.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright 2016 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 "soc15.h"
  27. #include "vega10/soc15ip.h"
  28. #include "vega10/OSSSYS/osssys_4_0_offset.h"
  29. #include "vega10/OSSSYS/osssys_4_0_sh_mask.h"
  30. #include "soc15_common.h"
  31. #include "vega10_ih.h"
  32. static void vega10_ih_set_interrupt_funcs(struct amdgpu_device *adev);
  33. /**
  34. * vega10_ih_enable_interrupts - Enable the interrupt ring buffer
  35. *
  36. * @adev: amdgpu_device pointer
  37. *
  38. * Enable the interrupt ring buffer (VEGA10).
  39. */
  40. static void vega10_ih_enable_interrupts(struct amdgpu_device *adev)
  41. {
  42. u32 ih_rb_cntl = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL));
  43. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 1);
  44. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 1);
  45. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL), ih_rb_cntl);
  46. adev->irq.ih.enabled = true;
  47. }
  48. /**
  49. * vega10_ih_disable_interrupts - Disable the interrupt ring buffer
  50. *
  51. * @adev: amdgpu_device pointer
  52. *
  53. * Disable the interrupt ring buffer (VEGA10).
  54. */
  55. static void vega10_ih_disable_interrupts(struct amdgpu_device *adev)
  56. {
  57. u32 ih_rb_cntl = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL));
  58. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 0);
  59. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 0);
  60. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL), ih_rb_cntl);
  61. /* set rptr, wptr to 0 */
  62. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_RPTR), 0);
  63. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR), 0);
  64. adev->irq.ih.enabled = false;
  65. adev->irq.ih.rptr = 0;
  66. }
  67. /**
  68. * vega10_ih_irq_init - init and enable the interrupt ring
  69. *
  70. * @adev: amdgpu_device pointer
  71. *
  72. * Allocate a ring buffer for the interrupt controller,
  73. * enable the RLC, disable interrupts, enable the IH
  74. * ring buffer and enable it (VI).
  75. * Called at device load and reume.
  76. * Returns 0 for success, errors for failure.
  77. */
  78. static int vega10_ih_irq_init(struct amdgpu_device *adev)
  79. {
  80. int ret = 0;
  81. int rb_bufsz;
  82. u32 ih_rb_cntl, ih_doorbell_rtpr;
  83. u32 tmp;
  84. u64 wptr_off;
  85. /* disable irqs */
  86. vega10_ih_disable_interrupts(adev);
  87. nbio_v6_1_ih_control(adev);
  88. ih_rb_cntl = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL));
  89. /* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
  90. if (adev->irq.ih.use_bus_addr) {
  91. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE), adev->irq.ih.rb_dma_addr >> 8);
  92. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_HI), ((u64)adev->irq.ih.rb_dma_addr >> 40) & 0xff);
  93. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_SPACE, 1);
  94. } else {
  95. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE), adev->irq.ih.gpu_addr >> 8);
  96. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_BASE_HI), (adev->irq.ih.gpu_addr >> 40) & 0xff);
  97. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_SPACE, 4);
  98. }
  99. rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
  100. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
  101. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_OVERFLOW_ENABLE, 1);
  102. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
  103. /* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register value is written to memory */
  104. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_WRITEBACK_ENABLE, 1);
  105. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_SNOOP, 1);
  106. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_RO, 0);
  107. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0);
  108. if (adev->irq.msi_enabled)
  109. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RPTR_REARM, 1);
  110. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL), ih_rb_cntl);
  111. /* set the writeback address whether it's enabled or not */
  112. if (adev->irq.ih.use_bus_addr)
  113. wptr_off = adev->irq.ih.rb_dma_addr + (adev->irq.ih.wptr_offs * 4);
  114. else
  115. wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
  116. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_ADDR_LO), lower_32_bits(wptr_off));
  117. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR_ADDR_HI), upper_32_bits(wptr_off) & 0xFF);
  118. /* set rptr, wptr to 0 */
  119. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_RPTR), 0);
  120. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_WPTR), 0);
  121. ih_doorbell_rtpr = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_DOORBELL_RPTR));
  122. if (adev->irq.ih.use_doorbell) {
  123. ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
  124. OFFSET, adev->irq.ih.doorbell_index);
  125. ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
  126. ENABLE, 1);
  127. } else {
  128. ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
  129. ENABLE, 0);
  130. }
  131. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_DOORBELL_RPTR), ih_doorbell_rtpr);
  132. nbio_v6_1_ih_doorbell_range(adev, adev->irq.ih.use_doorbell, adev->irq.ih.doorbell_index);
  133. tmp = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_STORM_CLIENT_LIST_CNTL));
  134. tmp = REG_SET_FIELD(tmp, IH_STORM_CLIENT_LIST_CNTL,
  135. CLIENT18_IS_STORM_CLIENT, 1);
  136. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_STORM_CLIENT_LIST_CNTL), tmp);
  137. tmp = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_INT_FLOOD_CNTL));
  138. tmp = REG_SET_FIELD(tmp, IH_INT_FLOOD_CNTL, FLOOD_CNTL_ENABLE, 1);
  139. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_INT_FLOOD_CNTL), tmp);
  140. pci_set_master(adev->pdev);
  141. /* enable interrupts */
  142. vega10_ih_enable_interrupts(adev);
  143. return ret;
  144. }
  145. /**
  146. * vega10_ih_irq_disable - disable interrupts
  147. *
  148. * @adev: amdgpu_device pointer
  149. *
  150. * Disable interrupts on the hw (VEGA10).
  151. */
  152. static void vega10_ih_irq_disable(struct amdgpu_device *adev)
  153. {
  154. vega10_ih_disable_interrupts(adev);
  155. /* Wait and acknowledge irq */
  156. mdelay(1);
  157. }
  158. /**
  159. * vega10_ih_get_wptr - get the IH ring buffer wptr
  160. *
  161. * @adev: amdgpu_device pointer
  162. *
  163. * Get the IH ring buffer wptr from either the register
  164. * or the writeback memory buffer (VEGA10). Also check for
  165. * ring buffer overflow and deal with it.
  166. * Returns the value of the wptr.
  167. */
  168. static u32 vega10_ih_get_wptr(struct amdgpu_device *adev)
  169. {
  170. u32 wptr, tmp;
  171. if (adev->irq.ih.use_bus_addr)
  172. wptr = le32_to_cpu(adev->irq.ih.ring[adev->irq.ih.wptr_offs]);
  173. else
  174. wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
  175. if (REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW)) {
  176. wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
  177. /* When a ring buffer overflow happen start parsing interrupt
  178. * from the last not overwritten vector (wptr + 32). Hopefully
  179. * this should allow us to catchup.
  180. */
  181. tmp = (wptr + 32) & adev->irq.ih.ptr_mask;
  182. dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
  183. wptr, adev->irq.ih.rptr, tmp);
  184. adev->irq.ih.rptr = tmp;
  185. tmp = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL));
  186. tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
  187. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_CNTL), tmp);
  188. }
  189. return (wptr & adev->irq.ih.ptr_mask);
  190. }
  191. /**
  192. * vega10_ih_decode_iv - decode an interrupt vector
  193. *
  194. * @adev: amdgpu_device pointer
  195. *
  196. * Decodes the interrupt vector at the current rptr
  197. * position and also advance the position.
  198. */
  199. static void vega10_ih_decode_iv(struct amdgpu_device *adev,
  200. struct amdgpu_iv_entry *entry)
  201. {
  202. /* wptr/rptr are in bytes! */
  203. u32 ring_index = adev->irq.ih.rptr >> 2;
  204. uint32_t dw[8];
  205. dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
  206. dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
  207. dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
  208. dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
  209. dw[4] = le32_to_cpu(adev->irq.ih.ring[ring_index + 4]);
  210. dw[5] = le32_to_cpu(adev->irq.ih.ring[ring_index + 5]);
  211. dw[6] = le32_to_cpu(adev->irq.ih.ring[ring_index + 6]);
  212. dw[7] = le32_to_cpu(adev->irq.ih.ring[ring_index + 7]);
  213. entry->client_id = dw[0] & 0xff;
  214. entry->src_id = (dw[0] >> 8) & 0xff;
  215. entry->ring_id = (dw[0] >> 16) & 0xff;
  216. entry->vm_id = (dw[0] >> 24) & 0xf;
  217. entry->vm_id_src = (dw[0] >> 31);
  218. entry->timestamp = dw[1] | ((u64)(dw[2] & 0xffff) << 32);
  219. entry->timestamp_src = dw[2] >> 31;
  220. entry->pas_id = dw[3] & 0xffff;
  221. entry->pasid_src = dw[3] >> 31;
  222. entry->src_data[0] = dw[4];
  223. entry->src_data[1] = dw[5];
  224. entry->src_data[2] = dw[6];
  225. entry->src_data[3] = dw[7];
  226. /* wptr/rptr are in bytes! */
  227. adev->irq.ih.rptr += 32;
  228. }
  229. /**
  230. * vega10_ih_set_rptr - set the IH ring buffer rptr
  231. *
  232. * @adev: amdgpu_device pointer
  233. *
  234. * Set the IH ring buffer rptr.
  235. */
  236. static void vega10_ih_set_rptr(struct amdgpu_device *adev)
  237. {
  238. if (adev->irq.ih.use_doorbell) {
  239. /* XXX check if swapping is necessary on BE */
  240. if (adev->irq.ih.use_bus_addr)
  241. adev->irq.ih.ring[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
  242. else
  243. adev->wb.wb[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
  244. WDOORBELL32(adev->irq.ih.doorbell_index, adev->irq.ih.rptr);
  245. } else {
  246. WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_RB_RPTR), adev->irq.ih.rptr);
  247. }
  248. }
  249. static int vega10_ih_early_init(void *handle)
  250. {
  251. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  252. vega10_ih_set_interrupt_funcs(adev);
  253. return 0;
  254. }
  255. static int vega10_ih_sw_init(void *handle)
  256. {
  257. int r;
  258. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  259. r = amdgpu_ih_ring_init(adev, 256 * 1024, true);
  260. if (r)
  261. return r;
  262. adev->irq.ih.use_doorbell = true;
  263. adev->irq.ih.doorbell_index = AMDGPU_DOORBELL64_IH << 1;
  264. r = amdgpu_irq_init(adev);
  265. return r;
  266. }
  267. static int vega10_ih_sw_fini(void *handle)
  268. {
  269. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  270. amdgpu_irq_fini(adev);
  271. amdgpu_ih_ring_fini(adev);
  272. return 0;
  273. }
  274. static int vega10_ih_hw_init(void *handle)
  275. {
  276. int r;
  277. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  278. r = vega10_ih_irq_init(adev);
  279. if (r)
  280. return r;
  281. return 0;
  282. }
  283. static int vega10_ih_hw_fini(void *handle)
  284. {
  285. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  286. vega10_ih_irq_disable(adev);
  287. return 0;
  288. }
  289. static int vega10_ih_suspend(void *handle)
  290. {
  291. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  292. return vega10_ih_hw_fini(adev);
  293. }
  294. static int vega10_ih_resume(void *handle)
  295. {
  296. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  297. return vega10_ih_hw_init(adev);
  298. }
  299. static bool vega10_ih_is_idle(void *handle)
  300. {
  301. /* todo */
  302. return true;
  303. }
  304. static int vega10_ih_wait_for_idle(void *handle)
  305. {
  306. /* todo */
  307. return -ETIMEDOUT;
  308. }
  309. static int vega10_ih_soft_reset(void *handle)
  310. {
  311. /* todo */
  312. return 0;
  313. }
  314. static int vega10_ih_set_clockgating_state(void *handle,
  315. enum amd_clockgating_state state)
  316. {
  317. return 0;
  318. }
  319. static int vega10_ih_set_powergating_state(void *handle,
  320. enum amd_powergating_state state)
  321. {
  322. return 0;
  323. }
  324. const struct amd_ip_funcs vega10_ih_ip_funcs = {
  325. .name = "vega10_ih",
  326. .early_init = vega10_ih_early_init,
  327. .late_init = NULL,
  328. .sw_init = vega10_ih_sw_init,
  329. .sw_fini = vega10_ih_sw_fini,
  330. .hw_init = vega10_ih_hw_init,
  331. .hw_fini = vega10_ih_hw_fini,
  332. .suspend = vega10_ih_suspend,
  333. .resume = vega10_ih_resume,
  334. .is_idle = vega10_ih_is_idle,
  335. .wait_for_idle = vega10_ih_wait_for_idle,
  336. .soft_reset = vega10_ih_soft_reset,
  337. .set_clockgating_state = vega10_ih_set_clockgating_state,
  338. .set_powergating_state = vega10_ih_set_powergating_state,
  339. };
  340. static const struct amdgpu_ih_funcs vega10_ih_funcs = {
  341. .get_wptr = vega10_ih_get_wptr,
  342. .decode_iv = vega10_ih_decode_iv,
  343. .set_rptr = vega10_ih_set_rptr
  344. };
  345. static void vega10_ih_set_interrupt_funcs(struct amdgpu_device *adev)
  346. {
  347. if (adev->irq.ih_funcs == NULL)
  348. adev->irq.ih_funcs = &vega10_ih_funcs;
  349. }
  350. const struct amdgpu_ip_block_version vega10_ih_ip_block =
  351. {
  352. .type = AMD_IP_BLOCK_TYPE_IH,
  353. .major = 4,
  354. .minor = 0,
  355. .rev = 0,
  356. .funcs = &vega10_ih_ip_funcs,
  357. };