sdma_v2_4.c 39 KB

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