sdma_v2_4.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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_8_1_d.h"
  34. #include "gmc/gmc_8_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_gfx_stop - stop the gfx async dma engines
  298. *
  299. * @adev: amdgpu_device pointer
  300. *
  301. * Stop the gfx async dma ring buffers (VI).
  302. */
  303. static void sdma_v2_4_gfx_stop(struct amdgpu_device *adev)
  304. {
  305. struct amdgpu_ring *sdma0 = &adev->sdma.instance[0].ring;
  306. struct amdgpu_ring *sdma1 = &adev->sdma.instance[1].ring;
  307. u32 rb_cntl, ib_cntl;
  308. int i;
  309. if ((adev->mman.buffer_funcs_ring == sdma0) ||
  310. (adev->mman.buffer_funcs_ring == sdma1))
  311. amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
  312. for (i = 0; i < adev->sdma.num_instances; i++) {
  313. rb_cntl = RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]);
  314. rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 0);
  315. WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
  316. ib_cntl = RREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i]);
  317. ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 0);
  318. WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], ib_cntl);
  319. }
  320. sdma0->ready = false;
  321. sdma1->ready = false;
  322. }
  323. /**
  324. * sdma_v2_4_rlc_stop - stop the compute async dma engines
  325. *
  326. * @adev: amdgpu_device pointer
  327. *
  328. * Stop the compute async dma queues (VI).
  329. */
  330. static void sdma_v2_4_rlc_stop(struct amdgpu_device *adev)
  331. {
  332. /* XXX todo */
  333. }
  334. /**
  335. * sdma_v2_4_enable - stop the async dma engines
  336. *
  337. * @adev: amdgpu_device pointer
  338. * @enable: enable/disable the DMA MEs.
  339. *
  340. * Halt or unhalt the async dma engines (VI).
  341. */
  342. static void sdma_v2_4_enable(struct amdgpu_device *adev, bool enable)
  343. {
  344. u32 f32_cntl;
  345. int i;
  346. if (enable == false) {
  347. sdma_v2_4_gfx_stop(adev);
  348. sdma_v2_4_rlc_stop(adev);
  349. }
  350. for (i = 0; i < adev->sdma.num_instances; i++) {
  351. f32_cntl = RREG32(mmSDMA0_F32_CNTL + sdma_offsets[i]);
  352. if (enable)
  353. f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, 0);
  354. else
  355. f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, 1);
  356. WREG32(mmSDMA0_F32_CNTL + sdma_offsets[i], f32_cntl);
  357. }
  358. }
  359. /**
  360. * sdma_v2_4_gfx_resume - setup and start the async dma engines
  361. *
  362. * @adev: amdgpu_device pointer
  363. *
  364. * Set up the gfx DMA ring buffers and enable them (VI).
  365. * Returns 0 for success, error for failure.
  366. */
  367. static int sdma_v2_4_gfx_resume(struct amdgpu_device *adev)
  368. {
  369. struct amdgpu_ring *ring;
  370. u32 rb_cntl, ib_cntl;
  371. u32 rb_bufsz;
  372. u32 wb_offset;
  373. int i, j, r;
  374. for (i = 0; i < adev->sdma.num_instances; i++) {
  375. ring = &adev->sdma.instance[i].ring;
  376. wb_offset = (ring->rptr_offs * 4);
  377. mutex_lock(&adev->srbm_mutex);
  378. for (j = 0; j < 16; j++) {
  379. vi_srbm_select(adev, 0, 0, 0, j);
  380. /* SDMA GFX */
  381. WREG32(mmSDMA0_GFX_VIRTUAL_ADDR + sdma_offsets[i], 0);
  382. WREG32(mmSDMA0_GFX_APE1_CNTL + sdma_offsets[i], 0);
  383. }
  384. vi_srbm_select(adev, 0, 0, 0, 0);
  385. mutex_unlock(&adev->srbm_mutex);
  386. WREG32(mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL + sdma_offsets[i], 0);
  387. /* Set ring buffer size in dwords */
  388. rb_bufsz = order_base_2(ring->ring_size / 4);
  389. rb_cntl = RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]);
  390. rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SIZE, rb_bufsz);
  391. #ifdef __BIG_ENDIAN
  392. rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SWAP_ENABLE, 1);
  393. rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL,
  394. RPTR_WRITEBACK_SWAP_ENABLE, 1);
  395. #endif
  396. WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
  397. /* Initialize the ring buffer's read and write pointers */
  398. WREG32(mmSDMA0_GFX_RB_RPTR + sdma_offsets[i], 0);
  399. WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], 0);
  400. /* set the wb address whether it's enabled or not */
  401. WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_HI + sdma_offsets[i],
  402. upper_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFF);
  403. WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_LO + sdma_offsets[i],
  404. lower_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC);
  405. rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RPTR_WRITEBACK_ENABLE, 1);
  406. WREG32(mmSDMA0_GFX_RB_BASE + sdma_offsets[i], ring->gpu_addr >> 8);
  407. WREG32(mmSDMA0_GFX_RB_BASE_HI + sdma_offsets[i], ring->gpu_addr >> 40);
  408. ring->wptr = 0;
  409. WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], ring->wptr << 2);
  410. /* enable DMA RB */
  411. rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 1);
  412. WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
  413. ib_cntl = RREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i]);
  414. ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 1);
  415. #ifdef __BIG_ENDIAN
  416. ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_SWAP_ENABLE, 1);
  417. #endif
  418. /* enable DMA IBs */
  419. WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], ib_cntl);
  420. ring->ready = true;
  421. r = amdgpu_ring_test_ring(ring);
  422. if (r) {
  423. ring->ready = false;
  424. return r;
  425. }
  426. if (adev->mman.buffer_funcs_ring == ring)
  427. amdgpu_ttm_set_active_vram_size(adev, adev->mc.real_vram_size);
  428. }
  429. return 0;
  430. }
  431. /**
  432. * sdma_v2_4_rlc_resume - setup and start the async dma engines
  433. *
  434. * @adev: amdgpu_device pointer
  435. *
  436. * Set up the compute DMA queues and enable them (VI).
  437. * Returns 0 for success, error for failure.
  438. */
  439. static int sdma_v2_4_rlc_resume(struct amdgpu_device *adev)
  440. {
  441. /* XXX todo */
  442. return 0;
  443. }
  444. /**
  445. * sdma_v2_4_load_microcode - load the sDMA ME ucode
  446. *
  447. * @adev: amdgpu_device pointer
  448. *
  449. * Loads the sDMA0/1 ucode.
  450. * Returns 0 for success, -EINVAL if the ucode is not available.
  451. */
  452. static int sdma_v2_4_load_microcode(struct amdgpu_device *adev)
  453. {
  454. const struct sdma_firmware_header_v1_0 *hdr;
  455. const __le32 *fw_data;
  456. u32 fw_size;
  457. int i, j;
  458. /* halt the MEs */
  459. sdma_v2_4_enable(adev, false);
  460. for (i = 0; i < adev->sdma.num_instances; i++) {
  461. if (!adev->sdma.instance[i].fw)
  462. return -EINVAL;
  463. hdr = (const struct sdma_firmware_header_v1_0 *)adev->sdma.instance[i].fw->data;
  464. amdgpu_ucode_print_sdma_hdr(&hdr->header);
  465. fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
  466. fw_data = (const __le32 *)
  467. (adev->sdma.instance[i].fw->data +
  468. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  469. WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], 0);
  470. for (j = 0; j < fw_size; j++)
  471. WREG32(mmSDMA0_UCODE_DATA + sdma_offsets[i], le32_to_cpup(fw_data++));
  472. WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], adev->sdma.instance[i].fw_version);
  473. }
  474. return 0;
  475. }
  476. /**
  477. * sdma_v2_4_start - setup and start the async dma engines
  478. *
  479. * @adev: amdgpu_device pointer
  480. *
  481. * Set up the DMA engines and enable them (VI).
  482. * Returns 0 for success, error for failure.
  483. */
  484. static int sdma_v2_4_start(struct amdgpu_device *adev)
  485. {
  486. int r;
  487. if (!adev->firmware.smu_load) {
  488. r = sdma_v2_4_load_microcode(adev);
  489. if (r)
  490. return r;
  491. } else {
  492. r = adev->smu.smumgr_funcs->check_fw_load_finish(adev,
  493. AMDGPU_UCODE_ID_SDMA0);
  494. if (r)
  495. return -EINVAL;
  496. r = adev->smu.smumgr_funcs->check_fw_load_finish(adev,
  497. AMDGPU_UCODE_ID_SDMA1);
  498. if (r)
  499. return -EINVAL;
  500. }
  501. /* unhalt the MEs */
  502. sdma_v2_4_enable(adev, true);
  503. /* start the gfx rings and rlc compute queues */
  504. r = sdma_v2_4_gfx_resume(adev);
  505. if (r)
  506. return r;
  507. r = sdma_v2_4_rlc_resume(adev);
  508. if (r)
  509. return r;
  510. return 0;
  511. }
  512. /**
  513. * sdma_v2_4_ring_test_ring - simple async dma engine test
  514. *
  515. * @ring: amdgpu_ring structure holding ring information
  516. *
  517. * Test the DMA engine by writing using it to write an
  518. * value to memory. (VI).
  519. * Returns 0 for success, error for failure.
  520. */
  521. static int sdma_v2_4_ring_test_ring(struct amdgpu_ring *ring)
  522. {
  523. struct amdgpu_device *adev = ring->adev;
  524. unsigned i;
  525. unsigned index;
  526. int r;
  527. u32 tmp;
  528. u64 gpu_addr;
  529. r = amdgpu_wb_get(adev, &index);
  530. if (r) {
  531. dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
  532. return r;
  533. }
  534. gpu_addr = adev->wb.gpu_addr + (index * 4);
  535. tmp = 0xCAFEDEAD;
  536. adev->wb.wb[index] = cpu_to_le32(tmp);
  537. r = amdgpu_ring_lock(ring, 5);
  538. if (r) {
  539. DRM_ERROR("amdgpu: dma failed to lock ring %d (%d).\n", ring->idx, r);
  540. amdgpu_wb_free(adev, index);
  541. return r;
  542. }
  543. amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
  544. SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR));
  545. amdgpu_ring_write(ring, lower_32_bits(gpu_addr));
  546. amdgpu_ring_write(ring, upper_32_bits(gpu_addr));
  547. amdgpu_ring_write(ring, SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(1));
  548. amdgpu_ring_write(ring, 0xDEADBEEF);
  549. amdgpu_ring_unlock_commit(ring);
  550. for (i = 0; i < adev->usec_timeout; i++) {
  551. tmp = le32_to_cpu(adev->wb.wb[index]);
  552. if (tmp == 0xDEADBEEF)
  553. break;
  554. DRM_UDELAY(1);
  555. }
  556. if (i < adev->usec_timeout) {
  557. DRM_INFO("ring test on %d succeeded in %d usecs\n", ring->idx, i);
  558. } else {
  559. DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
  560. ring->idx, tmp);
  561. r = -EINVAL;
  562. }
  563. amdgpu_wb_free(adev, index);
  564. return r;
  565. }
  566. /**
  567. * sdma_v2_4_ring_test_ib - test an IB on the DMA engine
  568. *
  569. * @ring: amdgpu_ring structure holding ring information
  570. *
  571. * Test a simple IB in the DMA ring (VI).
  572. * Returns 0 on success, error on failure.
  573. */
  574. static int sdma_v2_4_ring_test_ib(struct amdgpu_ring *ring)
  575. {
  576. struct amdgpu_device *adev = ring->adev;
  577. struct amdgpu_ib ib;
  578. struct fence *f = NULL;
  579. unsigned i;
  580. unsigned index;
  581. int r;
  582. u32 tmp = 0;
  583. u64 gpu_addr;
  584. r = amdgpu_wb_get(adev, &index);
  585. if (r) {
  586. dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
  587. return r;
  588. }
  589. gpu_addr = adev->wb.gpu_addr + (index * 4);
  590. tmp = 0xCAFEDEAD;
  591. adev->wb.wb[index] = cpu_to_le32(tmp);
  592. memset(&ib, 0, sizeof(ib));
  593. r = amdgpu_ib_get(ring, NULL, 256, &ib);
  594. if (r) {
  595. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  596. goto err0;
  597. }
  598. ib.ptr[0] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
  599. SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
  600. ib.ptr[1] = lower_32_bits(gpu_addr);
  601. ib.ptr[2] = upper_32_bits(gpu_addr);
  602. ib.ptr[3] = SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(1);
  603. ib.ptr[4] = 0xDEADBEEF;
  604. ib.ptr[5] = SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
  605. ib.ptr[6] = SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
  606. ib.ptr[7] = SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
  607. ib.length_dw = 8;
  608. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, &ib, 1, NULL,
  609. AMDGPU_FENCE_OWNER_UNDEFINED,
  610. &f);
  611. if (r)
  612. goto err1;
  613. r = fence_wait(f, false);
  614. if (r) {
  615. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  616. goto err1;
  617. }
  618. for (i = 0; i < adev->usec_timeout; i++) {
  619. tmp = le32_to_cpu(adev->wb.wb[index]);
  620. if (tmp == 0xDEADBEEF)
  621. break;
  622. DRM_UDELAY(1);
  623. }
  624. if (i < adev->usec_timeout) {
  625. DRM_INFO("ib test on ring %d succeeded in %u usecs\n",
  626. ring->idx, i);
  627. goto err1;
  628. } else {
  629. DRM_ERROR("amdgpu: ib test failed (0x%08X)\n", tmp);
  630. r = -EINVAL;
  631. }
  632. err1:
  633. fence_put(f);
  634. amdgpu_ib_free(adev, &ib);
  635. err0:
  636. amdgpu_wb_free(adev, index);
  637. return r;
  638. }
  639. /**
  640. * sdma_v2_4_vm_copy_pte - update PTEs by copying them from the GART
  641. *
  642. * @ib: indirect buffer to fill with commands
  643. * @pe: addr of the page entry
  644. * @src: src addr to copy from
  645. * @count: number of page entries to update
  646. *
  647. * Update PTEs by copying them from the GART using sDMA (CIK).
  648. */
  649. static void sdma_v2_4_vm_copy_pte(struct amdgpu_ib *ib,
  650. uint64_t pe, uint64_t src,
  651. unsigned count)
  652. {
  653. while (count) {
  654. unsigned bytes = count * 8;
  655. if (bytes > 0x1FFFF8)
  656. bytes = 0x1FFFF8;
  657. ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
  658. SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
  659. ib->ptr[ib->length_dw++] = bytes;
  660. ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
  661. ib->ptr[ib->length_dw++] = lower_32_bits(src);
  662. ib->ptr[ib->length_dw++] = upper_32_bits(src);
  663. ib->ptr[ib->length_dw++] = lower_32_bits(pe);
  664. ib->ptr[ib->length_dw++] = upper_32_bits(pe);
  665. pe += bytes;
  666. src += bytes;
  667. count -= bytes / 8;
  668. }
  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. * @addr: dst addr to write into pe
  676. * @count: number of page entries to update
  677. * @incr: increase next addr by incr bytes
  678. * @flags: access flags
  679. *
  680. * Update PTEs by writing them manually using sDMA (CIK).
  681. */
  682. static void sdma_v2_4_vm_write_pte(struct amdgpu_ib *ib,
  683. uint64_t pe,
  684. uint64_t addr, unsigned count,
  685. uint32_t incr, uint32_t flags)
  686. {
  687. uint64_t value;
  688. unsigned ndw;
  689. while (count) {
  690. ndw = count * 2;
  691. if (ndw > 0xFFFFE)
  692. ndw = 0xFFFFE;
  693. /* for non-physically contiguous pages (system) */
  694. ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
  695. SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
  696. ib->ptr[ib->length_dw++] = pe;
  697. ib->ptr[ib->length_dw++] = upper_32_bits(pe);
  698. ib->ptr[ib->length_dw++] = ndw;
  699. for (; ndw > 0; ndw -= 2, --count, pe += 8) {
  700. if (flags & AMDGPU_PTE_SYSTEM) {
  701. value = amdgpu_vm_map_gart(ib->ring->adev, addr);
  702. value &= 0xFFFFFFFFFFFFF000ULL;
  703. } else if (flags & AMDGPU_PTE_VALID) {
  704. value = addr;
  705. } else {
  706. value = 0;
  707. }
  708. addr += incr;
  709. value |= flags;
  710. ib->ptr[ib->length_dw++] = value;
  711. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  712. }
  713. }
  714. }
  715. /**
  716. * sdma_v2_4_vm_set_pte_pde - update the page tables using sDMA
  717. *
  718. * @ib: indirect buffer to fill with commands
  719. * @pe: addr of the page entry
  720. * @addr: dst addr to write into pe
  721. * @count: number of page entries to update
  722. * @incr: increase next addr by incr bytes
  723. * @flags: access flags
  724. *
  725. * Update the page tables using sDMA (CIK).
  726. */
  727. static void sdma_v2_4_vm_set_pte_pde(struct amdgpu_ib *ib,
  728. uint64_t pe,
  729. uint64_t addr, unsigned count,
  730. uint32_t incr, uint32_t flags)
  731. {
  732. uint64_t value;
  733. unsigned ndw;
  734. while (count) {
  735. ndw = count;
  736. if (ndw > 0x7FFFF)
  737. ndw = 0x7FFFF;
  738. if (flags & AMDGPU_PTE_VALID)
  739. value = addr;
  740. else
  741. value = 0;
  742. /* for physically contiguous pages (vram) */
  743. ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_GEN_PTEPDE);
  744. ib->ptr[ib->length_dw++] = pe; /* dst addr */
  745. ib->ptr[ib->length_dw++] = upper_32_bits(pe);
  746. ib->ptr[ib->length_dw++] = flags; /* mask */
  747. ib->ptr[ib->length_dw++] = 0;
  748. ib->ptr[ib->length_dw++] = value; /* value */
  749. ib->ptr[ib->length_dw++] = upper_32_bits(value);
  750. ib->ptr[ib->length_dw++] = incr; /* increment size */
  751. ib->ptr[ib->length_dw++] = 0;
  752. ib->ptr[ib->length_dw++] = ndw; /* number of entries */
  753. pe += ndw * 8;
  754. addr += ndw * incr;
  755. count -= ndw;
  756. }
  757. }
  758. /**
  759. * sdma_v2_4_vm_pad_ib - pad the IB to the required number of dw
  760. *
  761. * @ib: indirect buffer to fill with padding
  762. *
  763. */
  764. static void sdma_v2_4_vm_pad_ib(struct amdgpu_ib *ib)
  765. {
  766. struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ib->ring);
  767. u32 pad_count;
  768. int i;
  769. pad_count = (8 - (ib->length_dw & 0x7)) % 8;
  770. for (i = 0; i < pad_count; i++)
  771. if (sdma && sdma->burst_nop && (i == 0))
  772. ib->ptr[ib->length_dw++] =
  773. SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
  774. SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
  775. else
  776. ib->ptr[ib->length_dw++] =
  777. SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
  778. }
  779. /**
  780. * sdma_v2_4_ring_emit_vm_flush - cik vm flush using sDMA
  781. *
  782. * @ring: amdgpu_ring pointer
  783. * @vm: amdgpu_vm pointer
  784. *
  785. * Update the page table base and flush the VM TLB
  786. * using sDMA (VI).
  787. */
  788. static void sdma_v2_4_ring_emit_vm_flush(struct amdgpu_ring *ring,
  789. unsigned vm_id, uint64_t pd_addr)
  790. {
  791. amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_SRBM_WRITE) |
  792. SDMA_PKT_SRBM_WRITE_HEADER_BYTE_EN(0xf));
  793. if (vm_id < 8) {
  794. amdgpu_ring_write(ring, (mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR + vm_id));
  795. } else {
  796. amdgpu_ring_write(ring, (mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + vm_id - 8));
  797. }
  798. amdgpu_ring_write(ring, pd_addr >> 12);
  799. /* flush TLB */
  800. amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_SRBM_WRITE) |
  801. SDMA_PKT_SRBM_WRITE_HEADER_BYTE_EN(0xf));
  802. amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST);
  803. amdgpu_ring_write(ring, 1 << vm_id);
  804. /* wait for flush */
  805. amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
  806. SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(0) |
  807. SDMA_PKT_POLL_REGMEM_HEADER_FUNC(0)); /* always */
  808. amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST << 2);
  809. amdgpu_ring_write(ring, 0);
  810. amdgpu_ring_write(ring, 0); /* reference */
  811. amdgpu_ring_write(ring, 0); /* mask */
  812. amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
  813. SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(10)); /* retry count, poll interval */
  814. }
  815. static int sdma_v2_4_early_init(void *handle)
  816. {
  817. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  818. adev->sdma.num_instances = SDMA_MAX_INSTANCE;
  819. sdma_v2_4_set_ring_funcs(adev);
  820. sdma_v2_4_set_buffer_funcs(adev);
  821. sdma_v2_4_set_vm_pte_funcs(adev);
  822. sdma_v2_4_set_irq_funcs(adev);
  823. return 0;
  824. }
  825. static int sdma_v2_4_sw_init(void *handle)
  826. {
  827. struct amdgpu_ring *ring;
  828. int r, i;
  829. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  830. /* SDMA trap event */
  831. r = amdgpu_irq_add_id(adev, 224, &adev->sdma.trap_irq);
  832. if (r)
  833. return r;
  834. /* SDMA Privileged inst */
  835. r = amdgpu_irq_add_id(adev, 241, &adev->sdma.illegal_inst_irq);
  836. if (r)
  837. return r;
  838. /* SDMA Privileged inst */
  839. r = amdgpu_irq_add_id(adev, 247, &adev->sdma.illegal_inst_irq);
  840. if (r)
  841. return r;
  842. r = sdma_v2_4_init_microcode(adev);
  843. if (r) {
  844. DRM_ERROR("Failed to load sdma firmware!\n");
  845. return r;
  846. }
  847. for (i = 0; i < adev->sdma.num_instances; i++) {
  848. ring = &adev->sdma.instance[i].ring;
  849. ring->ring_obj = NULL;
  850. ring->use_doorbell = false;
  851. sprintf(ring->name, "sdma%d", i);
  852. r = amdgpu_ring_init(adev, ring, 256 * 1024,
  853. SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP), 0xf,
  854. &adev->sdma.trap_irq,
  855. (i == 0) ?
  856. AMDGPU_SDMA_IRQ_TRAP0 : AMDGPU_SDMA_IRQ_TRAP1,
  857. AMDGPU_RING_TYPE_SDMA);
  858. if (r)
  859. return r;
  860. }
  861. return r;
  862. }
  863. static int sdma_v2_4_sw_fini(void *handle)
  864. {
  865. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  866. int i;
  867. for (i = 0; i < adev->sdma.num_instances; i++)
  868. amdgpu_ring_fini(&adev->sdma.instance[i].ring);
  869. return 0;
  870. }
  871. static int sdma_v2_4_hw_init(void *handle)
  872. {
  873. int r;
  874. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  875. sdma_v2_4_init_golden_registers(adev);
  876. r = sdma_v2_4_start(adev);
  877. if (r)
  878. return r;
  879. return r;
  880. }
  881. static int sdma_v2_4_hw_fini(void *handle)
  882. {
  883. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  884. sdma_v2_4_enable(adev, false);
  885. return 0;
  886. }
  887. static int sdma_v2_4_suspend(void *handle)
  888. {
  889. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  890. return sdma_v2_4_hw_fini(adev);
  891. }
  892. static int sdma_v2_4_resume(void *handle)
  893. {
  894. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  895. return sdma_v2_4_hw_init(adev);
  896. }
  897. static bool sdma_v2_4_is_idle(void *handle)
  898. {
  899. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  900. u32 tmp = RREG32(mmSRBM_STATUS2);
  901. if (tmp & (SRBM_STATUS2__SDMA_BUSY_MASK |
  902. SRBM_STATUS2__SDMA1_BUSY_MASK))
  903. return false;
  904. return true;
  905. }
  906. static int sdma_v2_4_wait_for_idle(void *handle)
  907. {
  908. unsigned i;
  909. u32 tmp;
  910. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  911. for (i = 0; i < adev->usec_timeout; i++) {
  912. tmp = RREG32(mmSRBM_STATUS2) & (SRBM_STATUS2__SDMA_BUSY_MASK |
  913. SRBM_STATUS2__SDMA1_BUSY_MASK);
  914. if (!tmp)
  915. return 0;
  916. udelay(1);
  917. }
  918. return -ETIMEDOUT;
  919. }
  920. static void sdma_v2_4_print_status(void *handle)
  921. {
  922. int i, j;
  923. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  924. dev_info(adev->dev, "VI SDMA registers\n");
  925. dev_info(adev->dev, " SRBM_STATUS2=0x%08X\n",
  926. RREG32(mmSRBM_STATUS2));
  927. for (i = 0; i < adev->sdma.num_instances; i++) {
  928. dev_info(adev->dev, " SDMA%d_STATUS_REG=0x%08X\n",
  929. i, RREG32(mmSDMA0_STATUS_REG + sdma_offsets[i]));
  930. dev_info(adev->dev, " SDMA%d_F32_CNTL=0x%08X\n",
  931. i, RREG32(mmSDMA0_F32_CNTL + sdma_offsets[i]));
  932. dev_info(adev->dev, " SDMA%d_CNTL=0x%08X\n",
  933. i, RREG32(mmSDMA0_CNTL + sdma_offsets[i]));
  934. dev_info(adev->dev, " SDMA%d_SEM_WAIT_FAIL_TIMER_CNTL=0x%08X\n",
  935. i, RREG32(mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL + sdma_offsets[i]));
  936. dev_info(adev->dev, " SDMA%d_GFX_IB_CNTL=0x%08X\n",
  937. i, RREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i]));
  938. dev_info(adev->dev, " SDMA%d_GFX_RB_CNTL=0x%08X\n",
  939. i, RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]));
  940. dev_info(adev->dev, " SDMA%d_GFX_RB_RPTR=0x%08X\n",
  941. i, RREG32(mmSDMA0_GFX_RB_RPTR + sdma_offsets[i]));
  942. dev_info(adev->dev, " SDMA%d_GFX_RB_WPTR=0x%08X\n",
  943. i, RREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i]));
  944. dev_info(adev->dev, " SDMA%d_GFX_RB_RPTR_ADDR_HI=0x%08X\n",
  945. i, RREG32(mmSDMA0_GFX_RB_RPTR_ADDR_HI + sdma_offsets[i]));
  946. dev_info(adev->dev, " SDMA%d_GFX_RB_RPTR_ADDR_LO=0x%08X\n",
  947. i, RREG32(mmSDMA0_GFX_RB_RPTR_ADDR_LO + sdma_offsets[i]));
  948. dev_info(adev->dev, " SDMA%d_GFX_RB_BASE=0x%08X\n",
  949. i, RREG32(mmSDMA0_GFX_RB_BASE + sdma_offsets[i]));
  950. dev_info(adev->dev, " SDMA%d_GFX_RB_BASE_HI=0x%08X\n",
  951. i, RREG32(mmSDMA0_GFX_RB_BASE_HI + sdma_offsets[i]));
  952. mutex_lock(&adev->srbm_mutex);
  953. for (j = 0; j < 16; j++) {
  954. vi_srbm_select(adev, 0, 0, 0, j);
  955. dev_info(adev->dev, " VM %d:\n", j);
  956. dev_info(adev->dev, " SDMA%d_GFX_VIRTUAL_ADDR=0x%08X\n",
  957. i, RREG32(mmSDMA0_GFX_VIRTUAL_ADDR + sdma_offsets[i]));
  958. dev_info(adev->dev, " SDMA%d_GFX_APE1_CNTL=0x%08X\n",
  959. i, RREG32(mmSDMA0_GFX_APE1_CNTL + sdma_offsets[i]));
  960. }
  961. vi_srbm_select(adev, 0, 0, 0, 0);
  962. mutex_unlock(&adev->srbm_mutex);
  963. }
  964. }
  965. static int sdma_v2_4_soft_reset(void *handle)
  966. {
  967. u32 srbm_soft_reset = 0;
  968. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  969. u32 tmp = RREG32(mmSRBM_STATUS2);
  970. if (tmp & SRBM_STATUS2__SDMA_BUSY_MASK) {
  971. /* sdma0 */
  972. tmp = RREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET);
  973. tmp = REG_SET_FIELD(tmp, SDMA0_F32_CNTL, HALT, 0);
  974. WREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET, tmp);
  975. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA_MASK;
  976. }
  977. if (tmp & SRBM_STATUS2__SDMA1_BUSY_MASK) {
  978. /* sdma1 */
  979. tmp = RREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET);
  980. tmp = REG_SET_FIELD(tmp, SDMA0_F32_CNTL, HALT, 0);
  981. WREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET, tmp);
  982. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA1_MASK;
  983. }
  984. if (srbm_soft_reset) {
  985. sdma_v2_4_print_status((void *)adev);
  986. tmp = RREG32(mmSRBM_SOFT_RESET);
  987. tmp |= srbm_soft_reset;
  988. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  989. WREG32(mmSRBM_SOFT_RESET, tmp);
  990. tmp = RREG32(mmSRBM_SOFT_RESET);
  991. udelay(50);
  992. tmp &= ~srbm_soft_reset;
  993. WREG32(mmSRBM_SOFT_RESET, tmp);
  994. tmp = RREG32(mmSRBM_SOFT_RESET);
  995. /* Wait a little for things to settle down */
  996. udelay(50);
  997. sdma_v2_4_print_status((void *)adev);
  998. }
  999. return 0;
  1000. }
  1001. static int sdma_v2_4_set_trap_irq_state(struct amdgpu_device *adev,
  1002. struct amdgpu_irq_src *src,
  1003. unsigned type,
  1004. enum amdgpu_interrupt_state state)
  1005. {
  1006. u32 sdma_cntl;
  1007. switch (type) {
  1008. case AMDGPU_SDMA_IRQ_TRAP0:
  1009. switch (state) {
  1010. case AMDGPU_IRQ_STATE_DISABLE:
  1011. sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
  1012. sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 0);
  1013. WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
  1014. break;
  1015. case AMDGPU_IRQ_STATE_ENABLE:
  1016. sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
  1017. sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 1);
  1018. WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
  1019. break;
  1020. default:
  1021. break;
  1022. }
  1023. break;
  1024. case AMDGPU_SDMA_IRQ_TRAP1:
  1025. switch (state) {
  1026. case AMDGPU_IRQ_STATE_DISABLE:
  1027. sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
  1028. sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 0);
  1029. WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
  1030. break;
  1031. case AMDGPU_IRQ_STATE_ENABLE:
  1032. sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
  1033. sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE, 1);
  1034. WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
  1035. break;
  1036. default:
  1037. break;
  1038. }
  1039. break;
  1040. default:
  1041. break;
  1042. }
  1043. return 0;
  1044. }
  1045. static int sdma_v2_4_process_trap_irq(struct amdgpu_device *adev,
  1046. struct amdgpu_irq_src *source,
  1047. struct amdgpu_iv_entry *entry)
  1048. {
  1049. u8 instance_id, queue_id;
  1050. instance_id = (entry->ring_id & 0x3) >> 0;
  1051. queue_id = (entry->ring_id & 0xc) >> 2;
  1052. DRM_DEBUG("IH: SDMA trap\n");
  1053. switch (instance_id) {
  1054. case 0:
  1055. switch (queue_id) {
  1056. case 0:
  1057. amdgpu_fence_process(&adev->sdma.instance[0].ring);
  1058. break;
  1059. case 1:
  1060. /* XXX compute */
  1061. break;
  1062. case 2:
  1063. /* XXX compute */
  1064. break;
  1065. }
  1066. break;
  1067. case 1:
  1068. switch (queue_id) {
  1069. case 0:
  1070. amdgpu_fence_process(&adev->sdma.instance[1].ring);
  1071. break;
  1072. case 1:
  1073. /* XXX compute */
  1074. break;
  1075. case 2:
  1076. /* XXX compute */
  1077. break;
  1078. }
  1079. break;
  1080. }
  1081. return 0;
  1082. }
  1083. static int sdma_v2_4_process_illegal_inst_irq(struct amdgpu_device *adev,
  1084. struct amdgpu_irq_src *source,
  1085. struct amdgpu_iv_entry *entry)
  1086. {
  1087. DRM_ERROR("Illegal instruction in SDMA command stream\n");
  1088. schedule_work(&adev->reset_work);
  1089. return 0;
  1090. }
  1091. static int sdma_v2_4_set_clockgating_state(void *handle,
  1092. enum amd_clockgating_state state)
  1093. {
  1094. /* XXX handled via the smc on VI */
  1095. return 0;
  1096. }
  1097. static int sdma_v2_4_set_powergating_state(void *handle,
  1098. enum amd_powergating_state state)
  1099. {
  1100. return 0;
  1101. }
  1102. const struct amd_ip_funcs sdma_v2_4_ip_funcs = {
  1103. .early_init = sdma_v2_4_early_init,
  1104. .late_init = NULL,
  1105. .sw_init = sdma_v2_4_sw_init,
  1106. .sw_fini = sdma_v2_4_sw_fini,
  1107. .hw_init = sdma_v2_4_hw_init,
  1108. .hw_fini = sdma_v2_4_hw_fini,
  1109. .suspend = sdma_v2_4_suspend,
  1110. .resume = sdma_v2_4_resume,
  1111. .is_idle = sdma_v2_4_is_idle,
  1112. .wait_for_idle = sdma_v2_4_wait_for_idle,
  1113. .soft_reset = sdma_v2_4_soft_reset,
  1114. .print_status = sdma_v2_4_print_status,
  1115. .set_clockgating_state = sdma_v2_4_set_clockgating_state,
  1116. .set_powergating_state = sdma_v2_4_set_powergating_state,
  1117. };
  1118. static const struct amdgpu_ring_funcs sdma_v2_4_ring_funcs = {
  1119. .get_rptr = sdma_v2_4_ring_get_rptr,
  1120. .get_wptr = sdma_v2_4_ring_get_wptr,
  1121. .set_wptr = sdma_v2_4_ring_set_wptr,
  1122. .parse_cs = NULL,
  1123. .emit_ib = sdma_v2_4_ring_emit_ib,
  1124. .emit_fence = sdma_v2_4_ring_emit_fence,
  1125. .emit_vm_flush = sdma_v2_4_ring_emit_vm_flush,
  1126. .emit_hdp_flush = sdma_v2_4_ring_emit_hdp_flush,
  1127. .test_ring = sdma_v2_4_ring_test_ring,
  1128. .test_ib = sdma_v2_4_ring_test_ib,
  1129. .insert_nop = sdma_v2_4_ring_insert_nop,
  1130. };
  1131. static void sdma_v2_4_set_ring_funcs(struct amdgpu_device *adev)
  1132. {
  1133. int i;
  1134. for (i = 0; i < adev->sdma.num_instances; i++)
  1135. adev->sdma.instance[i].ring.funcs = &sdma_v2_4_ring_funcs;
  1136. }
  1137. static const struct amdgpu_irq_src_funcs sdma_v2_4_trap_irq_funcs = {
  1138. .set = sdma_v2_4_set_trap_irq_state,
  1139. .process = sdma_v2_4_process_trap_irq,
  1140. };
  1141. static const struct amdgpu_irq_src_funcs sdma_v2_4_illegal_inst_irq_funcs = {
  1142. .process = sdma_v2_4_process_illegal_inst_irq,
  1143. };
  1144. static void sdma_v2_4_set_irq_funcs(struct amdgpu_device *adev)
  1145. {
  1146. adev->sdma.trap_irq.num_types = AMDGPU_SDMA_IRQ_LAST;
  1147. adev->sdma.trap_irq.funcs = &sdma_v2_4_trap_irq_funcs;
  1148. adev->sdma.illegal_inst_irq.funcs = &sdma_v2_4_illegal_inst_irq_funcs;
  1149. }
  1150. /**
  1151. * sdma_v2_4_emit_copy_buffer - copy buffer using the sDMA engine
  1152. *
  1153. * @ring: amdgpu_ring structure holding ring information
  1154. * @src_offset: src GPU address
  1155. * @dst_offset: dst GPU address
  1156. * @byte_count: number of bytes to xfer
  1157. *
  1158. * Copy GPU buffers using the DMA engine (VI).
  1159. * Used by the amdgpu ttm implementation to move pages if
  1160. * registered as the asic copy callback.
  1161. */
  1162. static void sdma_v2_4_emit_copy_buffer(struct amdgpu_ib *ib,
  1163. uint64_t src_offset,
  1164. uint64_t dst_offset,
  1165. uint32_t byte_count)
  1166. {
  1167. ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
  1168. SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
  1169. ib->ptr[ib->length_dw++] = byte_count;
  1170. ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
  1171. ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
  1172. ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
  1173. ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
  1174. ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
  1175. }
  1176. /**
  1177. * sdma_v2_4_emit_fill_buffer - fill buffer using the sDMA engine
  1178. *
  1179. * @ring: amdgpu_ring structure holding ring information
  1180. * @src_data: value to write to buffer
  1181. * @dst_offset: dst GPU address
  1182. * @byte_count: number of bytes to xfer
  1183. *
  1184. * Fill GPU buffers using the DMA engine (VI).
  1185. */
  1186. static void sdma_v2_4_emit_fill_buffer(struct amdgpu_ib *ib,
  1187. uint32_t src_data,
  1188. uint64_t dst_offset,
  1189. uint32_t byte_count)
  1190. {
  1191. ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
  1192. ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
  1193. ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
  1194. ib->ptr[ib->length_dw++] = src_data;
  1195. ib->ptr[ib->length_dw++] = byte_count;
  1196. }
  1197. static const struct amdgpu_buffer_funcs sdma_v2_4_buffer_funcs = {
  1198. .copy_max_bytes = 0x1fffff,
  1199. .copy_num_dw = 7,
  1200. .emit_copy_buffer = sdma_v2_4_emit_copy_buffer,
  1201. .fill_max_bytes = 0x1fffff,
  1202. .fill_num_dw = 7,
  1203. .emit_fill_buffer = sdma_v2_4_emit_fill_buffer,
  1204. };
  1205. static void sdma_v2_4_set_buffer_funcs(struct amdgpu_device *adev)
  1206. {
  1207. if (adev->mman.buffer_funcs == NULL) {
  1208. adev->mman.buffer_funcs = &sdma_v2_4_buffer_funcs;
  1209. adev->mman.buffer_funcs_ring = &adev->sdma.instance[0].ring;
  1210. }
  1211. }
  1212. static const struct amdgpu_vm_pte_funcs sdma_v2_4_vm_pte_funcs = {
  1213. .copy_pte = sdma_v2_4_vm_copy_pte,
  1214. .write_pte = sdma_v2_4_vm_write_pte,
  1215. .set_pte_pde = sdma_v2_4_vm_set_pte_pde,
  1216. .pad_ib = sdma_v2_4_vm_pad_ib,
  1217. };
  1218. static void sdma_v2_4_set_vm_pte_funcs(struct amdgpu_device *adev)
  1219. {
  1220. if (adev->vm_manager.vm_pte_funcs == NULL) {
  1221. adev->vm_manager.vm_pte_funcs = &sdma_v2_4_vm_pte_funcs;
  1222. adev->vm_manager.vm_pte_funcs_ring = &adev->sdma.instance[0].ring;
  1223. adev->vm_manager.vm_pte_funcs_ring->is_pte_ring = true;
  1224. }
  1225. }