sdma_v2_4.c 39 KB

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