tonga_ih.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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_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 rb_bufsz;
  94. u32 interrupt_cntl, ih_rb_cntl, ih_doorbell_rtpr;
  95. u64 wptr_off;
  96. /* disable irqs */
  97. tonga_ih_disable_interrupts(adev);
  98. /* setup interrupt control */
  99. WREG32(mmINTERRUPT_CNTL2, adev->dummy_page.addr >> 8);
  100. interrupt_cntl = RREG32(mmINTERRUPT_CNTL);
  101. /* INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=0 - dummy read disabled with msi, enabled without msi
  102. * INTERRUPT_CNTL__IH_DUMMY_RD_OVERRIDE_MASK=1 - dummy read controlled by IH_DUMMY_RD_EN
  103. */
  104. interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_DUMMY_RD_OVERRIDE, 0);
  105. /* INTERRUPT_CNTL__IH_REQ_NONSNOOP_EN_MASK=1 if ring is in non-cacheable memory, e.g., vram */
  106. interrupt_cntl = REG_SET_FIELD(interrupt_cntl, INTERRUPT_CNTL, IH_REQ_NONSNOOP_EN, 0);
  107. WREG32(mmINTERRUPT_CNTL, interrupt_cntl);
  108. /* Ring Buffer base. [39:8] of 40-bit address of the beginning of the ring buffer*/
  109. if (adev->irq.ih.use_bus_addr)
  110. WREG32(mmIH_RB_BASE, adev->irq.ih.rb_dma_addr >> 8);
  111. else
  112. WREG32(mmIH_RB_BASE, adev->irq.ih.gpu_addr >> 8);
  113. rb_bufsz = order_base_2(adev->irq.ih.ring_size / 4);
  114. ih_rb_cntl = REG_SET_FIELD(0, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
  115. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RB_SIZE, rb_bufsz);
  116. /* Ring Buffer write pointer writeback. If enabled, IH_RB_WPTR register value is written to memory */
  117. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, WPTR_WRITEBACK_ENABLE, 1);
  118. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, MC_VMID, 0);
  119. if (adev->irq.msi_enabled)
  120. ih_rb_cntl = REG_SET_FIELD(ih_rb_cntl, IH_RB_CNTL, RPTR_REARM, 1);
  121. WREG32(mmIH_RB_CNTL, ih_rb_cntl);
  122. /* set the writeback address whether it's enabled or not */
  123. if (adev->irq.ih.use_bus_addr)
  124. wptr_off = adev->irq.ih.rb_dma_addr + (adev->irq.ih.wptr_offs * 4);
  125. else
  126. wptr_off = adev->wb.gpu_addr + (adev->irq.ih.wptr_offs * 4);
  127. WREG32(mmIH_RB_WPTR_ADDR_LO, lower_32_bits(wptr_off));
  128. WREG32(mmIH_RB_WPTR_ADDR_HI, upper_32_bits(wptr_off) & 0xFF);
  129. /* set rptr, wptr to 0 */
  130. WREG32(mmIH_RB_RPTR, 0);
  131. WREG32(mmIH_RB_WPTR, 0);
  132. ih_doorbell_rtpr = RREG32(mmIH_DOORBELL_RPTR);
  133. if (adev->irq.ih.use_doorbell) {
  134. ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
  135. OFFSET, adev->irq.ih.doorbell_index);
  136. ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
  137. ENABLE, 1);
  138. } else {
  139. ih_doorbell_rtpr = REG_SET_FIELD(ih_doorbell_rtpr, IH_DOORBELL_RPTR,
  140. ENABLE, 0);
  141. }
  142. WREG32(mmIH_DOORBELL_RPTR, ih_doorbell_rtpr);
  143. pci_set_master(adev->pdev);
  144. /* enable interrupts */
  145. tonga_ih_enable_interrupts(adev);
  146. return 0;
  147. }
  148. /**
  149. * tonga_ih_irq_disable - disable interrupts
  150. *
  151. * @adev: amdgpu_device pointer
  152. *
  153. * Disable interrupts on the hw (VI).
  154. */
  155. static void tonga_ih_irq_disable(struct amdgpu_device *adev)
  156. {
  157. tonga_ih_disable_interrupts(adev);
  158. /* Wait and acknowledge irq */
  159. mdelay(1);
  160. }
  161. /**
  162. * tonga_ih_get_wptr - get the IH ring buffer wptr
  163. *
  164. * @adev: amdgpu_device pointer
  165. *
  166. * Get the IH ring buffer wptr from either the register
  167. * or the writeback memory buffer (VI). Also check for
  168. * ring buffer overflow and deal with it.
  169. * Used by cz_irq_process(VI).
  170. * Returns the value of the wptr.
  171. */
  172. static u32 tonga_ih_get_wptr(struct amdgpu_device *adev)
  173. {
  174. u32 wptr, tmp;
  175. if (adev->irq.ih.use_bus_addr)
  176. wptr = le32_to_cpu(adev->irq.ih.ring[adev->irq.ih.wptr_offs]);
  177. else
  178. wptr = le32_to_cpu(adev->wb.wb[adev->irq.ih.wptr_offs]);
  179. if (REG_GET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW)) {
  180. wptr = REG_SET_FIELD(wptr, IH_RB_WPTR, RB_OVERFLOW, 0);
  181. /* When a ring buffer overflow happen start parsing interrupt
  182. * from the last not overwritten vector (wptr + 16). Hopefully
  183. * this should allow us to catchup.
  184. */
  185. dev_warn(adev->dev, "IH ring buffer overflow (0x%08X, 0x%08X, 0x%08X)\n",
  186. wptr, adev->irq.ih.rptr, (wptr + 16) & adev->irq.ih.ptr_mask);
  187. adev->irq.ih.rptr = (wptr + 16) & adev->irq.ih.ptr_mask;
  188. tmp = RREG32(mmIH_RB_CNTL);
  189. tmp = REG_SET_FIELD(tmp, IH_RB_CNTL, WPTR_OVERFLOW_CLEAR, 1);
  190. WREG32(mmIH_RB_CNTL, tmp);
  191. }
  192. return (wptr & adev->irq.ih.ptr_mask);
  193. }
  194. /**
  195. * tonga_ih_prescreen_iv - prescreen an interrupt vector
  196. *
  197. * @adev: amdgpu_device pointer
  198. *
  199. * Returns true if the interrupt vector should be further processed.
  200. */
  201. static bool tonga_ih_prescreen_iv(struct amdgpu_device *adev)
  202. {
  203. /* Process all interrupts */
  204. return true;
  205. }
  206. /**
  207. * tonga_ih_decode_iv - decode an interrupt vector
  208. *
  209. * @adev: amdgpu_device pointer
  210. *
  211. * Decodes the interrupt vector at the current rptr
  212. * position and also advance the position.
  213. */
  214. static void tonga_ih_decode_iv(struct amdgpu_device *adev,
  215. struct amdgpu_iv_entry *entry)
  216. {
  217. /* wptr/rptr are in bytes! */
  218. u32 ring_index = adev->irq.ih.rptr >> 2;
  219. uint32_t dw[4];
  220. dw[0] = le32_to_cpu(adev->irq.ih.ring[ring_index + 0]);
  221. dw[1] = le32_to_cpu(adev->irq.ih.ring[ring_index + 1]);
  222. dw[2] = le32_to_cpu(adev->irq.ih.ring[ring_index + 2]);
  223. dw[3] = le32_to_cpu(adev->irq.ih.ring[ring_index + 3]);
  224. entry->client_id = AMDGPU_IH_CLIENTID_LEGACY;
  225. entry->src_id = dw[0] & 0xff;
  226. entry->src_data[0] = dw[1] & 0xfffffff;
  227. entry->ring_id = dw[2] & 0xff;
  228. entry->vm_id = (dw[2] >> 8) & 0xff;
  229. entry->pas_id = (dw[2] >> 16) & 0xffff;
  230. /* wptr/rptr are in bytes! */
  231. adev->irq.ih.rptr += 16;
  232. }
  233. /**
  234. * tonga_ih_set_rptr - set the IH ring buffer rptr
  235. *
  236. * @adev: amdgpu_device pointer
  237. *
  238. * Set the IH ring buffer rptr.
  239. */
  240. static void tonga_ih_set_rptr(struct amdgpu_device *adev)
  241. {
  242. if (adev->irq.ih.use_doorbell) {
  243. /* XXX check if swapping is necessary on BE */
  244. if (adev->irq.ih.use_bus_addr)
  245. adev->irq.ih.ring[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
  246. else
  247. adev->wb.wb[adev->irq.ih.rptr_offs] = adev->irq.ih.rptr;
  248. WDOORBELL32(adev->irq.ih.doorbell_index, adev->irq.ih.rptr);
  249. } else {
  250. WREG32(mmIH_RB_RPTR, adev->irq.ih.rptr);
  251. }
  252. }
  253. static int tonga_ih_early_init(void *handle)
  254. {
  255. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  256. int ret;
  257. ret = amdgpu_irq_add_domain(adev);
  258. if (ret)
  259. return ret;
  260. tonga_ih_set_interrupt_funcs(adev);
  261. return 0;
  262. }
  263. static int tonga_ih_sw_init(void *handle)
  264. {
  265. int r;
  266. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  267. r = amdgpu_ih_ring_init(adev, 64 * 1024, true);
  268. if (r)
  269. return r;
  270. adev->irq.ih.use_doorbell = true;
  271. adev->irq.ih.doorbell_index = AMDGPU_DOORBELL_IH;
  272. r = amdgpu_irq_init(adev);
  273. return r;
  274. }
  275. static int tonga_ih_sw_fini(void *handle)
  276. {
  277. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  278. amdgpu_irq_fini(adev);
  279. amdgpu_ih_ring_fini(adev);
  280. amdgpu_irq_remove_domain(adev);
  281. return 0;
  282. }
  283. static int tonga_ih_hw_init(void *handle)
  284. {
  285. int r;
  286. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  287. r = tonga_ih_irq_init(adev);
  288. if (r)
  289. return r;
  290. return 0;
  291. }
  292. static int tonga_ih_hw_fini(void *handle)
  293. {
  294. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  295. tonga_ih_irq_disable(adev);
  296. return 0;
  297. }
  298. static int tonga_ih_suspend(void *handle)
  299. {
  300. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  301. return tonga_ih_hw_fini(adev);
  302. }
  303. static int tonga_ih_resume(void *handle)
  304. {
  305. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  306. return tonga_ih_hw_init(adev);
  307. }
  308. static bool tonga_ih_is_idle(void *handle)
  309. {
  310. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  311. u32 tmp = RREG32(mmSRBM_STATUS);
  312. if (REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
  313. return false;
  314. return true;
  315. }
  316. static int tonga_ih_wait_for_idle(void *handle)
  317. {
  318. unsigned i;
  319. u32 tmp;
  320. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  321. for (i = 0; i < adev->usec_timeout; i++) {
  322. /* read MC_STATUS */
  323. tmp = RREG32(mmSRBM_STATUS);
  324. if (!REG_GET_FIELD(tmp, SRBM_STATUS, IH_BUSY))
  325. return 0;
  326. udelay(1);
  327. }
  328. return -ETIMEDOUT;
  329. }
  330. static bool tonga_ih_check_soft_reset(void *handle)
  331. {
  332. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  333. u32 srbm_soft_reset = 0;
  334. u32 tmp = RREG32(mmSRBM_STATUS);
  335. if (tmp & SRBM_STATUS__IH_BUSY_MASK)
  336. srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET,
  337. SOFT_RESET_IH, 1);
  338. if (srbm_soft_reset) {
  339. adev->irq.srbm_soft_reset = srbm_soft_reset;
  340. return true;
  341. } else {
  342. adev->irq.srbm_soft_reset = 0;
  343. return false;
  344. }
  345. }
  346. static int tonga_ih_pre_soft_reset(void *handle)
  347. {
  348. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  349. if (!adev->irq.srbm_soft_reset)
  350. return 0;
  351. return tonga_ih_hw_fini(adev);
  352. }
  353. static int tonga_ih_post_soft_reset(void *handle)
  354. {
  355. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  356. if (!adev->irq.srbm_soft_reset)
  357. return 0;
  358. return tonga_ih_hw_init(adev);
  359. }
  360. static int tonga_ih_soft_reset(void *handle)
  361. {
  362. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  363. u32 srbm_soft_reset;
  364. if (!adev->irq.srbm_soft_reset)
  365. return 0;
  366. srbm_soft_reset = adev->irq.srbm_soft_reset;
  367. if (srbm_soft_reset) {
  368. u32 tmp;
  369. tmp = RREG32(mmSRBM_SOFT_RESET);
  370. tmp |= srbm_soft_reset;
  371. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  372. WREG32(mmSRBM_SOFT_RESET, tmp);
  373. tmp = RREG32(mmSRBM_SOFT_RESET);
  374. udelay(50);
  375. tmp &= ~srbm_soft_reset;
  376. WREG32(mmSRBM_SOFT_RESET, tmp);
  377. tmp = RREG32(mmSRBM_SOFT_RESET);
  378. /* Wait a little for things to settle down */
  379. udelay(50);
  380. }
  381. return 0;
  382. }
  383. static int tonga_ih_set_clockgating_state(void *handle,
  384. enum amd_clockgating_state state)
  385. {
  386. return 0;
  387. }
  388. static int tonga_ih_set_powergating_state(void *handle,
  389. enum amd_powergating_state state)
  390. {
  391. return 0;
  392. }
  393. static const struct amd_ip_funcs tonga_ih_ip_funcs = {
  394. .name = "tonga_ih",
  395. .early_init = tonga_ih_early_init,
  396. .late_init = NULL,
  397. .sw_init = tonga_ih_sw_init,
  398. .sw_fini = tonga_ih_sw_fini,
  399. .hw_init = tonga_ih_hw_init,
  400. .hw_fini = tonga_ih_hw_fini,
  401. .suspend = tonga_ih_suspend,
  402. .resume = tonga_ih_resume,
  403. .is_idle = tonga_ih_is_idle,
  404. .wait_for_idle = tonga_ih_wait_for_idle,
  405. .check_soft_reset = tonga_ih_check_soft_reset,
  406. .pre_soft_reset = tonga_ih_pre_soft_reset,
  407. .soft_reset = tonga_ih_soft_reset,
  408. .post_soft_reset = tonga_ih_post_soft_reset,
  409. .set_clockgating_state = tonga_ih_set_clockgating_state,
  410. .set_powergating_state = tonga_ih_set_powergating_state,
  411. };
  412. static const struct amdgpu_ih_funcs tonga_ih_funcs = {
  413. .get_wptr = tonga_ih_get_wptr,
  414. .prescreen_iv = tonga_ih_prescreen_iv,
  415. .decode_iv = tonga_ih_decode_iv,
  416. .set_rptr = tonga_ih_set_rptr
  417. };
  418. static void tonga_ih_set_interrupt_funcs(struct amdgpu_device *adev)
  419. {
  420. if (adev->irq.ih_funcs == NULL)
  421. adev->irq.ih_funcs = &tonga_ih_funcs;
  422. }
  423. const struct amdgpu_ip_block_version tonga_ih_ip_block =
  424. {
  425. .type = AMD_IP_BLOCK_TYPE_IH,
  426. .major = 3,
  427. .minor = 0,
  428. .rev = 0,
  429. .funcs = &tonga_ih_ip_funcs,
  430. };