tonga_ih.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright 2014 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 "vid.h"
  27. #include "oss/oss_3_0_d.h"
  28. #include "oss/oss_3_0_sh_mask.h"
  29. #include "bif/bif_5_1_d.h"
  30. #include "bif/bif_5_1_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 tonga_ih_set_interrupt_funcs(struct amdgpu_device *adev);
  46. /**
  47. * tonga_ih_enable_interrupts - Enable the interrupt ring buffer
  48. *
  49. * @adev: amdgpu_device pointer
  50. *
  51. * Enable the interrupt ring buffer (VI).
  52. */
  53. static void tonga_ih_enable_interrupts(struct amdgpu_device *adev)
  54. {
  55. u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
  56. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 1);
  57. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 1);
  58. WREG32(mmIH_RB_CNTL, ih_rb_cntl);
  59. adev->irq.ih.enabled = true;
  60. }
  61. /**
  62. * tonga_ih_disable_interrupts - Disable the interrupt ring buffer
  63. *
  64. * @adev: amdgpu_device pointer
  65. *
  66. * Disable the interrupt ring buffer (VI).
  67. */
  68. static void tonga_ih_disable_interrupts(struct amdgpu_device *adev)
  69. {
  70. u32 ih_rb_cntl = RREG32(mmIH_RB_CNTL);
  71. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 0);
  72. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, ENABLE_INTR, 0);
  73. WREG32(mmIH_RB_CNTL, ih_rb_cntl);
  74. /* set rptr, wptr to 0 */
  75. WREG32(mmIH_RB_RPTR, 0);
  76. WREG32(mmIH_RB_WPTR, 0);
  77. adev->irq.ih.enabled = false;
  78. adev->irq.ih.rptr = 0;
  79. }
  80. /**
  81. * tonga_ih_irq_init - init and enable the interrupt ring
  82. *
  83. * @adev: amdgpu_device pointer
  84. *
  85. * Allocate a ring buffer for the interrupt controller,
  86. * enable the RLC, disable interrupts, enable the IH
  87. * ring buffer and enable it (VI).
  88. * Called at device load and reume.
  89. * Returns 0 for success, errors for failure.
  90. */
  91. static int tonga_ih_irq_init(struct amdgpu_device *adev)
  92. {
  93. int ret = 0;
  94. int rb_bufsz;
  95. u32 interrupt_cntl, ih_rb_cntl, ih_doorbell_rtpr;
  96. u64 wptr_off;
  97. /* disable irqs */
  98. tonga_ih_disable_interrupts(adev);
  99. /* setup interrupt control */
  100. WREG32(mmINTERRUPT_CNTL2, adev->dummy_page.addr >> 8);
  101. interrupt_cntl = RREG32(mmINTERRUPT_CNTL);
  102. /* INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=0 - dummy read disabled with msi, enabled without msi
  103. * INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=1 - dummy read controlled by IH_DUMMY_RD_EN
  104. */
  105. interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_DUMMY_RD_OVERRIDE, 0);
  106. /* INTERRUPT_CNTL__IH_REQ_NONSNOOP_EN_MASK=1 if ring is in non-cacheable memory, e.g., vram */
  107. interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_REQ_NONSNOOP_EN, 0);
  108. WREG32(mmINTERRUPT_CNTL, interrupt_cntl);
  109. /* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
  110. if (adev->irq.ih.use_bus_addr)
  111. WREG32(mmIH_RB_BASE, adev->irq.ih.rb_dma_addr >> 8);
  112. else
  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 = REG_SET_FIELD(0, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
  116. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
  117. /* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register value is written to memory */
  118. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_WRITEBACK_ENABLE, 1);
  119. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0);
  120. if (adev->irq.msi_enabled)
  121. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RPTR_REARM, 1);
  122. WREG32(mmIH_RB_CNTL, ih_rb_cntl);
  123. /* set the writeback address whether it's enabled or not */
  124. if (adev->irq.ih.use_bus_addr)
  125. wptr_off = adev->irq.ih.rb_dma_addr + (adev->irq.ih.wptr_offs * 4);
  126. else
  127. wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
  128. WREG32(mmIH_RB_WPTR_ADDR_LO, lower_32_bits(wptr_off));
  129. WREG32(mmIH_RB_WPTR_ADDR_HI, upper_32_bits(wptr_off) & 0xFF);
  130. /* set rptr, wptr to 0 */
  131. WREG32(mmIH_RB_RPTR, 0);
  132. WREG32(mmIH_RB_WPTR, 0);
  133. ih_doorbell_rtpr = RREG32(mmIH_DOORBELL_RPTR);
  134. if (adev->irq.ih.use_doorbell) {
  135. ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
  136. OFFSET, adev->irq.ih.doorbell_index);
  137. ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
  138. ENABLE, 1);
  139. } else {
  140. ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
  141. ENABLE, 0);
  142. }
  143. WREG32(mmIH_DOORBELL_RPTR, ih_doorbell_rtpr);
  144. pci_set_master(adev->pdev);
  145. /* enable interrupts */
  146. tonga_ih_enable_interrupts(adev);
  147. return ret;
  148. }
  149. /**
  150. * tonga_ih_irq_disable - disable interrupts
  151. *
  152. * @adev: amdgpu_device pointer
  153. *
  154. * Disable interrupts on the hw (VI).
  155. */
  156. static void tonga_ih_irq_disable(struct amdgpu_device *adev)
  157. {
  158. tonga_ih_disable_interrupts(adev);
  159. /* Wait and acknowledge irq */
  160. mdelay(1);
  161. }
  162. /**
  163. * tonga_ih_get_wptr - get the IH ring buffer wptr
  164. *
  165. * @adev: amdgpu_device pointer
  166. *
  167. * Get the IH ring buffer wptr from either the register
  168. * or the writeback memory buffer (VI). Also check for
  169. * ring buffer overflow and deal with it.
  170. * Used by cz_irq_process(VI).
  171. * Returns the value of the wptr.
  172. */
  173. static u32 tonga_ih_get_wptr(struct amdgpu_device *adev)
  174. {
  175. u32 wptr, tmp;
  176. if (adev->irq.ih.use_bus_addr)
  177. wptr = le32_to_cpu(adev->irq.ih.ring[adev->irq.ih.wptr_offs]);
  178. else
  179. wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
  180. if (REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW)) {
  181. wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
  182. /* When a ring buffer overflow happen start parsing interrupt
  183. * from the last not overwritten vector (wptr + 16). Hopefully
  184. * this should allow us to catchup.
  185. */
  186. dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
  187. wptr, adev->irq.ih.rptr, (wptr + 16) & adev->irq.ih.ptr_mask);
  188. adev->irq.ih.rptr = (wptr + 16) & adev->irq.ih.ptr_mask;
  189. tmp = RREG32(mmIH_RB_CNTL);
  190. tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
  191. WREG32(mmIH_RB_CNTL, tmp);
  192. }
  193. return (wptr & adev->irq.ih.ptr_mask);
  194. }
  195. /**
  196. * tonga_ih_decode_iv - decode an interrupt vector
  197. *
  198. * @adev: amdgpu_device pointer
  199. *
  200. * Decodes the interrupt vector at the current rptr
  201. * position and also advance the position.
  202. */
  203. static void tonga_ih_decode_iv(struct amdgpu_device *adev,
  204. struct amdgpu_iv_entry *entry)
  205. {
  206. /* wptr/rptr are in bytes! */
  207. u32 ring_index = adev->irq.ih.rptr >> 2;
  208. uint32_t dw[4];
  209. dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
  210. dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
  211. dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
  212. dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
  213. entry->src_id = dw[0] & 0xff;
  214. entry->src_data = dw[1] & 0xfffffff;
  215. entry->ring_id = dw[2] & 0xff;
  216. entry->vm_id = (dw[2] >> 8) & 0xff;
  217. entry->pas_id = (dw[2] >> 16) & 0xffff;
  218. /* wptr/rptr are in bytes! */
  219. adev->irq.ih.rptr += 16;
  220. }
  221. /**
  222. * tonga_ih_set_rptr - set the IH ring buffer rptr
  223. *
  224. * @adev: amdgpu_device pointer
  225. *
  226. * Set the IH ring buffer rptr.
  227. */
  228. static void tonga_ih_set_rptr(struct amdgpu_device *adev)
  229. {
  230. if (adev->irq.ih.use_doorbell) {
  231. /* XXX check if swapping is necessary on BE */
  232. if (adev->irq.ih.use_bus_addr)
  233. adev->irq.ih.ring[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
  234. else
  235. adev->wb.wb[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
  236. WDOORBELL32(adev->irq.ih.doorbell_index, adev->irq.ih.rptr);
  237. } else {
  238. WREG32(mmIH_RB_RPTR, adev->irq.ih.rptr);
  239. }
  240. }
  241. static int tonga_ih_early_init(void *handle)
  242. {
  243. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  244. tonga_ih_set_interrupt_funcs(adev);
  245. return 0;
  246. }
  247. static int tonga_ih_sw_init(void *handle)
  248. {
  249. int r;
  250. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  251. r = amdgpu_ih_ring_init(adev, 4 * 1024, true);
  252. if (r)
  253. return r;
  254. adev->irq.ih.use_doorbell = true;
  255. adev->irq.ih.doorbell_index = AMDGPU_DOORBELL_IH;
  256. r = amdgpu_irq_init(adev);
  257. return r;
  258. }
  259. static int tonga_ih_sw_fini(void *handle)
  260. {
  261. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  262. amdgpu_irq_fini(adev);
  263. amdgpu_ih_ring_fini(adev);
  264. return 0;
  265. }
  266. static int tonga_ih_hw_init(void *handle)
  267. {
  268. int r;
  269. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  270. r = tonga_ih_irq_init(adev);
  271. if (r)
  272. return r;
  273. return 0;
  274. }
  275. static int tonga_ih_hw_fini(void *handle)
  276. {
  277. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  278. tonga_ih_irq_disable(adev);
  279. return 0;
  280. }
  281. static int tonga_ih_suspend(void *handle)
  282. {
  283. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  284. return tonga_ih_hw_fini(adev);
  285. }
  286. static int tonga_ih_resume(void *handle)
  287. {
  288. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  289. return tonga_ih_hw_init(adev);
  290. }
  291. static bool tonga_ih_is_idle(void *handle)
  292. {
  293. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  294. u32 tmp = RREG32(mmSRBM_STATUS);
  295. if (REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
  296. return false;
  297. return true;
  298. }
  299. static int tonga_ih_wait_for_idle(void *handle)
  300. {
  301. unsigned i;
  302. u32 tmp;
  303. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  304. for (i = 0; i < adev->usec_timeout; i++) {
  305. /* read MC_STATUS */
  306. tmp = RREG32(mmSRBM_STATUS);
  307. if (!REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
  308. return 0;
  309. udelay(1);
  310. }
  311. return -ETIMEDOUT;
  312. }
  313. static void tonga_ih_print_status(void *handle)
  314. {
  315. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  316. dev_info(adev->dev, "TONGA IH registers\n");
  317. dev_info(adev->dev, " SRBM_STATUS=0x%08X\n",
  318. RREG32(mmSRBM_STATUS));
  319. dev_info(adev->dev, " SRBM_STATUS2=0x%08X\n",
  320. RREG32(mmSRBM_STATUS2));
  321. dev_info(adev->dev, " INTERRUPT_CNTL=0x%08X\n",
  322. RREG32(mmINTERRUPT_CNTL));
  323. dev_info(adev->dev, " INTERRUPT_CNTL2=0x%08X\n",
  324. RREG32(mmINTERRUPT_CNTL2));
  325. dev_info(adev->dev, " IH_CNTL=0x%08X\n",
  326. RREG32(mmIH_CNTL));
  327. dev_info(adev->dev, " IH_RB_CNTL=0x%08X\n",
  328. RREG32(mmIH_RB_CNTL));
  329. dev_info(adev->dev, " IH_RB_BASE=0x%08X\n",
  330. RREG32(mmIH_RB_BASE));
  331. dev_info(adev->dev, " IH_RB_WPTR_ADDR_LO=0x%08X\n",
  332. RREG32(mmIH_RB_WPTR_ADDR_LO));
  333. dev_info(adev->dev, " IH_RB_WPTR_ADDR_HI=0x%08X\n",
  334. RREG32(mmIH_RB_WPTR_ADDR_HI));
  335. dev_info(adev->dev, " IH_RB_RPTR=0x%08X\n",
  336. RREG32(mmIH_RB_RPTR));
  337. dev_info(adev->dev, " IH_RB_WPTR=0x%08X\n",
  338. RREG32(mmIH_RB_WPTR));
  339. }
  340. static int tonga_ih_soft_reset(void *handle)
  341. {
  342. u32 srbm_soft_reset = 0;
  343. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  344. u32 tmp = RREG32(mmSRBM_STATUS);
  345. if (tmp & SRBM_STATUS__IH_BUSY_MASK)
  346. srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET,
  347. SOFT_RESET_IH, 1);
  348. if (srbm_soft_reset) {
  349. tonga_ih_print_status(adev);
  350. tmp = RREG32(mmSRBM_SOFT_RESET);
  351. tmp |= srbm_soft_reset;
  352. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  353. WREG32(mmSRBM_SOFT_RESET, tmp);
  354. tmp = RREG32(mmSRBM_SOFT_RESET);
  355. udelay(50);
  356. tmp &= ~srbm_soft_reset;
  357. WREG32(mmSRBM_SOFT_RESET, tmp);
  358. tmp = RREG32(mmSRBM_SOFT_RESET);
  359. /* Wait a little for things to settle down */
  360. udelay(50);
  361. tonga_ih_print_status(adev);
  362. }
  363. return 0;
  364. }
  365. static int tonga_ih_set_clockgating_state(void *handle,
  366. enum amd_clockgating_state state)
  367. {
  368. return 0;
  369. }
  370. static int tonga_ih_set_powergating_state(void *handle,
  371. enum amd_powergating_state state)
  372. {
  373. return 0;
  374. }
  375. const struct amd_ip_funcs tonga_ih_ip_funcs = {
  376. .early_init = tonga_ih_early_init,
  377. .late_init = NULL,
  378. .sw_init = tonga_ih_sw_init,
  379. .sw_fini = tonga_ih_sw_fini,
  380. .hw_init = tonga_ih_hw_init,
  381. .hw_fini = tonga_ih_hw_fini,
  382. .suspend = tonga_ih_suspend,
  383. .resume = tonga_ih_resume,
  384. .is_idle = tonga_ih_is_idle,
  385. .wait_for_idle = tonga_ih_wait_for_idle,
  386. .soft_reset = tonga_ih_soft_reset,
  387. .print_status = tonga_ih_print_status,
  388. .set_clockgating_state = tonga_ih_set_clockgating_state,
  389. .set_powergating_state = tonga_ih_set_powergating_state,
  390. };
  391. static const struct amdgpu_ih_funcs tonga_ih_funcs = {
  392. .get_wptr = tonga_ih_get_wptr,
  393. .decode_iv = tonga_ih_decode_iv,
  394. .set_rptr = tonga_ih_set_rptr
  395. };
  396. static void tonga_ih_set_interrupt_funcs(struct amdgpu_device *adev)
  397. {
  398. if (adev->irq.ih_funcs == NULL)
  399. adev->irq.ih_funcs = &tonga_ih_funcs;
  400. }