cik_sdma.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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 "amdgpu.h"
  27. #include "amdgpu_ucode.h"
  28. #include "amdgpu_trace.h"
  29. #include "cikd.h"
  30. #include "cik.h"
  31. #include "bif/bif_4_1_d.h"
  32. #include "bif/bif_4_1_sh_mask.h"
  33. #include "gca/gfx_7_2_d.h"
  34. #include "gca/gfx_7_2_enum.h"
  35. #include "gca/gfx_7_2_sh_mask.h"
  36. #include "gmc/gmc_7_1_d.h"
  37. #include "gmc/gmc_7_1_sh_mask.h"
  38. #include "oss/oss_2_0_d.h"
  39. #include "oss/oss_2_0_sh_mask.h"
  40. static const u32 sdma_offsets[SDMA_MAX_INSTANCE] =
  41. {
  42. SDMA0_REGISTER_OFFSET,
  43. SDMA1_REGISTER_OFFSET
  44. };
  45. static void cik_sdma_set_ring_funcs(struct amdgpu_device *adev);
  46. static void cik_sdma_set_irq_funcs(struct amdgpu_device *adev);
  47. static void cik_sdma_set_buffer_funcs(struct amdgpu_device *adev);
  48. static void cik_sdma_set_vm_pte_funcs(struct amdgpu_device *adev);
  49. static int cik_sdma_soft_reset(void *handle);
  50. MODULE_FIRMWARE("radeon/bonaire_sdma.bin");
  51. MODULE_FIRMWARE("radeon/bonaire_sdma1.bin");
  52. MODULE_FIRMWARE("radeon/hawaii_sdma.bin");
  53. MODULE_FIRMWARE("radeon/hawaii_sdma1.bin");
  54. MODULE_FIRMWARE("radeon/kaveri_sdma.bin");
  55. MODULE_FIRMWARE("radeon/kaveri_sdma1.bin");
  56. MODULE_FIRMWARE("radeon/kabini_sdma.bin");
  57. MODULE_FIRMWARE("radeon/kabini_sdma1.bin");
  58. MODULE_FIRMWARE("radeon/mullins_sdma.bin");
  59. MODULE_FIRMWARE("radeon/mullins_sdma1.bin");
  60. u32 amdgpu_cik_gpu_check_soft_reset(struct amdgpu_device *adev);
  61. /*
  62. * sDMA - System DMA
  63. * Starting with CIK, the GPU has new asynchronous
  64. * DMA engines. These engines are used for compute
  65. * and gfx. There are two DMA engines (SDMA0, SDMA1)
  66. * and each one supports 1 ring buffer used for gfx
  67. * and 2 queues used for compute.
  68. *
  69. * The programming model is very similar to the CP
  70. * (ring buffer, IBs, etc.), but sDMA has it's own
  71. * packet format that is different from the PM4 format
  72. * used by the CP. sDMA supports copying data, writing
  73. * embedded data, solid fills, and a number of other
  74. * things. It also has support for tiling/detiling of
  75. * buffers.
  76. */
  77. /**
  78. * cik_sdma_init_microcode - load ucode images from disk
  79. *
  80. * @adev: amdgpu_device pointer
  81. *
  82. * Use the firmware interface to load the ucode images into
  83. * the driver (not loaded into hw).
  84. * Returns 0 on success, error on failure.
  85. */
  86. static int cik_sdma_init_microcode(struct amdgpu_device *adev)
  87. {
  88. const char *chip_name;
  89. char fw_name[30];
  90. int err = 0, i;
  91. DRM_DEBUG("\n");
  92. switch (adev->asic_type) {
  93. case CHIP_BONAIRE:
  94. chip_name = "bonaire";
  95. break;
  96. case CHIP_HAWAII:
  97. chip_name = "hawaii";
  98. break;
  99. case CHIP_KAVERI:
  100. chip_name = "kaveri";
  101. break;
  102. case CHIP_KABINI:
  103. chip_name = "kabini";
  104. break;
  105. case CHIP_MULLINS:
  106. chip_name = "mullins";
  107. break;
  108. default: BUG();
  109. }
  110. for (i = 0; i < adev->sdma.num_instances; i++) {
  111. if (i == 0)
  112. snprintf(fw_name, sizeof(fw_name), "radeon/%s_sdma.bin", chip_name);
  113. else
  114. snprintf(fw_name, sizeof(fw_name), "radeon/%s_sdma1.bin", chip_name);
  115. err = request_firmware(&adev->sdma.instance[i].fw, fw_name, adev->dev);
  116. if (err)
  117. goto out;
  118. err = amdgpu_ucode_validate(adev->sdma.instance[i].fw);
  119. }
  120. out:
  121. if (err) {
  122. printk(KERN_ERR
  123. "cik_sdma: Failed to load firmware \"%s\"\n",
  124. fw_name);
  125. for (i = 0; i < adev->sdma.num_instances; i++) {
  126. release_firmware(adev->sdma.instance[i].fw);
  127. adev->sdma.instance[i].fw = NULL;
  128. }
  129. }
  130. return err;
  131. }
  132. /**
  133. * cik_sdma_ring_get_rptr - get the current read pointer
  134. *
  135. * @ring: amdgpu ring pointer
  136. *
  137. * Get the current rptr from the hardware (CIK+).
  138. */
  139. static uint32_t cik_sdma_ring_get_rptr(struct amdgpu_ring *ring)
  140. {
  141. u32 rptr;
  142. rptr = ring->adev->wb.wb[ring->rptr_offs];
  143. return (rptr & 0x3fffc) >> 2;
  144. }
  145. /**
  146. * cik_sdma_ring_get_wptr - get the current write pointer
  147. *
  148. * @ring: amdgpu ring pointer
  149. *
  150. * Get the current wptr from the hardware (CIK+).
  151. */
  152. static uint32_t cik_sdma_ring_get_wptr(struct amdgpu_ring *ring)
  153. {
  154. struct amdgpu_device *adev = ring->adev;
  155. u32 me = (ring == &adev->sdma.instance[0].ring) ? 0 : 1;
  156. return (RREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[me]) & 0x3fffc) >> 2;
  157. }
  158. /**
  159. * cik_sdma_ring_set_wptr - commit the write pointer
  160. *
  161. * @ring: amdgpu ring pointer
  162. *
  163. * Write the wptr back to the hardware (CIK+).
  164. */
  165. static void cik_sdma_ring_set_wptr(struct amdgpu_ring *ring)
  166. {
  167. struct amdgpu_device *adev = ring->adev;
  168. u32 me = (ring == &adev->sdma.instance[0].ring) ? 0 : 1;
  169. WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[me], (ring->wptr << 2) & 0x3fffc);
  170. }
  171. static void cik_sdma_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
  172. {
  173. struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ring);
  174. int i;
  175. for (i = 0; i < count; i++)
  176. if (sdma && sdma->burst_nop && (i == 0))
  177. amdgpu_ring_write(ring, ring->nop |
  178. SDMA_NOP_COUNT(count - 1));
  179. else
  180. amdgpu_ring_write(ring, ring->nop);
  181. }
  182. /**
  183. * cik_sdma_ring_emit_ib - Schedule an IB on the DMA engine
  184. *
  185. * @ring: amdgpu ring pointer
  186. * @ib: IB object to schedule
  187. *
  188. * Schedule an IB in the DMA ring (CIK).
  189. */
  190. static void cik_sdma_ring_emit_ib(struct amdgpu_ring *ring,
  191. struct amdgpu_ib *ib)
  192. {
  193. u32 extra_bits = (ib->vm ? ib->vm->ids[ring->idx].id : 0) & 0xf;
  194. u32 next_rptr = ring->wptr + 5;
  195. while ((next_rptr & 7) != 4)
  196. next_rptr++;
  197. next_rptr += 4;
  198. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0));
  199. amdgpu_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
  200. amdgpu_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xffffffff);
  201. amdgpu_ring_write(ring, 1); /* number of DWs to follow */
  202. amdgpu_ring_write(ring, next_rptr);
  203. /* IB packet must end on a 8 DW boundary */
  204. cik_sdma_ring_insert_nop(ring, (12 - (ring->wptr & 7)) % 8);
  205. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_INDIRECT_BUFFER, 0, extra_bits));
  206. amdgpu_ring_write(ring, ib->gpu_addr & 0xffffffe0); /* base must be 32 byte aligned */
  207. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr) & 0xffffffff);
  208. amdgpu_ring_write(ring, ib->length_dw);
  209. }
  210. /**
  211. * cik_sdma_ring_emit_hdp_flush - emit an hdp flush on the DMA ring
  212. *
  213. * @ring: amdgpu ring pointer
  214. *
  215. * Emit an hdp flush packet on the requested DMA ring.
  216. */
  217. static void cik_sdma_ring_emit_hdp_flush(struct amdgpu_ring *ring)
  218. {
  219. u32 extra_bits = (SDMA_POLL_REG_MEM_EXTRA_OP(1) |
  220. SDMA_POLL_REG_MEM_EXTRA_FUNC(3)); /* == */
  221. u32 ref_and_mask;
  222. if (ring == &ring->adev->sdma.instance[0].ring)
  223. ref_and_mask = GPU_HDP_FLUSH_DONE__SDMA0_MASK;
  224. else
  225. ref_and_mask = GPU_HDP_FLUSH_DONE__SDMA1_MASK;
  226. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0, extra_bits));
  227. amdgpu_ring_write(ring, mmGPU_HDP_FLUSH_DONE << 2);
  228. amdgpu_ring_write(ring, mmGPU_HDP_FLUSH_REQ << 2);
  229. amdgpu_ring_write(ring, ref_and_mask); /* reference */
  230. amdgpu_ring_write(ring, ref_and_mask); /* mask */
  231. amdgpu_ring_write(ring, (0xfff << 16) | 10); /* retry count, poll interval */
  232. }
  233. /**
  234. * cik_sdma_ring_emit_fence - emit a fence on the DMA ring
  235. *
  236. * @ring: amdgpu ring pointer
  237. * @fence: amdgpu fence object
  238. *
  239. * Add a DMA fence packet to the ring to write
  240. * the fence seq number and DMA trap packet to generate
  241. * an interrupt if needed (CIK).
  242. */
  243. static void cik_sdma_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  244. unsigned flags)
  245. {
  246. bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
  247. /* write the fence */
  248. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_FENCE, 0, 0));
  249. amdgpu_ring_write(ring, lower_32_bits(addr));
  250. amdgpu_ring_write(ring, upper_32_bits(addr));
  251. amdgpu_ring_write(ring, lower_32_bits(seq));
  252. /* optionally write high bits as well */
  253. if (write64bit) {
  254. addr += 4;
  255. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_FENCE, 0, 0));
  256. amdgpu_ring_write(ring, lower_32_bits(addr));
  257. amdgpu_ring_write(ring, upper_32_bits(addr));
  258. amdgpu_ring_write(ring, upper_32_bits(seq));
  259. }
  260. /* generate an interrupt */
  261. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_TRAP, 0, 0));
  262. }
  263. /**
  264. * cik_sdma_ring_emit_semaphore - emit a semaphore on the dma ring
  265. *
  266. * @ring: amdgpu_ring structure holding ring information
  267. * @semaphore: amdgpu semaphore object
  268. * @emit_wait: wait or signal semaphore
  269. *
  270. * Add a DMA semaphore packet to the ring wait on or signal
  271. * other rings (CIK).
  272. */
  273. static bool cik_sdma_ring_emit_semaphore(struct amdgpu_ring *ring,
  274. struct amdgpu_semaphore *semaphore,
  275. bool emit_wait)
  276. {
  277. u64 addr = semaphore->gpu_addr;
  278. u32 extra_bits = emit_wait ? 0 : SDMA_SEMAPHORE_EXTRA_S;
  279. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SEMAPHORE, 0, extra_bits));
  280. amdgpu_ring_write(ring, addr & 0xfffffff8);
  281. amdgpu_ring_write(ring, upper_32_bits(addr) & 0xffffffff);
  282. return true;
  283. }
  284. /**
  285. * cik_sdma_gfx_stop - stop the gfx async dma engines
  286. *
  287. * @adev: amdgpu_device pointer
  288. *
  289. * Stop the gfx async dma ring buffers (CIK).
  290. */
  291. static void cik_sdma_gfx_stop(struct amdgpu_device *adev)
  292. {
  293. struct amdgpu_ring *sdma0 = &adev->sdma.instance[0].ring;
  294. struct amdgpu_ring *sdma1 = &adev->sdma.instance[1].ring;
  295. u32 rb_cntl;
  296. int i;
  297. if ((adev->mman.buffer_funcs_ring == sdma0) ||
  298. (adev->mman.buffer_funcs_ring == sdma1))
  299. amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
  300. for (i = 0; i < adev->sdma.num_instances; i++) {
  301. rb_cntl = RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]);
  302. rb_cntl &= ~SDMA0_GFX_RB_CNTL__RB_ENABLE_MASK;
  303. WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
  304. WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], 0);
  305. }
  306. sdma0->ready = false;
  307. sdma1->ready = false;
  308. }
  309. /**
  310. * cik_sdma_rlc_stop - stop the compute async dma engines
  311. *
  312. * @adev: amdgpu_device pointer
  313. *
  314. * Stop the compute async dma queues (CIK).
  315. */
  316. static void cik_sdma_rlc_stop(struct amdgpu_device *adev)
  317. {
  318. /* XXX todo */
  319. }
  320. /**
  321. * cik_sdma_enable - stop the async dma engines
  322. *
  323. * @adev: amdgpu_device pointer
  324. * @enable: enable/disable the DMA MEs.
  325. *
  326. * Halt or unhalt the async dma engines (CIK).
  327. */
  328. static void cik_sdma_enable(struct amdgpu_device *adev, bool enable)
  329. {
  330. u32 me_cntl;
  331. int i;
  332. if (enable == false) {
  333. cik_sdma_gfx_stop(adev);
  334. cik_sdma_rlc_stop(adev);
  335. }
  336. for (i = 0; i < adev->sdma.num_instances; i++) {
  337. me_cntl = RREG32(mmSDMA0_F32_CNTL + sdma_offsets[i]);
  338. if (enable)
  339. me_cntl &= ~SDMA0_F32_CNTL__HALT_MASK;
  340. else
  341. me_cntl |= SDMA0_F32_CNTL__HALT_MASK;
  342. WREG32(mmSDMA0_F32_CNTL + sdma_offsets[i], me_cntl);
  343. }
  344. }
  345. /**
  346. * cik_sdma_gfx_resume - setup and start the async dma engines
  347. *
  348. * @adev: amdgpu_device pointer
  349. *
  350. * Set up the gfx DMA ring buffers and enable them (CIK).
  351. * Returns 0 for success, error for failure.
  352. */
  353. static int cik_sdma_gfx_resume(struct amdgpu_device *adev)
  354. {
  355. struct amdgpu_ring *ring;
  356. u32 rb_cntl, ib_cntl;
  357. u32 rb_bufsz;
  358. u32 wb_offset;
  359. int i, j, r;
  360. for (i = 0; i < adev->sdma.num_instances; i++) {
  361. ring = &adev->sdma.instance[i].ring;
  362. wb_offset = (ring->rptr_offs * 4);
  363. mutex_lock(&adev->srbm_mutex);
  364. for (j = 0; j < 16; j++) {
  365. cik_srbm_select(adev, 0, 0, 0, j);
  366. /* SDMA GFX */
  367. WREG32(mmSDMA0_GFX_VIRTUAL_ADDR + sdma_offsets[i], 0);
  368. WREG32(mmSDMA0_GFX_APE1_CNTL + sdma_offsets[i], 0);
  369. /* XXX SDMA RLC - todo */
  370. }
  371. cik_srbm_select(adev, 0, 0, 0, 0);
  372. mutex_unlock(&adev->srbm_mutex);
  373. WREG32(mmSDMA0_SEM_INCOMPLETE_TIMER_CNTL + sdma_offsets[i], 0);
  374. WREG32(mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL + sdma_offsets[i], 0);
  375. /* Set ring buffer size in dwords */
  376. rb_bufsz = order_base_2(ring->ring_size / 4);
  377. rb_cntl = rb_bufsz << 1;
  378. #ifdef __BIG_ENDIAN
  379. rb_cntl |= SDMA0_GFX_RB_CNTL__RB_SWAP_ENABLE_MASK |
  380. SDMA0_GFX_RB_CNTL__RPTR_WRITEBACK_SWAP_ENABLE_MASK;
  381. #endif
  382. WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
  383. /* Initialize the ring buffer's read and write pointers */
  384. WREG32(mmSDMA0_GFX_RB_RPTR + sdma_offsets[i], 0);
  385. WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], 0);
  386. /* set the wb address whether it's enabled or not */
  387. WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_HI + sdma_offsets[i],
  388. upper_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFF);
  389. WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_LO + sdma_offsets[i],
  390. ((adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC));
  391. rb_cntl |= SDMA0_GFX_RB_CNTL__RPTR_WRITEBACK_ENABLE_MASK;
  392. WREG32(mmSDMA0_GFX_RB_BASE + sdma_offsets[i], ring->gpu_addr >> 8);
  393. WREG32(mmSDMA0_GFX_RB_BASE_HI + sdma_offsets[i], ring->gpu_addr >> 40);
  394. ring->wptr = 0;
  395. WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], ring->wptr << 2);
  396. /* enable DMA RB */
  397. WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i],
  398. rb_cntl | SDMA0_GFX_RB_CNTL__RB_ENABLE_MASK);
  399. ib_cntl = SDMA0_GFX_IB_CNTL__IB_ENABLE_MASK;
  400. #ifdef __BIG_ENDIAN
  401. ib_cntl |= SDMA0_GFX_IB_CNTL__IB_SWAP_ENABLE_MASK;
  402. #endif
  403. /* enable DMA IBs */
  404. WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], ib_cntl);
  405. ring->ready = true;
  406. r = amdgpu_ring_test_ring(ring);
  407. if (r) {
  408. ring->ready = false;
  409. return r;
  410. }
  411. if (adev->mman.buffer_funcs_ring == ring)
  412. amdgpu_ttm_set_active_vram_size(adev, adev->mc.real_vram_size);
  413. }
  414. return 0;
  415. }
  416. /**
  417. * cik_sdma_rlc_resume - setup and start the async dma engines
  418. *
  419. * @adev: amdgpu_device pointer
  420. *
  421. * Set up the compute DMA queues and enable them (CIK).
  422. * Returns 0 for success, error for failure.
  423. */
  424. static int cik_sdma_rlc_resume(struct amdgpu_device *adev)
  425. {
  426. /* XXX todo */
  427. return 0;
  428. }
  429. /**
  430. * cik_sdma_load_microcode - load the sDMA ME ucode
  431. *
  432. * @adev: amdgpu_device pointer
  433. *
  434. * Loads the sDMA0/1 ucode.
  435. * Returns 0 for success, -EINVAL if the ucode is not available.
  436. */
  437. static int cik_sdma_load_microcode(struct amdgpu_device *adev)
  438. {
  439. const struct sdma_firmware_header_v1_0 *hdr;
  440. const __le32 *fw_data;
  441. u32 fw_size;
  442. int i, j;
  443. /* halt the MEs */
  444. cik_sdma_enable(adev, false);
  445. for (i = 0; i < adev->sdma.num_instances; i++) {
  446. if (!adev->sdma.instance[i].fw)
  447. return -EINVAL;
  448. hdr = (const struct sdma_firmware_header_v1_0 *)adev->sdma.instance[i].fw->data;
  449. amdgpu_ucode_print_sdma_hdr(&hdr->header);
  450. fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
  451. adev->sdma.instance[i].fw_version = le32_to_cpu(hdr->header.ucode_version);
  452. adev->sdma.instance[i].feature_version = le32_to_cpu(hdr->ucode_feature_version);
  453. if (adev->sdma.instance[i].feature_version >= 20)
  454. adev->sdma.instance[i].burst_nop = true;
  455. fw_data = (const __le32 *)
  456. (adev->sdma.instance[i].fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  457. WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], 0);
  458. for (j = 0; j < fw_size; j++)
  459. WREG32(mmSDMA0_UCODE_DATA + sdma_offsets[i], le32_to_cpup(fw_data++));
  460. WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], adev->sdma.instance[i].fw_version);
  461. }
  462. return 0;
  463. }
  464. /**
  465. * cik_sdma_start - setup and start the async dma engines
  466. *
  467. * @adev: amdgpu_device pointer
  468. *
  469. * Set up the DMA engines and enable them (CIK).
  470. * Returns 0 for success, error for failure.
  471. */
  472. static int cik_sdma_start(struct amdgpu_device *adev)
  473. {
  474. int r;
  475. r = cik_sdma_load_microcode(adev);
  476. if (r)
  477. return r;
  478. /* unhalt the MEs */
  479. cik_sdma_enable(adev, true);
  480. /* start the gfx rings and rlc compute queues */
  481. r = cik_sdma_gfx_resume(adev);
  482. if (r)
  483. return r;
  484. r = cik_sdma_rlc_resume(adev);
  485. if (r)
  486. return r;
  487. return 0;
  488. }
  489. /**
  490. * cik_sdma_ring_test_ring - simple async dma engine test
  491. *
  492. * @ring: amdgpu_ring structure holding ring information
  493. *
  494. * Test the DMA engine by writing using it to write an
  495. * value to memory. (CIK).
  496. * Returns 0 for success, error for failure.
  497. */
  498. static int cik_sdma_ring_test_ring(struct amdgpu_ring *ring)
  499. {
  500. struct amdgpu_device *adev = ring->adev;
  501. unsigned i;
  502. unsigned index;
  503. int r;
  504. u32 tmp;
  505. u64 gpu_addr;
  506. r = amdgpu_wb_get(adev, &index);
  507. if (r) {
  508. dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
  509. return r;
  510. }
  511. gpu_addr = adev->wb.gpu_addr + (index * 4);
  512. tmp = 0xCAFEDEAD;
  513. adev->wb.wb[index] = cpu_to_le32(tmp);
  514. r = amdgpu_ring_lock(ring, 5);
  515. if (r) {
  516. DRM_ERROR("amdgpu: dma failed to lock ring %d (%d).\n", ring->idx, r);
  517. amdgpu_wb_free(adev, index);
  518. return r;
  519. }
  520. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0));
  521. amdgpu_ring_write(ring, lower_32_bits(gpu_addr));
  522. amdgpu_ring_write(ring, upper_32_bits(gpu_addr));
  523. amdgpu_ring_write(ring, 1); /* number of DWs to follow */
  524. amdgpu_ring_write(ring, 0xDEADBEEF);
  525. amdgpu_ring_unlock_commit(ring);
  526. for (i = 0; i < adev->usec_timeout; i++) {
  527. tmp = le32_to_cpu(adev->wb.wb[index]);
  528. if (tmp == 0xDEADBEEF)
  529. break;
  530. DRM_UDELAY(1);
  531. }
  532. if (i < adev->usec_timeout) {
  533. DRM_INFO("ring test on %d succeeded in %d usecs\n", ring->idx, i);
  534. } else {
  535. DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
  536. ring->idx, tmp);
  537. r = -EINVAL;
  538. }
  539. amdgpu_wb_free(adev, index);
  540. return r;
  541. }
  542. /**
  543. * cik_sdma_ring_test_ib - test an IB on the DMA engine
  544. *
  545. * @ring: amdgpu_ring structure holding ring information
  546. *
  547. * Test a simple IB in the DMA ring (CIK).
  548. * Returns 0 on success, error on failure.
  549. */
  550. static int cik_sdma_ring_test_ib(struct amdgpu_ring *ring)
  551. {
  552. struct amdgpu_device *adev = ring->adev;
  553. struct amdgpu_ib ib;
  554. struct fence *f = NULL;
  555. unsigned i;
  556. unsigned index;
  557. int r;
  558. u32 tmp = 0;
  559. u64 gpu_addr;
  560. r = amdgpu_wb_get(adev, &index);
  561. if (r) {
  562. dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
  563. return r;
  564. }
  565. gpu_addr = adev->wb.gpu_addr + (index * 4);
  566. tmp = 0xCAFEDEAD;
  567. adev->wb.wb[index] = cpu_to_le32(tmp);
  568. memset(&ib, 0, sizeof(ib));
  569. r = amdgpu_ib_get(ring, NULL, 256, &ib);
  570. if (r) {
  571. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  572. goto err0;
  573. }
  574. ib.ptr[0] = SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
  575. ib.ptr[1] = lower_32_bits(gpu_addr);
  576. ib.ptr[2] = upper_32_bits(gpu_addr);
  577. ib.ptr[3] = 1;
  578. ib.ptr[4] = 0xDEADBEEF;
  579. ib.length_dw = 5;
  580. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, &ib, 1, NULL,
  581. AMDGPU_FENCE_OWNER_UNDEFINED,
  582. &f);
  583. if (r)
  584. goto err1;
  585. r = fence_wait(f, false);
  586. if (r) {
  587. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  588. goto err1;
  589. }
  590. for (i = 0; i < adev->usec_timeout; i++) {
  591. tmp = le32_to_cpu(adev->wb.wb[index]);
  592. if (tmp == 0xDEADBEEF)
  593. break;
  594. DRM_UDELAY(1);
  595. }
  596. if (i < adev->usec_timeout) {
  597. DRM_INFO("ib test on ring %d succeeded in %u usecs\n",
  598. ring->idx, i);
  599. goto err1;
  600. } else {
  601. DRM_ERROR("amdgpu: ib test failed (0x%08X)\n", tmp);
  602. r = -EINVAL;
  603. }
  604. err1:
  605. fence_put(f);
  606. amdgpu_ib_free(adev, &ib);
  607. err0:
  608. amdgpu_wb_free(adev, index);
  609. return r;
  610. }
  611. /**
  612. * cik_sdma_vm_copy_pages - update PTEs by copying them from the GART
  613. *
  614. * @ib: indirect buffer to fill with commands
  615. * @pe: addr of the page entry
  616. * @src: src addr to copy from
  617. * @count: number of page entries to update
  618. *
  619. * Update PTEs by copying them from the GART using sDMA (CIK).
  620. */
  621. static void cik_sdma_vm_copy_pte(struct amdgpu_ib *ib,
  622. uint64_t pe, uint64_t src,
  623. unsigned count)
  624. {
  625. while (count) {
  626. unsigned bytes = count * 8;
  627. if (bytes > 0x1FFFF8)
  628. bytes = 0x1FFFF8;
  629. ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY,
  630. SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
  631. ib->ptr[ib->length_dw++] = bytes;
  632. ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
  633. ib->ptr[ib->length_dw++] = lower_32_bits(src);
  634. ib->ptr[ib->length_dw++] = upper_32_bits(src);
  635. ib->ptr[ib->length_dw++] = lower_32_bits(pe);
  636. ib->ptr[ib->length_dw++] = upper_32_bits(pe);
  637. pe += bytes;
  638. src += bytes;
  639. count -= bytes / 8;
  640. }
  641. }
  642. /**
  643. * cik_sdma_vm_write_pages - update PTEs by writing them manually
  644. *
  645. * @ib: indirect buffer to fill with commands
  646. * @pe: addr of the page entry
  647. * @addr: dst addr to write into pe
  648. * @count: number of page entries to update
  649. * @incr: increase next addr by incr bytes
  650. * @flags: access flags
  651. *
  652. * Update PTEs by writing them manually using sDMA (CIK).
  653. */
  654. static void cik_sdma_vm_write_pte(struct amdgpu_ib *ib,
  655. uint64_t pe,
  656. uint64_t addr, unsigned count,
  657. uint32_t incr, uint32_t flags)
  658. {
  659. uint64_t value;
  660. unsigned ndw;
  661. while (count) {
  662. ndw = count * 2;
  663. if (ndw > 0xFFFFE)
  664. ndw = 0xFFFFE;
  665. /* for non-physically contiguous pages (system) */
  666. ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_WRITE,
  667. SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
  668. ib->ptr[ib->length_dw++] = pe;
  669. ib->ptr[ib->length_dw++] = upper_32_bits(pe);
  670. ib->ptr[ib->length_dw++] = ndw;
  671. for (; ndw > 0; ndw -= 2, --count, pe += 8) {
  672. if (flags & AMDGPU_PTE_SYSTEM) {
  673. value = amdgpu_vm_map_gart(ib->ring->adev, addr);
  674. value &= 0xFFFFFFFFFFFFF000ULL;
  675. } else if (flags & AMDGPU_PTE_VALID) {
  676. value = addr;
  677. } else {
  678. value = 0;
  679. }
  680. addr += incr;
  681. value |= flags;
  682. ib->ptr[ib->length_dw++] = value;
  683. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  684. }
  685. }
  686. }
  687. /**
  688. * cik_sdma_vm_set_pages - update the page tables using sDMA
  689. *
  690. * @ib: indirect buffer to fill with commands
  691. * @pe: addr of the page entry
  692. * @addr: dst addr to write into pe
  693. * @count: number of page entries to update
  694. * @incr: increase next addr by incr bytes
  695. * @flags: access flags
  696. *
  697. * Update the page tables using sDMA (CIK).
  698. */
  699. static void cik_sdma_vm_set_pte_pde(struct amdgpu_ib *ib,
  700. uint64_t pe,
  701. uint64_t addr, unsigned count,
  702. uint32_t incr, uint32_t flags)
  703. {
  704. uint64_t value;
  705. unsigned ndw;
  706. while (count) {
  707. ndw = count;
  708. if (ndw > 0x7FFFF)
  709. ndw = 0x7FFFF;
  710. if (flags & AMDGPU_PTE_VALID)
  711. value = addr;
  712. else
  713. value = 0;
  714. /* for physically contiguous pages (vram) */
  715. ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_GENERATE_PTE_PDE, 0, 0);
  716. ib->ptr[ib->length_dw++] = pe; /* dst addr */
  717. ib->ptr[ib->length_dw++] = upper_32_bits(pe);
  718. ib->ptr[ib->length_dw++] = flags; /* mask */
  719. ib->ptr[ib->length_dw++] = 0;
  720. ib->ptr[ib->length_dw++] = value; /* value */
  721. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  722. ib->ptr[ib->length_dw++] = incr; /* increment size */
  723. ib->ptr[ib->length_dw++] = 0;
  724. ib->ptr[ib->length_dw++] = ndw; /* number of entries */
  725. pe += ndw * 8;
  726. addr += ndw * incr;
  727. count -= ndw;
  728. }
  729. }
  730. /**
  731. * cik_sdma_vm_pad_ib - pad the IB to the required number of dw
  732. *
  733. * @ib: indirect buffer to fill with padding
  734. *
  735. */
  736. static void cik_sdma_vm_pad_ib(struct amdgpu_ib *ib)
  737. {
  738. struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ib->ring);
  739. u32 pad_count;
  740. int i;
  741. pad_count = (8 - (ib->length_dw & 0x7)) % 8;
  742. for (i = 0; i < pad_count; i++)
  743. if (sdma && sdma->burst_nop && (i == 0))
  744. ib->ptr[ib->length_dw++] =
  745. SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0) |
  746. SDMA_NOP_COUNT(pad_count - 1);
  747. else
  748. ib->ptr[ib->length_dw++] =
  749. SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0);
  750. }
  751. /**
  752. * cik_sdma_ring_emit_vm_flush - cik vm flush using sDMA
  753. *
  754. * @ring: amdgpu_ring pointer
  755. * @vm: amdgpu_vm pointer
  756. *
  757. * Update the page table base and flush the VM TLB
  758. * using sDMA (CIK).
  759. */
  760. static void cik_sdma_ring_emit_vm_flush(struct amdgpu_ring *ring,
  761. unsigned vm_id, uint64_t pd_addr)
  762. {
  763. u32 extra_bits = (SDMA_POLL_REG_MEM_EXTRA_OP(0) |
  764. SDMA_POLL_REG_MEM_EXTRA_FUNC(0)); /* always */
  765. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  766. if (vm_id < 8) {
  767. amdgpu_ring_write(ring, (mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR + vm_id));
  768. } else {
  769. amdgpu_ring_write(ring, (mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + vm_id - 8));
  770. }
  771. amdgpu_ring_write(ring, pd_addr >> 12);
  772. /* flush TLB */
  773. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
  774. amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST);
  775. amdgpu_ring_write(ring, 1 << vm_id);
  776. amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0, extra_bits));
  777. amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST << 2);
  778. amdgpu_ring_write(ring, 0);
  779. amdgpu_ring_write(ring, 0); /* reference */
  780. amdgpu_ring_write(ring, 0); /* mask */
  781. amdgpu_ring_write(ring, (0xfff << 16) | 10); /* retry count, poll interval */
  782. }
  783. static void cik_enable_sdma_mgcg(struct amdgpu_device *adev,
  784. bool enable)
  785. {
  786. u32 orig, data;
  787. if (enable && (adev->cg_flags & AMDGPU_CG_SUPPORT_SDMA_MGCG)) {
  788. WREG32(mmSDMA0_CLK_CTRL + SDMA0_REGISTER_OFFSET, 0x00000100);
  789. WREG32(mmSDMA0_CLK_CTRL + SDMA1_REGISTER_OFFSET, 0x00000100);
  790. } else {
  791. orig = data = RREG32(mmSDMA0_CLK_CTRL + SDMA0_REGISTER_OFFSET);
  792. data |= 0xff000000;
  793. if (data != orig)
  794. WREG32(mmSDMA0_CLK_CTRL + SDMA0_REGISTER_OFFSET, data);
  795. orig = data = RREG32(mmSDMA0_CLK_CTRL + SDMA1_REGISTER_OFFSET);
  796. data |= 0xff000000;
  797. if (data != orig)
  798. WREG32(mmSDMA0_CLK_CTRL + SDMA1_REGISTER_OFFSET, data);
  799. }
  800. }
  801. static void cik_enable_sdma_mgls(struct amdgpu_device *adev,
  802. bool enable)
  803. {
  804. u32 orig, data;
  805. if (enable && (adev->cg_flags & AMDGPU_CG_SUPPORT_SDMA_LS)) {
  806. orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET);
  807. data |= 0x100;
  808. if (orig != data)
  809. WREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET, data);
  810. orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET);
  811. data |= 0x100;
  812. if (orig != data)
  813. WREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET, data);
  814. } else {
  815. orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET);
  816. data &= ~0x100;
  817. if (orig != data)
  818. WREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET, data);
  819. orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET);
  820. data &= ~0x100;
  821. if (orig != data)
  822. WREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET, data);
  823. }
  824. }
  825. static int cik_sdma_early_init(void *handle)
  826. {
  827. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  828. adev->sdma.num_instances = SDMA_MAX_INSTANCE;
  829. cik_sdma_set_ring_funcs(adev);
  830. cik_sdma_set_irq_funcs(adev);
  831. cik_sdma_set_buffer_funcs(adev);
  832. cik_sdma_set_vm_pte_funcs(adev);
  833. return 0;
  834. }
  835. static int cik_sdma_sw_init(void *handle)
  836. {
  837. struct amdgpu_ring *ring;
  838. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  839. int r, i;
  840. r = cik_sdma_init_microcode(adev);
  841. if (r) {
  842. DRM_ERROR("Failed to load sdma firmware!\n");
  843. return r;
  844. }
  845. /* SDMA trap event */
  846. r = amdgpu_irq_add_id(adev, 224, &adev->sdma.trap_irq);
  847. if (r)
  848. return r;
  849. /* SDMA Privileged inst */
  850. r = amdgpu_irq_add_id(adev, 241, &adev->sdma.illegal_inst_irq);
  851. if (r)
  852. return r;
  853. /* SDMA Privileged inst */
  854. r = amdgpu_irq_add_id(adev, 247, &adev->sdma.illegal_inst_irq);
  855. if (r)
  856. return r;
  857. for (i = 0; i < adev->sdma.num_instances; i++) {
  858. ring = &adev->sdma.instance[i].ring;
  859. ring->ring_obj = NULL;
  860. sprintf(ring->name, "sdma%d", i);
  861. r = amdgpu_ring_init(adev, ring, 256 * 1024,
  862. SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0), 0xf,
  863. &adev->sdma.trap_irq,
  864. (i == 0) ?
  865. AMDGPU_SDMA_IRQ_TRAP0 : AMDGPU_SDMA_IRQ_TRAP1,
  866. AMDGPU_RING_TYPE_SDMA);
  867. if (r)
  868. return r;
  869. }
  870. return r;
  871. }
  872. static int cik_sdma_sw_fini(void *handle)
  873. {
  874. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  875. int i;
  876. for (i = 0; i < adev->sdma.num_instances; i++)
  877. amdgpu_ring_fini(&adev->sdma.instance[i].ring);
  878. return 0;
  879. }
  880. static int cik_sdma_hw_init(void *handle)
  881. {
  882. int r;
  883. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  884. r = cik_sdma_start(adev);
  885. if (r)
  886. return r;
  887. return r;
  888. }
  889. static int cik_sdma_hw_fini(void *handle)
  890. {
  891. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  892. cik_sdma_enable(adev, false);
  893. return 0;
  894. }
  895. static int cik_sdma_suspend(void *handle)
  896. {
  897. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  898. return cik_sdma_hw_fini(adev);
  899. }
  900. static int cik_sdma_resume(void *handle)
  901. {
  902. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  903. cik_sdma_soft_reset(handle);
  904. return cik_sdma_hw_init(adev);
  905. }
  906. static bool cik_sdma_is_idle(void *handle)
  907. {
  908. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  909. u32 tmp = RREG32(mmSRBM_STATUS2);
  910. if (tmp & (SRBM_STATUS2__SDMA_BUSY_MASK |
  911. SRBM_STATUS2__SDMA1_BUSY_MASK))
  912. return false;
  913. return true;
  914. }
  915. static int cik_sdma_wait_for_idle(void *handle)
  916. {
  917. unsigned i;
  918. u32 tmp;
  919. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  920. for (i = 0; i < adev->usec_timeout; i++) {
  921. tmp = RREG32(mmSRBM_STATUS2) & (SRBM_STATUS2__SDMA_BUSY_MASK |
  922. SRBM_STATUS2__SDMA1_BUSY_MASK);
  923. if (!tmp)
  924. return 0;
  925. udelay(1);
  926. }
  927. return -ETIMEDOUT;
  928. }
  929. static void cik_sdma_print_status(void *handle)
  930. {
  931. int i, j;
  932. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  933. dev_info(adev->dev, "CIK SDMA registers\n");
  934. dev_info(adev->dev, " SRBM_STATUS2=0x%08X\n",
  935. RREG32(mmSRBM_STATUS2));
  936. for (i = 0; i < adev->sdma.num_instances; i++) {
  937. dev_info(adev->dev, " SDMA%d_STATUS_REG=0x%08X\n",
  938. i, RREG32(mmSDMA0_STATUS_REG + sdma_offsets[i]));
  939. dev_info(adev->dev, " SDMA%d_ME_CNTL=0x%08X\n",
  940. i, RREG32(mmSDMA0_F32_CNTL + sdma_offsets[i]));
  941. dev_info(adev->dev, " SDMA%d_CNTL=0x%08X\n",
  942. i, RREG32(mmSDMA0_CNTL + sdma_offsets[i]));
  943. dev_info(adev->dev, " SDMA%d_SEM_INCOMPLETE_TIMER_CNTL=0x%08X\n",
  944. i, RREG32(mmSDMA0_SEM_INCOMPLETE_TIMER_CNTL + sdma_offsets[i]));
  945. dev_info(adev->dev, " SDMA%d_SEM_WAIT_FAIL_TIMER_CNTL=0x%08X\n",
  946. i, RREG32(mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL + sdma_offsets[i]));
  947. dev_info(adev->dev, " SDMA%d_GFX_IB_CNTL=0x%08X\n",
  948. i, RREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i]));
  949. dev_info(adev->dev, " SDMA%d_GFX_RB_CNTL=0x%08X\n",
  950. i, RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]));
  951. dev_info(adev->dev, " SDMA%d_GFX_RB_RPTR=0x%08X\n",
  952. i, RREG32(mmSDMA0_GFX_RB_RPTR + sdma_offsets[i]));
  953. dev_info(adev->dev, " SDMA%d_GFX_RB_WPTR=0x%08X\n",
  954. i, RREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i]));
  955. dev_info(adev->dev, " SDMA%d_GFX_RB_RPTR_ADDR_HI=0x%08X\n",
  956. i, RREG32(mmSDMA0_GFX_RB_RPTR_ADDR_HI + sdma_offsets[i]));
  957. dev_info(adev->dev, " SDMA%d_GFX_RB_RPTR_ADDR_LO=0x%08X\n",
  958. i, RREG32(mmSDMA0_GFX_RB_RPTR_ADDR_LO + sdma_offsets[i]));
  959. dev_info(adev->dev, " SDMA%d_GFX_RB_BASE=0x%08X\n",
  960. i, RREG32(mmSDMA0_GFX_RB_BASE + sdma_offsets[i]));
  961. dev_info(adev->dev, " SDMA%d_GFX_RB_BASE_HI=0x%08X\n",
  962. i, RREG32(mmSDMA0_GFX_RB_BASE_HI + sdma_offsets[i]));
  963. mutex_lock(&adev->srbm_mutex);
  964. for (j = 0; j < 16; j++) {
  965. cik_srbm_select(adev, 0, 0, 0, j);
  966. dev_info(adev->dev, " VM %d:\n", j);
  967. dev_info(adev->dev, " SDMA0_GFX_VIRTUAL_ADDR=0x%08X\n",
  968. RREG32(mmSDMA0_GFX_VIRTUAL_ADDR + sdma_offsets[i]));
  969. dev_info(adev->dev, " SDMA0_GFX_APE1_CNTL=0x%08X\n",
  970. RREG32(mmSDMA0_GFX_APE1_CNTL + sdma_offsets[i]));
  971. }
  972. cik_srbm_select(adev, 0, 0, 0, 0);
  973. mutex_unlock(&adev->srbm_mutex);
  974. }
  975. }
  976. static int cik_sdma_soft_reset(void *handle)
  977. {
  978. u32 srbm_soft_reset = 0;
  979. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  980. u32 tmp = RREG32(mmSRBM_STATUS2);
  981. if (tmp & SRBM_STATUS2__SDMA_BUSY_MASK) {
  982. /* sdma0 */
  983. tmp = RREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET);
  984. tmp |= SDMA0_F32_CNTL__HALT_MASK;
  985. WREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET, tmp);
  986. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA_MASK;
  987. }
  988. if (tmp & SRBM_STATUS2__SDMA1_BUSY_MASK) {
  989. /* sdma1 */
  990. tmp = RREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET);
  991. tmp |= SDMA0_F32_CNTL__HALT_MASK;
  992. WREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET, tmp);
  993. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA1_MASK;
  994. }
  995. if (srbm_soft_reset) {
  996. cik_sdma_print_status((void *)adev);
  997. tmp = RREG32(mmSRBM_SOFT_RESET);
  998. tmp |= srbm_soft_reset;
  999. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  1000. WREG32(mmSRBM_SOFT_RESET, tmp);
  1001. tmp = RREG32(mmSRBM_SOFT_RESET);
  1002. udelay(50);
  1003. tmp &= ~srbm_soft_reset;
  1004. WREG32(mmSRBM_SOFT_RESET, tmp);
  1005. tmp = RREG32(mmSRBM_SOFT_RESET);
  1006. /* Wait a little for things to settle down */
  1007. udelay(50);
  1008. cik_sdma_print_status((void *)adev);
  1009. }
  1010. return 0;
  1011. }
  1012. static int cik_sdma_set_trap_irq_state(struct amdgpu_device *adev,
  1013. struct amdgpu_irq_src *src,
  1014. unsigned type,
  1015. enum amdgpu_interrupt_state state)
  1016. {
  1017. u32 sdma_cntl;
  1018. switch (type) {
  1019. case AMDGPU_SDMA_IRQ_TRAP0:
  1020. switch (state) {
  1021. case AMDGPU_IRQ_STATE_DISABLE:
  1022. sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
  1023. sdma_cntl &= ~SDMA0_CNTL__TRAP_ENABLE_MASK;
  1024. WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
  1025. break;
  1026. case AMDGPU_IRQ_STATE_ENABLE:
  1027. sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
  1028. sdma_cntl |= SDMA0_CNTL__TRAP_ENABLE_MASK;
  1029. WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
  1030. break;
  1031. default:
  1032. break;
  1033. }
  1034. break;
  1035. case AMDGPU_SDMA_IRQ_TRAP1:
  1036. switch (state) {
  1037. case AMDGPU_IRQ_STATE_DISABLE:
  1038. sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
  1039. sdma_cntl &= ~SDMA0_CNTL__TRAP_ENABLE_MASK;
  1040. WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
  1041. break;
  1042. case AMDGPU_IRQ_STATE_ENABLE:
  1043. sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
  1044. sdma_cntl |= SDMA0_CNTL__TRAP_ENABLE_MASK;
  1045. WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
  1046. break;
  1047. default:
  1048. break;
  1049. }
  1050. break;
  1051. default:
  1052. break;
  1053. }
  1054. return 0;
  1055. }
  1056. static int cik_sdma_process_trap_irq(struct amdgpu_device *adev,
  1057. struct amdgpu_irq_src *source,
  1058. struct amdgpu_iv_entry *entry)
  1059. {
  1060. u8 instance_id, queue_id;
  1061. instance_id = (entry->ring_id & 0x3) >> 0;
  1062. queue_id = (entry->ring_id & 0xc) >> 2;
  1063. DRM_DEBUG("IH: SDMA trap\n");
  1064. switch (instance_id) {
  1065. case 0:
  1066. switch (queue_id) {
  1067. case 0:
  1068. amdgpu_fence_process(&adev->sdma.instance[0].ring);
  1069. break;
  1070. case 1:
  1071. /* XXX compute */
  1072. break;
  1073. case 2:
  1074. /* XXX compute */
  1075. break;
  1076. }
  1077. break;
  1078. case 1:
  1079. switch (queue_id) {
  1080. case 0:
  1081. amdgpu_fence_process(&adev->sdma.instance[1].ring);
  1082. break;
  1083. case 1:
  1084. /* XXX compute */
  1085. break;
  1086. case 2:
  1087. /* XXX compute */
  1088. break;
  1089. }
  1090. break;
  1091. }
  1092. return 0;
  1093. }
  1094. static int cik_sdma_process_illegal_inst_irq(struct amdgpu_device *adev,
  1095. struct amdgpu_irq_src *source,
  1096. struct amdgpu_iv_entry *entry)
  1097. {
  1098. DRM_ERROR("Illegal instruction in SDMA command stream\n");
  1099. schedule_work(&adev->reset_work);
  1100. return 0;
  1101. }
  1102. static int cik_sdma_set_clockgating_state(void *handle,
  1103. enum amd_clockgating_state state)
  1104. {
  1105. bool gate = false;
  1106. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1107. if (state == AMD_CG_STATE_GATE)
  1108. gate = true;
  1109. cik_enable_sdma_mgcg(adev, gate);
  1110. cik_enable_sdma_mgls(adev, gate);
  1111. return 0;
  1112. }
  1113. static int cik_sdma_set_powergating_state(void *handle,
  1114. enum amd_powergating_state state)
  1115. {
  1116. return 0;
  1117. }
  1118. const struct amd_ip_funcs cik_sdma_ip_funcs = {
  1119. .early_init = cik_sdma_early_init,
  1120. .late_init = NULL,
  1121. .sw_init = cik_sdma_sw_init,
  1122. .sw_fini = cik_sdma_sw_fini,
  1123. .hw_init = cik_sdma_hw_init,
  1124. .hw_fini = cik_sdma_hw_fini,
  1125. .suspend = cik_sdma_suspend,
  1126. .resume = cik_sdma_resume,
  1127. .is_idle = cik_sdma_is_idle,
  1128. .wait_for_idle = cik_sdma_wait_for_idle,
  1129. .soft_reset = cik_sdma_soft_reset,
  1130. .print_status = cik_sdma_print_status,
  1131. .set_clockgating_state = cik_sdma_set_clockgating_state,
  1132. .set_powergating_state = cik_sdma_set_powergating_state,
  1133. };
  1134. static const struct amdgpu_ring_funcs cik_sdma_ring_funcs = {
  1135. .get_rptr = cik_sdma_ring_get_rptr,
  1136. .get_wptr = cik_sdma_ring_get_wptr,
  1137. .set_wptr = cik_sdma_ring_set_wptr,
  1138. .parse_cs = NULL,
  1139. .emit_ib = cik_sdma_ring_emit_ib,
  1140. .emit_fence = cik_sdma_ring_emit_fence,
  1141. .emit_semaphore = cik_sdma_ring_emit_semaphore,
  1142. .emit_vm_flush = cik_sdma_ring_emit_vm_flush,
  1143. .emit_hdp_flush = cik_sdma_ring_emit_hdp_flush,
  1144. .test_ring = cik_sdma_ring_test_ring,
  1145. .test_ib = cik_sdma_ring_test_ib,
  1146. .insert_nop = cik_sdma_ring_insert_nop,
  1147. };
  1148. static void cik_sdma_set_ring_funcs(struct amdgpu_device *adev)
  1149. {
  1150. int i;
  1151. for (i = 0; i < adev->sdma.num_instances; i++)
  1152. adev->sdma.instance[i].ring.funcs = &cik_sdma_ring_funcs;
  1153. }
  1154. static const struct amdgpu_irq_src_funcs cik_sdma_trap_irq_funcs = {
  1155. .set = cik_sdma_set_trap_irq_state,
  1156. .process = cik_sdma_process_trap_irq,
  1157. };
  1158. static const struct amdgpu_irq_src_funcs cik_sdma_illegal_inst_irq_funcs = {
  1159. .process = cik_sdma_process_illegal_inst_irq,
  1160. };
  1161. static void cik_sdma_set_irq_funcs(struct amdgpu_device *adev)
  1162. {
  1163. adev->sdma.trap_irq.num_types = AMDGPU_SDMA_IRQ_LAST;
  1164. adev->sdma.trap_irq.funcs = &cik_sdma_trap_irq_funcs;
  1165. adev->sdma.illegal_inst_irq.funcs = &cik_sdma_illegal_inst_irq_funcs;
  1166. }
  1167. /**
  1168. * cik_sdma_emit_copy_buffer - copy buffer using the sDMA engine
  1169. *
  1170. * @ring: amdgpu_ring structure holding ring information
  1171. * @src_offset: src GPU address
  1172. * @dst_offset: dst GPU address
  1173. * @byte_count: number of bytes to xfer
  1174. *
  1175. * Copy GPU buffers using the DMA engine (CIK).
  1176. * Used by the amdgpu ttm implementation to move pages if
  1177. * registered as the asic copy callback.
  1178. */
  1179. static void cik_sdma_emit_copy_buffer(struct amdgpu_ib *ib,
  1180. uint64_t src_offset,
  1181. uint64_t dst_offset,
  1182. uint32_t byte_count)
  1183. {
  1184. ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY, SDMA_COPY_SUB_OPCODE_LINEAR, 0);
  1185. ib->ptr[ib->length_dw++] = byte_count;
  1186. ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
  1187. ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
  1188. ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
  1189. ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
  1190. ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
  1191. }
  1192. /**
  1193. * cik_sdma_emit_fill_buffer - fill buffer using the sDMA engine
  1194. *
  1195. * @ring: amdgpu_ring structure holding ring information
  1196. * @src_data: value to write to buffer
  1197. * @dst_offset: dst GPU address
  1198. * @byte_count: number of bytes to xfer
  1199. *
  1200. * Fill GPU buffers using the DMA engine (CIK).
  1201. */
  1202. static void cik_sdma_emit_fill_buffer(struct amdgpu_ib *ib,
  1203. uint32_t src_data,
  1204. uint64_t dst_offset,
  1205. uint32_t byte_count)
  1206. {
  1207. ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_CONSTANT_FILL, 0, 0);
  1208. ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
  1209. ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
  1210. ib->ptr[ib->length_dw++] = src_data;
  1211. ib->ptr[ib->length_dw++] = byte_count;
  1212. }
  1213. static const struct amdgpu_buffer_funcs cik_sdma_buffer_funcs = {
  1214. .copy_max_bytes = 0x1fffff,
  1215. .copy_num_dw = 7,
  1216. .emit_copy_buffer = cik_sdma_emit_copy_buffer,
  1217. .fill_max_bytes = 0x1fffff,
  1218. .fill_num_dw = 5,
  1219. .emit_fill_buffer = cik_sdma_emit_fill_buffer,
  1220. };
  1221. static void cik_sdma_set_buffer_funcs(struct amdgpu_device *adev)
  1222. {
  1223. if (adev->mman.buffer_funcs == NULL) {
  1224. adev->mman.buffer_funcs = &cik_sdma_buffer_funcs;
  1225. adev->mman.buffer_funcs_ring = &adev->sdma.instance[0].ring;
  1226. }
  1227. }
  1228. static const struct amdgpu_vm_pte_funcs cik_sdma_vm_pte_funcs = {
  1229. .copy_pte = cik_sdma_vm_copy_pte,
  1230. .write_pte = cik_sdma_vm_write_pte,
  1231. .set_pte_pde = cik_sdma_vm_set_pte_pde,
  1232. .pad_ib = cik_sdma_vm_pad_ib,
  1233. };
  1234. static void cik_sdma_set_vm_pte_funcs(struct amdgpu_device *adev)
  1235. {
  1236. if (adev->vm_manager.vm_pte_funcs == NULL) {
  1237. adev->vm_manager.vm_pte_funcs = &cik_sdma_vm_pte_funcs;
  1238. adev->vm_manager.vm_pte_funcs_ring = &adev->sdma.instance[0].ring;
  1239. adev->vm_manager.vm_pte_funcs_ring->is_pte_ring = true;
  1240. }
  1241. }