iceland_ih.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 <drm/drmP.h>
  24. #include "amdgpu.h"
  25. #include "amdgpu_ih.h"
  26. #include "vid.h"
  27. #include "oss/oss_2_4_d.h"
  28. #include "oss/oss_2_4_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 iceland_ih_set_interrupt_funcs(struct amdgpu_device *adev);
  46. /**
  47. * iceland_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 iceland_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 = REG_SET_FIELD(ih_cntl, IH_CNTL, ENABLE_INTR, 1);
  58. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 1);
  59. WREG32(mmIH_CNTL, ih_cntl);
  60. WREG32(mmIH_RB_CNTL, ih_rb_cntl);
  61. adev->irq.ih.enabled = true;
  62. }
  63. /**
  64. * iceland_ih_disable_interrupts - Disable the interrupt ring buffer
  65. *
  66. * @adev: amdgpu_device pointer
  67. *
  68. * Disable the interrupt ring buffer (VI).
  69. */
  70. static void iceland_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 = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_ENABLE, 0);
  75. ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL, ENABLE_INTR, 0);
  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. * iceland_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 (VI).
  92. * Called at device load and reume.
  93. * Returns 0 for success, errors for failure.
  94. */
  95. static int iceland_ih_irq_init(struct amdgpu_device *adev)
  96. {
  97. int rb_bufsz;
  98. u32 interrupt_cntl, ih_cntl, ih_rb_cntl;
  99. u64 wptr_off;
  100. /* disable irqs */
  101. iceland_ih_disable_interrupts(adev);
  102. /* setup interrupt control */
  103. WREG32(mmINTERRUPT_CNTL2, adev->dummy_page_addr >> 8);
  104. interrupt_cntl = RREG32(mmINTERRUPT_CNTL);
  105. /* INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=0 - dummy read disabled with msi, enabled without msi
  106. * INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=1 - dummy read controlled by IH_DUMMY_RD_EN
  107. */
  108. interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_DUMMY_RD_OVERRIDE, 0);
  109. /* INTERRUPT_CNTL__IH_REQ_NONSNOOP_EN_MASK=1 if ring is in non-cacheable memory, e.g., vram */
  110. interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_REQ_NONSNOOP_EN, 0);
  111. WREG32(mmINTERRUPT_CNTL, interrupt_cntl);
  112. /* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
  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_ENABLE, 1);
  116. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
  117. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
  118. /* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register value is written to memory */
  119. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_WRITEBACK_ENABLE, 1);
  120. /* set the writeback address whether it's enabled or not */
  121. wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
  122. WREG32(mmIH_RB_WPTR_ADDR_LO, lower_32_bits(wptr_off));
  123. WREG32(mmIH_RB_WPTR_ADDR_HI, upper_32_bits(wptr_off) & 0xFF);
  124. WREG32(mmIH_RB_CNTL, ih_rb_cntl);
  125. /* set rptr, wptr to 0 */
  126. WREG32(mmIH_RB_RPTR, 0);
  127. WREG32(mmIH_RB_WPTR, 0);
  128. /* Default settings for IH_CNTL (disabled at first) */
  129. ih_cntl = RREG32(mmIH_CNTL);
  130. ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL, MC_VMID, 0);
  131. if (adev->irq.msi_enabled)
  132. ih_cntl = REG_SET_FIELD(ih_cntl, IH_CNTL, RPTR_REARM, 1);
  133. WREG32(mmIH_CNTL, ih_cntl);
  134. pci_set_master(adev->pdev);
  135. /* enable interrupts */
  136. iceland_ih_enable_interrupts(adev);
  137. return 0;
  138. }
  139. /**
  140. * iceland_ih_irq_disable - disable interrupts
  141. *
  142. * @adev: amdgpu_device pointer
  143. *
  144. * Disable interrupts on the hw (VI).
  145. */
  146. static void iceland_ih_irq_disable(struct amdgpu_device *adev)
  147. {
  148. iceland_ih_disable_interrupts(adev);
  149. /* Wait and acknowledge irq */
  150. mdelay(1);
  151. }
  152. /**
  153. * iceland_ih_get_wptr - get the IH ring buffer wptr
  154. *
  155. * @adev: amdgpu_device pointer
  156. *
  157. * Get the IH ring buffer wptr from either the register
  158. * or the writeback memory buffer (VI). Also check for
  159. * ring buffer overflow and deal with it.
  160. * Used by cz_irq_process(VI).
  161. * Returns the value of the wptr.
  162. */
  163. static u32 iceland_ih_get_wptr(struct amdgpu_device *adev)
  164. {
  165. u32 wptr, tmp;
  166. wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
  167. if (REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW)) {
  168. wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
  169. /* When a ring buffer overflow happen start parsing interrupt
  170. * from the last not overwritten vector (wptr + 16). Hopefully
  171. * this should allow us to catchup.
  172. */
  173. dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
  174. wptr, adev->irq.ih.rptr, (wptr + 16) & adev->irq.ih.ptr_mask);
  175. adev->irq.ih.rptr = (wptr + 16) & adev->irq.ih.ptr_mask;
  176. tmp = RREG32(mmIH_RB_CNTL);
  177. tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
  178. WREG32(mmIH_RB_CNTL, tmp);
  179. }
  180. return (wptr & adev->irq.ih.ptr_mask);
  181. }
  182. /**
  183. * iceland_ih_prescreen_iv - prescreen an interrupt vector
  184. *
  185. * @adev: amdgpu_device pointer
  186. *
  187. * Returns true if the interrupt vector should be further processed.
  188. */
  189. static bool iceland_ih_prescreen_iv(struct amdgpu_device *adev)
  190. {
  191. u32 ring_index = adev->irq.ih.rptr >> 2;
  192. u16 pasid;
  193. switch (le32_to_cpu(adev->irq.ih.ring[ring_index]) & 0xff) {
  194. case 146:
  195. case 147:
  196. pasid = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]) >> 16;
  197. if (!pasid || amdgpu_vm_pasid_fault_credit(adev, pasid))
  198. return true;
  199. break;
  200. default:
  201. /* Not a VM fault */
  202. return true;
  203. }
  204. adev->irq.ih.rptr += 16;
  205. return false;
  206. }
  207. /**
  208. * iceland_ih_decode_iv - decode an interrupt vector
  209. *
  210. * @adev: amdgpu_device pointer
  211. *
  212. * Decodes the interrupt vector at the current rptr
  213. * position and also advance the position.
  214. */
  215. static void iceland_ih_decode_iv(struct amdgpu_device *adev,
  216. struct amdgpu_iv_entry *entry)
  217. {
  218. /* wptr/rptr are in bytes! */
  219. u32 ring_index = adev->irq.ih.rptr >> 2;
  220. uint32_t dw[4];
  221. dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
  222. dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
  223. dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
  224. dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
  225. entry->client_id = AMDGPU_IH_CLIENTID_LEGACY;
  226. entry->src_id = dw[0] & 0xff;
  227. entry->src_data[0] = dw[1] & 0xfffffff;
  228. entry->ring_id = dw[2] & 0xff;
  229. entry->vmid = (dw[2] >> 8) & 0xff;
  230. entry->pasid = (dw[2] >> 16) & 0xffff;
  231. /* wptr/rptr are in bytes! */
  232. adev->irq.ih.rptr += 16;
  233. }
  234. /**
  235. * iceland_ih_set_rptr - set the IH ring buffer rptr
  236. *
  237. * @adev: amdgpu_device pointer
  238. *
  239. * Set the IH ring buffer rptr.
  240. */
  241. static void iceland_ih_set_rptr(struct amdgpu_device *adev)
  242. {
  243. WREG32(mmIH_RB_RPTR, adev->irq.ih.rptr);
  244. }
  245. static int iceland_ih_early_init(void *handle)
  246. {
  247. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  248. int ret;
  249. ret = amdgpu_irq_add_domain(adev);
  250. if (ret)
  251. return ret;
  252. iceland_ih_set_interrupt_funcs(adev);
  253. return 0;
  254. }
  255. static int iceland_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, 64 * 1024, false);
  260. if (r)
  261. return r;
  262. r = amdgpu_irq_init(adev);
  263. return r;
  264. }
  265. static int iceland_ih_sw_fini(void *handle)
  266. {
  267. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  268. amdgpu_irq_fini(adev);
  269. amdgpu_ih_ring_fini(adev);
  270. amdgpu_irq_remove_domain(adev);
  271. return 0;
  272. }
  273. static int iceland_ih_hw_init(void *handle)
  274. {
  275. int r;
  276. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  277. r = iceland_ih_irq_init(adev);
  278. if (r)
  279. return r;
  280. return 0;
  281. }
  282. static int iceland_ih_hw_fini(void *handle)
  283. {
  284. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  285. iceland_ih_irq_disable(adev);
  286. return 0;
  287. }
  288. static int iceland_ih_suspend(void *handle)
  289. {
  290. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  291. return iceland_ih_hw_fini(adev);
  292. }
  293. static int iceland_ih_resume(void *handle)
  294. {
  295. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  296. return iceland_ih_hw_init(adev);
  297. }
  298. static bool iceland_ih_is_idle(void *handle)
  299. {
  300. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  301. u32 tmp = RREG32(mmSRBM_STATUS);
  302. if (REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
  303. return false;
  304. return true;
  305. }
  306. static int iceland_ih_wait_for_idle(void *handle)
  307. {
  308. unsigned i;
  309. u32 tmp;
  310. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  311. for (i = 0; i < adev->usec_timeout; i++) {
  312. /* read MC_STATUS */
  313. tmp = RREG32(mmSRBM_STATUS);
  314. if (!REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
  315. return 0;
  316. udelay(1);
  317. }
  318. return -ETIMEDOUT;
  319. }
  320. static int iceland_ih_soft_reset(void *handle)
  321. {
  322. u32 srbm_soft_reset = 0;
  323. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  324. u32 tmp = RREG32(mmSRBM_STATUS);
  325. if (tmp & SRBM_STATUS__IH_BUSY_MASK)
  326. srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET,
  327. SOFT_RESET_IH, 1);
  328. if (srbm_soft_reset) {
  329. tmp = RREG32(mmSRBM_SOFT_RESET);
  330. tmp |= srbm_soft_reset;
  331. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  332. WREG32(mmSRBM_SOFT_RESET, tmp);
  333. tmp = RREG32(mmSRBM_SOFT_RESET);
  334. udelay(50);
  335. tmp &= ~srbm_soft_reset;
  336. WREG32(mmSRBM_SOFT_RESET, tmp);
  337. tmp = RREG32(mmSRBM_SOFT_RESET);
  338. /* Wait a little for things to settle down */
  339. udelay(50);
  340. }
  341. return 0;
  342. }
  343. static int iceland_ih_set_clockgating_state(void *handle,
  344. enum amd_clockgating_state state)
  345. {
  346. return 0;
  347. }
  348. static int iceland_ih_set_powergating_state(void *handle,
  349. enum amd_powergating_state state)
  350. {
  351. return 0;
  352. }
  353. static const struct amd_ip_funcs iceland_ih_ip_funcs = {
  354. .name = "iceland_ih",
  355. .early_init = iceland_ih_early_init,
  356. .late_init = NULL,
  357. .sw_init = iceland_ih_sw_init,
  358. .sw_fini = iceland_ih_sw_fini,
  359. .hw_init = iceland_ih_hw_init,
  360. .hw_fini = iceland_ih_hw_fini,
  361. .suspend = iceland_ih_suspend,
  362. .resume = iceland_ih_resume,
  363. .is_idle = iceland_ih_is_idle,
  364. .wait_for_idle = iceland_ih_wait_for_idle,
  365. .soft_reset = iceland_ih_soft_reset,
  366. .set_clockgating_state = iceland_ih_set_clockgating_state,
  367. .set_powergating_state = iceland_ih_set_powergating_state,
  368. };
  369. static const struct amdgpu_ih_funcs iceland_ih_funcs = {
  370. .get_wptr = iceland_ih_get_wptr,
  371. .prescreen_iv = iceland_ih_prescreen_iv,
  372. .decode_iv = iceland_ih_decode_iv,
  373. .set_rptr = iceland_ih_set_rptr
  374. };
  375. static void iceland_ih_set_interrupt_funcs(struct amdgpu_device *adev)
  376. {
  377. if (adev->irq.ih_funcs == NULL)
  378. adev->irq.ih_funcs = &iceland_ih_funcs;
  379. }
  380. const struct amdgpu_ip_block_version iceland_ih_ip_block =
  381. {
  382. .type = AMD_IP_BLOCK_TYPE_IH,
  383. .major = 2,
  384. .minor = 4,
  385. .rev = 0,
  386. .funcs = &iceland_ih_ip_funcs,
  387. };