cik_ih.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright 2012 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 "cikd.h"
  27. #include "bif/bif_4_1_d.h"
  28. #include "bif/bif_4_1_sh_mask.h"
  29. #include "oss/oss_2_0_d.h"
  30. #include "oss/oss_2_0_sh_mask.h"
  31. /*
  32. * Interrupts
  33. * Starting with r6xx, interrupts are handled via a ring buffer.
  34. * Ring buffers are areas of GPU accessible memory that the GPU
  35. * writes interrupt vectors into and the host reads vectors out of.
  36. * There is a rptr (read pointer) that determines where the
  37. * host is currently reading, and a wptr (write pointer)
  38. * which determines where the GPU has written. When the
  39. * pointers are equal, the ring is idle. When the GPU
  40. * writes vectors to the ring buffer, it increments the
  41. * wptr. When there is an interrupt, the host then starts
  42. * fetching commands and processing them until the pointers are
  43. * equal again at which point it updates the rptr.
  44. */
  45. static void cik_ih_set_interrupt_funcs(struct amdgpu_device *adev);
  46. /**
  47. * cik_ih_enable_interrupts - Enable the interrupt ring buffer
  48. *
  49. * @adev: amdgpu_device pointer
  50. *
  51. * Enable the interrupt ring buffer (CIK).
  52. */
  53. static void cik_ih_enable_interrupts(struct amdgpu_device *adev)
  54. {
  55. u32 ih_cntl = RREG32(mmIH_CNTL);
  56. u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
  57. ih_cntl |= IH_CNTL__ENABLE_INTR_MASK;
  58. ih_rb_cntl |= IH_RB_CNTL__RB_ENABLE_MASK;
  59. WREG32(mmIH_CNTL, ih_cntl);
  60. WREG32(mmIH_RB_CNTL, ih_rb_cntl);
  61. adev->irq.ih.enabled = true;
  62. }
  63. /**
  64. * cik_ih_disable_interrupts - Disable the interrupt ring buffer
  65. *
  66. * @adev: amdgpu_device pointer
  67. *
  68. * Disable the interrupt ring buffer (CIK).
  69. */
  70. static void cik_ih_disable_interrupts(struct amdgpu_device *adev)
  71. {
  72. u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
  73. u32 ih_cntl = RREG32(mmIH_CNTL);
  74. ih_rb_cntl &= ~IH_RB_CNTL__RB_ENABLE_MASK;
  75. ih_cntl &= ~IH_CNTL__ENABLE_INTR_MASK;
  76. WREG32(mmIH_RB_CNTL, ih_rb_cntl);
  77. WREG32(mmIH_CNTL, ih_cntl);
  78. /* set rptr, wptr to 0 */
  79. WREG32(mmIH_RB_RPTR, 0);
  80. WREG32(mmIH_RB_WPTR, 0);
  81. adev->irq.ih.enabled = false;
  82. adev->irq.ih.rptr = 0;
  83. }
  84. /**
  85. * cik_ih_irq_init - init and enable the interrupt ring
  86. *
  87. * @adev: amdgpu_device pointer
  88. *
  89. * Allocate a ring buffer for the interrupt controller,
  90. * enable the RLC, disable interrupts, enable the IH
  91. * ring buffer and enable it (CIK).
  92. * Called at device load and reume.
  93. * Returns 0 for success, errors for failure.
  94. */
  95. static int cik_ih_irq_init(struct amdgpu_device *adev)
  96. {
  97. int ret = 0;
  98. int rb_bufsz;
  99. u32 interrupt_cntl, ih_cntl, ih_rb_cntl;
  100. u64 wptr_off;
  101. /* disable irqs */
  102. cik_ih_disable_interrupts(adev);
  103. /* setup interrupt control */
  104. WREG32(mmINTERRUPT_CNTL2, adev->dummy_page.addr >> 8);
  105. interrupt_cntl = RREG32(mmINTERRUPT_CNTL);
  106. /* INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=0 - dummy read disabled with msi, enabled without msi
  107. * INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=1 - dummy read controlled by IH_DUMMY_RD_EN
  108. */
  109. interrupt_cntl &= ~INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK;
  110. /* INTERRUPT_CNTL__IH_REQ_NONSNOOP_EN_MASK=1 if ring is in non-cacheable memory, e.g., vram */
  111. interrupt_cntl &= ~INTERRUPT_CNTL__IH_REQ_NONSNOOP_EN_MASK;
  112. WREG32(mmINTERRUPT_CNTL, interrupt_cntl);
  113. WREG32(mmIH_RB_BASE, adev->irq.ih.gpu_addr >> 8);
  114. rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
  115. ih_rb_cntl = (IH_RB_CNTL__WPTR_OVERFLOW_ENABLE_MASK |
  116. IH_RB_CNTL__WPTR_OVERFLOW_CLEAR_MASK |
  117. (rb_bufsz << 1));
  118. ih_rb_cntl |= IH_RB_CNTL__WPTR_WRITEBACK_ENABLE_MASK;
  119. /* set the writeback address whether it's enabled or not */
  120. wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
  121. WREG32(mmIH_RB_WPTR_ADDR_LO, lower_32_bits(wptr_off));
  122. WREG32(mmIH_RB_WPTR_ADDR_HI, upper_32_bits(wptr_off) & 0xFF);
  123. WREG32(mmIH_RB_CNTL, ih_rb_cntl);
  124. /* set rptr, wptr to 0 */
  125. WREG32(mmIH_RB_RPTR, 0);
  126. WREG32(mmIH_RB_WPTR, 0);
  127. /* Default settings for IH_CNTL (disabled at first) */
  128. ih_cntl = (0x10 << IH_CNTL__MC_WRREQ_CREDIT__SHIFT) |
  129. (0x10 << IH_CNTL__MC_WR_CLEAN_CNT__SHIFT) |
  130. (0 << IH_CNTL__MC_VMID__SHIFT);
  131. /* IH_CNTL__RPTR_REARM_MASK only works if msi's are enabled */
  132. if (adev->irq.msi_enabled)
  133. ih_cntl |= IH_CNTL__RPTR_REARM_MASK;
  134. WREG32(mmIH_CNTL, ih_cntl);
  135. pci_set_master(adev->pdev);
  136. /* enable irqs */
  137. cik_ih_enable_interrupts(adev);
  138. return ret;
  139. }
  140. /**
  141. * cik_ih_irq_disable - disable interrupts
  142. *
  143. * @adev: amdgpu_device pointer
  144. *
  145. * Disable interrupts on the hw (CIK).
  146. */
  147. static void cik_ih_irq_disable(struct amdgpu_device *adev)
  148. {
  149. cik_ih_disable_interrupts(adev);
  150. /* Wait and acknowledge irq */
  151. mdelay(1);
  152. }
  153. /**
  154. * cik_ih_get_wptr - get the IH ring buffer wptr
  155. *
  156. * @adev: amdgpu_device pointer
  157. *
  158. * Get the IH ring buffer wptr from either the register
  159. * or the writeback memory buffer (CIK). Also check for
  160. * ring buffer overflow and deal with it.
  161. * Used by cik_irq_process().
  162. * Returns the value of the wptr.
  163. */
  164. static u32 cik_ih_get_wptr(struct amdgpu_device *adev)
  165. {
  166. u32 wptr, tmp;
  167. wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
  168. if (wptr & IH_RB_WPTR__RB_OVERFLOW_MASK) {
  169. wptr &= ~IH_RB_WPTR__RB_OVERFLOW_MASK;
  170. /* When a ring buffer overflow happen start parsing interrupt
  171. * from the last not overwritten vector (wptr + 16). Hopefully
  172. * this should allow us to catchup.
  173. */
  174. dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
  175. wptr, adev->irq.ih.rptr, (wptr + 16) & adev->irq.ih.ptr_mask);
  176. adev->irq.ih.rptr = (wptr + 16) & adev->irq.ih.ptr_mask;
  177. tmp = RREG32(mmIH_RB_CNTL);
  178. tmp |= IH_RB_CNTL__WPTR_OVERFLOW_CLEAR_MASK;
  179. WREG32(mmIH_RB_CNTL, tmp);
  180. }
  181. return (wptr & adev->irq.ih.ptr_mask);
  182. }
  183. /* CIK IV Ring
  184. * Each IV ring entry is 128 bits:
  185. * [7:0] - interrupt source id
  186. * [31:8] - reserved
  187. * [59:32] - interrupt source data
  188. * [63:60] - reserved
  189. * [71:64] - RINGID
  190. * CP:
  191. * ME_ID [1:0], PIPE_ID[1:0], QUEUE_ID[2:0]
  192. * QUEUE_ID - for compute, which of the 8 queues owned by the dispatcher
  193. * - for gfx, hw shader state (0=PS...5=LS, 6=CS)
  194. * ME_ID - 0 = gfx, 1 = first 4 CS pipes, 2 = second 4 CS pipes
  195. * PIPE_ID - ME0 0=3D
  196. * - ME1&2 compute dispatcher (4 pipes each)
  197. * SDMA:
  198. * INSTANCE_ID [1:0], QUEUE_ID[1:0]
  199. * INSTANCE_ID - 0 = sdma0, 1 = sdma1
  200. * QUEUE_ID - 0 = gfx, 1 = rlc0, 2 = rlc1
  201. * [79:72] - VMID
  202. * [95:80] - PASID
  203. * [127:96] - reserved
  204. */
  205. /**
  206. * cik_ih_decode_iv - decode an interrupt vector
  207. *
  208. * @adev: amdgpu_device pointer
  209. *
  210. * Decodes the interrupt vector at the current rptr
  211. * position and also advance the position.
  212. */
  213. static void cik_ih_decode_iv(struct amdgpu_device *adev,
  214. struct amdgpu_iv_entry *entry)
  215. {
  216. /* wptr/rptr are in bytes! */
  217. u32 ring_index = adev->irq.ih.rptr >> 2;
  218. uint32_t dw[4];
  219. dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
  220. dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
  221. dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
  222. dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
  223. entry->src_id = dw[0] & 0xff;
  224. entry->src_data = dw[1] & 0xfffffff;
  225. entry->ring_id = dw[2] & 0xff;
  226. entry->vm_id = (dw[2] >> 8) & 0xff;
  227. entry->pas_id = (dw[2] >> 16) & 0xffff;
  228. /* wptr/rptr are in bytes! */
  229. adev->irq.ih.rptr += 16;
  230. }
  231. /**
  232. * cik_ih_set_rptr - set the IH ring buffer rptr
  233. *
  234. * @adev: amdgpu_device pointer
  235. *
  236. * Set the IH ring buffer rptr.
  237. */
  238. static void cik_ih_set_rptr(struct amdgpu_device *adev)
  239. {
  240. WREG32(mmIH_RB_RPTR, adev->irq.ih.rptr);
  241. }
  242. static int cik_ih_early_init(void *handle)
  243. {
  244. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  245. cik_ih_set_interrupt_funcs(adev);
  246. return 0;
  247. }
  248. static int cik_ih_sw_init(void *handle)
  249. {
  250. int r;
  251. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  252. r = amdgpu_ih_ring_init(adev, 64 * 1024, false);
  253. if (r)
  254. return r;
  255. r = amdgpu_irq_init(adev);
  256. return r;
  257. }
  258. static int cik_ih_sw_fini(void *handle)
  259. {
  260. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  261. amdgpu_irq_fini(adev);
  262. amdgpu_ih_ring_fini(adev);
  263. return 0;
  264. }
  265. static int cik_ih_hw_init(void *handle)
  266. {
  267. int r;
  268. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  269. r = cik_ih_irq_init(adev);
  270. if (r)
  271. return r;
  272. return 0;
  273. }
  274. static int cik_ih_hw_fini(void *handle)
  275. {
  276. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  277. cik_ih_irq_disable(adev);
  278. return 0;
  279. }
  280. static int cik_ih_suspend(void *handle)
  281. {
  282. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  283. return cik_ih_hw_fini(adev);
  284. }
  285. static int cik_ih_resume(void *handle)
  286. {
  287. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  288. return cik_ih_hw_init(adev);
  289. }
  290. static bool cik_ih_is_idle(void *handle)
  291. {
  292. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  293. u32 tmp = RREG32(mmSRBM_STATUS);
  294. if (tmp & SRBM_STATUS__IH_BUSY_MASK)
  295. return false;
  296. return true;
  297. }
  298. static int cik_ih_wait_for_idle(void *handle)
  299. {
  300. unsigned i;
  301. u32 tmp;
  302. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  303. for (i = 0; i < adev->usec_timeout; i++) {
  304. /* read MC_STATUS */
  305. tmp = RREG32(mmSRBM_STATUS) & SRBM_STATUS__IH_BUSY_MASK;
  306. if (!tmp)
  307. return 0;
  308. udelay(1);
  309. }
  310. return -ETIMEDOUT;
  311. }
  312. static void cik_ih_print_status(void *handle)
  313. {
  314. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  315. dev_info(adev->dev, "CIK IH registers\n");
  316. dev_info(adev->dev, " SRBM_STATUS=0x%08X\n",
  317. RREG32(mmSRBM_STATUS));
  318. dev_info(adev->dev, " SRBM_STATUS2=0x%08X\n",
  319. RREG32(mmSRBM_STATUS2));
  320. dev_info(adev->dev, " INTERRUPT_CNTL=0x%08X\n",
  321. RREG32(mmINTERRUPT_CNTL));
  322. dev_info(adev->dev, " INTERRUPT_CNTL2=0x%08X\n",
  323. RREG32(mmINTERRUPT_CNTL2));
  324. dev_info(adev->dev, " IH_CNTL=0x%08X\n",
  325. RREG32(mmIH_CNTL));
  326. dev_info(adev->dev, " IH_RB_CNTL=0x%08X\n",
  327. RREG32(mmIH_RB_CNTL));
  328. dev_info(adev->dev, " IH_RB_BASE=0x%08X\n",
  329. RREG32(mmIH_RB_BASE));
  330. dev_info(adev->dev, " IH_RB_WPTR_ADDR_LO=0x%08X\n",
  331. RREG32(mmIH_RB_WPTR_ADDR_LO));
  332. dev_info(adev->dev, " IH_RB_WPTR_ADDR_HI=0x%08X\n",
  333. RREG32(mmIH_RB_WPTR_ADDR_HI));
  334. dev_info(adev->dev, " IH_RB_RPTR=0x%08X\n",
  335. RREG32(mmIH_RB_RPTR));
  336. dev_info(adev->dev, " IH_RB_WPTR=0x%08X\n",
  337. RREG32(mmIH_RB_WPTR));
  338. }
  339. static int cik_ih_soft_reset(void *handle)
  340. {
  341. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  342. u32 srbm_soft_reset = 0;
  343. u32 tmp = RREG32(mmSRBM_STATUS);
  344. if (tmp & SRBM_STATUS__IH_BUSY_MASK)
  345. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_IH_MASK;
  346. if (srbm_soft_reset) {
  347. cik_ih_print_status((void *)adev);
  348. tmp = RREG32(mmSRBM_SOFT_RESET);
  349. tmp |= srbm_soft_reset;
  350. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  351. WREG32(mmSRBM_SOFT_RESET, tmp);
  352. tmp = RREG32(mmSRBM_SOFT_RESET);
  353. udelay(50);
  354. tmp &= ~srbm_soft_reset;
  355. WREG32(mmSRBM_SOFT_RESET, tmp);
  356. tmp = RREG32(mmSRBM_SOFT_RESET);
  357. /* Wait a little for things to settle down */
  358. udelay(50);
  359. cik_ih_print_status((void *)adev);
  360. }
  361. return 0;
  362. }
  363. static int cik_ih_set_clockgating_state(void *handle,
  364. enum amd_clockgating_state state)
  365. {
  366. return 0;
  367. }
  368. static int cik_ih_set_powergating_state(void *handle,
  369. enum amd_powergating_state state)
  370. {
  371. return 0;
  372. }
  373. const struct amd_ip_funcs cik_ih_ip_funcs = {
  374. .early_init = cik_ih_early_init,
  375. .late_init = NULL,
  376. .sw_init = cik_ih_sw_init,
  377. .sw_fini = cik_ih_sw_fini,
  378. .hw_init = cik_ih_hw_init,
  379. .hw_fini = cik_ih_hw_fini,
  380. .suspend = cik_ih_suspend,
  381. .resume = cik_ih_resume,
  382. .is_idle = cik_ih_is_idle,
  383. .wait_for_idle = cik_ih_wait_for_idle,
  384. .soft_reset = cik_ih_soft_reset,
  385. .print_status = cik_ih_print_status,
  386. .set_clockgating_state = cik_ih_set_clockgating_state,
  387. .set_powergating_state = cik_ih_set_powergating_state,
  388. };
  389. static const struct amdgpu_ih_funcs cik_ih_funcs = {
  390. .get_wptr = cik_ih_get_wptr,
  391. .decode_iv = cik_ih_decode_iv,
  392. .set_rptr = cik_ih_set_rptr
  393. };
  394. static void cik_ih_set_interrupt_funcs(struct amdgpu_device *adev)
  395. {
  396. if (adev->irq.ih_funcs == NULL)
  397. adev->irq.ih_funcs = &cik_ih_funcs;
  398. }