sdma_v2_4.c 38 KB

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