cik_sdma.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. * Copyright 2013 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. * Authors: Alex Deucher
  23. */
  24. #include <linux/firmware.h>
  25. #include <drm/drmP.h>
  26. #include "radeon.h"
  27. #include "radeon_ucode.h"
  28. #include "radeon_asic.h"
  29. #include "radeon_trace.h"
  30. #include "cikd.h"
  31. /* sdma */
  32. #define CIK_SDMA_UCODE_SIZE 1050
  33. #define CIK_SDMA_UCODE_VERSION 64
  34. u32 cik_gpu_check_soft_reset(struct radeon_device *rdev);
  35. /*
  36. * sDMA - System DMA
  37. * Starting with CIK, the GPU has new asynchronous
  38. * DMA engines. These engines are used for compute
  39. * and gfx. There are two DMA engines (SDMA0, SDMA1)
  40. * and each one supports 1 ring buffer used for gfx
  41. * and 2 queues used for compute.
  42. *
  43. * The programming model is very similar to the CP
  44. * (ring buffer, IBs, etc.), but sDMA has it's own
  45. * packet format that is different from the PM4 format
  46. * used by the CP. sDMA supports copying data, writing
  47. * embedded data, solid fills, and a number of other
  48. * things. It also has support for tiling/detiling of
  49. * buffers.
  50. */
  51. /**
  52. * cik_sdma_get_rptr - get the current read pointer
  53. *
  54. * @rdev: radeon_device pointer
  55. * @ring: radeon ring pointer
  56. *
  57. * Get the current rptr from the hardware (CIK+).
  58. */
  59. uint32_t cik_sdma_get_rptr(struct radeon_device *rdev,
  60. struct radeon_ring *ring)
  61. {
  62. u32 rptr, reg;
  63. if (rdev->wb.enabled) {
  64. rptr = rdev->wb.wb[ring->rptr_offs/4];
  65. } else {
  66. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  67. reg = SDMA0_GFX_RB_RPTR + SDMA0_REGISTER_OFFSET;
  68. else
  69. reg = SDMA0_GFX_RB_RPTR + SDMA1_REGISTER_OFFSET;
  70. rptr = RREG32(reg);
  71. }
  72. return (rptr & 0x3fffc) >> 2;
  73. }
  74. /**
  75. * cik_sdma_get_wptr - get the current write pointer
  76. *
  77. * @rdev: radeon_device pointer
  78. * @ring: radeon ring pointer
  79. *
  80. * Get the current wptr from the hardware (CIK+).
  81. */
  82. uint32_t cik_sdma_get_wptr(struct radeon_device *rdev,
  83. struct radeon_ring *ring)
  84. {
  85. u32 reg;
  86. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  87. reg = SDMA0_GFX_RB_WPTR + SDMA0_REGISTER_OFFSET;
  88. else
  89. reg = SDMA0_GFX_RB_WPTR + SDMA1_REGISTER_OFFSET;
  90. return (RREG32(reg) & 0x3fffc) >> 2;
  91. }
  92. /**
  93. * cik_sdma_set_wptr - commit the write pointer
  94. *
  95. * @rdev: radeon_device pointer
  96. * @ring: radeon ring pointer
  97. *
  98. * Write the wptr back to the hardware (CIK+).
  99. */
  100. void cik_sdma_set_wptr(struct radeon_device *rdev,
  101. struct radeon_ring *ring)
  102. {
  103. u32 reg;
  104. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  105. reg = SDMA0_GFX_RB_WPTR + SDMA0_REGISTER_OFFSET;
  106. else
  107. reg = SDMA0_GFX_RB_WPTR + SDMA1_REGISTER_OFFSET;
  108. WREG32(reg, (ring->wptr << 2) & 0x3fffc);
  109. (void)RREG32(reg);
  110. }
  111. /**
  112. * cik_sdma_ring_ib_execute - Schedule an IB on the DMA engine
  113. *
  114. * @rdev: radeon_device pointer
  115. * @ib: IB object to schedule
  116. *
  117. * Schedule an IB in the DMA ring (CIK).
  118. */
  119. void cik_sdma_ring_ib_execute(struct radeon_device *rdev,
  120. struct radeon_ib *ib)
  121. {
  122. struct radeon_ring *ring = &rdev->ring[ib->ring];
  123. u32 extra_bits = (ib->vm ? ib->vm->ids[ib->ring].id : 0) & 0xf;
  124. if (rdev->wb.enabled) {
  125. u32 next_rptr = ring->wptr + 5;
  126. while ((next_rptr & 7) != 4)
  127. next_rptr++;
  128. next_rptr += 4;
  129. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0));
  130. radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
  131. radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr));
  132. radeon_ring_write(ring, 1); /* number of DWs to follow */
  133. radeon_ring_write(ring, next_rptr);
  134. }
  135. /* IB packet must end on a 8 DW boundary */
  136. while ((ring->wptr & 7) != 4)
  137. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0));
  138. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_INDIRECT_BUFFER, 0, extra_bits));
  139. radeon_ring_write(ring, ib->gpu_addr & 0xffffffe0); /* base must be 32 byte aligned */
  140. radeon_ring_write(ring, upper_32_bits(ib->gpu_addr));
  141. radeon_ring_write(ring, ib->length_dw);
  142. }
  143. /**
  144. * cik_sdma_hdp_flush_ring_emit - emit an hdp flush on the DMA ring
  145. *
  146. * @rdev: radeon_device pointer
  147. * @ridx: radeon ring index
  148. *
  149. * Emit an hdp flush packet on the requested DMA ring.
  150. */
  151. static void cik_sdma_hdp_flush_ring_emit(struct radeon_device *rdev,
  152. int ridx)
  153. {
  154. struct radeon_ring *ring = &rdev->ring[ridx];
  155. u32 extra_bits = (SDMA_POLL_REG_MEM_EXTRA_OP(1) |
  156. SDMA_POLL_REG_MEM_EXTRA_FUNC(3)); /* == */
  157. u32 ref_and_mask;
  158. if (ridx == R600_RING_TYPE_DMA_INDEX)
  159. ref_and_mask = SDMA0;
  160. else
  161. ref_and_mask = SDMA1;
  162. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0, extra_bits));
  163. radeon_ring_write(ring, GPU_HDP_FLUSH_DONE);
  164. radeon_ring_write(ring, GPU_HDP_FLUSH_REQ);
  165. radeon_ring_write(ring, ref_and_mask); /* reference */
  166. radeon_ring_write(ring, ref_and_mask); /* mask */
  167. radeon_ring_write(ring, (0xfff << 16) | 10); /* retry count, poll interval */
  168. }
  169. /**
  170. * cik_sdma_fence_ring_emit - emit a fence on the DMA ring
  171. *
  172. * @rdev: radeon_device pointer
  173. * @fence: radeon fence object
  174. *
  175. * Add a DMA fence packet to the ring to write
  176. * the fence seq number and DMA trap packet to generate
  177. * an interrupt if needed (CIK).
  178. */
  179. void cik_sdma_fence_ring_emit(struct radeon_device *rdev,
  180. struct radeon_fence *fence)
  181. {
  182. struct radeon_ring *ring = &rdev->ring[fence->ring];
  183. u64 addr = rdev->fence_drv[fence->ring].gpu_addr;
  184. /* write the fence */
  185. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_FENCE, 0, 0));
  186. radeon_ring_write(ring, lower_32_bits(addr));
  187. radeon_ring_write(ring, upper_32_bits(addr));
  188. radeon_ring_write(ring, fence->seq);
  189. /* generate an interrupt */
  190. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_TRAP, 0, 0));
  191. /* flush HDP */
  192. cik_sdma_hdp_flush_ring_emit(rdev, fence->ring);
  193. }
  194. /**
  195. * cik_sdma_semaphore_ring_emit - emit a semaphore on the dma ring
  196. *
  197. * @rdev: radeon_device pointer
  198. * @ring: radeon_ring structure holding ring information
  199. * @semaphore: radeon semaphore object
  200. * @emit_wait: wait or signal semaphore
  201. *
  202. * Add a DMA semaphore packet to the ring wait on or signal
  203. * other rings (CIK).
  204. */
  205. bool cik_sdma_semaphore_ring_emit(struct radeon_device *rdev,
  206. struct radeon_ring *ring,
  207. struct radeon_semaphore *semaphore,
  208. bool emit_wait)
  209. {
  210. u64 addr = semaphore->gpu_addr;
  211. u32 extra_bits = emit_wait ? 0 : SDMA_SEMAPHORE_EXTRA_S;
  212. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SEMAPHORE, 0, extra_bits));
  213. radeon_ring_write(ring, addr & 0xfffffff8);
  214. radeon_ring_write(ring, upper_32_bits(addr));
  215. return true;
  216. }
  217. /**
  218. * cik_sdma_gfx_stop - stop the gfx async dma engines
  219. *
  220. * @rdev: radeon_device pointer
  221. *
  222. * Stop the gfx async dma ring buffers (CIK).
  223. */
  224. static void cik_sdma_gfx_stop(struct radeon_device *rdev)
  225. {
  226. u32 rb_cntl, reg_offset;
  227. int i;
  228. if ((rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX) ||
  229. (rdev->asic->copy.copy_ring_index == CAYMAN_RING_TYPE_DMA1_INDEX))
  230. radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
  231. for (i = 0; i < 2; i++) {
  232. if (i == 0)
  233. reg_offset = SDMA0_REGISTER_OFFSET;
  234. else
  235. reg_offset = SDMA1_REGISTER_OFFSET;
  236. rb_cntl = RREG32(SDMA0_GFX_RB_CNTL + reg_offset);
  237. rb_cntl &= ~SDMA_RB_ENABLE;
  238. WREG32(SDMA0_GFX_RB_CNTL + reg_offset, rb_cntl);
  239. WREG32(SDMA0_GFX_IB_CNTL + reg_offset, 0);
  240. }
  241. rdev->ring[R600_RING_TYPE_DMA_INDEX].ready = false;
  242. rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready = false;
  243. /* FIXME use something else than big hammer but after few days can not
  244. * seem to find good combination so reset SDMA blocks as it seems we
  245. * do not shut them down properly. This fix hibernation and does not
  246. * affect suspend to ram.
  247. */
  248. WREG32(SRBM_SOFT_RESET, SOFT_RESET_SDMA | SOFT_RESET_SDMA1);
  249. (void)RREG32(SRBM_SOFT_RESET);
  250. udelay(50);
  251. WREG32(SRBM_SOFT_RESET, 0);
  252. (void)RREG32(SRBM_SOFT_RESET);
  253. }
  254. /**
  255. * cik_sdma_rlc_stop - stop the compute async dma engines
  256. *
  257. * @rdev: radeon_device pointer
  258. *
  259. * Stop the compute async dma queues (CIK).
  260. */
  261. static void cik_sdma_rlc_stop(struct radeon_device *rdev)
  262. {
  263. /* XXX todo */
  264. }
  265. /**
  266. * cik_sdma_ctx_switch_enable - enable/disable sdma engine preemption
  267. *
  268. * @rdev: radeon_device pointer
  269. * @enable: enable/disable preemption.
  270. *
  271. * Halt or unhalt the async dma engines (CIK).
  272. */
  273. static void cik_sdma_ctx_switch_enable(struct radeon_device *rdev, bool enable)
  274. {
  275. uint32_t reg_offset, value;
  276. int i;
  277. for (i = 0; i < 2; i++) {
  278. if (i == 0)
  279. reg_offset = SDMA0_REGISTER_OFFSET;
  280. else
  281. reg_offset = SDMA1_REGISTER_OFFSET;
  282. value = RREG32(SDMA0_CNTL + reg_offset);
  283. if (enable)
  284. value |= AUTO_CTXSW_ENABLE;
  285. else
  286. value &= ~AUTO_CTXSW_ENABLE;
  287. WREG32(SDMA0_CNTL + reg_offset, value);
  288. }
  289. }
  290. /**
  291. * cik_sdma_enable - stop the async dma engines
  292. *
  293. * @rdev: radeon_device pointer
  294. * @enable: enable/disable the DMA MEs.
  295. *
  296. * Halt or unhalt the async dma engines (CIK).
  297. */
  298. void cik_sdma_enable(struct radeon_device *rdev, bool enable)
  299. {
  300. u32 me_cntl, reg_offset;
  301. int i;
  302. if (enable == false) {
  303. cik_sdma_gfx_stop(rdev);
  304. cik_sdma_rlc_stop(rdev);
  305. }
  306. for (i = 0; i < 2; i++) {
  307. if (i == 0)
  308. reg_offset = SDMA0_REGISTER_OFFSET;
  309. else
  310. reg_offset = SDMA1_REGISTER_OFFSET;
  311. me_cntl = RREG32(SDMA0_ME_CNTL + reg_offset);
  312. if (enable)
  313. me_cntl &= ~SDMA_HALT;
  314. else
  315. me_cntl |= SDMA_HALT;
  316. WREG32(SDMA0_ME_CNTL + reg_offset, me_cntl);
  317. }
  318. cik_sdma_ctx_switch_enable(rdev, enable);
  319. }
  320. /**
  321. * cik_sdma_gfx_resume - setup and start the async dma engines
  322. *
  323. * @rdev: radeon_device pointer
  324. *
  325. * Set up the gfx DMA ring buffers and enable them (CIK).
  326. * Returns 0 for success, error for failure.
  327. */
  328. static int cik_sdma_gfx_resume(struct radeon_device *rdev)
  329. {
  330. struct radeon_ring *ring;
  331. u32 rb_cntl, ib_cntl;
  332. u32 rb_bufsz;
  333. u32 reg_offset, wb_offset;
  334. int i, r;
  335. for (i = 0; i < 2; i++) {
  336. if (i == 0) {
  337. ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
  338. reg_offset = SDMA0_REGISTER_OFFSET;
  339. wb_offset = R600_WB_DMA_RPTR_OFFSET;
  340. } else {
  341. ring = &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX];
  342. reg_offset = SDMA1_REGISTER_OFFSET;
  343. wb_offset = CAYMAN_WB_DMA1_RPTR_OFFSET;
  344. }
  345. WREG32(SDMA0_SEM_INCOMPLETE_TIMER_CNTL + reg_offset, 0);
  346. WREG32(SDMA0_SEM_WAIT_FAIL_TIMER_CNTL + reg_offset, 0);
  347. /* Set ring buffer size in dwords */
  348. rb_bufsz = order_base_2(ring->ring_size / 4);
  349. rb_cntl = rb_bufsz << 1;
  350. #ifdef __BIG_ENDIAN
  351. rb_cntl |= SDMA_RB_SWAP_ENABLE | SDMA_RPTR_WRITEBACK_SWAP_ENABLE;
  352. #endif
  353. WREG32(SDMA0_GFX_RB_CNTL + reg_offset, rb_cntl);
  354. /* Initialize the ring buffer's read and write pointers */
  355. WREG32(SDMA0_GFX_RB_RPTR + reg_offset, 0);
  356. WREG32(SDMA0_GFX_RB_WPTR + reg_offset, 0);
  357. /* set the wb address whether it's enabled or not */
  358. WREG32(SDMA0_GFX_RB_RPTR_ADDR_HI + reg_offset,
  359. upper_32_bits(rdev->wb.gpu_addr + wb_offset) & 0xFFFFFFFF);
  360. WREG32(SDMA0_GFX_RB_RPTR_ADDR_LO + reg_offset,
  361. ((rdev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC));
  362. if (rdev->wb.enabled)
  363. rb_cntl |= SDMA_RPTR_WRITEBACK_ENABLE;
  364. WREG32(SDMA0_GFX_RB_BASE + reg_offset, ring->gpu_addr >> 8);
  365. WREG32(SDMA0_GFX_RB_BASE_HI + reg_offset, ring->gpu_addr >> 40);
  366. ring->wptr = 0;
  367. WREG32(SDMA0_GFX_RB_WPTR + reg_offset, ring->wptr << 2);
  368. /* enable DMA RB */
  369. WREG32(SDMA0_GFX_RB_CNTL + reg_offset, rb_cntl | SDMA_RB_ENABLE);
  370. ib_cntl = SDMA_IB_ENABLE;
  371. #ifdef __BIG_ENDIAN
  372. ib_cntl |= SDMA_IB_SWAP_ENABLE;
  373. #endif
  374. /* enable DMA IBs */
  375. WREG32(SDMA0_GFX_IB_CNTL + reg_offset, ib_cntl);
  376. ring->ready = true;
  377. r = radeon_ring_test(rdev, ring->idx, ring);
  378. if (r) {
  379. ring->ready = false;
  380. return r;
  381. }
  382. }
  383. if ((rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX) ||
  384. (rdev->asic->copy.copy_ring_index == CAYMAN_RING_TYPE_DMA1_INDEX))
  385. radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
  386. return 0;
  387. }
  388. /**
  389. * cik_sdma_rlc_resume - setup and start the async dma engines
  390. *
  391. * @rdev: radeon_device pointer
  392. *
  393. * Set up the compute DMA queues and enable them (CIK).
  394. * Returns 0 for success, error for failure.
  395. */
  396. static int cik_sdma_rlc_resume(struct radeon_device *rdev)
  397. {
  398. /* XXX todo */
  399. return 0;
  400. }
  401. /**
  402. * cik_sdma_load_microcode - load the sDMA ME ucode
  403. *
  404. * @rdev: radeon_device pointer
  405. *
  406. * Loads the sDMA0/1 ucode.
  407. * Returns 0 for success, -EINVAL if the ucode is not available.
  408. */
  409. static int cik_sdma_load_microcode(struct radeon_device *rdev)
  410. {
  411. int i;
  412. if (!rdev->sdma_fw)
  413. return -EINVAL;
  414. /* halt the MEs */
  415. cik_sdma_enable(rdev, false);
  416. if (rdev->new_fw) {
  417. const struct sdma_firmware_header_v1_0 *hdr =
  418. (const struct sdma_firmware_header_v1_0 *)rdev->sdma_fw->data;
  419. const __le32 *fw_data;
  420. u32 fw_size;
  421. radeon_ucode_print_sdma_hdr(&hdr->header);
  422. /* sdma0 */
  423. fw_data = (const __le32 *)
  424. (rdev->sdma_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  425. fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
  426. WREG32(SDMA0_UCODE_ADDR + SDMA0_REGISTER_OFFSET, 0);
  427. for (i = 0; i < fw_size; i++)
  428. WREG32(SDMA0_UCODE_DATA + SDMA0_REGISTER_OFFSET, le32_to_cpup(fw_data++));
  429. WREG32(SDMA0_UCODE_DATA + SDMA0_REGISTER_OFFSET, CIK_SDMA_UCODE_VERSION);
  430. /* sdma1 */
  431. fw_data = (const __le32 *)
  432. (rdev->sdma_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  433. fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
  434. WREG32(SDMA0_UCODE_ADDR + SDMA1_REGISTER_OFFSET, 0);
  435. for (i = 0; i < fw_size; i++)
  436. WREG32(SDMA0_UCODE_DATA + SDMA1_REGISTER_OFFSET, le32_to_cpup(fw_data++));
  437. WREG32(SDMA0_UCODE_DATA + SDMA1_REGISTER_OFFSET, CIK_SDMA_UCODE_VERSION);
  438. } else {
  439. const __be32 *fw_data;
  440. /* sdma0 */
  441. fw_data = (const __be32 *)rdev->sdma_fw->data;
  442. WREG32(SDMA0_UCODE_ADDR + SDMA0_REGISTER_OFFSET, 0);
  443. for (i = 0; i < CIK_SDMA_UCODE_SIZE; i++)
  444. WREG32(SDMA0_UCODE_DATA + SDMA0_REGISTER_OFFSET, be32_to_cpup(fw_data++));
  445. WREG32(SDMA0_UCODE_DATA + SDMA0_REGISTER_OFFSET, CIK_SDMA_UCODE_VERSION);
  446. /* sdma1 */
  447. fw_data = (const __be32 *)rdev->sdma_fw->data;
  448. WREG32(SDMA0_UCODE_ADDR + SDMA1_REGISTER_OFFSET, 0);
  449. for (i = 0; i < CIK_SDMA_UCODE_SIZE; i++)
  450. WREG32(SDMA0_UCODE_DATA + SDMA1_REGISTER_OFFSET, be32_to_cpup(fw_data++));
  451. WREG32(SDMA0_UCODE_DATA + SDMA1_REGISTER_OFFSET, CIK_SDMA_UCODE_VERSION);
  452. }
  453. WREG32(SDMA0_UCODE_ADDR + SDMA0_REGISTER_OFFSET, 0);
  454. WREG32(SDMA0_UCODE_ADDR + SDMA1_REGISTER_OFFSET, 0);
  455. return 0;
  456. }
  457. /**
  458. * cik_sdma_resume - setup and start the async dma engines
  459. *
  460. * @rdev: radeon_device pointer
  461. *
  462. * Set up the DMA engines and enable them (CIK).
  463. * Returns 0 for success, error for failure.
  464. */
  465. int cik_sdma_resume(struct radeon_device *rdev)
  466. {
  467. int r;
  468. r = cik_sdma_load_microcode(rdev);
  469. if (r)
  470. return r;
  471. /* unhalt the MEs */
  472. cik_sdma_enable(rdev, true);
  473. /* start the gfx rings and rlc compute queues */
  474. r = cik_sdma_gfx_resume(rdev);
  475. if (r)
  476. return r;
  477. r = cik_sdma_rlc_resume(rdev);
  478. if (r)
  479. return r;
  480. return 0;
  481. }
  482. /**
  483. * cik_sdma_fini - tear down the async dma engines
  484. *
  485. * @rdev: radeon_device pointer
  486. *
  487. * Stop the async dma engines and free the rings (CIK).
  488. */
  489. void cik_sdma_fini(struct radeon_device *rdev)
  490. {
  491. /* halt the MEs */
  492. cik_sdma_enable(rdev, false);
  493. radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_DMA_INDEX]);
  494. radeon_ring_fini(rdev, &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX]);
  495. /* XXX - compute dma queue tear down */
  496. }
  497. /**
  498. * cik_copy_dma - copy pages using the DMA engine
  499. *
  500. * @rdev: radeon_device pointer
  501. * @src_offset: src GPU address
  502. * @dst_offset: dst GPU address
  503. * @num_gpu_pages: number of GPU pages to xfer
  504. * @resv: reservation object to sync to
  505. *
  506. * Copy GPU paging using the DMA engine (CIK).
  507. * Used by the radeon ttm implementation to move pages if
  508. * registered as the asic copy callback.
  509. */
  510. struct radeon_fence *cik_copy_dma(struct radeon_device *rdev,
  511. uint64_t src_offset, uint64_t dst_offset,
  512. unsigned num_gpu_pages,
  513. struct reservation_object *resv)
  514. {
  515. struct radeon_fence *fence;
  516. struct radeon_sync sync;
  517. int ring_index = rdev->asic->copy.dma_ring_index;
  518. struct radeon_ring *ring = &rdev->ring[ring_index];
  519. u32 size_in_bytes, cur_size_in_bytes;
  520. int i, num_loops;
  521. int r = 0;
  522. radeon_sync_create(&sync);
  523. size_in_bytes = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT);
  524. num_loops = DIV_ROUND_UP(size_in_bytes, 0x1fffff);
  525. r = radeon_ring_lock(rdev, ring, num_loops * 7 + 14);
  526. if (r) {
  527. DRM_ERROR("radeon: moving bo (%d).\n", r);
  528. radeon_sync_free(rdev, &sync, NULL);
  529. return ERR_PTR(r);
  530. }
  531. radeon_sync_resv(rdev, &sync, resv, false);
  532. radeon_sync_rings(rdev, &sync, ring->idx);
  533. for (i = 0; i < num_loops; i++) {
  534. cur_size_in_bytes = size_in_bytes;
  535. if (cur_size_in_bytes > 0x1fffff)
  536. cur_size_in_bytes = 0x1fffff;
  537. size_in_bytes -= cur_size_in_bytes;
  538. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_COPY, SDMA_COPY_SUB_OPCODE_LINEAR, 0));
  539. radeon_ring_write(ring, cur_size_in_bytes);
  540. radeon_ring_write(ring, 0); /* src/dst endian swap */
  541. radeon_ring_write(ring, lower_32_bits(src_offset));
  542. radeon_ring_write(ring, upper_32_bits(src_offset));
  543. radeon_ring_write(ring, lower_32_bits(dst_offset));
  544. radeon_ring_write(ring, upper_32_bits(dst_offset));
  545. src_offset += cur_size_in_bytes;
  546. dst_offset += cur_size_in_bytes;
  547. }
  548. r = radeon_fence_emit(rdev, &fence, ring->idx);
  549. if (r) {
  550. radeon_ring_unlock_undo(rdev, ring);
  551. radeon_sync_free(rdev, &sync, NULL);
  552. return ERR_PTR(r);
  553. }
  554. radeon_ring_unlock_commit(rdev, ring, false);
  555. radeon_sync_free(rdev, &sync, fence);
  556. return fence;
  557. }
  558. /**
  559. * cik_sdma_ring_test - simple async dma engine test
  560. *
  561. * @rdev: radeon_device pointer
  562. * @ring: radeon_ring structure holding ring information
  563. *
  564. * Test the DMA engine by writing using it to write an
  565. * value to memory. (CIK).
  566. * Returns 0 for success, error for failure.
  567. */
  568. int cik_sdma_ring_test(struct radeon_device *rdev,
  569. struct radeon_ring *ring)
  570. {
  571. unsigned i;
  572. int r;
  573. unsigned index;
  574. u32 tmp;
  575. u64 gpu_addr;
  576. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  577. index = R600_WB_DMA_RING_TEST_OFFSET;
  578. else
  579. index = CAYMAN_WB_DMA1_RING_TEST_OFFSET;
  580. gpu_addr = rdev->wb.gpu_addr + index;
  581. tmp = 0xCAFEDEAD;
  582. rdev->wb.wb[index/4] = cpu_to_le32(tmp);
  583. r = radeon_ring_lock(rdev, ring, 5);
  584. if (r) {
  585. DRM_ERROR("radeon: dma failed to lock ring %d (%d).\n", ring->idx, r);
  586. return r;
  587. }
  588. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0));
  589. radeon_ring_write(ring, lower_32_bits(gpu_addr));
  590. radeon_ring_write(ring, upper_32_bits(gpu_addr));
  591. radeon_ring_write(ring, 1); /* number of DWs to follow */
  592. radeon_ring_write(ring, 0xDEADBEEF);
  593. radeon_ring_unlock_commit(rdev, ring, false);
  594. for (i = 0; i < rdev->usec_timeout; i++) {
  595. tmp = le32_to_cpu(rdev->wb.wb[index/4]);
  596. if (tmp == 0xDEADBEEF)
  597. break;
  598. DRM_UDELAY(1);
  599. }
  600. if (i < rdev->usec_timeout) {
  601. DRM_INFO("ring test on %d succeeded in %d usecs\n", ring->idx, i);
  602. } else {
  603. DRM_ERROR("radeon: ring %d test failed (0x%08X)\n",
  604. ring->idx, tmp);
  605. r = -EINVAL;
  606. }
  607. return r;
  608. }
  609. /**
  610. * cik_sdma_ib_test - test an IB on the DMA engine
  611. *
  612. * @rdev: radeon_device pointer
  613. * @ring: radeon_ring structure holding ring information
  614. *
  615. * Test a simple IB in the DMA ring (CIK).
  616. * Returns 0 on success, error on failure.
  617. */
  618. int cik_sdma_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
  619. {
  620. struct radeon_ib ib;
  621. unsigned i;
  622. unsigned index;
  623. int r;
  624. u32 tmp = 0;
  625. u64 gpu_addr;
  626. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  627. index = R600_WB_DMA_RING_TEST_OFFSET;
  628. else
  629. index = CAYMAN_WB_DMA1_RING_TEST_OFFSET;
  630. gpu_addr = rdev->wb.gpu_addr + index;
  631. tmp = 0xCAFEDEAD;
  632. rdev->wb.wb[index/4] = cpu_to_le32(tmp);
  633. r = radeon_ib_get(rdev, ring->idx, &ib, NULL, 256);
  634. if (r) {
  635. DRM_ERROR("radeon: failed to get ib (%d).\n", r);
  636. return r;
  637. }
  638. ib.ptr[0] = SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
  639. ib.ptr[1] = lower_32_bits(gpu_addr);
  640. ib.ptr[2] = upper_32_bits(gpu_addr);
  641. ib.ptr[3] = 1;
  642. ib.ptr[4] = 0xDEADBEEF;
  643. ib.length_dw = 5;
  644. r = radeon_ib_schedule(rdev, &ib, NULL, false);
  645. if (r) {
  646. radeon_ib_free(rdev, &ib);
  647. DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
  648. return r;
  649. }
  650. r = radeon_fence_wait(ib.fence, false);
  651. if (r) {
  652. DRM_ERROR("radeon: fence wait failed (%d).\n", r);
  653. return r;
  654. }
  655. for (i = 0; i < rdev->usec_timeout; i++) {
  656. tmp = le32_to_cpu(rdev->wb.wb[index/4]);
  657. if (tmp == 0xDEADBEEF)
  658. break;
  659. DRM_UDELAY(1);
  660. }
  661. if (i < rdev->usec_timeout) {
  662. DRM_INFO("ib test on ring %d succeeded in %u usecs\n", ib.fence->ring, i);
  663. } else {
  664. DRM_ERROR("radeon: ib test failed (0x%08X)\n", tmp);
  665. r = -EINVAL;
  666. }
  667. radeon_ib_free(rdev, &ib);
  668. return r;
  669. }
  670. /**
  671. * cik_sdma_is_lockup - Check if the DMA engine is locked up
  672. *
  673. * @rdev: radeon_device pointer
  674. * @ring: radeon_ring structure holding ring information
  675. *
  676. * Check if the async DMA engine is locked up (CIK).
  677. * Returns true if the engine appears to be locked up, false if not.
  678. */
  679. bool cik_sdma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  680. {
  681. u32 reset_mask = cik_gpu_check_soft_reset(rdev);
  682. u32 mask;
  683. if (ring->idx == R600_RING_TYPE_DMA_INDEX)
  684. mask = RADEON_RESET_DMA;
  685. else
  686. mask = RADEON_RESET_DMA1;
  687. if (!(reset_mask & mask)) {
  688. radeon_ring_lockup_update(rdev, ring);
  689. return false;
  690. }
  691. return radeon_ring_test_lockup(rdev, ring);
  692. }
  693. /**
  694. * cik_sdma_vm_copy_pages - update PTEs by copying them from the GART
  695. *
  696. * @rdev: radeon_device pointer
  697. * @ib: indirect buffer to fill with commands
  698. * @pe: addr of the page entry
  699. * @src: src addr to copy from
  700. * @count: number of page entries to update
  701. *
  702. * Update PTEs by copying them from the GART using sDMA (CIK).
  703. */
  704. void cik_sdma_vm_copy_pages(struct radeon_device *rdev,
  705. struct radeon_ib *ib,
  706. uint64_t pe, uint64_t src,
  707. unsigned count)
  708. {
  709. while (count) {
  710. unsigned bytes = count * 8;
  711. if (bytes > 0x1FFFF8)
  712. bytes = 0x1FFFF8;
  713. ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY,
  714. SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
  715. ib->ptr[ib->length_dw++] = bytes;
  716. ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
  717. ib->ptr[ib->length_dw++] = lower_32_bits(src);
  718. ib->ptr[ib->length_dw++] = upper_32_bits(src);
  719. ib->ptr[ib->length_dw++] = lower_32_bits(pe);
  720. ib->ptr[ib->length_dw++] = upper_32_bits(pe);
  721. pe += bytes;
  722. src += bytes;
  723. count -= bytes / 8;
  724. }
  725. }
  726. /**
  727. * cik_sdma_vm_write_pages - update PTEs by writing them manually
  728. *
  729. * @rdev: radeon_device pointer
  730. * @ib: indirect buffer to fill with commands
  731. * @pe: addr of the page entry
  732. * @addr: dst addr to write into pe
  733. * @count: number of page entries to update
  734. * @incr: increase next addr by incr bytes
  735. * @flags: access flags
  736. *
  737. * Update PTEs by writing them manually using sDMA (CIK).
  738. */
  739. void cik_sdma_vm_write_pages(struct radeon_device *rdev,
  740. struct radeon_ib *ib,
  741. uint64_t pe,
  742. uint64_t addr, unsigned count,
  743. uint32_t incr, uint32_t flags)
  744. {
  745. uint64_t value;
  746. unsigned ndw;
  747. while (count) {
  748. ndw = count * 2;
  749. if (ndw > 0xFFFFE)
  750. ndw = 0xFFFFE;
  751. /* for non-physically contiguous pages (system) */
  752. ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_WRITE,
  753. SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
  754. ib->ptr[ib->length_dw++] = pe;
  755. ib->ptr[ib->length_dw++] = upper_32_bits(pe);
  756. ib->ptr[ib->length_dw++] = ndw;
  757. for (; ndw > 0; ndw -= 2, --count, pe += 8) {
  758. if (flags & R600_PTE_SYSTEM) {
  759. value = radeon_vm_map_gart(rdev, addr);
  760. } else if (flags & R600_PTE_VALID) {
  761. value = addr;
  762. } else {
  763. value = 0;
  764. }
  765. addr += incr;
  766. value |= flags;
  767. ib->ptr[ib->length_dw++] = value;
  768. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  769. }
  770. }
  771. }
  772. /**
  773. * cik_sdma_vm_set_pages - update the page tables using sDMA
  774. *
  775. * @rdev: radeon_device pointer
  776. * @ib: indirect buffer to fill with commands
  777. * @pe: addr of the page entry
  778. * @addr: dst addr to write into pe
  779. * @count: number of page entries to update
  780. * @incr: increase next addr by incr bytes
  781. * @flags: access flags
  782. *
  783. * Update the page tables using sDMA (CIK).
  784. */
  785. void cik_sdma_vm_set_pages(struct radeon_device *rdev,
  786. struct radeon_ib *ib,
  787. uint64_t pe,
  788. uint64_t addr, unsigned count,
  789. uint32_t incr, uint32_t flags)
  790. {
  791. uint64_t value;
  792. unsigned ndw;
  793. while (count) {
  794. ndw = count;
  795. if (ndw > 0x7FFFF)
  796. ndw = 0x7FFFF;
  797. if (flags & R600_PTE_VALID)
  798. value = addr;
  799. else
  800. value = 0;
  801. /* for physically contiguous pages (vram) */
  802. ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_GENERATE_PTE_PDE, 0, 0);
  803. ib->ptr[ib->length_dw++] = pe; /* dst addr */
  804. ib->ptr[ib->length_dw++] = upper_32_bits(pe);
  805. ib->ptr[ib->length_dw++] = flags; /* mask */
  806. ib->ptr[ib->length_dw++] = 0;
  807. ib->ptr[ib->length_dw++] = value; /* value */
  808. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  809. ib->ptr[ib->length_dw++] = incr; /* increment size */
  810. ib->ptr[ib->length_dw++] = 0;
  811. ib->ptr[ib->length_dw++] = ndw; /* number of entries */
  812. pe += ndw * 8;
  813. addr += ndw * incr;
  814. count -= ndw;
  815. }
  816. }
  817. /**
  818. * cik_sdma_vm_pad_ib - pad the IB to the required number of dw
  819. *
  820. * @ib: indirect buffer to fill with padding
  821. *
  822. */
  823. void cik_sdma_vm_pad_ib(struct radeon_ib *ib)
  824. {
  825. while (ib->length_dw & 0x7)
  826. ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0);
  827. }
  828. /**
  829. * cik_dma_vm_flush - cik vm flush using sDMA
  830. *
  831. * @rdev: radeon_device pointer
  832. *
  833. * Update the page table base and flush the VM TLB
  834. * using sDMA (CIK).
  835. */
  836. void cik_dma_vm_flush(struct radeon_device *rdev, struct radeon_ring *ring,
  837. unsigned vm_id, uint64_t pd_addr)
  838. {
  839. u32 extra_bits = (SDMA_POLL_REG_MEM_EXTRA_OP(0) |
  840. SDMA_POLL_REG_MEM_EXTRA_FUNC(0)); /* always */
  841. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  842. if (vm_id < 8) {
  843. radeon_ring_write(ring, (VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm_id << 2)) >> 2);
  844. } else {
  845. radeon_ring_write(ring, (VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((vm_id - 8) << 2)) >> 2);
  846. }
  847. radeon_ring_write(ring, pd_addr >> 12);
  848. /* update SH_MEM_* regs */
  849. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  850. radeon_ring_write(ring, SRBM_GFX_CNTL >> 2);
  851. radeon_ring_write(ring, VMID(vm_id));
  852. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  853. radeon_ring_write(ring, SH_MEM_BASES >> 2);
  854. radeon_ring_write(ring, 0);
  855. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  856. radeon_ring_write(ring, SH_MEM_CONFIG >> 2);
  857. radeon_ring_write(ring, 0);
  858. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  859. radeon_ring_write(ring, SH_MEM_APE1_BASE >> 2);
  860. radeon_ring_write(ring, 1);
  861. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  862. radeon_ring_write(ring, SH_MEM_APE1_LIMIT >> 2);
  863. radeon_ring_write(ring, 0);
  864. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  865. radeon_ring_write(ring, SRBM_GFX_CNTL >> 2);
  866. radeon_ring_write(ring, VMID(0));
  867. /* flush HDP */
  868. cik_sdma_hdp_flush_ring_emit(rdev, ring->idx);
  869. /* flush TLB */
  870. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  871. radeon_ring_write(ring, VM_INVALIDATE_REQUEST >> 2);
  872. radeon_ring_write(ring, 1 << vm_id);
  873. radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0, extra_bits));
  874. radeon_ring_write(ring, VM_INVALIDATE_REQUEST >> 2);
  875. radeon_ring_write(ring, 0);
  876. radeon_ring_write(ring, 0); /* reference */
  877. radeon_ring_write(ring, 0); /* mask */
  878. radeon_ring_write(ring, (0xfff << 16) | 10); /* retry count, poll interval */
  879. }